Skip to content

Commit

Permalink
Implemented #26 (#27)
Browse files Browse the repository at this point in the history
* Implemented #26 
* Improved HID worker shutdown code
* Refactoring
  • Loading branch information
akopachov committed Nov 17, 2022
1 parent 9dcb6cb commit ee4252c
Show file tree
Hide file tree
Showing 26 changed files with 780 additions and 97 deletions.
2 changes: 1 addition & 1 deletion scenes/app_settings/totp_app_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ bool totp_scene_app_settings_handle_event(
}

SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
if(event->input.type != InputTypePress) {
if(event->input.type != InputTypePress && event->input.type != InputTypeRepeat) {
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion scenes/generate_token/totp_scene_generate_token.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ bool totp_scene_generate_token_handle_event(
return true;
}

if(event->input.type != InputTypePress) {
if(event->input.type != InputTypePress && event->input.type != InputTypeRepeat) {
return true;
}

Expand Down
34 changes: 34 additions & 0 deletions scenes/scene_director.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,46 @@
#include "../types/plugin_event.h"
#include "totp_scenes_enum.h"

/**
* @brief Activates scene
* @param plugin_state application state
* @param scene scene to be activated
* @param context scene context to be passed to the scene activation method
*/
void totp_scene_director_activate_scene(
PluginState* const plugin_state,
Scene scene,
const void* context);

/**
* @brief Deactivate current scene
* @param plugin_state application state
*/
void totp_scene_director_deactivate_active_scene(PluginState* const plugin_state);

/**
* @brief Initializes all the available scenes
* @param plugin_state application state
*/
void totp_scene_director_init_scenes(PluginState* const plugin_state);

/**
* @brief Renders current scene
* @param canvas canvas to render at
* @param plugin_state application state
*/
void totp_scene_director_render(Canvas* const canvas, PluginState* const plugin_state);

/**
* @brief Disposes all the available scenes
* @param plugin_state application state
*/
void totp_scene_director_dispose(const PluginState* const plugin_state);

/**
* @brief Handles application event for the current scene
* @param event event to be handled
* @param plugin_state application state
* @return \c true if event handled and applilcation should continue; \c false if application should be closed
*/
bool totp_scene_director_handle_event(PluginEvent* const event, PluginState* const plugin_state);
13 changes: 7 additions & 6 deletions scenes/token_menu/totp_scene_token_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,14 @@ bool totp_scene_token_menu_handle_event(const PluginEvent* const event, PluginSt
dialog_message_free(message);
if(dialog_result == DialogMessageButtonRight &&
!scene_state->current_token_index.is_null) {
ListNode* list_node = list_element_at(
plugin_state->tokens_list, scene_state->current_token_index.value);

TokenInfo* tokenInfo = list_node->data;
token_info_free(tokenInfo);
plugin_state->tokens_list = list_remove(plugin_state->tokens_list, list_node);
TokenInfo* tokenInfo = NULL;
plugin_state->tokens_list = list_remove_at(
plugin_state->tokens_list,
scene_state->current_token_index.value,
(void**)&tokenInfo);
plugin_state->tokens_count--;
furi_check(tokenInfo != NULL);
token_info_free(tokenInfo);

totp_full_save_config_file(plugin_state);
totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
Expand Down
26 changes: 26 additions & 0 deletions scenes/totp_scenes_enum.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
#pragma once

/**
* @brief TOTP application scenes
*/
typedef enum {
/**
* @brief Empty scene which does nothing
*/
TotpSceneNone,

/**
* @brief Scene where user have to enter PIN to authenticate
*/
TotpSceneAuthentication,

/**
* @brief Scene where actual TOTP token is getting generated and displayed to the user
*/
TotpSceneGenerateToken,

/**
* @brief Scene where user can add new token
*/
TotpSceneAddNewToken,

/**
* @brief Scene with a menu for given token, allowing user to do multiple actions
*/
TotpSceneTokenMenu,

/**
* @brief Scene where user can change application settings
*/
TotpSceneAppSettings
} Scene;
2 changes: 0 additions & 2 deletions services/base32/base32.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <string.h>

#include "base32.h"

int base32_decode(const uint8_t* encoded, uint8_t* result, int bufSize) {
Expand Down
10 changes: 8 additions & 2 deletions services/base32/base32.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,11 @@

#include <stdint.h>

int base32_decode(const uint8_t* encoded, uint8_t* result, int bufSize)
__attribute__((visibility("hidden")));
/**
* @brief Decodes Base-32 encoded bytes into plain bytes.
* @param encoded Base-32 encoded bytes
* @param[out] result result output buffer
* @param bufSize result output buffer size
* @return Decoded result length in bytes if successfully decoded; \c -1 otherwise
*/
int base32_decode(const uint8_t* encoded, uint8_t* result, int bufSize);
5 changes: 5 additions & 0 deletions services/cli/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "commands/delete/delete.h"
#include "commands/timezone/timezone.h"
#include "commands/help/help.h"
#include "commands/move/move.h"

static void totp_cli_print_unknown_command(const FuriString* unknown_command) {
TOTP_CLI_PRINTF(
Expand Down Expand Up @@ -44,6 +45,10 @@ static void totp_cli_handler(Cli* cli, FuriString* args, void* context) {
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_TIMEZONE) == 0 ||
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_TIMEZONE_ALT) == 0) {
totp_cli_command_timezone_handle(plugin_state, args, cli);
} else if(
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_MOVE) == 0 ||
furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_MOVE_ALT) == 0) {
totp_cli_command_move_handle(plugin_state, args, cli);
} else {
totp_cli_print_unknown_command(cmd);
}
Expand Down
2 changes: 2 additions & 0 deletions services/cli/commands/add/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
} else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_ADD_ARG_UNSECURE_PREFIX) == 0) {
mask_user_input = false;
parsed = true;
} else {
TOTP_CLI_PRINTF("Unknown argument \"%s\"\r\n", furi_string_get_cstr(temp_str));
}

if(!parsed) {
Expand Down
4 changes: 4 additions & 0 deletions services/cli/commands/help/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "../delete/delete.h"
#include "../list/list.h"
#include "../timezone/timezone.h"
#include "../move/move.h"

void totp_cli_command_help_docopt_commands() {
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_HELP ", " TOTP_CLI_COMMAND_HELP_ALT
Expand All @@ -23,13 +24,15 @@ void totp_cli_command_help_handle() {
totp_cli_command_add_docopt_usage();
totp_cli_command_delete_docopt_usage();
totp_cli_command_timezone_docopt_usage();
totp_cli_command_move_docopt_usage();
cli_nl();
TOTP_CLI_PRINTF("Commands:\r\n");
totp_cli_command_help_docopt_commands();
totp_cli_command_list_docopt_commands();
totp_cli_command_add_docopt_commands();
totp_cli_command_delete_docopt_commands();
totp_cli_command_timezone_docopt_commands();
totp_cli_command_move_docopt_commands();
cli_nl();
TOTP_CLI_PRINTF("Arguments:\r\n");
totp_cli_command_add_docopt_arguments();
Expand All @@ -39,4 +42,5 @@ void totp_cli_command_help_handle() {
TOTP_CLI_PRINTF("Options:\r\n");
totp_cli_command_add_docopt_options();
totp_cli_command_delete_docopt_options();
totp_cli_command_move_docopt_options();
}
164 changes: 164 additions & 0 deletions services/cli/commands/move/move.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#include "move.h"

#include <stdlib.h>
#include <lib/toolbox/args.h>
#include "../../../list/list.h"
#include "../../../../types/token_info.h"
#include "../../../config/config.h"
#include "../../cli_helpers.h"
#include "../../../../scenes/scene_director.h"

#define TOTP_CLI_COMMAND_MOVE_ARG_INDEX "index"

#define TOTP_CLI_COMMAND_MOVE_ARG_NEW_NAME "name"
#define TOTP_CLI_COMMAND_MOVE_ARG_NEW_NAME_PREFIX "-n"

#define TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX "index"
#define TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX "-i"

void totp_cli_command_move_docopt_commands() {
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_MOVE ", " TOTP_CLI_COMMAND_MOVE_ALT
" Move\\rename token\r\n");
}

void totp_cli_command_move_docopt_usage() {
TOTP_CLI_PRINTF(
" " TOTP_CLI_COMMAND_NAME
" " DOCOPT_REQUIRED(TOTP_CLI_COMMAND_MOVE " | " TOTP_CLI_COMMAND_MOVE_ALT) " " DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_MOVE_ARG_INDEX) " " DOCOPT_OPTIONAL(
DOCOPT_OPTION(
TOTP_CLI_COMMAND_MOVE_ARG_NEW_NAME_PREFIX,
DOCOPT_ARGUMENT(
TOTP_CLI_COMMAND_MOVE_ARG_NEW_NAME))) " " DOCOPT_OPTIONAL(DOCOPT_OPTION(TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX, DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX))) "\r\n");
}

void totp_cli_command_move_docopt_options() {
TOTP_CLI_PRINTF(" " DOCOPT_OPTION(
TOTP_CLI_COMMAND_MOVE_ARG_NEW_NAME_PREFIX,
DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_MOVE_ARG_NEW_NAME)) " New token name.\r\n");
TOTP_CLI_PRINTF(" " DOCOPT_OPTION(
TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX,
DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX)) " New token index.\r\n");
}

