cli_shared_methods.c 3.0 KB

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