Skip to content

Commit

Permalink
Read until receiving 'constants.MESSAGE_SIZE' bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffLIrion committed Nov 26, 2020
1 parent 2bd30f0 commit d42fea3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
22 changes: 15 additions & 7 deletions adb_shell/adb_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,8 +757,17 @@ def _read(self, expected_cmds, adb_info):
start = time.time()

while True:
msg = self._transport.bulk_read(constants.MESSAGE_SIZE, adb_info.transport_timeout_s)
_LOGGER.debug("bulk_read(%d): %s", constants.MESSAGE_SIZE, repr(msg))
# Read until `constants.MESSAGE_SIZE` bytes are received
msg = bytearray()
msg_length = constants.MESSAGE_SIZE
while msg_length > 0:
msg += self._transport.bulk_read(msg_length, adb_info.transport_timeout_s)
_LOGGER.debug("bulk_read(%d): %s", msg_length, repr(msg))
msg_length -= len(msg)

if time.time() - start > adb_info.read_timeout_s:
raise exceptions.AdbTimeoutError("Took longer than %f seconds to read %d bytes" % (adb_info.read_timeout_s, constants.MESSAGE_SIZE))

cmd, arg0, arg1, data_length, data_checksum = unpack(msg)
command = constants.WIRE_TO_ID.get(cmd)

Expand All @@ -769,7 +778,9 @@ def _read(self, expected_cmds, adb_info):
break

if time.time() - start > adb_info.read_timeout_s:
raise exceptions.InvalidCommandError("Never got one of the expected responses: %s (transport_timeout_s = %d, read_timeout_s = %d" % (expected_cmds, adb_info.transport_timeout_s, adb_info.read_timeout_s))
raise exceptions.InvalidCommandError("Never got one of the expected responses: %s (transport_timeout_s = %f, read_timeout_s = %f)" % (expected_cmds, adb_info.transport_timeout_s, adb_info.read_timeout_s))

data = bytearray()

if data_length > 0:
data = bytearray()
Expand All @@ -784,9 +795,6 @@ def _read(self, expected_cmds, adb_info):
if actual_checksum != data_checksum:
raise exceptions.InvalidChecksumError('Received checksum {0} != {1}'.format(actual_checksum, data_checksum))

else:
data = bytearray()

return command, arg0, arg1, bytes(data)

def _read_until(self, expected_cmds, adb_info):
Expand Down Expand Up @@ -833,7 +841,7 @@ def _read_until(self, expected_cmds, adb_info):
break

if time.time() - start > adb_info.read_timeout_s:
raise exceptions.InvalidCommandError("Never got one of the expected responses: %s (transport_timeout_s = %d, read_timeout_s = %d" % (expected_cmds, adb_info.transport_timeout_s, adb_info.read_timeout_s))
raise exceptions.InvalidCommandError("Never got one of the expected responses: %s (transport_timeout_s = %f, read_timeout_s = %f)" % (expected_cmds, adb_info.transport_timeout_s, adb_info.read_timeout_s))

# Ignore CLSE responses to previous commands
# https://github.com/JeffLIrion/adb_shell/pull/14
Expand Down
17 changes: 13 additions & 4 deletions adb_shell/adb_device_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,8 +752,17 @@ async def _read(self, expected_cmds, adb_info):
start = time.time()

while True:
msg = await self._transport.bulk_read(constants.MESSAGE_SIZE, adb_info.transport_timeout_s)
_LOGGER.debug("bulk_read(%d): %s", constants.MESSAGE_SIZE, repr(msg))
# Read until `constants.MESSAGE_SIZE` bytes are received
msg = bytearray()
msg_length = constants.MESSAGE_SIZE
while msg_length > 0:
msg += await self._transport.bulk_read(msg_length, adb_info.transport_timeout_s)
_LOGGER.debug("bulk_read(%d): %s", msg_length, repr(msg))
msg_length -= len(msg)

if time.time() - start > adb_info.read_timeout_s:
raise exceptions.AdbTimeoutError("Took longer than %f seconds to read %d bytes" % (adb_info.read_timeout_s, constants.MESSAGE_SIZE))

cmd, arg0, arg1, data_length, data_checksum = unpack(msg)
command = constants.WIRE_TO_ID.get(cmd)

Expand All @@ -764,7 +773,7 @@ async def _read(self, expected_cmds, adb_info):
break

if time.time() - start > adb_info.read_timeout_s:
raise exceptions.InvalidCommandError("Never got one of the expected responses: %s (transport_timeout_s = %d, read_timeout_s = %d" % (expected_cmds, adb_info.transport_timeout_s, adb_info.read_timeout_s))
raise exceptions.InvalidCommandError("Never got one of the expected responses: %s (transport_timeout_s = %f, read_timeout_s = %f)" % (expected_cmds, adb_info.transport_timeout_s, adb_info.read_timeout_s))

if data_length > 0:
data = bytearray()
Expand Down Expand Up @@ -828,7 +837,7 @@ async def _read_until(self, expected_cmds, adb_info):
break

if time.time() - start > adb_info.read_timeout_s:
raise exceptions.InvalidCommandError("Never got one of the expected responses: %s (transport_timeout_s = %d, read_timeout_s = %d" % (expected_cmds, adb_info.transport_timeout_s, adb_info.read_timeout_s))
raise exceptions.InvalidCommandError("Never got one of the expected responses: %s (transport_timeout_s = %f, read_timeout_s = %f)" % (expected_cmds, adb_info.transport_timeout_s, adb_info.read_timeout_s))

# Ignore CLSE responses to previous commands
# https://github.com/JeffLIrion/adb_shell/pull/14
Expand Down
20 changes: 10 additions & 10 deletions tests/test_adb_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ def parse_module(infile):

def parse_function(line):
"""Code for converting an `AdbDevice` method into an `AdbDeviceTest` method (see below)."""
name = line.split("(")[0].split()[-1]
args = line.split("(")[1].split(")")[0]
args_list = args.split(",")
arg_names = [arg.split("=")[0].strip() for arg in args_list]
args_format = ", ".join(["{}" for _ in arg_names[1:]])
args_str = ", ".join(arg_names[1:])
print(" {}".format(line.strip()))
print(" _LOGGER2.info(\"AdbDevice.{}({})\".format({}))".format(name, args_format, args_str))
print(" return AdbDevice.{}(self, {})".format(name, args_str))
name = line.split("(")[0].split()[-1]
args = line.split("(")[1].split(")")[0]
args_list = args.split(",")
arg_names = [arg.split("=")[0].strip() for arg in args_list]
args_format = ", ".join(["{}" for _ in arg_names[1:]])
args_str = ", ".join(arg_names[1:])
print(" {}".format(line.strip()))
print(" _LOGGER2.info(\"AdbDevice.{}({})\".format({}))".format(name, args_format, args_str))
print(" return AdbDevice.{}(self, {})".format(name, args_str))


class AdbDeviceTest(AdbDevice):
Expand Down Expand Up @@ -710,7 +710,7 @@ def test_push_file(self):
self.device.push('TEST_FILE', '/data', mtime=mtime)
self.assertEqual(expected_bulk_write, self.device._transport._bulk_write)

def test_push_issue113(self):
def _test_push_issue113(self):
# pytest tests/test_adb_device.py::TestAdbDevice::test_push_issue113 --log-cli-level=INFO
def push_progress_callback(device_path, bytes_written, total_bytes):
_LOGGER2.warning(f"ADB Push-Progress: {device_path} bytes_written:{bytes_written} total_bytes:{total_bytes}")
Expand Down

0 comments on commit d42fea3

Please sign in to comment.