adb.filesync_protocol module

ADB protocol implementation.

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

Contents

adb.filesync_protocol.DEFAULT_PUSH_MODE = 33272

Default mode for pushed files.

class adb.filesync_protocol.DeviceFile(filename, mode, size, mtime)

Bases: tuple

_fields_defaults = {}
property filename

Alias for field number 0

property mode

Alias for field number 1

property mtime

Alias for field number 3

property size

Alias for field number 2

class adb.filesync_protocol.FileSyncConnection(adb_connection, recv_header_format)[source]

Bases: object

Encapsulate a FileSync service connection.

Parameters
adb

ADB connection

Type

adb.adb_protocol._AdbConnection

send_buffer

bytearray(adb_protocol.MAX_ADB_DATA) (see adb.adb_protocol.MAX_ADB_DATA)

Type

byte_array

send_idx

TODO

Type

int

send_header_len

struct.calcsize(b'<2I')

Type

int

recv_buffer

TODO

Type

bytearray

recv_header_format

TODO

Type

bytes

recv_header_len

struct.calcsize(recv_header_format)

Type

int

Read(expected_ids, read_data=True)[source]

Read ADB messages and return FileSync packets.

_images/adb.filesync_protocol.FileSyncConnection.Read.CALL_GRAPH.svg_images/adb.filesync_protocol.FileSyncConnection.Read.CALLER_GRAPH.svg
Parameters
  • expected_ids (tuple[bytes]) – If the received header ID is not in expected_ids, an exception will be raised

  • read_data (bool) – Whether to read the received data

Returns

  • command_id (bytes) – The received header ID

  • tuple – TODO

  • data (bytearray) – The received data

Raises
ReadUntil(expected_ids, *finish_ids)[source]

Useful wrapper around FileSyncConnection.Read().

_images/adb.filesync_protocol.FileSyncConnection.ReadUntil.CALL_GRAPH.svg
Parameters
  • expected_ids (tuple[bytes]) – If the received header ID is not in expected_ids, an exception will be raised

  • finish_ids (tuple[bytes]) – We will read until we find a header ID that is in finish_ids

Yields
  • cmd_id (bytes) – The received header ID

  • header (tuple) – TODO

  • data (bytearray) – The received data

Send(command_id, data=b'', size=0)[source]

Send/buffer FileSync packets.

Packets are buffered and only flushed when this connection is read from. All messages have a response from the device, so this will always get flushed.

_images/adb.filesync_protocol.FileSyncConnection.Send.CALL_GRAPH.svg
Parameters
  • command_id (bytes) – Command to send.

  • data (str, bytes) – Optional data to send, must set data or size.

  • size (int) – Optionally override size from len(data).

_CanAddToSendBuffer(data_len)[source]

Determine whether data_len bytes of data can be added to the send buffer without exceeding adb.adb_protocol.MAX_ADB_DATA.

_images/adb.filesync_protocol.FileSyncConnection._CanAddToSendBuffer.CALLER_GRAPH.svg
Parameters

data_len (int) – The length of the data to be potentially added to the send buffer (not including the length of its header)

Returns

Whether data_len bytes of data can be added to the send buffer without exceeding adb.adb_protocol.MAX_ADB_DATA

Return type

bool

_Flush()[source]

TODO

_images/adb.filesync_protocol.FileSyncConnection._Flush.CALLER_GRAPH.svg
Raises

adb.usb_exceptions.WriteFailedError – Could not send data

_ReadBuffered(size)[source]

Read size bytes of data from self.recv_buffer.

_images/adb.filesync_protocol.FileSyncConnection._ReadBuffered.CALLER_GRAPH.svg
Parameters

size (int) – The amount of data to read

Returns

result – The read data

Return type

bytearray

