implement credentials menu
This commit is contained in:
parent
305eab7151
commit
afabf5ee74
|
@ -73,7 +73,7 @@ static bool credentials = false; // if the credentials are valid
|
|||
enum {
|
||||
MENU_HOME,
|
||||
MENU_USER,
|
||||
MENU_PWD,
|
||||
MENU_PASS,
|
||||
MENU_BUTTON,
|
||||
MENU_ENTER_MANUF,
|
||||
MENU_ENTER_USER,
|
||||
|
@ -175,15 +175,80 @@ void cdc_task(void) {
|
|||
if (tud_cdc_available()) {
|
||||
// read data
|
||||
char buf[CFG_TUD_CDC_RX_BUFSIZE];
|
||||
uint32_t count = tud_cdc_read(buf, sizeof(buf));
|
||||
(void) count;
|
||||
const uint32_t count = tud_cdc_read(buf, sizeof(buf));
|
||||
bool echo = true; // if we echo back input
|
||||
if (0 == count) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Echo back
|
||||
// Note: Skip echo by commenting out write() and write_flush()
|
||||
// for throughput test e.g
|
||||
// $ dd if=/dev/zero of=/dev/ttyACM0 count=10000
|
||||
tud_cdc_write(buf, count);
|
||||
tud_cdc_write_flush();
|
||||
const char* str = NULL; // message to print
|
||||
static uint16_t i = 0; // generic array index
|
||||
switch (menu) {
|
||||
case MENU_HOME:
|
||||
switch (buf[0]) {
|
||||
case 'c':
|
||||
i = 0; // reset index
|
||||
menu = MENU_USER; // go to corresponding menu
|
||||
str = "\r\nusername: ";
|
||||
case '\r':
|
||||
case '\n':
|
||||
break; // nothing to do
|
||||
case 'h':
|
||||
default:
|
||||
str = "\r\npress key to enter menu\r\nh help\r\nc enter credentials\r\n";
|
||||
}
|
||||
break;
|
||||
case MENU_USER:
|
||||
//echo = false; // keep secret
|
||||
for (uint16_t j = 0; j < count; j++) {
|
||||
if ('\r' == buf[0] || '\n' == buf[0]) { // end received
|
||||
username[i] = 0; // end username
|
||||
i = 0; // reset index
|
||||
menu = MENU_PASS; // go to next menu
|
||||
str = "\r\npassword: ";
|
||||
} else if (i >= sizeof(username) - 2) {
|
||||
memset(username, 0, sizeof(username)); // clear username
|
||||
str = "\r\nlimit reached\r\n";
|
||||
} else {
|
||||
username[i++] = buf[j]; // save username
|
||||
}
|
||||
}
|
||||
break;
|
||||
case MENU_PASS:
|
||||
echo = false; // keep secret
|
||||
for (uint16_t j = 0; j < count; j++) {
|
||||
if ('\r' == buf[0] || '\n' == buf[0]) { // end received
|
||||
password[i] = 0; // end password
|
||||
if (strlen(username) && strlen(password)) {
|
||||
credentials = true;
|
||||
str = "\r\ncredentials saved\r\n";
|
||||
} else {
|
||||
credentials = false;
|
||||
str = "\r\ninvalid credentials\r\n";
|
||||
}
|
||||
i = 0; // reset index
|
||||
menu = MENU_HOME; // go to next menu
|
||||
} else if (i >= sizeof(password) - 2) {
|
||||
memset(password, 0, sizeof(password)); // clear password
|
||||
str = "\r\nlimit reached\r\n";
|
||||
} else {
|
||||
password[i++] = buf[j]; // save password
|
||||
}
|
||||
}
|
||||
break;
|
||||
default: // unknown menu
|
||||
menu = MENU_HOME; // revert to home
|
||||
}
|
||||
|
||||
if (echo) {
|
||||
tud_cdc_write(buf, count);
|
||||
}
|
||||
if (str) {
|
||||
tud_cdc_write((const uint8_t*)str, strlen(str));
|
||||
}
|
||||
if (echo || str) {
|
||||
tud_cdc_write_flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -287,8 +352,16 @@ void hid_task(void)
|
|||
}
|
||||
|
||||
const char* no_cred = "no credentials saved\r\n";
|
||||
// check which button is released
|
||||
if (!button1_pressed && button1_ok && !button2_ok) { // button 1 released
|
||||
// check which button is pressed/released
|
||||
if (button1_pressed && button2_pressed && button1_ok && button2_ok) { // two buttons pressed
|
||||
if (credentials) {
|
||||
memset(username, 0, sizeof(username));
|
||||
memset(password, 0, sizeof(password));
|
||||
credentials = false;
|
||||
const char* str = "clearing credentials\r\n";
|
||||
board_uart_write((const uint8_t*)str, strlen(str));
|
||||
}
|
||||
} else if (!button1_pressed && button1_ok && !button2_ok) { // button 1 released
|
||||
if (!credentials) {
|
||||
board_uart_write((const uint8_t*)no_cred, strlen(no_cred));
|
||||
} else {
|
||||
|
@ -302,17 +375,7 @@ void hid_task(void)
|
|||
const char* str = "pasting password\r\n";
|
||||
board_uart_write((const uint8_t*)str, strlen(str));
|
||||
}
|
||||
} else if ((!button1_pressed || !button2_pressed) && button1_ok && button2_ok) { // one of two buttons released
|
||||
if (!credentials) {
|
||||
board_uart_write((const uint8_t*)no_cred, strlen(no_cred));
|
||||
} else {
|
||||
memset(username, 0, sizeof(username));
|
||||
memset(password, 0, sizeof(password));
|
||||
credentials = false;
|
||||
const char* str = "clearing credentials\n";
|
||||
board_uart_write((const uint8_t*)str, strlen(str));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// clear button state
|
||||
if (!button1_pressed) {
|
||||
|
|
Loading…
Reference in New Issue