cli.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Original idea: https://github.com/br0ziliy
  2. #include "cli.h"
  3. #include <lib/toolbox/args.h>
  4. #include "cli_helpers.h"
  5. #include "commands/list/list.h"
  6. #include "commands/add/add.h"
  7. #include "commands/delete/delete.h"
  8. #include "commands/timezone/timezone.h"
  9. #include "commands/help/help.h"
  10. #include "commands/move/move.h"
  11. #include "commands/pin/pin.h"
  12. #include "commands/notification/notification.h"
  13. #include "commands/reset/reset.h"
  14. #include "commands/automation/automation.h"
  15. static void totp_cli_print_unknown_command(const FuriString* unknown_command) {
  16. TOTP_CLI_PRINTF_ERROR(
  17. "Command \"%s\" is unknown. Use \"" TOTP_CLI_COMMAND_HELP
  18. "\" command to get list of available commands.",
  19. furi_string_get_cstr(unknown_command));
  20. }
  21. static void totp_cli_handler(Cli* cli, FuriString* args, void* context) {
  22. TotpCliContext* cli_context = context;
  23. PluginState* plugin_state = cli_context->plugin_state;
  24. FuriString* cmd = furi_string_alloc();
  25. args_read_string_and_trim(args, cmd);
  26. if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_HELP) == 0 ||
  27. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_HELP_ALT) == 0 ||
  28. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_HELP_ALT2) == 0 || furi_string_empty(cmd)) {
  29. totp_cli_command_help_handle();
  30. } else if(
  31. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_ADD) == 0 ||
  32. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_ADD_ALT) == 0 ||
  33. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_ADD_ALT2) == 0) {
  34. totp_cli_command_add_handle(plugin_state, args, cli);
  35. } else if(
  36. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_LIST) == 0 ||
  37. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_LIST_ALT) == 0) {
  38. totp_cli_command_list_handle(plugin_state, cli);
  39. } else if(
  40. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_DELETE) == 0 ||
  41. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_DELETE_ALT) == 0) {
  42. totp_cli_command_delete_handle(plugin_state, args, cli);
  43. } else if(
  44. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_TIMEZONE) == 0 ||
  45. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_TIMEZONE_ALT) == 0) {
  46. totp_cli_command_timezone_handle(plugin_state, args, cli);
  47. } else if(
  48. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_MOVE) == 0 ||
  49. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_MOVE_ALT) == 0) {
  50. totp_cli_command_move_handle(plugin_state, args, cli);
  51. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_PIN) == 0) {
  52. totp_cli_command_pin_handle(plugin_state, args, cli);
  53. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_NOTIFICATION) == 0) {
  54. totp_cli_command_notification_handle(plugin_state, args, cli);
  55. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_AUTOMATION) == 0) {
  56. totp_cli_command_automation_handle(plugin_state, args, cli);
  57. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_RESET) == 0) {
  58. totp_cli_command_reset_handle(cli, cli_context->event_queue);
  59. } else {
  60. totp_cli_print_unknown_command(cmd);
  61. }
  62. furi_string_free(cmd);
  63. }
  64. TotpCliContext*
  65. totp_cli_register_command_handler(PluginState* plugin_state, FuriMessageQueue* event_queue) {
  66. Cli* cli = furi_record_open(RECORD_CLI);
  67. TotpCliContext* context = malloc(sizeof(TotpCliContext));
  68. furi_check(context != NULL);
  69. context->plugin_state = plugin_state;
  70. context->event_queue = event_queue;
  71. cli_add_command(
  72. cli, TOTP_CLI_COMMAND_NAME, CliCommandFlagParallelSafe, totp_cli_handler, context);
  73. furi_record_close(RECORD_CLI);
  74. return context;
  75. }
  76. void totp_cli_unregister_command_handler(TotpCliContext* context) {
  77. Cli* cli = furi_record_open(RECORD_CLI);
  78. cli_delete_command(cli, TOTP_CLI_COMMAND_NAME);
  79. furi_record_close(RECORD_CLI);
  80. free(context);
  81. }