Blame view

src/libusb_wrapper.c 4.51 KB
d6e16d2e   achemin1   Tout propre
1
  #include "libusb_wrapper.h"
b79c0c43   gperson   ajout de code,Ver...
2
  
b24d2980   achemin1   Nettoyage du code...
3
  
72cd0d89   achemin1   (Ré)Ajout 4.1 bonus
4
  
d6e16d2e   achemin1   Tout propre
5
6
7
8
9
10
11
  void usbinit(libusb_context **context_ptr) {
      //libusb_context *context;
      int statusInit = libusb_init(context_ptr);
      if (statusInit != LIBUSB_SUCCESS) {
          perror("libusb_init");
          exit(-1);
      }
b24d2980   achemin1   Nettoyage du code...
12
13
  }
  
d6e16d2e   achemin1   Tout propre
14
15
  void usbclose(libusb_context *context) {
      libusb_exit(context);
b24d2980   achemin1   Nettoyage du code...
16
17
  }
  
d6e16d2e   achemin1   Tout propre
18
19
20
21
22
23
24
25
26
  // Si le méchant noyau est passé avant vous :
  void _getFromKernel(libusb_device_handle *handle, int interface) { //private method for now
      if (libusb_kernel_driver_active(handle, interface)) {
          int statusKDriver = libusb_detach_kernel_driver(handle, interface);
          if (statusKDriver != LIBUSB_SUCCESS) {
              perror("libusb_detach_kernel_driver");
              exit(-1);
          }
      }
b24d2980   achemin1   Nettoyage du code...
27
28
  }
  
d6e16d2e   achemin1   Tout propre
29
30
31
32
33
34
35
36
37
  ssize_t getListDevices(libusb_context *context, libusb_device ***list_ptr) {
      ssize_t count = libusb_get_device_list(context, list_ptr);
      if (count < 0) {
          perror("libusb_get_device_list");
          exit(-1);
      }
      return count;
  }
  
6bd47a15   achemin1   Ajout du principe...
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  void* _enumerateDevices(libusb_context *context, void (*func)(libusb_device *device)){
      libusb_device **list;
      ssize_t count = getListDevices(context, &list);
  
      ssize_t i = 0;
      for (i = 0; i < count; i++) {
          libusb_device *device = list[i];
  
          func(device);
      }
  
      libusb_free_device_list(list, 1);
  
      return NULL; //DBGONLY TEMP
  }
  
d6e16d2e   achemin1   Tout propre
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  //print readable string, not asked for tutoring
  void _printConfig(libusb_device *device) {//private method
      // Ouverture du périphérique
      libusb_device_handle *handle;
      int statusDevice = libusb_open(device, &handle);
      if (statusDevice != LIBUSB_SUCCESS) {
          perror("libusb_open");
          //return; //exit(-1);
      }
  
      int MAXLEN_DESCRIPTOR_STRING = 200;
      uint8_t desc_idx = 2;
      //uint16_t langid = 16;
      unsigned char data[200];
  
      // TEST 16
      for (desc_idx = 0; desc_idx < 16; desc_idx++) {
          int statusAscii = libusb_get_string_descriptor_ascii(
                  handle, desc_idx, data, MAXLEN_DESCRIPTOR_STRING);
          if (statusAscii == -9) // TEST seems to be LIBUSB_ERROR
              break;
          printf(" - Descriptor string : %s ; %i\n", data, statusAscii);
      }
  
      libusb_close(handle);
b24d2980   achemin1   Nettoyage du code...
79
80
  }
  
533260c1   achemin1   Ajout des lecture...
81
  
6bd47a15   achemin1   Ajout du principe...
82
83
  void _displayOneDevice(libusb_device *device){
      struct libusb_device_descriptor desc;
533260c1   achemin1   Ajout des lecture...
84
  
6bd47a15   achemin1   Ajout du principe...
85
86
87
88
89
90
      int status = libusb_get_device_descriptor(device, &desc);
      if (status != LIBUSB_SUCCESS){
          printf("Cannot get device desc : %s\n", libusb_error_name(status)); //DBGONLY
          perror("libusb_open");
          return;
      }
533260c1   achemin1   Ajout des lecture...
91
  
6bd47a15   achemin1   Ajout du principe...
92
93
      uint8_t bus = libusb_get_bus_number(device);
      uint8_t address = libusb_get_device_address(device);
533260c1   achemin1   Ajout des lecture...
94
  
6bd47a15   achemin1   Ajout du principe...
95
96
97
      printf("Device Found @ (Bus:Address) %d:%d\n", bus, address);
      printf("Vendor ID 0x0%x\n", desc.idVendor);
      printf("Product ID 0x0%x\n", desc.idProduct);
533260c1   achemin1   Ajout des lecture...
98
  
6bd47a15   achemin1   Ajout du principe...
99
100
      //displayDeviceEndpoints(device); //Not really work on Axel@Alptop
  }
