Blame view

tests/USBSendData/test_send_data.c 5.52 KB
97064000   root   16u2 desc terminé...
1
2
3
4
5
6
7
  #include <libusb-1.0/libusb.h>
  #include <stdio.h>
  #include <stdlib.h>
  
  // arduino ID vendor and product
  #define ID_VENDOR 0x2341
  #define ID_PRODUCT 0x01
dd5193ad   root   test USBSendData ...
8
9
  #define ENDPOINTS_NUMBER 4
  #define TIMEOUT 2000
bfb1101b   pifou   com 16u2 <-> PC c...
10
  #define MAX_DATA 50
97064000   root   16u2 desc terminé...
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  
  void init(libusb_context **context, libusb_device ***devices, ssize_t *devices_count){
  	int status = libusb_init(context);
  	if(status != 0){perror("libusb_init"); exit(-1);}
  
  	*devices_count = libusb_get_device_list(*context, devices);
  	if(*devices_count < 0){perror("libusb_get_device_list"); exit(-1);}
  }
  
  libusb_device* searchArduino(libusb_device **devices, ssize_t devices_count){
  	for(int i=0; i<devices_count; i++){
  		libusb_device *device = devices[i];
  
  		// device description
  		struct libusb_device_descriptor desc;
  		int status = libusb_get_device_descriptor(device, &desc);
  		if(status != 0) continue;
  		
  		// search for device
  		if(desc.idVendor == ID_VENDOR
  				&& desc.idProduct == ID_PRODUCT)
  			return device;
  	}
  	return NULL;
  }
  
  void openConnection(libusb_device *arduino, libusb_device_handle **handle, struct libusb_config_descriptor **config_desc){
  	// open connection
  	int status = libusb_open(arduino, handle);
  	if(status != 0){ perror("libusb_open"); exit(-1); }
  	
  	// prepare config
  	status = libusb_get_config_descriptor(arduino, 0, config_desc);
  	if(status != 0){ perror("libusb_get_config_descriptor"); exit(-1); }
  	int configuration = (*config_desc)->bConfigurationValue;
  
  	// detach kernel
  	for(int j=0; j<(*config_desc)->bNumInterfaces; j++){
  		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); }
  		}
  	}
  	
  	// use config
  	status = libusb_set_configuration(*handle, configuration);
  	if(status != 0){ perror("libusb_set_configuration"); exit(-1); }
  	
  	// claim interfaces
  	for(int j=0; j<(*config_desc)->bNumInterfaces; j++){
  		struct libusb_interface_descriptor interface_desc = (*config_desc)->interface[j].altsetting[0];
  		int interface = interface_desc.bInterfaceNumber;
  
  		status = libusb_claim_interface(*handle, interface);
  		if(status != 0){ perror("libusb_claim_interface"); exit(-1); }
  	}
  }
  
dd5193ad   root   test USBSendData ...
70
71
  void getEndpoints(struct libusb_config_descriptor *config_desc, struct libusb_endpoint_descriptor *endpoint_desc_list){
  	int count = 0;
97064000   root   16u2 desc terminé...
72
73
74
75
  	// in interfaces
  	for(int j=0; j<config_desc->bNumInterfaces; j++){
  		// find endpoints
  		for(int k=0; k<config_desc->interface[j].altsetting[0].bNumEndpoints; k++){
dd5193ad   root   test USBSendData ...
76
77
78
  			if(count > ENDPOINTS_NUMBER){ printf("getEndpoints: Array out of bound :%d:", count); exit(-1); }
  			*(endpoint_desc_list + count) = config_desc->interface[j].altsetting[0].endpoint[k];
  			count++;
97064000   root   16u2 desc terminé...
79
80
  		}
  	}
bfb1101b   pifou   com 16u2 <-> PC c...
81
  	//if(count != ENDPOINTS_NUMBER){ printf("Wrong number of endpoints.\nIs this the good device ?\n"); exit(-1); }
97064000   root   16u2 desc terminé...
82
83
  }
  
