Commit 9c0663555d2261d0b767716e0e934865a429f844

Authored by henyxia
1 parent ac55328a

USB Initialized

Showing 5 changed files with 112 additions and 20 deletions   Show diff stats
1 CC=gcc 1 CC=gcc
2 CC_FLAGS=-c -Wall -Werror -std=gnu99 -g 2 CC_FLAGS=-c -Wall -Werror -std=gnu99 -g
3 -CC_LIBS=-pthread 3 +CC_LIBS=-pthread -lusb-1.0
4 INCLUDES= 4 INCLUDES=
5 5
6 -SOURCES=main.c printx.c ui.c serial.c nfc.c bus.c hvc.c heat.c pump.c 6 +SOURCES=main.c printx.c ui.c serial.c nfc.c bus.c hvc.c heat.c pump.c usb.c
7 OBJECTS=$(SOURCES:.c=.o) 7 OBJECTS=$(SOURCES:.c=.o)
8 OUTPUT=tweekd 8 OUTPUT=tweekd
9 9
@@ -25,7 +25,7 @@ int main(void) @@ -25,7 +25,7 @@ int main(void)
25 printx(DEBUG, MAIN, "Creating processes\n"); 25 printx(DEBUG, MAIN, "Creating processes\n");
26 ret = pthread_create(&tUI, NULL, drawUI, NULL); 26 ret = pthread_create(&tUI, NULL, drawUI, NULL);
27 printx(DEBUG, MAIN, "UI Started ID %08x ret %d\n", tUI, ret); 27 printx(DEBUG, MAIN, "UI Started ID %08x ret %d\n", tUI, ret);
28 - printx(INFO, MAIN, "Initializing NFC"); 28 + printx(INFO, MAIN, "Initializing NFC\n");
29 if(!initNFC()) 29 if(!initNFC())
30 { 30 {
31 printx(ERROR, MAIN, "Unable to start the NFC interface\n"); 31 printx(ERROR, MAIN, "Unable to start the NFC interface\n");
@@ -7,6 +7,7 @@ @@ -7,6 +7,7 @@
7 #include <termios.h> 7 #include <termios.h>
8 #include "ui.h" 8 #include "ui.h"
9 #include "bus.h" 9 #include "bus.h"
  10 +#include "usb.h"
10 11
11 #define TIME_LENGTH 24 12 #define TIME_LENGTH 24
12 #define HEADER_TEXT_LENGTH 24 13 #define HEADER_TEXT_LENGTH 24
@@ -54,15 +55,13 @@ void setDebit(unsigned int d) @@ -54,15 +55,13 @@ void setDebit(unsigned int d)
54 55
55 void processScreen() 56 void processScreen()
56 { 57 {
57 - 58 +
58 } 59 }
59 60
60 void* drawUI(void* we) 61 void* drawUI(void* we)
61 { 62 {
62 while(!uiStop) 63 while(!uiStop)
63 { 64 {
64 - //printf("\x1b[2J\x1b[1;1H");  
65 - //displayUI();  
66 processScreen(); 65 processScreen();
67 usleep(SCREEN_TIME); 66 usleep(SCREEN_TIME);
68 } 67 }
@@ -84,20 +83,7 @@ void initUILog() @@ -84,20 +83,7 @@ void initUILog()
84 mainPid = getpid(); 83 mainPid = getpid();
85 uid[0]='\0'; 84 uid[0]='\0';
86 uidDate[0]='\0'; 85 uidDate[0]='\0';
87 -}  
88 -  
89 -char* fillHeaderWithSpaces(char* buf, char* text)  
90 -{  
91 - char tmpretheader[HEADER_TEXT_LENGTH+1];  
92 - strcpy(tmpretheader, text);  
93 - while(strlen(tmpretheader) < HEADER_TEXT_LENGTH)  
94 - strcat(tmpretheader, " ");  
95 -  
96 - for(int i=0; i<strlen(tmpretheader); i++)  
97 - buf[i] = tmpretheader[i];  
98 - buf[strlen(tmpretheader)] = '\0';  
99 -  
100 - return buf; 86 + initUSB();
101 } 87 }
102 88
103 void setStartTime(char* sT) 89 void setStartTime(char* sT)
usb.c 0 → 100644
@@ -0,0 +1,100 @@ @@ -0,0 +1,100 @@
  1 +#include <stdio.h>
  2 +#include <stdlib.h>
  3 +#include <stdbool.h>
  4 +#include <libusb-1.0/libusb.h>
  5 +#include "usb.h"
  6 +
  7 +#define VENDOR_ID 0xfccf
  8 +#define PRODUCT_ID 0xa001
  9 +#define ENDPOINT_OUT 0x01
  10 +#define MAX_SIZE_OUT 64
  11 +
  12 +libusb_device_handle* screenHandle;
  13 +
  14 +bool initUSB(void)
  15 +{
  16 + int ret;
  17 +
  18 + ret = libusb_init(NULL);
  19 + if(ret != 0)
  20 + {
  21 + printf("Error while initializing libusb, return : %d\n", ret);
  22 + return false;
  23 + }
  24 +
  25 + struct libusb_config_descriptor* dConfig = NULL;
  26 + libusb_device** list = NULL;
  27 + ssize_t cnt = libusb_get_device_list(NULL, &list);
  28 +
  29 + struct libusb_device_descriptor dDevice;
  30 +
  31 +
  32 + printf("Starting lsusb things\n");
  33 +
  34 + if(cnt < 0)
  35 + {
  36 + printf("Unable to get USB device list\n");
  37 + return false;
  38 + }
  39 +
  40 + printf("%d devices detected\n", cnt);
  41 + printf("List of compatible devices detected\n");
  42 +
  43 + for (int i = 0; i < cnt; i++)
  44 + {
  45 + libusb_device *device = list[i];
  46 + ret = libusb_get_device_descriptor(device, &dDevice);
  47 + if(VENDOR_ID == dDevice.idVendor && PRODUCT_ID == dDevice.idProduct)
  48 + {
  49 + printf("Bus %03d Device %03d: ID %04x:%04x\n",
  50 + libusb_get_bus_number(device),
  51 + libusb_get_device_address(device), dDevice.idVendor,
  52 + dDevice.idProduct);
  53 + ret = libusb_open(device, &screenHandle);
  54 + if(ret != 0)
  55 + {
  56 + printf("Unable to open this device, error %d\n", ret);
  57 + return false;
  58 + }
  59 + }
  60 + }
  61 +
  62 + libusb_free_device_list(list, 1);
  63 +
  64 + ret = libusb_get_config_descriptor(libusb_get_device(screenHandle), 0, &dConfig);
  65 + if(ret!=0)
  66 + {
  67 + printf("Descriptor for this device unavailable\n");
  68 + return false;
  69 + }
  70 + else
  71 + {
  72 + for(int j=0; j<dConfig->bNumInterfaces; j++)
  73 + {
  74 + if(libusb_kernel_driver_active(screenHandle, j) && (libusb_detach_kernel_driver(screenHandle, j) != 0))
  75 + {
  76 + printf("Unable to detach this device\n");
  77 + return false;
  78 + }
  79 + }
  80 + ret = libusb_set_configuration(screenHandle, dConfig->bConfigurationValue);
  81 + if(ret != 0)
  82 + {
  83 + printf("Configuration unavailable, error %d\n", ret);
  84 + return false;
  85 + }
  86 + for(int j=0; j<dConfig->bNumInterfaces; j++)
  87 + if(libusb_claim_interface(screenHandle, j) != 0)
  88 + {
  89 + printf("Device not claimed\n");
  90 + return false;
  91 + }
  92 + else
  93 + printf("Interface %d ready\n", j);
  94 + }
  95 +
  96 + libusb_free_config_descriptor(dConfig);
  97 +
  98 + return true;
  99 +}
  100 +
usb.h 0 → 100644
@@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
  1 +#ifndef __USB_H__
  2 +#define __USB_H__
  3 +
  4 +bool initUSB();
  5 +
  6 +#endif