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