aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Couzens <lynxis@fe80.eu>2018-10-25 14:34:09 +0200
committerPau Espin Pedrol <pespin@sysmocom.de>2018-10-31 08:57:27 +0000
commit2decdba7e569bfd2693787bbb17690a7531f3f64 (patch)
tree2113abb834ed4a74b8ca74cf4fd3fd54cf05ba8d
parent24dc93754b0057fcd7256c4709d5f0c87fc854b3 (diff)
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
-rwxr-xr-xutils/show_usb_device.py97
1 files changed, 97 insertions, 0 deletions
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@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)
+