Commit 9e490f23bd8cef9aec37f7a9ed16624409adc058

Authored by pifou
1 parent 08e49fd9

recuperation des endpoints

Showing 2 changed files with 57 additions and 22 deletions   Show diff stats
test_com.c
... ... @@ -2,6 +2,9 @@
2 2 #include <stdio.h>
3 3 #include <stdlib.h>
4 4  
  5 +#define ID_VENDOR 0x0951
  6 +#define ID_PRODUCT 0x01666
  7 +
5 8 // gcc file.c -l usb-1.0
6 9  
7 10 int main(){
... ... @@ -40,12 +43,10 @@ int main(){
40 43  
41 44 // detach kernel
42 45 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   - }
  46 + int interface = config_desc->interface[j].altsetting[0].bInterfaceNumber;
  47 + if(libusb_kernel_driver_active(handle, interface)){
  48 + status = libusb_detach_kernel_driver(handle, interface);
  49 + if(status != 0){ perror("libusb_detach_kernel_driver"); exit(-1); }
49 50 }
50 51 }
51 52  
... ... @@ -55,23 +56,23 @@ int main(){
55 56  
56 57 // claim interfaces
57 58 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   - }
64   -
65   - /* ... some code ... */
66   - // TODO endpoints
67   -
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); }
  59 + libusb_interface_descriptor *interface_desc = config_desc->interface[j].altsetting[0];
  60 +
  61 + int interface = interface_desc->bInterfaceNumber;
  62 + status = libusb_claim_interface(handle, interface);
  63 + if(status != 0){ perror("libusb_claim_interface"); exit(-1); }
  64 +
  65 + // find endpoints
  66 + for(int k=0; k<interface_desc->bNumEndpoints; k++){
  67 + libusb_endpoint_descriptor *endpoint_desc = interface_desc->endpoints
  68 +
  69 + /* ... some code ... */
  70 + // TODO what to do next ?
74 71 }
  72 +
  73 + // release interfaces
  74 + status = libusb_release_interface(handle, interface);
  75 + if(status != 0){ perror("libusb_release_interface"); exit(-1); }
75 76 }
76 77  
77 78 // free config
... ...
test_list_all.c 0 → 100644
... ... @@ -0,0 +1,34 @@
  1 +#include <libusb-1.0/libusb.h>
  2 +#include <stdio.h>
  3 +#include <stdlib.h>
  4 +
  5 +// gcc file.c -l usb-1.0
  6 +
  7 +int main(){
  8 + // init
  9 + libusb_context *context;
  10 + int status = libusb_init(&context);
  11 + if(status != 0){perror("libusb_init"); exit(-1);}
  12 +
  13 + // devices list
  14 + libusb_device **devices;
  15 + ssize_t devices_count = libusb_get_device_list(context, &devices);
  16 + if(devices_count < 0){perror("libusb_get_device_list");exit(-1);}
  17 + for(int i=0; i<devices_count; i++){
  18 + libusb_device *device = devices[i];
  19 +
  20 + struct libusb_device_descriptor desc;
  21 + int status = libusb_get_device_descriptor(device, &desc);
  22 + if(status != 0) continue;
  23 +
  24 + uint8_t bus = libusb_get_bus_number(device);
  25 + uint8_t address = libusb_get_device_address(device);
  26 +
  27 + printf("Device Found @ (Bus:Address) %d:%d\n", bus, address);
  28 + printf("Vendor ID 0x0%x\n", desc.idVendor);
  29 + printf("Product ID 0x0%x\n", desc.idProduct);
  30 + }
  31 + libusb_free_device_list(devices, 1);
  32 +
  33 + libusb_exit(context);
  34 +}
... ...