Skip to content

Commit af5b9f2

Browse files
authored
Implemented #177 (#179)
1 parent 658125b commit af5b9f2

File tree

11 files changed

+63
-57
lines changed

11 files changed

+63
-57
lines changed

application.fam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ App(
1515
],
1616
stack_size=2 * 1024,
1717
order=20,
18-
fap_version="3.10",
18+
fap_version="3.20",
1919
fap_author="Alexander Kopachov (@akopachov)",
2020
fap_description="Software-based TOTP authenticator for Flipper Zero device",
2121
fap_weburl="https://github.com/akopachov/flipper-zero_authenticator",

cli/commands/automation/automation.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#define TOTP_CLI_COMMAND_AUTOMATION_ARG_METHOD "automation"
88
#define TOTP_CLI_COMMAND_AUTOMATION_METHOD_NONE "none"
99
#define TOTP_CLI_COMMAND_AUTOMATION_METHOD_USB "usb"
10-
#ifdef TOTP_BADBT_TYPE_ENABLED
10+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
1111
#define TOTP_CLI_COMMAND_AUTOMATION_METHOD_BT "bt"
1212
#endif
1313
#define TOTP_CLI_COMMAND_AUTOMATION_LAYOUT_QWERTY "QWERTY"
@@ -31,7 +31,7 @@ void totp_cli_command_automation_docopt_arguments() {
3131
" " TOTP_CLI_COMMAND_AUTOMATION_ARG_METHOD
3232
" Automation method to be set. Must be one of: " TOTP_CLI_COMMAND_AUTOMATION_METHOD_NONE
3333
", " TOTP_CLI_COMMAND_AUTOMATION_METHOD_USB
34-
#ifdef TOTP_BADBT_TYPE_ENABLED
34+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
3535
", " TOTP_CLI_COMMAND_AUTOMATION_METHOD_BT
3636
#endif
3737
"\r\n");
@@ -47,17 +47,17 @@ void totp_cli_command_automation_docopt_options() {
4747
}
4848

4949
static void print_method(AutomationMethod method, const char* color) {
50-
#ifdef TOTP_BADBT_TYPE_ENABLED
50+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
5151
bool has_previous_method = false;
5252
#endif
5353
if(method & AutomationMethodBadUsb) {
5454
TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_AUTOMATION_METHOD_USB "\"");
55-
#ifdef TOTP_BADBT_TYPE_ENABLED
55+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
5656
has_previous_method = true;
5757
#endif
5858
}
5959

