cli_helpers.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #pragma once
  2. #include <cli/cli.h>
  3. #include "../types/plugin_state.h"
  4. #define TOTP_CLI_COMMAND_NAME "totp"
  5. #define DOCOPT_ARGUMENT(arg) "<" arg ">"
  6. #define DOCOPT_MULTIPLE(arg) arg "..."
  7. #define DOCOPT_OPTIONAL(param) "[" param "]"
  8. #define DOCOPT_REQUIRED(param) "(" param ")"
  9. #define DOCOPT_OPTION(option, value) option " " value
  10. #define DOCOPT_SWITCH(option) option
  11. #define DOCOPT_OPTIONS "[options]"
  12. #define DOCOPT_DEFAULT(val) "[default: " val "]"
  13. extern const char* TOTP_CLI_COLOR_ERROR;
  14. extern const char* TOTP_CLI_COLOR_WARNING;
  15. extern const char* TOTP_CLI_COLOR_SUCCESS;
  16. extern const char* TOTP_CLI_COLOR_INFO;
  17. #define TOTP_CLI_PRINTF(format, ...) printf(format, ##__VA_ARGS__)
  18. #define TOTP_CLI_PRINTF_COLORFUL(color, format, ...) \
  19. TOTP_CLI_PRINTF("\e[%s" format "\e[0m", color, ##__VA_ARGS__)
  20. #define TOTP_CLI_PRINTF_ERROR(format, ...) \
  21. TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_ERROR, format, ##__VA_ARGS__)
  22. #define TOTP_CLI_PRINTF_WARNING(format, ...) \
  23. TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_WARNING, format, ##__VA_ARGS__)
  24. #define TOTP_CLI_PRINTF_SUCCESS(format, ...) \
  25. TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_SUCCESS, format, ##__VA_ARGS__)
  26. #define TOTP_CLI_PRINTF_INFO(format, ...) \
  27. TOTP_CLI_PRINTF_COLORFUL(TOTP_CLI_COLOR_INFO, format, ##__VA_ARGS__)
  28. #define TOTP_CLI_LOCK_UI(plugin_state) \
  29. Scene __previous_scene = plugin_state->current_scene; \
  30. totp_scene_director_activate_scene(plugin_state, TotpSceneStandby); \
  31. totp_scene_director_force_redraw(plugin_state)
  32. #define TOTP_CLI_UNLOCK_UI(plugin_state) \
  33. totp_scene_director_activate_scene(plugin_state, __previous_scene); \
  34. totp_scene_director_force_redraw(plugin_state)
  35. /**
  36. * @brief Checks whether user is authenticated and entered correct PIN.
  37. * If user is not authenticated it prompts user to enter correct PIN to authenticate.
  38. * @param plugin_state application state
  39. * @param cli pointer to the firmware CLI subsystem
  40. * @return \c true if user is already authenticated or successfully authenticated; \c false otherwise
  41. */
  42. bool totp_cli_ensure_authenticated(const PluginState* plugin_state, Cli* cli);
  43. /**
  44. * @brief Forces application to be instantly closed
  45. * @param event_queue main app queue
  46. */
  47. void totp_cli_force_close_app(FuriMessageQueue* event_queue);
  48. /**
  49. * @brief Reads line of characters from console
  50. * @param cli pointer to the firmware CLI subsystem
  51. * @param out_str pointer to an output string to put read line to
  52. * @param mask_user_input whether to mask input characters in console or not
  53. * @return \c true if line successfully read and confirmed; \c false otherwise
  54. */
  55. bool totp_cli_read_line(Cli* cli, FuriString* out_str, bool mask_user_input);
  56. /**
  57. * @brief Extracts \c uint8_t value and trims arguments string
  58. * @param args arguments string
  59. * @param[out] value parsed value
  60. * @return \c true if value successfully read and parsed as \c uint8_t ; \c false otherwise
  61. */
  62. bool args_read_uint8_and_trim(FuriString* args, uint8_t* value);
  63. /**
  64. * @brief Free \c FuriString instance in a secure manner by clearing it first
  65. * @param str instance to free
  66. */
  67. void furi_string_secure_free(FuriString* str);
  68. /**
  69. * @brief Deletes last printed line in console
  70. */
  71. void totp_cli_delete_last_line();
  72. /**
  73. * @brief Deletes current printed line in console
  74. */
  75. void totp_cli_delete_current_line();
  76. /**
  77. * @brief Deletes last printed char in console
  78. */
  79. void totp_cli_delete_last_char();
  80. /**
  81. * @brief Prints error message about invalid command arguments
  82. */
  83. void totp_cli_print_invalid_arguments();
  84. /**
  85. * @brief Prints error message about config file update error
  86. */
  87. void totp_cli_print_error_updating_config_file();
  88. /**
  89. * @brief Prints error message about config file loading error
  90. */
  91. void totp_cli_print_error_loading_token_info();
  92. /**
  93. * @brief Prints message to let user knwo that command is processing now
  94. */
  95. void totp_cli_print_processing();