From 90e77abe08c4c19705200261f772972c2d033b0b Mon Sep 17 00:00:00 2001 From: hugo LAMY Date: Sun, 9 Mar 2025 03:00:00 +0100 Subject: [PATCH] nimp --- module02/ex04/main.c | 64 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/module02/ex04/main.c b/module02/ex04/main.c index bfe8496..16ac863 100644 --- a/module02/ex04/main.c +++ b/module02/ex04/main.c @@ -36,6 +36,9 @@ typedef enum { } State; volatile char username[] = "admin"; volatile char password[] = "password"; +volatile char username_input[100] = {0}; +volatile char password_input[100] = {0}; + volatile int input_index = 0; volatile State state = USERNAME; volatile int input_match = FALSE; @@ -79,12 +82,20 @@ ISR(USART_RX_vect) { // Table 12-7 : we select the code for USART uart_tx('\b'); // Move cursor back again } else if (received_char == '\n' || received_char == '\r') { // if enter is received if (state == USERNAME) { - if (input_index == STRLEN_LITERAL(username) && input_match) { + // if (input_index == STRLEN_LITERAL(username) && input_match) { + // username_correct = TRUE; + // } else { + // username_correct = FALSE; + // uart_printstr("(error1)"); + // } + + if (compare_str(username_input, username, STRLEN_LITERAL(username))) { username_correct = TRUE; } else { username_correct = FALSE; uart_printstr("(error1)"); } + input_match = FALSE; state = PASSWORD; input_index = 0; @@ -109,18 +120,65 @@ ISR(USART_RX_vect) { // Table 12-7 : we select the code for USART on_success(); } } else { + fill_str(received_char); if (state == USERNAME) { - check_str(received_char, username, STRLEN_LITERAL(username)); + // check_str(received_char, username, STRLEN_LITERAL(username)); uart_tx(received_char); } else if (state == PASSWORD) { - check_str(received_char, password, STRLEN_LITERAL(password)); + // check_str(received_char, password, STRLEN_LITERAL(password)); uart_tx('*'); } input_index++; } } +void fill_str(char input) { + if (input_index >= 100) { + uart_printstr("(error8)"); + return; + } + if (state == USERNAME) { + username_input[input_index] = input; + } else if (state == PASSWORD) { + password_input[input_index] = input; + } +} + +int compare_str() { + char input_str[100] = {0}; + char compare_str[100] = {0}; + if (state == USERNAME) { + compare_str = username; + input_str = username_input; + } else if (state == PASSWORD) { + compare_str = password; + input_str = password_input; + } + int length = STRLEN_LITERAL(compare_str); + int index = 0; + + while (*compare_str) { + if (index >= length) { + uart_printstr("(error5)"); + return FALSE; + } + if (input_str[index] != *compare_str) { + uart_printstr("(error6)"); + return FALSE; + } + compare_str++; + index++; + } + + for (int i = 0; i < length; i++) { + if (input[i] != compare_str[i]) { + return FALSE; + } + } + return TRUE; +} + void check_str(char input, volatile char compare_str[], unsigned int length) { if (input_index >= length) { // index has not already been incremented, so we need to compare greater or equal uart_printstr("(error5)");