mod02 bonus works

This commit is contained in:
hugo LAMY
2025-03-09 11:18:24 +01:00
parent 90e77abe08
commit 00f1670c14

View File

@@ -13,7 +13,6 @@
// TIMER // TIMER
#define PERIOD 2000 #define PERIOD 2000
#define PRESCALE_VALUE 1024 // can be 1, 8, 64, 256, 1024 #define PRESCALE_VALUE 1024 // can be 1, 8, 64, 256, 1024
#define STRLEN_LITERAL(str) (sizeof(str) / sizeof(str[0]) - 1) // -1 to remove the null terminator, only works with string literals, not with pointers
// END MACROS // END MACROS
@@ -22,7 +21,10 @@ void uart_init();
// char uart_rx(void); // char uart_rx(void);
void uart_tx(char c); void uart_tx(char c);
void uart_printstr(const char* str); void uart_printstr(const char* str);
void check_str(char input, volatile char compare_str[], unsigned int length); void remove_last_character();
void fill_str(char input);
int compare_str(volatile char *input_str, volatile char *base_str);
int strlength(volatile char *str);
void reset_message(); void reset_message();
void ask_for_username(); void ask_for_username();
void ask_for_password(); void ask_for_password();
@@ -38,10 +40,8 @@ volatile char username[] = "admin";
volatile char password[] = "password"; volatile char password[] = "password";
volatile char username_input[100] = {0}; volatile char username_input[100] = {0};
volatile char password_input[100] = {0}; volatile char password_input[100] = {0};
volatile int input_index = 0; volatile int input_index = 0;
volatile State state = USERNAME; volatile State state = USERNAME;
volatile int input_match = FALSE;
volatile int username_correct = FALSE; volatile int username_correct = FALSE;
volatile int password_correct = FALSE; volatile int password_correct = FALSE;
@@ -76,57 +76,43 @@ ISR(USART_RX_vect) { // Table 12-7 : we select the code for USART
char received_char = UDR0; // read received character char received_char = UDR0; // read received character
if (received_char == '\b' || received_char == 127) { // if backspace is received if (received_char == '\b' || received_char == 127) { // if backspace is received
if (input_index <= 0) return; if (input_index <= 0) return;
remove_last_character();
input_index--; input_index--;
uart_tx('\b'); // Move cursor back uart_tx('\b'); // Move cursor back
uart_tx(' '); // Erase the character on screen uart_tx(' '); // Erase the character on screen
uart_tx('\b'); // Move cursor back again uart_tx('\b'); // Move cursor back again
} else if (received_char == '\n' || received_char == '\r') { // if enter is received } else if (received_char == '\n' || received_char == '\r') { // if enter is received
if (state == USERNAME) { if (state == USERNAME) {
// if (input_index == STRLEN_LITERAL(username) && input_match) { if (compare_str(username_input, username)) {
// username_correct = TRUE;
// } else {
// username_correct = FALSE;
// uart_printstr("(error1)");
// }
if (compare_str(username_input, username, STRLEN_LITERAL(username))) {
username_correct = TRUE; username_correct = TRUE;
} else { } else {
username_correct = FALSE; username_correct = FALSE;
uart_printstr("(error1)"); // uart_printstr("(error1)");
} }
input_match = FALSE;
state = PASSWORD; state = PASSWORD;
input_index = 0; input_index = 0;
uart_tx('\r'); // Move cursor to next line uart_tx('\r');
uart_tx('\n'); uart_tx('\n');
ask_for_password(); ask_for_password();
} else if (state == PASSWORD) { } else if (state == PASSWORD) {
if (!username_correct) { if (!username_correct) {
uart_printstr("(error2)"); // uart_printstr("(error2)");
return reset_message(); return reset_message();
} }
if (input_index != STRLEN_LITERAL(password)) { if (!compare_str(password_input, password)) {
uart_printstr("(error3)"); // uart_printstr("(error3)");
return reset_message(); return reset_message();
} }
if (!input_match) { uart_tx('\r');
uart_printstr("(error4)");
return reset_message();
}
uart_tx('\r'); // Move cursor to next line
uart_tx('\n'); uart_tx('\n');
on_success(); on_success();
} }
} else { } else {
fill_str(received_char); fill_str(received_char);
if (state == USERNAME) { if (state == USERNAME) {
// check_str(received_char, username, STRLEN_LITERAL(username));
uart_tx(received_char); uart_tx(received_char);
} }
else if (state == PASSWORD) { else if (state == PASSWORD) {
// check_str(received_char, password, STRLEN_LITERAL(password));
uart_tx('*'); uart_tx('*');
} }
input_index++; input_index++;
@@ -135,7 +121,7 @@ ISR(USART_RX_vect) { // Table 12-7 : we select the code for USART
void fill_str(char input) { void fill_str(char input) {
if (input_index >= 100) { if (input_index >= 100) {
uart_printstr("(error8)"); // uart_printstr("(error4)");
return; return;
} }
if (state == USERNAME) { if (state == USERNAME) {
@@ -145,72 +131,49 @@ void fill_str(char input) {
} }
} }
int compare_str() { void remove_last_character() {
char input_str[100] = {0};
char compare_str[100] = {0};
if (state == USERNAME) { if (state == USERNAME) {
compare_str = username; username_input[input_index] = 0;
input_str = username_input;
} else if (state == PASSWORD) { } else if (state == PASSWORD) {
compare_str = password; password_input[input_index] = 0;
input_str = password_input;
} }
int length = STRLEN_LITERAL(compare_str); }
int index = 0;
while (*compare_str) { int compare_str(volatile char *input_str, volatile char *base_str) {
if (index >= length) { int base_length = strlength(base_str);
uart_printstr("(error5)"); int input_length = strlength(input_str);
return FALSE; if (base_length != input_length) {
} // uart_printstr("(error5)");
if (input_str[index] != *compare_str) { return FALSE;
uart_printstr("(error6)");
return FALSE;
}
compare_str++;
index++;
} }
for(int i = 0; i < base_length; i++) {
for (int i = 0; i < length; i++) { if (input_str[i] != base_str[i]) {
if (input[i] != compare_str[i]) { // uart_printstr("(error6)");
return FALSE; return FALSE;
} }
} }
return TRUE; return TRUE;
} }
void check_str(char input, volatile char compare_str[], unsigned int length) { int strlength(volatile char *str) {
if (input_index >= length) { // index has not already been incremented, so we need to compare greater or equal int length = 0;
uart_printstr("(error5)"); while (*str) {
input_match = FALSE; length++;
return; str++;
} }
if (input != compare_str[input_index]) { return length;
uart_printstr("(error6)");
input_match = FALSE;
return;
}
// if we reach this point, the input character matches the compare_str character
if (input_index == 0) {
input_match = TRUE;
return;
}
// if we reach this point, the input character is not the first character of the compare_str
if (input_match == FALSE) {
// if any previous character did not match, we don't need to check the next character
uart_printstr("(error7)");
return;
}
input_match = TRUE;
} }
void reset_message() { void reset_message() {
for(int i = 0; i < 100; i++) {
username_input[i] = 0;
password_input[i] = 0;
}
input_index = 0; input_index = 0;
input_match = FALSE;
username_correct = FALSE; username_correct = FALSE;
password_correct = FALSE; password_correct = FALSE;
state = USERNAME; state = USERNAME;
uart_tx('\r'); // Move cursor to next line uart_tx('\r');
uart_tx('\n'); uart_tx('\n');
on_error(); on_error();
ask_for_username(); ask_for_username();