Commit dd886d58e7c21dfdf32a5a298fbd377b7a107874

Authored by achemin1
1 parent a6e19c45

refactor du code avec clang-format + enlever la boucle infinie

Showing 3 changed files with 372 additions and 350 deletions   Show diff stats
include/libusb_wrapper.h
@@ -2,34 +2,34 @@ @@ -2,34 +2,34 @@
2 #define LIBUSB_WRAPPER_H_ 2 #define LIBUSB_WRAPPER_H_
3 3
4 #include <libusb-1.0/libusb.h> 4 #include <libusb-1.0/libusb.h>
5 -#include <stdlib.h>  
6 -#include <stdio.h>  
7 #include <stdbool.h> 5 #include <stdbool.h>
  6 +#include <stdio.h>
  7 +#include <stdlib.h>
8 8
9 -void usbinit(libusb_context** context_ptr);  
10 -void usbclose(libusb_context* context); 9 +void usbinit(libusb_context **context_ptr);
  10 +void usbclose(libusb_context *context);
11 11
12 -void interfaceclaim(libusb_device_handle *handle, struct libusb_interface *interface);  
13 -void interfaceclose(libusb_device_handle *handle, struct libusb_interface *interface); 12 +void interfaceclaim(libusb_device_handle *handle,
  13 + struct libusb_interface *interface);
  14 +void interfaceclose(libusb_device_handle *handle,
  15 + struct libusb_interface *interface);
14 16
15 -ssize_t getListDevices(libusb_context* context, libusb_device***list_ptr); 17 +ssize_t getListDevices(libusb_context *context, libusb_device ***list_ptr);
16 void getFromKernel(libusb_device_handle *handle, int interface); 18 void getFromKernel(libusb_device_handle *handle, int interface);
17 19
18 -  
19 void displayDevices(libusb_context *context); 20 void displayDevices(libusb_context *context);
20 void displayDevicesMore(libusb_context *context); 21 void displayDevicesMore(libusb_context *context);
21 22
22 -void getFirstDeviceFromID(libusb_context *context, int vid, int pid, libusb_device **device); 23 +void getFirstDeviceFromID(libusb_context *context, int vid, int pid,
  24 + libusb_device **device);
