servod: handle gracefully the lack of USB serial number

All the callers assume that usb_get_iserial() is returning a string but
it might return 'None' in case of error (e.g. no serial number on a
device). So update the function to return an empty string rather than
None to avoid dying in random places.
The typical failing case was when running servod with '--serialname'
parameter but no other USB filtering parameter, the daemon would iterate
over all connected USB devices and call usb_get_iserial() for each of
them. If one has no serial number, it would die like this when trying to
apply a string function on 'None':
Traceback (most recent call last):
  File "servod", line 9, in <module>
    load_entry_point('servo==0.0.1.post3721316', 'console_scripts', 'servod')()
  File "servo/", line 518, in main
  File "servo/", line 435, in main_function
    multiservo.parse_rc(logger, options.rcfile))
  File "servo/", line 328, in discover_servo
    all_servos.extend(usb_find(vid, pid, serialname))
  File "servo/", line 173, in usb_find
      (not serialname or usb_get_iserial(device).endswith(serialname)):
  AttributeError: 'NoneType' object has no attribute 'endswith'

Actually, for devices without a serial number string, iSerialNumber is 0.
So doing .getString() on 0 will likely return the string descriptor which
contains non-ascii bytes and will end up triggering exception in various
string conversion routines.
Skip trying to get the string in this case.

TEST=run servod with just the '--serialname' parameter
e.g. 'sudo servod --serial=00025' and see it starting up regardless
of the random USB devices connected to the workstation.

Change-Id: I0804dc06c64559f4772a31a90f9c32120c699294
Commit-Ready: Vincent Palatin <>
Tested-by: Vincent Palatin <>
Reviewed-by: Todd Broch <>
1 file changed