FreeCalypso > hg > fc-usbser-tools
comparison libuwrap/find_busdev.c @ 10:fd3fcba5a8ac
libuwrap: implement locating by bus/dev
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 07 Sep 2023 04:18:30 +0000 |
parents | libuwrap/find_matchspec.c@ab506f6aa57c |
children |
comparison
equal
deleted
inserted
replaced
9:ab506f6aa57c | 10:fd3fcba5a8ac |
---|---|
1 /* | |
2 * In this module we implement the function that locates a USB device | |
3 * by bus and device number/filename strings. | |
4 */ | |
5 | |
6 #include <stdio.h> | |
7 #include <stdlib.h> | |
8 #include <string.h> | |
9 #include <strings.h> | |
10 #include <usb.h> | |
11 #include "find_dev.h" | |
12 #include "prelim_init.h" | |
13 | |
14 struct usb_device * | |
15 find_usbdev_by_busdev(const char *bus_dev_spec) | |
16 { | |
17 const char *slash; | |
18 unsigned bus_name_len; | |
19 struct usb_bus *bus; | |
20 struct usb_device *dev; | |
21 | |
22 slash = index(bus_dev_spec, '/'); | |
23 if (!slash) { | |
24 fprintf(stderr, | |
25 "error: bus/dev path expected, but no slash found\n"); | |
26 exit(1); | |
27 } | |
28 bus_name_len = slash - bus_dev_spec; | |
29 libusb_prelim_init(); | |
30 for (bus = usb_get_busses(); bus; bus = bus->next) { | |
31 if (strlen(bus->dirname) != bus_name_len) | |
32 continue; | |
33 if (strncmp(bus->dirname, bus_dev_spec, bus_name_len)) | |
34 continue; | |
35 for (dev = bus->devices; dev; dev = dev->next) { | |
36 if (!strcmp(dev->filename, slash + 1)) | |
37 return dev; | |
38 } | |
39 } | |
40 return 0; | |
41 } |