533260c1   achemin1   Ajout des lecture...
101
  
6bd47a15   achemin1   Ajout du principe...
102
103
104
  void displayDevices(libusb_context *context) {
      _enumerateDevices(context, _displayOneDevice);
  }
d6e16d2e   achemin1   Tout propre
105
  
72cd0d89   achemin1   (Ré)Ajout 4.1 bonus
106
107
108
109
110
111
112
113
114
115
  void _displayOneDeviceMore(libusb_device *device){
      _displayOneDevice(device);
      _printConfig(device);
  }
  
  void displayDevicesMore(libusb_context *context) {
      _enumerateDevices(context, _displayOneDeviceMore);
  }
  
  
6bd47a15   achemin1   Ajout du principe...
116
  //void getFirstDeviceFromID(vid, pid)
d6e16d2e   achemin1   Tout propre
117
  
d6e16d2e   achemin1   Tout propre
118
  
6bd47a15   achemin1   Ajout du principe...
119
  //void getDevicesFromID(vid, pid)
d6e16d2e   achemin1   Tout propre
120
  
533260c1   achemin1   Ajout des lecture...
121
  
6bd47a15   achemin1   Ajout du principe...
122
123
124
125
  void displayDeviceEndpoints(libusb_device_handle* handle) {
      //recover pointer from handle
      libusb_device *device;
      device = libusb_get_device(handle);
c8f6b178   achemin1   Affichage valeur ...
126
  
3d1c6506   achemin1   Affichage de l'ex...
127
  
d6e16d2e   achemin1   Tout propre
128
      // lectures des configs?
3d1c6506   achemin1   Affichage de l'ex...
129
130
      int configuration = 0; // valueof("bConfigurationValue");
      int statusConfig = libusb_set_configuration(handle, configuration);
d6e16d2e   achemin1   Tout propre
131
132
133
      if (statusConfig != LIBUSB_SUCCESS) {
          perror("libusb_set_configuration");
          return;
fef05237   achemin1   ajout condition p...
134
      }
c8f6b178   achemin1   Affichage valeur ...
135
  
d6e16d2e   achemin1   Tout propre
136
  
3d1c6506   achemin1   Affichage de l'ex...
137
138
      // 4.2 configuration du périph usb
      struct libusb_config_descriptor *config;
d6e16d2e   achemin1   Tout propre
139
140
141
142
      int statusFetchConfig = libusb_get_active_config_descriptor(device, &config);
      if (statusFetchConfig != LIBUSB_SUCCESS) {
          perror("libusb_get_active_config_descriptor");
          return;
c8f6b178   achemin1   Affichage valeur ...
143
144
      }
      printf("Config.bConfigurationValue : %d\n", config->bConfigurationValue);
3d1c6506   achemin1   Affichage de l'ex...
145
146
147
      printf("Config/ bLength:%d;bDescriptorType:%d;bNumInterfaces:%d",
             config->bLength, config->bDescriptorType, config->bNumInterfaces);
  
3d1c6506   achemin1   Affichage de l'ex...
148
  
d6e16d2e   achemin1   Tout propre
149
      // itération de l'interface
3d1c6506   achemin1   Affichage de l'ex...
150
151
      for (int indexInterface = 0; indexInterface < config->bNumInterfaces;
           indexInterface++) {
d6e16d2e   achemin1   Tout propre
152
153
154
155
          // struct libusb_interface * interface =
          // &config->interface[indexInterface];
          printf("^%d", indexInterface);
          printf("Alt%d", config->interface[indexInterface].num_altsetting);
3d1c6506   achemin1   Affichage de l'ex...
156
157
158
      }
  
      printf("\n");
b79c0c43   gperson   ajout de code,Ver...
159
  
d6e16d2e   achemin1   Tout propre
160
      libusb_close(handle);
b79c0c43   gperson   ajout de code,Ver...
161
  
d6e16d2e   achemin1   Tout propre
162
  }