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

+ 1 - 1
application.fam

@@ -15,7 +15,7 @@ App(
     ],
     stack_size=2 * 1024,
     order=20,
-    fap_version="3.10",
+    fap_version="3.20",
     fap_author="Alexander Kopachov (@akopachov)",
     fap_description="Software-based TOTP authenticator for Flipper Zero device",
     fap_weburl="https://github.com/akopachov/flipper-zero_authenticator",

+ 7 - 7
cli/commands/automation/automation.c

@@ -7,7 +7,7 @@
 #define TOTP_CLI_COMMAND_AUTOMATION_ARG_METHOD "automation"
 #define TOTP_CLI_COMMAND_AUTOMATION_METHOD_NONE "none"
 #define TOTP_CLI_COMMAND_AUTOMATION_METHOD_USB "usb"
-#ifdef TOTP_BADBT_TYPE_ENABLED
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
 #define TOTP_CLI_COMMAND_AUTOMATION_METHOD_BT "bt"
 #endif
 #define TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_QWERTY "QWERTY"
@@ -31,7 +31,7 @@ void totp_cli_command_automation_docopt_arguments() {
         "  " TOTP_CLI_COMMAND_AUTOMATION_ARG_METHOD
         "    Automation method to be set. Must be one of: " TOTP_CLI_COMMAND_AUTOMATION_METHOD_NONE
         ", " TOTP_CLI_COMMAND_AUTOMATION_METHOD_USB
-#ifdef TOTP_BADBT_TYPE_ENABLED
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
         ", " TOTP_CLI_COMMAND_AUTOMATION_METHOD_BT
 #endif
         "\r\n");
@@ -47,17 +47,17 @@ void totp_cli_command_automation_docopt_options() {
 }
 
 static void print_method(AutomationMethod method, const char* color) {
-#ifdef TOTP_BADBT_TYPE_ENABLED
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
     bool has_previous_method = false;
 #endif
     if(method & AutomationMethodBadUsb) {
         TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_AUTOMATION_METHOD_USB "\"");
-#ifdef TOTP_BADBT_TYPE_ENABLED
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
         has_previous_method = true;
 #endif
     }
 
