Alexander Kopachov 2 лет назад
Родитель
Сommit
2901da828b

+ 4 - 2
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;
         }
     }

+ 3 - 0
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 <Enter> key at the end of token input automation\r\n");
+    TOTP_CLI_PRINTF("                 # " TOTP_TOKEN_AUTOMATION_FEATURE_TAB_AT_THE_END_NAME
+                    " - Type <Tab> key at the end of token input automation\r\n");
 }
 
 void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {

+ 15 - 1
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 <Enter> key at the end");
+            "| %-20s | %-28.28s |\r\n",
+            AUTOMATION_FEATURES_PROPERTY_HEADER,
+            "Type <Enter> 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 <Tab> key at the end");
+        header_printed = true;
     }
 }
 

+ 9 - 19
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) {

+ 5 - 0
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;

+ 7 - 1
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

+ 6 - 18
workers/bt_type_code/bt_type_code.c

@@ -3,8 +3,7 @@
 #include <storage/storage.h>
 #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);
     }
 }

+ 42 - 0
workers/common.c

@@ -0,0 +1,42 @@
+#include "common.h"
+#include <furi/furi.h>
+#include <furi_hal.h>
+#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);
+    }
+}

+ 11 - 0
workers/common.h

@@ -0,0 +1,11 @@
+#pragma once
+#include <stdint.h>
+#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);

+ 0 - 16
workers/constants.c

@@ -1,16 +0,0 @@
-#include "constants.h"
-#include <furi_hal.h>
-
-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;

+ 0 - 5
workers/constants.h

@@ -1,5 +0,0 @@
-#pragma once
-#include <stdint.h>
-
-extern const uint8_t hid_number_keys[10];
-extern const uint8_t hid_enter_key;

+ 6 - 17
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);