void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
int token_index;
if(!args_read_int_and_trim(args, &token_index)) {
TOTP_CLI_PRINT_INVALID_ARGUMENTS();
return;
}

if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
return;
}

if(token_index < 1 || token_index > plugin_state->tokens_count) {
TOTP_CLI_PRINT_INVALID_ARGUMENTS();
return;
}

FuriString* temp_str = furi_string_alloc();

char* new_token_name = NULL;
int new_token_index = 0;

while(args_read_string_and_trim(args, temp_str)) {
bool parsed = false;
if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_MOVE_ARG_NEW_NAME_PREFIX) == 0) {
if(!args_read_string_and_trim(args, temp_str)) {
TOTP_CLI_PRINTF(
"Missed value for argument \"" TOTP_CLI_COMMAND_MOVE_ARG_NEW_NAME_PREFIX
"\"\r\n");
} else {
if(new_token_name != NULL) {
free(new_token_name);
}

new_token_name = malloc(furi_string_size(temp_str) + 1);
if(new_token_name == NULL) {
furi_string_free(temp_str);
return;
}

strlcpy(
new_token_name,
furi_string_get_cstr(temp_str),
furi_string_size(temp_str) + 1);
parsed = true;
}
} else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX) == 0) {
if(!args_read_int_and_trim(args, &new_token_index)) {
TOTP_CLI_PRINTF(
"Missed value for argument \"" TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX
"\"\r\n");
} else if(new_token_index < 1 || new_token_index > plugin_state->tokens_count) {
TOTP_CLI_PRINTF(
"\"%" PRId16
"\" is incorrect value for argument \"" TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX
"\"\r\n",
new_token_index);
} else {
parsed = true;
}
} else {
TOTP_CLI_PRINTF("Unknown argument \"%s\"\r\n", furi_string_get_cstr(temp_str));
}

