adb.adb_protocol module

ADB protocol implementation.

Implements the ADB protocol as seen in android’s adb/adbd binaries, but only the host side.

Contents

adb.adb_protocol.AUTH_RSAPUBLICKEY = 3

AUTH constants for arg0.

adb.adb_protocol.AUTH_SIGNATURE = 2

AUTH constants for arg0.

adb.adb_protocol.AUTH_TOKEN = 1

AUTH constants for arg0.

class adb.adb_protocol.AdbMessage(command=None, arg0=None, arg1=None, data=b'')[source]

Bases: object

ADB Protocol and message class.

local_id/remote_id

Turns out the documentation is host/device ambidextrous, so local_id is the id for ‘the sender’ and remote_id is for ‘the recipient’. So since we’re only on the host, we’ll re-document with host_id and device_id:

OPEN(host_id, 0, 'shell:XXX')
READY/OKAY(device_id, host_id, '')
WRITE(0, host_id, 'data')
CLOSE(device_id, host_id, '')
_images/adb.adb_protocol.AdbMessage.__init__.CALLER_GRAPH.svg
Parameters
  • command (bytes, None) – One of: [b'SYNC', b'CNXN', b'AUTH', b'OPEN', b'OKAY', b'CLSE', b'WRTE']

  • arg0 (TODO, None) – TODO

  • arg1 (TODO, None) – TODO

  • data (bytes) – TODO

command

The value in AdbMessage.commands that corresponds to the command parameter

Type

int

commands

A dictionary with keys [b'SYNC', b'CNXN', b'AUTH', b'OPEN', b'OKAY', b'CLSE', b'WRTE'].

Type

dict

connections

TODO

Type

int

constants

A dictionary with values [b'SYNC', b'CNXN', b'AUTH', b'OPEN', b'OKAY', b'CLSE', b'WRTE'].

Type

dict

format

The format for unpacking the ADB message.

Type

bytes

ids

[b'SYNC', b'CNXN', b'AUTH', b'OPEN', b'OKAY', b'CLSE', b'WRTE']

Type

list[bytes]

static CalculateChecksum(data)[source]

TODO

_images/adb.adb_protocol.AdbMessage.CalculateChecksum.CALLER_GRAPH.svg
Returns

TODO

Return type

TODO

classmethod Command(usb, service, command='', timeout_ms=None)[source]

One complete set of USB packets for a single command.

Sends service:command in a new connection, reading the data for the response. All the data is held in memory, large responses will be slow and can fill up memory.

_images/adb.adb_protocol.AdbMessage.Command.CALL_GRAPH.svg
Parameters
Returns

The response from the service.

Return type

str

Raises
classmethod Connect(usb, banner=b'notadb', rsa_keys=None, auth_timeout_ms=100)[source]

Establish a new connection to the device.

_images/adb.adb_protocol.AdbMessage.Connect.CALL_GRAPH.svg
Parameters
  • usb (adb.common.TcpHandle, adb.common.UsbHandle) – A adb.common.TcpHandle or adb.common.UsbHandle instance with BulkRead and BulkWrite methods.

  • banner (str) – A string to send as a host identifier.

  • rsa_keys (list[adb_protocol.AuthSigner]) – 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 (int) – 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.

Returns

banner – The device’s reported banner. Always starts with the state (device, recovery, or sideload), sometimes includes information after a : with various product information.

Return type

TODO

Raises
classmethod InteractiveShellCommand(conn, cmd=None, strip_cmd=True, delim=None, strip_delim=True, clean_stdout=True)[source]

Retrieves stdout of the current InteractiveShell and sends a shell command if provided TODO: Should we turn this into a yield based function so we can stream all output?

_images/adb.adb_protocol.AdbMessage.InteractiveShellCommand.CALL_GRAPH.svg
Parameters
  • conn (AdbConnection) – Instance of AdbConnection

  • cmd (str, None) – Command to run on the target.

  • strip_cmd (bool) – Strip command name from stdout.

  • delim (TODO) – Delimiter to look for in the output to know when to stop expecting more output (usually the shell prompt)

  • strip_delim (bool) – Strip the provided delimiter from the output

  • clean_stdout (bool) – Cleanup the stdout stream of any backspaces and the characters that were deleted by the backspace

