Refactor UART handling and TUI dialogs: integrate UartThreadFlags structure, update uartListener to manage UART status, and add functions for person dialog operations.

This commit is contained in:
2025-05-17 23:46:54 +02:00
parent 61c7a6ad0f
commit 9e7fa91180
5 changed files with 64 additions and 17 deletions
+10 -6
View File
@@ -4,22 +4,26 @@
#include "main.h" #include "main.h"
#include "users.h" #include "users.h"
typedef struct
{
BYTE wait_for_UART_flag;
BYTE end_from_main_flag;
BYTE uart_status;
} UartThreadFlags;
typedef struct typedef struct
{ {
char *uart_address; char *uart_address;
BYTE *uart_status;
list_t *person_list_head; list_t *person_list_head;
UartThreadFlags *flags;
} UartThreadArgs; } UartThreadArgs;
// UART handler thread // UART handler thread
void *uartListener(void *arg); void *uartListener(void *arg);
uint16_t parseIncommingPacket(BYTE *_packet); uint16_t parseIncommingPacket(BYTE *_packet);
void sendPacketToDevice(BYTE * _packet); void sendPacketToDevice(BYTE *_packet);
#endif #endif
+4
View File
@@ -34,4 +34,8 @@ int exportDialog(list_t *_list, int terminal_x, int terminal_y);
void editDatabaseMenu(list_t *_person_list, int terminal_x, int terminal_y); void editDatabaseMenu(list_t *_person_list, int terminal_x, int terminal_y);
void addPersonDialog(list_t *_person_list, int terminal_x, int terminal_y);
void deletePersonDialog(list_t *_person_list, int terminal_x, int terminal_y);
#endif #endif
+12 -4
View File
@@ -22,7 +22,6 @@ int main(int argc, char const **argv)
} }
int y_max, x_max; int y_max, x_max;
BYTE uart_status = FALSE;
char uart_address[51]; char uart_address[51];
pthread_t uart_thread; pthread_t uart_thread;
@@ -79,17 +78,24 @@ int main(int argc, char const **argv)
uartDialog(uart_address, x_max, y_max); uartDialog(uart_address, x_max, y_max);
UartThreadFlags flags;
flags.end_from_main_flag = FALSE;
flags.wait_for_UART_flag = FALSE;
UartThreadArgs *args = malloc(sizeof(UartThreadArgs)); UartThreadArgs *args = malloc(sizeof(UartThreadArgs));
args->uart_address = uart_address; args->uart_address = uart_address;
args->person_list_head = &person_list; args->person_list_head = &person_list;
args->uart_status = &uart_status; args->flags = &flags;
pthread_create(&uart_thread, NULL, uartListener, args); pthread_create(&uart_thread, NULL, uartListener, args);
usleep(1000); while (flags.wait_for_UART_flag)
;
usleep(50000);
attron(A_BOLD); attron(A_BOLD);
if (uart_status == TRUE) if (flags.uart_status == TRUE)
{ {
mvprintw(y_max - 1, 0, "RFID reader status: "); mvprintw(y_max - 1, 0, "RFID reader status: ");
attron(COLOR_PAIR(3)); attron(COLOR_PAIR(3));
@@ -108,6 +114,8 @@ int main(int argc, char const **argv)
mainMenu(&person_list, argv[1], x_max, y_max); mainMenu(&person_list, argv[1], x_max, y_max);
flags.end_from_main_flag = TRUE;
pthread_join(uart_thread, NULL); pthread_join(uart_thread, NULL);
free(args); free(args);
+28 -7
View File
@@ -17,17 +17,18 @@ void *uartListener(void *arg)
char *uart_address = thread_args->uart_address; char *uart_address = thread_args->uart_address;
list_t *person_list = thread_args->person_list_head; list_t *person_list = thread_args->person_list_head;
BYTE *uart_status = thread_args->uart_status; UartThreadFlags *flags = thread_args->flags;
flags->wait_for_UART_flag = TRUE;
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)
{ {
//perror("Failed to open UART"); // perror("Failed to open UART");
*uart_status = FALSE; flags->wait_for_UART_flag = FALSE;
flags->uart_status = FALSE;
return NULL; return NULL;
} }
*uart_status = TRUE;
struct termios options; struct termios options;
tcgetattr(uart, &options); tcgetattr(uart, &options);
@@ -49,10 +50,14 @@ void *uartListener(void *arg)
if (tcsetattr(uart, TCSANOW, &options) != 0) if (tcsetattr(uart, TCSANOW, &options) != 0)
{ {
perror("tcsetattr"); flags->wait_for_UART_flag = FALSE;
exit(EXIT_FAILURE); flags->uart_status = FALSE;
return NULL;
} }
flags->uart_status = TRUE;
flags->wait_for_UART_flag = FALSE;
enum enum
{ {
SYNC0, SYNC0,
@@ -64,13 +69,29 @@ void *uartListener(void *arg)
while (1) while (1)
{ {
if (flags->end_from_main_flag)
{
close(uart);
return NULL;
}
uint8_t byte; uint8_t byte;
ssize_t n = read(uart, &byte, 1); ssize_t n = read(uart, &byte, 1);
if (n == -1) if (n == -1)
{ {
if (errno == EAGAIN || errno == EWOULDBLOCK)
{
usleep(1000);
continue;
}
perror("read"); perror("read");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (n == 0)
{
usleep(1000);
continue;
}
switch (state) switch (state)
{ {
+10
View File
@@ -687,3 +687,13 @@ int exportDialog(list_t *_list, int terminal_x, int terminal_y)
void editDatabaseMenu(list_t *_person_list, int terminal_x, int terminal_y) void editDatabaseMenu(list_t *_person_list, int terminal_x, int terminal_y)
{ {
} }
void addPersonDialog(list_t *_person_list, int terminal_x, int terminal_y)
{
}
void deletePersonDialog(list_t *_person_list, int terminal_x, int terminal_y)
{
}