Explorar el Código

Refactoring (#214)

* Refactoring

* CLang format changes

---------

Co-authored-by: akopachov <akopachov@users.noreply.github.com>
Alexander Kopachov hace 2 años
padre
commit
09e1e9445a

+ 53 - 53
ui/scenes/add_new_token/totp_scene_add_new_token.c

@@ -315,65 +315,65 @@ bool totp_scene_add_new_token_handle_event(
                     RollOverflowBehaviorRoll);
                     RollOverflowBehaviorRoll);
             }
             }
             break;
             break;
-        case InputKeyOk:
-            break;
-        case InputKeyBack:
-            totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
-            break;
-        default:
-            break;
-        }
-    } else if(event->input.type == InputTypeShort && event->input.key == InputKeyOk) {
-        switch(scene_state->selected_control) {
-        case TokenNameTextBox:
-            ask_user_input(
-                plugin_state,
-                "Token name",
-                &scene_state->token_name,
-                &scene_state->token_name_length);
-            break;
-        case TokenSecretTextBox:
-            ask_user_input(
-                plugin_state,
-                "Token secret",
-                &scene_state->token_secret,
-                &scene_state->token_secret_length);
-            break;
-        case TokenAlgoSelect:
-            break;
-        case TokenLengthSelect:
-            break;
-        case TokenDurationOrCounterSelect:
-            if(scene_state->type == TokenTypeHOTP) {
+        case InputKeyOk: {
+            switch(scene_state->selected_control) {
+            case TokenNameTextBox:
                 ask_user_input(
                 ask_user_input(
                     plugin_state,
                     plugin_state,
-                    "Initial counter",
-                    &scene_state->initial_counter,
-                    &scene_state->initial_counter_length);
+                    "Token name",
+                    &scene_state->token_name,
+                    &scene_state->token_name_length);
+                break;
+            case TokenSecretTextBox:
+                ask_user_input(
+                    plugin_state,
+                    "Token secret",
+                    &scene_state->token_secret,
+                    &scene_state->token_secret_length);
+                break;
+            case TokenAlgoSelect:
+                break;
+            case TokenLengthSelect:
+                break;
+            case TokenDurationOrCounterSelect:
+                if(scene_state->type == TokenTypeHOTP) {
+                    ask_user_input(
+                        plugin_state,
+                        "Initial counter",
+                        &scene_state->initial_counter,
+                        &scene_state->initial_counter_length);
+                }
+                break;
+            case ConfirmButton: {
+                struct TotpAddContext add_context = {
+                    .scene_state = scene_state, .crypto_settings = &plugin_state->crypto_settings};
+                TokenInfoIteratorContext* iterator_context =
+                    totp_config_get_token_iterator_context(plugin_state);
+                TotpIteratorUpdateTokenResult add_result = totp_token_info_iterator_add_new_token(
+                    iterator_context, &add_token_handler, &add_context);
+
+                if(add_result == TotpIteratorUpdateTokenResultSuccess) {
+                    totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
+                } else if(add_result == TotpIteratorUpdateTokenResultInvalidSecret) {
+                    show_invalid_field_message(
+                        plugin_state, TokenSecretTextBox, "Token secret is invalid");
+                } else if(add_result == TotpIteratorUpdateTokenResultInvalidCounter) {
+                    show_invalid_field_message(
+                        plugin_state, TokenDurationOrCounterSelect, "Initial counter is invalid");
+                } else if(add_result == TotpIteratorUpdateTokenResultFileUpdateFailed) {
+                    totp_dialogs_config_updating_error(plugin_state);
+                }
+
+                break;
             }
             }
-            break;
-        case ConfirmButton: {
-            struct TotpAddContext add_context = {
-                .scene_state = scene_state, .crypto_settings = &plugin_state->crypto_settings};
-            TokenInfoIteratorContext* iterator_context =
-                totp_config_get_token_iterator_context(plugin_state);
-            TotpIteratorUpdateTokenResult add_result = totp_token_info_iterator_add_new_token(
-                iterator_context, &add_token_handler, &add_context);
-
-            if(add_result == TotpIteratorUpdateTokenResultSuccess) {
-                totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
-            } else if(add_result == TotpIteratorUpdateTokenResultInvalidSecret) {
-                show_invalid_field_message(
-                    plugin_state, TokenSecretTextBox, "Token secret is invalid");
-            } else if(add_result == TotpIteratorUpdateTokenResultInvalidCounter) {
-                show_invalid_field_message(
-                    plugin_state, TokenDurationOrCounterSelect, "Initial counter is invalid");
-            } else if(add_result == TotpIteratorUpdateTokenResultFileUpdateFailed) {
-                totp_dialogs_config_updating_error(plugin_state);
+            default:
+                break;
             }
             }
-
             break;
             break;
         }
         }
+        case InputKeyBack:
+            totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
+            break;
         default:
         default:
             break;
             break;
         }
         }