Returns

stdout – The stdout from the shell command.

Return type

TODO

classmethod Open(usb, destination, timeout_ms=None)[source]

Opens a new connection to the device via an OPEN message.

Not the same as the posix open or any other google3 Open methods.

_images/adb.adb_protocol.AdbMessage.Open.CALL_GRAPH.svg_images/adb.adb_protocol.AdbMessage.Open.CALLER_GRAPH.svg
Parameters
Returns

The local connection id.

Return type

_AdbConnection, None

Raises
Pack()[source]

Returns this message in an over-the-wire format.

_images/adb.adb_protocol.AdbMessage.Pack.CALL_GRAPH.svg_images/adb.adb_protocol.AdbMessage.Pack.CALLER_GRAPH.svg
Returns

TODO

Return type

bytes

classmethod Read(usb, expected_cmds, timeout_ms=None, total_timeout_ms=None)[source]

Receive a response from the device.

_images/adb.adb_protocol.AdbMessage.Read.CALL_GRAPH.svg_images/adb.adb_protocol.AdbMessage.Read.CALLER_GRAPH.svg
Parameters
  • usb (adb.common.TcpHandle, adb.common.UsbHandle) – TODO

  • expected_cmds (TODO) – Read until we receive a header ID that is in expected_cmds

  • timeout_ms (int, None) – Timeout in milliseconds for USB packets.

  • total_timeout_ms (int, None) – The total time to wait for a command in expected_cmds

Returns

  • command (TODO) – TODO

  • arg0 (TODO) – TODO

  • arg1 (TODO) – TODO

  • bytes – TODO

Raises
Send(usb, timeout_ms=None)[source]

Send this message over USB.

_images/adb.adb_protocol.AdbMessage.Send.CALL_GRAPH.svg
Parameters
classmethod StreamingCommand(usb, service, command='', timeout_ms=None)[source]

One complete set of USB packets for a single command.

Sends service:command in a new connection, reading the data for the response. All the data is held in memory, large responses will be slow and can fill up memory.

_images/adb.adb_protocol.AdbMessage.StreamingCommand.CALL_GRAPH.svg_images/adb.adb_protocol.AdbMessage.StreamingCommand.CALLER_GRAPH.svg
Parameters
Yields

str – The responses from the service.

Raises
classmethod Unpack(message)[source]

TODO

_images/adb.adb_protocol.AdbMessage.Unpack.CALLER_GRAPH.svg
Parameters

message (TODO) – TODO

Returns

  • cmd (TODO) – TODO

  • arg0 (TODO) – TODO

  • arg1 (TODO) – TODO

  • data_length (TODO) – TODO

  • data_checksum (TODO) – TODO

  • unused_magic (TODO) – TODO

Raises

ValueError – Unable to unpack the ADB command.

property checksum

TODO

_images/adb.adb_protocol.AdbMessage.checksum.CALL_GRAPH.svg_images/adb.adb_protocol.AdbMessage.checksum.CALLER_GRAPH.svg
Returns

TODO

Return type

TODO

commands = {b'AUTH': 1213486401, b'CLSE': 1163086915, b'CNXN': 1314410051, b'OKAY': 1497451343, b'OPEN': 1313165391, b'SYNC': 1129208147, b'WRTE': 1163154007}
connections = 0
constants = {1129208147: b'SYNC', 1163086915: b'CLSE', 1163154007: b'WRTE', 1213486401: b'AUTH', 1313165391: b'OPEN', 1314410051: b'CNXN', 1497451343: b'OKAY'}
format = b'<6I'
ids = [b'SYNC', b'CNXN', b'AUTH', b'OPEN', b'OKAY', b'CLSE', b'WRTE']
class adb.adb_protocol.AuthSigner[source]

Bases: object

Signer for use with authenticated ADB, introduced in 4.4.x/KitKat.

GetPublicKey()[source]

Returns the public key in PEM format without headers or newlines.

Raises

NotImplementedError – This method is implemented in subclasses.

Sign(data)[source]

Signs given data using a private key.

Parameters

