Update project files and headers: ensure consistency and maintainability across the codebase.

This commit is contained in:
2025-05-18 22:14:42 +02:00
parent 39e72b33aa
commit 102f0c1b6b
15 changed files with 464 additions and 129 deletions
+2 -5
View File
@@ -28,8 +28,7 @@ int loadListFromCSV(char *_filename, list_t *_list)
}
else
{
// vstupni databaze je vadna
// neco jako ja
exit(-1);
}
}
@@ -45,7 +44,7 @@ int saveListToCSV(char *_filename, list_t *_list)
{
return 0;
}
int save_sum = 0;
int save_sum = 0;
node_t *cursor = _list->head;
while (cursor != NULL)
{
@@ -57,8 +56,6 @@ int saveListToCSV(char *_filename, list_t *_list)
cursor = cursor->next;
}
fclose(f_out);
return save_sum;
}
+43 -16
View File
@@ -1,5 +1,7 @@
#include "logger.h"
Logger *logger_global = NULL;
Logger *initLogger(const char *_filename)
{
Logger *logger_struct = malloc(sizeof(Logger));
@@ -19,58 +21,83 @@ Logger *initLogger(const char *_filename)
return logger_struct;
}
int logErrorEvent(Logger *_logger, const char *_message)
int logErrorEvent(Logger *_logger, const char *_fmt, ...)
{
if (_logger == NULL)
{
return 0;
}
char current_time_str[32];
printDateAndTimeInString(time(NULL), current_time_str);
fprintf(_logger->f_log, "%s [ERROR] %s", current_time_str, _message);
fprintf(_logger->f_log, "%s [ERROR] ", current_time_str);
va_list args;
va_start(args, _fmt);
vfprintf(_logger->f_log, _fmt, args);
va_end(args);
fprintf(_logger->f_log, "\n");
fflush(_logger->f_log);
return 1;
}
int logWarningEvent(Logger *_logger, const char *_message)
int logWarningEvent(Logger *_logger, const char *_fmt, ...)
{
if (_logger == NULL)
{
return 0;
}
char current_time_str[32];
printDateAndTimeInString(time(NULL), current_time_str);
fprintf(_logger->f_log, "%s [WARNING] %s", current_time_str, _message);
fprintf(_logger->f_log, "%s [WARNING] ", current_time_str);
va_list args;
va_start(args, _fmt);
vfprintf(_logger->f_log, _fmt, args);
va_end(args);
fprintf(_logger->f_log, "\n");
fflush(_logger->f_log);
return 1;
}
int logHardwareEvent(Logger *_logger, const char *_event_origin, const char *_message)
int logHardwareEvent(Logger *_logger, const char *_event_origin, const char *_fmt, ...)
{
if (_logger == NULL)
{
return 0;
}
char current_time_str[32];
printDateAndTimeInString(time(NULL), current_time_str);
fprintf(_logger->f_log, "%s [%s] %s", current_time_str, _event_origin, _message);
fprintf(_logger->f_log, "%s [%s] ", current_time_str, _event_origin);
va_list args;
va_start(args, _fmt);
vfprintf(_logger->f_log, _fmt, args);
va_end(args);
fprintf(_logger->f_log, "\n");
fflush(_logger->f_log);
return 1;
}
int logInfoEvent(Logger *_logger, const char *_message)
int logInfoEvent(Logger *_logger, const char *_fmt, ...)
{
if (_logger == NULL)
{
return 0;
}
char current_time_str[32];
printDateAndTimeInString(time(NULL), current_time_str);
fprintf(_logger->f_log, "%s [INFO] %s", current_time_str, _message);
fprintf(_logger->f_log, "%s [INFO] ", current_time_str);
va_list args;
va_start(args, _fmt);
vfprintf(_logger->f_log, _fmt, args);
va_end(args);
fprintf(_logger->f_log, "\n");
fflush(_logger->f_log);
return 1;
}
int endLogger(Logger *_logger)
+6 -8
View File
@@ -17,6 +17,8 @@ int main(int argc, char const **argv)
return EXIT_FAILURE;
}
logger_global = initLogger("lighttam_journal");
int y_max, x_max;
char uart_address[51];
pthread_t uart_thread;
@@ -28,18 +30,14 @@ int main(int argc, char const **argv)
{
endwin();
clear();
logErrorEvent(logger_global, "Terminal Window is too small");
endLogger(logger_global);
puts("Terminal is too small. Exiting...\n");
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;
}
node_t *person_list_head = NULL;
list_t person_list;
person_list.head = person_list_head;
pthread_mutex_init(&person_list.lock, NULL);
+16 -2
View File
@@ -1,6 +1,7 @@
#include "rfid_handler.h"
#include "users.h"
#include "main.h"
#include "logger.h"
#include <stdio.h>
#include <stdlib.h>
@@ -24,7 +25,7 @@ void *uartListener(void *arg)
int uart = open(uart_address, O_RDWR | O_NOCTTY | O_NDELAY);
if (uart == -1)
{
// perror("Failed to open UART");
logErrorEvent(logger_global, "UART open failed for %s: %s", uart_address, strerror(errno));
flags->wait_for_UART_flag = FALSE;
flags->uart_status = FALSE;
return NULL;
@@ -50,10 +51,12 @@ void *uartListener(void *arg)
if (tcsetattr(uart, TCSANOW, &options) != 0)
{
logErrorEvent(logger_global, "UART tcsetattr failed: %s", strerror(errno));
flags->wait_for_UART_flag = FALSE;
flags->uart_status = FALSE;
return NULL;
}
logInfoEvent(logger_global, "UART %s opened and configured", uart_address);
flags->uart_status = TRUE;
flags->wait_for_UART_flag = FALSE;
@@ -71,6 +74,7 @@ void *uartListener(void *arg)
{
if (flags->end_from_main_flag)
{
logInfoEvent(logger_global, "UART thread received end signal, closing UART.");
close(uart);
return NULL;
}
@@ -85,6 +89,9 @@ void *uartListener(void *arg)
continue;
}
perror("read");
logErrorEvent(logger_global, "UART read error: %s", strerror(errno));
clear();
endwin();
exit(EXIT_FAILURE);
}
if (n == 0)
@@ -129,15 +136,17 @@ void *uartListener(void *arg)
if (crc == packet[5])
{
logInfoEvent(logger_global, "Valid packet received from UART (CRC OK)");
BYTE success;
uint16_t r_uuid = parseIncommingPacket(packet);
node_t *selected = searchPersonByUUID(person_list, r_uuid);
success = addTimeEvent(selected, person_list);
sendPacketToDevice(uart, selected, success);
logInfoEvent(logger_global, "Processed RFID event for UUID: %u, success: %u", r_uuid, success);
}
else
{
//fprintf(stderr, "Chyba CRC!\n");
logWarningEvent(logger_global, "UART packet CRC error");
sendPacketToDevice(uart, NULL, 0);
}
state = SYNC0;
@@ -147,6 +156,8 @@ void *uartListener(void *arg)
}
close(uart);
logInfoEvent(logger_global, "UART %s closed", uart_address);
return NULL;
}
@@ -206,4 +217,7 @@ void sendPacketToDevice(int _uart, node_t *_person, BYTE state)
offset += 1;
write(_uart, packet, offset);
logInfoEvent(logger_global, "Sent packet to device (state=%u, name=%s, surname=%s)", state,
_person && _person->user.name ? _person->user.name : "(none)",
_person && _person->user.surname ? _person->user.surname : "(none)");
}
+63
View File
@@ -1,5 +1,6 @@
#include "users.h"
#include "main.h"
#include "logger.h"
#include <stdio.h>
#include <stdlib.h>
@@ -47,11 +48,16 @@ node_t *searchPersonByUUID(
{
pthread_mutex_unlock(&_list->lock);
logInfoEvent(logger_global, "Person found by UUID: %u", _uuid);
return current;
}
current = current->next;
}
pthread_mutex_unlock(&_list->lock);
logWarningEvent(logger_global, "Person with UUID %u not found", _uuid);
return NULL;
}
@@ -62,6 +68,8 @@ uint16_t searchUUIDByName(list_t *_list, char *_name, char *_surname, uint16_t *
if (uuids == NULL)
{
*_uuids_found = NULL;
logErrorEvent(logger_global, "Memory allocation failed in searchUUIDByName");
return 0;
}
@@ -80,6 +88,8 @@ uint16_t searchUUIDByName(list_t *_list, char *_name, char *_surname, uint16_t *
{
pthread_mutex_unlock(&_list->lock);
free(uuids);
logErrorEvent(logger_global, "Memory allocation failed in searchUUIDByName (realloc)");
endLogger(logger_global);
puts("Fatal error: free memory is not sufficient for this operation");
exit(EXIT_FAILURE);
}
@@ -92,6 +102,9 @@ uint16_t searchUUIDByName(list_t *_list, char *_name, char *_surname, uint16_t *
pthread_mutex_unlock(&_list->lock);
*_uuids_found = uuids;
logInfoEvent(logger_global, "Found %u UUID(s) for name: %s %s", sum, _name, _surname);
return sum;
}
@@ -103,12 +116,16 @@ int addPersonToList(
{
node_t *new_node = (node_t *)malloc(sizeof(node_t));
if (new_node == NULL)
{
logErrorEvent(logger_global, "Memory allocation failed in addPersonToList");
return 0;
}
if (!strlen(_name) || !strlen(_surname))
{
free(new_node);
new_node = NULL;
logWarningEvent(logger_global, "Attempted to add person with empty name or surname");
return 0;
}
@@ -121,6 +138,7 @@ int addPersonToList(
free(new_node->user.name);
free(new_node->user.surname);
free(new_node);
logErrorEvent(logger_global, "Memory allocation failed for name/surname in addPersonToList");
return 0;
}
@@ -140,6 +158,8 @@ int addPersonToList(
_list->head = new_node;
pthread_mutex_unlock(&_list->lock);
logInfoEvent(logger_global, "Added person (UUID: %u) as head: %s %s", new_node->user.uuid, _name, _surname);
return 0;
}
@@ -151,6 +171,8 @@ int addPersonToList(
current->next = new_node;
pthread_mutex_unlock(&_list->lock);
logInfoEvent(logger_global, "Added person (UUID: %u): %s %s", new_node->user.uuid, _name, _surname);
return 1;
}
@@ -158,6 +180,7 @@ int loadPersonToList(list_t *_list, uint16_t _uuid, char *_name, char *_surname,
{
if (!strlen(_name) || !strlen(_surname))
{
logWarningEvent(logger_global, "Attempted to load person with empty name or surname");
return 0;
}
@@ -169,6 +192,7 @@ int loadPersonToList(list_t *_list, uint16_t _uuid, char *_name, char *_surname,
if (_list->head == NULL)
{
pthread_mutex_unlock(&_list->lock);
logErrorEvent(logger_global, "Memory allocation failed in loadPersonToList (head)");
return 0;
}
_list->head->next = NULL;
@@ -188,6 +212,7 @@ int loadPersonToList(list_t *_list, uint16_t _uuid, char *_name, char *_surname,
free(target->user.name);
free(target->user.surname);
pthread_mutex_unlock(&_list->lock);
logErrorEvent(logger_global, "Memory allocation failed for name/surname in loadPersonToList (head)");
return 0;
}
@@ -198,6 +223,9 @@ int loadPersonToList(list_t *_list, uint16_t _uuid, char *_name, char *_surname,
target->user.total = _total;
target->user.available = _available;
pthread_mutex_unlock(&_list->lock);
logInfoEvent(logger_global, "Loaded person as head (UUID: %u): %s %s", _uuid, _name, _surname);
return 1;
}
@@ -205,6 +233,7 @@ int loadPersonToList(list_t *_list, uint16_t _uuid, char *_name, char *_surname,
if (new_node == NULL)
{
pthread_mutex_unlock(&_list->lock);
logErrorEvent(logger_global, "Memory allocation failed in loadPersonToList (new node)");
return 0;
}
@@ -218,6 +247,7 @@ int loadPersonToList(list_t *_list, uint16_t _uuid, char *_name, char *_surname,
free(new_node->user.surname);
free(new_node);
pthread_mutex_unlock(&_list->lock);
logErrorEvent(logger_global, "Memory allocation failed for name/surname in loadPersonToList (new node)");
return 0;
}
@@ -237,6 +267,9 @@ int loadPersonToList(list_t *_list, uint16_t _uuid, char *_name, char *_surname,
}
current->next = new_node;
pthread_mutex_unlock(&_list->lock);
logInfoEvent(logger_global, "Loaded person (UUID: %u): %s %s", _uuid, _name, _surname);
return 1;
}
@@ -249,6 +282,7 @@ int addTimeEvent(
if (_person == NULL)
{
logWarningEvent(logger_global, "addTimeEvent called with NULL person");
return 0;
}
@@ -270,13 +304,18 @@ int addTimeEvent(
_person->user.last_time_event = current_time;
pthread_mutex_unlock(&_list->lock);
logInfoEvent(logger_global, "Time event for UUID %u: type %u, time %ld", _person->user.uuid, type, current_time);
return type;
}
int removePersonFromList(list_t *_list, node_t **_person)
{
if (_list == NULL || _list->head == NULL || _person == NULL || *_person == NULL)
{
logWarningEvent(logger_global, "removePersonFromList called with invalid arguments");
return 0;
}
pthread_mutex_lock(&_list->lock);
node_t *current = _list->head;
@@ -294,6 +333,7 @@ int removePersonFromList(list_t *_list, node_t **_person)
{
prev->next = current->next;
}
logInfoEvent(logger_global, "Removing person (UUID: %u): %s %s", current->user.uuid, current->user.name, current->user.surname);
free(current->user.name);
free(current->user.surname);
free(current);
@@ -305,5 +345,28 @@ int removePersonFromList(list_t *_list, node_t **_person)
current = current->next;
}
pthread_mutex_unlock(&_list->lock);
logWarningEvent(logger_global, "Person to remove not found in list");
return 0;
}
void freePersonList(list_t ** _list){
if (_list == NULL || *_list == NULL)
return;
pthread_mutex_lock(&(*_list)->lock);
node_t *current = (*_list)->head;
while (current != NULL)
{
node_t *next = current->next;
free(current->user.name);
free(current->user.surname);
free(current);
current = next;
}
(*_list)->head = NULL;
pthread_mutex_unlock(&(*_list)->lock);
free(*_list);
*_list = NULL;
}