cli_helpers.h 3.9 KB

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