Commit dd5193ad369c374eb70621508de936fe48abcf6e

Authored by root
1 parent 97064000

test USBSendData ready for testing

tests/USBSendData/a.out deleted
No preview for this file type
tests/USBSendData/test_send_data.c
@@ -5,13 +5,8 @@ @@ -5,13 +5,8 @@
5 // arduino ID vendor and product 5 // arduino ID vendor and product
6 #define ID_VENDOR 0x2341 6 #define ID_VENDOR 0x2341
7 #define ID_PRODUCT 0x01 7 #define ID_PRODUCT 0x01
8 -  
9 -// gcc file.c -l usb-1.0  
10 -  
11 -typedef struct {  
12 - int interface_id;  
13 - int endpoint_id;  
14 -} Endpoint; 8 +#define ENDPOINTS_NUMBER 4
  9 +#define TIMEOUT 2000
15 10
16 void init(libusb_context **context, libusb_device ***devices, ssize_t *devices_count){ 11 void init(libusb_context **context, libusb_device ***devices, ssize_t *devices_count){
17 int status = libusb_init(context); 12 int status = libusb_init(context);
@@ -71,53 +66,37 @@ void openConnection(libusb_device *arduino, libusb_device_handle **handle, struc @@ -71,53 +66,37 @@ void openConnection(libusb_device *arduino, libusb_device_handle **handle, struc
71 } 66 }
72 } 67 }
73 68
74 -void getEndpoints(struct libusb_config_descriptor *config_desc, Endpoint* endpoint_list, int* endpoints_size){  
75 - // how many endpoints in total ?  
76 - *endpoints_size = 0;  
77 - for(int j=0; j<config_desc->bNumInterfaces; j++)  
78 - *endpoints_size += config_desc->interface[j].altsetting[0].bNumEndpoints;  
79 -  
80 - // list endpoints  
81 - endpoints[*endpoints_size]; 69 +void getEndpoints(struct libusb_config_descriptor *config_desc, struct libusb_endpoint_descriptor *endpoint_desc_list){
  70 + int count = 0;
82 // in interfaces 71 // in interfaces
83 for(int j=0; j<config_desc->bNumInterfaces; j++){ 72 for(int j=0; j<config_desc->bNumInterfaces; j++){
84 // find endpoints 73 // find endpoints
85 for(int k=0; k<config_desc->interface[j].altsetting[0].bNumEndpoints; k++){ 74 for(int k=0; k<config_desc->interface[j].altsetting[0].bNumEndpoints; k++){
86 - struct libusb_endpoint_descriptor endpoint_desc = config_desc->interface[j].altsetting[0].endpoint[k];  
87 -  
88 - //DEBUG list endpoints bEndpointAddress and bmAttributes  
89 - /*printf("Endpoint inter. %d, num. %d\n", j, k);  
90 - printf("bEndpointAddress = 0x%x\n", endpoint_desc.bEndpointAddress);  
91 - printf("bmAttributes = 0x%x\n", endpoint_desc.bmAttributes);*/ 75 + if(count > ENDPOINTS_NUMBER){ printf("getEndpoints: Array out of bound :%d:", count); exit(-1); }
  76 + *(endpoint_desc_list + count) = config_desc->interface[j].altsetting[0].endpoint[k];
  77 + count++;
92 } 78 }
93 } 79 }
94 - 80 + if(count != ENDPOINTS_NUMBER){ printf("Wrong number of endpoints.\nIs this the good device ?\n"); exit(-1); }
95 } 81 }
96 82
97 -int main(){ 83 +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){
98 // init 84 // init
99 - libusb_context *context;  
100 - libusb_device **devices;  
101 ssize_t devices_count; 85 ssize_t devices_count;
102 - init(&context, &devices, &devices_count); 86 + init(context, devices, &devices_count);
103 87
104 // get arduino device 88 // get arduino device
105 - libusb_device *arduino = searchArduino(devices, devices_count);  
106 - if(arduino == NULL){ printf("Arduino not found\n"); exit(-1); } 89 + libusb_device *arduino = searchArduino(*devices, devices_count);
  90 + if(arduino == NULL){ printf("Arduino device not found\n"); exit(-1); }
107 91
108 // open connection, use config, detach kernel and claim interfaces 92 // open connection, use config, detach kernel and claim interfaces
109 - libusb_device_handle *handle;  
110 - struct libusb_config_descriptor *config_desc;  
111 - openConnection(arduino, &handle, &config_desc); 93 + openConnection(arduino, handle, config_desc);
112 94
113 // get enpoints 95 // get enpoints
114 - Endpoint* endpoint_list;  
115 - int endpoint_list_size;  
116 - getEndpoints(config_desc);  
117 -//TODO ^^^^^^^^^^^^  
118 -  
119 - 96 + getEndpoints(*config_desc, endpoint_desc_list);
  97 +}
120 98
  99 +void stop(struct libusb_config_descriptor *config_desc, libusb_device_handle *handle, libusb_device **devices, libusb_context *context){
121 // release interfaces 100 // release interfaces
122 for(int j=0; j<config_desc->bNumInterfaces; j++){ 101 for(int j=0; j<config_desc->bNumInterfaces; j++){
123 struct libusb_interface_descriptor interface_desc = config_desc->interface[j].altsetting[0]; 102 struct libusb_interface_descriptor interface_desc = config_desc->interface[j].altsetting[0];
@@ -140,3 +119,40 @@ int main(){ @@ -140,3 +119,40 @@ int main(){
140 libusb_exit(context); 119 libusb_exit(context);
141 } 120 }
142 121
  122 +int main(){
  123 + libusb_context *context;
  124 + libusb_device **devices;
  125 + libusb_device_handle *handle;
  126 + struct libusb_config_descriptor *config_desc;
  127 + struct libusb_endpoint_descriptor endpoint_desc_list[ENDPOINTS_NUMBER];
  128 +
  129 + // start
  130 + printf("Starting...\n");
  131 + start(&context, &devices, &handle, &config_desc, endpoint_desc_list);
  132 + printf("Start complete\n");
  133 +
  134 + // send data test
  135 + printf("Sending data...\n");
  136 + unsigned char data_sent = 0x52;
  137 + int transferred_bytes = 0;
  138 + int status = libusb_interrupt_transfer(handle, endpoint_desc_list[0].bEndpointAddress, &data_sent, 1, &transferred_bytes, TIMEOUT);
  139 + printf("Done with status :%d:\nTranferred bytes :%d:\nData sent :0x%x:\n", status, transferred_bytes, data_sent);
  140 +
  141 + // recieve data test
  142 + printf("Recieving data...\n");
  143 + unsigned char data_recived = 0;
  144 + transferred_bytes = 0;
  145 + status = libusb_interrupt_transfer(handle, endpoint_desc_list[2].bEndpointAddress, &data_recived, 1, &transferred_bytes, TIMEOUT);
  146 + printf("Done with status :%d:\nTranferred bytes :%d:\nData recieved :0x%x:\n", status, transferred_bytes, data_recived);
  147 +
  148 + // stop
  149 + printf("Stoping...\n");
  150 + stop(config_desc, handle, devices, context);
  151 + printf("Stop complete...\n");
  152 +}
  153 +
  154 +
  155 +/*printf("Endpoint inter. %d, num. %d\n", j, k);
  156 +printf("bEndpointAddress = 0x%x\n", endpoint_desc.bEndpointAddress);
  157 +printf("bmAttributes = 0x%x\n", endpoint_desc.bmAttributes);*/
  158 +