Change in osmo-gsm-tester[master]: utils: Introduce show_usb_device.py

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Pau Espin Pedrol gerrit-no-reply at lists.osmocom.org
Wed Oct 31 08:57:27 UTC 2018


Pau Espin Pedrol has submitted this change and it was merged. ( https://gerrit.osmocom.org/11460 )

Change subject: utils: Introduce show_usb_device.py
......................................................................

utils: Introduce show_usb_device.py

This is a small script written by Alexander Couzens that is useful to
list modems and its properties in a quick and easy way in
osmo-gsm-tester setup.

Change-Id: Iec049e2d56d61ecd50b65b64d95d69641fa0f8be
---
A utils/show_usb_device.py
1 file changed, 97 insertions(+), 0 deletions(-)

Approvals:
  Harald Welte: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/utils/show_usb_device.py b/utils/show_usb_device.py
new file mode 100755
index 0000000..9136234
--- /dev/null
+++ b/utils/show_usb_device.py
@@ -0,0 +1,97 @@
+#!/usr/bin/env python3
+# Alexander Couzens <lynxis at fe80.eu>
+# MIT
+
+# show usb device with their net and serial devices
+
+import os
+import usb.core
+import usb.util
+from pprint import pprint
+
+def get_path_ids(bus, port_numbers):
+    port_numbers = [str(port) for port in port_numbers]
+    ports = '.'.join(port_numbers)
+    return '{}-{}'.format(bus, ports)
+
+def get_usb_dir(bus, port_numbers):
+    return '/sys/bus/usb/devices/' + get_path_ids(bus, port_numbers) + '/'
+
+def get_usbmisc_from_usb(bus, port_numbers):
+    usbmisc_ifaces = []
+    path = get_usb_dir(bus, port_numbers)
+    path_ids = get_path_ids(bus, port_numbers)
+
+    usb_interfaces = [f for f in os.listdir(path) if f.startswith(path_ids)]
+    for usb_iface in usb_interfaces:
+        listdir = [f for f in os.listdir(path + usb_iface) if f == ('usbmisc')]
+        if listdir:
+            # found a net iface
+            usbmisc_ifaces += os.listdir(path + usb_iface + '/usbmisc/')
+    return usbmisc_ifaces
+
+def get_net_from_usb(bus, port_numbers):
+    net_ifaces = []
+    path = get_usb_dir(bus, port_numbers)
+    path_ids = get_path_ids(bus, port_numbers)
+
+    usb_interfaces = [f for f in os.listdir(path) if f.startswith(path_ids)]
+    for usb_iface in usb_interfaces:
+        listdir = [f for f in os.listdir(path + usb_iface) if f == ('net')]
+        if listdir:
+            # found a net iface
+            net_ifaces += os.listdir(path + usb_iface + '/net/')
+    return net_ifaces
+
+def get_serial_from_usb(bus, port_numbers):
+    serial_ifaces = []
+    path = get_usb_dir(bus, port_numbers)
+    path_ids = get_path_ids(bus, port_numbers)
+
+    usb_interfaces = [f for f in os.listdir(path) if f.startswith(path_ids)]
+    for usb_iface in usb_interfaces:
+        serial_ifaces += [f for f in os.listdir(path + usb_iface) if f.startswith('tty')]
+    return serial_ifaces
+
+def get_product(bus, port_numbers):
+    usb_dir = get_usb_dir(bus, port_numbers)
+    try:
+        product = open(os.path.join(usb_dir, 'product')).read().strip()
+    except OSError as exp:
+        product = "Unknown"
+    return product
+
+def get_manuf(bus, port_numbers):
+    usb_dir = get_usb_dir(bus, port_numbers)
+    try:
+        manuf = open(os.path.join(usb_dir, 'manufacturer')).read().strip()
+    except OSError:
+        manuf = "Unknown"
+    return manuf
+
+def get_name(bus, port_numbers):
+    manuf = get_manuf(bus, port_numbers)
+    product = get_product(bus, port_numbers)
+    return "%s %s" % (manuf, product)
+
+if __name__ == '__main__':
+    USB_DEVS = [dev for dev in usb.core.find(find_all=True)]
+    RESULT = {}
+    for device in USB_DEVS:
+        result = {}
+        if not device.port_numbers:
+            continue
+
+        # retrieve manuf + product from /sys because non-root user can not ask the usb device
+        result['name'] = get_name(device.bus, device.port_numbers)
+        result['path'] = get_usb_dir(device.bus, device.port_numbers)
+        result['net'] = get_net_from_usb(device.bus, device.port_numbers)
+        result['cdc'] = get_usbmisc_from_usb(device.bus, device.port_numbers)
+        result['serial'] = get_serial_from_usb(device.bus, device.port_numbers)
+
+        # only show device which have serial or net devices
+        if result['net'] or result['serial']:
+            RESULT[device] = result
+
+    pprint(RESULT)
+

-- 
To view, visit https://gerrit.osmocom.org/11460
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-gsm-tester
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Iec049e2d56d61ecd50b65b64d95d69641fa0f8be
Gerrit-Change-Number: 11460
Gerrit-PatchSet: 4
Gerrit-Owner: Pau Espin Pedrol <pespin at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder (1000002)
Gerrit-Reviewer: Max <msuraev at sysmocom.de>
Gerrit-Reviewer: Pau Espin Pedrol <pespin at sysmocom.de>
Gerrit-Reviewer: lynxis lazus <lynxis at fe80.eu>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20181031/107e859d/attachment.htm>


More information about the gerrit-log mailing list