dd5193ad   root   test USBSendData ...
84
  void start(libusb_context **context, libusb_device ***devices, libusb_device_handle **handle, struct libusb_config_descriptor **config_desc, struct libusb_endpoint_descriptor *endpoint_desc_list){
97064000   root   16u2 desc terminé...
85
  	// init
97064000   root   16u2 desc terminé...
86
  	ssize_t devices_count;
dd5193ad   root   test USBSendData ...
87
  	init(context, devices, &devices_count);
97064000   root   16u2 desc terminé...
88
89
  
  	// get arduino device
dd5193ad   root   test USBSendData ...
90
91
  	libusb_device *arduino = searchArduino(*devices, devices_count);
  	if(arduino == NULL){ printf("Arduino device not found\n"); exit(-1); }
97064000   root   16u2 desc terminé...
92
93
  
  	// open connection, use config, detach kernel and claim interfaces
dd5193ad   root   test USBSendData ...
94
  	openConnection(arduino, handle, config_desc);
97064000   root   16u2 desc terminé...
95
96
  
  	// get enpoints
dd5193ad   root   test USBSendData ...
97
98
  	getEndpoints(*config_desc, endpoint_desc_list);
  }
97064000   root   16u2 desc terminé...
99
  
dd5193ad   root   test USBSendData ...
100
  void stop(struct libusb_config_descriptor *config_desc, libusb_device_handle *handle, libusb_device **devices, libusb_context *context){
97064000   root   16u2 desc terminé...
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  	// release interfaces
  	for(int j=0; j<config_desc->bNumInterfaces; j++){
  		struct libusb_interface_descriptor interface_desc = config_desc->interface[j].altsetting[0];
  		int interface = interface_desc.bInterfaceNumber;
  
  		int status = libusb_release_interface(handle, interface);
  		if(status != 0){ perror("libusb_release_interface"); exit(-1); }
  	}
  	
  	// free config
  	libusb_free_config_descriptor(config_desc);
  
  	// close connection
  	libusb_close(handle);
  
  	// free device list
  	libusb_free_device_list(devices, 1);
  
  	// libusb exit
  	libusb_exit(context);
  }
  
dd5193ad   root   test USBSendData ...
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  int main(){
  	libusb_context *context;
  	libusb_device **devices;
  	libusb_device_handle *handle;
  	struct libusb_config_descriptor *config_desc;
  	struct libusb_endpoint_descriptor endpoint_desc_list[ENDPOINTS_NUMBER];
  
  	// start
  	printf("Starting...\n");
  	start(&context, &devices, &handle, &config_desc, endpoint_desc_list);
  	printf("Start complete\n");
  
  	// send data test
  	printf("Sending data...\n");
bfb1101b   pifou   com 16u2 <-> PC c...
137
138
139
140
141
142
  	unsigned char data_sent[MAX_DATA] = {0x52};
  	int data_length = 1;
  	int transferred_bytes;
  	int status = libusb_interrupt_transfer(handle, endpoint_desc_list[0].bEndpointAddress, data_sent, data_length, &transferred_bytes, TIMEOUT);
  	printf("Done with status :%d: (%s)\nTranferred bytes :%d:\nData sent :0x%x:\n", status, libusb_error_name(status), transferred_bytes, data_sent[0]);
  	if(status!=0){ perror("libusb_interrupt_transfer"); exit(-1); }
dd5193ad   root   test USBSendData ...
143
144
145
  
  	// recieve data test
  	printf("Recieving data...\n");
bfb1101b   pifou   com 16u2 <-> PC c...
146
147
148
149
150
  	unsigned char data_received[MAX_DATA];
  	data_length = 1;
  	status = libusb_interrupt_transfer(handle, endpoint_desc_list[2].bEndpointAddress, data_received, data_length, &transferred_bytes, TIMEOUT);
  	printf("Done with status :%d: (%s)\nTranferred bytes :%d:\nData receieved :0x%x:\n", status, libusb_error_name(status), transferred_bytes, data_received[0]);
  	if(status!=0){ perror("libusb_interrupt_transfer"); exit(-1); }
dd5193ad   root   test USBSendData ...
151
152
153
154
155
156
  	
  	// stop
  	printf("Stoping...\n");
  	stop(config_desc, handle, devices, context);
  	printf("Stop complete...\n");
  }
dd5193ad   root   test USBSendData ...