data (bytes) – The data to be signed

Raises

NotImplementedError – This method is implemented in subclasses.

exception adb.adb_protocol.InterleavedDataError[source]

Bases: Exception

We only support command sent serially.

_images/adb.adb_protocol.InterleavedDataError.CALL_GRAPH.svg
exception adb.adb_protocol.InvalidChecksumError[source]

Bases: Exception

Checksum of data didn’t match expected checksum.

_images/adb.adb_protocol.InvalidChecksumError.CALL_GRAPH.svg
exception adb.adb_protocol.InvalidCommandError(message, response_header, response_data)[source]

Bases: Exception

Got an invalid command over USB.

_images/adb.adb_protocol.InvalidCommandError.CALL_GRAPH.svg_images/adb.adb_protocol.InvalidCommandError.__init__.CALLER_GRAPH.svg
exception adb.adb_protocol.InvalidResponseError[source]

Bases: Exception

Got an invalid response to our command.

_images/adb.adb_protocol.InvalidResponseError.CALL_GRAPH.svg
adb.adb_protocol.MAX_ADB_DATA = 4096

Maximum amount of data in an ADB packet.

adb.adb_protocol.MakeWireIDs(ids)[source]

TODO

Parameters

ids (list[bytes]) – TODO

Returns

  • id_to_wire (dict) – TODO

  • wire_to_id (dict) – TODO

adb.adb_protocol.VERSION = 16777216

ADB protocol version.

class adb.adb_protocol._AdbConnection(usb, local_id, remote_id, timeout_ms)[source]

Bases: object

ADB Connection.

_images/adb.adb_protocol._AdbConnection.__init__.CALLER_GRAPH.svg
Parameters
  • usb (adb.common.UsbHandle) – TODO

  • local_id (TODO) – TODO

  • remote_id (TODO) – TODO

  • timeout_ms (int) – Timeout in milliseconds for USB packets.

local_id

The ID for the sender

Type

TODO

remote_id

The ID for the recipient

Type

TODO

timeout_ms

Timeout in milliseconds for USB packets.

Type

int

usb

TODO

Type

adb.common.UsbHandle

Close()[source]

TODO

_images/adb.adb_protocol._AdbConnection.Close.CALL_GRAPH.svg_images/adb.adb_protocol._AdbConnection.Close.CALLER_GRAPH.svg
Raises
Okay()[source]

TODO

_images/adb.adb_protocol._AdbConnection.Okay.CALL_GRAPH.svg_images/adb.adb_protocol._AdbConnection.Okay.CALLER_GRAPH.svg
ReadUntil(*expected_cmds)[source]

Read a packet, Ack any write packets.

_images/adb.adb_protocol._AdbConnection.ReadUntil.CALL_GRAPH.svg_images/adb.adb_protocol._AdbConnection.ReadUntil.CALLER_GRAPH.svg
Parameters

*expected_cmds (TODO) – TODO

Returns

  • cmd (TODO) – TODO

  • data (TODO) – TODO

Raises
ReadUntilClose()[source]

Yield packets until a b'CLSE' packet is received.

_images/adb.adb_protocol._AdbConnection.ReadUntilClose.CALL_GRAPH.svg
Yields

data (TODO) – TODO

Write(data)[source]

Write a packet and expect an Ack.

_images/adb.adb_protocol._AdbConnection.Write.CALL_GRAPH.svg
Parameters

data (TODO) – TODO

Returns

len(data)

Return type

int

Raises
_Send(command, arg0, arg1, data=b'')[source]

TODO

_images/adb.adb_protocol._AdbConnection._Send.CALLER_GRAPH.svg
Parameters
  • command (TODO) – TODO

  • arg0 (TODO) – TODO

  • arg1 (TODO) – TODO

  • data (bytes) – TODO

adb.adb_protocol.find_backspace_runs(stdout_bytes, start_pos)[source]

TODO

_images/adb.adb_protocol.find_backspace_runs.CALLER_GRAPH.svg
Parameters
  • stdout_bytes (TODO) – TODO

  • start_pos (TODO) – TODO

Returns

  • int – The index/position of the first backspace.

  • num_backspaces (int) – TODO