-#ifdef TOTP_BADBT_TYPE_ENABLED
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
     if(method & AutomationMethodBadBt) {
         if(has_previous_method) {
             TOTP_CLI_PRINTF_COLORFUL(color, " and ");
@@ -121,7 +121,7 @@ void totp_cli_command_automation_handle(PluginState* plugin_state, FuriString* a
             new_method_provided = true;
             new_method |= AutomationMethodBadUsb;
         }
-#ifdef TOTP_BADBT_TYPE_ENABLED
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
         else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_AUTOMATION_METHOD_BT) == 0) {
             new_method_provided = true;
             new_method |= AutomationMethodBadBt;
@@ -161,7 +161,7 @@ void totp_cli_command_automation_handle(PluginState* plugin_state, FuriString* a
                 totp_cli_print_error_updating_config_file();
             }
 
-#ifdef TOTP_BADBT_TYPE_ENABLED
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
             if(!(new_method & AutomationMethodBadBt) &&
                plugin_state->bt_type_code_worker_context != NULL) {
                 totp_bt_type_code_worker_free(plugin_state->bt_type_code_worker_context);

+ 2 - 7
features_config.h

@@ -4,13 +4,8 @@
 #endif
 
 // Include Bluetooth token input automation
-#ifndef TOTP_NO_BADBT_TYPE
-#define TOTP_BADBT_TYPE_ENABLED
-#endif
-
-// Include token input automation icons on the main screen
-#ifndef TOTP_NO_AUTOMATION_ICONS
-#define TOTP_AUTOMATION_ICONS_ENABLED
+#ifndef TOTP_NO_BADBT_AUTOMATION
+#define TOTP_BADBT_AUTOMATION_ENABLED
 #endif
 
 // List of compatible firmwares

+ 10 - 11
services/config/config.c

@@ -111,7 +111,16 @@ static char* totp_config_file_backup_i(Storage* storage) {
 static bool totp_open_config_file(Storage* storage, FlipperFormat** file) {
     FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
 
-    if(storage_common_stat(storage, CONFIG_FILE_PATH, NULL) == FSE_OK) {
+    bool conf_file_exists = storage_common_stat(storage, CONFIG_FILE_PATH, NULL) == FSE_OK;
+    if(!conf_file_exists) {
+        FURI_LOG_I(LOGGING_TAG, "Application catalog needs to be migrated");
+        FS_Error migration_result =
+            storage_common_migrate(storage, EXT_PATH("authenticator"), CONFIG_FILE_DIRECTORY_PATH);
+        FURI_LOG_I(LOGGING_TAG, "Migrated catalog. Result code: %d", (int)migration_result);
+        conf_file_exists = storage_common_stat(storage, CONFIG_FILE_PATH, NULL) == FSE_OK;
+    }
+
+    if(conf_file_exists) {
         FURI_LOG_D(LOGGING_TAG, "Config file %s found", CONFIG_FILE_PATH);
         if(!flipper_format_file_open_existing(fff_data_file, CONFIG_FILE_PATH)) {
             FURI_LOG_E(LOGGING_TAG, "Error opening existing file %s", CONFIG_FILE_PATH);
@@ -120,16 +129,6 @@ static bool totp_open_config_file(Storage* storage, FlipperFormat** file) {
         }
     } else {
         FURI_LOG_D(LOGGING_TAG, "Config file %s is not found. Will create new.", CONFIG_FILE_PATH);
-        if(storage_common_stat(storage, CONFIG_FILE_DIRECTORY_PATH, NULL) == FSE_NOT_EXIST) {
-            FURI_LOG_D(
-                LOGGING_TAG,
-                "Directory %s doesn't exist. Will create new.",
-                CONFIG_FILE_DIRECTORY_PATH);
-            if(!storage_simply_mkdir(storage, CONFIG_FILE_DIRECTORY_PATH)) {
-                FURI_LOG_E(LOGGING_TAG, "Error creating directory %s", CONFIG_FILE_DIRECTORY_PATH);
-                return false;
-            }
-        }
 
         if(!flipper_format_file_open_new(fff_data_file, CONFIG_FILE_PATH)) {
             totp_close_config_file(fff_data_file);

+ 1 - 1
services/config/constants.h

@@ -2,7 +2,7 @@
 
 #include <storage/storage.h>
 
-#define CONFIG_FILE_DIRECTORY_PATH EXT_PATH("authenticator")
+#define CONFIG_FILE_DIRECTORY_PATH STORAGE_APP_DATA_PATH_PREFIX
 #define CONFIG_FILE_HEADER "Flipper TOTP plugin config file"
 #define CONFIG_FILE_ACTUAL_VERSION (8)
 

+ 6 - 3
totp_app.c

@@ -134,7 +134,7 @@ static bool totp_plugin_state_init(PluginState* const plugin_state) {
 
     plugin_state->event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
 
-#ifdef TOTP_BADBT_TYPE_ENABLED
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
     if(plugin_state->automation_method & AutomationMethodBadBt) {
         plugin_state->bt_type_code_worker_context = totp_bt_type_code_worker_init();
     } else {
@@ -168,14 +168,17 @@ static void totp_plugin_state_free(PluginState* plugin_state) {
         free(plugin_state->crypto_verify_data);
     }
 
-#ifdef TOTP_BADBT_TYPE_ENABLED
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
     if(plugin_state->bt_type_code_worker_context != NULL) {
         totp_bt_type_code_worker_free(plugin_state->bt_type_code_worker_context);
         plugin_state->bt_type_code_worker_context = NULL;
     }
 #endif
 
-    furi_message_queue_free(plugin_state->event_queue);
+    if(plugin_state->event_queue != NULL) {
+        furi_message_queue_free(plugin_state->event_queue);
+    }
+
     free(plugin_state);
 }
 

+ 1 - 1
types/automation_method.h

@@ -7,7 +7,7 @@ typedef uint8_t AutomationMethod;
 enum AutomationMethods {
     AutomationMethodNone = 0b00,
     AutomationMethodBadUsb = 0b01,
-#ifdef TOTP_BADBT_TYPE_ENABLED
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
     AutomationMethodBadBt = 0b10,
 #endif
 };

+ 2 - 2
types/plugin_state.h

@@ -10,7 +10,7 @@
 #include "notification_method.h"
 #include "automation_method.h"
 #include "automation_kb_layout.h"
-#ifdef TOTP_BADBT_TYPE_ENABLED
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
 #include "../workers/bt_type_code/bt_type_code.h"
 #endif
 #include "../services/crypto/constants.h"
@@ -89,7 +89,7 @@ typedef struct {
      */
     AutomationKeyboardLayout automation_kb_layout;
 
-#ifdef TOTP_BADBT_TYPE_ENABLED
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
     /**
      * @brief Bad-Bluetooth worker context
      */

+ 25 - 15
ui/scenes/app_settings/totp_app_settings.c

@@ -12,30 +12,30 @@
 #include "../../../services/convert/convert.h"
 #include <roll_value.h>
 #include "../../../features_config.h"
-#ifdef TOTP_BADBT_TYPE_ENABLED
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
 #include "../../../workers/bt_type_code/bt_type_code.h"
 #endif
 
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
+#define AUTOMATION_LIST_MAX_INDEX (3)
+#else
+#define AUTOMATION_LIST_MAX_INDEX (1)
+#endif
+#define BAD_KB_LAYOUT_LIST_MAX_INDEX (1)
+#define FONT_TEST_STR_LENGTH (7)
+
 static const char* YES_NO_LIST[] = {"NO", "YES"};
 static const char* AUTOMATION_LIST[] = {
     "None",
     "USB"
-#ifdef TOTP_BADBT_TYPE_ENABLED
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
     ,
     "Bluetooth",
     "BT and USB"
 #endif
 };
-
-#ifdef TOTP_BADBT_TYPE_ENABLED
-#define AUTOMATION_LIST_MAX_INDEX (3)
-#else
-#define AUTOMATION_LIST_MAX_INDEX (1)
-#endif
-
 static const char* BAD_KB_LAYOUT_LIST[] = {"QWERTY", "AZERTY"};
 static const char* FONT_TEST_STR = "0123BCD";
-static const uint8_t FONT_TEST_STR_LENGTH = 7;
 
 typedef enum {
     HoursInput,
@@ -71,8 +71,10 @@ void totp_scene_app_settings_activate(PluginState* plugin_state) {
     scene_state->tz_offset_minutes = 60.0f * off_dec;
     scene_state->notification_sound = plugin_state->notification_method & NotificationMethodSound;
     scene_state->notification_vibro = plugin_state->notification_method & NotificationMethodVibro;
-    scene_state->automation_method = plugin_state->automation_method;
-    scene_state->automation_kb_layout = plugin_state->automation_kb_layout;
+    scene_state->automation_method =
+        MIN(plugin_state->automation_method, AUTOMATION_LIST_MAX_INDEX);
+    scene_state->automation_kb_layout =
+        MIN(plugin_state->automation_kb_layout, BAD_KB_LAYOUT_LIST_MAX_INDEX);
 
     scene_state->active_font = plugin_state->active_font_index;
 }
@@ -281,7 +283,11 @@ bool totp_scene_app_settings_handle_event(
                     RollOverflowBehaviorRoll);
             } else if(scene_state->selected_control == BadKeyboardLayoutSelect) {
                 totp_roll_value_uint8_t(
-                    &scene_state->automation_kb_layout, 1, 0, 1, RollOverflowBehaviorRoll);
+                    &scene_state->automation_kb_layout,
+                    1,
+                    0,
+                    BAD_KB_LAYOUT_LIST_MAX_INDEX,
+                    RollOverflowBehaviorRoll);
             }
             break;
         case InputKeyLeft:
@@ -311,7 +317,11 @@ bool totp_scene_app_settings_handle_event(
                     RollOverflowBehaviorRoll);
             } else if(scene_state->selected_control == BadKeyboardLayoutSelect) {
                 totp_roll_value_uint8_t(
-                    &scene_state->automation_kb_layout, -1, 0, 1, RollOverflowBehaviorRoll);
+                    &scene_state->automation_kb_layout,
+                    -1,
+                    0,
+                    BAD_KB_LAYOUT_LIST_MAX_INDEX,
+                    RollOverflowBehaviorRoll);
             }
             break;
         case InputKeyOk:
@@ -342,7 +352,7 @@ bool totp_scene_app_settings_handle_event(
             return false;
         }
 
-#ifdef TOTP_BADBT_TYPE_ENABLED
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
         if((scene_state->automation_method & AutomationMethodBadBt) == 0 &&
            plugin_state->bt_type_code_worker_context != NULL) {
             totp_bt_type_code_worker_free(plugin_state->bt_type_code_worker_context);

+ 6 - 8
ui/scenes/generate_token/totp_scene_generate_token.c

@@ -15,7 +15,7 @@
 #include "../../../features_config.h"
 #include "../../../workers/generate_totp_code/generate_totp_code.h"
 #include "../../../workers/usb_type_code/usb_type_code.h"
-#ifdef TOTP_BADBT_TYPE_ENABLED
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
 #include "../../../workers/bt_type_code/bt_type_code.h"
 #endif
 
@@ -214,7 +214,7 @@ void totp_scene_generate_token_activate(PluginState* plugin_state) {
     scene_state->active_font = available_fonts[plugin_state->active_font_index];
     scene_state->notification_app = furi_record_open(RECORD_NOTIFICATION);
 
-#ifdef TOTP_BADBT_TYPE_ENABLED
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
 
     if(plugin_state->automation_method & AutomationMethodBadBt) {
         if(plugin_state->bt_type_code_worker_context == NULL) {
@@ -309,11 +309,10 @@ void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_
             canvas, SCREEN_WIDTH - 8, SCREEN_HEIGHT_CENTER - 24, &I_totp_arrow_right_8x9);
     }
 
-#ifdef TOTP_AUTOMATION_ICONS_ENABLED
     if(plugin_state->automation_method & AutomationMethodBadUsb) {
         canvas_draw_icon(
             canvas,
-#ifdef TOTP_BADBT_TYPE_ENABLED
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
             SCREEN_WIDTH_CENTER -
                 (plugin_state->automation_method & AutomationMethodBadBt ? 33 : 15),
 #else
@@ -324,7 +323,7 @@ void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_
             &I_hid_usb_31x9);
     }
 
-#ifdef TOTP_BADBT_TYPE_ENABLED
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
     if(plugin_state->automation_method & AutomationMethodBadBt &&
        plugin_state->bt_type_code_worker_context != NULL &&
        totp_bt_type_code_worker_is_advertising(plugin_state->bt_type_code_worker_context)) {
@@ -336,7 +335,6 @@ void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_
             &I_hid_ble_31x9);
     }
 #endif
-#endif
 }
 
 bool totp_scene_generate_token_handle_event(
@@ -366,7 +364,7 @@ bool totp_scene_generate_token_handle_event(
                 get_notification_sequence_automation(plugin_state, scene_state));
             return true;
         }
-#ifdef TOTP_BADBT_TYPE_ENABLED
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
         else if(
             event->input.key == InputKeyUp &&
             plugin_state->automation_method & AutomationMethodBadBt) {
@@ -444,7 +442,7 @@ void totp_scene_generate_token_deactivate(PluginState* plugin_state) {
     if(plugin_state->automation_method & AutomationMethodBadUsb) {
         totp_usb_type_code_worker_stop(scene_state->usb_type_code_worker_context);
     }
-#ifdef TOTP_BADBT_TYPE_ENABLED
+#ifdef TOTP_BADBT_AUTOMATION_ENABLED
     if(plugin_state->automation_method & AutomationMethodBadBt) {
         totp_bt_type_code_worker_stop(plugin_state->bt_type_code_worker_context);
     }

+ 2 - 1
workers/bt_type_code/bt_type_code.c

@@ -12,13 +12,14 @@
 #include "../../types/token_info.h"
 #include "../type_code_common.h"
 #include "../../features_config.h"
+#include "../../services/config/constants.h"
 
 #if TOTP_TARGET_FIRMWARE == TOTP_FIRMWARE_XTREME_UL
 #define TOTP_BT_WORKER_BT_ADV_NAME_MAX_LEN FURI_HAL_BT_ADV_NAME_LENGTH
 #define TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN GAP_MAC_ADDR_SIZE
 #endif
 
-#define HID_BT_KEYS_STORAGE_PATH EXT_PATH("authenticator/.bt_hid.keys")
+#define HID_BT_KEYS_STORAGE_PATH CONFIG_FILE_DIRECTORY_PATH "/.bt_hid.keys"
 
 struct TotpBtTypeCodeWorkerContext {
     char* code_buffer;