From 3bdc9dafd99be9061898dd1775057e4f0bea4b08 Mon Sep 17 00:00:00 2001 From: henyxia Date: Wed, 4 Mar 2015 14:23:18 +0100 Subject: [PATCH] NFC Added UI refreshed printx fixed --- Makefile | 2 +- main.c | 10 ++++++++-- nfc.c | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ nfc.h | 9 +++++++++ printx.c | 3 ++- ui.c | 15 +++++++++++---- ui.h | 1 + 7 files changed, 199 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 9ca760e..6847773 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CC_FLAGS=-c -Wall -Werror -std=gnu99 -g CC_LIBS=-pthread INCLUDES= -SOURCES=main.c printx.c ui.c +SOURCES=main.c printx.c ui.c nfc.c OBJECTS=$(SOURCES:.c=.o) OUTPUT=tweekd diff --git a/main.c b/main.c index 0cf3919..665c8ba 100644 --- a/main.c +++ b/main.c @@ -2,13 +2,14 @@ #include #include "printx.h" #include "ui.h" +#include "nfc.h" #include int main(void) { int ret; - pthread_t tUI;//, tNFC;//tHVC + pthread_t tUI, tNFC;//tHVC initUILog(); if(!initLog()) { @@ -21,7 +22,12 @@ int main(void) ret = pthread_create(&tUI, NULL, drawUI, NULL); printx(DEBUG, MAIN, "UI Started ID %08x ret %d\n", tUI, ret); printx(INFO, MAIN, "Initializing NFC"); - //pthread_create(&tNFC, NULL, + if(!initNFC()) + { + printx(ERROR, MAIN, "Unable to start the NFC interface\n"); + return 2; + } + pthread_create(&tNFC, NULL, processNFC, NULL); pthread_join(tUI, NULL); closeLog(); return 0; diff --git a/nfc.c b/nfc.c index e69de29..a543fee 100644 --- a/nfc.c +++ b/nfc.c @@ -0,0 +1,167 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "ui.h" +#include "nfc.h" +#include "printx.h" + +#define DEVICE1 "/dev/ttyACM0" +#define SPEED B19200 +#define NFC_POLLING_TIME 500000 +#define WAIT_BEFORE_RETRY 50000 +#define NFC_TAG_LENGTH 9 +#define GET_DEVICE_MODEL 'A' +#define DEVICE_MODEL_NFC 'B' +#define GET_NFC_STATUS 'C' +#define NO_TAG_DETECTED 'D' +#define PROFESSOR_TAG 'E' +#define STUDENT_TAG 'F' +#define GET_TAG_DETAILS 'H' + +// Globals +int serial_fd = -1; +bool stop = false; +struct termios saveterm; + +bool init_serial(char* device, int speed) +{ + struct termios new; + + int fd=open(device,O_RDWR|O_NOCTTY|O_NONBLOCK); + + if(fd<0) + return false; + + tcgetattr(fd,&saveterm); // save current port settings + bzero(&new,sizeof(new)); + new.c_cflag=CLOCAL|CREAD|speed|CS8; + new.c_iflag=0; + new.c_oflag=0; + new.c_lflag=0; // set input mode (non-canonical, no echo,...) + new.c_cc[VTIME]=0; // inter-character timer unused + new.c_cc[VMIN]=1; // blocking read until 1 char received + tcflush(fd, TCIFLUSH); + tcsetattr(fd,TCSANOW,&new); + + serial_fd = fd; + sleep(2); + return true; +} + +void close_serial() +{ + tcsetattr(serial_fd,TCSANOW,&saveterm); + close(serial_fd); +} + +unsigned char getData() +{ + unsigned char buf; + while(read(serial_fd, &buf, 1) != 1) + usleep(WAIT_BEFORE_RETRY); + return buf; +} + +void sendData(unsigned char data) +{ + while(write(serial_fd, &data, 1) != 1) + usleep(WAIT_BEFORE_RETRY); +} + +bool initNFC() +{ + unsigned char data; + printx(DEBUG, NFC, "Connecting to interface 1\n"); + if(init_serial(DEVICE1, SPEED)) + { + printx(DEBUG, NFC, "Connected to an interface\n"); + sendData(GET_DEVICE_MODEL); + printx(DEBUG, NFC, "Sended identification request %02x\n", GET_DEVICE_MODEL); + data = getData(); + if(data == DEVICE_MODEL_NFC) + printx(INFO, NFC, "NFC Arduino connected\n"); + else + printx(DEBUG, NFC, "NFC Arduino not found, response was %02x\n", data); + } + else + { + printx(WARNING, NFC, "Unable to connect to such interface\n"); + return false; + } + + return true; +} + +bool isTagPresent(char* tag) +{ + unsigned char data; + sendData(GET_NFC_STATUS); + data = getData(); + if(data == PROFESSOR_TAG) + { + sendData(GET_TAG_DETAILS); + printx(INFO, NFC, "Professor Tag Detected %02x\n", data); + return (read(serial_fd, tag, 6) == 6); + } + else if(data == STUDENT_TAG) + { + sendData(GET_TAG_DETAILS); + printx(INFO, NFC, "Student Tag Detected\n"); + tag[0] = getData(); + printx(DEBUG, NFC, "Last read %02x\n", tag[0]); + tag[1] = getData(); + printx(DEBUG, NFC, "Last read %02x\n", tag[1]); + tag[2] = getData(); + printx(DEBUG, NFC, "Last read %02x\n", tag[2]); + tag[3] = getData(); + printx(DEBUG, NFC, "Last read %02x\n", tag[3]); + tag[4] = getData(); + printx(DEBUG, NFC, "Last read %02x\n", tag[4]); + tag[5] = getData(); + printx(DEBUG, NFC, "Last read %02x\n", tag[5]); + tag[6] = getData(); + printx(DEBUG, NFC, "Last read %02x\n", tag[6]); + tag[7] = getData(); + printx(DEBUG, NFC, "Last read %02x\n", tag[7]); + return true; + } + else if(data == NO_TAG_DETECTED) + { + printx(DEBUG, NFC, "No Tag Detected\n"); + return false; + } + else + { + printx(DEBUG, NFC, "Read data did not matched with expected data %02x\n", data); + return false; + } + return true; +} + +void* processNFC(void* we) +{ + char myTag[NFC_TAG_LENGTH]; + char buffer[NFC_TAG_LENGTH]; + + while(!stop) + { + printx(DEBUG, NFC, "Scanning for tags\n"); + if(isTagPresent(buffer)) + { + sprintf(myTag, "%s", buffer); + printx(DEBUG, NFC, "Want to send %s as new tag\n", myTag); + setTagName(myTag); + } + + usleep(NFC_POLLING_TIME); + } + + return NULL; +} diff --git a/nfc.h b/nfc.h index e69de29..a598d63 100644 --- a/nfc.h +++ b/nfc.h @@ -0,0 +1,9 @@ +#ifndef __NFC_H__ +#define __NFC_H__ + +#include + +bool initNFC(); +void* processNFC(); + +#endif diff --git a/printx.c b/printx.c index e582683..fcbc215 100644 --- a/printx.c +++ b/printx.c @@ -57,7 +57,8 @@ void printx(severity s, msgfrom from, char* str, ...) va_start(arglist, str); vsprintf(buffer1, str, arglist); fprintf(logfile, buffer1); - sprintf(buffer2, "[%s] : %s%s%s", f_name[from], s_color[s], buffer1, S_RESET); + fflush(logfile); + sprintf(buffer2, "[%s] %s%s%s", f_name[from], s_color[s], buffer1, S_RESET); removeCharFromString('\n', buffer2); addToLog(buffer2); va_end(arglist); diff --git a/ui.c b/ui.c index a3fcc6e..260956f 100644 --- a/ui.c +++ b/ui.c @@ -7,7 +7,7 @@ #define TIME_LENGTH 24 #define HEADER_TEXT_LENGTH 24 -#define LOG_LINES 25 +#define LOG_LINES 35 #define LOG_LENGTH 82 #define IPS 20 #define SCREEN_TIME 1000000/IPS @@ -31,6 +31,13 @@ void* drawUI(void* we) return NULL; } +void setTagName(char* tag) +{ + time_t now = time(NULL); + strcpy(uid, tag); + strftime(uidDate, HEADER_TEXT_LENGTH, "%F-%T:%d", localtime(&now)); +} + void initUILog() { started[0]='\0'; @@ -85,10 +92,10 @@ void displayUI() //header 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"); printf("\u2503 %s \u2503 T H E \u2503", fillHeaderWithSpaces(buffer, "Started since")); - printf(" %s \u2503\n", fillHeaderWithSpaces(buffer, "No Tag Detected")); - printf("\u2503 %s \u2503 T W E E K \u2503 ", fillHeaderWithSpaces(buffer, started)); sprintf(buffer, "%s", strlen(uidDate) > 0 ? uidDate : "Waiting for a tag"); - printf("%s \u2503\n", fillHeaderWithSpaces(buffer, buffer)); + printf(" %s \u2503\n", fillHeaderWithSpaces(buffer, buffer)); + printf("\u2503 %s \u2503 T W E E K \u2503 ", fillHeaderWithSpaces(buffer, started)); + printf("%s \u2503\n", fillHeaderWithSpaces(buffer, " ")); sprintf(buffer, "PID %d", mainPid); printf("\u2503 %s \u2503 P R O J E C T \u2503 ", fillHeaderWithSpaces(buffer, buffer)); sprintf(buffer, "UID : %s", strlen(uid) > 0 ? uid : "Nope"); diff --git a/ui.h b/ui.h index dffff18..87c9194 100644 --- a/ui.h +++ b/ui.h @@ -6,5 +6,6 @@ void initUILog(); void addToLog(char*); void displayUI(); void setStartTime(char*); +void setTagName(char*); #endif -- libgit2 0.21.2