def Read(self, expected_ids, read_data=True): """Read ADB messages and return FileSync packets.""" if self.send_buffer: self._Flush() # Read one filesync packet off the recv buffer. header_data = self._ReadBuffered(self.recv_header_len) header = struct.unpack(self.recv_header_format, header_data) # Header is (ID, ...). command_id = self.wire_to_id[header[0]] if command_id not in expected_ids: if command_id == 'FAIL': raise usb_exceptions.AdbCommandFailureException( 'Command failed.') raise adb_protocol.InvalidResponseError( 'Expected one of %s, got %s' % (expected_ids, command_id)) if not read_data: return command_id, header[1:] # Header is (ID, ..., size). size = header[-1] data = self._ReadBuffered(size) return command_id, header[1:-1], data
def Close(self): self._Send('CLSE', arg0=self.local_id, arg1=self.remote_id) cmd, data = self.ReadUntil('CLSE') if cmd != 'CLSE': if cmd == 'FAIL': raise usb_exceptions.AdbCommandFailureException('Command failed.', data) raise InvalidCommandError('Expected a CLSE response, got %s (%s)', cmd, data)
def Write(self, data): """Write a packet and expect an Ack.""" self._Send('WRTE', arg0=self.local_id, arg1=self.remote_id, data=data) # Expect an ack in response. cmd, okay_data = self.ReadUntil('OKAY') if cmd != 'OKAY': if cmd == 'FAIL': raise usb_exceptions.AdbCommandFailureException( 'Command failed.', okay_data) raise InvalidCommandError( 'Expected an OKAY in response to a WRITE, got %s (%s)', cmd, okay_data) return len(data)
def ReadUntilClose(self): """Yield packets until a Close packet is received.""" while True: cmd, data = self.ReadUntil('CLSE', 'WRTE') if cmd == 'CLSE': self._Send('CLSE', arg0=self.local_id, arg1=self.remote_id) break if cmd != 'WRTE': if cmd == 'FAIL': raise usb_exceptions.AdbCommandFailureException( 'Command failed.', data) raise InvalidCommandError('Expected a WRITE or a CLOSE, got %s (%s)', cmd, data) yield data