c++ - Open a device by name using libftdi or libusb -
i using libftdi multiple ftdi devices program running on ubuntu 14.04. have udev rule detects devices based on custom manufacturer string , gives them symlink in dev directory. similar /dev/my-device
. use libftdi open device using string instead of pid/vid/serial number.
i did not see capability available in libftdi checked libusb , didn't see functionality either.
you try this:
static int usbgetdescriptorstring(usb_dev_handle *dev, int index, int langid, char *buf, int buflen) { char buffer[256]; int rval, i; // make standard request get_descriptor, type string , given index // (e.g. dev->iproduct) rval = usb_control_msg(dev, usb_type_standard | usb_recip_device | usb_endpoint_in, usb_req_get_descriptor, (usb_dt_string << 8) + index, langid, buffer, sizeof(buffer), 1000); if (rval < 0) // error return rval; // rval should bytes read, buffer[0] contains actual response size if ((unsigned char)buffer[0] < rval) rval = (unsigned char)buffer[0]; // string shorter bytes read if (buffer[1] != usb_dt_string) // second byte data type return 0; // invalid return type // we're dealing utf-16le here actual chars half of rval, // , index 0 doesn't count rval /= 2; /* lossy conversion iso latin1 */ (i = 1; < rval && < buflen; i++) { if (buffer[2 * + 1] == 0) buf[i - 1] = buffer[2 * i]; else buf[i - 1] = '?'; /* outside of iso latin1 range */ } buf[i - 1] = 0; return - 1; } static usb_dev_handle * usbopendevice(int vendor, char *vendorname, int product, char *productname) { struct usb_bus *bus; struct usb_device *dev; char devvendor[256], devproduct[256]; usb_dev_handle * handle = null; usb_init(); usb_find_busses(); usb_find_devices(); (bus = usb_get_busses(); bus; bus = bus->next) { (dev = bus->devices; dev; dev = dev->next) { if (dev->descriptor.idvendor != vendor || dev->descriptor.idproduct != product) continue; /* need open device in order query strings */ if (!(handle = usb_open(dev))) { fprintf(stderr, "warning: cannot open usb device: %sn", usb_strerror()); continue; } /* vendor name */ if (usbgetdescriptorstring(handle, dev->descriptor.imanufacturer, 0x0409, devvendor, sizeof(devvendor)) < 0) { fprintf(stderr, "warning: cannot query manufacturer device: %sn", usb_strerror()); usb_close(handle); continue; } /* product name */ if (usbgetdescriptorstring(handle, dev->descriptor.iproduct, 0x0409, devproduct, sizeof(devvendor)) < 0) { fprintf(stderr, "warning: cannot query product device: %sn", usb_strerror()); usb_close(handle); continue; } if (strcmp(devvendor, vendorname) == 0 && strcmp(devproduct, productname) == 0) return handle; else usb_close(handle); } } return null; }
Comments
Post a Comment