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:
Vendored
+3
-1
@@ -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"
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
|
||||
+9
-4
@@ -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.
|
||||
*
|
||||
|
||||
+17
-2
@@ -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);
|
||||
|
||||
//
|
||||
|
||||
|
||||
+14
-6
@@ -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));
|
||||
}
|
||||
+19
-13
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user