Commit d4d5bb6dd9aab8aa3995380f51d21c258ced71ff

Authored by henyxia
1 parent 3bdc9daf

Bus working

tweekd can now exit cleanly
@@ -3,7 +3,7 @@ CC_FLAGS=-c -Wall -Werror -std=gnu99 -g @@ -3,7 +3,7 @@ CC_FLAGS=-c -Wall -Werror -std=gnu99 -g
3 CC_LIBS=-pthread 3 CC_LIBS=-pthread
4 INCLUDES= 4 INCLUDES=
5 5
6 -SOURCES=main.c printx.c ui.c nfc.c 6 +SOURCES=main.c printx.c ui.c nfc.c bus.c
7 OBJECTS=$(SOURCES:.c=.o) 7 OBJECTS=$(SOURCES:.c=.o)
8 OUTPUT=tweekd 8 OUTPUT=tweekd
9 9
bus.c 0 → 100644
@@ -0,0 +1,82 @@ @@ -0,0 +1,82 @@
  1 +#include <stdio.h>
  2 +#include <stdbool.h>
  3 +#include <unistd.h>
  4 +#include <sys/types.h>
  5 +#include <sys/stat.h>
  6 +#include <fcntl.h>
  7 +#include <errno.h>
  8 +#include <string.h>
  9 +#include "printx.h"
  10 +#include "ui.h"
  11 +#include "nfc.h"
  12 +
  13 +#define CMD_MAX 70
  14 +
  15 +int bus;
  16 +bool busFree = true;
  17 +bool busStop = false;
  18 +
  19 +void stopBus()
  20 +{
  21 + busStop = true;
  22 +}
  23 +
  24 +void processCmd(char* buffer)
  25 +{
  26 + buffer[strlen(buffer)-1]='\0';
  27 + if(strcmp(buffer, "quit") == 0 || strcmp(buffer, "exit") == 0)
  28 + {
  29 + printx(INFO, BUS, "Exit request receved, processing ...\n");
  30 + stopNFC();
  31 + //stopHVC();
  32 + stopBus();
  33 + stopUI();
  34 + }
  35 + //else
  36 + //printx(DEBUG, BUS, "STRLEN : %d and strcmp ret %d", strlen(buffer), strcmp(buffer, "quit"));
  37 +}
  38 +
  39 +void* processBus(void* we)
  40 +{
  41 + bus = open("bus.log", O_RDWR);
  42 + char buffer[CMD_MAX];
  43 + printx(DEBUG, BUS, "Waiting for events\n");
  44 + while(!busStop)
  45 + {
  46 + while(busFree);
  47 + while(!busFree);
  48 + printx(DEBUG, BUS, "Event receved !\n");
  49 + busFree = false;
  50 + lseek(bus, 0, SEEK_SET);
  51 + printx(DEBUG, BUS, "Data read %d\n", read(bus, buffer, CMD_MAX));
  52 + ftruncate(bus, 0);
  53 + sync();
  54 + processCmd(buffer);
  55 + busFree = true;
  56 + printx(DEBUG, BUS, buffer);
  57 + }
  58 + return NULL;
  59 +}
  60 +
  61 +bool initBus()
  62 +{
  63 + bus = creat("bus.log", 0666);
  64 + if(bus == -1)
  65 + {
  66 + printf("Unable to open the bus file\n");
  67 + return false;
  68 + }
  69 +
  70 + return true;
  71 +}
  72 +
  73 +void sendToBus(char* cmd)
  74 +{
  75 + while(!busFree);
  76 + busFree = false;
  77 + ftruncate(bus, 0);
  78 + ftruncate(bus, CMD_MAX);
  79 + lseek(bus, 0, SEEK_SET);
  80 + dprintf(bus, "%s\n", cmd);
  81 + busFree = true;
  82 +}
bus.h 0 → 100644
@@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
  1 +#ifndef __BUS_H__
  2 +#define __BUS_H__
  3 +
  4 +bool initBus();
  5 +void* processBus(void*);
  6 +void sendToBus(char*);
  7 +
  8 +#endif
