feat(ex04)
This commit is contained in:
parent
5a954c3713
commit
87ba742807
3 changed files with 163 additions and 20 deletions
|
|
@ -7,7 +7,12 @@ void uart_init(void);
|
|||
|
||||
void uart_tx(char data);
|
||||
void uart_sendstring(const char* str);
|
||||
|
||||
void uart_send_u8(uint8_t val);
|
||||
void uart_send_u16(uint16_t val);
|
||||
|
||||
void uart_send_u8_hex(uint8_t val);
|
||||
void uart_send_u16_hex(uint16_t val);
|
||||
|
||||
char uart_rx(void);
|
||||
|
||||
|
|
|
|||
115
ex04/src/main.c
115
ex04/src/main.c
|
|
@ -6,7 +6,7 @@
|
|||
#include "uart.h"
|
||||
#include "utils.h"
|
||||
|
||||
#define BUFFERSIZE 20
|
||||
#define BUFFERSIZE 32
|
||||
|
||||
enum State {
|
||||
ASK_USERNAME,
|
||||
|
|
@ -22,33 +22,61 @@ enum State {
|
|||
INVALID_PASSWORD,
|
||||
|
||||
OVERFLOW_INPUT,
|
||||
|
||||
RESTART,
|
||||
};
|
||||
|
||||
const char username[] = "maix";
|
||||
const char password[] = "securePassword1234";
|
||||
|
||||
#define D1 PB0
|
||||
#define D2 PB1
|
||||
#define D3 PB2
|
||||
#define D4 PB4
|
||||
|
||||
#define LED_MASK (_BV(D1) | _BV(D2) | _BV(D3) | _BV(D4))
|
||||
|
||||
void valid_password(void);
|
||||
void invalid_password(void);
|
||||
|
||||
int main(void) {
|
||||
uart_init();
|
||||
DDRB |= _BV(D1) | _BV(D2) | _BV(D3) | _BV(D4);
|
||||
// enable the S_REG
|
||||
// SREG |= _BV(SREG_I);
|
||||
char buffer[BUFFERSIZE] = {};
|
||||
uint8_t cursor = 0;
|
||||
enum State state = ASK_USERNAME;
|
||||
|
||||
bool is_username_valid = false;
|
||||
bool is_password_valid = false;
|
||||
|
||||
while (true) {
|
||||
switch (state) {
|
||||
case ASK_USERNAME: {
|
||||
uart_sendstring("\r\nUsername: ");
|
||||
uart_sendstring("Username: ");
|
||||
state = ASKING_USERNAME;
|
||||
cursor = 0;
|
||||
ft_bzero(&buffer, sizeof(buffer));
|
||||
is_username_valid = false;
|
||||
break;
|
||||
}
|
||||
case ASK_PASSWORD: {
|
||||
uart_sendstring("Password: ");
|
||||
state = ASKING_PASSWORD;
|
||||
cursor = 0;
|
||||
ft_bzero(&buffer, sizeof(buffer));
|
||||
is_password_valid = false;
|
||||
break;
|
||||
}
|
||||
|
||||
case OVERFLOW_INPUT: {
|
||||
uart_sendstring("\r\n/!\\OVERFLOW DETECTED /!\\\r\n/!\\RESETING/!\\\r\n");
|
||||
ft_bzero(&buffer, sizeof(buffer));
|
||||
cursor = 0;
|
||||
state = ASK_USERNAME;
|
||||
cursor = 0;
|
||||
state = ASK_USERNAME;
|
||||
is_username_valid = false;
|
||||
is_password_valid = false;
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
@ -78,28 +106,97 @@ int main(void) {
|
|||
}
|
||||
|
||||
case ASKING_PASSWORD: {
|
||||
char data = uart_rx();
|
||||
if (data == '\r') {
|
||||
state = VERIFY_PASSWORD;
|
||||
uart_sendstring("\r\n");
|
||||
continue;
|
||||
}
|
||||
if (data == '\x7F') {
|
||||
if (cursor) {
|
||||
uart_sendstring("\b \b");
|
||||
cursor--;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
uart_tx('*');
|
||||
|
||||
if (cursor + 1 >= BUFFERSIZE) {
|
||||
state = OVERFLOW_INPUT;
|
||||
continue;
|
||||
}
|
||||
|
||||
buffer[cursor++] = data;
|
||||
break;
|
||||
}
|
||||
|
||||
case VERIFY_USERNAME: {
|
||||
uart_sendstring("\r\n");
|
||||
uart_sendstring(buffer);
|
||||
cursor = 0;
|
||||
ft_bzero(buffer, sizeof(buffer));
|
||||
state = ASK_USERNAME;
|
||||
cursor = 0;
|
||||
state = ASK_PASSWORD;
|
||||
uint8_t i = 0;
|
||||
for (i = 0; username[i] && buffer[i] == username[i] && i < BUFFERSIZE; i++)
|
||||
;
|
||||
is_username_valid = username[i] == 0;
|
||||
|
||||
break;
|
||||
}
|
||||
case VERIFY_PASSWORD: {
|
||||
cursor = 0;
|
||||
uint8_t i = 0;
|
||||
for (i = 0; password[i] && buffer[i] == password[i] && i < BUFFERSIZE; i++)
|
||||
;
|
||||
is_password_valid = password[i] == 0;
|
||||
if (is_password_valid && is_username_valid)
|
||||
state = VALID_PASSWORD;
|
||||
else
|
||||
state = INVALID_PASSWORD;
|
||||
break;
|
||||
}
|
||||
|
||||
case VALID_PASSWORD: {
|
||||
valid_password();
|
||||
state = RESTART;
|
||||
break;
|
||||
}
|
||||
|
||||
case INVALID_PASSWORD: {
|
||||
invalid_password();
|
||||
state = RESTART;
|
||||
break;
|
||||
}
|
||||
|
||||
case RESTART: {
|
||||
uart_sendstring("Press any key to restart\r\n");
|
||||
(void)uart_rx();
|
||||
PORTB &= ~LED_MASK;
|
||||
state = ASK_USERNAME;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void valid_password(void) {
|
||||
uint8_t leds[4] = {_BV(D1), _BV(D2), _BV(D3), _BV(D4)};
|
||||
|
||||
for (uint8_t i = 0; i < 5; i++) {
|
||||
for (uint8_t j = 0; j < 4; j++) {
|
||||
PORTB = (PORTB & ~LED_MASK) | leds[j];
|
||||
delay_ms(150);
|
||||
}
|
||||
for (uint8_t j = 0; j < 4; j++) {
|
||||
PORTB = (PORTB & ~LED_MASK) | leds[3 - j];
|
||||
delay_ms(150);
|
||||
}
|
||||
}
|
||||
uart_sendstring("Congrats you are now inside the mainframe!\r\n");
|
||||
}
|
||||
|
||||
void invalid_password(void) {
|
||||
for (uint8_t i = 0; i < 5; i++) {
|
||||
PORTB &= ~LED_MASK;
|
||||
delay_ms(200);
|
||||
PORTB |= LED_MASK;
|
||||
delay_ms(200);
|
||||
}
|
||||
uart_sendstring("Invalid password !\r\n");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,15 +49,56 @@ void uart_sendstring(const char* str) {
|
|||
}
|
||||
|
||||
void uart_send_u8(uint8_t val) {
|
||||
char buf[4] = {0, 0, 0, 0};
|
||||
buf[0] = '0' + val / 100;
|
||||
val %= 100;
|
||||
buf[1] = '0' + val / 10;
|
||||
buf[2] = '0' + val % 10;
|
||||
if (buf[0] != '0')
|
||||
uart_sendstring(&buf[0]);
|
||||
else if (buf[1] != '0')
|
||||
uart_sendstring(&buf[1]);
|
||||
else if (buf[2] != '0')
|
||||
uart_sendstring(&buf[2]);
|
||||
char buf[4] = {0, 0, 0, 0};
|
||||
uint8_t idx = 0;
|
||||
bool print = false;
|
||||
|
||||
uint8_t modulus = 100;
|
||||
while (modulus) {
|
||||
uint8_t digit = val / modulus;
|
||||
if (print || digit != 0) {
|
||||
print = true;
|
||||
buf[idx++] = '0' + digit;
|
||||
}
|
||||
val %= modulus;
|
||||
modulus /= 10;
|
||||
}
|
||||
|
||||
uart_sendstring(buf);
|
||||
}
|
||||
|
||||
void uart_send_u16(uint16_t val) {
|
||||
char buf[6] = {0, 0, 0, 0, 0, 0};
|
||||
uint8_t idx = 0;
|
||||
bool print = false;
|
||||
|
||||
uint16_t modulus = 10000;
|
||||
while (modulus) {
|
||||
uint8_t digit = val / modulus;
|
||||
if (print || digit != 0) {
|
||||
print = true;
|
||||
buf[idx++] = '0' + digit;
|
||||
}
|
||||
val %= modulus;
|
||||
modulus /= 10;
|
||||
}
|
||||
uart_sendstring(buf);
|
||||
}
|
||||
|
||||
void uart_send_u8_hex(uint8_t val) {
|
||||
char buf[3] = {0, 0, 0};
|
||||
buf[0] = "0123456789abcdef"[(val >> 4) & 0x0F];
|
||||
buf[1] = "0123456789abcdef"[(val >> 0) & 0x0F];
|
||||
|
||||
uart_sendstring(buf);
|
||||
}
|
||||
|
||||
void uart_send_u16_hex(uint16_t val) {
|
||||
char buf[5] = {0, 0, 0, 0, 0};
|
||||
|
||||
buf[0] = "0123456789abcdef"[(val >> 12) & 0x0F];
|
||||
buf[1] = "0123456789abcdef"[(val >> 8) & 0x0F];
|
||||
buf[2] = "0123456789abcdef"[(val >> 4) & 0x0F];
|
||||
buf[3] = "0123456789abcdef"[(val >> 0) & 0x0F];
|
||||
uart_sendstring(buf);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue