Skip to content

Commit

Permalink
Implemented #36 (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
akopachov committed Nov 23, 2022
1 parent fbb9d22 commit 805adfe
Show file tree
Hide file tree
Showing 31 changed files with 483 additions and 209 deletions.
3 changes: 3 additions & 0 deletions cli/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "commands/help/help.h"
#include "commands/move/move.h"
#include "commands/pin/pin.h"
#include "commands/notification/notification.h"

static void totp_cli_print_unknown_command(const FuriString* unknown_command) {
TOTP_CLI_PRINTF(
Expand Down Expand Up @@ -52,6 +53,8 @@ static void totp_cli_handler(Cli* cli, FuriString* args, void* context) {
totp_cli_command_move_handle(plugin_state, args, cli);
} else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_PIN) == 0) {
totp_cli_command_pin_handle(plugin_state, args, cli);
} else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_NOTIFICATION) == 0) {
totp_cli_command_notification_handle(plugin_state, args, cli);
} else {
totp_cli_print_unknown_command(cmd);
}
Expand Down
1 change: 1 addition & 0 deletions cli/cli_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define TOTP_CLI_COMMAND_NAME "totp"

#define DOCOPT_ARGUMENT(arg) "<" arg ">"
#define DOCOPT_MULTIPLE(arg) arg "..."
#define DOCOPT_OPTIONAL(param) "[" param "]"
#define DOCOPT_REQUIRED(param) "(" param ")"
#define DOCOPT_OPTION(option, value) option " " value
Expand Down
20 changes: 4 additions & 16 deletions cli/commands/add/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "../../../lib/list/list.h"
#include "../../../types/token_info.h"
#include "../../../services/config/config.h"
#include "../../../services/convert/convert.h"
#include "../../cli_helpers.h"
#include "../../../ui/scene_director.h"

Expand All @@ -14,21 +15,6 @@
#define TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX "-d"
#define TOTP_CLI_COMMAND_ADD_ARG_UNSECURE_PREFIX "-u"

static bool token_info_set_digits_from_str(TokenInfo* token_info, const FuriString* str) {
switch(furi_string_get_char(str, 0)) {
case '6':
token_info->digits = TOTP_6_DIGITS;
return true;
case '8':
token_info->digits = TOTP_8_DIGITS;
return true;
default:
break;
}

return false;
}

static bool token_info_set_algo_from_str(TokenInfo* token_info, const FuriString* str) {
if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME) == 0) {
token_info->algo = SHA1;
Expand Down Expand Up @@ -73,6 +59,7 @@ void totp_cli_command_add_docopt_options() {
DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_ADD_ARG_ALGO)) " Token hashing algorithm.\r\n");
TOTP_CLI_PRINTF(
" Could be one of: sha1, sha256, sha512 " DOCOPT_DEFAULT("sha1") "\r\n");
cli_nl();
TOTP_CLI_PRINTF(" " DOCOPT_OPTION(
TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX,
DOCOPT_ARGUMENT(
Expand Down Expand Up @@ -164,7 +151,8 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
TOTP_CLI_PRINTF(
"Missed value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX
"\"\r\n");
} else if(!token_info_set_digits_from_str(token_info, temp_str)) {
} else if(!token_info_set_digits_from_int(
token_info, CONVERT_CHAR_TO_DIGIT(furi_string_get_char(temp_str, 0)))) {
TOTP_CLI_PRINTF(
"\"%s\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX
"\"\r\n",
Expand Down
4 changes: 4 additions & 0 deletions cli/commands/help/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "../timezone/timezone.h"
#include "../move/move.h"
#include "../pin/pin.h"
#include "../notification/notification.h"

void totp_cli_command_help_docopt_commands() {
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_HELP ", " TOTP_CLI_COMMAND_HELP_ALT
Expand All @@ -27,6 +28,7 @@ void totp_cli_command_help_handle() {
totp_cli_command_timezone_docopt_usage();
totp_cli_command_move_docopt_usage();
totp_cli_command_pin_docopt_usage();
totp_cli_command_notification_docopt_usage();
cli_nl();
TOTP_CLI_PRINTF("Commands:\r\n");
totp_cli_command_help_docopt_commands();
Expand All @@ -36,11 +38,13 @@ void totp_cli_command_help_handle() {
totp_cli_command_timezone_docopt_commands();
totp_cli_command_move_docopt_commands();
totp_cli_command_pin_docopt_commands();
totp_cli_command_notification_docopt_commands();
cli_nl();
TOTP_CLI_PRINTF("Arguments:\r\n");
totp_cli_command_add_docopt_arguments();
totp_cli_command_delete_docopt_arguments();
totp_cli_command_timezone_docopt_arguments();
totp_cli_command_notification_docopt_arguments();
cli_nl();
TOTP_CLI_PRINTF("Options:\r\n");
totp_cli_command_add_docopt_options();
Expand Down
16 changes: 1 addition & 15 deletions cli/commands/list/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,6 @@ static char* get_algo_as_cstr(TokenHashAlgo algo) {
return "UNKNOWN";
}

static uint8_t get_digits_as_int(TokenDigitsCount digits) {
switch(digits) {
case TOTP_6_DIGITS:
return 6;
case TOTP_8_DIGITS:
return 8;
default:
break;
}

return 6;
}

void totp_cli_command_list_docopt_commands() {
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_LIST ", " TOTP_CLI_COMMAND_LIST_ALT
" List all available tokens\r\n");
Expand All @@ -59,13 +46,12 @@ void totp_cli_command_list_handle(PluginState* plugin_state, Cli* cli) {
uint16_t index = 1;
TOTP_LIST_FOREACH(plugin_state->tokens_list, node, {
TokenInfo* token_info = (TokenInfo*)node->data;
token_info_get_digits_count(token_info);
TOTP_CLI_PRINTF(
"| %-3" PRIu16 " | %-27.27s | %-6s | %-6" PRIu8 " |\r\n",
index,
token_info->name,
get_algo_as_cstr(token_info->algo),
get_digits_as_int(token_info->digits));
token_info->digits);
index++;
});
TOTP_CLI_PRINTF("+-----+-----------------------------+--------+--------+\r\n");
Expand Down
106 changes: 106 additions & 0 deletions cli/commands/notification/notification.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#include "notification.h"
#include <lib/toolbox/args.h>
#include "../../../services/config/config.h"
#include "../../../ui/scene_director.h"
#include "../../cli_helpers.h"

#define TOTP_CLI_COMMAND_NOTIFICATION_ARG_METHOD "method"
#define TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE "none"
#define TOTP_CLI_COMMAND_NOTIFICATION_METHOD_SOUND "sound"
#define TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO "vibro"

void totp_cli_command_notification_docopt_commands() {
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_NOTIFICATION
" Get or set notification method\r\n");
}

void totp_cli_command_notification_docopt_usage() {
TOTP_CLI_PRINTF(
" " TOTP_CLI_COMMAND_NAME " " TOTP_CLI_COMMAND_NOTIFICATION " " DOCOPT_OPTIONAL(
DOCOPT_MULTIPLE(DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_NOTIFICATION_ARG_METHOD))) "\r\n");
}

void totp_cli_command_notification_docopt_arguments() {
TOTP_CLI_PRINTF(
" " TOTP_CLI_COMMAND_NOTIFICATION_ARG_METHOD
" Notification method to be set. Must be one of [" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE
", " TOTP_CLI_COMMAND_NOTIFICATION_METHOD_SOUND
", " TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO "]\r\n");
}

static void totp_cli_command_notification_print_method(NotificationMethod method) {
bool has_previous_method = false;
if(method & NotificationMethodSound) {
TOTP_CLI_PRINTF("\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_SOUND "\"");
has_previous_method = true;
}
if(method & NotificationMethodVibro) {
if(has_previous_method) {
TOTP_CLI_PRINTF(" and ");
}

TOTP_CLI_PRINTF("\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO "\"");
}
if(method == NotificationMethodNone) {
TOTP_CLI_PRINTF("\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE "\"");
}
}

void totp_cli_command_notification_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
return;
}

FuriString* temp_str = furi_string_alloc();
bool new_method_provided = false;
NotificationMethod new_method = NotificationMethodNone;
bool args_valid = true;
while(args_read_string_and_trim(args, temp_str)) {
if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE) == 0) {
new_method_provided = true;
new_method = NotificationMethodNone;
} else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_NOTIFICATION_METHOD_SOUND) == 0) {
new_method_provided = true;
new_method |= NotificationMethodSound;
} else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO) == 0) {
new_method_provided = true;
new_method |= NotificationMethodVibro;
} else {
args_valid = false;
break;
}
}

