Refactor RFID handler and user structures; add UART configuration and packet parsing functions; create logger header and source files
This commit is contained in:
@@ -1,7 +1,15 @@
|
|||||||
#ifndef __RFID_HANDLER_H__
|
#ifndef __RFID_HANDLER_H__
|
||||||
#define __RFIT_HANDLER_H__
|
#define __RFIT_HANDLER_H__
|
||||||
|
|
||||||
#endif
|
#include "main.h"
|
||||||
|
|
||||||
// UART handler thread
|
// UART handler thread
|
||||||
void *uartListener(void *arg);
|
void *uartListener(void *arg);
|
||||||
|
|
||||||
|
void parseIncommingPacket(BYTE * _packet);
|
||||||
|
|
||||||
|
void sendPacketToDevice(BYTE * _packet);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -12,8 +12,8 @@ typedef struct person
|
|||||||
{
|
{
|
||||||
uint16_t uuid;
|
uint16_t uuid;
|
||||||
|
|
||||||
char *name;
|
BYTE *name;
|
||||||
char *surname;
|
BYTE *surname;
|
||||||
uint32_t department;
|
uint32_t department;
|
||||||
|
|
||||||
time_t last_time_event;
|
time_t last_time_event;
|
||||||
|
|||||||
+1
-1
@@ -8,11 +8,11 @@
|
|||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
#include "users.h"
|
#include "users.h"
|
||||||
#include "tui.h"
|
#include "tui.h"
|
||||||
#include "rfid_handler.h"
|
#include "rfid_handler.h"
|
||||||
#include "file_operations.h"
|
#include "file_operations.h"
|
||||||
#include "main.h"
|
|
||||||
|
|
||||||
int main(int argc, char const **argv)
|
int main(int argc, char const **argv)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -21,6 +21,104 @@ void *uartListener(void *arg)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct termios options;
|
||||||
|
tcgetattr(uart, &options);
|
||||||
|
|
||||||
|
cfsetispeed(&options, B9600);
|
||||||
|
cfsetospeed(&options, B9600);
|
||||||
|
options.c_cflag &= ~PARENB;
|
||||||
|
options.c_cflag &= ~CSTOPB;
|
||||||
|
options.c_cflag &= ~CSIZE;
|
||||||
|
options.c_cflag |= CS8;
|
||||||
|
options.c_cflag |= CREAD | CLOCAL;
|
||||||
|
|
||||||
|
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
|
||||||
|
options.c_iflag &= ~(IXON | IXOFF | IXANY);
|
||||||
|
options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL);
|
||||||
|
options.c_oflag &= ~OPOST;
|
||||||
|
|
||||||
|
options.c_cc[VMIN] = 1;
|
||||||
|
options.c_cc[VTIME] = 0;
|
||||||
|
|
||||||
|
if (tcsetattr(uart, TCSANOW, &options) != 0)
|
||||||
|
{
|
||||||
|
perror("tcsetattr");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
enum
|
||||||
|
{
|
||||||
|
SYNC0,
|
||||||
|
SYNC1,
|
||||||
|
READING_PACKET
|
||||||
|
} state = SYNC0;
|
||||||
|
uint8_t packet[6];
|
||||||
|
int packet_index = 0;
|
||||||
|
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
uint8_t byte;
|
||||||
|
ssize_t n = read(uart, &byte, 1);
|
||||||
|
if (n == -1)
|
||||||
|
{
|
||||||
|
perror("read");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (state)
|
||||||
|
{
|
||||||
|
case SYNC0:
|
||||||
|
if (byte == 0x55)
|
||||||
|
{
|
||||||
|
packet[0] = byte;
|
||||||
|
packet_index = 1;
|
||||||
|
state = SYNC1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SYNC1:
|
||||||
|
if (byte == 0xFF)
|
||||||
|
{
|
||||||
|
packet[1] = byte;
|
||||||
|
packet_index = 2;
|
||||||
|
state = READING_PACKET;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state = SYNC0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case READING_PACKET:
|
||||||
|
packet[packet_index++] = byte;
|
||||||
|
if (packet_index == 6)
|
||||||
|
{
|
||||||
|
uint8_t crc = 0;
|
||||||
|
for (int i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
crc ^= packet[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (crc == packet[5])
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//fprintf(stderr, "Chyba CRC!\n");
|
||||||
|
}
|
||||||
|
state = SYNC0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
close(uart);
|
close(uart);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void parseIncommingPacket(BYTE * _packet)
|
||||||
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user