libusb_wrapper.c
4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "libusb_wrapper.h"
void usbinit(libusb_context **context_ptr) {
//libusb_context *context;
int statusInit = libusb_init(context_ptr);
if (statusInit != LIBUSB_SUCCESS) {
perror("libusb_init");
exit(-1);
}
}
void usbclose(libusb_context *context) {
libusb_exit(context);
}
// Si le méchant noyau est passé avant vous :
void _getFromKernel(libusb_device_handle *handle, int interface) { //private method for now
if (libusb_kernel_driver_active(handle, interface)) {
int statusKDriver = libusb_detach_kernel_driver(handle, interface);
if (statusKDriver != LIBUSB_SUCCESS) {
perror("libusb_detach_kernel_driver");
exit(-1);
}
}
}
ssize_t getListDevices(libusb_context *context, libusb_device ***list_ptr) {
ssize_t count = libusb_get_device_list(context, list_ptr);
if (count < 0) {
perror("libusb_get_device_list");
exit(-1);
}
return count;
}
//print readable string, not asked for tutoring
void _printConfig(libusb_device *device) {//private method
// Ouverture du périphérique
libusb_device_handle *handle;
int statusDevice = libusb_open(device, &handle);
if (statusDevice != LIBUSB_SUCCESS) {
perror("libusb_open");
//return; //exit(-1);
}
int MAXLEN_DESCRIPTOR_STRING = 200;
uint8_t desc_idx = 2;
//uint16_t langid = 16;
unsigned char data[200];
// TEST 16
for (desc_idx = 0; desc_idx < 16; desc_idx++) {
int statusAscii = libusb_get_string_descriptor_ascii(
handle, desc_idx, data, MAXLEN_DESCRIPTOR_STRING);
if (statusAscii == -9) // TEST seems to be LIBUSB_ERROR
break;
printf(" - Descriptor string : %s ; %i\n", data, statusAscii);
}
libusb_close(handle);
}
void displayDevices(libusb_context *context) {
libusb_device **list;
ssize_t count = getListDevices(context, &list);
ssize_t i = 0;
for (i = 0; i < count; i++) {
libusb_device *device = list[i];
struct libusb_device_descriptor desc;
int status = libusb_get_device_descriptor(device, &desc);
if (status != LIBUSB_SUCCESS){
printf("Cannot get device desc : %s\n", libusb_error_name(status)); //DBGONLY
perror("libusb_open");
continue;
}
uint8_t bus = libusb_get_bus_number(device);
uint8_t address = libusb_get_device_address(device);
printf("Device Found @ (Bus:Address) %d:%d\n", bus, address);
printf("Vendor ID 0x0%x\n", desc.idVendor);
printf("Product ID 0x0%x\n", desc.idProduct);
//_printConfig(device); //Does it work ?
//displayDeviceEndpoints(device); //Not really work on Axel@Alptop
}
libusb_free_device_list(list, 1);
}
void displayDeviceEndpoints(libusb_device *device) {
printf("Yes you're in\n");
// Ouverture du périphérique
libusb_device_handle *handle;
int statusDevice = libusb_open(device, &handle);
if (statusDevice != LIBUSB_SUCCESS) {
printf("This error is : %d\n", statusDevice); //DBGONLY
printf("Printable error : %s\n", libusb_error_name(statusDevice)); //DBGONLY
perror("libusb_open");
return;
}
// lectures des configs?
int configuration = 0; // valueof("bConfigurationValue");
int statusConfig = libusb_set_configuration(handle, configuration);
if (statusConfig != LIBUSB_SUCCESS) {
perror("libusb_set_configuration");
return;
}
// 4.2 configuration du périph usb
struct libusb_config_descriptor *config;
int statusFetchConfig = libusb_get_active_config_descriptor(device, &config);
if (statusFetchConfig != LIBUSB_SUCCESS) {
perror("libusb_get_active_config_descriptor");
return;
}
printf("Config.bConfigurationValue : %d\n", config->bConfigurationValue);
printf("Config/ bLength:%d;bDescriptorType:%d;bNumInterfaces:%d",
config->bLength, config->bDescriptorType, config->bNumInterfaces);
// itération de l'interface
for (int indexInterface = 0; indexInterface < config->bNumInterfaces;
indexInterface++) {
// struct libusb_interface * interface =
// &config->interface[indexInterface];
printf("^%d", indexInterface);
printf("Alt%d", config->interface[indexInterface].num_altsetting);
}
printf("\n");
libusb_close(handle);
}