Skip to content

Commit

Permalink
* Updated firmware submodules (#76) (#78)
Browse files Browse the repository at this point in the history
* Updated firmware submodules (#76)
* Added more colors to cli
* Fixed bug #77
  • Loading branch information
akopachov authored Feb 16, 2023
1 parent ebb0a3a commit 44df62c
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 83 deletions.
2 changes: 1 addition & 1 deletion cli/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "commands/reset/reset.h"

static void totp_cli_print_unknown_command(const FuriString* unknown_command) {
TOTP_CLI_PRINTF(
TOTP_CLI_PRINTF_ERROR(
"Command \"%s\" is unknown. Use \"" TOTP_CLI_COMMAND_HELP
"\" command to get list of available commands.",
furi_string_get_cstr(unknown_command));
Expand Down
29 changes: 27 additions & 2 deletions cli/cli_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,31 @@
_Pragma(STRINGIFY(GCC diagnostic pop)) \
} while(false)

#define TOTP_CLI_PRINTF_COLORFUL(color, format, ...) \
do { \
_Pragma(STRINGIFY(GCC diagnostic push)) \
_Pragma(STRINGIFY(GCC diagnostic ignored "-Wdouble-promotion")) \
printf("\e[%s", color); \
printf(format, ##__VA_ARGS__); \
printf("\e[0m"); \
fflush(stdout); \
_Pragma(STRINGIFY(GCC diagnostic pop)) \
} while(false)

#define TOTP_CLI_COLOR_ERROR "91m"
#define TOTP_CLI_COLOR_WARNING "93m"
#define TOTP_CLI_COLOR_SUCCESS "92m"
#define TOTP_CLI_COLOR_INFO "96m"

#define TOTP_CLI_PRINTF_ERROR(format, ...) \
TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_ERROR, format, ##__VA_ARGS__)
#define TOTP_CLI_PRINTF_WARNING(format, ...) \
TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_WARNING, format, ##__VA_ARGS__)
#define TOTP_CLI_PRINTF_SUCCESS(format, ...) \
TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_SUCCESS, format, ##__VA_ARGS__)
#define TOTP_CLI_PRINTF_INFO(format, ...) \
TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_INFO, format, ##__VA_ARGS__)

#define TOTP_CLI_DELETE_LAST_LINE() \
TOTP_CLI_PRINTF("\033[A\33[2K\r"); \
fflush(stdout)
Expand All @@ -35,11 +60,11 @@
fflush(stdout)

#define TOTP_CLI_PRINT_INVALID_ARGUMENTS() \
TOTP_CLI_PRINTF( \
TOTP_CLI_PRINTF_ERROR( \
"Invalid command arguments. use \"help\" command to get list of available commands")

#define TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE() \
TOTP_CLI_PRINTF("An error has occurred during updating config file\r\n")
TOTP_CLI_PRINTF_ERROR("An error has occurred during updating config file\r\n")

/**
* @brief Checks whether user is authenticated and entered correct PIN.
Expand Down
22 changes: 11 additions & 11 deletions cli/commands/add/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
bool parsed = false;
if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX) == 0) {
if(!args_read_string_and_trim(args, temp_str)) {
TOTP_CLI_PRINTF("Missed value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX
"\"\r\n");
TOTP_CLI_PRINTF_ERROR(
"Missed value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX "\"\r\n");
} else if(!token_info_set_algo_from_str(token_info, temp_str)) {
TOTP_CLI_PRINTF(
TOTP_CLI_PRINTF_ERROR(
"\"%s\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX
"\"\r\n",
furi_string_get_cstr(temp_str));
Expand All @@ -128,11 +128,11 @@ 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_DIGITS_PREFIX) == 0) {
uint8_t digit_value;
if(!args_read_uint8_and_trim(args, &digit_value)) {
TOTP_CLI_PRINTF(
TOTP_CLI_PRINTF_ERROR(
"Missed or incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX
"\"\r\n");
} else if(!token_info_set_digits_from_int(token_info, digit_value)) {
TOTP_CLI_PRINTF(
TOTP_CLI_PRINTF_ERROR(
"\"%" PRIu8
"\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX
"\"\r\n",
Expand All @@ -143,11 +143,11 @@ 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_DURATION_PREFIX) == 0) {
uint8_t duration_value;
if(!args_read_uint8_and_trim(args, &duration_value)) {
TOTP_CLI_PRINTF(
TOTP_CLI_PRINTF_ERROR(
"Missed or incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DURATION_PREFIX
"\"\r\n");
} else if(!token_info_set_duration_from_int(token_info, duration_value)) {
TOTP_CLI_PRINTF(
TOTP_CLI_PRINTF_ERROR(
"\"%" PRIu8
"\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DURATION_PREFIX
"\"\r\n",
Expand All @@ -159,7 +159,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
mask_user_input = false;
parsed = true;
} else {
TOTP_CLI_PRINTF("Unknown argument \"%s\"\r\n", furi_string_get_cstr(temp_str));
TOTP_CLI_PRINTF_ERROR("Unknown argument \"%s\"\r\n", furi_string_get_cstr(temp_str));
}

if(!parsed) {
Expand All @@ -176,7 +176,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
if(!totp_cli_read_line(cli, temp_str, mask_user_input) ||
!totp_cli_ensure_authenticated(plugin_state, cli)) {
TOTP_CLI_DELETE_LAST_LINE();
TOTP_CLI_PRINTF("Cancelled by user\r\n");
TOTP_CLI_PRINTF_INFO("Cancelled by user\r\n");
furi_string_secure_free(temp_str);
token_info_free(token_info);
return;
Expand All @@ -189,7 +189,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
furi_string_get_cstr(temp_str),
furi_string_size(temp_str),
plugin_state->iv)) {
TOTP_CLI_PRINTF("Token secret seems to be invalid and can not be parsed\r\n");
TOTP_CLI_PRINTF_ERROR("Token secret seems to be invalid and can not be parsed\r\n");
furi_string_secure_free(temp_str);
token_info_free(token_info);
return;
Expand All @@ -206,7 +206,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, token_info, furi_check);
plugin_state->tokens_count++;
if(totp_config_file_save_new_token(token_info) == TotpConfigFileUpdateSuccess) {
TOTP_CLI_PRINTF("Token \"%s\" has been successfully added\r\n", token_info->name);
TOTP_CLI_PRINTF_SUCCESS("Token \"%s\" has been successfully added\r\n", token_info->name);
} else {
TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
}
Expand Down
11 changes: 6 additions & 5 deletions cli/commands/delete/delete.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ void totp_cli_command_delete_handle(PluginState* plugin_state, FuriString* args,

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

if(totp_full_save_config_file(plugin_state) == TotpConfigFileUpdateSuccess) {
TOTP_CLI_PRINTF("Token \"%s\" has been successfully deleted\r\n", token_info->name);
TOTP_CLI_PRINTF_SUCCESS(
"Token \"%s\" has been successfully deleted\r\n", token_info->name);
} else {
TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
}
Expand All @@ -105,6 +106,6 @@ void totp_cli_command_delete_handle(PluginState* plugin_state, FuriString* args,
totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
}
} else {
TOTP_CLI_PRINTF("User not confirmed\r\n");
TOTP_CLI_PRINTF_INFO("User has not confirmed\r\n");
}
}
11 changes: 6 additions & 5 deletions cli/commands/move/move.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C
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(
TOTP_CLI_PRINTF_ERROR(
"Missed value for argument \"" TOTP_CLI_COMMAND_MOVE_ARG_NEW_NAME_PREFIX
"\"\r\n");
} else {
Expand All @@ -87,11 +87,11 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C
}
} 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(
TOTP_CLI_PRINTF_ERROR(
"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(
TOTP_CLI_PRINTF_ERROR(
"\"%" PRId16
"\" is incorrect value for argument \"" TOTP_CLI_COMMAND_MOVE_ARG_NEW_INDEX_PREFIX
"\"\r\n",
Expand All @@ -100,7 +100,7 @@ void totp_cli_command_move_handle(PluginState* plugin_state, FuriString* args, C
parsed = true;
}
} else {
TOTP_CLI_PRINTF("Unknown argument \"%s\"\r\n", furi_string_get_cstr(temp_str));
TOTP_CLI_PRINTF_ERROR("Unknown argument \"%s\"\r\n", furi_string_get_cstr(temp_str));
}

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

if(token_updated) {
if(totp_full_save_config_file(plugin_state) == TotpConfigFileUpdateSuccess) {
TOTP_CLI_PRINTF("Token \"%s\" has been successfully updated\r\n", token_info->name);
TOTP_CLI_PRINTF_SUCCESS(
"Token \"%s\" has been successfully updated\r\n", token_info->name);
} else {
TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
}
Expand Down
19 changes: 10 additions & 9 deletions cli/commands/notification/notification.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ void totp_cli_command_notification_docopt_arguments() {
", " TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO "]\r\n");
}

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

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

Expand Down Expand Up @@ -88,8 +88,8 @@ void totp_cli_command_notification_handle(PluginState* plugin_state, FuriString*
plugin_state->notification_method = new_method;
if(totp_config_file_update_notification_method(new_method) ==
TotpConfigFileUpdateSuccess) {
TOTP_CLI_PRINTF("Notification method is set to ");
totp_cli_command_notification_print_method(new_method);
TOTP_CLI_PRINTF_SUCCESS("Notification method is set to ");
totp_cli_command_notification_print_method(new_method, TOTP_CLI_COLOR_SUCCESS);
cli_nl();
} else {
TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
Expand All @@ -99,8 +99,9 @@ void totp_cli_command_notification_handle(PluginState* plugin_state, FuriString*
totp_scene_director_activate_scene(plugin_state, previous_scene, NULL);
}
} else {
TOTP_CLI_PRINTF("Current notification method is ");
totp_cli_command_notification_print_method(plugin_state->notification_method);
TOTP_CLI_PRINTF_INFO("Current notification method is ");
totp_cli_command_notification_print_method(
plugin_state->notification_method, TOTP_CLI_COLOR_INFO);
cli_nl();
}
} while(false);
Expand Down
6 changes: 3 additions & 3 deletions cli/commands/pin/pin.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static bool totp_cli_read_pin(Cli* cli, uint8_t* pin, uint8_t* pin_length) {
}
} else if(c == CliSymbolAsciiETX) {
TOTP_CLI_DELETE_CURRENT_LINE();
TOTP_CLI_PRINTF("Cancelled by user\r\n");
TOTP_CLI_PRINTF_INFO("Cancelled by user\r\n");
return false;
} else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
if(*pin_length > 0) {
Expand Down Expand Up @@ -162,9 +162,9 @@ void totp_cli_command_pin_handle(PluginState* plugin_state, FuriString* args, Cl

if(totp_full_save_config_file(plugin_state) == TotpConfigFileUpdateSuccess) {
if(do_change) {
TOTP_CLI_PRINTF("PIN has been successfully changed\r\n");
TOTP_CLI_PRINTF_SUCCESS("PIN has been successfully changed\r\n");
} else if(do_remove) {
TOTP_CLI_PRINTF("PIN has been successfully removed\r\n");
TOTP_CLI_PRINTF_SUCCESS("PIN has been successfully removed\r\n");
}
} else {
TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
Expand Down
14 changes: 7 additions & 7 deletions cli/commands/reset/reset.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ void totp_cli_command_reset_docopt_usage() {
}

void totp_cli_command_reset_handle(Cli* cli, FuriMessageQueue* event_queue) {
TOTP_CLI_PRINTF(
TOTP_CLI_PRINTF_WARNING(
"As a result of reset all the settings and tokens will be permanently lost.\r\n");
TOTP_CLI_PRINTF("Do you really want to reset application?\r\n");
TOTP_CLI_PRINTF("Type \"" TOTP_CLI_RESET_CONFIRMATION_KEYWORD
"\" and hit <ENTER> to confirm:\r\n");
TOTP_CLI_PRINTF_WARNING("Do you really want to reset application?\r\n");
TOTP_CLI_PRINTF_WARNING("Type \"" TOTP_CLI_RESET_CONFIRMATION_KEYWORD
"\" and hit <ENTER> to confirm:\r\n");
FuriString* temp_str = furi_string_alloc();
bool is_confirmed = totp_cli_read_line(cli, temp_str, false) &&
furi_string_cmpi_str(temp_str, TOTP_CLI_RESET_CONFIRMATION_KEYWORD) == 0;
furi_string_free(temp_str);
if(is_confirmed) {
totp_config_file_reset();
TOTP_CLI_PRINTF("Application has been successfully reset to default.\r\n");
TOTP_CLI_PRINTF("Now application will be closed to apply all the changes.\r\n");
TOTP_CLI_PRINTF_SUCCESS("Application has been successfully reset to default.\r\n");
TOTP_CLI_PRINTF_SUCCESS("Now application will be closed to apply all the changes.\r\n");
totp_cli_force_close_app(event_queue);
} else {
TOTP_CLI_PRINTF("Action was not confirmed by user\r\n");
TOTP_CLI_PRINTF_INFO("Action was not confirmed by user\r\n");
}
}
11 changes: 6 additions & 5 deletions cli/commands/timezone/timezone.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ void totp_cli_command_timezone_handle(PluginState* plugin_state, FuriString* arg

FuriString* temp_str = furi_string_alloc();
if(args_read_string_and_trim(args, temp_str)) {
float tz = strtof(furi_string_get_cstr(temp_str), NULL);
if(tz >= -12.75f && tz <= 12.75f) {
char* strtof_endptr;
float tz = strtof(furi_string_get_cstr(temp_str), &strtof_endptr);
if(*strtof_endptr == 0 && tz >= -12.75f && tz <= 12.75f) {
plugin_state->timezone_offset = tz;
if(totp_config_file_update_timezone_offset(tz) == TotpConfigFileUpdateSuccess) {
TOTP_CLI_PRINTF("Timezone is set to %f\r\n", tz);
TOTP_CLI_PRINTF_SUCCESS("Timezone is set to %f\r\n", tz);
} else {
TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
}
Expand All @@ -46,10 +47,10 @@ void totp_cli_command_timezone_handle(PluginState* plugin_state, FuriString* arg
totp_scene_director_activate_scene(plugin_state, TotpSceneAppSettings, NULL);
}
} else {
TOTP_CLI_PRINTF("Invalid timezone offset\r\n");
TOTP_CLI_PRINTF_ERROR("Invalid timezone offset\r\n");
}
} else {
TOTP_CLI_PRINTF("Current timezone offset is %f\r\n", plugin_state->timezone_offset);
TOTP_CLI_PRINTF_INFO("Current timezone offset is %f\r\n", plugin_state->timezone_offset);
}
furi_string_free(temp_str);
}
Loading

0 comments on commit 44df62c

Please sign in to comment.