60-
#ifdef TOTP_BADBT_TYPE_ENABLED
60+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
6161
if(method & AutomationMethodBadBt) {
6262
if(has_previous_method) {
6363
TOTP_CLI_PRINTF_COLORFUL(color, " and ");
@@ -121,7 +121,7 @@ void totp_cli_command_automation_handle(PluginState* plugin_state, FuriString* a
121121
new_method_provided = true;
122122
new_method |= AutomationMethodBadUsb;
123123
}
124-
#ifdef TOTP_BADBT_TYPE_ENABLED
124+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
125125
else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_AUTOMATION_METHOD_BT) == 0) {
126126
new_method_provided = true;
127127
new_method |= AutomationMethodBadBt;
@@ -161,7 +161,7 @@ void totp_cli_command_automation_handle(PluginState* plugin_state, FuriString* a
161161
totp_cli_print_error_updating_config_file();
162162
}
163163

164-
#ifdef TOTP_BADBT_TYPE_ENABLED
164+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
165165
if(!(new_method & AutomationMethodBadBt) &&
166166
plugin_state->bt_type_code_worker_context != NULL) {
167167
totp_bt_type_code_worker_free(plugin_state->bt_type_code_worker_context);

features_config.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@
44
#endif
55

66
// Include Bluetooth token input automation
7-
#ifndef TOTP_NO_BADBT_TYPE
8-
#define TOTP_BADBT_TYPE_ENABLED
9-
#endif
10-
11-
// Include token input automation icons on the main screen
12-
#ifndef TOTP_NO_AUTOMATION_ICONS
13-
#define TOTP_AUTOMATION_ICONS_ENABLED
7+
#ifndef TOTP_NO_BADBT_AUTOMATION
8+
#define TOTP_BADBT_AUTOMATION_ENABLED
149
#endif
1510

1611
// List of compatible firmwares

services/config/config.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,16 @@ static char* totp_config_file_backup_i(Storage* storage) {
111111
static bool totp_open_config_file(Storage* storage, FlipperFormat** file) {
112112
FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
113113

114-
if(storage_common_stat(storage, CONFIG_FILE_PATH, NULL) == FSE_OK) {
114+
bool conf_file_exists = storage_common_stat(storage, CONFIG_FILE_PATH, NULL) == FSE_OK;
115+
if(!conf_file_exists) {
116+
FURI_LOG_I(LOGGING_TAG, "Application catalog needs to be migrated");
117+
FS_Error migration_result =
118+
storage_common_migrate(storage, EXT_PATH("authenticator"), CONFIG_FILE_DIRECTORY_PATH);
119+
FURI_LOG_I(LOGGING_TAG, "Migrated catalog. Result code: %d", (int)migration_result);
120+
conf_file_exists = storage_common_stat(storage, CONFIG_FILE_PATH, NULL) == FSE_OK;
121+
}
122+
123+
if(conf_file_exists) {
115124
FURI_LOG_D(LOGGING_TAG, "Config file %s found", CONFIG_FILE_PATH);
116125
if(!flipper_format_file_open_existing(fff_data_file, CONFIG_FILE_PATH)) {
117126
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) {
120129
}
121130
} else {
122131
FURI_LOG_D(LOGGING_TAG, "Config file %s is not found. Will create new.", CONFIG_FILE_PATH);
123-
if(storage_common_stat(storage, CONFIG_FILE_DIRECTORY_PATH, NULL) == FSE_NOT_EXIST) {
124-
FURI_LOG_D(
125-
LOGGING_TAG,
126-
"Directory %s doesn't exist. Will create new.",
127-
CONFIG_FILE_DIRECTORY_PATH);
128-
if(!storage_simply_mkdir(storage, CONFIG_FILE_DIRECTORY_PATH)) {
129-
FURI_LOG_E(LOGGING_TAG, "Error creating directory %s", CONFIG_FILE_DIRECTORY_PATH);
130-
return false;
131-
}
132-
}
133132

134133
if(!flipper_format_file_open_new(fff_data_file, CONFIG_FILE_PATH)) {
135134
totp_close_config_file(fff_data_file);

services/config/constants.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <storage/storage.h>
44

5-
#define CONFIG_FILE_DIRECTORY_PATH EXT_PATH("authenticator")
5+
#define CONFIG_FILE_DIRECTORY_PATH STORAGE_APP_DATA_PATH_PREFIX
66
#define CONFIG_FILE_HEADER "Flipper TOTP plugin config file"
77
#define CONFIG_FILE_ACTUAL_VERSION (8)
88

totp_app.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static bool totp_plugin_state_init(PluginState* const plugin_state) {
134134

135135
plugin_state->event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
136136

137-
#ifdef TOTP_BADBT_TYPE_ENABLED
137+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
138138
if(plugin_state->automation_method & AutomationMethodBadBt) {
139139
plugin_state->bt_type_code_worker_context = totp_bt_type_code_worker_init();
140140
} else {
@@ -168,14 +168,17 @@ static void totp_plugin_state_free(PluginState* plugin_state) {
168168
free(plugin_state->crypto_verify_data);
169169
}
170170

171-
#ifdef TOTP_BADBT_TYPE_ENABLED
171+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
172172
if(plugin_state->bt_type_code_worker_context != NULL) {
173173
totp_bt_type_code_worker_free(plugin_state->bt_type_code_worker_context);
174174
plugin_state->bt_type_code_worker_context = NULL;
175175
}
176176
#endif
177177

178-
furi_message_queue_free(plugin_state->event_queue);
178+
if(plugin_state->event_queue != NULL) {
179+
furi_message_queue_free(plugin_state->event_queue);
180+
}
181+
179182
free(plugin_state);
180183
}
181184

types/automation_method.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ typedef uint8_t AutomationMethod;
77
enum AutomationMethods {
88
AutomationMethodNone = 0b00,
99
AutomationMethodBadUsb = 0b01,
10-
#ifdef TOTP_BADBT_TYPE_ENABLED
10+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
1111
AutomationMethodBadBt = 0b10,
1212
#endif
1313
};

types/plugin_state.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "notification_method.h"
1111
#include "automation_method.h"
1212
#include "automation_kb_layout.h"
13-
#ifdef TOTP_BADBT_TYPE_ENABLED
13+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
1414
#include "../workers/bt_type_code/bt_type_code.h"
1515
#endif
1616
#include "../services/crypto/constants.h"
@@ -89,7 +89,7 @@ typedef struct {
8989
*/
9090
AutomationKeyboardLayout automation_kb_layout;
9191

92-
#ifdef TOTP_BADBT_TYPE_ENABLED
92+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
9393
/**
9494
* @brief Bad-Bluetooth worker context
9595
*/

ui/scenes/app_settings/totp_app_settings.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,30 @@
1212
#include "../../../services/convert/convert.h"
1313
#include <roll_value.h>
1414
#include "../../../features_config.h"
15-
#ifdef TOTP_BADBT_TYPE_ENABLED
15+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
1616
#include "../../../workers/bt_type_code/bt_type_code.h"
1717
#endif
1818

19+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
20+
#define AUTOMATION_LIST_MAX_INDEX (3)
21+
#else
22+
#define AUTOMATION_LIST_MAX_INDEX (1)
23+
#endif
24+
#define BAD_KB_LAYOUT_LIST_MAX_INDEX (1)
25+
#define FONT_TEST_STR_LENGTH (7)
26+
1927
static const char* YES_NO_LIST[] = {"NO", "YES"};
2028
static const char* AUTOMATION_LIST[] = {
2129
"None",
2230
"USB"
23-
#ifdef TOTP_BADBT_TYPE_ENABLED
31+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
2432
,
2533
"Bluetooth",
2634
"BT and USB"
2735
#endif
2836
};
29-
30-
#ifdef TOTP_BADBT_TYPE_ENABLED
31-
#define AUTOMATION_LIST_MAX_INDEX (3)
32-
#else
33-
#define AUTOMATION_LIST_MAX_INDEX (1)
34-
#endif
35-
3637
static const char* BAD_KB_LAYOUT_LIST[] = {"QWERTY", "AZERTY"};
3738
static const char* FONT_TEST_STR = "0123BCD";
38-
static const uint8_t FONT_TEST_STR_LENGTH = 7;
3939

4040
typedef enum {
4141
HoursInput,
@@ -71,8 +71,10 @@ void totp_scene_app_settings_activate(PluginState* plugin_state) {
7171
scene_state->tz_offset_minutes = 60.0f * off_dec;
7272
scene_state->notification_sound = plugin_state->notification_method & NotificationMethodSound;
7373
scene_state->notification_vibro = plugin_state->notification_method & NotificationMethodVibro;
74-
scene_state->automation_method = plugin_state->automation_method;
75-
scene_state->automation_kb_layout = plugin_state->automation_kb_layout;
74+
scene_state->automation_method =
75+
MIN(plugin_state->automation_method, AUTOMATION_LIST_MAX_INDEX);
76+
scene_state->automation_kb_layout =
77+
MIN(plugin_state->automation_kb_layout, BAD_KB_LAYOUT_LIST_MAX_INDEX);
7678

7779
scene_state->active_font = plugin_state->active_font_index;
7880
}
@@ -281,7 +283,11 @@ bool totp_scene_app_settings_handle_event(
281283
RollOverflowBehaviorRoll);
282284
} else if(scene_state->selected_control == BadKeyboardLayoutSelect) {
283285
totp_roll_value_uint8_t(
284-
&scene_state->automation_kb_layout, 1, 0, 1, RollOverflowBehaviorRoll);
286+
&scene_state->automation_kb_layout,
287+
1,
288+
0,
289+
BAD_KB_LAYOUT_LIST_MAX_INDEX,
290+
RollOverflowBehaviorRoll);
285291
}
286292
break;
287293
case InputKeyLeft:
@@ -311,7 +317,11 @@ bool totp_scene_app_settings_handle_event(
311317
RollOverflowBehaviorRoll);
312318
} else if(scene_state->selected_control == BadKeyboardLayoutSelect) {
313319
totp_roll_value_uint8_t(
314-
&scene_state->automation_kb_layout, -1, 0, 1, RollOverflowBehaviorRoll);
320+
&scene_state->automation_kb_layout,
321+
-1,
322+
0,
323+
BAD_KB_LAYOUT_LIST_MAX_INDEX,
324+
RollOverflowBehaviorRoll);
315325
}
316326
break;
317327
case InputKeyOk:
@@ -342,7 +352,7 @@ bool totp_scene_app_settings_handle_event(
342352
return false;
343353
}
344354

345-
#ifdef TOTP_BADBT_TYPE_ENABLED
355+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
346356
if((scene_state->automation_method & AutomationMethodBadBt) == 0 &&
347357
plugin_state->bt_type_code_worker_context != NULL) {
348358
totp_bt_type_code_worker_free(plugin_state->bt_type_code_worker_context);

ui/scenes/generate_token/totp_scene_generate_token.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "../../../features_config.h"
1616
#include "../../../workers/generate_totp_code/generate_totp_code.h"
1717
#include "../../../workers/usb_type_code/usb_type_code.h"
18-
#ifdef TOTP_BADBT_TYPE_ENABLED
18+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
1919
#include "../../../workers/bt_type_code/bt_type_code.h"
2020
#endif
2121

@@ -214,7 +214,7 @@ void totp_scene_generate_token_activate(PluginState* plugin_state) {
214214
scene_state->active_font = available_fonts[plugin_state->active_font_index];
215215
scene_state->notification_app = furi_record_open(RECORD_NOTIFICATION);
216216

217-
#ifdef TOTP_BADBT_TYPE_ENABLED
217+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
218218

219219
if(plugin_state->automation_method & AutomationMethodBadBt) {
220220
if(plugin_state->bt_type_code_worker_context == NULL) {
@@ -309,11 +309,10 @@ void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_
309309
canvas, SCREEN_WIDTH - 8, SCREEN_HEIGHT_CENTER - 24, &I_totp_arrow_right_8x9);
310310
}
311311

312-
#ifdef TOTP_AUTOMATION_ICONS_ENABLED
313312
if(plugin_state->automation_method & AutomationMethodBadUsb) {
314313
canvas_draw_icon(
315314
canvas,
316-
#ifdef TOTP_BADBT_TYPE_ENABLED
315+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
317316
SCREEN_WIDTH_CENTER -
318317
(plugin_state->automation_method & AutomationMethodBadBt ? 33 : 15),
319318
#else
@@ -324,7 +323,7 @@ void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_
324323
&I_hid_usb_31x9);
325324
}
326325

327-
#ifdef TOTP_BADBT_TYPE_ENABLED
326+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
328327
if(plugin_state->automation_method & AutomationMethodBadBt &&
329328
plugin_state->bt_type_code_worker_context != NULL &&
330329
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_
336335
&I_hid_ble_31x9);
337336
}
338337
#endif
339-
#endif
340338
}
341339

342340
bool totp_scene_generate_token_handle_event(
@@ -366,7 +364,7 @@ bool totp_scene_generate_token_handle_event(
366364
get_notification_sequence_automation(plugin_state, scene_state));
367365
return true;
368366
}
369-
#ifdef TOTP_BADBT_TYPE_ENABLED
367+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
370368
else if(
371369
event->input.key == InputKeyUp &&
372370
plugin_state->automation_method & AutomationMethodBadBt) {
@@ -444,7 +442,7 @@ void totp_scene_generate_token_deactivate(PluginState* plugin_state) {
444442
if(plugin_state->automation_method & AutomationMethodBadUsb) {
445443
totp_usb_type_code_worker_stop(scene_state->usb_type_code_worker_context);
446444
}
447-
#ifdef TOTP_BADBT_TYPE_ENABLED
445+
#ifdef TOTP_BADBT_AUTOMATION_ENABLED
448446
if(plugin_state->automation_method & AutomationMethodBadBt) {
449447
totp_bt_type_code_worker_stop(plugin_state->bt_type_code_worker_context);
450448
}

0 commit comments

Comments
 (0)