cli_helpers.c 2.0 KB

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