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
#define PERIOD 2000
#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
@@ -22,7 +21,10 @@ void uart_init();
// char uart_rx(void);
void uart_tx(char c);
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 ask_for_username();
void ask_for_password();
@@ -38,10 +40,8 @@ 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;
volatile int username_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
if (received_char == '\b' || received_char == 127) { // if backspace is received
if (input_index <= 0) return;
remove_last_character();
input_index--;
uart_tx('\b'); // Move cursor back
uart_tx(' '); // Erase the character on screen
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) {
// username_correct = TRUE;
// } else {
// username_correct = FALSE;
// uart_printstr("(error1)");
// }
if (compare_str(username_input, username, STRLEN_LITERAL(username))) {
if (compare_str(username_input, username)) {
username_correct = TRUE;
} else {
username_correct = FALSE;
uart_printstr("(error1)");
// uart_printstr("(error1)");
}
input_match = FALSE;
state = PASSWORD;
input_index = 0;
uart_tx('\r'); // Move cursor to next line
uart_tx('\r');
uart_tx('\n');
ask_for_password();
} else if (state == PASSWORD) {
if (!username_correct) {
uart_printstr("(error2)");
// uart_printstr("(error2)");
return reset_message();
}
if (input_index != STRLEN_LITERAL(password)) {
uart_printstr("(error3)");
if (!compare_str(password_input, password)) {
// uart_printstr("(error3)");
return reset_message();
}
if (!input_match) {
uart_printstr("(error4)");
return reset_message();
}
uart_tx('\r'); // Move cursor to next line
uart_tx('\r');
uart_tx('\n');
on_success();
}
} else {
fill_str(received_char);
if (state == 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));
uart_tx('*');
}
input_index++;
@@ -135,7 +121,7 @@ ISR(USART_RX_vect) { // Table 12-7 : we select the code for USART
void fill_str(char input) {
if (input_index >= 100) {
uart_printstr("(error8)");
// uart_printstr("(error4)");
return;
}
if (state == USERNAME) {
@@ -145,72 +131,49 @@ void fill_str(char input) {
}
}
int compare_str() {
char input_str[100] = {0};
char compare_str[100] = {0};
void remove_last_character() {
if (state == USERNAME) {
compare_str = username;
input_str = username_input;
username_input[input_index] = 0;
} else if (state == PASSWORD) {
compare_str = password;
input_str = password_input;
password_input[input_index] = 0;
}
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++;
int compare_str(volatile char *input_str, volatile char *base_str) {
int base_length = strlength(base_str);
int input_length = strlength(input_str);
if (base_length != input_length) {
// uart_printstr("(error5)");
return FALSE;
}
for (int i = 0; i < length; i++) {
if (input[i] != compare_str[i]) {
for(int i = 0; i < base_length; i++) {
if (input_str[i] != base_str[i]) {
// uart_printstr("(error6)");
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)");
input_match = FALSE;
return;
int strlength(volatile char *str) {
int length = 0;
while (*str) {
length++;
str++;
}
if (input != compare_str[input_index]) {
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;
return length;
}
void reset_message() {
for(int i = 0; i < 100; i++) {
username_input[i] = 0;
password_input[i] = 0;
}
input_index = 0;
input_match = FALSE;
username_correct = FALSE;
password_correct = FALSE;
state = USERNAME;
uart_tx('\r'); // Move cursor to next line
uart_tx('\r');
uart_tx('\n');
on_error();
ask_for_username();