cli_shared_methods.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "cli_shared_methods.h"
  2. #include <cli/cli.h>
  3. #include <lib/toolbox/args.h>
  4. #include "cli_helpers.h"
  5. #include "../types/plugin_event.h"
  6. bool totp_cli_ensure_authenticated(const PluginState* plugin_state, Cli* cli) {
  7. if(plugin_state->current_scene == TotpSceneAuthentication) {
  8. TOTP_CLI_PRINTF("Pleases enter PIN on your flipper device\r\n");
  9. while((plugin_state->current_scene == TotpSceneAuthentication ||
  10. plugin_state->current_scene == TotpSceneNone) &&
  11. !cli_cmd_interrupt_received(cli)) {
  12. furi_delay_ms(100);
  13. }
  14. TOTP_CLI_DELETE_LAST_LINE();
  15. if(plugin_state->current_scene == TotpSceneAuthentication || //-V560
  16. plugin_state->current_scene == TotpSceneNone) { //-V560
  17. TOTP_CLI_PRINTF_INFO("Cancelled by user\r\n");
  18. return false;
  19. }
  20. }
  21. return true;
  22. }
  23. void totp_cli_force_close_app(FuriMessageQueue* event_queue) {
  24. PluginEvent event = {.type = EventForceCloseApp};
  25. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  26. }
  27. bool totp_cli_read_line(Cli* cli, FuriString* out_str, bool mask_user_input) {
  28. uint8_t c;
  29. while(cli_read(cli, &c, 1) == 1) {
  30. if(c == CliKeyEsc) {
  31. // Some keys generating escape-sequences
  32. // We need to ignore them as we care about alpha-numerics only
  33. uint8_t c2;
  34. cli_read_timeout(cli, &c2, 1, 0);
  35. cli_read_timeout(cli, &c2, 1, 0);
  36. } else if(c == CliKeyETX) {
  37. cli_nl(cli);
  38. return false;
  39. } else if(
  40. (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  41. c == '/' || c == '=' || c == '+') {
  42. if(mask_user_input) {
  43. putc('*', stdout);
  44. } else {
  45. putc(c, stdout);
  46. }
  47. fflush(stdout);
  48. furi_string_push_back(out_str, c);
  49. } else if(c == CliKeyBackspace || c == CliKeyDEL) {
  50. size_t out_str_size = furi_string_size(out_str);
  51. if(out_str_size > 0) {
  52. TOTP_CLI_DELETE_LAST_CHAR();
  53. furi_string_left(out_str, out_str_size - 1);
  54. }
  55. } else if(c == CliKeyCR) {
  56. cli_nl(cli);
  57. break;
  58. }
  59. }
  60. return true;
  61. }
  62. bool args_read_uint8_and_trim(FuriString* args, uint8_t* value) {
  63. int int_value;
  64. if(!args_read_int_and_trim(args, &int_value) || int_value < 0 || int_value > UINT8_MAX) {
  65. return false;
  66. }
  67. *value = (uint8_t)int_value;
  68. return true;
  69. }
  70. void furi_string_secure_free(FuriString* str) {
  71. for(long i = furi_string_size(str) - 1; i >= 0; i--) {
  72. furi_string_set_char(str, i, '\0');
  73. }
  74. furi_string_free(str);
  75. }
  76. void totp_cli_printf_missed_argument_value(char* arg) {
  77. TOTP_CLI_PRINTF_ERROR("Missed or incorrect value for argument \"%s\"\r\n", arg);
  78. }
  79. void totp_cli_printf_unknown_argument(const FuriString* arg) {
  80. TOTP_CLI_PRINTF("Unknown argument \"%s\"\r\n", furi_string_get_cstr(arg));
  81. }