Commit 34d0d1db855d17d9c87ff7926e74ca584ea1f930
1 parent
f5d752c1
seance 1
Showing
2 changed files
with
83 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,51 @@ |
1 | +#include <libusb-1.0/libusb.h> | |
2 | +#include <stdio.h> | |
3 | +#include <stdlib.h> | |
4 | + | |
5 | +int main(){ | |
6 | + // init | |
7 | + libusb_context *context; | |
8 | + int status = libusb_init(&context); | |
9 | + if(status != 0){perror("libusb_init"); exit(-1);} | |
10 | + | |
11 | + // devices list | |
12 | + libusb_device **devices; | |
13 | + ssize_t devices_count = libusb_get_device_list(context, &devices); | |
14 | + if(devices_count < 0){perror("libusb_get_device_list"); exit(-1);} | |
15 | + for(int i=0; i<devices_count; i++){ | |
16 | + libusb_device *device = devices[i]; | |
17 | + | |
18 | + // open connection | |
19 | + libusb_device_handle *handle; | |
20 | + status=libusb_open(device,&handle); | |
21 | + if(status!=0){ perror("libusb_open"); exit(-1); } | |
22 | + | |
23 | + // init interface | |
24 | + // TODO int interface = valueof("bInterfaceNumber"); | |
25 | + status = libusb_claim_interface(handle, interface); | |
26 | + if(status != 0){perror("libusb_claim_interface"); exit(-1);} | |
27 | + | |
28 | + // detach kernel | |
29 | + if(libusb_kernel_driver_active(handle, interface)){ | |
30 | + status = libusb_detach_kernel_driver(handle, interface); | |
31 | + if(status != 0){perror("libusb_detach_kernel_driver"); exit(-1);} | |
32 | + } | |
33 | + | |
34 | + // use config | |
35 | + // TODO int configuration = valueof("bConfigurationValue"); | |
36 | + status = libusb_set_configuration(handle, configuration); | |
37 | + if(status != 0){perror("libusb_set_configuration"); exit(-1);} | |
38 | + | |
39 | + /* ... some code ... */ | |
40 | + | |
41 | + // close interface | |
42 | + status = libusb_release_interface(handle, interface); | |
43 | + if(status != 0){perror("libusb_release_interface"); exit(-1);} | |
44 | + | |
45 | + // close connection | |
46 | + libusb_close(handle); | |
47 | + } | |
48 | + libusb_free_device_list(devices, 1); | |
49 | + | |
50 | + libusb_exit(context); | |
51 | +} | ... | ... |
... | ... | @@ -0,0 +1,32 @@ |
1 | +#include <libusb-1.0/libusb.h> | |
2 | +#include <stdio.h> | |
3 | +#include <stdlib.h> | |
4 | + | |
5 | +int main(){ | |
6 | + // init | |
7 | + libusb_context *context; | |
8 | + int status = libusb_init(&context); | |
9 | + if(status != 0){perror("libusb_init"); exit(-1);} | |
10 | + | |
11 | + // devices list | |
12 | + libusb_device **devices; | |
13 | + ssize_t devices_count = libusb_get_device_list(context, &devices); | |
14 | + if(devices_count < 0){perror("libusb_get_device_list");exit(-1);} | |
15 | + for(int i=0; i<devices_count; i++){ | |
16 | + libusb_device *device = devices[i]; | |
17 | + | |
18 | + struct libusb_device_descriptor desc; | |
19 | + int status = libusb_get_device_descriptor(device, &desc); | |
20 | + if(status != 0) continue; | |
21 | + | |
22 | + uint8_t bus = libusb_get_bus_number(device); | |
23 | + uint8_t address = libusb_get_device_address(device); | |
24 | + | |
25 | + printf("Device Found @ (Bus:Address) %d:%d\n", bus, address); | |
26 | + printf("Vendor ID 0x0%x\n", desc.idVendor); | |
27 | + printf("Product ID 0x0%x\n", desc.idProduct); | |
28 | + } | |
29 | + libusb_free_device_list(devices, 1); | |
30 | + | |
31 | + libusb_exit(context); | |
32 | +} | ... | ... |