opendevice.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Name: opendevice.c Project: V-USB host-side library Author: Christian
  3. * Starkjohann Creation Date: 2008-04-10 Tabsize: 4 Copyright: (c) 2008 by
  4. * OBJECTIVE DEVELOPMENT Software GmbH License: GNU GPL v2 (see License.txt),
  5. * GNU GPL v3 or proprietary (CommercialLicense.txt) This Revision: $Id:
  6. * opendevice.c 740 2009-04-13 18:23:31Z cs $
  7. */
  8. /*
  9. * General Description: The functions in this module can be used to find and
  10. * open a device based on libusb or libusb-win32.
  11. */
  12. #include <stdio.h>
  13. #include "opendevice.h"
  14. /*
  15. * -------------------------------------------------------------------------
  16. */
  17. #define MATCH_SUCCESS 1
  18. #define MATCH_FAILED 0
  19. #define MATCH_ABORT -1
  20. /*
  21. * private interface: match text and p, return MATCH_SUCCESS, MATCH_FAILED, or
  22. * MATCH_ABORT.
  23. */
  24. static int _shellStyleMatch(char *text, char *p)
  25. {
  26. int last,
  27. matched,
  28. reverse;
  29. for (; *p; text++, p++) {
  30. if (*text == 0 && *p != '*')
  31. return MATCH_ABORT;
  32. switch (*p) {
  33. case '\\':
  34. /*
  35. * Literal match with following character.
  36. */
  37. p++;
  38. /*
  39. * FALLTHROUGH
  40. */
  41. default:
  42. if (*text != *p)
  43. return MATCH_FAILED;
  44. continue;
  45. case '?':
  46. /*
  47. * Match anything.
  48. */
  49. continue;
  50. case '*':
  51. while (*++p == '*')
  52. /*
  53. * Consecutive stars act just like one.
  54. */
  55. continue;
  56. if (*p == 0)
  57. /*
  58. * Trailing star matches everything.
  59. */
  60. return MATCH_SUCCESS;
  61. while (*text)
  62. if ((matched = _shellStyleMatch(text++, p)) != MATCH_FAILED)
  63. return matched;
  64. return MATCH_ABORT;
  65. case '[':
  66. reverse = p[1] == '^';
  67. if (reverse) /* Inverted character class. */
  68. p++;
  69. matched = MATCH_FAILED;
  70. if (p[1] == ']' || p[1] == '-')
  71. if (*++p == *text)
  72. matched = MATCH_SUCCESS;
  73. for (last = *p; *++p && *p != ']'; last = *p)
  74. if (*p == '-' && p[1] != ']' ? *text <= *++p
  75. && *text >= last : *text == *p)
  76. matched = MATCH_SUCCESS;
  77. if (matched == reverse)
  78. return MATCH_FAILED;
  79. continue;
  80. }
  81. }
  82. return *text == 0;
  83. }
  84. /*
  85. * public interface for shell style matching: returns 0 if fails, 1 if matches
  86. */
  87. static int shellStyleMatch(char *text, char *pattern)
  88. {
  89. if (pattern == NULL) /* NULL pattern is synonymous to "*" */
  90. return 1;
  91. return _shellStyleMatch(text, pattern) == MATCH_SUCCESS;
  92. }
  93. /*
  94. * -------------------------------------------------------------------------
  95. */
  96. int usbGetStringAscii(usb_dev_handle * dev, int index, char *buf, int buflen)
  97. {
  98. char buffer[256];
  99. int rval,
  100. i;
  101. if ((rval = usb_get_string_simple(dev, index, buf, buflen)) >= 0) /* use
  102. * libusb
  103. * version
  104. * if
  105. * it
  106. * works
  107. */
  108. return rval;
  109. if ((rval =
  110. usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR,
  111. (USB_DT_STRING << 8) + index, 0x0409, buffer,
  112. sizeof(buffer), 5000)) < 0)
  113. return rval;
  114. if (buffer[1] != USB_DT_STRING) {
  115. *buf = 0;
  116. return 0;
  117. }
  118. if ((unsigned char) buffer[0] < rval)
  119. rval = (unsigned char) buffer[0];
  120. rval /= 2;
  121. /*
  122. * lossy conversion to ISO Latin1:
  123. */
  124. for (i = 1; i < rval; i++) {
  125. if (i > buflen) /* destination buffer overflow */
  126. break;
  127. buf[i - 1] = buffer[2 * i];
  128. if (buffer[2 * i + 1] != 0) /* outside of ISO Latin1 range */
  129. buf[i - 1] = '?';
  130. }
  131. buf[i - 1] = 0;
  132. return i - 1;
  133. }
  134. /*
  135. * -------------------------------------------------------------------------
  136. */
  137. int usbOpenDevice(usb_dev_handle ** device, int vendorID,
  138. char *vendorNamePattern, int productID,
  139. char *productNamePattern, char *serialNamePattern,
  140. FILE * printMatchingDevicesFp, FILE * warningsFp)
  141. {
  142. struct usb_bus *bus;
  143. struct usb_device *dev;
  144. usb_dev_handle *handle = NULL;
  145. int errorCode = USBOPEN_ERR_NOTFOUND;
  146. usb_find_busses();
  147. usb_find_devices();
  148. for (bus = usb_get_busses(); bus; bus = bus->next) {
  149. for (dev = bus->devices; dev; dev = dev->next) { /* iterate over
  150. * all devices
  151. * on all
  152. * busses */
  153. if ((vendorID == 0 || dev->descriptor.idVendor == vendorID)
  154. && (productID == 0 || dev->descriptor.idProduct == productID)) {
  155. char vendor[256],
  156. product[256],
  157. serial[256];
  158. int len;
  159. handle = usb_open(dev); /* we need to open the device in order
  160. * to query strings */
  161. if (!handle) {
  162. errorCode = USBOPEN_ERR_ACCESS;
  163. if (warningsFp != NULL)
  164. fprintf(warningsFp,
  165. "Warning: cannot open VID=0x%04x PID=0x%04x: %s\n",
  166. dev->descriptor.idVendor,
  167. dev->descriptor.idProduct, usb_strerror());
  168. continue;
  169. }
  170. /*
  171. * now check whether the names match:
  172. */
  173. len = vendor[0] = 0;
  174. if (dev->descriptor.iManufacturer > 0) {
  175. len =
  176. usbGetStringAscii(handle, dev->descriptor.iManufacturer,
  177. vendor, sizeof(vendor));
  178. }
  179. if (len < 0) {
  180. errorCode = USBOPEN_ERR_ACCESS;
  181. if (warningsFp != NULL)
  182. fprintf(warningsFp,
  183. "Warning: cannot query manufacturer for VID=0x%04x PID=0x%04x: %s\n",
  184. dev->descriptor.idVendor,
  185. dev->descriptor.idProduct, usb_strerror());
  186. } else {
  187. errorCode = USBOPEN_ERR_NOTFOUND;
  188. /*
  189. * printf("seen device from vendor ->%s<-\n", vendor);
  190. */
  191. if (shellStyleMatch(vendor, vendorNamePattern)) {
  192. len = product[0] = 0;
  193. if (dev->descriptor.iProduct > 0) {
  194. len =
  195. usbGetStringAscii(handle,
  196. dev->descriptor.iProduct,
  197. product, sizeof(product));
  198. }
  199. if (len < 0) {
  200. errorCode = USBOPEN_ERR_ACCESS;
  201. if (warningsFp != NULL)
  202. fprintf(warningsFp,
  203. "Warning: cannot query product for VID=0x%04x PID=0x%04x: %s\n",
  204. dev->descriptor.idVendor,
  205. dev->descriptor.idProduct,
  206. usb_strerror());
  207. } else {
  208. errorCode = USBOPEN_ERR_NOTFOUND;
  209. /*
  210. * printf("seen product ->%s<-\n", product);
  211. */
  212. if (shellStyleMatch(product, productNamePattern)) {
  213. len = serial[0] = 0;
  214. if (dev->descriptor.iSerialNumber > 0) {
  215. len =
  216. usbGetStringAscii(handle,
  217. dev->descriptor.
  218. iSerialNumber, serial,
  219. sizeof(serial));
  220. }
  221. if (len < 0) {
  222. errorCode = USBOPEN_ERR_ACCESS;
  223. if (warningsFp != NULL)
  224. fprintf(warningsFp,
  225. "Warning: cannot query serial for VID=0x%04x PID=0x%04x: %s\n",
  226. dev->descriptor.idVendor,
  227. dev->descriptor.idProduct,
  228. usb_strerror());
  229. }
  230. if (shellStyleMatch(serial, serialNamePattern)) {
  231. if (printMatchingDevicesFp != NULL) {
  232. if (serial[0] == 0) {
  233. fprintf(printMatchingDevicesFp,
  234. "VID=0x%04x PID=0x%04x vendor=\"%s\" product=\"%s\"\n",
  235. dev->descriptor.idVendor,
  236. dev->descriptor.idProduct,
  237. vendor, product);
  238. } else {
  239. fprintf(printMatchingDevicesFp,
  240. "VID=0x%04x PID=0x%04x vendor=\"%s\" product=\"%s\" serial=\"%s\"\n",
  241. dev->descriptor.idVendor,
  242. dev->descriptor.idProduct,
  243. vendor, product, serial);
  244. }
  245. } else {
  246. break;
  247. }
  248. }
  249. }
  250. }
  251. }
  252. }
  253. usb_close(handle);
  254. handle = NULL;
  255. }
  256. }
  257. if (handle) /* we have found a deice */
  258. break;
  259. }
  260. if (handle != NULL) {
  261. errorCode = 0;
  262. *device = handle;
  263. }
  264. if (printMatchingDevicesFp != NULL) /* never return an error for listing
  265. * only */
  266. errorCode = 0;
  267. return errorCode;
  268. }
  269. /*
  270. * -------------------------------------------------------------------------
  271. */