From 1fa61ece93491fae29592db7c0708ade80fd8420 Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Sat, 29 Jan 2022 20:45:29 -0800 Subject: [PATCH] add windows device and port detection --- meshtastic/supported_device.py | 32 +++++++++++++++ meshtastic/util.py | 75 ++++++++++++++++++++++++++-------- 2 files changed, 89 insertions(+), 18 deletions(-) diff --git a/meshtastic/supported_device.py b/meshtastic/supported_device.py index 283ef60..e9934c5 100755 --- a/meshtastic/supported_device.py +++ b/meshtastic/supported_device.py @@ -4,6 +4,7 @@ import platform import subprocess +import re # Goal is to detect which device and port to use from the supported devices # without installing any libraries that are not currently in the python meshtastic library @@ -152,4 +153,35 @@ def active_ports_on_supported_devices(sds): port = parts[-1] #print(f'port:{port}') ports.add(port) + elif system == "Windows": + # for each device in supported devices found + for d in sds: + # find the port(s) + com_ports = detect_windows_port(d) + #print(f'com_ports:{com_ports}') + # add all ports + for com_port in com_ports: + ports.add(com_port) + return ports + + +def detect_windows_port(sd): + """detect if Windows port""" + ports = set() + + if sd: + system = platform.system() + + if system == "Windows": + command = 'powershell.exe "Get-PnpDevice | Where-Object{ ($_.DeviceId -like ' + command += f"'*{sd.usb_vendor_id_in_hex.upper()}*'" + command += ')} | Format-List"' + + #print(f'command:{command}') + _, sp_output = subprocess.getstatusoutput(command) + #print(f'sp_output:{sp_output}') + p = re.compile('\(COM(.*)\)') + for x in p.findall(sp_output): + #print(f'x:{x}') + ports.add(f'COM{x}') return ports diff --git a/meshtastic/util.py b/meshtastic/util.py index c66885f..d06c703 100644 --- a/meshtastic/util.py +++ b/meshtastic/util.py @@ -297,11 +297,36 @@ def detect_supported_devices(): elif system == "Windows": # if windows, run Get-PnpDevice - pass + _, sp_output = subprocess.getstatusoutput('powershell.exe "Get-PnpDevice -PresentOnly | Format-List"') + #print(f'sp_output:{sp_output}') + vids = get_unique_vendor_ids() + for vid in vids: + #print(f'looking for {vid.upper()}...') + search = f'DeviceID.*{vid.upper()}&' + #search = f'{vid.upper()}' + #print(f'search:"{search}"') + if re.search(search, sp_output, re.MULTILINE): + #print(f'Found vendor id that matches') + devices = get_devices_with_vendor_id(vid) + # check device id + for device in devices: + #print(f'device:{device} device.usb_product_id_in_hex:{device.usb_product_id_in_hex}') + if device.usb_product_id_in_hex: + search = f'DeviceID.*{vid.upper()}&PID_{device.usb_product_id_in_hex.upper()}' + #print(f'search:"{search}"') + if re.search(search, sp_output, re.MULTILINE): + # concatenate the devices with vendor id to possibles + possible_devices.add(device) + # do a check to see if there is a Windows driver issue + if detect_windows_needs_driver(device, False): + print("WARNING: Need to install driver.") + else: + # if there is a supported device witout a product id, then it + # might be a match... so, concatenate + possible_devices.add(device) elif system == "Darwin": # run: system_profiler SPUSBDataType - # could also run ioreg # if mac air (eg: arm m1) do not know how to get info TODO: research _, sp_output = subprocess.getstatusoutput('system_profiler SPUSBDataType') @@ -326,20 +351,34 @@ def detect_supported_devices(): # if there is a supported device witout a product id, then it # might be a match... so, concatenate possible_devices.add(device) - - # ls -al /dev/{tty,cu}.* - # crw-rw-rw- 1 root wheel 0x9000003 Jan 13 02:46 /dev/cu.Bluetooth-Incoming-Port - # crw-rw-rw- 1 root wheel 0x9000005 Jan 29 12:00 /dev/cu.usbserial-0001 - # crw-rw-rw- 1 root wheel 0x9000001 Jan 13 02:45 /dev/cu.wlan-debug - # crw-rw-rw- 1 root wheel 0x9000002 Jan 13 02:46 /dev/tty.Bluetooth-Incoming-Port - # crw-rw-rw- 1 root wheel 0x9000004 Jan 29 12:00 /dev/tty.usbserial-0001 - # crw-rw-rw- 1 root wheel 0x9000000 Jan 13 02:45 /dev/tty.wlan-debug - # and exclude any "Bluetooth" or "wlan" files - # TODO: which should we prefer: cu or tty devices? - - # mac: ioreg -p IOUSB - # +-o Root - # +-o AppleT8103USBXHCI@00000000