18 #include "opendevice.h"
22 #define MATCH_SUCCESS 1
23 #define MATCH_FAILED 0
24 #define MATCH_ABORT -1
27 static int _shellStyleMatch(
char *text,
char *p)
29 int last, matched, reverse;
31 for(; *p; text++, p++){
32 if(*text == 0 && *p !=
'*')
54 if((matched = _shellStyleMatch(text++, p)) != MATCH_FAILED)
58 reverse = p[1] ==
'^';
61 matched = MATCH_FAILED;
62 if(p[1] ==
']' || p[1] ==
'-')
64 matched = MATCH_SUCCESS;
65 for(last = *p; *++p && *p !=
']'; last = *p)
66 if (*p ==
'-' && p[1] !=
']' ? *text <= *++p && *text >= last : *text == *p)
67 matched = MATCH_SUCCESS;
68 if(matched == reverse)
77 static int shellStyleMatch(
char *text,
char *pattern)
81 return _shellStyleMatch(text, pattern) == MATCH_SUCCESS;
86 int usbGetStringAscii(usb_dev_handle *dev,
int index,
char *buf,
int buflen)
91 if((rval = usb_get_string_simple(dev, index, buf, buflen)) >= 0)
93 if((rval = usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, 0x0409, buffer,
sizeof(buffer), 5000)) < 0)
95 if(buffer[1] != USB_DT_STRING){
99 if((
unsigned char)buffer[0] < rval)
100 rval = (
unsigned char)buffer[0];
106 buf[i-1] = buffer[2 * i];
107 if(buffer[2 * i + 1] != 0)
116 int usbOpenDevice(usb_dev_handle **device,
int vendorID,
char *vendorNamePattern,
int productID,
char *productNamePattern,
char *serialNamePattern, FILE *printMatchingDevicesFp, FILE *warningsFp)
119 struct usb_device *dev;
120 usb_dev_handle *handle = NULL;
121 int errorCode = USBOPEN_ERR_NOTFOUND;
125 for(bus = usb_get_busses(); bus; bus = bus->next){
126 for(dev = bus->devices; dev; dev = dev->next){
127 if((vendorID == 0 || dev->descriptor.idVendor == vendorID)
128 && (productID == 0 || dev->descriptor.idProduct == productID)){
129 char vendor[256], product[256], serial[256];
131 handle = usb_open(dev);
133 errorCode = USBOPEN_ERR_ACCESS;
134 if(warningsFp != NULL)
135 fprintf(warningsFp,
"Warning: cannot open VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror());
140 if(dev->descriptor.iManufacturer > 0){
141 len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, vendor,
sizeof(vendor));
144 errorCode = USBOPEN_ERR_ACCESS;
145 if(warningsFp != NULL)
146 fprintf(warningsFp,
"Warning: cannot query manufacturer for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror());
148 errorCode = USBOPEN_ERR_NOTFOUND;
150 if(shellStyleMatch(vendor, vendorNamePattern)){
151 len = product[0] = 0;
152 if(dev->descriptor.iProduct > 0){
153 len = usbGetStringAscii(handle, dev->descriptor.iProduct, product,
sizeof(product));
156 errorCode = USBOPEN_ERR_ACCESS;
157 if(warningsFp != NULL)
158 fprintf(warningsFp,
"Warning: cannot query product for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror());
160 errorCode = USBOPEN_ERR_NOTFOUND;
162 if(shellStyleMatch(product, productNamePattern)){
164 if(dev->descriptor.iSerialNumber > 0){
165 len = usbGetStringAscii(handle, dev->descriptor.iSerialNumber, serial,
sizeof(serial));
168 errorCode = USBOPEN_ERR_ACCESS;
169 if(warningsFp != NULL)
170 fprintf(warningsFp,
"Warning: cannot query serial for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror());
172 if(shellStyleMatch(serial, serialNamePattern)){
173 if(printMatchingDevicesFp != NULL){
175 fprintf(printMatchingDevicesFp,
"VID=0x%04x PID=0x%04x vendor=\"%s\" product=\"%s\"\n", dev->descriptor.idVendor, dev->descriptor.idProduct, vendor, product);
177 fprintf(printMatchingDevicesFp,
"VID=0x%04x PID=0x%04x vendor=\"%s\" product=\"%s\" serial=\"%s\"\n", dev->descriptor.idVendor, dev->descriptor.idProduct, vendor, product, serial);
198 if(printMatchingDevicesFp != NULL)