@@ -3,13 +3,14 @@ @@ -3,13 +3,14 @@
3 #include "printx.h" 3 #include "printx.h"
4 #include "ui.h" 4 #include "ui.h"
5 #include "nfc.h" 5 #include "nfc.h"
  6 +#include "bus.h"
6 7
7 #include <unistd.h> 8 #include <unistd.h>
8 9
9 int main(void) 10 int main(void)
10 { 11 {
11 int ret; 12 int ret;
12 - pthread_t tUI, tNFC;//tHVC 13 + pthread_t tUI, tNFC, tBUS;//tHVC
13 initUILog(); 14 initUILog();
14 if(!initLog()) 15 if(!initLog())
15 { 16 {
@@ -28,6 +29,12 @@ int main(void) @@ -28,6 +29,12 @@ int main(void)
28 return 2; 29 return 2;
29 } 30 }
30 pthread_create(&tNFC, NULL, processNFC, NULL); 31 pthread_create(&tNFC, NULL, processNFC, NULL);
  32 + if(!initBus())
  33 + {
  34 + printx(ERROR, MAIN, "Unable to start the BUS interface\n");
  35 + return 3;
  36 + }
  37 + pthread_create(&tBUS, NULL, processBus, NULL);
31 pthread_join(tUI, NULL); 38 pthread_join(tUI, NULL);
32 closeLog(); 39 closeLog();
33 return 0; 40 return 0;
@@ -27,7 +27,7 @@ @@ -27,7 +27,7 @@
27 27
28 // Globals 28 // Globals
29 int serial_fd = -1; 29 int serial_fd = -1;
30 -bool stop = false; 30 +bool nfcStop = false;
31 struct termios saveterm; 31 struct termios saveterm;
32 32
33 bool init_serial(char* device, int speed) 33 bool init_serial(char* device, int speed)
@@ -61,6 +61,12 @@ void close_serial() @@ -61,6 +61,12 @@ void close_serial()
61 close(serial_fd); 61 close(serial_fd);
62 } 62 }
63 63
  64 +void stopNFC()
  65 +{
  66 + close_serial();
  67 + nfcStop = true;
  68 +}
  69 +
