Skip to content

Commit 44df62c

Browse files
authored
* Updated firmware submodules (#76) (#78)
* Updated firmware submodules (#76) * Added more colors to cli * Fixed bug #77
1 parent ebb0a3a commit 44df62c

File tree

13 files changed

+105
-83
lines changed

13 files changed

+105
-83
lines changed

cli/cli.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "commands/reset/reset.h"
1515

1616
static void totp_cli_print_unknown_command(const FuriString* unknown_command) {
17-
TOTP_CLI_PRINTF(
17+
TOTP_CLI_PRINTF_ERROR(
1818
"Command \"%s\" is unknown. Use \"" TOTP_CLI_COMMAND_HELP
1919
"\" command to get list of available commands.",
2020
furi_string_get_cstr(unknown_command));

cli/cli_helpers.h

+27-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,31 @@
2222
_Pragma(STRINGIFY(GCC diagnostic pop)) \
2323
} while(false)
2424

25+
#define TOTP_CLI_PRINTF_COLORFUL(color, format, ...) \
26+
do { \
27+
_Pragma(STRINGIFY(GCC diagnostic push)) \
28+
_Pragma(STRINGIFY(GCC diagnostic ignored "-Wdouble-promotion")) \
29+
printf("\e[%s", color); \
30+
printf(format, ##__VA_ARGS__); \
31+
printf("\e[0m"); \
32+
fflush(stdout); \
33+
_Pragma(STRINGIFY(GCC diagnostic pop)) \
34+
} while(false)
35+
36+
#define TOTP_CLI_COLOR_ERROR "91m"
37+
#define TOTP_CLI_COLOR_WARNING "93m"
38+
#define TOTP_CLI_COLOR_SUCCESS "92m"
39+
#define TOTP_CLI_COLOR_INFO "96m"
40+
41+
#define TOTP_CLI_PRINTF_ERROR(format, ...) \
42+
TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_ERROR, format, ##__VA_ARGS__)
43+
#define TOTP_CLI_PRINTF_WARNING(format, ...) \
44+
TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_WARNING, format, ##__VA_ARGS__)
45+
#define TOTP_CLI_PRINTF_SUCCESS(format, ...) \
46+
TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_SUCCESS, format, ##__VA_ARGS__)
47+
#define TOTP_CLI_PRINTF_INFO(format, ...) \
48+
TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_INFO, format, ##__VA_ARGS__)
49+
2550
#define TOTP_CLI_DELETE_LAST_LINE() \
2651
TOTP_CLI_PRINTF("\033[A\33[2K\r"); \
2752
fflush(stdout)
@@ -35,11 +60,11 @@
3560
fflush(stdout)
3661

3762
#define TOTP_CLI_PRINT_INVALID_ARGUMENTS() \
38-
TOTP_CLI_PRINTF( \
63+
TOTP_CLI_PRINTF_ERROR( \
3964
"Invalid command arguments. use \"help\" command to get list of available commands")
4065

4166
#define TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE() \
42-
TOTP_CLI_PRINTF("An error has occurred during updating config file\r\n")
67+
TOTP_CLI_PRINTF_ERROR("An error has occurred during updating config file\r\n")
4368

4469
/**
4570
* @brief Checks whether user is authenticated and entered correct PIN.

cli/commands/add/add.c

+11-11
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
115115
bool parsed = false;
116116
if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX) == 0) {
117117
if(!args_read_string_and_trim(args, temp_str)) {
118-
TOTP_CLI_PRINTF("Missed value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX
119-
"\"\r\n");
118+
TOTP_CLI_PRINTF_ERROR(
119+
"Missed value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX "\"\r\n");
120120
} else if(!token_info_set_algo_from_str(token_info, temp_str)) {
121-
TOTP_CLI_PRINTF(
121+
TOTP_CLI_PRINTF_ERROR(
122122
"\"%s\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX
123123
"\"\r\n",
124124
furi_string_get_cstr(temp_str));
@@ -128,11 +128,11 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
128128
} else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX) == 0) {
129129
uint8_t digit_value;
130130
if(!args_read_uint8_and_trim(args, &digit_value)) {
131-
TOTP_CLI_PRINTF(
131+
TOTP_CLI_PRINTF_ERROR(
132132
"Missed or incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX
133133
"\"\r\n");
134134
} else if(!token_info_set_digits_from_int(token_info, digit_value)) {
135-
TOTP_CLI_PRINTF(
135+
TOTP_CLI_PRINTF_ERROR(
136136
"\"%" PRIu8
137137
"\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX
138138
"\"\r\n",
@@ -143,11 +143,11 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
143143
} else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_ADD_ARG_DURATION_PREFIX) == 0) {
144144
uint8_t duration_value;
145145
if(!args_read_uint8_and_trim(args, &duration_value)) {
146-
TOTP_CLI_PRINTF(
146+
TOTP_CLI_PRINTF_ERROR(
147147
"Missed or incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DURATION_PREFIX
148148
"\"\r\n");
149149
} else if(!token_info_set_duration_from_int(token_info, duration_value)) {
150-
TOTP_CLI_PRINTF(
150+
TOTP_CLI_PRINTF_ERROR(
151151
"\"%" PRIu8
152152
"\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DURATION_PREFIX
153153
"\"\r\n",
@@ -159,7 +159,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
159159
mask_user_input = false;
160160
parsed = true;
161161
} else {
162-
TOTP_CLI_PRINTF("Unknown argument \"%s\"\r\n", furi_string_get_cstr(temp_str));
162+
TOTP_CLI_PRINTF_ERROR("Unknown argument \"%s\"\r\n", furi_string_get_cstr(temp_str));
163163
}
164164

165165
if(!parsed) {
@@ -176,7 +176,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
176176
if(!totp_cli_read_line(cli, temp_str, mask_user_input) ||
177177
!totp_cli_ensure_authenticated(plugin_state, cli)) {
178178
TOTP_CLI_DELETE_LAST_LINE();
179-
TOTP_CLI_PRINTF("Cancelled by user\r\n");
179+
TOTP_CLI_PRINTF_INFO("Cancelled by user\r\n");
180180
furi_string_secure_free(temp_str);
181181
token_info_free(token_info);
182182
return;
@@ -189,7 +189,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
189189
furi_string_get_cstr(temp_str),
190190
furi_string_size(temp_str),
191191
plugin_state->iv)) {
192-
TOTP_CLI_PRINTF("Token secret seems to be invalid and can not be parsed\r\n");
192+
TOTP_CLI_PRINTF_ERROR("Token secret seems to be invalid and can not be parsed\r\n");
193193
furi_string_secure_free(temp_str);
194194
token_info_free(token_info);
195195
return;
@@ -206,7 +206,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
206206
TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, token_info, furi_check);
207207
plugin_state->tokens_count++;
208208
if(totp_config_file_save_new_token(token_info) == TotpConfigFileUpdateSuccess) {
209-
TOTP_CLI_PRINTF("Token \"%s\" has been successfully added\r\n", token_info->name);
209+
TOTP_CLI_PRINTF_SUCCESS("Token \"%s\" has been successfully added\r\n", token_info->name);
210210
} else {
211211
TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
212212
}

cli/commands/delete/delete.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ void totp_cli_command_delete_handle(PluginState* plugin_state, FuriString* args,
6464

6565
bool confirmed = !confirm_needed;
6666
if(confirm_needed) {
67-
TOTP_CLI_PRINTF("WARNING!\r\n");
68-
TOTP_CLI_PRINTF(
67+
TOTP_CLI_PRINTF_WARNING("WARNING!\r\n");
68+
TOTP_CLI_PRINTF_WARNING(
6969
"TOKEN \"%s\" WILL BE PERMANENTLY DELETED WITHOUT ABILITY TO RECOVER IT.\r\n",
7070
token_info->name);
71-
TOTP_CLI_PRINTF("Confirm? [y/n]\r\n");
71+
TOTP_CLI_PRINTF_WARNING("Confirm? [y/n]\r\n");
7272
fflush(stdout);
7373
char user_pick;
7474
do {
@@ -94,7 +94,8 @@ void totp_cli_command_delete_handle(PluginState* plugin_state, FuriString* args,
9494
plugin_state->tokens_count--;
9595

9696
if(totp_full_save_config_file(plugin_state) == TotpConfigFileUpdateSuccess) {
97-
TOTP_CLI_PRINTF("Token \"%s\" has been successfully deleted\r\n", token_info->name);
97+
TOTP_CLI_PRINTF_SUCCESS(
98+
"Token \"%s\" has been successfully deleted\r\n", token_info->name);
9899
} else {
99100
TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
100101
}
@@ -105,6 +106,6 @@ void totp_cli_command_delete_handle(PluginState* plugin_state, FuriString* args,
105106
totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
106107
}
107108
} else {
108-
TOTP_CLI_PRINTF("User not confirmed\r\n");
109+
TOTP_CLI_PRINTF_INFO("User has not confirmed\r\n");
109110
}
110111
}

cli/commands/move/move.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C
6565
bool parsed = false;
6666
if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_MOVE_ARG_NEW_NAME_PREFIX) == 0) {
6767
if(!args_read_string_and_trim(args, temp_str)) {
68-
TOTP_CLI_PRINTF(
68+
TOTP_CLI_PRINTF_ERROR(
6969
"Missed value for argument \"" TOTP_CLI_COMMAND_MOVE_ARG_NEW_NAME_PREFIX
7070
"\"\r\n");
7171
} else {
@@ -87,11 +87,11 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C
8787
}
8888
} else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX) == 0) {
8989
if(!args_read_int_and_trim(args, &new_token_index)) {
90-
TOTP_CLI_PRINTF(
90+
TOTP_CLI_PRINTF_ERROR(
9191
"Missed value for argument \"" TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX
9292
"\"\r\n");
9393
} else if(new_token_index < 1 || new_token_index > plugin_state->tokens_count) {
94-
TOTP_CLI_PRINTF(
94+
TOTP_CLI_PRINTF_ERROR(
9595
"\"%" PRId16
9696
"\" is incorrect value for argument \"" TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX
9797
"\"\r\n",
@@ -100,7 +100,7 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C
100100
parsed = true;
101101
}
102102
} else {
103-
TOTP_CLI_PRINTF("Unknown argument \"%s\"\r\n", furi_string_get_cstr(temp_str));
103+
TOTP_CLI_PRINTF_ERROR("Unknown argument \"%s\"\r\n", furi_string_get_cstr(temp_str));
104104
}
105105

106106
if(!parsed) {
@@ -148,7 +148,8 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C
148148

149149
if(token_updated) {
150150
if(totp_full_save_config_file(plugin_state) == TotpConfigFileUpdateSuccess) {
151-
TOTP_CLI_PRINTF("Token \"%s\" has been successfully updated\r\n", token_info->name);
151+
TOTP_CLI_PRINTF_SUCCESS(
152+
"Token \"%s\" has been successfully updated\r\n", token_info->name);
152153
} else {
153154
TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
154155
}

cli/commands/notification/notification.c

+10-9
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ void totp_cli_command_notification_docopt_arguments() {
2828
", " TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO "]\r\n");
2929
}
3030

31-
static void totp_cli_command_notification_print_method(NotificationMethod method) {
31+
static void totp_cli_command_notification_print_method(NotificationMethod method, char* color) {
3232
bool has_previous_method = false;
3333
if(method & NotificationMethodSound) {
34-
TOTP_CLI_PRINTF("\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_SOUND "\"");
34+
TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_SOUND "\"");
3535
has_previous_method = true;
3636
}
3737
if(method & NotificationMethodVibro) {
3838
if(has_previous_method) {
39-
TOTP_CLI_PRINTF(" and ");
39+
TOTP_CLI_PRINTF_COLORFUL(color, " and ");
4040
}
4141

42-
TOTP_CLI_PRINTF("\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO "\"");
42+
TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO "\"");
4343
}
4444
if(method == NotificationMethodNone) {
45-
TOTP_CLI_PRINTF("\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE "\"");
45+
TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE "\"");
4646
}
4747
}
4848

@@ -88,8 +88,8 @@ void totp_cli_command_notification_handle(PluginState* plugin_state, FuriString*
8888
plugin_state->notification_method = new_method;
8989
if(totp_config_file_update_notification_method(new_method) ==
9090
TotpConfigFileUpdateSuccess) {
91-
TOTP_CLI_PRINTF("Notification method is set to ");
92-
totp_cli_command_notification_print_method(new_method);
91+
TOTP_CLI_PRINTF_SUCCESS("Notification method is set to ");
92+
totp_cli_command_notification_print_method(new_method, TOTP_CLI_COLOR_SUCCESS);
9393
cli_nl();
9494
} else {
9595
TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
@@ -99,8 +99,9 @@ void totp_cli_command_notification_handle(PluginState* plugin_state, FuriString*
9999
totp_scene_director_activate_scene(plugin_state, previous_scene, NULL);
100100
}
101101
} else {
102-
TOTP_CLI_PRINTF("Current notification method is ");
103-
totp_cli_command_notification_print_method(plugin_state->notification_method);
102+
TOTP_CLI_PRINTF_INFO("Current notification method is ");
103+
totp_cli_command_notification_print_method(
104+
plugin_state->notification_method, TOTP_CLI_COLOR_INFO);
104105
cli_nl();
105106
}
106107
} while(false);

cli/commands/pin/pin.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static bool totp_cli_read_pin(Cli* cli, uint8_t* pin, uint8_t* pin_length) {
6565
}
6666
} else if(c == CliSymbolAsciiETX) {
6767
TOTP_CLI_DELETE_CURRENT_LINE();
68-
TOTP_CLI_PRINTF("Cancelled by user\r\n");
68+
TOTP_CLI_PRINTF_INFO("Cancelled by user\r\n");
6969
return false;
7070
} else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
7171
if(*pin_length > 0) {
@@ -162,9 +162,9 @@ void totp_cli_command_pin_handle(PluginState* plugin_state, FuriString* args, Cl
162162

163163
if(totp_full_save_config_file(plugin_state) == TotpConfigFileUpdateSuccess) {
164164
if(do_change) {
165-
TOTP_CLI_PRINTF("PIN has been successfully changed\r\n");
165+
TOTP_CLI_PRINTF_SUCCESS("PIN has been successfully changed\r\n");
166166
} else if(do_remove) {
167-
TOTP_CLI_PRINTF("PIN has been successfully removed\r\n");
167+
TOTP_CLI_PRINTF_SUCCESS("PIN has been successfully removed\r\n");
168168
}
169169
} else {
170170
TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();

cli/commands/reset/reset.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ void totp_cli_command_reset_docopt_usage() {
1717
}
1818

1919
void totp_cli_command_reset_handle(Cli* cli, FuriMessageQueue* event_queue) {
20-
TOTP_CLI_PRINTF(
20+
TOTP_CLI_PRINTF_WARNING(
2121
"As a result of reset all the settings and tokens will be permanently lost.\r\n");
22-
TOTP_CLI_PRINTF("Do you really want to reset application?\r\n");
23-
TOTP_CLI_PRINTF("Type \"" TOTP_CLI_RESET_CONFIRMATION_KEYWORD
24-
"\" and hit <ENTER> to confirm:\r\n");
22+
TOTP_CLI_PRINTF_WARNING("Do you really want to reset application?\r\n");
23+
TOTP_CLI_PRINTF_WARNING("Type \"" TOTP_CLI_RESET_CONFIRMATION_KEYWORD
24+
"\" and hit <ENTER> to confirm:\r\n");
2525
FuriString* temp_str = furi_string_alloc();
2626
bool is_confirmed = totp_cli_read_line(cli, temp_str, false) &&
2727
furi_string_cmpi_str(temp_str, TOTP_CLI_RESET_CONFIRMATION_KEYWORD) == 0;
2828
furi_string_free(temp_str);
2929
if(is_confirmed) {
3030
totp_config_file_reset();
31-
TOTP_CLI_PRINTF("Application has been successfully reset to default.\r\n");
32-
TOTP_CLI_PRINTF("Now application will be closed to apply all the changes.\r\n");
31+
TOTP_CLI_PRINTF_SUCCESS("Application has been successfully reset to default.\r\n");
32+
TOTP_CLI_PRINTF_SUCCESS("Now application will be closed to apply all the changes.\r\n");
3333
totp_cli_force_close_app(event_queue);
3434
} else {
35-
TOTP_CLI_PRINTF("Action was not confirmed by user\r\n");
35+
TOTP_CLI_PRINTF_INFO("Action was not confirmed by user\r\n");
3636
}
3737
}

cli/commands/timezone/timezone.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ void totp_cli_command_timezone_handle(PluginState* plugin_state, FuriString* arg
3030

3131
FuriString* temp_str = furi_string_alloc();
3232
if(args_read_string_and_trim(args, temp_str)) {
33-
float tz = strtof(furi_string_get_cstr(temp_str), NULL);
34-
if(tz >= -12.75f && tz <= 12.75f) {
33+
char* strtof_endptr;
34+
float tz = strtof(furi_string_get_cstr(temp_str), &strtof_endptr);
35+
if(*strtof_endptr == 0 && tz >= -12.75f && tz <= 12.75f) {
3536
plugin_state->timezone_offset = tz;
3637
if(totp_config_file_update_timezone_offset(tz) == TotpConfigFileUpdateSuccess) {
37-
TOTP_CLI_PRINTF("Timezone is set to %f\r\n", tz);
38+
TOTP_CLI_PRINTF_SUCCESS("Timezone is set to %f\r\n", tz);
3839
} else {
3940
TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
4041
}
@@ -46,10 +47,10 @@ void totp_cli_command_timezone_handle(PluginState* plugin_state, FuriString* arg
4647
totp_scene_director_activate_scene(plugin_state, TotpSceneAppSettings, NULL);
4748
}
4849
} else {
49-
TOTP_CLI_PRINTF("Invalid timezone offset\r\n");
50+
TOTP_CLI_PRINTF_ERROR("Invalid timezone offset\r\n");
5051
}
5152
} else {
52-
TOTP_CLI_PRINTF("Current timezone offset is %f\r\n", plugin_state->timezone_offset);
53+
TOTP_CLI_PRINTF_INFO("Current timezone offset is %f\r\n", plugin_state->timezone_offset);
5354
}
5455
furi_string_free(temp_str);
5556
}

0 commit comments

Comments
 (0)