From: Gerhard Sittig Date: Tue, 18 Jun 2019 17:06:49 +0000 (+0200) Subject: serial_hid: improve and tighten "find USB" for the HID transport X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=a8fcee15e0308042143c21e531f470110968bc68;p=libsigrok.git serial_hid: improve and tighten "find USB" for the HID transport The .find_usb() method of the HID transport previously returned connections which lacked the "hid/" prefix and chip type, thus could later not be used with open() and data communication calls. Add the missing decoration. As a minor optimization, lookup the chip type and name from the caller's VID:PID pair already if it's specific enough, still lookup the chip for every enumerated device if the caller's VID:PID pair is unspecific. Tighten the result set of the HID transport's USB search. Only return connections which involve one of the supported chips. The other devices very probably cannot be used to connect to measurement equipment anyway, listing mice and keyboards and other unrelated stuff was found to be not so useful. --- diff --git a/src/serial_hid.c b/src/serial_hid.c index 221bb6af..759a58c4 100644 --- a/src/serial_hid.c +++ b/src/serial_hid.c @@ -251,19 +251,36 @@ static GSList *ser_hid_hidapi_list(GSList *list, sr_ser_list_append_t append) } /* - * The HIDAPI specific find_usb() callback, invoked by common serial.c code. - * Enumerate devices for the specified VID:PID pair. - * Invoke an "append" callback with 'path' for the device. + * Enumerate devices for the specified VID:PID pair. Invoke an "append" + * callback with 'path' for found devices. Exclusively finds supported + * chip types, skips unknown VID:PID pairs (even if caller specified). */ static GSList *ser_hid_hidapi_find_usb(GSList *list, sr_ser_find_append_t append, uint16_t vendor_id, uint16_t product_id) { + const char *caller_chip; + const char *dev_chip; struct hid_device_info *devs, *curdev; const char *name; + char *path; + + caller_chip = ser_hid_chip_find_name_vid_pid(vendor_id, product_id); devs = hid_enumerate(vendor_id, product_id); for (curdev = devs; curdev; curdev = curdev->next) { - name = curdev->path; + dev_chip = caller_chip; + if (!dev_chip) { + dev_chip = ser_hid_chip_find_name_vid_pid( + curdev->vendor_id, curdev->product_id); + } + if (!dev_chip) + continue; + path = get_hidapi_path_copy(curdev->path); + if (!path) + continue; + name = g_strdup_printf("%s/%s/%s", + SER_HID_CONN_PREFIX, dev_chip, path); + g_free(path); list = append(list, name); } hid_free_enumeration(devs);