2 #include "opendevice.h"
9 /* private interface: match text and p, return MATCH_SUCCESS, MATCH_FAILED, or MATCH_ABORT. */
10 static int _shellStyleMatch(char *text, char *p)
12 int last, matched, reverse;
14 for(; *p; text++, p++){
15 if(*text == 0 && *p != '*')
19 /* Literal match with following character. */
31 /* Consecutive stars act just like one. */
34 /* Trailing star matches everything. */
37 if((matched = _shellStyleMatch(text++, p)) != MATCH_FAILED)
41 reverse = p[1] == '^';
42 if(reverse) /* Inverted character class. */
44 matched = MATCH_FAILED;
45 if(p[1] == ']' || p[1] == '-')
47 matched = MATCH_SUCCESS;
48 for(last = *p; *++p && *p != ']'; last = *p)
49 if (*p == '-' && p[1] != ']' ? *text <= *++p && *text >= last : *text == *p)
50 matched = MATCH_SUCCESS;
51 if(matched == reverse)
59 /* public interface for shell style matching: returns 0 if fails, 1 if matches */
60 static int shellStyleMatch(char *text, char *pattern)
62 if(pattern == NULL) /* NULL pattern is synonymous to "*" */
64 return _shellStyleMatch(text, pattern) == MATCH_SUCCESS;
67 /* ------------------------------------------------------------------------- */
69 int usbGetStringAscii(usb_dev_handle *dev, int index, char *buf, int buflen)
74 if((rval = usb_get_string_simple(dev, index, buf, buflen)) >= 0) /* use libusb version if it works */
76 if((rval = usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, 0x0409, buffer, sizeof(buffer), 5000)) < 0)
78 if(buffer[1] != USB_DT_STRING){
82 if((unsigned char)buffer[0] < rval)
83 rval = (unsigned char)buffer[0];
85 /* lossy conversion to ISO Latin1: */
87 if(i > buflen) /* destination buffer overflow */
89 buf[i-1] = buffer[2 * i];
90 if(buffer[2 * i + 1] != 0) /* outside of ISO Latin1 range */
97 /* ------------------------------------------------------------------------- */
100 usb_dev_handle **device, int vendorID, char *vendorNamePattern,
101 int productID, char *productNamePattern,
102 char *serialNamePattern, FILE *printMatchingDevicesFp,
107 struct usb_device *dev;
108 usb_dev_handle *handle = NULL;
109 int errorCode = USBOPEN_ERR_NOTFOUND;
113 for(bus = usb_get_busses(); bus; bus = bus->next){
114 for(dev = bus->devices; dev; dev = dev->next){ /* iterate over all devices on all busses */
115 if((vendorID == 0 || dev->descriptor.idVendor == vendorID)
116 && (productID == 0 || dev->descriptor.idProduct == productID)){
117 char vendor[256], product[256], serial[256];
119 handle = usb_open(dev); /* we need to open the device in order to query strings */
121 errorCode = USBOPEN_ERR_ACCESS;
122 if(warningsFp != NULL)
123 fprintf(warningsFp, "Warning: cannot open VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror());
126 /* now check whether the names match: */
128 if(dev->descriptor.iManufacturer > 0){
129 len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, vendor, sizeof(vendor));
132 errorCode = USBOPEN_ERR_ACCESS;
133 if(warningsFp != NULL)
134 fprintf(warningsFp, "Warning: cannot query manufacturer for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror());
136 errorCode = USBOPEN_ERR_NOTFOUND;
137 /* printf("seen device from vendor ->%s<-\n", vendor); */
138 if(shellStyleMatch(vendor, vendorNamePattern)){
139 len = product[0] = 0;
140 if(dev->descriptor.iProduct > 0){
141 len = usbGetStringAscii(handle, dev->descriptor.iProduct, product, sizeof(product));
144 errorCode = USBOPEN_ERR_ACCESS;
145 if(warningsFp != NULL)
146 fprintf(warningsFp, "Warning: cannot query product for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror());
148 errorCode = USBOPEN_ERR_NOTFOUND;
149 /* printf("seen product ->%s<-\n", product); */
150 if(shellStyleMatch(product, productNamePattern)){
152 if(dev->descriptor.iSerialNumber > 0){
153 len = usbGetStringAscii(handle, dev->descriptor.iSerialNumber, serial, sizeof(serial));
156 errorCode = USBOPEN_ERR_ACCESS;
157 if(warningsFp != NULL)
158 fprintf(warningsFp, "Warning: cannot query serial for VID=0x%04x PID=0x%04x: %s\n", dev->descriptor.idVendor, dev->descriptor.idProduct, usb_strerror());
160 if(shellStyleMatch(serial, serialNamePattern)){
161 if(printMatchingDevicesFp != NULL){
163 fprintf(printMatchingDevicesFp, "VID=0x%04x PID=0x%04x vendor=\"%s\" product=\"%s\"\n", dev->descriptor.idVendor, dev->descriptor.idProduct, vendor, product);
165 fprintf(printMatchingDevicesFp, "VID=0x%04x PID=0x%04x vendor=\"%s\" product=\"%s\" serial=\"%s\"\n", dev->descriptor.idVendor, dev->descriptor.idProduct, vendor, product, serial);
179 if(handle) /* we have found a deice */
186 if(printMatchingDevicesFp != NULL) /* never return an error for listing only */
191 /* ------------------------------------------------------------------------- */