#include #include #include // arduino ID vendor and product #define ID_VENDOR 0x2341 #define ID_PRODUCT 0x01 #define ENDPOINTS_NUMBER 4 #define TIMEOUT 2000 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; ibConfigurationValue; // 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); } } } 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++){ 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); } } 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 ssize_t devices_count; init(context, devices, &devices_count); // get arduino device 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 openConnection(arduino, handle, config_desc); // get enpoints 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]; 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); } 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);*/