Commit 9424ae9ab8d1d9a9162c02c795010713f75a44fa

Authored by achemin1
1 parent 6fbb050a

ajouts claim interface

include/libusb_wrapper.h
@@ -7,9 +7,11 @@ @@ -7,9 +7,11 @@
7 #include <stdbool.h> 7 #include <stdbool.h>
8 8
9 void usbinit(libusb_context** context_ptr); 9 void usbinit(libusb_context** context_ptr);
10 -  
11 void usbclose(libusb_context* context); 10 void usbclose(libusb_context* context);
12 11
  12 +void interfaceclaim(libusb_device_handle *handle, struct libusb_interface *interface);
  13 +void interfaceclose(libusb_device_handle *handle, struct libusb_interface *interface);
  14 +
13 ssize_t getListDevices(libusb_context* context, libusb_device***list_ptr); 15 ssize_t getListDevices(libusb_context* context, libusb_device***list_ptr);
14 void getFromKernel(libusb_device_handle *handle, int interface); 16 void getFromKernel(libusb_device_handle *handle, int interface);
15 17
@@ -19,6 +21,11 @@ void displayDevicesMore(libusb_context *context); @@ -19,6 +21,11 @@ void displayDevicesMore(libusb_context *context);
19 21
20 void getFirstDeviceFromID(libusb_context *context, int vid, int pid, libusb_device **device); 22 void getFirstDeviceFromID(libusb_context *context, int vid, int pid, libusb_device **device);
21 23
  24 +void getOurInterfaces(libusb_device *device,
  25 + struct libusb_interface **int_hidjoy,
  26 + struct libusb_interface **int_leds,
  27 + struct libusb_interface **int_vibrators
  28 +);
22 29
23 void displayDeviceEndpoints (); 30 void displayDeviceEndpoints ();
24 31
src/libusb_wrapper.c
@@ -15,6 +15,18 @@ void usbclose(libusb_context *context) { @@ -15,6 +15,18 @@ void usbclose(libusb_context *context) {
15 libusb_exit(context); 15 libusb_exit(context);
16 } 16 }
17 17
  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
  20 + int status=libusb_claim_interface(handle, interface_desc->bInterfaceNumber);
  21 + if(status!=0){ perror("libusb_claim_interface"); exit(-1); }
  22 +}
  23 +
  24 +void interfaceclose(libusb_device_handle *handle, struct libusb_interface *interface){
  25 + const struct libusb_interface_descriptor * interface_desc = &interface->altsetting[0];
  26 + int status = libusb_release_interface(handle, interface_desc->bInterfaceNumber);
  27 + if(status!=0){ perror("libusb_release_interface"); exit(-1); }
  28 +}
  29 +
18 // Si le méchant noyau est passé avant vous : 30 // Si le méchant noyau est passé avant vous :
19 void getFromKernel(libusb_device_handle *handle, int interface) { //private method for now 31 void getFromKernel(libusb_device_handle *handle, int interface) { //private method for now
20 if (libusb_kernel_driver_active(handle, interface)) { 32 if (libusb_kernel_driver_active(handle, interface)) {
@@ -196,7 +208,6 @@ void displayDeviceEndpoints(libusb_device_handle* handle) { @@ -196,7 +208,6 @@ void displayDeviceEndpoints(libusb_device_handle* handle) {
196 printf("---extra: %s\n", endpoint_desc->extra); 208 printf("---extra: %s\n", endpoint_desc->extra);
197 if(endpoint_desc->bmAttributes == LIBUSB_TRANSFER_TYPE_INTERRUPT)//TODO AJOUT MASQUE ? (voir doc) 209 if(endpoint_desc->bmAttributes == LIBUSB_TRANSFER_TYPE_INTERRUPT)//TODO AJOUT MASQUE ? (voir doc)
198 printf("---is of type INTERRUPT\n"); 210 printf("---is of type INTERRUPT\n");
199 -  
200 } 211 }
201 } 212 }
202 213
@@ -228,3 +239,47 @@ void displayDeviceEndpoints(libusb_device_handle* handle) { @@ -228,3 +239,47 @@ void displayDeviceEndpoints(libusb_device_handle* handle) {
228 libusb_close(handle); 239 libusb_close(handle);
229 240
230 } 241 }
  242 +
  243 +void getOurInterfaces(libusb_device *device,
  244 + struct libusb_interface **int_hidjoy,
  245 + struct libusb_interface **int_leds,
  246 + struct libusb_interface **int_vibrators
  247 +) {
  248 + //REDONDANCE DE CODE AVEC displayDeviceEndpoints
  249 +
  250 + // 4.2 configuration du périph usb
  251 + struct libusb_config_descriptor *config;
  252 + int statusFetchConfig = libusb_get_active_config_descriptor(device, &config);
  253 + if (statusFetchConfig != LIBUSB_SUCCESS) {
  254 + perror("libusb_get_active_config_descriptor");
  255 + return;
  256 + }
  257 +
  258 + //caractéristiques globales
  259 + printf("Config.bConfigurationValue : %d\n", config->bConfigurationValue);
  260 + printf("Config/ bLength:%d\nbDescriptorType:%d\nbNumInterfaces:%d\n",
  261 + config->bLength, config->bDescriptorType, config->bNumInterfaces);
  262 +
  263 +
  264 + //itération des interfaces
  265 + printf("Itération de l'interface\n");
  266 + for (int indexInterface = 0; indexInterface < config->bNumInterfaces; indexInterface++) {
  267 + printf("-indexInterface=%d\n", indexInterface);
  268 + printf("-Altsetting=%d\n", config->interface[indexInterface].num_altsetting);
  269 +
  270 + struct libusb_interface * interface = &config->interface[indexInterface];
  271 +
  272 + //todo use le descriptor pour avoir bInterfaceNumber?
  273 + if(indexInterface==0){
  274 + *int_hidjoy = interface;
  275 + }else if(indexInterface==1){
  276 + *int_leds = interface;
  277 + }else if(indexInterface==2){
  278 + *int_vibrators = interface;
  279 + }
  280 +
  281 + //#define pour les indices correspondants aux interfaces
  282 + //variables globales
  283 + }
  284 +
  285 +}
231 \ No newline at end of file 286 \ No newline at end of file
@@ -64,7 +64,34 @@ int main(int argc, char *argv[]){ @@ -64,7 +64,34 @@ int main(int argc, char *argv[]){
64 return 1; 64 return 1;
65 } 65 }
66 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 +
67 //Take the interfaces 74 //Take the interfaces
  75 + printf("Search 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("Claim interfaces ...\n");
  83 + printf("-Leds\n");
  84 + interfaceclaim(device_handle, int_leds);
  85 + printf("-Vibrators\n");
  86 + interfaceclaim(device_handle, int_vibrators);
  87 +
  88 + //Close
  89 + printf("Unclaim interfaces ...\n");
  90 + //printf("-HID Joy\n"); //would not work
  91 + //interfaceclose(device, ...)
  92 + printf("-Leds\n");
  93 + interfaceclose(device_handle, int_leds);
  94 + interfaceclose(device_handle, int_vibrators);
68 95
69 96
70 printf("Finished\n"); 97 printf("Finished\n");