1
0
mirror of git://projects.qi-hardware.com/ben-wpan.git synced 2024-09-30 00:46:21 +03:00

tools/lib/usbopen.c: make vendor and/or product optional

- usbopen.c (open_usb): determine vendor and product objective outside
  the loop
- usbopen.c (open_usb): check vendor and product only if non-zero
- usbopen.c (bad_id, parse_usb_id): vendor and product can now be
  omitted
This commit is contained in:
Werner Almesberger 2011-05-11 03:40:03 -03:00
parent 3deac4165e
commit 55d220b400

View File

@ -62,15 +62,18 @@ usb_dev_handle *open_usb(uint16_t default_vendor, uint16_t default_product)
initialize(); initialize();
if (!vendor)
vendor = default_vendor;
if (!product)
product = default_product;
for (bus = usb_get_busses(); bus; bus = bus->next) for (bus = usb_get_busses(); bus; bus = bus->next)
for (dev = bus->devices; dev; dev = dev->next) { for (dev = bus->devices; dev; dev = dev->next) {
if (restricted_path && restricted_path != dev) if (restricted_path && restricted_path != dev)
continue; continue;
if (dev->descriptor.idVendor != if (vendor && dev->descriptor.idVendor != vendor)
(vendor ? vendor : default_vendor))
continue; continue;
if (dev->descriptor.idProduct != if (product && dev->descriptor.idProduct != product)
(product ? product : default_product))
continue; continue;
handle = usb_open(dev); handle = usb_open(dev);
#ifdef DO_FULL_USB_BUREAUCRACY #ifdef DO_FULL_USB_BUREAUCRACY
@ -103,7 +106,7 @@ usb_dev_handle *open_usb(uint16_t default_vendor, uint16_t default_product)
static void bad_id(const char *id) static void bad_id(const char *id)
{ {
fprintf(stderr, "\"%s\" is not a valid vendor:product ID\n", id); fprintf(stderr, "\"%s\" is not a valid [vendor]:[product] ID\n", id);
exit(1); exit(1);
} }
@ -113,18 +116,29 @@ void parse_usb_id(const char *id)
unsigned long tmp; unsigned long tmp;
char *end; char *end;
tmp = strtoul(id, &end, 16); if (*id == ':') {
if (*end != ':') vendor = 0;
end = (char *) id; /* ugly */
} else {
tmp = strtoul(id, &end, 16);
if (*end != ':')
bad_id(id);
if (tmp > 0xffff)
bad_id(id);
vendor = tmp;
}
if (!*end)
bad_id(id); bad_id(id);
if (tmp > 0xffff) if (!end[1])
bad_id(id); product = 0;
vendor = tmp; else {
tmp = strtoul(end+1, &end, 16); tmp = strtoul(end+1, &end, 16);
if (*end) if (*end)
bad_id(id); bad_id(id);
if (tmp > 0xffff) if (tmp > 0xffff)
bad_id(id); bad_id(id);
product = tmp; product = tmp;
}
} }