int usbOpenDevice(usb_dev_handle **device, int vendorID, char *vendorNamePattern, int productID, char *productNamePattern, char *serialNamePattern, FILE *printMatchingDevicesFp, FILE *warningsFp) { struct usb_bus *bus; struct usb_device *dev; usb_dev_handle *handle = NULL; int errorCode = USBOPEN_ERR_NOTFOUND; usb_find_busses(); usb_find_devices(); for(bus = usb_get_busses(); bus; bus = bus->next) { for(dev = bus->devices; dev; dev = dev->next) { if((vendorID == 0 || dev->descriptor.idVendor == vendorID) && (productID == 0 || dev->descriptor.idProduct == productID)) { char vendor[256], product[256], serial[256]; int len; handle = usb_open(dev); if(!handle) { errorCode = USBOPEN_ERR_ACCESS; if(warningsFp != NULL) { fprintf(warningsFp, "Warning: cannot open VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); } continue; } len = vendor[0] = 0; if(dev->descriptor.iManufacturer > 0) { len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, vendor, sizeof(vendor)); } if(len < 0) { errorCode = USBOPEN_ERR_ACCESS; if(warningsFp != NULL) { fprintf(warningsFp, "Warning: cannot query manufacturer for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); } } else { errorCode = USBOPEN_ERR_NOTFOUND; if(shellStyleMatch(vendor, vendorNamePattern)) { len = product[0] = 0; if(dev->descriptor.iProduct > 0) { len = usbGetStringAscii(handle, dev->descriptor.iProduct, product, sizeof(product)); } if(len < 0) { errorCode = USBOPEN_ERR_ACCESS; if(warningsFp != NULL) fprintf(warningsFp, "Warning: cannot query product for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); } else { errorCode = USBOPEN_ERR_NOTFOUND; if(shellStyleMatch(product, productNamePattern)) { len = serial[0] = 0; if(dev->descriptor.iSerialNumber > 0) { len = usbGetStringAscii(handle, dev->descriptor.iSerialNumber, serial, sizeof(serial)); } if(len < 0) { errorCode = USBOPEN_ERR_ACCESS; if(warningsFp != NULL) fprintf(warningsFp, "Warning: cannot query serial for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); } if(shellStyleMatch(serial, serialNamePattern)) { if(printMatchingDevicesFp != NULL) { if(serial[0] == 0) { fprintf(printMatchingDevicesFp, "VID=0x%04x PID=0x%04x vendor=\"%s\" product=\"%s\"\n", dev->descriptor.idVendor, dev->descriptor.idProduct, vendor, product); } else { fprintf(printMatchingDevicesFp, "VID=0x%04x PID=0x%04x vendor=\"%s\" product=\"%s\" serial=\"%s\"\n", dev->descriptor.idVendor, dev->descriptor.idProduct, vendor, product, serial); } } else { break; } } } } } } usb_close(handle); handle = NULL; } } if(handle) { break; } } if(handle != NULL) { errorCode = 0; *device = handle; } if(printMatchingDevicesFp != NULL) { errorCode = 0; } return errorCode; }
int usbOpenDevice(usb_dev_handle ** device, int vendorID, char *vendorNamePattern, int productID, char *productNamePattern, char *serialNamePattern, FILE * printMatchingDevicesFp, FILE * warningsFp) { struct usb_bus *bus; struct usb_device *dev; usb_dev_handle *handle = NULL; int errorCode = USBOPEN_ERR_NOTFOUND; usb_find_busses(); usb_find_devices(); for (bus = usb_get_busses(); bus; bus = bus->next) { for (dev = bus->devices; dev; dev = dev->next) { /* iterate over * all devices * on all * busses */ if ((vendorID == 0 || dev->descriptor.idVendor == vendorID) && (productID == 0 || dev->descriptor.idProduct == productID)) { char vendor[256], product[256], serial[256]; int len; handle = usb_open(dev); /* we need to open the device in order * to query strings */ if (!handle) { errorCode = USBOPEN_ERR_ACCESS; if (warningsFp != NULL) fprintf(warningsFp, "Warning: cannot open VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); continue; } /* * now check whether the names match: */ len = vendor[0] = 0; if (dev->descriptor.iManufacturer > 0) { len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, vendor, sizeof(vendor)); } if (len < 0) { errorCode = USBOPEN_ERR_ACCESS; if (warningsFp != NULL) fprintf(warningsFp, "Warning: cannot query manufacturer for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); } else { errorCode = USBOPEN_ERR_NOTFOUND; /* * printf("seen device from vendor ->%s<-\n", vendor); */ if (shellStyleMatch(vendor, vendorNamePattern)) { len = product[0] = 0; if (dev->descriptor.iProduct > 0) { len = usbGetStringAscii(handle, dev->descriptor.iProduct, product, sizeof(product)); } if (len < 0) { errorCode = USBOPEN_ERR_ACCESS; if (warningsFp != NULL) fprintf(warningsFp, "Warning: cannot query product for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); } else { errorCode = USBOPEN_ERR_NOTFOUND; /* * printf("seen product ->%s<-\n", product); */ if (shellStyleMatch(product, productNamePattern)) { len = serial[0] = 0; if (dev->descriptor.iSerialNumber > 0) { len = usbGetStringAscii(handle, dev->descriptor. iSerialNumber, serial, sizeof(serial)); } if (len < 0) { errorCode = USBOPEN_ERR_ACCESS; if (warningsFp != NULL) fprintf(warningsFp, "Warning: cannot query serial for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); } if (shellStyleMatch(serial, serialNamePattern)) { if (printMatchingDevicesFp != NULL) { if (serial[0] == 0) { fprintf(printMatchingDevicesFp, "VID=0x%04x PID=0x%04x vendor=\"%s\" product=\"%s\"\n", dev->descriptor.idVendor, dev->descriptor.idProduct, vendor, product); } else { fprintf(printMatchingDevicesFp, "VID=0x%04x PID=0x%04x vendor=\"%s\" product=\"%s\" serial=\"%s\"\n", dev->descriptor.idVendor, dev->descriptor.idProduct, vendor, product, serial); } } else { break; } } } } } } usb_close(handle); handle = NULL; } } if (handle) /* we have found a deice */ break; } if (handle != NULL) { errorCode = 0; *device = handle; } if (printMatchingDevicesFp != NULL) /* never return an error for listing * only */ errorCode = 0; return errorCode; }
int usbOpenDevice(usb_dev_handle **device, int vendorID, char *vendorNamePattern, int productID, char *productNamePattern, char *serialNamePattern, FILE *printMatchingDevicesFp, FILE *warningsFp) { struct usb_bus *bus; struct usb_device *dev; usb_dev_handle *handle = NULL; int errorCode = USBOPEN_ERR_NOTFOUND; usb_find_busses(); usb_find_devices(); for(bus = usb_get_busses(); bus; bus = bus->next){ for(dev = bus->devices; dev; dev = dev->next){ /* последовательный опрос всех устройств на всех шинах */ if((vendorID == 0 || dev->descriptor.idVendor == vendorID) && (productID == 0 || dev->descriptor.idProduct == productID)){ char vendor[256], product[256], serial[256]; int len; handle = usb_open(dev); /* Нам нужно открыть устройство в соответсвии строкам запроса */ if(!handle){ errorCode = USBOPEN_ERR_ACCESS; if(warningsFp != NULL) fprintf(warningsFp, "Warning: cannot open VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); continue; } /* теперь проверяем имена на совпадение: */ len = vendor[0] = 0; if(dev->descriptor.iManufacturer > 0){ len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, vendor, sizeof(vendor)); } if(len < 0){ errorCode = USBOPEN_ERR_ACCESS; if(warningsFp != NULL) fprintf(warningsFp, "Warning: cannot query manufacturer for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); }else{ errorCode = USBOPEN_ERR_NOTFOUND; /* printf("seen device from vendor ->%s<-\n", vendor); */ if(shellStyleMatch(vendor, vendorNamePattern)){ len = product[0] = 0; if(dev->descriptor.iProduct > 0){ len = usbGetStringAscii(handle, dev->descriptor.iProduct, product, sizeof(product)); } if(len < 0){ errorCode = USBOPEN_ERR_ACCESS; if(warningsFp != NULL) fprintf(warningsFp, "Warning: cannot query product for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); }else{ errorCode = USBOPEN_ERR_NOTFOUND; /* printf("seen product ->%s<-\n", product); */ if(shellStyleMatch(product, productNamePattern)){ len = serial[0] = 0; if(dev->descriptor.iSerialNumber > 0){ len = usbGetStringAscii(handle, dev->descriptor.iSerialNumber, serial, sizeof(serial)); } if(len < 0){ errorCode = USBOPEN_ERR_ACCESS; if(warningsFp != NULL) fprintf(warningsFp, "Warning: cannot query serial for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror()); } if(shellStyleMatch(serial, serialNamePattern)){ if(printMatchingDevicesFp != NULL){ if(serial[0] == 0){ fprintf(printMatchingDevicesFp, "VID=0x%04x PID=0x%04x vendor=\"%s\" product=\"%s\"\n", dev->descriptor.idVendor, dev->descriptor.idProduct, vendor, product); }else{ fprintf(printMatchingDevicesFp, "VID=0x%04x PID=0x%04x vendor=\"%s\" product=\"%s\" serial=\"%s\"\n", dev->descriptor.idVendor, dev->descriptor.idProduct, vendor, product, serial); } }else{ break; } } } } } } usb_close(handle); handle = NULL; } } if(handle) /* мы нашли устройство */ break; } if(handle != NULL){ errorCode = 0; *device = handle; } if(printMatchingDevicesFp != NULL) /* не возвращать ошибку только для листинга */ errorCode = 0; return errorCode; }