cli_helpers.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "cli_helpers.h"
  2. #include <cli/cli.h>
  3. #include <lib/toolbox/args.h>
  4. #include "../types/plugin_event.h"
  5. const char* TOTP_CLI_COLOR_ERROR = "91m";
  6. const char* TOTP_CLI_COLOR_WARNING = "93m";
  7. const char* TOTP_CLI_COLOR_SUCCESS = "92m";
  8. const char* TOTP_CLI_COLOR_INFO = "96m";
  9. bool totp_cli_ensure_authenticated(const PluginState* plugin_state, Cli* cli) {
  10. if(plugin_state->current_scene == TotpSceneAuthentication) {
  11. TOTP_CLI_PRINTF("Pleases enter PIN on your flipper device\r\n");
  12. while((plugin_state->current_scene == TotpSceneAuthentication ||
  13. plugin_state->current_scene == TotpSceneNone) &&
  14. !cli_cmd_interrupt_received(cli)) {
  15. furi_delay_ms(100);
  16. }
  17. totp_cli_delete_last_line();
  18. if(plugin_state->current_scene == TotpSceneAuthentication || //-V560
  19. plugin_state->current_scene == TotpSceneNone) { //-V560
  20. TOTP_CLI_PRINTF_INFO("Cancelled by user\r\n");
  21. return false;
  22. }
  23. }
  24. return true;
  25. }
  26. void totp_cli_force_close_app(FuriMessageQueue* event_queue) {
  27. PluginEvent event = {.type = EventForceCloseApp};
  28. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  29. }
  30. bool totp_cli_read_line(Cli* cli, FuriString* out_str, bool mask_user_input) {
  31. uint8_t c;
  32. while(cli_read(cli, &c, 1) == 1) {
  33. if(c == CliSymbolAsciiEsc) {
  34. // Some keys generating escape-sequences
  35. // We need to ignore them as we care about alpha-numerics only
  36. uint8_t c2;
  37. cli_read_timeout(cli, &c2, 1, 0);
  38. cli_read_timeout(cli, &c2, 1, 0);
  39. } else if(c == CliSymbolAsciiETX) {
  40. cli_nl();
  41. return false;
  42. } else if(
  43. (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  44. c == '/' || c == '=' || c == '+') {
  45. if(mask_user_input) {
  46. putc('*', stdout);
  47. } else {
  48. putc(c, stdout);
  49. }
  50. fflush(stdout);
  51. furi_string_push_back(out_str, c);
  52. } else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
  53. size_t out_str_size = furi_string_size(out_str);
  54. if(out_str_size > 0) {
  55. totp_cli_delete_last_char();
  56. furi_string_left(out_str, out_str_size - 1);
  57. }
  58. } else if(c == CliSymbolAsciiCR) {
  59. cli_nl();
  60. break;
  61. }
  62. }
  63. return true;
  64. }
  65. bool args_read_uint8_and_trim(FuriString* args, uint8_t* value) {
  66. int int_value;
  67. if(!args_read_int_and_trim(args, &int_value) || int_value < 0 || int_value > UINT8_MAX) {
  68. return false;
  69. }
  70. *value = (uint8_t)int_value;
  71. return true;
  72. }
  73. void furi_string_secure_free(FuriString* str) {
  74. for(long i = furi_string_size(str) - 1; i >= 0; i--) {
  75. furi_string_set_char(str, i, '\0');
  76. }
  77. furi_string_free(str);
  78. }
  79. void totp_cli_print_invalid_arguments() {
  80. TOTP_CLI_PRINTF_ERROR(
  81. "Invalid command arguments. use \"help\" command to get list of available commands");
  82. }
  83. void totp_cli_print_error_updating_config_file() {
  84. TOTP_CLI_PRINTF_ERROR("An error has occurred during updating config file\r\n");
  85. }
  86. void totp_cli_print_error_loading_token_info() {
  87. TOTP_CLI_PRINTF_ERROR("An error has occurred during loading token information\r\n");
  88. }
  89. void totp_cli_print_processing() {
  90. TOTP_CLI_PRINTF("Processing, please wait...\r\n");
  91. }
  92. void totp_cli_delete_last_char() {
  93. TOTP_CLI_PRINTF("\b \b");
  94. fflush(stdout);
  95. }
  96. void totp_cli_delete_current_line() {
  97. TOTP_CLI_PRINTF("\33[2K\r");
  98. fflush(stdout);
  99. }
  100. void totp_cli_delete_last_line() {
  101. TOTP_CLI_PRINTF("\033[A\33[2K\r");
  102. fflush(stdout);
  103. }