Exemplo n.º 1
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 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')
Exemplo n.º 2
0
 def testDeviceDescriptions(self):
     bd = find_usb_devices.GetBusNumberToDeviceTreeMap(fast=False)
     dev_foo = bd[1].FindDeviceNumber(11)
     dev_bar = bd[1].FindDeviceNumber(12)
     dev_usb_device_p7_h1_t0 = bd[2].FindDeviceNumber(21)
     self.assertEquals(dev_foo.desc, 'foo')
     self.assertEquals(dev_bar.desc, 'bar')
     self.assertEquals(dev_usb_device_p7_h1_t0.desc,
                       'ID 0403:6001 usb_device_p7_h1_t0')
Exemplo n.º 3
0
 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
Exemplo n.º 4
0
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 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'
Exemplo n.º 7
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']:
            # 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 GenerateSerialMap(hub_types=None):
  """Generates a map of phone serial numbers to BattOr serial numbers.

  Generates a dict of:
  {<phone serial 1>: <battor serial 1>,
   <phone serial 2>: <battor serial 2>}
  indicating which phone serial numbers should be matched with
  which BattOr serial numbers. Mapping is based on the physical port numbers
  of the hubs that the BattOrs and phones are connected to.

  Args:
      hub_types: List of hub types to check for.
      Defaults to ['plugable_7port',
                   'plugable_7port_usb3_part2',
                   'plugable_7port_usb3_part3']
      (see usb_hubs.py for details)
  """
  hub_types = [usb_hubs.GetHubType(x)
               for x in hub_types or ['plugable_7port',
                                      'plugable_7port_usb3_part2',
                                      'plugable_7port_usb3_part3']]
  devtree = find_usb_devices.GetBusNumberToDeviceTreeMap()

  # List of serial numbers in the system that represent BattOrs.
  battor_serials = list(GetBattorSerialNumbers(devtree))

  # If there's only one BattOr in the system, then a serial number ma
  # is not necessary.
  if len(battor_serials) == 1:
    return {}

  # List of dictionaries, one for each hub, that maps the physical
  # port number to the serial number of that hub. For instance, in a 2
  # hub system, this could return [{1:'ab', 2:'cd'}, {1:'jkl', 2:'xyz'}]
  # where 'ab' and 'cd' are the phone serial numbers and 'jkl' and 'xyz'
  # are the BattOr serial numbers.
  port_to_serial = find_usb_devices.GetAllPhysicalPortToSerialMaps(
      hub_types, device_tree_map=devtree)

  class serials(object):
    def __init__(self):
      self.phone = None
      self.battor = None

  # Map of {physical port number: [phone serial #, BattOr serial #]. This
  # map is populated by executing the code below. For instance, in the above
  # example, after the code below is executed, port_to_devices would equal
  # {1: ['ab', 'jkl'], 2: ['cd', 'xyz']}
  port_to_devices = collections.defaultdict(serials)
  for hub in port_to_serial:
    for (port, serial) in hub.iteritems():
      if serial in battor_serials:
        if port_to_devices[port].battor is not None:
          raise battor_error.BattorError('Multiple BattOrs on same port number')
        else:
          port_to_devices[port].battor = serial
      else:
        if port_to_devices[port].phone is not None:
          raise battor_error.BattorError('Multiple phones on same port number')
        else:
          port_to_devices[port].phone = serial

  # Turn the port_to_devices map into a map of the form
  # {phone serial number: BattOr serial number}.
  result = {}
  for pair in port_to_devices.values():
    if pair.phone is None:
      raise battor_error.BattorError(
          'BattOr detected with no corresponding phone')
    if pair.battor is None:
      raise battor_error.BattorError(
          'Phone detected with no corresponding BattOr')
    result[pair.phone] = pair.battor
  return result
Exemplo n.º 9
0
 def testGetBattors(self):
     bd = find_usb_devices.GetBusNumberToDeviceTreeMap()
     self.assertEquals(
         battor_device_mapping.GetBattorList(bd),
         ['ttyUSB0', 'ttyUSB1', 'ttyUSB2', 'ttyUSB3', 'ttyUSB4'])
Exemplo n.º 10
0
 def testIsBattor(self):
     bd = find_usb_devices.GetBusNumberToDeviceTreeMap()
     self.assertTrue(battor_device_mapping.IsBattor('ttyUSB3', bd))
     self.assertFalse(battor_device_mapping.IsBattor('ttyUSB5', bd))
Exemplo n.º 11
0
def GetBattorPathFromPhoneSerial(serial,
                                 serial_map=None,
                                 serial_map_file=None):
    """Gets the TTY path (e.g. '/dev/ttyUSB0')  to communicate with the BattOr.

  (1) If serial_map is given, it is treated as a dictionary mapping
  phone serial numbers to BattOr serial numbers. This function will get the
  TTY path for the given BattOr serial number.

  (2) If serial_map_file is given, it is treated as the name of a
  phone-to-BattOr mapping file (generated with GenerateSerialMapFile)
  and this will be loaded and used as the dict to map port numbers to
  BattOr serial numbers.

  You can only give one of serial_map and serial_map_file.

  Args:
    serial: Serial number of phone connected on the same physical port that
    the BattOr is connected to.
    serial_map: Map of phone serial numbers to BattOr serial numbers, given
    as a dictionary.
    serial_map_file: Map of phone serial numbers to BattOr serial numbers,
    given as a file.
    hub_types: List of hub types to check for. Used only if serial_map_file
    is None.

  Returns:
    Device string used to communicate with device.

  Raises:
    ValueError: If serial number is not given.
    BattorError: If BattOr not found or unexpected USB topology.
  """
    # If there's only one BattOr connected to the system, just use that one.
    # This allows for use on, e.g., a developer's workstation with no hubs.
    devtree = find_usb_devices.GetBusNumberToDeviceTreeMap()
    all_battors = GetBattorList(devtree)
    if len(all_battors) == 1:
        return '/dev/' + all_battors[0]

    if not serial:
        raise battor_error.BattorError(
            'Two or more BattOrs connected, no serial provided')

    if serial_map and serial_map_file:
        raise ValueError('Cannot specify both serial_map and serial_map_file')

    if serial_map_file:
        serial_map = ReadSerialMapFile(serial_map_file)

    tty_string = _PhoneToPathMap(serial, serial_map, devtree)

    if not tty_string:
        raise battor_error.BattorError(
            'No device with given serial number detected.')

    if IsBattor(tty_string, devtree):
        return '/dev/' + tty_string
    else:
        raise battor_error.BattorError(
            'Device with given serial number is not a BattOr.')