id_to_wire = {b'DATA': 1096040772, b'DENT': 1414415684, b'DONE': 1162760004, b'FAIL': 1279869254, b'LIST': 1414744396, b'OKAY': 1497451343, b'QUIT': 1414092113, b'RECV': 1447249234, b'SEND': 1145980243, b'STAT': 1413567571}
ids = [b'STAT', b'LIST', b'SEND', b'RECV', b'DENT', b'DONE', b'DATA', b'OKAY', b'FAIL', b'QUIT']
wire_to_id = {1096040772: b'DATA', 1145980243: b'SEND', 1162760004: b'DONE', 1279869254: b'FAIL', 1413567571: b'STAT', 1414092113: b'QUIT', 1414415684: b'DENT', 1414744396: b'LIST', 1447249234: b'RECV', 1497451343: b'OKAY'}
class adb.filesync_protocol.FilesyncProtocol[source]

Bases: object

Implements the FileSync protocol as described in sync.txt.

classmethod List(connection, path)[source]

Get a list of the files in path.

Parameters
Returns

files – Information about the files in path

Return type

list[DeviceFile]

classmethod Pull(connection, filename, dest_file, progress_callback)[source]

Pull a file from the device into the file-like dest_file.

_images/adb.filesync_protocol.FilesyncProtocol.Pull.CALL_GRAPH.svg
Parameters
  • connection (adb.adb_protocol._AdbConnection) – ADB connection

  • filename (str) – The file to be pulled

  • dest_file (_io.BytesIO) – File-like object for writing to

  • progress_callback (function, None) – Callback method that accepts filename, bytes_written, and total_bytes

Raises

PullFailedError – Unable to pull file

classmethod Push(connection, datafile, filename, st_mode=33272, mtime=0, progress_callback=None)[source]

Push a file-like object to the device.

_images/adb.filesync_protocol.FilesyncProtocol.Push.CALL_GRAPH.svg_images/adb.filesync_protocol.FilesyncProtocol.Push.CALLER_GRAPH.svg
Parameters
  • connection (adb.adb_protocol._AdbConnection) – ADB connection

  • datafile (_io.BytesIO) – File-like object for reading from

  • filename (str) – Filename to push to

  • st_mode (int) – Stat mode for filename

  • mtime (int) – Modification time

  • progress_callback (function, None) – Callback method that accepts filename, bytes_written, and total_bytes

Raises

PushFailedError – Raised on push failure.

static Stat(connection, filename)[source]

Get file status (mode, size, and mtime).

_images/adb.filesync_protocol.FilesyncProtocol.Stat.CALLER_GRAPH.svg
Parameters
Returns

  • mode (int) – The mode of the file

  • size (int) – The size of the file

  • mtime (int) – The time of last modification for the file

Raises

adb.adb_protocol.InvalidResponseError – Expected STAT response to STAT, got something else

classmethod _HandleProgress(progress_callback)[source]

Calls the callback with the current progress and total bytes written/received.

_images/adb.filesync_protocol.FilesyncProtocol._HandleProgress.CALL_GRAPH.svg_images/adb.filesync_protocol.FilesyncProtocol._HandleProgress.CALLER_GRAPH.svg
Parameters

progress_callback (function) – Callback method that accepts filename, bytes_written, and total_bytes; total_bytes will be -1 for file-like objects.

exception adb.filesync_protocol.InterleavedDataError[source]

Bases: Exception

We only support command sent serially.

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

Bases: Exception

Checksum of data didn’t match expected checksum.

_images/adb.filesync_protocol.InvalidChecksumError.CALL_GRAPH.svg
adb.filesync_protocol.MAX_PUSH_DATA = 2048

Maximum size of a filesync DATA packet.

exception adb.filesync_protocol.PullFailedError[source]

Bases: Exception

Pulling a file failed for some reason.

_images/adb.filesync_protocol.PullFailedError.CALL_GRAPH.svg
exception adb.filesync_protocol.PushFailedError[source]

Bases: Exception

Pushing a file failed for some reason.

_images/adb.filesync_protocol.PushFailedError.CALL_GRAPH.svg