2 * Project: AVR-USB host-side library
3 * Author: Christian Starkjohann
4 * Creation Date: 2008-04-10
6 * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
7 * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
8 * This Revision: $Id: opendevice.c 692 2008-11-07 15:07:40Z cs $
13 The functions in this module can be used to find and open a device based on
14 libusb or libusb-win32.
18 #include "opendevice.h"
20 /* ------------------------------------------------------------------------- */
22 #define MATCH_SUCCESS 1
23 #define MATCH_FAILED 0
24 #define MATCH_ABORT -1
26 /* private interface: match text and p, return MATCH_SUCCESS, MATCH_FAILED, or MATCH_ABORT. */
27 static int _shellStyleMatch(char *text, char *p)
29 int last, matched, reverse;
31 for(; *p; text++, p++){
32 if(*text == 0 && *p != '*')
36 /* Literal match with following character. */
48 /* Consecutive stars act just like one. */
51 /* Trailing star matches everything. */
54 if((matched = _shellStyleMatch(text++, p)) != MATCH_FAILED)
58 reverse = p[1] == '^';
59 if(reverse) /* Inverted character class. */
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)
76 /* public interface for shell style matching: returns 0 if fails, 1 if matches */
77 static int shellStyleMatch(char *text, char *pattern)
79 if(pattern == NULL) /* NULL pattern is synonymous to "*" */
81 return _shellStyleMatch(text, pattern) == MATCH_SUCCESS;
84 /* ------------------------------------------------------------------------- */
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) /* use libusb version if it works */
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];
102 /* lossy conversion to ISO Latin1: */
104 if(i > buflen) /* destination buffer overflow */
106 buf[i-1] = buffer[2 * i];
107 if(buffer[2 * i + 1] != 0) /* outside of ISO Latin1 range */
114 /* ------------------------------------------------------------------------- */
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){ /* iterate over all devices on all busses */
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); /* we need to open the device in order to query strings */
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());
138 /* now check whether the names match: */
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;
149 /* printf("seen device from vendor ->%s<-\n", vendor); */
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;
161 /* printf("seen product ->%s<-\n", product); */
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);
191 if(handle) /* we have found a deice */
198 if(printMatchingDevicesFp != NULL) /* never return an error for listing only */
203 /* ------------------------------------------------------------------------- */