cli_helpers.c 2.5 KB

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