diff --git a/include/rfid_handler.h b/include/rfid_handler.h index e5d796e..1e413b3 100644 --- a/include/rfid_handler.h +++ b/include/rfid_handler.h @@ -4,22 +4,26 @@ #include "main.h" #include "users.h" +typedef struct +{ + BYTE wait_for_UART_flag; + BYTE end_from_main_flag; + BYTE uart_status; +} UartThreadFlags; + typedef struct { char *uart_address; - BYTE *uart_status; + list_t *person_list_head; + UartThreadFlags *flags; } UartThreadArgs; - - // UART handler thread void *uartListener(void *arg); uint16_t parseIncommingPacket(BYTE *_packet); -void sendPacketToDevice(BYTE * _packet); +void sendPacketToDevice(BYTE *_packet); #endif - - diff --git a/include/tui.h b/include/tui.h index 43620f6..afdb1fe 100644 --- a/include/tui.h +++ b/include/tui.h @@ -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 addPersonDialog(list_t *_person_list, int terminal_x, int terminal_y); + +void deletePersonDialog(list_t *_person_list, int terminal_x, int terminal_y); + #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 99ddb5e..1dd3ade 100644 --- a/src/main.c +++ b/src/main.c @@ -22,7 +22,6 @@ int main(int argc, char const **argv) } int y_max, x_max; - BYTE uart_status = FALSE; char uart_address[51]; pthread_t uart_thread; @@ -79,17 +78,24 @@ int main(int argc, char const **argv) 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)); args->uart_address = uart_address; args->person_list_head = &person_list; - args->uart_status = &uart_status; + args->flags = &flags; pthread_create(&uart_thread, NULL, uartListener, args); - usleep(1000); + while (flags.wait_for_UART_flag) + ; + + usleep(50000); attron(A_BOLD); - if (uart_status == TRUE) + if (flags.uart_status == TRUE) { mvprintw(y_max - 1, 0, "RFID reader status: "); attron(COLOR_PAIR(3)); @@ -108,6 +114,8 @@ int main(int argc, char const **argv) mainMenu(&person_list, argv[1], x_max, y_max); + flags.end_from_main_flag = TRUE; + pthread_join(uart_thread, NULL); free(args); diff --git a/src/rfid_handler.c b/src/rfid_handler.c index cabd614..c85796e 100644 --- a/src/rfid_handler.c +++ b/src/rfid_handler.c @@ -17,17 +17,18 @@ void *uartListener(void *arg) char *uart_address = thread_args->uart_address; 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); if (uart == -1) { - //perror("Failed to open UART"); - *uart_status = FALSE; + // perror("Failed to open UART"); + flags->wait_for_UART_flag = FALSE; + flags->uart_status = FALSE; return NULL; } - *uart_status = TRUE; - struct termios options; tcgetattr(uart, &options); @@ -49,10 +50,14 @@ void *uartListener(void *arg) if (tcsetattr(uart, TCSANOW, &options) != 0) { - perror("tcsetattr"); - exit(EXIT_FAILURE); + flags->wait_for_UART_flag = FALSE; + flags->uart_status = FALSE; + return NULL; } + flags->uart_status = TRUE; + flags->wait_for_UART_flag = FALSE; + enum { SYNC0, @@ -64,13 +69,29 @@ void *uartListener(void *arg) while (1) { + if (flags->end_from_main_flag) + { + close(uart); + return NULL; + } + uint8_t byte; ssize_t n = read(uart, &byte, 1); if (n == -1) { + if (errno == EAGAIN || errno == EWOULDBLOCK) + { + usleep(1000); + continue; + } perror("read"); exit(EXIT_FAILURE); } + if (n == 0) + { + usleep(1000); + continue; + } switch (state) { diff --git a/src/tui.c b/src/tui.c index 53ab4cb..f3dfb7a 100644 --- a/src/tui.c +++ b/src/tui.c @@ -686,4 +686,14 @@ int exportDialog(list_t *_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) +{ + } \ No newline at end of file