cli_shared_methods.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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, PipeSide* pipe) {
  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_is_pipe_broken_or_is_etx_next_char(pipe)) {
  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(void* ctx) {
  24. FuriMessageQueue* event_queue = ctx;
  25. PluginEvent event = {.type = EventForceCloseApp};
  26. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  27. }
  28. bool totp_cli_read_line(PipeSide* pipe, FuriString* out_str, bool mask_user_input) {
  29. uint8_t c;
  30. while(pipe_receive(pipe, &c, 1) == 1) {
  31. if(c == CliKeyEsc) {
  32. // Some keys generating escape-sequences
  33. // We need to ignore them as we care about alpha-numerics only
  34. uint8_t c2;
  35. pipe_receive(pipe, &c2, 1);
  36. pipe_receive(pipe, &c2, 1);
  37. } else if(c == CliKeyETX) {
  38. TOTP_CLI_NL();
  39. return false;
  40. } else if(
  41. (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  42. c == '/' || c == '=' || c == '+') {
  43. if(mask_user_input) {
  44. putc('*', stdout);
  45. } else {
  46. putc(c, stdout);
  47. }
  48. fflush(stdout);
  49. furi_string_push_back(out_str, c);
  50. } else if(c == CliKeyBackspace || c == CliKeyDEL) {
  51. size_t out_str_size = furi_string_size(out_str);
  52. if(out_str_size > 0) {
  53. TOTP_CLI_DELETE_LAST_CHAR();
  54. furi_string_left(out_str, out_str_size - 1);
  55. }
  56. } else if(c == CliKeyCR) {
  57. TOTP_CLI_NL();
  58. break;
  59. }
  60. }
  61. return true;
  62. }
  63. bool args_read_uint8_and_trim(FuriString* args, uint8_t* value) {
  64. int int_value;
  65. if(!args_read_int_and_trim(args, &int_value) || int_value < 0 || int_value > UINT8_MAX) {
  66. return false;
  67. }
  68. *value = (uint8_t)int_value;
  69. return true;
  70. }
  71. void furi_string_secure_free(FuriString* str) {
  72. for(long i = furi_string_size(str) - 1; i >= 0; i--) {
  73. furi_string_set_char(str, i, '\0');
  74. }
  75. furi_string_free(str);
  76. }
  77. void totp_cli_printf_missed_argument_value(char* arg) {
  78. TOTP_CLI_PRINTF_ERROR("Missed or incorrect value for argument \"%s\"\r\n", arg);
  79. }
  80. void totp_cli_printf_unknown_argument(const FuriString* arg) {
  81. TOTP_CLI_PRINTF("Unknown argument \"%s\"\r\n", furi_string_get_cstr(arg));
  82. }