Skip to content

Commit d6e6ba0

Browse files
authored
Implemented #36 (#38)
1 parent 981f8c3 commit d6e6ba0

31 files changed

+483
-209
lines changed

totp/cli/cli.c

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "commands/help/help.h"
1111
#include "commands/move/move.h"
1212
#include "commands/pin/pin.h"
13+
#include "commands/notification/notification.h"
1314

1415
static void totp_cli_print_unknown_command(const FuriString* unknown_command) {
1516
TOTP_CLI_PRINTF(
@@ -52,6 +53,8 @@ static void totp_cli_handler(Cli* cli, FuriString* args, void* context) {
5253
totp_cli_command_move_handle(plugin_state, args, cli);
5354
} else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_PIN) == 0) {
5455
totp_cli_command_pin_handle(plugin_state, args, cli);
56+
} else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_NOTIFICATION) == 0) {
57+
totp_cli_command_notification_handle(plugin_state, args, cli);
5558
} else {
5659
totp_cli_print_unknown_command(cmd);
5760
}

totp/cli/cli_helpers.h

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define TOTP_CLI_COMMAND_NAME "totp"
77

88
#define DOCOPT_ARGUMENT(arg) "<" arg ">"
9+
#define DOCOPT_MULTIPLE(arg) arg "..."
910
#define DOCOPT_OPTIONAL(param) "[" param "]"
1011
#define DOCOPT_REQUIRED(param) "(" param ")"
1112
#define DOCOPT_OPTION(option, value) option " " value

totp/cli/commands/add/add.c

+4-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "../../../lib/list/list.h"
55
#include "../../../types/token_info.h"
66
#include "../../../services/config/config.h"
7+
#include "../../../services/convert/convert.h"
78
#include "../../cli_helpers.h"
89
#include "../../../ui/scene_director.h"
910

@@ -14,21 +15,6 @@
1415
#define TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX "-d"
1516
#define TOTP_CLI_COMMAND_ADD_ARG_UNSECURE_PREFIX "-u"
1617

