| OLD | NEW |
| 1 # Copyright 2014 Google Inc. All rights reserved. | 1 # Copyright 2014 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); | 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with the License. | 4 # you may not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at | 5 # You may obtain a copy of the License at |
| 6 # | 6 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 | 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # | 8 # |
| 9 # Unless required by applicable law or agreed to in writing, software | 9 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, | 10 # distributed under the License is distributed on an "AS IS" BASIS, |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 # Will reentrantly call self._Add() via parent._OnRead() | 242 # Will reentrantly call self._Add() via parent._OnRead() |
| 243 if not self._manager.ReadAndDispatch(timeout_ms=self._timeout_ms): | 243 if not self._manager.ReadAndDispatch(timeout_ms=self._timeout_ms): |
| 244 # Failed to read from the device, the connection likely dropped. | 244 # Failed to read from the device, the connection likely dropped. |
| 245 raise StopIteration() | 245 raise StopIteration() |
| 246 continue | 246 continue |
| 247 if isinstance(i, StopIteration): | 247 if isinstance(i, StopIteration): |
| 248 raise i | 248 raise i |
| 249 return i | 249 return i |
| 250 | 250 |
| 251 def _Add(self, message): | 251 def _Add(self, message): |
| 252 self._queue.put(message.data) | 252 self._queue.put(message) |
| 253 | 253 |
| 254 def _Close(self): | 254 def _Close(self): |
| 255 self._queue.put(StopIteration()) | 255 self._queue.put(StopIteration()) |
| 256 | 256 |
| 257 def __init__(self, manager, local_id, service_name, timeout_ms=None): | 257 def __init__(self, manager, local_id, service_name, timeout_ms=None): |
| 258 # ID as given by the remote device. | 258 # ID as given by the remote device. |
| 259 self.remote_id = 0 | 259 self.remote_id = 0 |
| 260 # Service requested on the remote device. | 260 # Service requested on the remote device. |
| 261 self.service_name = service_name | 261 self.service_name = service_name |
| 262 # Self assigned local ID. | 262 # Self assigned local ID. |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 # cases. | 319 # cases. |
| 320 logging.warning( | 320 logging.warning( |
| 321 'Unexpected remote ID: expected %d: %s', self.remote_id, message) | 321 'Unexpected remote ID: expected %d: %s', self.remote_id, message) |
| 322 if message.header.arg1 != self._local_id: | 322 if message.header.arg1 != self._local_id: |
| 323 raise InvalidResponseError( | 323 raise InvalidResponseError( |
| 324 'Unexpected local ID: expected %d' % self._local_id, message) | 324 'Unexpected local ID: expected %d' % self._local_id, message) |
| 325 if cmd_name == 'CLSE': | 325 if cmd_name == 'CLSE': |
| 326 self._HasClosed() | 326 self._HasClosed() |
| 327 return | 327 return |
| 328 if cmd_name == 'OKAY': | 328 if cmd_name == 'OKAY': |
| 329 self._yielder._Add(message) |
| 329 return | 330 return |
| 330 if cmd_name == 'WRTE': | 331 if cmd_name == 'WRTE': |
| 331 try: | 332 try: |
| 332 self._Write('OKAY', '') | 333 self._Write('OKAY', '') |
| 333 except usb_exceptions.WriteFailedError as e: | 334 except usb_exceptions.WriteFailedError as e: |
| 334 _LOG.info('%s._OnRead(): Failed to reply OKAY: %s', self.port_path, e) | 335 _LOG.info('%s._OnRead(): Failed to reply OKAY: %s', self.port_path, e) |
| 335 self._yielder._Add(message) | 336 self._yielder._Add(message) |
| 336 return | 337 return |
| 337 if cmd_name == 'AUTH': | 338 if cmd_name == 'AUTH': |
| 338 self._manager._HandleAUTH(message) | 339 self._manager._HandleAUTH(message) |
| 339 return | 340 return |
| 340 if cmd_name == 'CNXN': | 341 if cmd_name == 'CNXN': |
| 341 self._manager.HandleCNXN(message) | 342 self._manager.HandleCNXN(message) |
| 342 return | 343 return |
| 343 # Unexpected message. | 344 # Unexpected message. |
| 344 assert False, message | 345 assert False, message |
| 345 | 346 |
| 346 # Adaptors. | 347 # Adaptors. |
| 347 | 348 |
| 348 def Write(self, data): | 349 def Write(self, data): |
| 349 self._Write('WRTE', data) | 350 self._Write('WRTE', data) |
| 350 | 351 |
| 351 def ReadUntil(self, _): | 352 def ReadUntil(self, finish_command='WRTE'): |
| 352 try: | 353 try: |
| 353 return 'WRTE', self._yielder.next() | 354 while True: |
| 355 message = self._yielder.next() |
| 356 if message.header.command_name == finish_command: |
| 357 return message |
| 354 except StopIteration: | 358 except StopIteration: |
| 355 raise InvalidResponseError('Never got \'WRTE\'', '<N/A>') | 359 raise InvalidResponseError('Never got \'WRTE\'', '<N/A>') |
| 356 | 360 |
| 357 | 361 |
| 358 class AdbConnectionManager(object): | 362 class AdbConnectionManager(object): |
| 359 """Multiplexes the multiple connections.""" | 363 """Multiplexes the multiple connections.""" |
| 360 # Maximum amount of data in an ADB packet. Value of MAX_PAYLOAD_V2 in adb.h. | 364 # Maximum amount of data in an ADB packet. Value of MAX_PAYLOAD_V2 in adb.h. |
| 361 MAX_ADB_DATA = 256*1024 | 365 MAX_ADB_DATA = 256*1024 |
| 362 | 366 |
| 363 def __init__(self, usb, banner, rsa_keys, auth_timeout_ms): | 367 def __init__(self, usb, banner, rsa_keys, auth_timeout_ms): |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 458 can fill up memory. | 462 can fill up memory. |
| 459 | 463 |
| 460 Args: | 464 Args: |
| 461 service: The service on the device to talk to. | 465 service: The service on the device to talk to. |
| 462 command: The command to send to the service. | 466 command: The command to send to the service. |
| 463 timeout_ms: Timeout for USB packets, in milliseconds. | 467 timeout_ms: Timeout for USB packets, in milliseconds. |
| 464 """ | 468 """ |
| 465 return self.Open('%s:%s' % (service, command), timeout_ms).__iter__() | 469 return self.Open('%s:%s' % (service, command), timeout_ms).__iter__() |
| 466 | 470 |
| 467 def Command(self, service, command='', timeout_ms=None): | 471 def Command(self, service, command='', timeout_ms=None): |
| 468 return ''.join(self.StreamingCommand(service, command, timeout_ms)) | 472 return ''.join(msg.data for msg in self.StreamingCommand(service, command, |
| 473 timeout_ms)) |
| 469 | 474 |
| 470 def ReadAndDispatch(self, timeout_ms=None): | 475 def ReadAndDispatch(self, timeout_ms=None): |
| 471 """Receive a response from the device.""" | 476 """Receive a response from the device.""" |
| 472 with self._lock: | 477 with self._lock: |
| 473 try: | 478 try: |
| 474 msg = _AdbMessage.Read(self._usb, timeout_ms) | 479 msg = _AdbMessage.Read(self._usb, timeout_ms) |
| 475 except usb_exceptions.ReadFailedError as e: | 480 except usb_exceptions.ReadFailedError as e: |
| 476 # adbd could be rebooting, etc. Return None to signal that this kind of | 481 # adbd could be rebooting, etc. Return None to signal that this kind of |
| 477 # failure is expected. | 482 # failure is expected. |
| 478 _LOG.info( | 483 _LOG.info( |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 582 msg.Write(self._usb) | 587 msg.Write(self._usb) |
| 583 return _AdbMessage.Read(self._usb, auth_timeout_ms) | 588 return _AdbMessage.Read(self._usb, auth_timeout_ms) |
| 584 | 589 |
| 585 def _Unregister(self, conn_id): | 590 def _Unregister(self, conn_id): |
| 586 with self._lock: | 591 with self._lock: |
| 587 self._UnregisterLocked(conn_id) | 592 self._UnregisterLocked(conn_id) |
| 588 | 593 |
| 589 def _UnregisterLocked(self, conn_id): | 594 def _UnregisterLocked(self, conn_id): |
| 590 # self._lock must be held. | 595 # self._lock must be held. |
| 591 self._connections.pop(conn_id, None) | 596 self._connections.pop(conn_id, None) |
| OLD | NEW |