def IsBattOrConnected(test_platform, android_device=None, android_device_map=None, android_device_file=None): """Returns True if BattOr is detected.""" if test_platform == 'android': if not android_device: raise ValueError( 'Must pass android device serial when determining ' 'support on android platform') if not android_device_map: device_tree = find_usb_devices.GetBusNumberToDeviceTreeMap() if device_tree: logging.warning('Device tree:') for _, node in sorted(device_tree.iteritems()): node.Display() else: logging.warning('Empty device tree.') if len(battor_device_mapping.GetBattOrList(device_tree)) == 1: return True if android_device_file: android_device_map = battor_device_mapping.ReadSerialMapFile( android_device_file) else: try: android_device_map = battor_device_mapping.GenerateSerialMap( ) logging.warning('Android device map: %s', android_device_map) except battor_error.BattOrError: logging.exception('Error generating serial map') return False # If neither if statement above is triggered, it means that an # android_device_map was passed in and will be used. return str(android_device) in android_device_map elif test_platform == 'win': for (_1, desc, _2) in serial.tools.list_ports.comports(): if 'USB Serial Port' in desc: return True logging.info('No usb serial port discovered. Available ones are: %s' % list(serial.tools.list_ports.comports())) return False elif test_platform == 'mac': for (_1, desc, _2) in serial.tools.list_ports.comports(): if 'BattOr' in desc: return True return False elif test_platform == 'linux': device_tree = find_usb_devices.GetBusNumberToDeviceTreeMap(fast=True) return bool(battor_device_mapping.GetBattOrList(device_tree)) return False
def _GetBattOrPath(self, target_platform, android_device=None, battor_path=None, battor_map_file=None, battor_map=None): """Determines most likely path to the correct BattOr.""" if target_platform not in self._SUPPORTED_PLATFORMS: raise battor_error.BattOrError('%s is an unsupported platform.' % target_platform) if target_platform in ['win']: # Right now, the BattOr agent binary isn't able to automatically detect # the BattOr port on Windows. To get around this, we know that the BattOr # shows up with a name of 'USB Serial Port', so use the COM port that # corresponds to a presentation.device with that name. for (port, desc, _) in serial.tools.list_ports.comports(): if 'USB Serial Port' in desc: return port raise battor_error.BattOrError( 'Could not find BattOr attached to machine.') if target_platform in ['mac']: for (port, desc, _) in serial.tools.list_ports.comports(): if 'BattOr' in desc: return port if target_platform in ['android', 'linux']: device_tree = find_usb_devices.GetBusNumberToDeviceTreeMap( fast=True) if battor_path: if not isinstance(battor_path, basestring): raise battor_error.BattOrError( 'An invalid BattOr path was specified.') return battor_path if target_platform == 'android': if not android_device: raise battor_error.BattOrError( 'Must specify presentation.device for Android platform.' ) if not battor_map_file and not battor_map: # No map was passed, so must create one. battor_map = battor_device_mapping.GenerateSerialMap() return battor_device_mapping.GetBattOrPathFromPhoneSerial( str(android_device), serial_map_file=battor_map_file, serial_map=battor_map) # Not Android and no explicitly passed BattOr. battors = battor_device_mapping.GetBattOrList(device_tree) if len(battors) != 1: raise battor_error.BattOrError( 'For non-Android platforms, exactly one BattOr must be ' 'attached unless address is explicitly given.') return '/dev/%s' % battors.pop() raise NotImplementedError( 'BattOr Wrapper not implemented for given platform')
def _FindBattOrPath(config): battor_path = config.battor_path if not config.battor_path and not config.serial_map: device_tree = find_usb_devices.GetBusNumberToDeviceTreeMap() battors = battor_device_mapping.GetBattOrList(device_tree) assert len(battors) == 1, ('Must specify BattOr path if there is not ' 'exactly one') battor_path = battors[0] return battor_path
def setUp(self): self._platform = py_utils.GetHostOsName() self._battor_list = None if self._platform == 'linux': device_tree = find_usb_devices.GetBusNumberToDeviceTreeMap() self._battor_list = battor_device_mapping.GetBattOrList( device_tree) if not battor_wrapper.IsBattOrConnected(self._platform): self._battor_list = []
def setUp(self): test_platform = platform.system() self._battor_list = None if 'Win' in test_platform: self._platform = 'win' elif 'Linux' in test_platform: self._platform = 'linux' device_tree = find_usb_devices.GetBusNumberToDeviceTreeMap() self._battor_list = battor_device_mapping.GetBattOrList( device_tree) elif 'Darwin' in test_platform: self._platform = 'mac' if not battor_wrapper.IsBattOrConnected(self._platform): self._battor_list = []
def testGetBattOrs(self): bd = find_usb_devices.GetBusNumberToDeviceTreeMap() self.assertEquals( battor_device_mapping.GetBattOrList(bd), ['ttyUSB0', 'ttyUSB1', 'ttyUSB2', 'ttyUSB3', 'ttyUSB4'])