2011/02/14

libusb 初探2--libusb_get_device_list & libusb_get_device_descriptor

libusb_get_device_list() & libusb_get_device_descriptor() 這兩個函式,除了在 examples/lsusb.c 中出現範例之外,參考文件可以見libusb descriptor

攤開 libusb 的原始碼,裡面有一個 libusb_open_device_with_vid_pid(), 剛好就用了這兩個 api, 列之如下:


API_EXPORTED libusb_device_handle *libusb_open_device_with_vid_pid(
libusb_context *ctx, uint16_t vendor_id, uint16_t product_id)
{
struct libusb_device **devs;
struct libusb_device *found = NULL;
struct libusb_device *dev;
struct libusb_device_handle *handle = NULL;
size_t i = 0;
int r;

if (libusb_get_device_list(ctx, &devs) < 0) return NULL; while ((dev = devs[i++]) != NULL) { struct libusb_device_descriptor desc; r = libusb_get_device_descriptor(dev, &desc); if (r < 0) goto out; if (desc.idVendor == vendor_id && desc.idProduct == product_id) { found = dev; break; } } if (found) { r = libusb_open(found, &handle); if (r < 0) handle = NULL; } out: libusb_free_device_list(devs, 1); return handle; }

比較特別的就是多了 libusb_open()

0 意見: