diff --git a/Makefile b/Makefile index 6452778..78e89d8 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,9 @@ CC=gcc CC_FLAGS=-c -Wall -Werror -std=gnu99 -g -CC_LIBS=-pthread +CC_LIBS=-pthread -lusb-1.0 INCLUDES= -SOURCES=main.c printx.c ui.c serial.c nfc.c bus.c hvc.c heat.c pump.c +SOURCES=main.c printx.c ui.c serial.c nfc.c bus.c hvc.c heat.c pump.c usb.c OBJECTS=$(SOURCES:.c=.o) OUTPUT=tweekd diff --git a/main.c b/main.c index 80e5428..ca77e3b 100644 --- a/main.c +++ b/main.c @@ -25,7 +25,7 @@ int main(void) printx(DEBUG, MAIN, "Creating processes\n"); ret = pthread_create(&tUI, NULL, drawUI, NULL); printx(DEBUG, MAIN, "UI Started ID %08x ret %d\n", tUI, ret); - printx(INFO, MAIN, "Initializing NFC"); + printx(INFO, MAIN, "Initializing NFC\n"); if(!initNFC()) { printx(ERROR, MAIN, "Unable to start the NFC interface\n"); diff --git a/ui.c b/ui.c index 8029f41..a86e61a 100644 --- a/ui.c +++ b/ui.c @@ -7,6 +7,7 @@ #include #include "ui.h" #include "bus.h" +#include "usb.h" #define TIME_LENGTH 24 #define HEADER_TEXT_LENGTH 24 @@ -54,15 +55,13 @@ void setDebit(unsigned int d) void processScreen() { - + } void* drawUI(void* we) { while(!uiStop) { - //printf("\x1b[2J\x1b[1;1H"); - //displayUI(); processScreen(); usleep(SCREEN_TIME); } @@ -84,20 +83,7 @@ void initUILog() mainPid = getpid(); uid[0]='\0'; uidDate[0]='\0'; -} - -char* fillHeaderWithSpaces(char* buf, char* text) -{ - char tmpretheader[HEADER_TEXT_LENGTH+1]; - strcpy(tmpretheader, text); - while(strlen(tmpretheader) < HEADER_TEXT_LENGTH) - strcat(tmpretheader, " "); - - for(int i=0; i +#include +#include +#include +#include "usb.h" + +#define VENDOR_ID 0xfccf +#define PRODUCT_ID 0xa001 +#define ENDPOINT_OUT 0x01 +#define MAX_SIZE_OUT 64 + +libusb_device_handle* screenHandle; + +bool initUSB(void) +{ + int ret; + + ret = libusb_init(NULL); + if(ret != 0) + { + printf("Error while initializing libusb, return : %d\n", ret); + return false; + } + + struct libusb_config_descriptor* dConfig = NULL; + libusb_device** list = NULL; + ssize_t cnt = libusb_get_device_list(NULL, &list); + + struct libusb_device_descriptor dDevice; + + + printf("Starting lsusb things\n"); + + if(cnt < 0) + { + printf("Unable to get USB device list\n"); + return false; + } + + printf("%d devices detected\n", cnt); + printf("List of compatible devices detected\n"); + + for (int i = 0; i < cnt; i++) + { + libusb_device *device = list[i]; + ret = libusb_get_device_descriptor(device, &dDevice); + if(VENDOR_ID == dDevice.idVendor && PRODUCT_ID == dDevice.idProduct) + { + printf("Bus %03d Device %03d: ID %04x:%04x\n", + libusb_get_bus_number(device), + libusb_get_device_address(device), dDevice.idVendor, + dDevice.idProduct); + ret = libusb_open(device, &screenHandle); + if(ret != 0) + { + printf("Unable to open this device, error %d\n", ret); + return false; + } + } + } + + libusb_free_device_list(list, 1); + + ret = libusb_get_config_descriptor(libusb_get_device(screenHandle), 0, &dConfig); + if(ret!=0) + { + printf("Descriptor for this device unavailable\n"); + return false; + } + else + { + for(int j=0; jbNumInterfaces; j++) + { + if(libusb_kernel_driver_active(screenHandle, j) && (libusb_detach_kernel_driver(screenHandle, j) != 0)) + { + printf("Unable to detach this device\n"); + return false; + } + } + ret = libusb_set_configuration(screenHandle, dConfig->bConfigurationValue); + if(ret != 0) + { + printf("Configuration unavailable, error %d\n", ret); + return false; + } + for(int j=0; jbNumInterfaces; j++) + if(libusb_claim_interface(screenHandle, j) != 0) + { + printf("Device not claimed\n"); + return false; + } + else + printf("Interface %d ready\n", j); + } + + libusb_free_config_descriptor(dConfig); + + return true; +} + diff --git a/usb.h b/usb.h new file mode 100644 index 0000000..20e60fa --- /dev/null +++ b/usb.h @@ -0,0 +1,6 @@ +#ifndef __USB_H__ +#define __USB_H__ + +bool initUSB(); + +#endif -- libgit2 0.21.2