do {
if(!args_valid) {
TOTP_CLI_PRINT_INVALID_ARGUMENTS();
break;
}

if(new_method_provided) {
Scene previous_scene = TotpSceneNone;
if(plugin_state->current_scene == TotpSceneGenerateToken ||
plugin_state->current_scene == TotpSceneAppSettings) {
previous_scene = plugin_state->current_scene;
totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
}

plugin_state->notification_method = new_method;
totp_config_file_update_notification_method(new_method);

if(previous_scene != TotpSceneNone) {
totp_scene_director_activate_scene(plugin_state, previous_scene, NULL);
}

TOTP_CLI_PRINTF("Notification method is set to ");
totp_cli_command_notification_print_method(new_method);
cli_nl();
} else {
TOTP_CLI_PRINTF("Current notification method is ");
totp_cli_command_notification_print_method(plugin_state->notification_method);
cli_nl();
}
} while(false);

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

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

#define TOTP_CLI_COMMAND_NOTIFICATION "notify"

void totp_cli_command_notification_handle(PluginState* plugin_state, FuriString* args, Cli* cli);
void totp_cli_command_notification_docopt_commands();
void totp_cli_command_notification_docopt_usage();
void totp_cli_command_notification_docopt_arguments();
2 changes: 0 additions & 2 deletions cli/commands/timezone/timezone.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ void totp_cli_command_timezone_docopt_usage() {
void totp_cli_command_timezone_docopt_arguments() {
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_TIMEZONE_ARG_TIMEZONE
" Timezone offset in hours to be set.\r\n");
TOTP_CLI_PRINTF(
" If not provided then current timezone offset will be printed\r\n");
}

void totp_cli_command_timezone_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
Expand Down
Binary file added images/totp_arrow_bottom_10x5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions lib/roll_value/roll_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

#include <stdint.h>

typedef enum {
typedef uint8_t TotpRollValueOverflowBehavior;

enum TotpRollValueOverflowBehaviors {
/**
* @brief Do not change value if it reached constraint
*/
Expand All @@ -12,7 +14,7 @@ typedef enum {
* @brief Set value to opposite constraint value if it reached constraint
*/
RollOverflowBehaviorRoll
} TotpRollValueOverflowBehavior;
};

#define TOTP_ROLL_VALUE_FN_HEADER(type, step_type) \
void totp_roll_value_##type( \
Expand Down
Loading

0 comments on commit 805adfe

Please sign in to comment.