Commit d6e16d2ead77f14f9dc73044f9f37c06e2502399
1 parent
b24d2980
Tout propre
Showing
4 changed files
with
128 additions
and
109 deletions
Show diff stats
README.md
include/libusb_wrapper.h
1 | +#ifndef LIBUSB_WRAPPER_H_ | |
2 | +#define LIBUSB_WRAPPER_H_ | |
3 | + | |
1 | 4 | #include <libusb-1.0/libusb.h> |
5 | +#include <stdlib.h> | |
6 | +#include <stdio.h> | |
7 | + | |
8 | +void usbinit(libusb_context** context_ptr); | |
9 | + | |
10 | +void usbclose(libusb_context* context); | |
11 | + | |
12 | +ssize_t getListDevices(libusb_context* context, libusb_device***list_ptr); | |
2 | 13 | |
3 | -void usbinit(); | |
14 | +void displayDevices(libusb_context *context); | |
4 | 15 | |
5 | -void usbclose(); | |
16 | +void displayDeviceEndpoints (); | |
6 | 17 | |
7 | -void displayDevices(); | |
18 | +#endif | |
8 | 19 | |
9 | -void displayDeviceEndpoints(); | ... | ... |
src/libusb_wrapper.c
1 | -#include <libusb-1.0/libusb.h> | |
2 | -#include <stdio.h> | |
3 | -#include <stdlib.h> | |
1 | +#include "libusb_wrapper.h" | |
4 | 2 | |
5 | 3 | |
6 | -void usbinit(){ | |
7 | - printf("Hello usbinit\n"); | |
4 | +void usbinit(libusb_context **context_ptr) { | |
5 | + //libusb_context *context; | |
6 | + int statusInit = libusb_init(context_ptr); | |
7 | + if (statusInit != LIBUSB_SUCCESS) { | |
8 | + perror("libusb_init"); | |
9 | + exit(-1); | |
10 | + } | |
8 | 11 | } |
9 | 12 | |
10 | -void usbclose(){ | |
11 | - printf("Hello usbclose\n"); | |
13 | +void usbclose(libusb_context *context) { | |
14 | + libusb_exit(context); | |
12 | 15 | } |
13 | 16 | |
14 | -void displayDevices(){ | |
15 | - printf("Hello list of device"); | |
17 | +// Si le mรฉchant noyau est passรฉ avant vous : | |
18 | +void _getFromKernel(libusb_device_handle *handle, int interface) { //private method for now | |
19 | + if (libusb_kernel_driver_active(handle, interface)) { | |
20 | + int statusKDriver = libusb_detach_kernel_driver(handle, interface); | |
21 | + if (statusKDriver != LIBUSB_SUCCESS) { | |
22 | + perror("libusb_detach_kernel_driver"); | |
23 | + exit(-1); | |
24 | + } | |
25 | + } | |
16 | 26 | } |
17 | 27 | |
18 | -void displayDeviceEndpoints(){ | |
19 | - printf("Hello endpoint\n"); | |
28 | +ssize_t getListDevices(libusb_context *context, libusb_device ***list_ptr) { | |
29 | + ssize_t count = libusb_get_device_list(context, list_ptr); | |
30 | + if (count < 0) { | |
31 | + perror("libusb_get_device_list"); | |
32 | + exit(-1); | |
33 | + } | |
34 | + return count; | |
35 | +} | |
36 | + | |
37 | +//print readable string, not asked for tutoring | |
38 | +void _printConfig(libusb_device *device) {//private method | |
39 | + // Ouverture du pรฉriphรฉrique | |
40 | + libusb_device_handle *handle; | |
41 | + int statusDevice = libusb_open(device, &handle); | |
42 | + if (statusDevice != LIBUSB_SUCCESS) { | |
43 | + perror("libusb_open"); | |
44 | + //return; //exit(-1); | |
45 | + } | |
46 | + | |
47 | + int MAXLEN_DESCRIPTOR_STRING = 200; | |
48 | + uint8_t desc_idx = 2; | |
49 | + //uint16_t langid = 16; | |
50 | + unsigned char data[200]; | |
51 | + | |
52 | + // TEST 16 | |
53 | + for (desc_idx = 0; desc_idx < 16; desc_idx++) { | |
54 | + int statusAscii = libusb_get_string_descriptor_ascii( | |
55 | + handle, desc_idx, data, MAXLEN_DESCRIPTOR_STRING); | |
56 | + if (statusAscii == -9) // TEST seems to be LIBUSB_ERROR | |
57 | + break; | |
58 | + printf(" - Descriptor string : %s ; %i\n", data, statusAscii); | |
59 | + } | |
60 | + | |
61 | + libusb_close(handle); | |
20 | 62 | } |
21 | 63 | |
22 | -/*int main() { | |
23 | 64 | |
24 | - // initiate the usb thing | |
65 | +void displayDevices(libusb_context *context) { | |
66 | + libusb_device **list; | |
67 | + ssize_t count = getListDevices(context, &list); | |
25 | 68 | |
26 | - libusb_context *context; | |
27 | - int statusInit = libusb_init(&context); | |
28 | - if (statusInit != 0) { | |
29 | - perror("libusb_init"); | |
30 | - exit(-1); | |
31 | - } | |
32 | - /* ... some code ...*//* | |
69 | + ssize_t i = 0; | |
70 | + for (i = 0; i < count; i++) { | |
71 | + libusb_device *device = list[i]; | |
72 | + struct libusb_device_descriptor desc; | |
33 | 73 | |
34 | - // actually enumerates all the usb and gives some infos abot the usb things | |
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 | + } | |
35 | 80 | |
36 | - libusb_device **list; | |
37 | - ssize_t count = libusb_get_device_list(context, &list); | |
38 | - if (count < 0) { | |
39 | - perror("libusb_get_device_list"); | |
40 | - exit(-1); | |
41 | - } | |
42 | - ssize_t i = 0; | |
43 | - for (i = 0; i < count; i++) { | |
44 | - libusb_device *device = list[i]; | |
45 | - struct libusb_device_descriptor desc; | |
81 | + uint8_t bus = libusb_get_bus_number(device); | |
82 | + uint8_t address = libusb_get_device_address(device); | |
46 | 83 | |
47 | - int status = libusb_get_device_descriptor(device, &desc); | |
48 | - if (status != 0) | |
49 | - continue; | |
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); | |
50 | 87 | |
51 | - uint8_t bus = libusb_get_bus_number(device); | |
52 | - uint8_t address = libusb_get_device_address(device); | |
53 | 88 | |
54 | - printf("Device Found @ (Bus:Address) %d:%d\n", bus, address); | |
55 | - printf("Vendor ID 0x0%x\n", desc.idVendor); | |
56 | - printf("Product ID 0x0%x\n", desc.idProduct); | |
89 | + //_printConfig(device); //Does it work ? | |
90 | + //displayDeviceEndpoints(device); //Not really work on Axel@Alptop | |
91 | + } | |
92 | + | |
93 | + libusb_free_device_list(list, 1); | |
94 | + | |
95 | +} | |
96 | + | |
97 | + | |
98 | +void displayDeviceEndpoints(libusb_device *device) { | |
99 | + printf("Yes you're in\n"); | |
57 | 100 | |
58 | 101 | // Ouverture du pรฉriphรฉrique |
59 | 102 | libusb_device_handle *handle; |
60 | 103 | int statusDevice = libusb_open(device, &handle); |
61 | - if (statusDevice != 0) { | |
62 | - perror("libusb_open"); | |
63 | - exit(-1); | |
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; | |
64 | 109 | } |
65 | 110 | |
66 | - // Si le mรฉchant noyau est passรฉ avant vous : | |
67 | - /*int interface = 0; // TEST : FONCTIONNE MAIS CORRESPOND A QUOI? | |
68 | - if (libusb_kernel_driver_active(handle, interface)) { | |
69 | - int statusKDriver = libusb_detach_kernel_driver(handle, interface); | |
70 | - if (statusKDriver != 0) { | |
71 | - perror("libusb_detach_kernel_driver"); | |
72 | - exit(-1); | |
73 | - } | |
74 | - }*/ | |
75 | 111 | |
76 | - /*// lectures des configs? | |
112 | + // lectures des configs? | |
77 | 113 | int configuration = 0; // valueof("bConfigurationValue"); |
78 | 114 | int statusConfig = libusb_set_configuration(handle, configuration); |
79 | - if (statusConfig != 0) { | |
80 | - perror("libusb_set_configuration"); /*exit(-1);*/ | |
81 | - /* } | |
82 | - *//* | |
83 | - int MAXLEN_DESCRIPTOR_STRING = 200; | |
84 | - uint8_t desc_idx = 2; | |
85 | - uint16_t langid = 16; | |
86 | - unsigned char data[200]; | |
87 | - | |
88 | - // TEST 16 | |
89 | - for (desc_idx = 0; desc_idx < 16; desc_idx++) { | |
90 | - int statusAscii = libusb_get_string_descriptor_ascii( | |
91 | - handle, desc_idx, data, MAXLEN_DESCRIPTOR_STRING); | |
92 | - if (statusAscii == -9) // TEST seems to be LIBUSB_ERROR | |
93 | - break; | |
94 | - printf("Descriptor string : %s ; %i\n", data, statusAscii); | |
115 | + if (statusConfig != LIBUSB_SUCCESS) { | |
116 | + perror("libusb_set_configuration"); | |
117 | + return; | |
95 | 118 | } |
96 | 119 | |
120 | + | |
97 | 121 | // 4.2 configuration du pรฉriph usb |
98 | 122 | struct libusb_config_descriptor *config; |
99 | - int statusFetchConfig = | |
100 | - libusb_get_active_config_descriptor(device, &config); | |
101 | - if (statusFetchConfig != 0) { | |
102 | - perror("libusb_get_active_config_descriptor"); | |
103 | - // exit(-1); | |
123 | + int statusFetchConfig = libusb_get_active_config_descriptor(device, &config); | |
124 | + if (statusFetchConfig != LIBUSB_SUCCESS) { | |
125 | + perror("libusb_get_active_config_descriptor"); | |
126 | + return; | |
104 | 127 | } |
105 | 128 | printf("Config.bConfigurationValue : %d\n", config->bConfigurationValue); |
106 | 129 | printf("Config/ bLength:%d;bDescriptorType:%d;bNumInterfaces:%d", |
107 | 130 | config->bLength, config->bDescriptorType, config->bNumInterfaces); |
108 | 131 | |
109 | - // itรฉration de l'interface | |
110 | 132 | |
133 | + // itรฉration de l'interface | |
111 | 134 | for (int indexInterface = 0; indexInterface < config->bNumInterfaces; |
112 | 135 | indexInterface++) { |
113 | - // struct libusb_interface * interface = | |
114 | - // &config->interface[indexInterface]; | |
115 | - printf("^%d", indexInterface); | |
116 | - printf("Alt%d", config->interface[indexInterface].num_altsetting); | |
136 | + // struct libusb_interface * interface = | |
137 | + // &config->interface[indexInterface]; | |
138 | + printf("^%d", indexInterface); | |
139 | + printf("Alt%d", config->interface[indexInterface].num_altsetting); | |
117 | 140 | } |
118 | 141 | |
119 | 142 | printf("\n"); |
120 | - /* | |
121 | - //appropriation de l'interface | |
122 | - | |
123 | - int statusClaimInterface=libusb_claim_interface(handle,interface); | |
124 | - if(status!=0){ perror("libusb_claim_interface"); exit(-1); } | |
125 | - /* ... some code ... */ | |
126 | - /* | |
127 | - | |
128 | - | |
129 | - | |
130 | - status=libusb_release_interface(handle,interface); | |
131 | - if(status!=0){ perror("libusb_release_interface"); exit(-1); } | |
132 | - */ | |
133 | - | |
134 | - /* ... some ... code */ | |
135 | - /*libusb_close(handle); | |
136 | - }*/ | |
137 | 143 | |
138 | -/* libusb_free_device_list(list, 1); | |
144 | + libusb_close(handle); | |
139 | 145 | |
140 | - libusb_exit(context); | |
141 | - return 0; | |
142 | -}*/ | |
146 | +} | |
143 | 147 | \ No newline at end of file | ... | ... |
src/main.c
1 | -#include <stdio.h> | |
2 | 1 | #include "libusb_wrapper.h" |
2 | +#include <stdio.h> | |
3 | 3 | |
4 | 4 | int main(){ |
5 | - printf("Hello World :)\n"); | |
5 | + printf("Hello World :-|\n"); | |
6 | 6 | |
7 | - usbinit(); | |
7 | + libusb_context *context; | |
8 | + usbinit(&context); | |
8 | 9 | |
9 | 10 | //if 4.1 |
10 | - displayDevices(); | |
11 | + displayDevices(context); | |
12 | + //else | |
11 | 13 | |
12 | 14 | //if 4.2 |
13 | - displayDeviceEndpoints(); | |
15 | + //displayDeviceEndpoints(Ourdevice); | |
16 | + //else | |
14 | 17 | |
15 | - usbclose(); | |
18 | + usbclose(context); | |
16 | 19 | |
17 | 20 | return 0; |
18 | 21 | } | ... | ... |