~martijnbraam/pts-devctl

a6b1267114010bd7320f5aab58fdf4a03c68f916 — Martijn Braam 2 months ago 1980426 master
Also detect fastboot and adb devices per USB port
2 files changed, 58 insertions(+), 33 deletions(-)

M devctl/__main__.py
M devctl/enumerator.py
M devctl/__main__.py => devctl/__main__.py +12 -12
@@ 10,7 10,7 @@ from colorama import Fore
import tabulate
import serial

from devctl.enumerator import find_uart_ports, find_disks, get_device_from_args
from devctl.enumerator import find_uart_ports, find_flash_devices, get_device_from_args
from devctl.util import printe, printc




@@ 23,10 23,10 @@ class Flasher(threading.Thread):

    def find_target(self):
        if '-' in self.target:
            disks = find_disks()
            disks = find_flash_devices()
            for disk in disks:
                if disk.diskport == self.target:
                    return disk.diskdevice
                if disk.flashport == self.target:
                    return disk.flashdevice
        elif os.path.exists(self.target):
            return self.target
        return None


@@ 128,7 128,7 @@ def flash(args, device):
            control.press_bootloader(False)

            # Start writing the image in a second thread
            thread = Flasher(args.image, args.diskdevice, control)
            thread = Flasher(args.image, args.flashdevice, control)
            thread.daemon = True
            thread.start()
        elif line.startswith(b'[    0.000000] Booting Linux on physical CPU'):


@@ 161,7 161,7 @@ def cmd_list(args):
            testdevices.append(dev)
        else:
            uartadapters.append(dev)
    diskdevices = find_disks()
    flashdevices = find_flash_devices()

    if len(testdevices) > 0:
        print(Fore.BLUE + "Test devices:" + Fore.RESET)


@@ 179,12 179,12 @@ def cmd_list(args):
        print(tabulate.tabulate(rows, ['USB port', 'Name', 'Device TTY']))
        print()

    if len(diskdevices) > 0:
        print(Fore.BLUE + "Disk devices:" + Fore.RESET)
    if len(flashdevices) > 0:
        print(Fore.BLUE + "Flash devices:" + Fore.RESET)
        rows = []
        for dev in diskdevices:
            rows.append([dev.diskport, dev.name, dev.diskdevice])
        print(tabulate.tabulate(rows, ['USB port', 'Name', 'Disk']))
        for dev in flashdevices:
            rows.append([dev.flashport, dev.name, dev.type, dev.flashdevice or dev.flashref])
        print(tabulate.tabulate(rows, ['USB port', 'Name', 'Type', 'Reference']))


def cmd_flash(args):


@@ 198,7 198,7 @@ def cmd_flash(args):
    if device.uartdevice is None:
        printe('system', 'UART device is not set')
        sys.exit(1)
    if args.diskdevice is None:
    if args.flashdevice is None:
        printe('system', 'Disk device is not set')
        sys.exit(1)


M devctl/enumerator.py => devctl/enumerator.py +46 -21
@@ 11,11 11,32 @@ class Device:
        self.name = None
        self.uartdevice = None
        self.controldevice = None
        self.diskdevice = None
        self.flashdevice = None
        self.flashref = None
        self.flashtype = None
        self.serial = None
        self.controlport = None
        self.uartport = None
        self.diskport = None
        self.flashport = None


def name_from_port(port):
    result = ""
    if os.path.isfile(os.path.join('/sys/bus/usb/devices', port, 'manufacturer')):
        with open(os.path.join('/sys/bus/usb/devices', port, 'manufacturer')) as handle:
            result = handle.read().strip() + " "

    if os.path.isfile(os.path.join('/sys/bus/usb/devices', port, 'product')):
        with open(os.path.join('/sys/bus/usb/devices', port, 'product')) as handle:
            result += handle.read().strip()
    return result.strip()


def serial_from_port(port):
    if os.path.isfile(os.path.join('/sys/bus/usb/devices', port, 'serial')):
        with open(os.path.join('/sys/bus/usb/devices', port, 'serial')) as handle:
            return handle.read().strip()
    return None


def find_uart_ports():


@@ 76,8 97,8 @@ def find_uart_ports():
    return testdevices + uartadapters


def find_disks():
    diskdevices = []
def find_flash_devices():
    flashdevices = []
    # Find all USB devices that are SCSI storage
    for device in glob.glob('/sys/bus/usb/devices/*/host*'):
        part = device.split('/')


@@ 88,21 109,25 @@ def find_disks():
        for disk in disks:
            blockdev = disk.split('/')[-1]
            result = Device('disk')
            result.diskport = port
            result.diskdevice = os.path.join('/dev', blockdev)
            result.flashport = port
            result.flashdevice = os.path.join('/dev', blockdev)
            result.name = name_from_port(port)
            flashdevices.append(result)

            if os.path.isfile(os.path.join('/sys/bus/usb/devices', port, 'manufacturer')):
                with open(os.path.join('/sys/bus/usb/devices', port, 'manufacturer')) as handle:
                    result.name = handle.read().strip() + " "
            else:
                result.name = ""
    for device in glob.glob('/sys/bus/usb/devices/*/interface'):
        part = device.split('/')
        port_intf = part[-2]
        port, intf = port_intf.split(':', maxsplit=1)

            if os.path.isfile(os.path.join('/sys/bus/usb/devices', port, 'product')):
                with open(os.path.join('/sys/bus/usb/devices', port, 'product')) as handle:
                    result.name += handle.read().strip()
            result.name = result.name.strip()
            diskdevices.append(result)
    return diskdevices
        with open(device, 'r') as handle:
            interface = handle.read().strip()
        if interface in ['ADB Interface', 'fastboot']:
            result = Device('ADB' if 'ADB' in interface else 'fastboot')
            result.flashport = port
            result.name = name_from_port(port)
            result.flashref = serial_from_port(port)
            flashdevices.append(result)
    return flashdevices


def get_device_from_args(args):


@@ 147,9 172,9 @@ def get_device_from_args(args):
        else:
            device.uartdevice = args.uartdevice

    if args.diskdevice is not None:
        if '-' in args.diskdevice:
            device.diskport = args.diskdevice
    if args.flashdevice is not None:
        if '-' in args.flashdevice:
            device.flashport = args.flashdevice
        else:
            device.diskdevice = args.diskdevice
            device.flashdevice = args.flashdevice
    return device