64 unsigned char getData() 70 unsigned char getData()
65 { 71 {
66 unsigned char buf; 72 unsigned char buf;
@@ -150,7 +156,7 @@ void* processNFC(void* we) @@ -150,7 +156,7 @@ void* processNFC(void* we)
150 char myTag[NFC_TAG_LENGTH]; 156 char myTag[NFC_TAG_LENGTH];
151 char buffer[NFC_TAG_LENGTH]; 157 char buffer[NFC_TAG_LENGTH];
152 158
153 - while(!stop) 159 + while(!nfcStop)
154 { 160 {
155 printx(DEBUG, NFC, "Scanning for tags\n"); 161 printx(DEBUG, NFC, "Scanning for tags\n");
156 if(isTagPresent(buffer)) 162 if(isTagPresent(buffer))
@@ -5,5 +5,6 @@ @@ -5,5 +5,6 @@
5 5
6 bool initNFC(); 6 bool initNFC();
7 void* processNFC(); 7 void* processNFC();
  8 +void stopNFC();
8 9
9 #endif 10 #endif
@@ -3,7 +3,10 @@ @@ -3,7 +3,10 @@
3 #include <unistd.h> 3 #include <unistd.h>
4 #include <string.h> 4 #include <string.h>
5 #include <time.h> 5 #include <time.h>
  6 +#include <stdbool.h>
  7 +#include <termios.h>
6 #include "ui.h" 8 #include "ui.h"
  9 +#include "bus.h"
7 10
8 #define TIME_LENGTH 24 11 #define TIME_LENGTH 24
9 #define HEADER_TEXT_LENGTH 24 12 #define HEADER_TEXT_LENGTH 24
@@ -17,11 +20,23 @@ char uilog[LOG_LINES][LOG_LENGTH]; @@ -17,11 +20,23 @@ char uilog[LOG_LINES][LOG_LENGTH];
17 char started[TIME_LENGTH]; 20 char started[TIME_LENGTH];
18 char uid[HEADER_TEXT_LENGTH]; 21 char uid[HEADER_TEXT_LENGTH];
19 char uidDate[HEADER_TEXT_LENGTH]; 22 char uidDate[HEADER_TEXT_LENGTH];
  23 +char cmd[LOG_LENGTH];
20 pid_t mainPid; 24 pid_t mainPid;
  25 +bool heat = false;
  26 +bool pump = false;
  27 +unsigned int temp = 1;
  28 +unsigned int debit = 1;
  29 +struct termios old={0};
  30 +bool uiStop = false;
  31 +
  32 +void stopUI()
  33 +{
  34 + uiStop = true;
  35 +}
21 36
22 void* drawUI(void* we) 37 void* drawUI(void* we)
23 { 38 {
24 - while(1) 39 + while(!uiStop)
25 { 40 {
26 printf("\x1b[2J\x1b[1;1H"); 41 printf("\x1b[2J\x1b[1;1H");
27 displayUI(); 42 displayUI();
@@ -31,6 +46,17 @@ void* drawUI(void* we) @@ -31,6 +46,17 @@ void* drawUI(void* we)
31 return NULL; 46 return NULL;
32 } 47 }
33 48
  49 +void setDFG(void)
  50 +{
  51 + if(tcgetattr(0, &old)<0)
  52 + perror("tcsetattr()");
  53 + old.c_lflag&=~ICANON;
  54 + old.c_cc[VMIN]=1;
  55 + old.c_cc[VTIME]=0;
  56 + if(tcsetattr(0, TCSANOW, &old)<0)
  57 + perror("tcsetattr ICANON");
  58 +}
  59 +
34 void setTagName(char* tag) 60 void setTagName(char* tag)
35 { 61 {
36 time_t now = time(NULL); 62 time_t now = time(NULL);
@@ -40,6 +66,8 @@ void setTagName(char* tag) @@ -40,6 +66,8 @@ void setTagName(char* tag)
40 66
41 void initUILog() 67 void initUILog()
42 { 68 {
  69 + setDFG();
  70 + cmd[0]='\0';
43 started[0]='\0'; 71 started[0]='\0';
44 mainPid = getpid(); 72 mainPid = getpid();
45 uid[0]='\0'; 73 uid[0]='\0';
@@ -80,15 +108,36 @@ char* fillLogWithSpaces(char* buf, char* text) @@ -80,15 +108,36 @@ char* fillLogWithSpaces(char* buf, char* text)
80 return buf; 108 return buf;
81 } 109 }
82 110
  111 +char* fillStatusWithSpaces(char* buf, char* text)
  112 +{
  113 + strncpy(buf, text, LOG_LENGTH);
  114 + strncat(buf, SPACES, LOG_LENGTH-strlen(buf)-1-7);
  115 +
  116 + return buf;
  117 +}
83 118
84 void setStartTime(char* sT) 119 void setStartTime(char* sT)
85 { 120 {
86 strcpy(started, sT); 121 strcpy(started, sT);
87 } 122 }
88 123
  124 +char getch()
  125 +{
  126 + char buf=0;
  127 + if(read(0,&buf,1)<0)
  128 + perror("read()");
  129 + return buf;
  130 +}
  131 +
89 void displayUI() 132 void displayUI()
90 { 133 {
91 char buffer[LOG_LENGTH]; 134 char buffer[LOG_LENGTH];
  135 + struct timeval tv = {0, 5000};
  136 + char bufferO[4];
  137 + fd_set rdfs;
  138 + FD_ZERO(&rdfs);
  139 + FD_SET(STDIN_FILENO, &rdfs);
  140 + select(1, &rdfs, NULL, NULL, &tv);
92 //header 141 //header
93 printf("\u250F\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2533\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2533\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2513\n"); 142 printf("\u250F\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2533\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2533\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2513\n");
94 printf("\u2503 %s \u2503 T H E \u2503", fillHeaderWithSpaces(buffer, "Started since")); 143 printf("\u2503 %s \u2503 T H E \u2503", fillHeaderWithSpaces(buffer, "Started since"));
@@ -100,7 +149,10 @@ void displayUI() @@ -100,7 +149,10 @@ void displayUI()
100 printf("\u2503 %s \u2503 P R O J E C T \u2503 ", fillHeaderWithSpaces(buffer, buffer)); 149 printf("\u2503 %s \u2503 P R O J E C T \u2503 ", fillHeaderWithSpaces(buffer, buffer));
101 sprintf(buffer, "UID : %s", strlen(uid) > 0 ? uid : "Nope"); 150 sprintf(buffer, "UID : %s", strlen(uid) > 0 ? uid : "Nope");
102 printf("%s \u2503\n", fillHeaderWithSpaces(buffer, buffer)); 151 printf("%s \u2503\n", fillHeaderWithSpaces(buffer, buffer));
103 - printf("\u2522\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u253B\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u253B\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u252B\n"); 152 + printf("\u2523\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u253B\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u253B\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u252B\n");
  153 + sprintf(buffer, "\u2503 HEAT[%s] %02d\u2103 PUMP[%s] %04d", heat ? "ON " : "OFF", temp, pump ? "ON " : "OFF", debit);
  154 + printf("%s \u2503\n", fillStatusWithSpaces(buffer, buffer));
  155 + printf("\u2523\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u252B\n");
104 //body 156 //body
105 int i=0; 157 int i=0;
106 while(i<LOG_LINES) 158 while(i<LOG_LINES)
@@ -108,5 +160,45 @@ void displayUI() @@ -108,5 +160,45 @@ void displayUI()
108 printf("\u2503 %s \u2503\n", fillLogWithSpaces(buffer, uilog[LOG_LINES-i-1])); 160 printf("\u2503 %s \u2503\n", fillLogWithSpaces(buffer, uilog[LOG_LINES-i-1]));
109 i++; 161 i++;
110 } 162 }
  163 + printf("\u2523\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u252B\n");
  164 + //cmd
  165 + if(FD_ISSET(STDIN_FILENO, &rdfs))
  166 + {
  167 + bufferO[0] = getch();
  168 + if(bufferO[0] >= 65 && bufferO[0] <= 90 && bufferO[1] == '\0')
  169 + {
  170 + sprintf(buffer, "%c", bufferO[0] + 32);
  171 + strcat(cmd, buffer);
  172 + }
  173 + else if(bufferO[0] >= 97 && bufferO[0] <= 122 && bufferO[1] == '\0')
  174 + {
  175 + sprintf(buffer, "%c", bufferO[0]);
  176 + strcat(cmd, buffer);
  177 + }
  178 + else if(bufferO[0] == 32)
  179 + {
  180 + sprintf(buffer, "%c", bufferO[0]);
  181 + strcat(cmd, buffer);
  182 + }
  183 + else if(bufferO[0] == 10)
  184 + {
  185 + sendToBus(cmd);
  186 + cmd[0]='\0';
  187 + }
  188 + else if(bufferO[0] == 127)
  189 + {
  190 + if(strlen(cmd)>0)
  191 + cmd[strlen(cmd)-1]='\0';
  192 + }
  193 + else
  194 + {
  195 + //sprintf(buffer, "Unrecognized %02x", bufferO[0]);
  196 + //strcat(cmd, buffer);
  197 + }
  198 + }
  199 + sprintf(buffer, "\u2503> %s", cmd);
  200 + printf("%s\u2503\n", fillStatusWithSpaces(buffer, buffer));
111 printf("\u2517\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u251B\n"); 201 printf("\u2517\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u251B\n");
  202 + printf("\x1b[2A\x1b[%dC", strlen(cmd) + 3);
  203 + fflush(stdout);
112 } 204 }
@@ -7,5 +7,6 @@ void addToLog(char*); @@ -7,5 +7,6 @@ void addToLog(char*);
7 void displayUI(); 7 void displayUI();
8 void setStartTime(char*); 8 void setStartTime(char*);
9 void setTagName(char*); 9 void setTagName(char*);
  10 +void stopUI();
10 11
11 #endif 12 #endif