17-
static bool token_info_set_digits_from_str(TokenInfo* token_info, const FuriString* str) {
18-
switch(furi_string_get_char(str, 0)) {
19-
case '6':
20-
token_info->digits = TOTP_6_DIGITS;
21-
return true;
22-
case '8':
23-
token_info->digits = TOTP_8_DIGITS;
24-
return true;
25-
default:
26-
break;
27-
}
28-
29-
return false;
30-
}
31-
3218
static bool token_info_set_algo_from_str(TokenInfo* token_info, const FuriString* str) {
3319
if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME) == 0) {
3420
token_info->algo = SHA1;
@@ -73,6 +59,7 @@ void totp_cli_command_add_docopt_options() {
7359
DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_ADD_ARG_ALGO)) " Token hashing algorithm.\r\n");
7460
TOTP_CLI_PRINTF(
7561
" Could be one of: sha1, sha256, sha512 " DOCOPT_DEFAULT("sha1") "\r\n");
62+
cli_nl();
7663
TOTP_CLI_PRINTF(" " DOCOPT_OPTION(
7764
TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX,
7865
DOCOPT_ARGUMENT(
@@ -164,7 +151,8 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
164151
TOTP_CLI_PRINTF(
165152
"Missed value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX
166153
"\"\r\n");
167-
} else if(!token_info_set_digits_from_str(token_info, temp_str)) {
154+
} else if(!token_info_set_digits_from_int(
155+
token_info, CONVERT_CHAR_TO_DIGIT(furi_string_get_char(temp_str, 0)))) {
168156
TOTP_CLI_PRINTF(
169157
"\"%s\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX
170158
"\"\r\n",

totp/cli/commands/help/help.c

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "../timezone/timezone.h"
77
#include "../move/move.h"
88
#include "../pin/pin.h"
9+
#include "../notification/notification.h"
910

1011
void totp_cli_command_help_docopt_commands() {
1112
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_HELP ", " TOTP_CLI_COMMAND_HELP_ALT
@@ -27,6 +28,7 @@ void totp_cli_command_help_handle() {
2728
totp_cli_command_timezone_docopt_usage();
2829
totp_cli_command_move_docopt_usage();
2930
totp_cli_command_pin_docopt_usage();
31+
totp_cli_command_notification_docopt_usage();
3032
cli_nl();
3133
TOTP_CLI_PRINTF("Commands:\r\n");
3234
totp_cli_command_help_docopt_commands();
@@ -36,11 +38,13 @@ void totp_cli_command_help_handle() {
3638
totp_cli_command_timezone_docopt_commands();
3739
totp_cli_command_move_docopt_commands();
3840
totp_cli_command_pin_docopt_commands();
41+
totp_cli_command_notification_docopt_commands();
3942
cli_nl();
4043
TOTP_CLI_PRINTF("Arguments:\r\n");
4144
totp_cli_command_add_docopt_arguments();
4245
totp_cli_command_delete_docopt_arguments();
4346
totp_cli_command_timezone_docopt_arguments();
47+
totp_cli_command_notification_docopt_arguments();
4448
cli_nl();
4549
TOTP_CLI_PRINTF("Options:\r\n");
4650
totp_cli_command_add_docopt_options();

totp/cli/commands/list/list.c

+1-15
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,6 @@ static char* get_algo_as_cstr(TokenHashAlgo algo) {
2020
return "UNKNOWN";
2121
}
2222

23-
static uint8_t get_digits_as_int(TokenDigitsCount digits) {
24-
switch(digits) {
25-
case TOTP_6_DIGITS:
26-
return 6;
27-
case TOTP_8_DIGITS:
28-
return 8;
29-
default:
30-
break;
31-
}
32-
33-
return 6;
34-
}
35-
3623
void totp_cli_command_list_docopt_commands() {
3724
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_LIST ", " TOTP_CLI_COMMAND_LIST_ALT
3825
" List all available tokens\r\n");
@@ -59,13 +46,12 @@ void totp_cli_command_list_handle(PluginState* plugin_state, Cli* cli) {
5946
uint16_t index = 1;
6047
TOTP_LIST_FOREACH(plugin_state->tokens_list, node, {
6148
TokenInfo* token_info = (TokenInfo*)node->data;
62-
token_info_get_digits_count(token_info);
6349
TOTP_CLI_PRINTF(
6450
"| %-3" PRIu16 " | %-27.27s | %-6s | %-6" PRIu8 " |\r\n",
6551
index,
6652
token_info->name,
6753
get_algo_as_cstr(token_info->algo),
68-
get_digits_as_int(token_info->digits));
54+
token_info->digits);
6955
index++;
7056
});
7157
TOTP_CLI_PRINTF("+-----+-----------------------------+--------+--------+\r\n");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#include "notification.h"
2+
#include <lib/toolbox/args.h>
3+
#include "../../../services/config/config.h"
4+
#include "../../../ui/scene_director.h"
5+
#include "../../cli_helpers.h"
6+
7+
#define TOTP_CLI_COMMAND_NOTIFICATION_ARG_METHOD "method"
8+
#define TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE "none"
9+
#define TOTP_CLI_COMMAND_NOTIFICATION_METHOD_SOUND "sound"
10+
#define TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO "vibro"
11+
12+
void totp_cli_command_notification_docopt_commands() {
13+
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_NOTIFICATION
14+
" Get or set notification method\r\n");
15+
}
16+
17+
void totp_cli_command_notification_docopt_usage() {
18+
TOTP_CLI_PRINTF(
19+
" " TOTP_CLI_COMMAND_NAME " " TOTP_CLI_COMMAND_NOTIFICATION " " DOCOPT_OPTIONAL(
20+
DOCOPT_MULTIPLE(DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_NOTIFICATION_ARG_METHOD))) "\r\n");
21+
}
22+
23+
void totp_cli_command_notification_docopt_arguments() {
24+
TOTP_CLI_PRINTF(
25+
" " TOTP_CLI_COMMAND_NOTIFICATION_ARG_METHOD
26+
" Notification method to be set. Must be one of [" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE
27+
", " TOTP_CLI_COMMAND_NOTIFICATION_METHOD_SOUND
28+
", " TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO "]\r\n");
29+
}
30+
31+
static void totp_cli_command_notification_print_method(NotificationMethod method) {
32+
bool has_previous_method = false;
33+
if(method & NotificationMethodSound) {
34+
TOTP_CLI_PRINTF("\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_SOUND "\"");
35+
has_previous_method = true;
36+
}
37+
if(method & NotificationMethodVibro) {
38+
if(has_previous_method) {
39+
TOTP_CLI_PRINTF(" and ");
40+
}
41+
42+
TOTP_CLI_PRINTF("\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO "\"");
43+
}
44+
if(method == NotificationMethodNone) {
45+
TOTP_CLI_PRINTF("\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE "\"");
46+
}
47+
}
48+
49+
void totp_cli_command_notification_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
50+
if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
51+
return;
52+
}
53+
54+
FuriString* temp_str = furi_string_alloc();
55+
bool new_method_provided = false;
56+
NotificationMethod new_method = NotificationMethodNone;
57+
bool args_valid = true;
58+
while(args_read_string_and_trim(args, temp_str)) {
59+
if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE) == 0) {
60+
new_method_provided = true;
61+
new_method = NotificationMethodNone;
62+
} else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_NOTIFICATION_METHOD_SOUND) == 0) {
63+
new_method_provided = true;
64+
new_method |= NotificationMethodSound;
65+
} else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO) == 0) {
66+
new_method_provided = true;
67+
new_method |= NotificationMethodVibro;
68+
} else {
69+
args_valid = false;
70+
break;
71+
}
72+
}
73+
74+
do {
75+
if(!args_valid) {
76+
TOTP_CLI_PRINT_INVALID_ARGUMENTS();
77+
break;
78+
}
79+
80+
if(new_method_provided) {
81+
Scene previous_scene = TotpSceneNone;
82+
if(plugin_state->current_scene == TotpSceneGenerateToken ||
83+
plugin_state->current_scene == TotpSceneAppSettings) {
84+
previous_scene = plugin_state->current_scene;
85+
totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
86+
}
87+
88+
plugin_state->notification_method = new_method;
89+
totp_config_file_update_notification_method(new_method);
90+
91+
if(previous_scene != TotpSceneNone) {
92+
totp_scene_director_activate_scene(plugin_state, previous_scene, NULL);
93+
}
94+
95+
TOTP_CLI_PRINTF("Notification method is set to ");
96+
totp_cli_command_notification_print_method(new_method);
97+
cli_nl();
98+
} else {
99+
TOTP_CLI_PRINTF("Current notification method is ");
100+
totp_cli_command_notification_print_method(plugin_state->notification_method);
101+
cli_nl();
102+
}
103+
} while(false);
104+
105+
furi_string_free(temp_str);
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include <cli/cli.h>
4+
#include "../../../types/plugin_state.h"
5+
6+
#define TOTP_CLI_COMMAND_NOTIFICATION "notify"
7+
8+
void totp_cli_command_notification_handle(PluginState* plugin_state, FuriString* args, Cli* cli);
9+
void totp_cli_command_notification_docopt_commands();
10+
void totp_cli_command_notification_docopt_usage();
11+
void totp_cli_command_notification_docopt_arguments();

totp/cli/commands/timezone/timezone.c

-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ void totp_cli_command_timezone_docopt_usage() {
2121
void totp_cli_command_timezone_docopt_arguments() {
2222
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_TIMEZONE_ARG_TIMEZONE
2323
" Timezone offset in hours to be set.\r\n");
24-
TOTP_CLI_PRINTF(
25-
" If not provided then current timezone offset will be printed\r\n");
2624
}
2725

2826
void totp_cli_command_timezone_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
147 Bytes
Loading

totp/lib/roll_value/roll_value.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
#include <stdint.h>
44

5-
typedef enum {
5+
typedef uint8_t TotpRollValueOverflowBehavior;
6+
7+
enum TotpRollValueOverflowBehaviors {
68
/**
79
* @brief Do not change value if it reached constraint
810
*/
@@ -12,7 +14,7 @@ typedef enum {
1214
* @brief Set value to opposite constraint value if it reached constraint
1315
*/
1416
RollOverflowBehaviorRoll
15-
} TotpRollValueOverflowBehavior;
17+
};
1618

1719
#define TOTP_ROLL_VALUE_FN_HEADER(type, step_type) \
1820
void totp_roll_value_##type( \

0 commit comments

Comments
 (0)