From 863d3e03e458292c8a8b5bb3d259e2b845cf7b96 Mon Sep 17 00:00:00 2001 From: "DLABAL, Eduard" Date: Tue, 13 May 2025 19:51:19 +0200 Subject: [PATCH] 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. --- .vscode/settings.json | 4 +++- include/rfid_handler.h | 11 +++++++++-- include/users.h | 13 +++++++++---- src/main.c | 19 +++++++++++++++++-- src/rfid_handler.c | 22 +++++++++++++++------- src/users.c | 32 +++++++++++++++++++------------- 6 files changed, 72 insertions(+), 29 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index d10acf6..18bd204 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -6,7 +6,9 @@ "errno.h": "c", "rfid_handler.h": "c", "fcntl.h": "c", - "file_operations.h": "c" + "file_operations.h": "c", + "string.h": "c", + "termios.h": "c" }, "C_Cpp.errorSquiggles": "enabled" } \ No newline at end of file diff --git a/include/rfid_handler.h b/include/rfid_handler.h index 3a0e9d2..0e555c7 100644 --- a/include/rfid_handler.h +++ b/include/rfid_handler.h @@ -1,12 +1,19 @@ #ifndef __RFID_HANDLER_H__ -#define __RFIT_HANDLER_H__ +#define __RFID_HANDLER_H__ #include "main.h" +#include "users.h" + +typedef struct +{ + char *uart_address; + list_t *person_list_head; +} UartThreadArgs; // UART handler thread void *uartListener(void *arg); -void parseIncommingPacket(BYTE * _packet); +void parseIncommingPacket(list_t *_head, BYTE *_packet); void sendPacketToDevice(BYTE * _packet); diff --git a/include/users.h b/include/users.h index 9ec16d6..24d3f3b 100644 --- a/include/users.h +++ b/include/users.h @@ -12,16 +12,14 @@ typedef struct person { uint16_t uuid; - BYTE *name; - BYTE *surname; + char *name; + char *surname; uint32_t department; time_t last_time_event; time_t total; BYTE available; - - pthread_mutex_t lock; } person_t; typedef struct node @@ -30,6 +28,13 @@ typedef struct node struct node *next; } 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. * diff --git a/src/main.c b/src/main.c index 9e102f4..8f45c1c 100644 --- a/src/main.c +++ b/src/main.c @@ -12,7 +12,6 @@ #include "users.h" #include "tui.h" #include "rfid_handler.h" -#include "file_operations.h" int main(int argc, char const **argv) { @@ -37,6 +36,18 @@ int main(int argc, char const **argv) 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(); noraw(); cbreak(); @@ -49,7 +60,11 @@ int main(int argc, char const **argv) 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); // diff --git a/src/rfid_handler.c b/src/rfid_handler.c index f2ab590..63b103f 100644 --- a/src/rfid_handler.c +++ b/src/rfid_handler.c @@ -13,7 +13,11 @@ 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); if (uart == -1) { @@ -46,8 +50,7 @@ void *uartListener(void *arg) exit(EXIT_FAILURE); } - - enum + enum { SYNC0, SYNC1, @@ -102,11 +105,11 @@ void *uartListener(void *arg) if (crc == packet[5]) { - + parseIncommingPacket(person_list_head, packet); } else { - //fprintf(stderr, "Chyba CRC!\n"); + // fprintf(stderr, "Chyba CRC!\n"); } state = SYNC0; } @@ -118,7 +121,12 @@ void *uartListener(void *arg) 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)); } \ No newline at end of file diff --git a/src/users.c b/src/users.c index c703ad5..5079f4e 100644 --- a/src/users.c +++ b/src/users.c @@ -13,20 +13,25 @@ uint16_t generateUUID( node_t *_head) { - uint16_t uuid = rand() % (UINT16_MAX - 1 + 1) + 1; - node_t *current = _head; - while (current != NULL) + uint16_t uuid; + int found; + + do { - if (current->user.uuid == uuid) - { - uuid = rand() % (UINT16_MAX - 1 + 1) + 1; - current = _head; - } - else + found = 0; + uuid = (uint16_t)(rand() % (UINT16_MAX + 1)); + + node_t *current = _head; + while (current != NULL) { + if (current->user.uuid == uuid) + { + found = 1; + } current = current->next; } - } + } while (found); + return uuid; } @@ -44,7 +49,7 @@ node_t *searchPersonByUUID( current = current->next; } return NULL; -}; +} uint16_t *searchUUIDByName( node_t *_head, @@ -72,7 +77,7 @@ uint16_t *searchUUIDByName( { sum++; - realloc(uuids, sum * sizeof(uint16_t)); + uuids = realloc(uuids, sum * sizeof(uint16_t)); if (uuids == NULL) { puts("Fatal error: free memory is not sufficient for this operation"); @@ -82,7 +87,8 @@ uint16_t *searchUUIDByName( uuids[sum - 1] = current->user.uuid; } } - if (sum == 0) return NULL; + if (sum == 0) + return NULL; return uuids; }