def testBattorDictMapping(self): map_dict = { 'Phone1': 'Battor1', 'Phone2': 'Battor2', 'Phone3': 'Battor3' } a1 = battor_device_mapping.GetBattorPathFromPhoneSerial( 'Phone1', serial_map=map_dict) a2 = battor_device_mapping.GetBattorPathFromPhoneSerial( 'Phone2', serial_map=map_dict) a3 = battor_device_mapping.GetBattorPathFromPhoneSerial( 'Phone3', serial_map=map_dict) self.assertEquals(a1, '/dev/ttyUSB1') self.assertEquals(a2, '/dev/ttyUSB2') self.assertEquals(a3, '/dev/ttyUSB3')
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 testBattorDictFromFileMapping(self): map_dict = { 'Phone1': 'Battor1', 'Phone2': 'Battor2', 'Phone3': 'Battor3' } curr_dir = os.path.dirname(os.path.realpath(__file__)) filename = os.path.join(curr_dir, 'test', 'data', 'test_write_map.json') battor_device_mapping.WriteSerialMapFile(filename, map_dict) a1 = battor_device_mapping.GetBattorPathFromPhoneSerial( 'Phone1', serial_map_file=filename) a2 = battor_device_mapping.GetBattorPathFromPhoneSerial( 'Phone2', serial_map_file=filename) a3 = battor_device_mapping.GetBattorPathFromPhoneSerial( 'Phone3', serial_map_file=filename) self.assertEquals(a1, '/dev/ttyUSB1') self.assertEquals(a2, '/dev/ttyUSB2') self.assertEquals(a3, '/dev/ttyUSB3')
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()