23 25
24 void getOurInterfaces(libusb_device *device, 26 void getOurInterfaces(libusb_device *device,
25 struct libusb_interface **int_hidjoy, 27 struct libusb_interface **int_hidjoy,
26 struct libusb_interface **int_leds, 28 struct libusb_interface **int_leds,
27 - struct libusb_interface **int_vibrators  
28 -); 29 + struct libusb_interface **int_vibrators);
29 30
30 -void displayDeviceEndpoints (); 31 +void displayDeviceEndpoints();
31 32
32 -char getOnlyEndpoint(struct libusb_interface * interface); 33 +char getOnlyEndpoint(struct libusb_interface *interface);
33 34
34 #endif 35 #endif
35 -  
src/libusb_wrapper.c
1 #include "libusb_wrapper.h" 1 #include "libusb_wrapper.h"
2 2
3 -  
4 -  
5 void usbinit(libusb_context **context_ptr) { 3 void usbinit(libusb_context **context_ptr) {
6 - //libusb_context *context;  
7 - int statusInit = libusb_init(context_ptr);  
8 - if (statusInit != LIBUSB_SUCCESS) {  
9 - perror("libusb_init");  
10 - exit(-1);  
11 - } 4 + // libusb_context *context;
  5 + int statusInit = libusb_init(context_ptr);
  6 + if (statusInit != LIBUSB_SUCCESS) {
  7 + perror("libusb_init");
  8 + exit(-1);
  9 + }
12 } 10 }
13 11
14 -void usbclose(libusb_context *context) {  
15 - libusb_exit(context);  
16 -} 12 +void usbclose(libusb_context *context) { libusb_exit(context); }
17 13
18 -void interfaceclaim(libusb_device_handle *handle, struct libusb_interface *interface){  
19 - const struct libusb_interface_descriptor * interface_desc = &interface->altsetting[0]; //TODO enlever cette ligne, refactor 14 +void interfaceclaim(libusb_device_handle *handle,
  15 + struct libusb_interface *interface) {
  16 + const struct libusb_interface_descriptor *interface_desc =
  17 + &interface->altsetting[0]; // TODO enlever cette ligne, refactor
20 18
21 - if(interface_desc->bInterfaceClass == LIBUSB_CLASS_VENDOR_SPEC){//ON PEUT VERIFIER LA CLASS  
22 - printf("> vendor specific class\n"); 19 + if (interface_desc->bInterfaceClass ==
  20 + LIBUSB_CLASS_VENDOR_SPEC) { // ON PEUT VERIFIER LA CLASS
  21 + printf("> vendor specific class\n");
23 22
24 - int status=libusb_claim_interface(handle, interface_desc->bInterfaceNumber);  
25 - if(status!=0){ perror("libusb_claim_interface"); exit(-1); }  
26 - } 23 + int status =
  24 + libusb_claim_interface(handle, interface_desc->bInterfaceNumber);
  25 + if (status != 0) {
  26 + perror("libusb_claim_interface");
  27 + exit(-1);
  28 + }
  29 + }
27 } 30 }
28 31
29 -void interfaceclose(libusb_device_handle *handle, struct libusb_interface *interface){  
30 - const struct libusb_interface_descriptor * interface_desc = &interface->altsetting[0];  
31 - int status = libusb_release_interface(handle, interface_desc->bInterfaceNumber);  
32 - if(status!=0){ perror("libusb_release_interface"); exit(-1); } 32 +void interfaceclose(libusb_device_handle *handle,
  33 + struct libusb_interface *interface) {
  34 + const struct libusb_interface_descriptor *interface_desc =
  35 + &interface->altsetting[0];
  36 + int status =
  37 + libusb_release_interface(handle, interface_desc->bInterfaceNumber);
  38 + if (status != 0) {
  39 + perror("libusb_release_interface");
  40 + exit(-1);
  41 + }
33 } 42 }
34 43
35 // Si le méchant noyau est passé avant vous : 44 // Si le méchant noyau est passé avant vous :
36 -void getFromKernel(libusb_device_handle *handle, int interface) { //private method for now  
37 - if (libusb_kernel_driver_active(handle, interface)) {  
38 - int statusKDriver = libusb_detach_kernel_driver(handle, interface);  
39 - if (statusKDriver != LIBUSB_SUCCESS) {  
40 - perror("libusb_detach_kernel_driver");  
41 - exit(-1);  
42 - } 45 +void getFromKernel(libusb_device_handle *handle,
  46 + int interface) { // private method for now
  47 + if (libusb_kernel_driver_active(handle, interface)) {
  48 + int statusKDriver = libusb_detach_kernel_driver(handle, interface);
  49 + if (statusKDriver != LIBUSB_SUCCESS) {
  50 + perror("libusb_detach_kernel_driver");
  51 + exit(-1);
43 } 52 }
  53 + }
44 } 54 }
45 55
46 ssize_t getListDevices(libusb_context *context, libusb_device ***list_ptr) { 56 ssize_t getListDevices(libusb_context *context, libusb_device ***list_ptr) {
47 - ssize_t count = libusb_get_device_list(context, list_ptr);  
48 - if (count < 0) {  
49 - perror("libusb_get_device_list");  
50 - exit(-1);  
51 - }  
52 - return count; 57 + ssize_t count = libusb_get_device_list(context, list_ptr);
  58 + if (count < 0) {
  59 + perror("libusb_get_device_list");
  60 + exit(-1);
  61 + }
  62 + return count;
53 } 63 }
54 64
55 -void* _enumerateDevices(libusb_context *context, void (*func)(libusb_device *device)){  
56 - libusb_device **list;  
57 - ssize_t count = getListDevices(context, &list); 65 +void *_enumerateDevices(libusb_context *context,
  66 + void (*func)(libusb_device *device)) {
  67 + libusb_device **list;
  68 + ssize_t count = getListDevices(context, &list);
58 69
59 - ssize_t i = 0;  
60 - for (i = 0; i < count; i++) {  
61 - libusb_device *device = list[i]; 70 + ssize_t i = 0;
  71 + for (i = 0; i < count; i++) {
  72 + libusb_device *device = list[i];
62 73
63 - func(device);  
64 - } 74 + func(device);
  75 + }
65 76
66 - libusb_free_device_list(list, 1); 77 + libusb_free_device_list(list, 1);
67 78
68 - return NULL; //DBGONLY TEMP 79 + return NULL; // DBGONLY TEMP
69 } 80 }
70 81
71 -//print readable string, not asked for tutoring  
72 -void _printConfig(libusb_device *device) {//private method  
73 - // Ouverture du périphérique  
74 - libusb_device_handle *handle;  
75 - int statusDevice = libusb_open(device, &handle);  
76 - if (statusDevice != LIBUSB_SUCCESS) {  
77 - perror("libusb_open");  
78 - //return; //exit(-1);  
79 - }  
80 -  
81 - int MAXLEN_DESCRIPTOR_STRING = 200;  
82 - uint8_t desc_idx = 2;  
83 - //uint16_t langid = 16;  
84 - unsigned char data[200];  
85 -  
86 - // TEST 16  
87 - for (desc_idx = 0; desc_idx < 16; desc_idx++) {  
88 - int statusAscii = libusb_get_string_descriptor_ascii(  
89 - handle, desc_idx, data, MAXLEN_DESCRIPTOR_STRING);  
90 - if (statusAscii == -9) // TEST seems to be LIBUSB_ERROR  
91 - break;  
92 - printf(" - Descriptor string : %s ; %i\n", data, statusAscii);  
93 - }  
94 -  
95 - libusb_close(handle); 82 +// print readable string, not asked for tutoring
  83 +void _printConfig(libusb_device *device) { // private method
  84 + // Ouverture du périphérique
  85 + libusb_device_handle *handle;
  86 + int statusDevice = libusb_open(device, &handle);
  87 + if (statusDevice != LIBUSB_SUCCESS) {
  88 + perror("libusb_open");
  89 + // return; //exit(-1);
  90 + }
  91 +
  92 + int MAXLEN_DESCRIPTOR_STRING = 200;
  93 + uint8_t desc_idx = 2;
  94 + // uint16_t langid = 16;
  95 + unsigned char data[200];
  96 +
  97 + // TEST 16
  98 + for (desc_idx = 0; desc_idx < 16; desc_idx++) {
  99 + int statusAscii = libusb_get_string_descriptor_ascii(
  100 + handle, desc_idx, data, MAXLEN_DESCRIPTOR_STRING);
  101 + if (statusAscii == -9) // TEST seems to be LIBUSB_ERROR
  102 + break;
  103 + printf(" - Descriptor string : %s ; %i\n", data, statusAscii);
  104 + }
  105 +
  106 + libusb_close(handle);
96 } 107 }
97 108
  109 +void _displayOneDevice(libusb_device *device) {
  110 + struct libusb_device_descriptor desc;
98 111
99 -void _displayOneDevice(libusb_device *device){  
100 - struct libusb_device_descriptor desc; 112 + int status = libusb_get_device_descriptor(device, &desc);
  113 + if (status != LIBUSB_SUCCESS) {
  114 + printf("Cannot get device desc : %s\n",
  115 + libusb_error_name(status)); // DBGONLY
  116 + perror("libusb_open");
  117 + return;
  118 + }
101 119
102 - int status = libusb_get_device_descriptor(device, &desc);  
103 - if (status != LIBUSB_SUCCESS){  
104 - printf("Cannot get device desc : %s\n", libusb_error_name(status)); //DBGONLY  
105 - perror("libusb_open");  
106 - return;  
107 - }  
108 -  
109 - uint8_t bus = libusb_get_bus_number(device);  
110 - uint8_t address = libusb_get_device_address(device); 120 + uint8_t bus = libusb_get_bus_number(device);
  121 + uint8_t address = libusb_get_device_address(device);
111 122
112 - printf("Device Found @ (Bus:Address) %d:%d\n", bus, address);  
113 - printf("Vendor ID 0x0%x\n", desc.idVendor);  
114 - printf("Product ID 0x0%x\n", desc.idProduct); 123 + printf("Device Found @ (Bus:Address) %d:%d\n", bus, address);
  124 + printf("Vendor ID 0x0%x\n", desc.idVendor);
  125 + printf("Product ID 0x0%x\n", desc.idProduct);
115 126
116 - //displayDeviceEndpoints(device); //Not really work on Axel@Alptop 127 + // displayDeviceEndpoints(device); //Not really work on Axel@Alptop
117 } 128 }
118 129
119 void displayDevices(libusb_context *context) { 130 void displayDevices(libusb_context *context) {
120 - _enumerateDevices(context, _displayOneDevice); 131 + _enumerateDevices(context, _displayOneDevice);
121 } 132 }
122 133
123 -void _displayOneDeviceMore(libusb_device *device){  
124 - _displayOneDevice(device);  
125 - _printConfig(device); 134 +void _displayOneDeviceMore(libusb_device *device) {
  135 + _displayOneDevice(device);
  136 + _printConfig(device);
126 } 137 }
127 138
128 void displayDevicesMore(libusb_context *context) { 139 void displayDevicesMore(libusb_context *context) {
129 - _enumerateDevices(context, _displayOneDeviceMore); 140 + _enumerateDevices(context, _displayOneDeviceMore);
130 } 141 }
131 142
132 -//get device from iteration (and not from not recommanded function :  
133 -//ugly using global vars ?? Cannot communicate between getFirstDeviceFromID and _getFirstDeviceFromID 143 +// get device from iteration (and not from not recommanded function :
  144 +// ugly using global vars ?? Cannot communicate between getFirstDeviceFromID and
  145 +// _getFirstDeviceFromID