+ 29 - 28
ui/scenes/app_settings/totp_app_settings.c

@@ -330,6 +330,35 @@ bool totp_scene_app_settings_handle_event(
             }
             }
             break;
             break;
         case InputKeyOk:
         case InputKeyOk:
+            if(scene_state->selected_control == ConfirmButton) {
+                plugin_state->timezone_offset = (float)scene_state->tz_offset_hours +
+                                                (float)scene_state->tz_offset_minutes / 60.0f;
+
+                plugin_state->notification_method =
+                    (scene_state->notification_sound ? NotificationMethodSound :
+                                                       NotificationMethodNone) |
+                    (scene_state->notification_vibro ? NotificationMethodVibro :
+                                                       NotificationMethodNone);
+
+                plugin_state->automation_method = scene_state->automation_method;
+                plugin_state->active_font_index = scene_state->active_font_index;
+                plugin_state->automation_kb_layout = scene_state->automation_kb_layout;
+
+                if(!totp_config_file_update_user_settings(plugin_state)) {
+                    totp_dialogs_config_updating_error(plugin_state);
+                    return false;
+                }
+
+#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);
+                    plugin_state->bt_type_code_worker_context = NULL;
+                }
+#endif
+
+                totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu);
+            }
             break;
             break;
         case InputKeyBack: {
         case InputKeyBack: {
             totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu);
             totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu);
@@ -338,34 +367,6 @@ bool totp_scene_app_settings_handle_event(
         default:
         default:
             break;
             break;
         }
         }
-    } else if(
-        event->input.type == InputTypeShort && event->input.key == InputKeyOk &&
-        scene_state->selected_control == ConfirmButton) {
-        plugin_state->timezone_offset =
-            (float)scene_state->tz_offset_hours + (float)scene_state->tz_offset_minutes / 60.0f;
-
-        plugin_state->notification_method =
-            (scene_state->notification_sound ? NotificationMethodSound : NotificationMethodNone) |
-            (scene_state->notification_vibro ? NotificationMethodVibro : NotificationMethodNone);
-
-        plugin_state->automation_method = scene_state->automation_method;
-        plugin_state->active_font_index = scene_state->active_font_index;
-        plugin_state->automation_kb_layout = scene_state->automation_kb_layout;
-
-        if(!totp_config_file_update_user_settings(plugin_state)) {
-            totp_dialogs_config_updating_error(plugin_state);
-            return false;
-        }
-
-#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);
-            plugin_state->bt_type_code_worker_context = NULL;
-        }
-#endif
-
-        totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu);
     }
     }
 
 
     return true;
     return true;

+ 35 - 35
ui/scenes/authenticate/totp_scene_authenticate.c

@@ -84,7 +84,7 @@ bool totp_scene_authenticate_handle_event(
     }
     }
 
 
     SceneState* scene_state = plugin_state->current_scene_state;
     SceneState* scene_state = plugin_state->current_scene_state;
