Refactor RFID handler and user structures; add UART configuration and packet parsing functions; create logger header and source files

This commit is contained in:
2025-05-13 10:28:37 +02:00
parent 039a9e4770
commit 0a5f176582
6 changed files with 110 additions and 4 deletions
View File
+1 -1
View File
@@ -8,11 +8,11 @@
#include <termios.h>
#include <string.h>
#include "main.h"
#include "users.h"
#include "tui.h"
#include "rfid_handler.h"
#include "file_operations.h"
#include "main.h"
int main(int argc, char const **argv)
{
+98
View File
@@ -21,6 +21,104 @@ void *uartListener(void *arg)
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);
return NULL;
}
void parseIncommingPacket(BYTE * _packet)
{
}