1#!/usr/bin/env python3 2# coding=utf-8 3 4# 5# Copyright (c) 2021 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.OFFLINE: 38 return TestDeviceState.NOT_AVAILABLE 39 elif device_state == DeviceState.RECOVERY: 40 return TestDeviceState.RECOVERY 41 elif device_state == DeviceState.BOOTLOADER: 42 return TestDeviceState.FASTBOOT 43 else: 44 return TestDeviceState.NOT_AVAILABLE 45 46 47@unique 48class DeviceState(Enum): 49 BOOTLOADER = "bootloader" 50 OFFLINE = "offline" 51 ONLINE = "device" 52 RECOVERY = "recovery" 53 54 @staticmethod 55 def get_state(state): 56 for device_state in DeviceState: 57 if device_state.value == state: 58 return device_state 59 return None 60 61 62@unique 63class DeviceEvent(Enum): 64 """ 65 Represents a test device event that can change allocation state 66 """ 67 CONNECTED_ONLINE = "CONNECTED_ONLINE" 68 CONNECTED_OFFLINE = "CONNECTED_OFFLINE" 69 STATE_CHANGE_ONLINE = "STATE_CHANGE_ONLINE" 70 STATE_CHANGE_OFFLINE = "STATE_CHANGE_OFFLINE" 71 DISCONNECTED = "DISCONNECTED" 72 FORCE_AVAILABLE = "FORCE_AVAILABLE" 73 AVAILABLE_CHECK_PASSED = "AVAILABLE_CHECK_PASSED" 74 AVAILABLE_CHECK_FAILED = "AVAILABLE_CHECK_FAILED" 75 AVAILABLE_CHECK_IGNORED = "AVAILABLE_CHECK_IGNORED" 76 ALLOCATE_REQUEST = "ALLOCATE_REQUEST" 77 FORCE_ALLOCATE_REQUEST = "FORCE_ALLOCATE_REQUEST" 78 FREE_AVAILABLE = "FREE_AVAILABLE" 79 FREE_UNRESPONSIVE = "FREE_UNRESPONSIVE" 80 FREE_UNAVAILABLE = "FREE_UNAVAILABLE" 81 FREE_UNKNOWN = "FREE_UNKNOWN" 82 83 84def handle_allocation_event(old_state, event): 85 new_state = None 86 if event == DeviceEvent.CONNECTED_ONLINE \ 87 or event == DeviceEvent.STATE_CHANGE_ONLINE: 88 if old_state == DeviceAllocationState.allocated: 89 new_state = old_state 90 else: 91 new_state = DeviceAllocationState.checking_availability 92 elif event == DeviceEvent.CONNECTED_OFFLINE \ 93 or event == DeviceEvent.STATE_CHANGE_OFFLINE \ 94 or event == DeviceEvent.DISCONNECTED: 95 if old_state == DeviceAllocationState.allocated: 96 new_state = old_state 97 else: 98 new_state = DeviceAllocationState.unknown 99 elif event == DeviceEvent.ALLOCATE_REQUEST: 100 new_state = DeviceAllocationState.allocated 101 elif event == DeviceEvent.FREE_AVAILABLE: 102 new_state = DeviceAllocationState.available 103 elif event == DeviceEvent.FREE_UNAVAILABLE: 104 new_state = DeviceAllocationState.unknown 105 elif event == DeviceEvent.AVAILABLE_CHECK_IGNORED: 106 new_state = DeviceAllocationState.ignored 107 elif event == DeviceEvent.AVAILABLE_CHECK_PASSED: 108 new_state = DeviceAllocationState.available 109 110 return new_state 111 112 113@dataclass 114class DeviceAllocationState: 115 ignored = "Ignored" 116 available = "Available" 117 allocated = "Allocated" 118 checking_availability = "Checking_Availability" 119 unavailable = "Unavailable" 120 unknown = "unknown" 121