diff --git a/cli/cli_helpers.c b/cli/cli_helpers.c index 8cafbdbfcfa..9c524b42625 100644 --- a/cli/cli_helpers.c +++ b/cli/cli_helpers.c @@ -7,14 +7,16 @@ bool totp_cli_ensure_authenticated(const PluginState* plugin_state, Cli* cli) { if(plugin_state->current_scene == TotpSceneAuthentication) { TOTP_CLI_PRINTF("Pleases enter PIN on your flipper device\r\n"); - while(plugin_state->current_scene == TotpSceneAuthentication && + while((plugin_state->current_scene == TotpSceneAuthentication || + plugin_state->current_scene == TotpSceneNone) && !cli_cmd_interrupt_received(cli)) { furi_delay_ms(100); } TOTP_CLI_DELETE_LAST_LINE(); - if(plugin_state->current_scene == TotpSceneAuthentication) { //-V547 + if(plugin_state->current_scene == TotpSceneAuthentication || //-V560 + plugin_state->current_scene == TotpSceneNone) { //-V560 return false; } } diff --git a/cli/commands/add/add.c b/cli/commands/add/add.c index eeb7aed8e88..6b7572bce09 100644 --- a/cli/commands/add/add.c +++ b/cli/commands/add/add.c @@ -51,12 +51,15 @@ void totp_cli_command_add_docopt_options() { DOCOPT_ARGUMENT( TOTP_CLI_COMMAND_ARG_AUTOMATION_FEATURE)) " Token automation features to be enabled. Must be one of: " TOTP_TOKEN_AUTOMATION_FEATURE_NONE_NAME ", " TOTP_TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END_NAME + ", " TOTP_TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END_NAME " " DOCOPT_DEFAULT( TOTP_TOKEN_AUTOMATION_FEATURE_NONE_NAME) "\r\n"); TOTP_CLI_PRINTF(" # " TOTP_TOKEN_AUTOMATION_FEATURE_NONE_NAME " - No features\r\n"); TOTP_CLI_PRINTF(" # " TOTP_TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END_NAME " - Type key at the end of token input automation\r\n"); + TOTP_CLI_PRINTF(" # " TOTP_TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END_NAME + " - Type key at the end of token input automation\r\n"); } void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cli* cli) { diff --git a/cli/commands/details/details.c b/cli/commands/details/details.c index 75cd7030464..0bee99bb7eb 100644 --- a/cli/commands/details/details.c +++ b/cli/commands/details/details.c @@ -7,15 +7,29 @@ #include "../../cli_helpers.h" #include "../../common_command_arguments.h" +#define AUTOMATION_FEATURES_PROPERTY_HEADER "Automation features" + static void print_automation_features(const TokenInfo* token_info) { if(token_info->automation_features == TOKEN_AUTOMATION_FEATURE_NONE) { TOTP_CLI_PRINTF("| %-20s | %-28.28s |\r\n", "Automation features", "None"); return; } + bool header_printed = false; if(token_info->automation_features & TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END) { TOTP_CLI_PRINTF( - "| %-20s | %-28.28s |\r\n", "Automation features", "Type key at the end"); + "| %-20s | %-28.28s |\r\n", + AUTOMATION_FEATURES_PROPERTY_HEADER, + "Type key at the end"); + header_printed = true; + } + + if(token_info->automation_features & TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END) { + TOTP_CLI_PRINTF( + "| %-20s | %-28.28s |\r\n", + header_printed ? "" : AUTOMATION_FEATURES_PROPERTY_HEADER, + "Type key at the end"); + header_printed = true; } } diff --git a/cli/commands/move/move.c b/cli/commands/move/move.c index 3526fb51398..265c7bc64cb 100644 --- a/cli/commands/move/move.c +++ b/cli/commands/move/move.c @@ -54,28 +54,18 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C activate_generate_token_scene = true; } - bool token_updated = false; TokenInfo* token_info = NULL; - if(new_token_index > 0) { - plugin_state->tokens_list = - list_remove_at(plugin_state->tokens_list, token_index - 1, (void**)&token_info); - furi_check(token_info != NULL); - plugin_state->tokens_list = - list_insert_at(plugin_state->tokens_list, new_token_index - 1, token_info); - token_updated = true; - } else { - token_info = list_element_at(plugin_state->tokens_list, token_index - 1)->data; - } + plugin_state->tokens_list = + list_remove_at(plugin_state->tokens_list, token_index - 1, (void**)&token_info); + furi_check(token_info != NULL); + plugin_state->tokens_list = + list_insert_at(plugin_state->tokens_list, new_token_index - 1, token_info); - if(token_updated) { - if(totp_full_save_config_file(plugin_state) == TotpConfigFileUpdateSuccess) { - TOTP_CLI_PRINTF_SUCCESS( - "Token \"%s\" has been successfully updated\r\n", token_info->name); - } else { - TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE(); - } + if(totp_full_save_config_file(plugin_state) == TotpConfigFileUpdateSuccess) { + TOTP_CLI_PRINTF_SUCCESS( + "Token \"%s\" has been successfully updated\r\n", token_info->name); } else { - TOTP_CLI_PRINT_INVALID_ARGUMENTS(); + TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE(); } if(activate_generate_token_scene) { diff --git a/types/token_info.c b/types/token_info.c index 49c0d098c44..19d83448c0d 100644 --- a/types/token_info.c +++ b/types/token_info.c @@ -113,6 +113,11 @@ bool token_info_set_automation_feature_from_str(TokenInfo* token_info, const Fur return true; } + if(furi_string_cmpi_str(str, TOTP_TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END_NAME) == 0) { + token_info->automation_features |= TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END; + return true; + } + if(furi_string_cmpi_str(str, TOTP_TOKEN_AUTOMATION_FEATURE_NONE_NAME) == 0) { token_info->automation_features = TOKEN_AUTOMATION_FEATURE_NONE; return true; diff --git a/types/token_info.h b/types/token_info.h index c4f168eb738..1af9dfe4e42 100644 --- a/types/token_info.h +++ b/types/token_info.h @@ -13,6 +13,7 @@ #define TOTP_TOKEN_AUTOMATION_FEATURE_NONE_NAME "none" #define TOTP_TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END_NAME "enter" +#define TOTP_TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END_NAME "tab" typedef uint8_t TokenHashAlgo; typedef uint8_t TokenDigitsCount; @@ -65,7 +66,12 @@ enum TokenAutomationFeatures { /** * @brief Press "Enter" key at the end as a part of token input automation */ - TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END = 0b01 + TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END = 0b01, + + /** + * @brief Press "Tab" key at the end as a part of token input automation + */ + TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END = 0b10 }; #define TOTP_TOKEN_DIGITS_MAX_COUNT 8 diff --git a/workers/bt_type_code/bt_type_code.c b/workers/bt_type_code/bt_type_code.c index 7962a152d78..0dbb586127d 100644 --- a/workers/bt_type_code/bt_type_code.c +++ b/workers/bt_type_code/bt_type_code.c @@ -3,8 +3,7 @@ #include #include "../../types/common.h" #include "../../types/token_info.h" -#include "../../services/convert/convert.h" -#include "../constants.h" +#include "../common.h" #define HID_BT_KEYS_STORAGE_PATH EXT_PATH("authenticator/.bt_hid.keys") @@ -19,7 +18,6 @@ static void totp_type_code_worker_press_key(uint8_t key) { } static void totp_type_code_worker_type_code(TotpBtTypeCodeWorkerContext* context) { - TokenAutomationFeature features = context->flags; uint8_t i = 0; do { furi_delay_ms(500); @@ -27,21 +25,11 @@ static void totp_type_code_worker_type_code(TotpBtTypeCodeWorkerContext* context } while(!context->is_connected && i < 100 && !totp_type_code_worker_stop_requested()); if(context->is_connected && furi_mutex_acquire(context->string_sync, 500) == FuriStatusOk) { - furi_delay_ms(500); - i = 0; - while(i < context->string_length && context->string[i] != 0) { - uint8_t digit = CONVERT_CHAR_TO_DIGIT(context->string[i]); - if(digit > 9) break; - uint8_t hid_kb_key = hid_number_keys[digit]; - totp_type_code_worker_press_key(hid_kb_key); - i++; - } - - if(features & TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END) { - furi_delay_ms(30); - totp_type_code_worker_press_key(hid_enter_key); - } - + totp_type_code_worker_execute_automation( + &totp_type_code_worker_press_key, + context->string, + context->string_length, + context->flags); furi_mutex_release(context->string_sync); } } diff --git a/workers/common.c b/workers/common.c new file mode 100644 index 00000000000..b603c4ec1c3 --- /dev/null +++ b/workers/common.c @@ -0,0 +1,42 @@ +#include "common.h" +#include +#include +#include "../../services/convert/convert.h" + +static const uint8_t hid_number_keys[10] = { + HID_KEYBOARD_0, + HID_KEYBOARD_1, + HID_KEYBOARD_2, + HID_KEYBOARD_3, + HID_KEYBOARD_4, + HID_KEYBOARD_5, + HID_KEYBOARD_6, + HID_KEYBOARD_7, + HID_KEYBOARD_8, + HID_KEYBOARD_9}; + +void totp_type_code_worker_execute_automation( + TOTP_AUTOMATION_PRESS_KEY key_press_fn, + const char* string, + uint8_t string_length, + TokenAutomationFeature features) { + furi_delay_ms(500); + uint8_t i = 0; + while(i < string_length && string[i] != 0) { + uint8_t digit = CONVERT_CHAR_TO_DIGIT(string[i]); + if(digit > 9) break; + uint8_t hid_kb_key = hid_number_keys[digit]; + (*key_press_fn)(hid_kb_key); + i++; + } + + if(features & TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END) { + furi_delay_ms(30); + (*key_press_fn)(HID_KEYBOARD_RETURN); + } + + if(features & TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END) { + furi_delay_ms(30); + (*key_press_fn)(HID_KEYBOARD_TAB); + } +} \ No newline at end of file diff --git a/workers/common.h b/workers/common.h new file mode 100644 index 00000000000..0bb48d50153 --- /dev/null +++ b/workers/common.h @@ -0,0 +1,11 @@ +#pragma once +#include +#include "../types/token_info.h" + +typedef void (*TOTP_AUTOMATION_PRESS_KEY)(uint8_t key); + +void totp_type_code_worker_execute_automation( + TOTP_AUTOMATION_PRESS_KEY key_press_fn, + const char* string, + uint8_t string_length, + TokenAutomationFeature features); \ No newline at end of file diff --git a/workers/constants.c b/workers/constants.c deleted file mode 100644 index cde5582ef86..00000000000 --- a/workers/constants.c +++ /dev/null @@ -1,16 +0,0 @@ -#include "constants.h" -#include - -const uint8_t hid_number_keys[10] = { - HID_KEYBOARD_0, - HID_KEYBOARD_1, - HID_KEYBOARD_2, - HID_KEYBOARD_3, - HID_KEYBOARD_4, - HID_KEYBOARD_5, - HID_KEYBOARD_6, - HID_KEYBOARD_7, - HID_KEYBOARD_8, - HID_KEYBOARD_9}; - -const uint8_t hid_enter_key = HID_KEYBOARD_RETURN; \ No newline at end of file diff --git a/workers/constants.h b/workers/constants.h deleted file mode 100644 index 1ec324d3fc8..00000000000 --- a/workers/constants.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once -#include - -extern const uint8_t hid_number_keys[10]; -extern const uint8_t hid_enter_key; diff --git a/workers/usb_type_code/usb_type_code.c b/workers/usb_type_code/usb_type_code.c index 9d2a834d375..2b36426ea1a 100644 --- a/workers/usb_type_code/usb_type_code.c +++ b/workers/usb_type_code/usb_type_code.c @@ -1,7 +1,7 @@ #include "usb_type_code.h" #include "../../services/convert/convert.h" #include "../../types/token_info.h" -#include "../constants.h" +#include "../common.h" static void totp_type_code_worker_restore_usb_mode(TotpUsbTypeCodeWorkerContext* context) { if(context->usb_mode_prev != NULL) { @@ -21,7 +21,6 @@ static void totp_type_code_worker_press_key(uint8_t key) { } static void totp_type_code_worker_type_code(TotpUsbTypeCodeWorkerContext* context) { - TokenAutomationFeature features = context->flags; context->usb_mode_prev = furi_hal_usb_get_config(); furi_hal_usb_unlock(); furi_check(furi_hal_usb_set_config(&usb_hid, NULL) == true); @@ -33,21 +32,11 @@ static void totp_type_code_worker_type_code(TotpUsbTypeCodeWorkerContext* contex if(furi_hal_hid_is_connected() && furi_mutex_acquire(context->string_sync, 500) == FuriStatusOk) { - furi_delay_ms(500); - i = 0; - while(i < context->string_length && context->string[i] != 0) { - uint8_t digit = CONVERT_CHAR_TO_DIGIT(context->string[i]); - if(digit > 9) break; - uint8_t hid_kb_key = hid_number_keys[digit]; - totp_type_code_worker_press_key(hid_kb_key); - i++; - } - - if(features & TOKEN_AUTOMATION_FEATURE_ENTER_AT_THE_END) { - furi_delay_ms(30); - totp_type_code_worker_press_key(hid_enter_key); - } - + totp_type_code_worker_execute_automation( + &totp_type_code_worker_press_key, + context->string, + context->string_length, + context->flags); furi_mutex_release(context->string_sync); furi_delay_ms(100);