-    if(event->input.type == InputTypeShort) {
+    if(event->input.type == InputTypePress) {
         switch(event->input.key) {
         switch(event->input.key) {
         case InputKeyUp:
         case InputKeyUp:
             if(scene_state->code_length < MAX_CODE_LENGTH) {
             if(scene_state->code_length < MAX_CODE_LENGTH) {
@@ -110,8 +110,41 @@ bool totp_scene_authenticate_handle_event(
                 scene_state->code_length++;
                 scene_state->code_length++;
             }
             }
             break;
             break;
-        case InputKeyOk:
+        case InputKeyOk: {
+            CryptoSeedIVResult seed_result = totp_crypto_seed_iv(
+                &plugin_state->crypto_settings,
+                &scene_state->code_input[0],
+                scene_state->code_length);
+
+            if(seed_result & CryptoSeedIVResultFlagSuccess &&
+               seed_result & CryptoSeedIVResultFlagNewCryptoVerifyData) {
+                totp_config_file_update_crypto_signatures(plugin_state);
+            }
+
+            if(totp_crypto_verify_key(&plugin_state->crypto_settings)) {
+                totp_config_file_ensure_latest_encryption(
+                    plugin_state, &scene_state->code_input[0], scene_state->code_length);
+                totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
+            } else {
+                memset(&scene_state->code_input[0], 0, MAX_CODE_LENGTH);
+                memset(&plugin_state->crypto_settings.iv[0], 0, CRYPTO_IV_LENGTH);
+                scene_state->code_length = 0;
+
+                DialogMessage* message = dialog_message_alloc();
+                dialog_message_set_buttons(message, "Try again", NULL, NULL);
+                dialog_message_set_header(
+                    message,
+                    "You entered\ninvalid PIN",
+                    SCREEN_WIDTH_CENTER - 25,
+                    SCREEN_HEIGHT_CENTER - 5,
+                    AlignCenter,
+                    AlignCenter);
+                dialog_message_set_icon(message, &I_DolphinCommon_56x48, 72, 17);
+                dialog_message_show(plugin_state->dialogs_app, message);
+                dialog_message_free(message);
+            }
             break;
             break;
+        }
         case InputKeyBack:
         case InputKeyBack:
             if(scene_state->code_length > 0) {
             if(scene_state->code_length > 0) {
                 scene_state->code_input[scene_state->code_length - 1] = 0;
                 scene_state->code_input[scene_state->code_length - 1] = 0;
@@ -121,39 +154,6 @@ bool totp_scene_authenticate_handle_event(
         default:
         default:
             break;
             break;
         }
         }
-    } else if(event->input.type == InputTypeRelease && event->input.key == InputKeyOk) {
-        CryptoSeedIVResult seed_result = totp_crypto_seed_iv(
-            &plugin_state->crypto_settings, &scene_state->code_input[0], scene_state->code_length);
-
-        if(seed_result & CryptoSeedIVResultFlagSuccess &&
-           seed_result & CryptoSeedIVResultFlagNewCryptoVerifyData) {
-            totp_config_file_update_crypto_signatures(plugin_state);
-        }
-
-        if(totp_crypto_verify_key(&plugin_state->crypto_settings)) {
-            FURI_LOG_D(LOGGING_TAG, "PIN is valid");
-            totp_config_file_ensure_latest_encryption(
-                plugin_state, &scene_state->code_input[0], scene_state->code_length);
-            totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
-        } else {
-            FURI_LOG_D(LOGGING_TAG, "PIN is NOT valid");
-            memset(&scene_state->code_input[0], 0, MAX_CODE_LENGTH);
-            memset(&plugin_state->crypto_settings.iv[0], 0, CRYPTO_IV_LENGTH);
-            scene_state->code_length = 0;
-
-            DialogMessage* message = dialog_message_alloc();
-            dialog_message_set_buttons(message, "Try again", NULL, NULL);
-            dialog_message_set_header(
-                message,
-                "You entered\ninvalid PIN",
-                SCREEN_WIDTH_CENTER - 25,
-                SCREEN_HEIGHT_CENTER - 5,
-                AlignCenter,
-                AlignCenter);
-            dialog_message_set_icon(message, &I_DolphinCommon_56x48, 72, 17);
-            dialog_message_show(plugin_state->dialogs_app, message);
-            dialog_message_free(message);
-        }
     }
     }
 
 
     return true;
     return true;

+ 1 - 2
ui/scenes/generate_token/totp_scene_generate_token.c

@@ -422,14 +422,13 @@ bool totp_scene_generate_token_handle_event(
             break;
             break;
         }
         }
         case InputKeyOk:
         case InputKeyOk:
+            totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu);
             break;
             break;
         case InputKeyBack:
         case InputKeyBack:
             break;
             break;
         default:
         default:
             break;
             break;
         }
         }
