1# Copyright (c) 2023 Nordic Semiconductor ASA 2# 3# SPDX-License-Identifier: Apache-2.0 4 5from __future__ import annotations 6 7import logging 8from typing import Type 9 10from twister_harness.device.device_adapter import DeviceAdapter 11from twister_harness.device.hardware_adapter import HardwareAdapter 12from twister_harness.device.qemu_adapter import QemuAdapter 13from twister_harness.device.binary_adapter import ( 14 CustomSimulatorAdapter, 15 NativeSimulatorAdapter, 16 UnitSimulatorAdapter, 17) 18from twister_harness.exceptions import TwisterHarnessException 19 20logger = logging.getLogger(__name__) 21 22 23class DeviceFactory: 24 _devices: dict[str, Type[DeviceAdapter]] = {} 25 26 @classmethod 27 def discover(cls): 28 """Return available devices.""" 29 30 @classmethod 31 def register_device_class(cls, name: str, klass: Type[DeviceAdapter]): 32 if name not in cls._devices: 33 cls._devices[name] = klass 34 35 @classmethod 36 def get_device(cls, name: str) -> Type[DeviceAdapter]: 37 logger.debug('Get device type "%s"', name) 38 try: 39 return cls._devices[name] 40 except KeyError as exc: 41 logger.error('There is no device with name "%s"', name) 42 raise TwisterHarnessException(f'There is no device with name "{name}"') from exc 43 44 45DeviceFactory.register_device_class('custom', CustomSimulatorAdapter) 46DeviceFactory.register_device_class('native', NativeSimulatorAdapter) 47DeviceFactory.register_device_class('unit', UnitSimulatorAdapter) 48DeviceFactory.register_device_class('hardware', HardwareAdapter) 49DeviceFactory.register_device_class('qemu', QemuAdapter) 50