Browse Source

Added serial interface support

Giulio 3 years ago
parent
commit
8d00509ab2
4 changed files with 63 additions and 9 deletions
  1. 2 0
      adb/adb_commands.py
  2. 0 8
      adb/client.py
  3. 56 0
      adb/common.py
  4. 5 1
      client.py

+ 2 - 0
adb/adb_commands.py

@@ -134,6 +134,8 @@ class AdbCommands(object):
 
             if serial and ':' in serial:
                 self._handle = common.TcpHandle(serial, timeout_ms=default_timeout_ms)
+            elif 'tty' in serial:
+                self._handle = common.SerialHandle(serial, timeout_ms=default_timeout_ms)
             else:
                 self._handle = common.UsbHandle.FindAndOpen(
                     DeviceIsAvailable, port_path=port_path, serial=serial,

+ 0 - 8
adb/client.py

@@ -1,8 +0,0 @@
-from adb import adb_commands
-from adb import sign_m2crypto
-
-
-# Connect to the device
-device = adb_commands.AdbCommands()
-device.ConnectDevice(port_path=Non, serial="192.168.43.100")
-device.Stat('/tmp')

+ 56 - 0
adb/common.py

@@ -22,6 +22,8 @@ import threading
 import weakref
 import select
 
+import serial
+
 import libusb1
 import usb1
 
@@ -285,6 +287,60 @@ class UsbHandle(object):
             if device_matcher is None or device_matcher(handle):
                 yield handle
 
+class SerialHandle(object):
+    def __init__(self, serial, timeout_ms=None):
+
+        if isinstance(serial, (bytes, bytearray)):
+            serial = serial.decode('utf-8')
+
+        if ',' in serial:
+            self.port, self.speed = serial.split(',')
+        else:
+            self.port = serial
+            self.speed = 115200
+
+        self._connection = None
+        self._serial_number = '%s,%s' % (self.port, self.speed)
+        self._timeout_ms = float(timeout_ms) if timeout_ms else None
+
+        self._connect()
+
+    def _connect(self):
+        timeout = self.TimeoutSeconds(self._timeout_ms)
+        
+        self._connection = serial.Serial(self.port, self.speed, timeout=timeout)
+
+    @property
+    def serial_number(self):
+        return self._serial_number
+
+    def BulkWrite(self, data, timeout=None):
+        t = self.TimeoutSeconds(timeout)
+        _, writeable, _ = select.select([], [self._connection], [], t)
+        if writeable:
+            return self._connection.write(data)
+        msg = 'Sending data to {} timed out after {}s. No data was sent.'.format(
+            self.serial_number, t)
+        raise serial.SerialTimeoutException(msg)
+
+    def BulkRead(self, numbytes, timeout=None):
+        t = self.TimeoutSeconds(timeout)
+        readable, _, _ = select.select([self._connection], [], [], t)
+        if readable:
+            return self._connection.read(numbytes)
+        msg = 'Reading from {} timed out (Timeout {}s)'.format(
+            self._serial_number, t)
+        #raise serial.TcpTimeoutException(msg)
+
+    def Timeout(self, timeout_ms):
+        return float(timeout_ms) if timeout_ms is not None else self._timeout_ms
+
+    def TimeoutSeconds(self, timeout_ms):
+        timeout = self.Timeout(timeout_ms)
+        return timeout / 1000.0 if timeout is not None else timeout
+
+    def Close(self):
+        return self._connection.close()
 
 class TcpHandle(object):
     """TCP connection object.

+ 5 - 1
client.py

@@ -6,7 +6,11 @@ queue = list()
 
 # Connect to the device
 device = adb_commands.AdbCommands()
-device.ConnectDevice(port_path=None, serial="192.168.43.168:5555")
+# XCB over wifi
+#device.ConnectDevice(port_path=None, serial="192.168.43.168:5555")
+# XCB over serial port
+device.ConnectDevice(port_path=None, serial="/dev/ttyS11,115200")
+
 
 if sys.argv[1] == 'ls':
 	root = device.List(sys.argv[2])