Refactor RFID handler and user structures; update memory management in main.c and users.c; improve UUID generation logic and add list handling in parseIncomingPacket function.

This commit is contained in:
2025-05-13 19:51:19 +02:00
parent 6fcbe861ae
commit 863d3e03e4
6 changed files with 72 additions and 29 deletions
+3 -1
View File
@@ -6,7 +6,9 @@
"errno.h": "c", "errno.h": "c",
"rfid_handler.h": "c", "rfid_handler.h": "c",
"fcntl.h": "c", "fcntl.h": "c",
"file_operations.h": "c" "file_operations.h": "c",
"string.h": "c",
"termios.h": "c"
}, },
"C_Cpp.errorSquiggles": "enabled" "C_Cpp.errorSquiggles": "enabled"
} }
+9 -2
View File
@@ -1,12 +1,19 @@
#ifndef __RFID_HANDLER_H__ #ifndef __RFID_HANDLER_H__
#define __RFIT_HANDLER_H__ #define __RFID_HANDLER_H__
#include "main.h" #include "main.h"
#include "users.h"
typedef struct
{
char *uart_address;
list_t *person_list_head;
} UartThreadArgs;
// UART handler thread // UART handler thread
void *uartListener(void *arg); void *uartListener(void *arg);
void parseIncommingPacket(BYTE * _packet); void parseIncommingPacket(list_t *_head, BYTE *_packet);
void sendPacketToDevice(BYTE * _packet); void sendPacketToDevice(BYTE * _packet);
+9 -4
View File
@@ -12,16 +12,14 @@ typedef struct person
{ {
uint16_t uuid; uint16_t uuid;
BYTE *name; char *name;
BYTE *surname; char *surname;
uint32_t department; uint32_t department;
time_t last_time_event; time_t last_time_event;
time_t total; time_t total;
BYTE available; BYTE available;
pthread_mutex_t lock;
} person_t; } person_t;
typedef struct node typedef struct node
@@ -30,6 +28,13 @@ typedef struct node
struct node *next; struct node *next;
} node_t; } node_t;
typedef struct list
{
node_t *head;
pthread_mutex_t lock;
} list_t;
/** /**
* @brief Generates a unique identifier (UUID) for a new node in the linked list. * @brief Generates a unique identifier (UUID) for a new node in the linked list.
* *
+17 -2
View File
@@ -12,7 +12,6 @@
#include "users.h" #include "users.h"
#include "tui.h" #include "tui.h"
#include "rfid_handler.h" #include "rfid_handler.h"
#include "file_operations.h"
int main(int argc, char const **argv) int main(int argc, char const **argv)
{ {
@@ -37,6 +36,18 @@ int main(int argc, char const **argv)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
node_t *person_list_head = (node_t *)malloc(sizeof(node_t));
if (person_list_head == NULL)
{
endwin();
clear();
puts("Memory allocation error. Exiting...\n");
return EXIT_FAILURE;
}
list_t person_list;
person_list.head = person_list_head;
pthread_mutex_init(&person_list.lock, NULL);
noecho(); noecho();
noraw(); noraw();
cbreak(); cbreak();
@@ -49,7 +60,11 @@ int main(int argc, char const **argv)
uartDialog(uart_address, x_max, y_max); uartDialog(uart_address, x_max, y_max);
pthread_create(&uart_thread, NULL, uartListener, uart_address); UartThreadArgs *args = malloc(sizeof(UartThreadArgs));
args->uart_address = uart_address;
args->person_list_head = &person_list;
pthread_create(&uart_thread, NULL, uartListener, args);
// //
+14 -6
View File
@@ -13,7 +13,11 @@
void *uartListener(void *arg) void *uartListener(void *arg)
{ {
char *uart_address = (char *)arg; UartThreadArgs *thread_args = (UartThreadArgs *)arg;
char *uart_address = thread_args->uart_address;
list_t *person_list_head = thread_args->person_list_head;
int uart = open(uart_address, O_RDWR | O_NOCTTY | O_NDELAY); int uart = open(uart_address, O_RDWR | O_NOCTTY | O_NDELAY);
if (uart == -1) if (uart == -1)
{ {
@@ -46,8 +50,7 @@ void *uartListener(void *arg)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
enum
enum
{ {
SYNC0, SYNC0,
SYNC1, SYNC1,
@@ -102,11 +105,11 @@ void *uartListener(void *arg)
if (crc == packet[5]) if (crc == packet[5])
{ {
parseIncommingPacket(person_list_head, packet);
} }
else else
{ {
//fprintf(stderr, "Chyba CRC!\n"); // fprintf(stderr, "Chyba CRC!\n");
} }
state = SYNC0; state = SYNC0;
} }
@@ -118,7 +121,12 @@ void *uartListener(void *arg)
return NULL; return NULL;
} }
void parseIncommingPacket(BYTE * _packet) void parseIncommingPacket(list_t *_head, BYTE *_packet)
{ {
uint16_t r_id;
BYTE r_event_type;
memcpy(&r_id, &_packet[2], 2);
memcpy(&r_event_type, &_packet[4], 1);
addTimeEvent(searchPersonByUUID(_head->head, r_id));
} }
+19 -13
View File
@@ -13,20 +13,25 @@
uint16_t generateUUID( uint16_t generateUUID(
node_t *_head) node_t *_head)
{ {
uint16_t uuid = rand() % (UINT16_MAX - 1 + 1) + 1; uint16_t uuid;
node_t *current = _head; int found;
while (current != NULL)
do
{ {
if (current->user.uuid == uuid) found = 0;
{ uuid = (uint16_t)(rand() % (UINT16_MAX + 1));
uuid = rand() % (UINT16_MAX - 1 + 1) + 1;
current = _head; node_t *current = _head;
} while (current != NULL)
else
{ {
if (current->user.uuid == uuid)
{
found = 1;
}
current = current->next; current = current->next;
} }
} } while (found);
return uuid; return uuid;
} }
@@ -44,7 +49,7 @@ node_t *searchPersonByUUID(
current = current->next; current = current->next;
} }
return NULL; return NULL;
}; }
uint16_t *searchUUIDByName( uint16_t *searchUUIDByName(
node_t *_head, node_t *_head,
@@ -72,7 +77,7 @@ uint16_t *searchUUIDByName(
{ {
sum++; sum++;
realloc(uuids, sum * sizeof(uint16_t)); uuids = realloc(uuids, sum * sizeof(uint16_t));
if (uuids == NULL) if (uuids == NULL)
{ {
puts("Fatal error: free memory is not sufficient for this operation"); puts("Fatal error: free memory is not sufficient for this operation");
@@ -82,7 +87,8 @@ uint16_t *searchUUIDByName(
uuids[sum - 1] = current->user.uuid; uuids[sum - 1] = current->user.uuid;
} }
} }
if (sum == 0) return NULL; if (sum == 0)
return NULL;
return uuids; return uuids;
} }