if(!parsed) {
TOTP_CLI_PRINT_INVALID_ARGUMENTS();
furi_string_free(temp_str);
if(new_token_name != NULL) {
free(new_token_name);
}
return;
}
}

if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
furi_string_free(temp_str);
if(new_token_name != NULL) {
free(new_token_name);
}
return;
}

bool activate_generate_token_scene = false;
if(plugin_state->current_scene != TotpSceneAuthentication) {
totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
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;
}

if(new_token_name != NULL) {
free(token_info->name);
token_info->name = new_token_name;
token_updated = true;
}

if(token_updated) {
totp_full_save_config_file(plugin_state);
}

if(activate_generate_token_scene) {
totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
}

if(token_updated) {
TOTP_CLI_PRINTF("Token \"%s\" has been successfully updated\r\n", token_info->name);
} else {
TOTP_CLI_PRINT_INVALID_ARGUMENTS();
}

furi_string_free(temp_str);
}
12 changes: 12 additions & 0 deletions services/cli/commands/move/move.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <cli/cli.h>
#include "../../../../types/plugin_state.h"

#define TOTP_CLI_COMMAND_MOVE "move"
#define TOTP_CLI_COMMAND_MOVE_ALT "mv"

void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, Cli* cli);
void totp_cli_command_move_docopt_commands();
void totp_cli_command_move_docopt_usage();
void totp_cli_command_move_docopt_options();
Loading

0 comments on commit ee4252c

Please sign in to comment.