cli_shared_methods.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "cli_shared_methods.h"
  2. #include <toolbox/cli/cli_command.h>
  3. #include <cli/cli_main_commands.h>
  4. #include <cli/cli_ansi.h>
  5. #include <lib/toolbox/args.h>
  6. #include "cli_helpers.h"
  7. #include "../types/plugin_event.h"
  8. bool totp_cli_ensure_authenticated(const PluginState* plugin_state, PipeSide* pipe) {
  9. if(plugin_state->current_scene == TotpSceneAuthentication) {
  10. TOTP_CLI_PRINTF("Pleases enter PIN on your flipper device\r\n");
  11. while((plugin_state->current_scene == TotpSceneAuthentication ||
  12. plugin_state->current_scene == TotpSceneNone) &&
  13. !cli_is_pipe_broken_or_is_etx_next_char(pipe)) {
  14. furi_delay_ms(100);
  15. }
  16. TOTP_CLI_DELETE_LAST_LINE();
  17. if(plugin_state->current_scene == TotpSceneAuthentication || //-V560
  18. plugin_state->current_scene == TotpSceneNone) { //-V560
  19. TOTP_CLI_PRINTF_INFO("Cancelled by user\r\n");
  20. return false;
  21. }
  22. }
  23. return true;
  24. }
  25. void totp_cli_force_close_app(void* ctx) {
  26. FuriMessageQueue* event_queue = ctx;
  27. PluginEvent event = {.type = EventForceCloseApp};
  28. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  29. }
  30. bool totp_cli_read_line(PipeSide* pipe, FuriString* out_str, bool mask_user_input) {
  31. uint8_t c;
  32. while(pipe_receive(pipe, &c, 1) == 1) {
  33. if(c == CliKeyEsc) {
  34. // Some keys generating escape-sequences
  35. // We need to ignore them as we care about alpha-numerics only
  36. uint8_t c2;
  37. pipe_receive(pipe, &c2, 1);
  38. pipe_receive(pipe, &c2, 1);
  39. } else if(c == CliKeyETX) {
  40. printf("\r\n");
  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 == CliKeyBackspace || c == CliKeyDEL) {
  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 == CliKeyCR) {
  59. printf("\r\n");
  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_printf_missed_argument_value(char* arg) {
  80. TOTP_CLI_PRINTF_ERROR("Missed or incorrect value for argument \"%s\"\r\n", arg);
  81. }
  82. void totp_cli_printf_unknown_argument(const FuriString* arg) {
  83. TOTP_CLI_PRINTF("Unknown argument \"%s\"\r\n", furi_string_get_cstr(arg));
  84. }