Commit 6bd47a159f2f6c2ce43a3ec572013393c5d013d1

Authored by achemin1
1 parent 4d621b43

Ajout du principe d'énumération des devices

Showing 1 changed file with 41 additions and 35 deletions   Show diff stats
src/libusb_wrapper.c
... ... @@ -34,6 +34,22 @@ ssize_t getListDevices(libusb_context *context, libusb_device ***list_ptr) {
34 34 return count;
35 35 }
36 36  
  37 +void* _enumerateDevices(libusb_context *context, void (*func)(libusb_device *device)){
  38 + libusb_device **list;
  39 + ssize_t count = getListDevices(context, &list);
  40 +
  41 + ssize_t i = 0;
  42 + for (i = 0; i < count; i++) {
  43 + libusb_device *device = list[i];
  44 +
  45 + func(device);
  46 + }
  47 +
  48 + libusb_free_device_list(list, 1);
  49 +
  50 + return NULL; //DBGONLY TEMP
  51 +}
  52 +
37 53 //print readable string, not asked for tutoring
38 54 void _printConfig(libusb_device *device) {//private method
39 55 // Ouverture du périphérique
... ... @@ -62,51 +78,41 @@ void _printConfig(libusb_device *device) {//private method
62 78 }
63 79  
64 80  
65   -void displayDevices(libusb_context *context) {
66   - libusb_device **list;
67   - ssize_t count = getListDevices(context, &list);
  81 +void _displayOneDevice(libusb_device *device){
  82 + struct libusb_device_descriptor desc;
68 83  
69   - ssize_t i = 0;
70   - for (i = 0; i < count; i++) {
71   - libusb_device *device = list[i];
72   - struct libusb_device_descriptor desc;
73   -
74   - int status = libusb_get_device_descriptor(device, &desc);
75   - if (status != LIBUSB_SUCCESS){
76   - printf("Cannot get device desc : %s\n", libusb_error_name(status)); //DBGONLY
77   - perror("libusb_open");
78   - continue;
79   - }
  84 + int status = libusb_get_device_descriptor(device, &desc);
  85 + if (status != LIBUSB_SUCCESS){
  86 + printf("Cannot get device desc : %s\n", libusb_error_name(status)); //DBGONLY
  87 + perror("libusb_open");
  88 + return;
  89 + }
80 90  
81   - uint8_t bus = libusb_get_bus_number(device);
82   - uint8_t address = libusb_get_device_address(device);
  91 + uint8_t bus = libusb_get_bus_number(device);
  92 + uint8_t address = libusb_get_device_address(device);
83 93  
84   - printf("Device Found @ (Bus:Address) %d:%d\n", bus, address);
85   - printf("Vendor ID 0x0%x\n", desc.idVendor);
86   - printf("Product ID 0x0%x\n", desc.idProduct);
  94 + printf("Device Found @ (Bus:Address) %d:%d\n", bus, address);
  95 + printf("Vendor ID 0x0%x\n", desc.idVendor);
  96 + printf("Product ID 0x0%x\n", desc.idProduct);
87 97  
  98 + //_printConfig(device); //Does it work ?
  99 + //displayDeviceEndpoints(device); //Not really work on Axel@Alptop
  100 +}
88 101  
89   - //_printConfig(device); //Does it work ?
90   - //displayDeviceEndpoints(device); //Not really work on Axel@Alptop
91   - }
  102 +void displayDevices(libusb_context *context) {
  103 + _enumerateDevices(context, _displayOneDevice);
  104 +}
92 105  
93   - libusb_free_device_list(list, 1);
  106 +//void getFirstDeviceFromID(vid, pid)
94 107  
95   -}
96 108  
  109 +//void getDevicesFromID(vid, pid)
97 110  
98   -void displayDeviceEndpoints(libusb_device *device) {
99   - printf("Yes you're in\n");
100 111  
101   - // Ouverture du périphérique
102   - libusb_device_handle *handle;
103   - int statusDevice = libusb_open(device, &handle);
104   - if (statusDevice != LIBUSB_SUCCESS) {
105   - printf("This error is : %d\n", statusDevice); //DBGONLY
106   - printf("Printable error : %s\n", libusb_error_name(statusDevice)); //DBGONLY
107   - perror("libusb_open");
108   - return;
109   - }
  112 +void displayDeviceEndpoints(libusb_device_handle* handle) {
  113 + //recover pointer from handle
  114 + libusb_device *device;
  115 + device = libusb_get_device(handle);
110 116  
111 117  
112 118 // lectures des configs?
... ...