Commit 3bdc9dafd99be9061898dd1775057e4f0bea4b08

Authored by henyxia
1 parent 297a72df

NFC Added

UI refreshed
printx fixed
Makefile
... ... @@ -3,7 +3,7 @@ CC_FLAGS=-c -Wall -Werror -std=gnu99 -g
3 3 CC_LIBS=-pthread
4 4 INCLUDES=
5 5  
6   -SOURCES=main.c printx.c ui.c
  6 +SOURCES=main.c printx.c ui.c nfc.c
7 7 OBJECTS=$(SOURCES:.c=.o)
8 8 OUTPUT=tweekd
9 9  
... ...
... ... @@ -2,13 +2,14 @@
2 2 #include <pthread.h>
3 3 #include "printx.h"
4 4 #include "ui.h"
  5 +#include "nfc.h"
5 6  
6 7 #include <unistd.h>
7 8  
8 9 int main(void)
9 10 {
10 11 int ret;
11   - pthread_t tUI;//, tNFC;//tHVC
  12 + pthread_t tUI, tNFC;//tHVC
12 13 initUILog();
13 14 if(!initLog())
14 15 {
... ... @@ -21,7 +22,12 @@ int main(void)
21 22 ret = pthread_create(&tUI, NULL, drawUI, NULL);
22 23 printx(DEBUG, MAIN, "UI Started ID %08x ret %d\n", tUI, ret);
23 24 printx(INFO, MAIN, "Initializing NFC");
24   - //pthread_create(&tNFC, NULL,
  25 + if(!initNFC())
  26 + {
  27 + printx(ERROR, MAIN, "Unable to start the NFC interface\n");
  28 + return 2;
  29 + }
  30 + pthread_create(&tNFC, NULL, processNFC, NULL);
25 31 pthread_join(tUI, NULL);
26 32 closeLog();
27 33 return 0;
... ...
... ... @@ -0,0 +1,167 @@
  1 +#include <stdio.h>
  2 +#include <stdlib.h>
  3 +#include <termios.h>
  4 +#include <unistd.h>
  5 +#include <errno.h>
  6 +#include <sys/types.h>
  7 +#include <sys/stat.h>
  8 +#include <fcntl.h>
  9 +#include <strings.h>
  10 +#include <inttypes.h>
  11 +#include "ui.h"
  12 +#include "nfc.h"
  13 +#include "printx.h"
  14 +
  15 +#define DEVICE1 "/dev/ttyACM0"
  16 +#define SPEED B19200
  17 +#define NFC_POLLING_TIME 500000
  18 +#define WAIT_BEFORE_RETRY 50000
  19 +#define NFC_TAG_LENGTH 9
  20 +#define GET_DEVICE_MODEL 'A'
  21 +#define DEVICE_MODEL_NFC 'B'
  22 +#define GET_NFC_STATUS 'C'
  23 +#define NO_TAG_DETECTED 'D'
  24 +#define PROFESSOR_TAG 'E'
  25 +#define STUDENT_TAG 'F'
  26 +#define GET_TAG_DETAILS 'H'
  27 +
  28 +// Globals
  29 +int serial_fd = -1;
  30 +bool stop = false;
  31 +struct termios saveterm;
  32 +
  33 +bool init_serial(char* device, int speed)
  34 +{
  35 + struct termios new;
  36 +
  37 + int fd=open(device,O_RDWR|O_NOCTTY|O_NONBLOCK);
  38 +
  39 + if(fd<0)
  40 + return false;
  41 +
  42 + tcgetattr(fd,&saveterm); // save current port settings
  43 + bzero(&new,sizeof(new));
  44 + new.c_cflag=CLOCAL|CREAD|speed|CS8;
  45 + new.c_iflag=0;
  46 + new.c_oflag=0;
  47 + new.c_lflag=0; // set input mode (non-canonical, no echo,...)
  48 + new.c_cc[VTIME]=0; // inter-character timer unused
  49 + new.c_cc[VMIN]=1; // blocking read until 1 char received
  50 + tcflush(fd, TCIFLUSH);
  51 + tcsetattr(fd,TCSANOW,&new);
  52 +
  53 + serial_fd = fd;
  54 + sleep(2);
  55 + return true;
  56 +}
  57 +
  58 +void close_serial()
  59 +{
  60 + tcsetattr(serial_fd,TCSANOW,&saveterm);
  61 + close(serial_fd);
  62 +}
  63 +
  64 +unsigned char getData()
  65 +{
  66 + unsigned char buf;
  67 + while(read(serial_fd, &buf, 1) != 1)
  68 + usleep(WAIT_BEFORE_RETRY);
  69 + return buf;
  70 +}
  71 +
  72 +void sendData(unsigned char data)
  73 +{
  74 + while(write(serial_fd, &data, 1) != 1)
  75 + usleep(WAIT_BEFORE_RETRY);
  76 +}
  77 +
  78 +bool initNFC()
  79 +{
  80 + unsigned char data;
  81 + printx(DEBUG, NFC, "Connecting to interface 1\n");
  82 + if(init_serial(DEVICE1, SPEED))
  83 + {
  84 + printx(DEBUG, NFC, "Connected to an interface\n");
  85 + sendData(GET_DEVICE_MODEL);
  86 + printx(DEBUG, NFC, "Sended identification request %02x\n", GET_DEVICE_MODEL);
  87 + data = getData();
  88 + if(data == DEVICE_MODEL_NFC)
  89 + printx(INFO, NFC, "NFC Arduino connected\n");
  90 + else
  91 + printx(DEBUG, NFC, "NFC Arduino not found, response was %02x\n", data);
  92 + }
  93 + else
  94 + {
  95 + printx(WARNING, NFC, "Unable to connect to such interface\n");
  96 + return false;
  97 + }
  98 +
  99 + return true;
  100 +}
  101 +
  102 +bool isTagPresent(char* tag)
  103 +{
  104 + unsigned char data;
  105 + sendData(GET_NFC_STATUS);
  106 + data = getData();
  107 + if(data == PROFESSOR_TAG)
  108 + {
  109 + sendData(GET_TAG_DETAILS);
  110 + printx(INFO, NFC, "Professor Tag Detected %02x\n", data);
  111 + return (read(serial_fd, tag, 6) == 6);
  112 + }
  113 + else if(data == STUDENT_TAG)
  114 + {
  115 + sendData(GET_TAG_DETAILS);
  116 + printx(INFO, NFC, "Student Tag Detected\n");
  117 + tag[0] = getData();
  118 + printx(DEBUG, NFC, "Last read %02x\n", tag[0]);
  119 + tag[1] = getData();
  120 + printx(DEBUG, NFC, "Last read %02x\n", tag[1]);
  121 + tag[2] = getData();
  122 + printx(DEBUG, NFC, "Last read %02x\n", tag[2]);
  123 + tag[3] = getData();
  124 + printx(DEBUG, NFC, "Last read %02x\n", tag[3]);
  125 + tag[4] = getData();
  126 + printx(DEBUG, NFC, "Last read %02x\n", tag[4]);
  127 + tag[5] = getData();
  128 + printx(DEBUG, NFC, "Last read %02x\n", tag[5]);
  129 + tag[6] = getData();
  130 + printx(DEBUG, NFC, "Last read %02x\n", tag[6]);
  131 + tag[7] = getData();
  132 + printx(DEBUG, NFC, "Last read %02x\n", tag[7]);
  133 + return true;
  134 + }
  135 + else if(data == NO_TAG_DETECTED)
  136 + {
  137 + printx(DEBUG, NFC, "No Tag Detected\n");
  138 + return false;
  139 + }
  140 + else
  141 + {
  142 + printx(DEBUG, NFC, "Read data did not matched with expected data %02x\n", data);
  143 + return false;
  144 + }
  145 + return true;
  146 +}
  147 +
  148 +void* processNFC(void* we)
  149 +{
  150 + char myTag[NFC_TAG_LENGTH];
  151 + char buffer[NFC_TAG_LENGTH];
  152 +
  153 + while(!stop)
  154 + {
  155 + printx(DEBUG, NFC, "Scanning for tags\n");
  156 + if(isTagPresent(buffer))
  157 + {
  158 + sprintf(myTag, "%s", buffer);
  159 + printx(DEBUG, NFC, "Want to send %s as new tag\n", myTag);
  160 + setTagName(myTag);
  161 + }
  162 +
  163 + usleep(NFC_POLLING_TIME);
  164 + }
  165 +
  166 + return NULL;
  167 +}
... ...
... ... @@ -0,0 +1,9 @@
  1 +#ifndef __NFC_H__
  2 +#define __NFC_H__
  3 +
  4 +#include <stdbool.h>
  5 +
  6 +bool initNFC();
  7 +void* processNFC();
  8 +
  9 +#endif
... ...
printx.c
... ... @@ -57,7 +57,8 @@ void printx(severity s, msgfrom from, char* str, ...)
57 57 va_start(arglist, str);
58 58 vsprintf(buffer1, str, arglist);
59 59 fprintf(logfile, buffer1);
60   - sprintf(buffer2, "[%s] : %s%s%s", f_name[from], s_color[s], buffer1, S_RESET);
  60 + fflush(logfile);
  61 + sprintf(buffer2, "[%s] %s%s%s", f_name[from], s_color[s], buffer1, S_RESET);
61 62 removeCharFromString('\n', buffer2);
62 63 addToLog(buffer2);
63 64 va_end(arglist);
... ...
... ... @@ -7,7 +7,7 @@
7 7  
8 8 #define TIME_LENGTH 24
9 9 #define HEADER_TEXT_LENGTH 24
10   -#define LOG_LINES 25
  10 +#define LOG_LINES 35
11 11 #define LOG_LENGTH 82
12 12 #define IPS 20
13 13 #define SCREEN_TIME 1000000/IPS
... ... @@ -31,6 +31,13 @@ void* drawUI(void* we)
31 31 return NULL;
32 32 }
33 33  
  34 +void setTagName(char* tag)
  35 +{
  36 + time_t now = time(NULL);
  37 + strcpy(uid, tag);
  38 + strftime(uidDate, HEADER_TEXT_LENGTH, "%F-%T:%d", localtime(&now));
  39 +}
  40 +
34 41 void initUILog()
35 42 {
36 43 started[0]='\0';
... ... @@ -85,10 +92,10 @@ void displayUI()
85 92 //header
86 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");
87 94 printf("\u2503 %s \u2503 T H E \u2503", fillHeaderWithSpaces(buffer, "Started since"));
88   - printf(" %s \u2503\n", fillHeaderWithSpaces(buffer, "No Tag Detected"));
89   - printf("\u2503 %s \u2503 T W E E K \u2503 ", fillHeaderWithSpaces(buffer, started));
90 95 sprintf(buffer, "%s", strlen(uidDate) > 0 ? uidDate : "Waiting for a tag");
91   - printf("%s \u2503\n", fillHeaderWithSpaces(buffer, buffer));
  96 + printf(" %s \u2503\n", fillHeaderWithSpaces(buffer, buffer));
  97 + printf("\u2503 %s \u2503 T W E E K \u2503 ", fillHeaderWithSpaces(buffer, started));
  98 + printf("%s \u2503\n", fillHeaderWithSpaces(buffer, " "));
92 99 sprintf(buffer, "PID %d", mainPid);
93 100 printf("\u2503 %s \u2503 P R O J E C T \u2503 ", fillHeaderWithSpaces(buffer, buffer));
94 101 sprintf(buffer, "UID : %s", strlen(uid) > 0 ? uid : "Nope");
... ...
... ... @@ -6,5 +6,6 @@ void initUILog();
6 6 void addToLog(char*);
7 7 void displayUI();
8 8 void setStartTime(char*);
  9 +void setTagName(char*);
9 10  
10 11 #endif
... ...