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': 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 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 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 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'
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()
def testGetBattors(self): bd = find_usb_devices.GetBusNumberToDeviceTreeMap() self.assertEquals( battor_device_mapping.GetBattorList(bd), ['ttyUSB0', 'ttyUSB1', 'ttyUSB2', 'ttyUSB3', 'ttyUSB4'])