134 int g_vid; 146 int g_vid;
135 int g_pid; 147 int g_pid;
136 libusb_device *g_device = NULL; 148 libusb_device *g_device = NULL;
137 void _getFirstDeviceFromID(libusb_device *device) { 149 void _getFirstDeviceFromID(libusb_device *device) {
138 - struct libusb_device_descriptor desc;  
139 -  
140 - int status = libusb_get_device_descriptor(device, &desc);  
141 - if (status != LIBUSB_SUCCESS){  
142 - printf("Cannot get device desc : %s\n", libusb_error_name(status)); //DBGONLY  
143 - perror("libusb_open");  
144 - return;  
145 - }  
146 -  
147 -  
148 - if(desc.idVendor == g_vid && desc.idProduct == g_pid){  
149 - g_device = device;  
150 - } 150 + struct libusb_device_descriptor desc;
  151 +
  152 + int status = libusb_get_device_descriptor(device, &desc);
  153 + if (status != LIBUSB_SUCCESS) {
  154 + printf("Cannot get device desc : %s\n",
  155 + libusb_error_name(status)); // DBGONLY
  156 + perror("libusb_open");
  157 + return;
  158 + }
  159 +
  160 + if (desc.idVendor == g_vid && desc.idProduct == g_pid) {
  161 + g_device = device;
  162 + }
151 } 163 }
152 164
153 -void getFirstDeviceFromID(libusb_context *context, int vid, int pid, libusb_device **device) {  
154 - g_vid = vid; //pass parameters for enumeration  
155 - g_pid = pid; //idem  
156 - _enumerateDevices(context, _getFirstDeviceFromID);  
157 - *device = g_device; //get return from enumeration 165 +void getFirstDeviceFromID(libusb_context *context, int vid, int pid,
  166 + libusb_device **device) {
  167 + g_vid = vid; // pass parameters for enumeration
  168 + g_pid = pid; // idem
  169 + _enumerateDevices(context, _getFirstDeviceFromID);
  170 + *device = g_device; // get return from enumeration
158 } 171 }
159 172
160 -//void getDevicesFromID(vid, pid) //return array  
161 -  
162 -  
163 -void displayDeviceEndpoints(libusb_device_handle* handle) {  
164 - //recover pointer from handle  
165 - libusb_device *device;  
166 - device = libusb_get_device(handle);  
167 -  
168 -  
169 - // lectures des configs => on prend la première configuration pour l'instant  
170 - /*int configuration = 0; // valueof("bConfigurationValue");  
171 - int statusConfig = libusb_set_configuration(handle, configuration);  
172 - if (statusConfig != LIBUSB_SUCCESS) {  
173 - perror("libusb_set_configuration");  
174 - return;  
175 - }*/  
176 -  
177 - // 4.2 configuration du périph usb  
178 - struct libusb_config_descriptor *config;  
179 - int statusFetchConfig = libusb_get_active_config_descriptor(device, &config);  
180 - if (statusFetchConfig != LIBUSB_SUCCESS) {  
181 - perror("libusb_get_active_config_descriptor");  
182 - return; 173 +// void getDevicesFromID(vid, pid) //return array
  174 +
  175 +void displayDeviceEndpoints(libusb_device_handle *handle) {
  176 + // recover pointer from handle
  177 + libusb_device *device;
  178 + device = libusb_get_device(handle);
  179 +
  180 + // lectures des configs => on prend la première configuration pour l'instant
  181 + /*int configuration = 0; // valueof("bConfigurationValue");
  182 + int statusConfig = libusb_set_configuration(handle, configuration);
  183 + if (statusConfig != LIBUSB_SUCCESS) {
  184 + perror("libusb_set_configuration");
  185 + return;
  186 + }*/
  187 +
  188 + // 4.2 configuration du périph usb
  189 + struct libusb_config_descriptor *config;
  190 + int statusFetchConfig = libusb_get_active_config_descriptor(device, &config);
  191 + if (statusFetchConfig != LIBUSB_SUCCESS) {
  192 + perror("libusb_get_active_config_descriptor");
  193 + return;
  194 + }
  195 +
  196 + // caractéristiques globales
  197 + printf("Config.bConfigurationValue : %d\n", config->bConfigurationValue);
  198 + printf("Config/ bLength:%d\nbDescriptorType:%d\nbNumInterfaces:%d\n",
  199 + config->bLength, config->bDescriptorType, config->bNumInterfaces);
  200 +
  201 + // itération des interfaces
  202 + printf("Itération de l'interface\n");
  203 + for (int indexInterface = 0; indexInterface < config->bNumInterfaces;
  204 + indexInterface++) {
  205 + printf("-indexInterface=%d\n", indexInterface);
  206 + printf("-Altsetting=%d\n",
  207 + config->interface[indexInterface].num_altsetting);
  208 +
  209 + const struct libusb_interface *interface =
  210 + &config->interface[indexInterface];
  211 +
  212 + // if 1 setting (or more)
  213 + if (interface->num_altsetting != 0) {
  214 + const struct libusb_interface_descriptor *interface_desc =
  215 + &interface->altsetting[0];
  216 +
  217 + printf("--bNumEndpoints=%d\n", interface_desc->bNumEndpoints);
  218 + printf("--bDescriptorType=%d\n", interface_desc->bDescriptorType);
  219 +
  220 + for (int indexEndpoints = 0;
  221 + indexEndpoints < interface_desc->bNumEndpoints; indexEndpoints++) {
  222 + const struct libusb_endpoint_descriptor *endpoint_desc =
  223 + &interface_desc->endpoint[indexEndpoints];
  224 + printf("---bDescriptorType=%d\n", endpoint_desc->bDescriptorType);
  225 + printf("---bEndpointAddress=%d\n", endpoint_desc->bEndpointAddress);
  226 +
  227 + if (endpoint_desc->extra != NULL)
  228 + printf("---extra: %s\n", endpoint_desc->extra);
  229 + if (endpoint_desc->bmAttributes ==
  230 + LIBUSB_TRANSFER_TYPE_INTERRUPT) // TODO AJOUT MASQUE ? (voir doc)
  231 + printf("---is of type INTERRUPT\n");
  232 + }
183 } 233 }
184 234
185 - //caractéristiques globales  
186 - printf("Config.bConfigurationValue : %d\n", config->bConfigurationValue);  
187 - printf("Config/ bLength:%d\nbDescriptorType:%d\nbNumInterfaces:%d\n",  
188 - config->bLength, config->bDescriptorType, config->bNumInterfaces);  
189 -  
190 -  
191 - //itération des interfaces  
192 - printf("Itération de l'interface\n");  
193 - for (int indexInterface = 0; indexInterface < config->bNumInterfaces; indexInterface++) {  
194 - printf("-indexInterface=%d\n", indexInterface);  
195 - printf("-Altsetting=%d\n", config->interface[indexInterface].num_altsetting);  
196 -  
197 - const struct libusb_interface * interface = &config->interface[indexInterface];  
198 -  
199 -  
200 - //if 1 setting (or more)  
201 - if(interface->num_altsetting != 0) {  
202 - const struct libusb_interface_descriptor * interface_desc = &interface->altsetting[0];  
203 -  
204 - printf("--bNumEndpoints=%d\n", interface_desc->bNumEndpoints);  
205 - printf("--bDescriptorType=%d\n", interface_desc->bDescriptorType);  
206 -  
207 - for(int indexEndpoints = 0; indexEndpoints < interface_desc->bNumEndpoints ; indexEndpoints++){  
208 - const struct libusb_endpoint_descriptor * endpoint_desc = &interface_desc->endpoint[indexEndpoints];  
209 - printf("---bDescriptorType=%d\n", endpoint_desc->bDescriptorType);  
210 - printf("---bEndpointAddress=%d\n", endpoint_desc->bEndpointAddress);  
211 -  
212 - if(endpoint_desc->extra != NULL)  
213 - printf("---extra: %s\n", endpoint_desc->extra);  
214 - if(endpoint_desc->bmAttributes == LIBUSB_TRANSFER_TYPE_INTERRUPT)//TODO AJOUT MASQUE ? (voir doc)  
215 - printf("---is of type INTERRUPT\n");  
216 - }  
217 - }  
218 -  
219 - 235 + // Affichez l’indice et le numéro de chaque interface détectée et réclamée.
  236 + // Affichez aussi les points d’accès trouvés en précisant ceux sauvés.
220 237
221 - //Affichez l’indice et le numéro de chaque interface détectée et réclamée. Affichez aussi les points d’accès trouvés en précisant ceux sauvés. 238 + /*
  239 + //int interface=valueof("bInterfaceNumber");
  240 + int
  241 + status=libusb_claim_interface(handle,interface->bInterfaceNumber);
  242 + if(status!=0){ perror("libusb_claim_interface"); exit(-1); }
222 243
223 -/*  
224 - //int interface=valueof("bInterfaceNumber");  
225 - int status=libusb_claim_interface(handle,interface->bInterfaceNumber);  
226 - if(status!=0){ perror("libusb_claim_interface"); exit(-1); } 244 + status=libusb_release_interface(handle,interface->bInterfaceNumber);
  245 + if(status!=0){ perror("libusb_release_interface"); exit(-1); }
227 246
228 - status=libusb_release_interface(handle,interface->bInterfaceNumber);  
229 - if(status!=0){ perror("libusb_release_interface"); exit(-1); } 247 + //claim_interface(.., config interface altsetting bInterfaceNumber
  248 + //config interface altsetting endpoint bmAttributes ==
  249 + LIBUSB_TRANSFER_TYPE_INTERRUPT
  250 + //add des enpoint unint8_t*/
230 251
231 - //claim_interface(.., config interface altsetting bInterfaceNumber  
232 - //config interface altsetting endpoint bmAttributes == LIBUSB_TRANSFER_TYPE_INTERRUPT  
233 - //add des enpoint unint8_t*/ 252 + // DESCRIPTOR.C /.H :décommenter
234 253
  254 + // IL FAUT RELEASE UNIQUEMENT LES INTERFACES QUI NE SONT PAS DES HID
  255 + }
235 256
236 - //DESCRIPTOR.C /.H :décommenter  
237 -  
238 -  
239 - //IL FAUT RELEASE UNIQUEMENT LES INTERFACES QUI NE SONT PAS DES HID  
240 - }  
241 -  
242 - printf("\n");  
243 -  
244 - libusb_close(handle); 257 + printf("\n");
245 258
  259 + libusb_close(handle);
246 } 260 }
247 261
248 void getOurInterfaces(libusb_device *device, 262 void getOurInterfaces(libusb_device *device,
249 struct libusb_interface **int_hidjoy, 263 struct libusb_interface **int_hidjoy,
250 struct libusb_interface **int_leds, 264 struct libusb_interface **int_leds,
251 - struct libusb_interface **int_vibrators  
252 -) {  
253 - struct libusb_config_descriptor *config;  
254 - int statusFetchConfig = libusb_get_active_config_descriptor(device, &config);  
255 - if (statusFetchConfig != LIBUSB_SUCCESS) {  
256 - perror("libusb_get_active_config_descriptor");  
257 - return;  
258 - }  
259 -  
260 - //itération des interfaces  
261 - for (int indexInterface = 0; indexInterface < config->bNumInterfaces; indexInterface++) {  
262 -  
263 - const struct libusb_interface * interface = &config->interface[indexInterface];  
264 -  
265 - //todo use le descriptor pour avoir bInterfaceNumber?  
266 - if(indexInterface==0){  
267 - *int_hidjoy = (struct libusb_interface *)interface;  
268 - }else if(indexInterface==1){  
269 - *int_leds = (struct libusb_interface *)interface;  
270 - }else if(indexInterface==2){  
271 - *int_vibrators = (struct libusb_interface *)interface;  
272 - }  
273 -  
274 - //#define pour les indices correspondants aux interfaces  
275 - //variables globales 265 + struct libusb_interface **int_vibrators) {
  266 + struct libusb_config_descriptor *config;
  267 + int statusFetchConfig = libusb_get_active_config_descriptor(device, &config);
  268 + if (statusFetchConfig != LIBUSB_SUCCESS) {
  269 + perror("libusb_get_active_config_descriptor");
  270 + return;
  271 + }
  272 +
  273 + // itération des interfaces
  274 + for (int indexInterface = 0; indexInterface < config->bNumInterfaces;
  275 + indexInterface++) {
  276 +
  277 + const struct libusb_interface *interface =
  278 + &config->interface[indexInterface];
  279 +
  280 + // todo use le descriptor pour avoir bInterfaceNumber?
  281 + if (indexInterface == 0) {
  282 + *int_hidjoy = (struct libusb_interface *)interface;
  283 + } else if (indexInterface == 1) {
  284 + *int_leds = (struct libusb_interface *)interface;
  285 + } else if (indexInterface == 2) {
  286 + *int_vibrators = (struct libusb_interface *)interface;
276 } 287 }
277 288
  289 + //#define pour les indices correspondants aux interfaces
  290 + // variables globales
  291 + }
278 } 292 }
279 293
280 -char getOnlyEndpoint(struct libusb_interface * interface){ 294 +char getOnlyEndpoint(struct libusb_interface *interface) {
281 295
282 - const struct libusb_interface_descriptor * interface_desc = &interface->altsetting[0]; 296 + const struct libusb_interface_descriptor *interface_desc =
  297 + &interface->altsetting[0];
283 298
284 - const struct libusb_endpoint_descriptor * endpoint_desc = &interface_desc->endpoint[0]; 299 + const struct libusb_endpoint_descriptor *endpoint_desc =
  300 + &interface_desc->endpoint[0];
285 301
286 - if(endpoint_desc->bmAttributes != LIBUSB_TRANSFER_TYPE_INTERRUPT)//ON PEUT VERIFIER LE TRANSFER TYPE  
287 - printf("> is NOT of type INTERRUPT\n"); 302 + if (endpoint_desc->bmAttributes !=
  303 + LIBUSB_TRANSFER_TYPE_INTERRUPT) // ON PEUT VERIFIER LE TRANSFER TYPE
  304 + printf("> is NOT of type INTERRUPT\n");
288 305
289 - return (char)endpoint_desc->bEndpointAddress; 306 + return (char)endpoint_desc->bEndpointAddress;
290 } 307 }
@@ -5,123 +5,128 @@ @@ -5,123 +5,128 @@
5 #define VID 1003 5 #define VID 1003
6 #define PID 8259 6 #define PID 8259
7 7
8 -int main(int argc, char *argv[]){  
9 - printf("Hello World :-|\n");  
10 -  
11 - libusb_context *context;  
12 - usbinit(&context);  
13 - #ifdef DEBUG  
14 - libusb_set_option(context, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_DEBUG);  
15 - #endif  
16 -  
17 - if(argc>1){  
18 - //command mode  
19 - if(strcmp(argv[1], "demo4.1") == 0) { //if 4.1  
20 - printf("\e[91mDemo 4.1\e[39m\n");  
21 - displayDevices(context); 8 +int main(int argc, char *argv[]) {
  9 + printf("Hello World :-|\n");
  10 +
  11 + libusb_context *context;
  12 + usbinit(&context);
  13 +#ifdef DEBUG
  14 + libusb_set_option(context, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_DEBUG);
  15 +#endif
  16 +
  17 + if (argc > 1) {
  18 + // command mode
  19 + if (strcmp(argv[1], "demo4.1") == 0) { // if 4.1
  20 + printf("\e[91mDemo 4.1\e[39m\n");
  21 + displayDevices(context);
  22 + } else if (strcmp(argv[1], "demo4.1plus") == 0) { // if 4.1 bonus
  23 + printf("\e[91mDemo 4.1\e[39m\n");
  24 + displayDevicesMore(context);
  25 + } else if (strcmp(argv[1], "demo4.2") == 0) { // if 4.2
  26 + printf("\e[91mDemo 4.2\e[39m\n");
  27 +
  28 + if (argc > 3) {
  29 + uint16_t vendor_id;
  30 + uint16_t product_id;
  31 + sscanf(argv[2], "%hu", &vendor_id);
  32 + sscanf(argv[3], "%hu", &product_id);
  33 +
  34 + libusb_device_handle *handle;
  35 + handle =
  36 + libusb_open_device_with_vid_pid(context, vendor_id, product_id);
  37 + if (handle != NULL) {
  38 + printf("Show endpoints of VID:%hu;PID:%hu\n", vendor_id, product_id);
  39 + displayDeviceEndpoints(handle);
  40 + } else {
  41 + printf("Error while getting handle of VID:%hu;PID:%hu\n", vendor_id,
  42 + product_id);
22 } 43 }
23 - else if(strcmp(argv[1], "demo4.1plus") == 0) { //if 4.1 bonus  
24 - printf("\e[91mDemo 4.1\e[39m\n");  
25 - displayDevicesMore(context);  
26 - }  
27 - else if(strcmp(argv[1], "demo4.2") == 0){//if 4.2  
28 - printf("\e[91mDemo 4.2\e[39m\n");  
29 -  
30 - if(argc > 3){  
31 - uint16_t vendor_id;  
32 - uint16_t product_id;  
33 - sscanf(argv[2], "%hu", &vendor_id);  
34 - sscanf(argv[3], "%hu", &product_id);  
35 -  
36 - libusb_device_handle* handle;  
37 - handle = libusb_open_device_with_vid_pid(context, vendor_id, product_id);  
38 - if(handle != NULL){  
39 - printf("Show endpoints of VID:%hu;PID:%hu\n", vendor_id, product_id);  
40 - displayDeviceEndpoints(handle);  
41 - }  
42 - else{  
43 - printf("Error while getting handle of VID:%hu;PID:%hu\n", vendor_id, product_id);  
44 - }  
45 - }else{  
46 - printf("Error, need VID and PID\n");  
47 - }  
48 - }else{  
49 - printf("Wrong command\n");  
50 - }  
51 -  
52 - }else{  
53 - //interactive mode  
54 - printf("Welcome to interactive mode\n");  
55 - printf("Compiled for VID:PID => %d:%d\n", VID, PID);  
56 -  
57 - //Find the device  
58 - printf("Iterate list of devices, and getting the right one\n");  
59 - libusb_device *device = NULL;  
60 - getFirstDeviceFromID(context, VID, PID, &device);  
61 - //code  
62 - if(device==NULL){  
63 - printf("Error : cannot find the device !\n");  
64 - return 1;  
65 - }  
66 -  
67 - //take the handle  
68 - libusb_device_handle *device_handle = NULL;  
69 - if(libusb_open(device, &device_handle) != 0){  
70 - printf("Error : cannot handle the device\n");  
71 - return 1;  
72 - }  
73 -  
74 - //Take the interfaces  
75 - printf("\nSearch for interfaces\n");  
76 - struct libusb_interface * int_hidjoy = NULL;  
77 - struct libusb_interface * int_leds = NULL;  
78 - struct libusb_interface * int_vibrators = NULL;  
79 - getOurInterfaces(device, &int_hidjoy, &int_leds, &int_vibrators);  
80 -  
81 - //Claim interface  
82 - printf("\nClaim interfaces ...\n");  
83 - printf("-Leds\n");  
84 - interfaceclaim(device_handle, int_leds);  
85 - printf("-Vibrators\n");  
86 - interfaceclaim(device_handle, int_vibrators);  
87 -  
88 - //récupération des endpoints  
89 - printf("\nRécupération endpoints\n");  
90 - printf("-Leds");  
91 - int endpoint_leds=getOnlyEndpoint(int_leds); /* ID of endpoint (bit 8 is 0) */  
92 - printf(" is %d\n", endpoint_leds);  
93 - printf("-Vibrators");  
94 - int endpoint_vibrators=getOnlyEndpoint(int_vibrators); /* ID of endpoint (bit 8 is 0) */  
95 - printf(" is %d\n", endpoint_vibrators);  
96 -  
97 -  
98 - /*LE CODE UTILE*/  
99 -while(1){  
100 - unsigned char data[2] = {0xff,0xff}; // data to send or to receive  
101 - int size=2; // size to send or maximum size to receive  
102 - int timeout=1000; // timeout in ms  
103 -  
104 - //OUT interrupt, from host to device  
105 - int bytes_out;  
106 - printf("%p %02X\n",device_handle,endpoint_leds);  
107 - int status=libusb_interrupt_transfer(device_handle,endpoint_leds,data,size,&bytes_out,timeout);  
108 - if(status!=0){ perror("libusb_interrupt_transfer"); exit(-1); }  
109 -}  
110 - /*FIN DU CODE UTILE*/  
111 -  
112 - //Close  
113 - printf("\nUnclaim interfaces ...\n");  
114 - printf("-Leds\n");  
115 - interfaceclose(device_handle, int_leds);  
116 - printf("-Vibrators\n");  
117 - interfaceclose(device_handle, int_vibrators);  
118 -  
119 -  
120 - printf("Finished\n");  
121 - }  
122 -  
123 -  
124 - usbclose(context);  
125 -  
126 - return 0; 44 + } else {
  45 + printf("Error, need VID and PID\n");
  46 + }
  47 + } else {
  48 + printf("Wrong command\n");
  49 + }
  50 +
  51 + } else {
  52 + // interactive mode
  53 + printf("Welcome to interactive mode\n");
  54 + printf("Compiled for VID:PID => %d:%d\n", VID, PID);
  55 +
  56 + // Find the device
  57 + printf("Iterate list of devices, and getting the right one\n");
  58 + libusb_device *device = NULL;
  59 + getFirstDeviceFromID(context, VID, PID, &device);
  60 + // code
  61 + if (device == NULL) {
  62 + printf("Error : cannot find the device !\n");
  63 + return 1;
  64 + }
  65 +
  66 + // take the handle
  67 + libusb_device_handle *device_handle = NULL;
  68 + if (libusb_open(device, &device_handle) != 0) {
  69 + printf("Error : cannot handle the device\n");
  70 + return 1;
  71 + }
  72 +
  73 + // Take the interfaces
  74 + printf("\nSearch for interfaces\n");
  75 + struct libusb_interface *int_hidjoy = NULL;
  76 + struct libusb_interface *int_leds = NULL;
  77 + struct libusb_interface *int_vibrators = NULL;
  78 + getOurInterfaces(device, &int_hidjoy, &int_leds, &int_vibrators);
  79 +
  80 + // Claim interface
  81 + printf("\nClaim interfaces ...\n");
  82 + printf("-Leds\n");
  83 + interfaceclaim(device_handle, int_leds);
  84 + printf("-Vibrators\n");
  85 + interfaceclaim(device_handle, int_vibrators);
  86 +
  87 + // récupération des endpoints
  88 + printf("\nRécupération endpoints\n");
  89 + printf("-Leds");
  90 + int endpoint_leds =
  91 + getOnlyEndpoint(int_leds); /* ID of endpoint (bit 8 is 0) */
  92 + printf(" is %d\n", endpoint_leds);
  93 + printf("-Vibrators");
  94 + int endpoint_vibrators =
  95 + getOnlyEndpoint(int_vibrators); /* ID of endpoint (bit 8 is 0) */
  96 + printf(" is %d\n", endpoint_vibrators);
  97 +
  98 + /*LE CODE UTILE*/
  99 + int i;
  100 + for(i = 0 ; i<5 ; i++) {
  101 + unsigned char data[2] = {0xff, 0xff}; // data to send or to receive
  102 + int size = 2; // size to send or maximum size to receive
  103 + int timeout = 1000; // timeout in ms
  104 +
  105 + // OUT interrupt, from host to device
  106 + int bytes_out;
  107 + printf("%p %02X\n", device_handle, endpoint_leds);
  108 + int status = libusb_interrupt_transfer(device_handle, endpoint_leds, data,
  109 + size, &bytes_out, timeout);
  110 + if (status != 0) {
  111 + perror("libusb_interrupt_transfer");
  112 + exit(-1);
  113 + }
  114 +
  115 + //sleep(1); demande l'ajout de macro pour la portabilité
  116 + }
  117 + /*FIN DU CODE UTILE*/
  118 +
  119 + // Close
  120 + printf("\nUnclaim interfaces ...\n");
  121 + printf("-Leds\n");
  122 + interfaceclose(device_handle, int_leds);
  123 + printf("-Vibrators\n");
  124 + interfaceclose(device_handle, int_vibrators);
  125 +
  126 + printf("Finished\n");
  127 + }
  128 +
  129 + usbclose(context);
  130 +
  131 + return 0;
127 } 132 }