Commit 533260c1d0481c8ad234a1543a0dc73cc868ea9a
1 parent
3cacbf71
Ajout des lectures des string de descripteur
Showing
1 changed file
with
80 additions
and
43 deletions
Show diff stats
src/GetUsbInfos.c
1 | -#include <stdio.h> | |
2 | 1 | #include <libusb-1.0/libusb.h> |
2 | +#include <stdio.h> | |
3 | 3 | #include <stdlib.h> |
4 | 4 | |
5 | +int main() { | |
6 | + | |
7 | + // initiate the usb thing | |
8 | + | |
9 | + libusb_context *context; | |
10 | + int statusInit = libusb_init(&context); | |
11 | + if (statusInit != 0) { | |
12 | + perror("libusb_init"); | |
13 | + exit(-1); | |
14 | + } | |
15 | + /* ... some code ... */ | |
16 | + | |
17 | + // actually enumerates all the usb and gives some infos abot the usb things | |
18 | + | |
19 | + libusb_device **list; | |
20 | + ssize_t count = libusb_get_device_list(context, &list); | |
21 | + if (count < 0) { | |
22 | + perror("libusb_get_device_list"); | |
23 | + exit(-1); | |
24 | + } | |
25 | + ssize_t i = 0; | |
26 | + for (i = 0; i < count; i++) { | |
27 | + libusb_device *device = list[i]; | |
28 | + struct libusb_device_descriptor desc; | |
29 | + | |
30 | + int status = libusb_get_device_descriptor(device, &desc); | |
31 | + if (status != 0) | |
32 | + continue; | |
33 | + | |
34 | + uint8_t bus = libusb_get_bus_number(device); | |
35 | + uint8_t address = libusb_get_device_address(device); | |
36 | + | |
37 | + printf("Device Found @ (Bus:Address) %d:%d\n", bus, address); | |
38 | + printf("Vendor ID 0x0%x\n", desc.idVendor); | |
39 | + printf("Product ID 0x0%x\n", desc.idProduct); | |
40 | + | |
41 | + // Ouverture du périphérique | |
42 | + libusb_device_handle *handle; | |
43 | + int statusDevice = libusb_open(device, &handle); | |
44 | + if (statusDevice != 0) { | |
45 | + perror("libusb_open"); | |
46 | + exit(-1); | |
47 | + } | |
48 | +/* | |
49 | + // Si le méchant noyau est passé avant vous : | |
50 | + int interface = 0; // TEST : FONCTIONNE MAIS CORRESPOND A QUOI? | |
51 | + if (libusb_kernel_driver_active(handle, interface)) { | |
52 | + int statusKDriver = libusb_detach_kernel_driver(handle, interface); | |
53 | + if (statusKDriver != 0) { | |
54 | + perror("libusb_detach_kernel_driver"); | |
55 | + exit(-1); | |
56 | + } | |
57 | + } | |
58 | + | |
59 | + // lectures des configs? | |
60 | + int configuration = 0; // valueof("bConfigurationValue"); | |
61 | + int statusConfig = libusb_set_configuration(handle, configuration); | |
62 | + if (statusConfig != 0) { | |
63 | + perror("libusb_set_configuration"); /*exit(-1);*/ | |
64 | + /* } | |
65 | +*/ | |
66 | + int MAXLEN_DESCRIPTOR_STRING = 200; | |
67 | + uint8_t desc_idx = 2; | |
68 | + uint16_t langid = 16; | |
69 | + unsigned char data[200]; | |
70 | + | |
71 | + | |
72 | + for(desc_idx = 0; desc_idx < 5; desc_idx++){ | |
73 | + libusb_get_string_descriptor_ascii(handle, desc_idx, | |
74 | + data, MAXLEN_DESCRIPTOR_STRING); | |
75 | + | |
76 | + printf("Descriptor string : %s\n", data); | |
77 | +} | |
78 | + /* ... some ... code */ | |
79 | + libusb_close(handle); | |
80 | + } | |
5 | 81 | |
82 | + libusb_free_device_list(list, 1); | |
6 | 83 | |
7 | - | |
8 | - | |
9 | - | |
10 | -int main(){ | |
11 | - | |
12 | - | |
13 | - | |
14 | - | |
15 | - | |
16 | - | |
17 | -//initiate the usb thing | |
18 | - | |
19 | - libusb_context *context; | |
20 | - int status=libusb_init(&context); | |
21 | - if(status!=0) {perror("libusb_init"); exit(-1);} | |
22 | - /* ... some code ... */ | |
23 | - | |
24 | - | |
25 | -// actually enumerates all the usb and gives some infos abot the usb things | |
26 | - | |
27 | - libusb_device **list; | |
28 | - ssize_t count=libusb_get_device_list(context,&list); | |
29 | - if(count<0) {perror("libusb_get_device_list"); exit(-1);} | |
30 | - ssize_t i=0; | |
31 | - for(i=0;i<count;i++){ | |
32 | - libusb_device *device=list[i]; | |
33 | - struct libusb_device_descriptor desc; | |
34 | - | |
35 | - int status=libusb_get_device_descriptor(device,&desc); | |
36 | - if(status!=0) continue; | |
37 | - | |
38 | - uint8_t bus=libusb_get_bus_number(device); | |
39 | - uint8_t address=libusb_get_device_address(device); | |
40 | - | |
41 | - printf("Device Found @ (Bus:Address) %d:%d\n",bus,address); | |
42 | - printf("Vendor ID 0x0%x\n",desc.idVendor); | |
43 | - printf("Product ID 0x0%x\n",desc.idProduct); | |
44 | - } | |
45 | - libusb_free_device_list(list,1); | |
46 | - | |
47 | - libusb_exit(context); | |
48 | - return 0; | |
84 | + libusb_exit(context); | |
85 | + return 0; | |
49 | 86 | } | ... | ... |