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 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 test_generate_serial_map(self): setup_battor_test([{1:'Phn1', 2:'Phn2', 3:'Phn3'}, {1:'Bat1', 2:'Bat2', 3:'Bat3'}], [{}, {1:'ttyUSB0', 2:'ttyUSB1', 3:'ttyUSB2'}], ['ttyUSB0', 'ttyUSB1', 'ttyUSB2'], ['Bat1', 'Bat2', 'Bat3']) result = battor_device_mapping.GenerateSerialMap() self.assertEqual(len(result), 3) self.assertEqual(result['Phn1'], 'Bat1') self.assertEqual(result['Phn2'], 'Bat2') self.assertEqual(result['Phn3'], 'Bat3')
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 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( ) except battor_error.BattorError: 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': # TODO(rnephew): When we have a BattOr that can attach to mac, find a way # to detect BattOrs on mac. 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']: # TODO: We need a way to automatically detect correct port. # crbug.com/60397 return 'COM3' 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 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( 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()