From dd5193ad369c374eb70621508de936fe48abcf6e Mon Sep 17 00:00:00 2001 From: root Date: Sat, 19 Jan 2019 18:05:32 +0100 Subject: [PATCH] test USBSendData ready for testing --- tests/USBSendData/a.out | Bin 13744 -> 0 bytes tests/USBSendData/test_send_data.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------- 2 files changed, 53 insertions(+), 37 deletions(-) delete mode 100755 tests/USBSendData/a.out diff --git a/tests/USBSendData/a.out b/tests/USBSendData/a.out deleted file mode 100755 index 29d9303..0000000 Binary files a/tests/USBSendData/a.out and /dev/null differ diff --git a/tests/USBSendData/test_send_data.c b/tests/USBSendData/test_send_data.c index e03343a..1a53891 100644 --- a/tests/USBSendData/test_send_data.c +++ b/tests/USBSendData/test_send_data.c @@ -5,13 +5,8 @@ // arduino ID vendor and product #define ID_VENDOR 0x2341 #define ID_PRODUCT 0x01 - -// gcc file.c -l usb-1.0 - -typedef struct { - int interface_id; - int endpoint_id; -} Endpoint; +#define ENDPOINTS_NUMBER 4 +#define TIMEOUT 2000 void init(libusb_context **context, libusb_device ***devices, ssize_t *devices_count){ int status = libusb_init(context); @@ -71,53 +66,37 @@ void openConnection(libusb_device *arduino, libusb_device_handle **handle, struc } } -void getEndpoints(struct libusb_config_descriptor *config_desc, Endpoint* endpoint_list, int* endpoints_size){ - // how many endpoints in total ? - *endpoints_size = 0; - for(int j=0; jbNumInterfaces; j++) - *endpoints_size += config_desc->interface[j].altsetting[0].bNumEndpoints; - - // list endpoints - endpoints[*endpoints_size]; +void getEndpoints(struct libusb_config_descriptor *config_desc, struct libusb_endpoint_descriptor *endpoint_desc_list){ + int count = 0; // in interfaces for(int j=0; jbNumInterfaces; j++){ // find endpoints for(int k=0; kinterface[j].altsetting[0].bNumEndpoints; k++){ - struct libusb_endpoint_descriptor endpoint_desc = config_desc->interface[j].altsetting[0].endpoint[k]; - - //DEBUG list endpoints bEndpointAddress and bmAttributes - /*printf("Endpoint inter. %d, num. %d\n", j, k); - printf("bEndpointAddress = 0x%x\n", endpoint_desc.bEndpointAddress); - printf("bmAttributes = 0x%x\n", endpoint_desc.bmAttributes);*/ + 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++; } } - + if(count != ENDPOINTS_NUMBER){ printf("Wrong number of endpoints.\nIs this the good device ?\n"); exit(-1); } } -int main(){ +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){ // init - libusb_context *context; - libusb_device **devices; ssize_t devices_count; - init(&context, &devices, &devices_count); + init(context, devices, &devices_count); // get arduino device - libusb_device *arduino = searchArduino(devices, devices_count); - if(arduino == NULL){ printf("Arduino not found\n"); exit(-1); } + libusb_device *arduino = searchArduino(*devices, devices_count); + if(arduino == NULL){ printf("Arduino device not found\n"); exit(-1); } // open connection, use config, detach kernel and claim interfaces - libusb_device_handle *handle; - struct libusb_config_descriptor *config_desc; - openConnection(arduino, &handle, &config_desc); + openConnection(arduino, handle, config_desc); // get enpoints - Endpoint* endpoint_list; - int endpoint_list_size; - getEndpoints(config_desc); -//TODO ^^^^^^^^^^^^ - - + getEndpoints(*config_desc, endpoint_desc_list); +} +void stop(struct libusb_config_descriptor *config_desc, libusb_device_handle *handle, libusb_device **devices, libusb_context *context){ // release interfaces for(int j=0; jbNumInterfaces; j++){ struct libusb_interface_descriptor interface_desc = config_desc->interface[j].altsetting[0]; @@ -140,3 +119,40 @@ int main(){ libusb_exit(context); } +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"); + unsigned char data_sent = 0x52; + int transferred_bytes = 0; + int status = libusb_interrupt_transfer(handle, endpoint_desc_list[0].bEndpointAddress, &data_sent, 1, &transferred_bytes, TIMEOUT); + printf("Done with status :%d:\nTranferred bytes :%d:\nData sent :0x%x:\n", status, transferred_bytes, data_sent); + + // recieve data test + printf("Recieving data...\n"); + unsigned char data_recived = 0; + transferred_bytes = 0; + status = libusb_interrupt_transfer(handle, endpoint_desc_list[2].bEndpointAddress, &data_recived, 1, &transferred_bytes, TIMEOUT); + printf("Done with status :%d:\nTranferred bytes :%d:\nData recieved :0x%x:\n", status, transferred_bytes, data_recived); + + // stop + printf("Stoping...\n"); + stop(config_desc, handle, devices, context); + printf("Stop complete...\n"); +} + + +/*printf("Endpoint inter. %d, num. %d\n", j, k); +printf("bEndpointAddress = 0x%x\n", endpoint_desc.bEndpointAddress); +printf("bmAttributes = 0x%x\n", endpoint_desc.bmAttributes);*/ + -- libgit2 0.21.2