feat: add filtering based on serial number

Closes https://github.com/espressif/esptool/issues/1027
This commit is contained in:
Jaroslav Burian
2024-11-01 13:43:38 +01:00
parent e0deeac2bb
commit 88319dbc31
2 changed files with 14 additions and 3 deletions

View File

@@ -158,5 +158,6 @@ at least one FilterValue for each specified FilterType to be considered. Example
* ``--port-filter vid=0x303A --port-filter pid=0x0002`` matches Espressif ESP32-S2 in USB-OTG mode by VID and PID.
* ``--port-filter vid=0x303A --port-filter pid=0x1001`` matches Espressif USB-Serial/JTAG unit used by multiple chips by VID and PID.
* ``--port-filter name=ttyUSB`` matches ports where the port name contains the specified text.
* ``--port-filter serial=7c98d1065267ee11bcc4c8ab93cd958c`` matches ports where the serial number contains the specified text.
See also the `Espressif USB customer-allocated PID repository <https://github.com/espressif/usb-pids>`_

View File

@@ -135,7 +135,7 @@ def main(argv=None, esp=None):
parser.add_argument(
"--port-filter",
action="append",
help="Serial port device filter, can be vid=NUMBER, pid=NUMBER, name=SUBSTRING",
help="Serial port device filter, can be vid=NUMBER, pid=NUMBER, name=SUBSTRING, serial=SUBSTRING",
type=str,
default=[],
)
@@ -728,6 +728,7 @@ def main(argv=None, esp=None):
args.filterVids = []
args.filterPids = []
args.filterNames = []
args.filterSerials = []
for f in args.port_filter:
kvp = f.split("=")
if len(kvp) != 2:
@@ -738,6 +739,8 @@ def main(argv=None, esp=None):
args.filterPids.append(arg_auto_int(kvp[1]))
elif kvp[0] == "name":
args.filterNames.append(kvp[1])
elif kvp[0] == "serial":
args.filterSerials.append(kvp[1])
else:
raise FatalError("Option --port-filter argument key not recognized")
@@ -776,7 +779,9 @@ def main(argv=None, esp=None):
initial_baud = args.baud
if args.port is None:
ser_list = get_port_list(args.filterVids, args.filterPids, args.filterNames)
ser_list = get_port_list(
args.filterVids, args.filterPids, args.filterNames, args.filterSerials
)
print("Found %d serial ports" % len(ser_list))
else:
ser_list = [args.port]
@@ -1082,7 +1087,7 @@ def arg_auto_chunk_size(string: str) -> int:
return num
def get_port_list(vids=[], pids=[], names=[]):
def get_port_list(vids=[], pids=[], names=[], serials=[]):
if list_ports is None:
raise FatalError(
"Listing all serial ports is currently not available. "
@@ -1103,6 +1108,11 @@ def get_port_list(vids=[], pids=[], names=[]):
port.name is None or all(name not in port.name for name in names)
):
continue
if serials and (
port.serial_number is None
or all(serial not in port.serial_number for serial in serials)
):
continue
ports.append(port.device)
return sorted(ports)