-    } else if(event->input.type == InputTypeShort && event->input.key == InputKeyOk) {
-        totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu);
     }
     }
 
 
     return true;
     return true;

+ 52 - 52
ui/scenes/token_menu/totp_scene_token_menu.c

@@ -116,65 +116,65 @@ bool totp_scene_token_menu_handle_event(const PluginEvent* const event, PluginSt
             break;
             break;
         case InputKeyLeft:
         case InputKeyLeft:
             break;
             break;
-        case InputKeyOk:
-            break;
-        case InputKeyBack: {
-            totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
-            break;
-        }
-        default:
-            break;
-        }
-    } else if(event->input.type == InputTypeShort && event->input.key == InputKeyOk) {
-        switch(scene_state->selected_control) {
-        case AddNewToken: {
+        case InputKeyOk: {
+            switch(scene_state->selected_control) {
+            case AddNewToken: {
 #ifdef TOTP_UI_ADD_NEW_TOKEN_ENABLED
 #ifdef TOTP_UI_ADD_NEW_TOKEN_ENABLED
-            totp_scene_director_activate_scene(plugin_state, TotpSceneAddNewToken);
+                totp_scene_director_activate_scene(plugin_state, TotpSceneAddNewToken);
 #else
 #else
-            DialogMessage* message = dialog_message_alloc();
-            dialog_message_set_buttons(message, "Back", NULL, NULL);
-            dialog_message_set_header(message, "Information", 0, 0, AlignLeft, AlignTop);
-            dialog_message_set_text(
-                message,
-                "Read here\nhttps://t.ly/8ZOtj\n how to add new token",
-                SCREEN_WIDTH_CENTER,
-                SCREEN_HEIGHT_CENTER,
-                AlignCenter,
-                AlignCenter);
-            dialog_message_show(plugin_state->dialogs_app, message);
-            dialog_message_free(message);
-#endif
-            break;
-        }
-        case DeleteToken: {
-            DialogMessage* message = dialog_message_alloc();
-            dialog_message_set_buttons(message, "No", NULL, "Yes");
-            dialog_message_set_header(message, "Confirmation", 0, 0, AlignLeft, AlignTop);
-            dialog_message_set_text(
-                message,
-                "Are you sure want to delete?",
-                SCREEN_WIDTH_CENTER,
-                SCREEN_HEIGHT_CENTER,
-                AlignCenter,
-                AlignCenter);
-            DialogMessageButton dialog_result =
+                DialogMessage* message = dialog_message_alloc();
+                dialog_message_set_buttons(message, "Back", NULL, NULL);
+                dialog_message_set_header(message, "Information", 0, 0, AlignLeft, AlignTop);
+                dialog_message_set_text(
+                    message,
+                    "Read here\nhttps://t.ly/8ZOtj\nhow to add new token",
+                    SCREEN_WIDTH_CENTER,
+                    SCREEN_HEIGHT_CENTER,
+                    AlignCenter,
+                    AlignCenter);
                 dialog_message_show(plugin_state->dialogs_app, message);
                 dialog_message_show(plugin_state->dialogs_app, message);
-            dialog_message_free(message);
-            TokenInfoIteratorContext* iterator_context =
-                totp_config_get_token_iterator_context(plugin_state);
-            if(dialog_result == DialogMessageButtonRight &&
-               totp_token_info_iterator_get_total_count(iterator_context) > 0) {
-                if(!totp_token_info_iterator_remove_current_token_info(iterator_context)) {
-                    totp_dialogs_config_updating_error(plugin_state);
-                    return false;
-                }
+                dialog_message_free(message);
+#endif
+                break;
+            }
+            case DeleteToken: {
+                DialogMessage* message = dialog_message_alloc();
+                dialog_message_set_buttons(message, "No", NULL, "Yes");
+                dialog_message_set_header(message, "Confirmation", 0, 0, AlignLeft, AlignTop);
+                dialog_message_set_text(
+                    message,
+                    "Are you sure want to delete?",
+                    SCREEN_WIDTH_CENTER,
+                    SCREEN_HEIGHT_CENTER,
+                    AlignCenter,
+                    AlignCenter);
+                DialogMessageButton dialog_result =
+                    dialog_message_show(plugin_state->dialogs_app, message);
+                dialog_message_free(message);
+                TokenInfoIteratorContext* iterator_context =
+                    totp_config_get_token_iterator_context(plugin_state);
+                if(dialog_result == DialogMessageButtonRight &&
+                   totp_token_info_iterator_get_total_count(iterator_context) > 0) {
+                    if(!totp_token_info_iterator_remove_current_token_info(iterator_context)) {
+                        totp_dialogs_config_updating_error(plugin_state);
+                        return false;
+                    }
 
 
-                totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
+                    totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
+                }
+                break;
+            }
+            case AppSettings: {
+                totp_scene_director_activate_scene(plugin_state, TotpSceneAppSettings);
+                break;
+            }
+            default:
+                break;
             }
             }
             break;
             break;
         }
         }
-        case AppSettings: {
-            totp_scene_director_activate_scene(plugin_state, TotpSceneAppSettings);
+        case InputKeyBack: {
+            totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
             break;
             break;
         }
         }
         default:
         default:

+ 6 - 4
workers/type_code_common.c

@@ -88,6 +88,8 @@ void totp_type_code_worker_execute_automation(
         return;
         return;
     }
     }
 
 
+    uint32_t keystroke_delay = get_keystroke_delay(features);
+
     while(i < code_buffer_size && (cb_char = code_buffer[i]) != 0) {
     while(i < code_buffer_size && (cb_char = code_buffer[i]) != 0) {
         uint8_t char_index = CONVERT_CHAR_TO_DIGIT(cb_char);
         uint8_t char_index = CONVERT_CHAR_TO_DIGIT(cb_char);
         if(char_index > 9) {
         if(char_index > 9) {
@@ -105,18 +107,18 @@ void totp_type_code_worker_execute_automation(
         }
         }
 
 
         totp_type_code_worker_press_key(hid_kb_key, key_press_fn, key_release_fn, features);
         totp_type_code_worker_press_key(hid_kb_key, key_press_fn, key_release_fn, features);
-        furi_delay_ms(get_keystroke_delay(features));
+        furi_delay_ms(keystroke_delay);
         i++;
         i++;
     }
     }
 
 
     if(features & TokenAutomationFeatureEnterAtTheEnd) {
     if(features & TokenAutomationFeatureEnterAtTheEnd) {
-        furi_delay_ms(get_keystroke_delay(features));
+        furi_delay_ms(keystroke_delay);
         totp_type_code_worker_press_key(
         totp_type_code_worker_press_key(
             HID_KEYBOARD_RETURN, key_press_fn, key_release_fn, features);
             HID_KEYBOARD_RETURN, key_press_fn, key_release_fn, features);
     }
     }
 
 
     if(features & TokenAutomationFeatureTabAtTheEnd) {
     if(features & TokenAutomationFeatureTabAtTheEnd) {
-        furi_delay_ms(get_keystroke_delay(features));
+        furi_delay_ms(keystroke_delay);
         totp_type_code_worker_press_key(HID_KEYBOARD_TAB, key_press_fn, key_release_fn, features);
         totp_type_code_worker_press_key(HID_KEYBOARD_TAB, key_press_fn, key_release_fn, features);
     }
     }
-}
+}