From 1db10e1f07b7c76de00ff21b4ceba1ce014b9689 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Sun, 14 Apr 2024 05:53:56 +0200 Subject: [PATCH] add timeout menu --- examples/device/hid_cdc_passkey/src/main.c | 77 ++++++++++++++++++++-- 1 file changed, 71 insertions(+), 6 deletions(-) diff --git a/examples/device/hid_cdc_passkey/src/main.c b/examples/device/hid_cdc_passkey/src/main.c index 688e8af54..a484abc2a 100644 --- a/examples/device/hid_cdc_passkey/src/main.c +++ b/examples/device/hid_cdc_passkey/src/main.c @@ -104,11 +104,13 @@ static void board_supplement_init(void) HAL_GPIO_Init(BUTTON2_PORT, &GPIO_InitStruct); } -uint32_t board_button1_read(void) { +uint32_t board_button1_read(void) +{ return BUTTON1_STATE_ACTIVE == HAL_GPIO_ReadPin(BUTTON1_PORT, BUTTON1_PIN); } -uint32_t board_button2_read(void) { +uint32_t board_button2_read(void) +{ return BUTTON2_STATE_ACTIVE == HAL_GPIO_ReadPin(BUTTON2_PORT, BUTTON2_PIN); } @@ -188,8 +190,8 @@ int main(void) tud_task(); // tinyusb device task // check for credential timeout - const uint32_t minutes = board_millis() / 1000 / 60; - if (credentials && ((minutes > entered_when + config.timeout_global) || (minutes > entered_when + TIMEOUT_GLOBAL))) { + const uint32_t minutes = board_millis(); + if (credentials && ((minutes > entered_when + config.timeout_global * 1000 * 60) || (minutes > entered_when + TIMEOUT_GLOBAL * 1000 * 60))) { const char str[] = "global timeout reached, clearing cedentials\r\n"; tud_cdc_write_str(str); tud_cdc_write_flush(); @@ -201,7 +203,7 @@ int main(void) pasted_when = 0; entered_when = 0; } - if (credentials && ((minutes > pasted_when + config.timeout_repeat) || (minutes > pasted_when + TIMEOUT_REPEAT))) { + if (credentials && ((minutes > pasted_when + config.timeout_repeat * 1000 * 60) || (minutes > pasted_when + TIMEOUT_REPEAT * 1000 * 60))) { const char str[] = "repeat timeout reached, clearing cedentials\r\n"; tud_cdc_write_str(str); tud_cdc_write_flush(); @@ -256,6 +258,8 @@ void tud_resume_cb(void) //--------------------------------------------------------------------+ void cdc_task(void) { + static char tmp[64]; + // connected() check for DTR bit // Most but not all terminal client set this when making connection // if ( tud_cdc_connected() ) @@ -285,6 +289,16 @@ void cdc_task(void) str = "\r\nbuttons swapped\r\n"; save_config(); break; + case 'g': // set global timeout + i = 0; // reset index + menu = MENU_TIMEOUT_GLOBAL; + str = "\r\nenter global timeout in minutes: "; + break; + case 'r': // set repeat timeout + i = 0; // reset index + menu = MENU_TIMEOUT_REPEAT; + str = "\r\nenter repeat timeout in minutes: "; + break; case '\r': case '\n': break; // nothing to do @@ -293,7 +307,10 @@ void cdc_task(void) str = "\r\npress key to enter menu\r\n" \ "h help\r\n" \ "c enter credentials\r\n" \ - "b swap buttons\r\n"; + "b swap buttons\r\n" \ + "r set repeat timeout\r\n" \ + "g set global timeout\r\n" \ + ; } break; case MENU_USER: @@ -336,6 +353,54 @@ void cdc_task(void) } } break; + case MENU_TIMEOUT_GLOBAL: + for (uint16_t j = 0; j < count; j++) { + if ('\r' == buf[0] || '\n' == buf[0]) { // end received + tmp[i] = 0; // end time input + const uint16_t time = atoi(tmp); + if (strlen(tmp) > 0 && time > 0) { + config.timeout_global = time; + } + if (config.timeout_global > TIMEOUT_GLOBAL) { + config.timeout_global = TIMEOUT_GLOBAL; + } + snprintf(tmp, sizeof(tmp), "\r\nglobal timeout set to %u minutes\r\n", config.timeout_global); + str = tmp; + i = 0; // reset index + save_config(); + menu = MENU_HOME; // go to next menu + } else if (i >= sizeof(tmp) - 2) { + memset(tmp, 0, sizeof(tmp)); // clear password + str = "\r\nlimit reached\r\n"; + } else { + tmp[i++] = buf[j]; // save password + } + } + break; + case MENU_TIMEOUT_REPEAT: + for (uint16_t j = 0; j < count; j++) { + if ('\r' == buf[0] || '\n' == buf[0]) { // end received + tmp[i] = 0; // end time input + const uint16_t time = atoi(tmp); + if (strlen(tmp) > 0 && time > 0) { + config.timeout_repeat = time; + } + if (config.timeout_repeat > TIMEOUT_REPEAT) { + config.timeout_repeat = TIMEOUT_REPEAT; + } + snprintf(tmp, sizeof(tmp), "\r\nrepeat timeout set to %u minutes\r\n", config.timeout_repeat); + str = tmp; + i = 0; // reset index + save_config(); + menu = MENU_HOME; // go to next menu + } else if (i >= sizeof(tmp) - 2) { + memset(tmp, 0, sizeof(tmp)); // clear password + str = "\r\nlimit reached\r\n"; + } else { + tmp[i++] = buf[j]; // save password + } + } + break; default: // unknown menu menu = MENU_HOME; // revert to home }