comparison libuwrap/find_desc_str.c @ 11:fe4231326fb2

libuwrap: implement locating by description-string
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 07 Sep 2023 04:58:16 +0000
parents libuwrap/find_matchspec.c@ab506f6aa57c
children
comparison
equal deleted inserted replaced
10:fd3fcba5a8ac 11:fe4231326fb2
1 /*
2 * In this module we implement the function that locates a USB device
3 * by libftdi-style description-string.
4 */
5
6 #include <ctype.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <strings.h>
11 #include <usb.h>
12 #include "find_dev.h"
13
14 struct usb_device *
15 find_usbdev_by_desc_string(const char *desc_string)
16 {
17 struct usbdev_matchspec match;
18 const char *cp;
19 char *endp;
20
21 if (!desc_string[0] || desc_string[1] != ':') {
22 inv_syntax: fprintf(stderr,
23 "error: USB device selector string has invalid syntax\n");
24 exit(1);
25 }
26 switch (desc_string[0]) {
27 case 'd':
28 return find_usbdev_by_busdev(desc_string + 2);
29 case 'i':
30 case 's':
31 break;
32 default:
33 goto inv_syntax;
34 }
35 bzero(&match, sizeof match);
36 cp = desc_string + 2;
37 if (!isdigit(*cp))
38 goto inv_syntax;
39 match.usb_vid = strtoul(cp, &endp, 0);
40 cp = endp;
41 if (*cp++ != ':')
42 goto inv_syntax;
43 if (!isdigit(*cp))
44 goto inv_syntax;
45 match.usb_pid = strtoul(cp, &endp, 0);
46 cp = endp;
47 if (!*cp) {
48 if (desc_string[0] != 'i')
49 goto inv_syntax;
50 return find_usbdev_by_matchspec(&match);
51 }
52 if (*cp++ != ':')
53 goto inv_syntax;
54 switch (desc_string[0]) {
55 case 'i':
56 if (!isdigit(*cp))
57 goto inv_syntax;
58 match.index = strtoul(cp, &endp, 0);
59 if (*endp)
60 goto inv_syntax;
61 break;
62 case 's':
63 match.serial = cp;
64 }
65 return find_usbdev_by_matchspec(&match);
66 }