From b24d29803d55ee112aa239b5d51aac3bad675dcf Mon Sep 17 00:00:00 2001 From: Axel CHEMIN Date: Thu, 12 Nov 2020 17:45:55 +0100 Subject: [PATCH] Nettoyage du code, structure --- Makefile | 15 +++++++++++---- include/libusb_wrapper.h | 9 +++++++++ src/GetUsbInfos.c | 125 ----------------------------------------------------------------------------------------------------------------------------- src/libusb_wrapper.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 18 ++++++++++++++++++ 5 files changed, 180 insertions(+), 129 deletions(-) create mode 100644 include/libusb_wrapper.h delete mode 100644 src/GetUsbInfos.c create mode 100644 src/libusb_wrapper.c create mode 100644 src/main.c diff --git a/Makefile b/Makefile index 0a3f02f..a935378 100644 --- a/Makefile +++ b/Makefile @@ -24,13 +24,12 @@ all : main demostep -main : - echo "Here compile the main program" +main : libusb_wrapper ${SRC}/main.c + gcc -o ${OBJ}/$@.o -c ${SRC}/$@.c -I ${INCLUDE} ${CLIBS} ${CFLAGS} + gcc -o ${BIN}/$@.out ${OBJ}/main.o ${OBJ}/libusb_wrapper.o ${CFLAGS} ${CLIBS} demostep : STEP4.1 - - STEP4.1 : ${SRC}/GetUsbInfos.c bin gcc -o ${BIN}/STEP4.1.out ${SRC}/GetUsbInfos.c ${CLIBS} ${CFLAGS} @@ -40,6 +39,14 @@ STEP4.2 : ${SRC}/FourPointTwo.c bin STEP4.3 : ${SRC}/FourPointThree.c bin gcc -o ${BIN}/STEP4.3.out ${SRC}/FourPointThree.c ${CLIBS} ${CFLAGS} + +libs: libusb_wrapper + +libusb_wrapper: obj ${SRC}/libusb_wrapper.c ${INCLUDE}/libusb_wrapper.h + gcc -o ${OBJ}/$@.o -c ${SRC}/$@.c -I ${INCLUDE} ${CLIBS} ${CFLAGS} + + + obj : @-mkdir ${OBJ} diff --git a/include/libusb_wrapper.h b/include/libusb_wrapper.h new file mode 100644 index 0000000..4c7e9ee --- /dev/null +++ b/include/libusb_wrapper.h @@ -0,0 +1,9 @@ +#include + +void usbinit(); + +void usbclose(); + +void displayDevices(); + +void displayDeviceEndpoints(); diff --git a/src/GetUsbInfos.c b/src/GetUsbInfos.c deleted file mode 100644 index 43ddb13..0000000 --- a/src/GetUsbInfos.c +++ /dev/null @@ -1,125 +0,0 @@ -#include -#include -#include - -int main() { - - // initiate the usb thing - - libusb_context *context; - int statusInit = libusb_init(&context); - if (statusInit != 0) { - perror("libusb_init"); - exit(-1); - } - /* ... some code ... */ - - // actually enumerates all the usb and gives some infos abot the usb things - - libusb_device **list; - ssize_t count = libusb_get_device_list(context, &list); - if (count < 0) { - perror("libusb_get_device_list"); - exit(-1); - } - ssize_t i = 0; - for (i = 0; i < count; i++) { - libusb_device *device = list[i]; - struct libusb_device_descriptor desc; - - int status = libusb_get_device_descriptor(device, &desc); - if (status != 0) - continue; - - uint8_t bus = libusb_get_bus_number(device); - uint8_t address = libusb_get_device_address(device); - - printf("Device Found @ (Bus:Address) %d:%d\n", bus, address); - printf("Vendor ID 0x0%x\n", desc.idVendor); - printf("Product ID 0x0%x\n", desc.idProduct); - - // Ouverture du périphérique - libusb_device_handle *handle; - int statusDevice = libusb_open(device, &handle); - if (statusDevice != 0) { - perror("libusb_open"); - exit(-1); - } - - // Si le méchant noyau est passé avant vous : - /*int interface = 0; // TEST : FONCTIONNE MAIS CORRESPOND A QUOI? - if (libusb_kernel_driver_active(handle, interface)) { - int statusKDriver = libusb_detach_kernel_driver(handle, interface); - if (statusKDriver != 0) { - perror("libusb_detach_kernel_driver"); - exit(-1); - } - }*/ - - /*// lectures des configs? - int configuration = 0; // valueof("bConfigurationValue"); - int statusConfig = libusb_set_configuration(handle, configuration); - if (statusConfig != 0) { - perror("libusb_set_configuration"); /*exit(-1);*/ - /* } - */ - int MAXLEN_DESCRIPTOR_STRING = 200; - uint8_t desc_idx = 2; - uint16_t langid = 16; - unsigned char data[200]; - - // TEST 16 - for (desc_idx = 0; desc_idx < 16; desc_idx++) { - int statusAscii = libusb_get_string_descriptor_ascii( - handle, desc_idx, data, MAXLEN_DESCRIPTOR_STRING); - if (statusAscii == -9) // TEST seems to be LIBUSB_ERROR - break; - printf("Descriptor string : %s ; %i\n", data, statusAscii); - } - - // 4.2 configuration du périph usb - struct libusb_config_descriptor *config; - int statusFetchConfig = - libusb_get_active_config_descriptor(device, &config); - if (statusFetchConfig != 0) { - perror("libusb_get_active_config_descriptor"); - // exit(-1); - } - printf("Config.bConfigurationValue : %d\n", config->bConfigurationValue); - printf("Config/ bLength:%d;bDescriptorType:%d;bNumInterfaces:%d", - config->bLength, config->bDescriptorType, config->bNumInterfaces); - - // itération de l'interface - - for (int indexInterface = 0; indexInterface < config->bNumInterfaces; - indexInterface++) { - // struct libusb_interface * interface = - // &config->interface[indexInterface]; - printf("^%d", indexInterface); - printf("Alt%d", config->interface[indexInterface].num_altsetting); - } - - printf("\n"); - /* - //appropriation de l'interface - - int statusClaimInterface=libusb_claim_interface(handle,interface); - if(status!=0){ perror("libusb_claim_interface"); exit(-1); } - /* ... some code ... */ - /* - - - - status=libusb_release_interface(handle,interface); - if(status!=0){ perror("libusb_release_interface"); exit(-1); } - */ - - /* ... some ... code */ - libusb_close(handle); - } - - libusb_free_device_list(list, 1); - - libusb_exit(context); - return 0; -} diff --git a/src/libusb_wrapper.c b/src/libusb_wrapper.c new file mode 100644 index 0000000..6d216cf --- /dev/null +++ b/src/libusb_wrapper.c @@ -0,0 +1,142 @@ +#include +#include +#include + + +void usbinit(){ + printf("Hello usbinit\n"); +} + +void usbclose(){ + printf("Hello usbclose\n"); +} + +void displayDevices(){ + printf("Hello list of device"); +} + +void displayDeviceEndpoints(){ + printf("Hello endpoint\n"); +} + +/*int main() { + + // initiate the usb thing + + libusb_context *context; + int statusInit = libusb_init(&context); + if (statusInit != 0) { + perror("libusb_init"); + exit(-1); + } + /* ... some code ...*//* + + // actually enumerates all the usb and gives some infos abot the usb things + + libusb_device **list; + ssize_t count = libusb_get_device_list(context, &list); + if (count < 0) { + perror("libusb_get_device_list"); + exit(-1); + } + ssize_t i = 0; + for (i = 0; i < count; i++) { + libusb_device *device = list[i]; + struct libusb_device_descriptor desc; + + int status = libusb_get_device_descriptor(device, &desc); + if (status != 0) + continue; + + uint8_t bus = libusb_get_bus_number(device); + uint8_t address = libusb_get_device_address(device); + + printf("Device Found @ (Bus:Address) %d:%d\n", bus, address); + printf("Vendor ID 0x0%x\n", desc.idVendor); + printf("Product ID 0x0%x\n", desc.idProduct); + + // Ouverture du périphérique + libusb_device_handle *handle; + int statusDevice = libusb_open(device, &handle); + if (statusDevice != 0) { + perror("libusb_open"); + exit(-1); + } + + // Si le méchant noyau est passé avant vous : + /*int interface = 0; // TEST : FONCTIONNE MAIS CORRESPOND A QUOI? + if (libusb_kernel_driver_active(handle, interface)) { + int statusKDriver = libusb_detach_kernel_driver(handle, interface); + if (statusKDriver != 0) { + perror("libusb_detach_kernel_driver"); + exit(-1); + } + }*/ + + /*// lectures des configs? + int configuration = 0; // valueof("bConfigurationValue"); + int statusConfig = libusb_set_configuration(handle, configuration); + if (statusConfig != 0) { + perror("libusb_set_configuration"); /*exit(-1);*/ + /* } + *//* + int MAXLEN_DESCRIPTOR_STRING = 200; + uint8_t desc_idx = 2; + uint16_t langid = 16; + unsigned char data[200]; + + // TEST 16 + for (desc_idx = 0; desc_idx < 16; desc_idx++) { + int statusAscii = libusb_get_string_descriptor_ascii( + handle, desc_idx, data, MAXLEN_DESCRIPTOR_STRING); + if (statusAscii == -9) // TEST seems to be LIBUSB_ERROR + break; + printf("Descriptor string : %s ; %i\n", data, statusAscii); + } + + // 4.2 configuration du périph usb + struct libusb_config_descriptor *config; + int statusFetchConfig = + libusb_get_active_config_descriptor(device, &config); + if (statusFetchConfig != 0) { + perror("libusb_get_active_config_descriptor"); + // exit(-1); + } + printf("Config.bConfigurationValue : %d\n", config->bConfigurationValue); + printf("Config/ bLength:%d;bDescriptorType:%d;bNumInterfaces:%d", + config->bLength, config->bDescriptorType, config->bNumInterfaces); + + // itération de l'interface + + for (int indexInterface = 0; indexInterface < config->bNumInterfaces; + indexInterface++) { + // struct libusb_interface * interface = + // &config->interface[indexInterface]; + printf("^%d", indexInterface); + printf("Alt%d", config->interface[indexInterface].num_altsetting); + } + + printf("\n"); + /* + //appropriation de l'interface + + int statusClaimInterface=libusb_claim_interface(handle,interface); + if(status!=0){ perror("libusb_claim_interface"); exit(-1); } + /* ... some code ... */ + /* + + + + status=libusb_release_interface(handle,interface); + if(status!=0){ perror("libusb_release_interface"); exit(-1); } + */ + + /* ... some ... code */ + /*libusb_close(handle); + }*/ + +/* libusb_free_device_list(list, 1); + + libusb_exit(context); + return 0; +}*/ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..b19a7f8 --- /dev/null +++ b/src/main.c @@ -0,0 +1,18 @@ +#include +#include "libusb_wrapper.h" + +int main(){ + printf("Hello World :)\n"); + + usbinit(); + + //if 4.1 + displayDevices(); + + //if 4.2 + displayDeviceEndpoints(); + + usbclose(); + + return 0; +} -- libgit2 0.21.2