Ejemplo n.º 1
0
 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')
Ejemplo n.º 2
0
 def testBattOrDictFromFileMapping(self):
   try:
     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)
   finally:
     os.remove(filename)
   self.assertEquals(a1, '/dev/ttyUSB1')
   self.assertEquals(a2, '/dev/ttyUSB2')
   self.assertEquals(a3, '/dev/ttyUSB3')
Ejemplo n.º 3
0
    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')