4 from hailo_platform.common.tools.cmd_utils.base_utils import CmdUtilsBaseUtil
5 from hailo_platform.drivers.hw_object import EthernetDevice, PcieDevice
6 from hailo_platform.common.logger.logger import default_logger
7 from hailo_platform.drivers.hailort.pyhailort import PcieDeviceInfo, InternalPcieDevice
9 logger = default_logger()
11 class HailoCLITargets(Enum):
16 class HailoDeviceCmdUtil(CmdUtilsBaseUtil):
18 Base class for any cmd utility that use a specific hailo device
20 def __init__(self, args_parser, set_target_args=True):
21 super().__init__(args_parser)
22 self._parser = args_parser
24 self.add_target_args(args_parser)
26 def get_target(self, args):
27 self.validate_args(args)
28 target_type = self.get_target_type(args)
29 if target_type == HailoCLITargets.udp.value:
30 return EthernetDevice(args.ip)
33 return PcieDevice(device_info=args.board_location)
34 except Exception as e:
35 logger.error('Internal PCIe error (No PCIe device connected?)')
36 logger.error('Error code: {}'.format(e))
39 def get_target_type(self, args):
40 if args.target is not None:
43 if args.ip is not None:
44 # If IP is given, we assume that the target is udp
45 return HailoCLITargets.udp.value
47 # Otherwise the default target is pcie
48 return HailoCLITargets.pcie.value
51 def validate_args(self, args):
52 if args.target == HailoCLITargets.udp.value:
54 self._parser.error('When using --target udp, you must supply --ip')
55 if args.board_location:
56 self._parser.error("When using --target udp, you must not supply --board-location")
58 if args.board_location:
59 all_devices = InternalPcieDevice.scan_devices()
60 if args.board_location not in all_devices:
61 self._parser.error('Device {} does not appear on your host, please run `hailo scan -d pcie` to see all available devices'
62 .format(args.board_location))
64 def add_target_args(self, args_parser):
65 args_parser.add_argument('--target', type=str, choices=[t.value for t in HailoCLITargets],
66 default=None, help='Device type to use')
67 args_parser.add_argument('--ip', type=str, default=None, help='IP address of the target (udp)')
68 args_parser.add_argument('-s', '--board-location', help=PcieDeviceInfo.BOARD_LOCATION_HELP_STRING,
69 type=PcieDeviceInfo.argument_type)