cli_helpers.c 2.6 KB

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