• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3
4#
5# Copyright (c) 2022 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19from dataclasses import dataclass
20from enum import Enum
21from enum import unique
22
23
24@unique
25class TestDeviceState(Enum):
26    FASTBOOT = "FASTBOOT"
27    ONLINE = "ONLINE"
28    RECOVERY = "RECOVERY"
29    NOT_AVAILABLE = "NOT_AVAILABLE"
30
31    @staticmethod
32    def get_test_device_state(device_state):
33        if device_state is None:
34            return TestDeviceState.NOT_AVAILABLE
35        elif device_state == DeviceState.ONLINE:
36            return TestDeviceState.ONLINE
37        elif device_state == DeviceState.CONNECTED:
38            return TestDeviceState.ONLINE
39        elif device_state == DeviceState.OFFLINE:
40            return TestDeviceState.NOT_AVAILABLE
41        elif device_state == DeviceState.RECOVERY:
42            return TestDeviceState.RECOVERY
43        elif device_state == DeviceState.BOOTLOADER:
44            return TestDeviceState.FASTBOOT
45        else:
46            return TestDeviceState.NOT_AVAILABLE
47
48
49@unique
50class DeviceState(Enum):
51    BOOTLOADER = "bootloader"
52    OFFLINE = "offline"
53    ONLINE = "device"
54    CONNECTED = "connected"
55    RECOVERY = "recovery"
56
57    @staticmethod
58    def get_state(state):
59        for device_state in DeviceState:
60            if device_state.value == state.lower():
61                return device_state
62        return None
63
64
65@unique
66class DeviceEvent(Enum):
67    """
68    Represents a test device event that can change allocation state
69    """
70    CONNECTED_ONLINE = "CONNECTED_ONLINE"
71    CONNECTED_OFFLINE = "CONNECTED_OFFLINE"
72    STATE_CHANGE_ONLINE = "STATE_CHANGE_ONLINE"
73    STATE_CHANGE_OFFLINE = "STATE_CHANGE_OFFLINE"
74    DISCONNECTED = "DISCONNECTED"
75    FORCE_AVAILABLE = "FORCE_AVAILABLE"
76    AVAILABLE_CHECK_PASSED = "AVAILABLE_CHECK_PASSED"
77    AVAILABLE_CHECK_FAILED = "AVAILABLE_CHECK_FAILED"
78    AVAILABLE_CHECK_IGNORED = "AVAILABLE_CHECK_IGNORED"
79    ALLOCATE_REQUEST = "ALLOCATE_REQUEST"
80    FORCE_ALLOCATE_REQUEST = "FORCE_ALLOCATE_REQUEST"
81    FREE_AVAILABLE = "FREE_AVAILABLE"
82    FREE_UNRESPONSIVE = "FREE_UNRESPONSIVE"
83    FREE_UNAVAILABLE = "FREE_UNAVAILABLE"
84    FREE_UNKNOWN = "FREE_UNKNOWN"
85
86
87def handle_allocation_event(old_state, event):
88    new_state = None
89    if event == DeviceEvent.CONNECTED_ONLINE \
90            or event == DeviceEvent.STATE_CHANGE_ONLINE:
91        if old_state == DeviceAllocationState.allocated:
92            new_state = old_state
93        else:
94            new_state = DeviceAllocationState.checking_availability
95    elif event == DeviceEvent.CONNECTED_OFFLINE \
96            or event == DeviceEvent.STATE_CHANGE_OFFLINE \
97            or event == DeviceEvent.DISCONNECTED:
98        if old_state == DeviceAllocationState.allocated:
99            new_state = old_state
100        else:
101            new_state = DeviceAllocationState.unknown
102    elif event == DeviceEvent.ALLOCATE_REQUEST:
103        new_state = DeviceAllocationState.allocated
104    elif event == DeviceEvent.FREE_AVAILABLE:
105        new_state = DeviceAllocationState.available
106    elif event == DeviceEvent.FREE_UNAVAILABLE:
107        new_state = DeviceAllocationState.unknown
108    elif event == DeviceEvent.AVAILABLE_CHECK_IGNORED:
109        new_state = DeviceAllocationState.ignored
110    elif event == DeviceEvent.AVAILABLE_CHECK_PASSED:
111        new_state = DeviceAllocationState.available
112
113    return new_state
114
115
116@dataclass
117class DeviceAllocationState:
118    ignored = "Ignored"
119    available = "Available"
120    allocated = "Allocated"
121    checking_availability = "Checking_Availability"
122    unavailable = "Unavailable"
123    unusable = "Unusable"
124    unknown = "unknown"
125