From af5b9f21f4737ab2063cee051a6b5a698c2cac0b Mon Sep 17 00:00:00 2001 From: Alexander Kopachov Date: Tue, 1 Aug 2023 10:18:32 +0200 Subject: [PATCH] Implemented #177 (#179) --- application.fam | 2 +- cli/commands/automation/automation.c | 14 +++---- features_config.h | 9 +---- services/config/config.c | 21 +++++----- services/config/constants.h | 2 +- totp_app.c | 9 +++-- types/automation_method.h | 2 +- types/plugin_state.h | 4 +- ui/scenes/app_settings/totp_app_settings.c | 40 ++++++++++++------- .../totp_scene_generate_token.c | 14 +++---- workers/bt_type_code/bt_type_code.c | 3 +- 11 files changed, 63 insertions(+), 57 deletions(-) diff --git a/application.fam b/application.fam index 3156c81ce17..aba01fe739f 100644 --- a/application.fam +++ b/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", diff --git a/cli/commands/automation/automation.c b/cli/commands/automation/automation.c index b4baa1fbc5a..87c121dbe53 100644 --- a/cli/commands/automation/automation.c +++ b/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); diff --git a/features_config.h b/features_config.h index 1f1e77d8c76..6750b0e90f6 100644 --- a/features_config.h +++ b/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 diff --git a/services/config/config.c b/services/config/config.c index bb6c831c276..be4e5d550ed 100644 --- a/services/config/config.c +++ b/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); diff --git a/services/config/constants.h b/services/config/constants.h index 6a950caee41..1205bae073a 100644 --- a/services/config/constants.h +++ b/services/config/constants.h @@ -2,7 +2,7 @@ #include -#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) diff --git a/totp_app.c b/totp_app.c index 12e087179ed..95031a67bea 100644 --- a/totp_app.c +++ b/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); } diff --git a/types/automation_method.h b/types/automation_method.h index b51e59e03cd..89e39e18e2a 100644 --- a/types/automation_method.h +++ b/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 }; diff --git a/types/plugin_state.h b/types/plugin_state.h index f9250a3fa15..8c20093efb7 100644 --- a/types/plugin_state.h +++ b/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 */ diff --git a/ui/scenes/app_settings/totp_app_settings.c b/ui/scenes/app_settings/totp_app_settings.c index dbef1e8d788..33dcd5b43bc 100644 --- a/ui/scenes/app_settings/totp_app_settings.c +++ b/ui/scenes/app_settings/totp_app_settings.c @@ -12,30 +12,30 @@ #include "../../../services/convert/convert.h" #include #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); diff --git a/ui/scenes/generate_token/totp_scene_generate_token.c b/ui/scenes/generate_token/totp_scene_generate_token.c index 517086bc770..02f3857ebf3 100644 --- a/ui/scenes/generate_token/totp_scene_generate_token.c +++ b/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); } diff --git a/workers/bt_type_code/bt_type_code.c b/workers/bt_type_code/bt_type_code.c index 97a10b0d5e0..16dfcc0e841 100644 --- a/workers/bt_type_code/bt_type_code.c +++ b/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;