fd33bb16e4533f0c603df3f8f5c51b6b8df7d4a7
[platform/upstream/hailort.git] /
1 #!/usr/bin/env python
2 from enum import Enum
3
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
8
9 logger = default_logger()
10
11 class HailoCLITargets(Enum):
12     udp = 'udp'
13     pcie = 'pcie'
14
15
16 class HailoDeviceCmdUtil(CmdUtilsBaseUtil):
17     """
18     Base class for any cmd utility that use a specific hailo device
19     """
20     def __init__(self, args_parser, set_target_args=True):
21         super().__init__(args_parser)
22         self._parser = args_parser
23         if set_target_args:
24             self.add_target_args(args_parser)
25
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)
31         else:
32             try:
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))
37                 raise
38
39     def get_target_type(self, args):
40         if args.target is not None:
41             return args.target
42
43         if args.ip is not None:
44             # If IP is given, we assume that the target is udp
45             return HailoCLITargets.udp.value
46
47         # Otherwise the default target is pcie
48         return HailoCLITargets.pcie.value
49
50
51     def validate_args(self, args):
52         if args.target == HailoCLITargets.udp.value:
53             if not args.ip:
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")
57
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))
63
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)