This repository has been archived on 2026-05-29. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
LightTAM/src/rfid_handler.c
T

124 lines
2.6 KiB
C

#include "rfid_handler.h"
#include "users.h"
#include "main.h"
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <string.h>
void *uartListener(void *arg)
{
char *uart_address = (char *)arg;
int uart = open(uart_address, O_RDWR | O_NOCTTY | O_NDELAY);
if (uart == -1)
{
perror("Failed to open UART");
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)
{
}