Blame view

tests/USBEndpoints/test_list_endpoints.c 2.77 KB
34d0d1db   pifou   seance 1
1
2
3
4
  #include <libusb-1.0/libusb.h>
  #include <stdio.h>
  #include <stdlib.h>
  
32bf1efe   root   correction du scr...
5
6
  // arduino ID vendor and product
  #define ID_VENDOR 0x2341
659c68f6   pifou   Recherches sur at...
7
  #define ID_PRODUCT 0x01
9e490f23   pifou   recuperation des ...
8
  
08e49fd9   pifou   Seance 2
9
10
  // gcc file.c -l usb-1.0
  
34d0d1db   pifou   seance 1
11
12
13
14
15
16
17
18
19
20
21
22
23
  int main(){
  	// init
  	libusb_context *context;
  	int status = libusb_init(&context);
  	if(status != 0){perror("libusb_init"); exit(-1);}
  
  	// devices list
  	libusb_device **devices;
  	ssize_t devices_count = libusb_get_device_list(context, &devices);
  	if(devices_count < 0){perror("libusb_get_device_list"); exit(-1);}
  	for(int i=0; i<devices_count; i++){
  		libusb_device *device = devices[i];
  
08e49fd9   pifou   Seance 2
24
25
26
27
28
29
  		// device description
  		struct libusb_device_descriptor desc;
  		int status = libusb_get_device_descriptor(device, &desc);
  		if(status != 0) continue;
  		
  		// search for device
32bf1efe   root   correction du scr...
30
31
  		if(desc.idVendor != ID_VENDOR
  				|| desc.idProduct != ID_PRODUCT)
08e49fd9   pifou   Seance 2
32
33
  			continue;
  
34d0d1db   pifou   seance 1
34
35
  		// open connection
  		libusb_device_handle *handle;
08e49fd9   pifou   Seance 2
36
37
38
39
40
41
42
43
  		status = libusb_open(device, &handle);
  		if(status != 0){ perror("libusb_open"); exit(-1); }
  		
  		// prepare config
  		struct libusb_config_descriptor *config_desc;
  		status = libusb_get_config_descriptor(device, 0, &config_desc);
  		if(status != 0){ perror("libusb_get_config_descriptor"); exit(-1); }
  		int configuration = config_desc->bConfigurationValue;
34d0d1db   pifou   seance 1
44
45
  
  		// detach kernel
08e49fd9   pifou   Seance 2
46
  		for(int j=0; j<config_desc->bNumInterfaces; j++){
9e490f23   pifou   recuperation des ...
47
48
49
50
  			int interface = config_desc->interface[j].altsetting[0].bInterfaceNumber;
  			if(libusb_kernel_driver_active(handle, interface)){
  				status = libusb_detach_kernel_driver(handle, interface);
  				if(status != 0){ perror("libusb_detach_kernel_driver"); exit(-1); }
08e49fd9   pifou   Seance 2
51
  			}
34d0d1db   pifou   seance 1
52
  		}
08e49fd9   pifou   Seance 2
53
  		
34d0d1db   pifou   seance 1
54
  		// use config
34d0d1db   pifou   seance 1
55
  		status = libusb_set_configuration(handle, configuration);
08e49fd9   pifou   Seance 2
56
57
58
59
  		if(status != 0){ perror("libusb_set_configuration"); exit(-1); }
  		
  		// claim interfaces
  		for(int j=0; j<config_desc->bNumInterfaces; j++){
32bf1efe   root   correction du scr...
60
  			struct libusb_interface_descriptor interface_desc = config_desc->interface[j].altsetting[0];
9e490f23   pifou   recuperation des ...
61
  			
32bf1efe   root   correction du scr...
62
  			int interface = interface_desc.bInterfaceNumber;
9e490f23   pifou   recuperation des ...
63
64
65
66
  			status = libusb_claim_interface(handle, interface);
  			if(status != 0){ perror("libusb_claim_interface"); exit(-1); }
  			
  			// find endpoints
32bf1efe   root   correction du scr...
67
68
  			for(int k=0; k<interface_desc.bNumEndpoints; k++){
  				struct libusb_endpoint_descriptor endpoint_desc = interface_desc.endpoint[k];
9e490f23   pifou   recuperation des ...
69
  				
9d698fff   pifou   Listage des endpo...
70
71
  				// list endpoints bEndpointAddress and bmAttributes
  				printf("Endpoint inter. %d, num. %d\n", j, k);
32bf1efe   root   correction du scr...
72
73
  				printf("bEndpointAddress = 0x%x\n", endpoint_desc.bEndpointAddress);
  				printf("bmAttributes = 0x%x\n", endpoint_desc.bmAttributes);
08e49fd9   pifou   Seance 2
74
  			}
9e490f23   pifou   recuperation des ...
75
76
77
78
  			
  			// release interfaces
  			status = libusb_release_interface(handle, interface);
  			if(status != 0){ perror("libusb_release_interface"); exit(-1); }
08e49fd9   pifou   Seance 2
79
80
81
82
  		}
  		
  		// free config
  		libusb_free_config_descriptor(config_desc);
34d0d1db   pifou   seance 1
83
84
85
86
87
88
89
90
  
  		// close connection
  		libusb_close(handle);
  	}
  	libusb_free_device_list(devices, 1);
  
  	libusb_exit(context);
  }