Commit 7d6f5a45ed1474e28f2b235fb3af1ce446cbf91d

Authored by achemin1
1 parent 7e2160f8

add begin of main program

Showing 3 changed files with 105 additions and 17 deletions   Show diff stats
include/libusb_wrapper.h
... ... @@ -11,10 +11,15 @@ void usbinit(libusb_context** context_ptr);
11 11 void usbclose(libusb_context* context);
12 12  
13 13 ssize_t getListDevices(libusb_context* context, libusb_device***list_ptr);
  14 +void getFromKernel(libusb_device_handle *handle, int interface);
  15 +
14 16  
15 17 void displayDevices(libusb_context *context);
16 18 void displayDevicesMore(libusb_context *context);
17 19  
  20 +void getFirstDeviceFromID(libusb_context *context, int vid, int pid, libusb_device **device);
  21 +
  22 +
18 23 void displayDeviceEndpoints ();
19 24  
20 25 #endif
... ...
src/libusb_wrapper.c
... ... @@ -16,7 +16,7 @@ void usbclose(libusb_context *context) {
16 16 }
17 17  
18 18 // Si le méchant noyau est passé avant vous :
19   -void _getFromKernel(libusb_device_handle *handle, int interface) { //private method for now
  19 +void getFromKernel(libusb_device_handle *handle, int interface) { //private method for now
20 20 if (libusb_kernel_driver_active(handle, interface)) {
21 21 int statusKDriver = libusb_detach_kernel_driver(handle, interface);
22 22 if (statusKDriver != LIBUSB_SUCCESS) {
... ... @@ -112,11 +112,35 @@ void displayDevicesMore(libusb_context *context) {
112 112 _enumerateDevices(context, _displayOneDeviceMore);
113 113 }
114 114  
  115 +//get device from iteration (and not from not recommanded function :
  116 +//ugly using global vars ?? Cannot communicate between getFirstDeviceFromID and _getFirstDeviceFromID
  117 +int g_vid;
  118 +int g_pid;
  119 +libusb_device *g_device = NULL;
  120 +void _getFirstDeviceFromID(libusb_device *device) {
  121 + struct libusb_device_descriptor desc;
115 122  
116   -//void getFirstDeviceFromID(vid, pid)
  123 + int status = libusb_get_device_descriptor(device, &desc);
  124 + if (status != LIBUSB_SUCCESS){
  125 + printf("Cannot get device desc : %s\n", libusb_error_name(status)); //DBGONLY
  126 + perror("libusb_open");
  127 + return;
  128 + }
117 129  
118 130  
119   -//void getDevicesFromID(vid, pid)
  131 + if(desc.idVendor == g_vid && desc.idProduct == g_pid){
  132 + g_device = device;
  133 + }
  134 +}
  135 +
  136 +void getFirstDeviceFromID(libusb_context *context, int vid, int pid, libusb_device **device) {
  137 + g_vid = vid; //pass parameters for enumeration
  138 + g_pid = pid; //idem
  139 + _enumerateDevices(context, _getFirstDeviceFromID);
  140 + device = &g_device; //get return from enumeration
  141 +}
  142 +
  143 +//void getDevicesFromID(vid, pid) //return array
120 144  
121 145  
122 146 void displayDeviceEndpoints(libusb_device_handle* handle) {
... ... @@ -125,14 +149,13 @@ void displayDeviceEndpoints(libusb_device_handle* handle) {
125 149 device = libusb_get_device(handle);
126 150  
127 151  
128   - // lectures des configs?
129   - int configuration = 0; // valueof("bConfigurationValue");
  152 + // lectures des configs => on prend la première configuration pour l'instant
  153 + /*int configuration = 0; // valueof("bConfigurationValue");
130 154 int statusConfig = libusb_set_configuration(handle, configuration);
131 155 if (statusConfig != LIBUSB_SUCCESS) {
132 156 perror("libusb_set_configuration");
133 157 return;
134   - }
135   -
  158 + }*/
136 159  
137 160 // 4.2 configuration du périph usb
138 161 struct libusb_config_descriptor *config;
... ... @@ -141,18 +164,63 @@ void displayDeviceEndpoints(libusb_device_handle* handle) {
141 164 perror("libusb_get_active_config_descriptor");
142 165 return;
143 166 }
  167 +
  168 + //caractéristiques globales
144 169 printf("Config.bConfigurationValue : %d\n", config->bConfigurationValue);
145   - printf("Config/ bLength:%d;bDescriptorType:%d;bNumInterfaces:%d",
  170 + printf("Config/ bLength:%d\nbDescriptorType:%d\nbNumInterfaces:%d\n",
146 171 config->bLength, config->bDescriptorType, config->bNumInterfaces);
147 172  
148 173  
149   - // itération de l'interface
150   - for (int indexInterface = 0; indexInterface < config->bNumInterfaces;
151   - indexInterface++) {
152   - // struct libusb_interface * interface =
153   - // &config->interface[indexInterface];
154   - printf("^%d", indexInterface);
155   - printf("Alt%d", config->interface[indexInterface].num_altsetting);
  174 + //itération des interfaces
  175 + printf("Itération de l'interface\n");
  176 + for (int indexInterface = 0; indexInterface < config->bNumInterfaces; indexInterface++) {
  177 + printf("-indexInterface=%d\n", indexInterface);
  178 + printf("-Altsetting=%d\n", config->interface[indexInterface].num_altsetting);
  179 +
  180 + const struct libusb_interface * interface = &config->interface[indexInterface];
  181 +
  182 +
  183 + //if 1 setting (or more)
  184 + if(interface->num_altsetting != 0) {
  185 + const struct libusb_interface_descriptor * interface_desc = &interface->altsetting[0];
  186 +
  187 + printf("--bNumEndpoints=%d\n", interface_desc->bNumEndpoints);
  188 + printf("--bDescriptorType=%d\n", interface_desc->bDescriptorType);
  189 +
  190 + for(int indexEndpoints = 0; indexEndpoints < interface_desc->bNumEndpoints ; indexEndpoints++){
  191 + const struct libusb_endpoint_descriptor * endpoint_desc = &interface_desc->endpoint[indexEndpoints];
  192 + printf("---bDescriptorType=%d\n", endpoint_desc->bDescriptorType);
  193 + printf("---bEndpointAddress=%d\n", endpoint_desc->bEndpointAddress);
  194 +
  195 + if(endpoint_desc->extra != NULL)
  196 + printf("---extra: %s\n", endpoint_desc->extra);
  197 + if(endpoint_desc->bmAttributes == LIBUSB_TRANSFER_TYPE_INTERRUPT)//TODO AJOUT MASQUE ? (voir doc)
  198 + printf("---is of type INTERRUPT\n");
  199 +
  200 + }
  201 + }
  202 +
  203 +
  204 +
  205 + //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.
  206 +
  207 +/*
  208 + //int interface=valueof("bInterfaceNumber");
  209 + int status=libusb_claim_interface(handle,interface->bInterfaceNumber);
  210 + if(status!=0){ perror("libusb_claim_interface"); exit(-1); }
  211 +
  212 + status=libusb_release_interface(handle,interface->bInterfaceNumber);
  213 + if(status!=0){ perror("libusb_release_interface"); exit(-1); }
  214 +
  215 + //claim_interface(.., config interface altsetting bInterfaceNumber
  216 + //config interface altsetting endpoint bmAttributes == LIBUSB_TRANSFER_TYPE_INTERRUPT
  217 + //add des enpoint unint8_t*/
  218 +
  219 +
  220 + //DESCRIPTOR.C /.H :décommenter
  221 +
  222 +
  223 + //IL FAUT RELEASE UNIQUEMENT LES INTERFACES QUI NE SONT PAS DES HID
156 224 }
157 225  
158 226 printf("\n");
... ...
src/main.c
... ... @@ -2,6 +2,9 @@
2 2 #include <stdio.h>
3 3 #include <string.h>
4 4  
  5 +#define VID 1003
  6 +#define PID 8259
  7 +
5 8 int main(int argc, char *argv[]){
6 9 printf("Hello World :-|\n");
7 10  
... ... @@ -34,7 +37,7 @@ int main(int argc, char *argv[]){
34 37 handle = libusb_open_device_with_vid_pid(context, vendor_id, product_id);
35 38 if(handle != NULL){
36 39 printf("Show endpoints of VID:%hu;PID:%hu\n", vendor_id, product_id);
37   - displayDeviceEndpoints(NULL);
  40 + displayDeviceEndpoints(handle);
38 41 }
39 42 else{
40 43 printf("Error while getting handle of VID:%hu;PID:%hu\n", vendor_id, product_id);
... ... @@ -49,8 +52,20 @@ int main(int argc, char *argv[]){
49 52 }else{
50 53 //interactive mode
51 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 interfaces
52 68  
53   - /*Future code*/
54 69  
55 70 printf("Finished\n");
56 71 }
... ...