Beispiel #1
0
def GetRemoteDevices(banner, endpoints, default_timeout_ms, auth_timeout_ms,
                     on_error=None, as_root=False):
  """Returns the list of devices available.

  Caller MUST call CloseDevices(devices) on the return value or call .Close() on
  each element to close the TCP handles.

  Arguments:
  - banner: authentication banner associated with the RSA keys. It's better to
        use a constant.
  - endpoints: list of ip[:port] endpoints of devices to connect to via TCP.
  - default_timeout_ms: default I/O operation timeout.
  - auth_timeout_ms: timeout for the user to accept the public key.
  - on_error: callback when an internal failure occurs.
  - as_root: if True, restarts adbd as root if possible.

  Returns one of:
    - list of HighDevice instances.
    - None if adb is unavailable.
  """
  with _ADB_KEYS_LOCK:
    if not _ADB_KEYS:
      return []
  # Create unopened handles for all remote devices.
  handles = [common.TcpHandle(endpoint) for endpoint in endpoints]

  return _ConnectFromHandles(handles, banner=banner,
                             default_timeout_ms=default_timeout_ms,
                             auth_timeout_ms=auth_timeout_ms, on_error=on_error,
                             as_root=as_root)
Beispiel #2
0
    def _Find(self, use_serial):
        """Initializes self._handle from self.port_path.

    The handle is left unopened.
    """
        assert not self._handle
        assert not self._adb_cmd
        # TODO(maruel): Add support for TCP/IP communication.
        try:
            if self.port_path:
                previous_port_path = self._port_path
                if use_serial:
                    assert self._serial
                    self._handle = common.UsbHandle.Find(
                        adb_commands.DeviceIsAvailable,
                        serial=self._serial,
                        timeout_ms=self._default_timeout_ms)
                    # Update the new found port path.
                    self._port_path = self._handle.port_path_str
                else:
                    self._handle = common.UsbHandle.Find(
                        adb_commands.DeviceIsAvailable,
                        port_path=self.port_path,
                        timeout_ms=self._default_timeout_ms)
                _LOG.info('%s._Find(%s) %s = %s', previous_port_path,
                          use_serial, self._serial,
                          self.port_path if self._handle else 'None')
            else:
                self._handle = common.TcpHandle(self._serial)
        except (common.usb1.USBError, usb_exceptions.DeviceNotFoundError) as e:
            _LOG.debug('%s._Find(%s) %s : %s', self.port_path, use_serial,
                       self._serial, e)
        return bool(self._handle)
Beispiel #3
0
    def ConnectDevice(self,
                      port_path=None,
                      serial=None,
                      default_timeout_ms=None,
                      **kwargs):
        """Convenience function to setup a transport handle for the adb device from
             usb path or serial then connect to it.

        Args:
          port_path: The filename of usb port to use.
          serial: The serial number of the device to use.
          default_timeout_ms: The default timeout in milliseconds to use.
          kwargs: handle: Device handle to use (instance of common.TcpHandle or common.UsbHandle)
                  banner: Connection banner to pass to the remote device
                  rsa_keys: List of AuthSigner subclass instances to be used for
                      authentication. The device can either accept one of these via the Sign
                      method, or we will send the result of GetPublicKey from the first one
                      if the device doesn't accept any of them.
                  auth_timeout_ms: Timeout to wait for when sending a new public key. This
                      is only relevant when we send a new public key. The device shows a
                      dialog and this timeout is how long to wait for that dialog. If used
                      in automation, this should be low to catch such a case as a failure
                      quickly; while in interactive settings it should be high to allow
                      users to accept the dialog. We default to automation here, so it's low
                      by default.

        If serial specifies a TCP address:port, then a TCP connection is
        used instead of a USB connection.
        """

        # If there isnt a handle override (used by tests), build one here
        if 'handle' in kwargs:
            self._handle = kwargs.pop('handle')
        else:
            # if necessary, convert serial to a unicode string
            if isinstance(serial, (bytes, bytearray)):
                serial = serial.decode('utf-8')

            if serial and ':' in serial:
                self._handle = common.TcpHandle(serial,
                                                timeout_ms=default_timeout_ms)
            else:
                self._handle = common.UsbHandle.FindAndOpen(
                    DeviceIsAvailable,
                    port_path=port_path,
                    serial=serial,
                    timeout_ms=default_timeout_ms)

        self._Connect(**kwargs)

        return self
Beispiel #4
0
def FindDevice(on_error, serial, timeout_ms=1000):  # pragma: no cover
    """Finds a device with serial if relevant, otherwise take the device connect
  if there is exactly one.
  """
    # Find the usb path for this device, so the next operations are done with
    # the usb path.
    if ':' in serial:
        handle = common.TcpHandle(serial, timeout_ms=timeout_ms)
    else:
        handle = common.UsbHandle.Find(adb_commands_safe.DeviceIsAvailable,
                                       serial=serial,
                                       timeout_ms=timeout_ms)
    if handle:
        path = '/'.join(map(str, handle.port_path))
        logging.info('Automatically selected %s : %s', path, serial)
        return path
    on_error('Didn\'t find device with serial %s' % serial)
Beispiel #5
0
  def ConnectDevice(
      cls, port_path=None, serial=None, default_timeout_ms=None, **kwargs):
    """Convenience function to get an adb device from usb path or serial.

    Args:
      port_path: The filename of usb port to use.
      serial: The serial number of the device to use.
      default_timeout_ms: The default timeout in milliseconds to use.

    If serial specifies a TCP address:port, then a TCP connection is
    used instead of a USB connection.
    """
    if serial and ':' in serial:
        handle = common.TcpHandle(serial)
    else:
        handle = common.UsbHandle.FindAndOpen(
            DeviceIsAvailable, port_path=port_path, serial=serial,
            timeout_ms=default_timeout_ms)
    return cls.Connect(handle, **kwargs)