Commit 08e49fd919cd64a4b3b948232aeb5f17bdbe02de

Authored by pifou
1 parent 34d0d1db

Seance 2

Showing 1 changed file with 51 additions and 16 deletions   Show diff stats
test_com.c
... ... @@ -2,6 +2,8 @@
2 2 #include <stdio.h>
3 3 #include <stdlib.h>
4 4  
  5 +// gcc file.c -l usb-1.0
  6 +
5 7 int main(){
6 8 // init
7 9 libusb_context *context;
... ... @@ -15,32 +17,65 @@ int main(){
15 17 for(int i=0; i<devices_count; i++){
16 18 libusb_device *device = devices[i];
17 19  
  20 + // device description
  21 + struct libusb_device_descriptor desc;
  22 + int status = libusb_get_device_descriptor(device, &desc);
  23 + if(status != 0) continue;
  24 +
  25 + // search for device
  26 + if(desc.idVendor != 0x0951
  27 + || desc.idProduct != 0x01666)
  28 + continue;
  29 +
18 30 // open connection
19 31 libusb_device_handle *handle;
20   - status=libusb_open(device,&handle);
21   - if(status!=0){ perror("libusb_open"); exit(-1); }
22   -
23   - // init interface
24   - // TODO int interface = valueof("bInterfaceNumber");
25   - status = libusb_claim_interface(handle, interface);
26   - if(status != 0){perror("libusb_claim_interface"); exit(-1);}
  32 + status = libusb_open(device, &handle);
  33 + if(status != 0){ perror("libusb_open"); exit(-1); }
  34 +
  35 + // prepare config
  36 + struct libusb_config_descriptor *config_desc;
  37 + status = libusb_get_config_descriptor(device, 0, &config_desc);
  38 + if(status != 0){ perror("libusb_get_config_descriptor"); exit(-1); }
  39 + int configuration = config_desc->bConfigurationValue;
27 40  
28 41 // detach kernel
29   - if(libusb_kernel_driver_active(handle, interface)){
30   - status = libusb_detach_kernel_driver(handle, interface);
31   - if(status != 0){perror("libusb_detach_kernel_driver"); exit(-1);}
  42 + for(int j=0; j<config_desc->bNumInterfaces; j++){
  43 + for(int k=0; k<config_desc->interface[j].num_altsetting; k++){
  44 + int interface = config_desc->interface[j].altsetting[k].bInterfaceNumber;
  45 + if(libusb_kernel_driver_active(handle, interface)){
  46 + status = libusb_detach_kernel_driver(handle, interface);
  47 + if(status != 0){ perror("libusb_detach_kernel_driver"); exit(-1); }
  48 + }
  49 + }
32 50 }
33   -
  51 +
34 52 // use config
35   - // TODO int configuration = valueof("bConfigurationValue");
36 53 status = libusb_set_configuration(handle, configuration);
37   - if(status != 0){perror("libusb_set_configuration"); exit(-1);}
  54 + if(status != 0){ perror("libusb_set_configuration"); exit(-1); }
  55 +
  56 + // claim interfaces
  57 + for(int j=0; j<config_desc->bNumInterfaces; j++){
  58 + for(int k=0; k<config_desc->interface[j].num_altsetting; k++){
  59 + int interface = config_desc->interface[j].altsetting[k].bInterfaceNumber;
  60 + status = libusb_claim_interface(handle, interface);
  61 + if(status != 0){ perror("libusb_claim_interface"); exit(-1); }
  62 + }
  63 + }
38 64  
39 65 /* ... some code ... */
  66 + // TODO endpoints
40 67  
41   - // close interface
42   - status = libusb_release_interface(handle, interface);
43   - if(status != 0){perror("libusb_release_interface"); exit(-1);}
  68 + // release interfaces
  69 + for(int j=0; j<config_desc->bNumInterfaces; j++){
  70 + for(int k=0; k<config_desc->interface[j].num_altsetting; k++){
  71 + int interface = config_desc->interface[j].altsetting[k].bInterfaceNumber;
  72 + status = libusb_release_interface(handle, interface);
  73 + if(status != 0){ perror("libusb_release_interface"); exit(-1); }
  74 + }
  75 + }
  76 +
  77 + // free config
  78 + libusb_free_config_descriptor(config_desc);
44 79  
45 80 // close connection
46 81 libusb_close(handle);
... ...