cli_helpers.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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(
  38. (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  39. c == '/' || c == '=' || c == '+') {
  40. if(mask_user_input) {
  41. putc('*', stdout);
  42. } else {
  43. putc(c, stdout);
  44. }
  45. fflush(stdout);
  46. furi_string_push_back(out_str, c);
  47. } else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
  48. size_t out_str_size = furi_string_size(out_str);
  49. if(out_str_size > 0) {
  50. TOTP_CLI_DELETE_LAST_CHAR();
  51. furi_string_left(out_str, out_str_size - 1);
  52. }
  53. } else if(c == CliSymbolAsciiCR) {
  54. cli_nl();
  55. break;
  56. }
  57. }
  58. return true;
  59. }
  60. bool args_read_uint8_and_trim(FuriString* args, uint8_t* value) {
  61. int int_value;
  62. if(!args_read_int_and_trim(args, &int_value) || int_value < 0 || int_value > UINT8_MAX) {
  63. return false;
  64. }
  65. *value = (uint8_t)int_value;
  66. return true;
  67. }
  68. void furi_string_secure_free(FuriString* str) {
  69. for(long i = furi_string_size(str) - 1; i >= 0; i--) {
  70. furi_string_set_char(str, i, '\0');
  71. }
  72. furi_string_free(str);
  73. }