diff options
author | Matthew Blecker <matthewb@chromium.org> | 2022-09-16 11:53:38 -0700 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-09-16 23:09:14 +0000 |
commit | 68822db5f053e0120bfcb16ef2aa7ea70e36727c (patch) | |
tree | 0640ece2e316eedfa8239836a26807ad666666b2 /extra/usb_serial | |
parent | ba09d8565974dbc9eb4dbd915d32583da6fe0311 (diff) | |
download | chrome-ec-68822db5f053e0120bfcb16ef2aa7ea70e36727c.tar.gz |
usb_serial: fix console.py handling of --serialno
This was trying to pass the optional "langid" arg to usb.util.get_string(),
but was doing so as a positional argument, in the wrong position.
This bug was papered over by a blanket "except:" clause that would fall
back on not specifying langid. With older PyUSB versions an exception from
the incorrect args would be triggered, thus the fallback was always used.
PyUSB now returns garbage for the serial number from the incorrect args, so
the serial number comparisons stopped working.
The langid being specified was 256 / 0x100, which is in a reserved block,
not specific to any real language. The get_string() invocation returns
the correct serial number with or without that langid specified (using
the args correctly), and servod never specifies langid in these lookups,
so this removes use of langid entirely to match servod behavior and remove
need for the try/except fallback.
This also fixes another blanket "except:" to specify the exception it's
actually intending to catch (IndexError).
Finally, this adds printing of default option values in the command line
help output, so that it's clear --device has a default value and will
usually need to be specified too when using --serialno. (We should
change or remove the default and make --device optional when using
--serialno, but I'll leave that for another patch.)
BRANCH=none
BUG=b:243562728
TEST=With multiple servos plugged in, including multiple servo_v4p1,
tested opening consoles via --serialno by itself, --device by itself,
and the two options in conjunction.
Signed-off-by: Matthew Blecker <matthewb@chromium.org>
Change-Id: Idd731ce76c9ed9a2e067bca11ede42b782a94669
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3902449
Reviewed-by: Ziting Shen <zitingshen@google.com>
Diffstat (limited to 'extra/usb_serial')
-rwxr-xr-x | extra/usb_serial/console.py | 13 |
1 files changed, 6 insertions, 7 deletions
diff --git a/extra/usb_serial/console.py b/extra/usb_serial/console.py index cc81547671..2b0ecd5f13 100755 --- a/extra/usb_serial/console.py +++ b/extra/usb_serial/console.py @@ -97,11 +97,7 @@ class Susb: dev = None if serialname: for d in dev_list: - dev_serial = "PyUSB doesn't have a stable interface" - try: - dev_serial = usb.util.get_string(d, 256, d.iSerialNumber) - except: - dev_serial = usb.util.get_string(d, d.iSerialNumber) + dev_serial = usb.util.get_string(d, d.iSerialNumber) if dev_serial == serialname: dev = d break @@ -110,7 +106,7 @@ class Susb: else: try: dev = dev_list[0] - except: + except IndexError: raise SusbError( "USB device %04x:%04x not found" % (vendor, product) ) @@ -250,7 +246,10 @@ class Suart: Ctrl-C exits. """ -parser = argparse.ArgumentParser(description="Open a console to a USB device") +parser = argparse.ArgumentParser( + description="Open a console to a USB device", + formatter_class=argparse.ArgumentDefaultsHelpFormatter, +) parser.add_argument( "-d", "--device", |