cli.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "cli.h"
  2. #include <lib/toolbox/args.h>
  3. #include <flipper_application/flipper_application.h>
  4. #include <flipper_application/plugins/composite_resolver.h>
  5. #include <loader/firmware_api/firmware_api.h>
  6. #include "cli_helpers.h"
  7. #include "plugins/timezone/meta.h"
  8. #include "plugins/list/meta.h"
  9. #include "plugins/modify/add/meta.h"
  10. #include "plugins/modify/update/meta.h"
  11. #include "plugins/delete/meta.h"
  12. #include "plugins/help/meta.h"
  13. #include "plugins/move/meta.h"
  14. #include "plugins/pin/meta.h"
  15. #include "plugins/notification/meta.h"
  16. #include "plugins/reset/meta.h"
  17. #include "plugins/automation/meta.h"
  18. #include "plugins/details/meta.h"
  19. #include "plugins/version/meta.h"
  20. #include "plugins/export/meta.h"
  21. #include "cli_plugin_interface.h"
  22. #include "../app_api_interface.h"
  23. #include <toolbox/cli/cli_command.h>
  24. #include <toolbox/cli/cli_ansi.h>
  25. #include <cli/cli_main_commands.h>
  26. struct TotpCliContext {
  27. PluginState* plugin_state;
  28. CompositeApiResolver* plugin_api_resolver;
  29. };
  30. static void totp_cli_print_unknown_command(const FuriString* unknown_command) {
  31. TOTP_CLI_PRINTF_ERROR(
  32. "Command \"%s\" is unknown. Use \"" TOTP_CLI_COMMAND_HELP
  33. "\" command to get list of available commands.",
  34. furi_string_get_cstr(unknown_command));
  35. }
  36. static void run_external_cli_plugin_handler(
  37. const char* handler_name,
  38. TotpCliContext* cli_context,
  39. FuriString* args,
  40. PipeSide* pipe) {
  41. Storage* storage = furi_record_open(RECORD_STORAGE);
  42. FlipperApplication* plugin_app = flipper_application_alloc(
  43. storage, composite_api_resolver_get(cli_context->plugin_api_resolver));
  44. do {
  45. FuriString* full_handler_path =
  46. furi_string_alloc_printf(EXT_PATH("apps_data/totp/plugins/%s.fal"), handler_name);
  47. FlipperApplicationPreloadStatus preload_res =
  48. flipper_application_preload(plugin_app, furi_string_get_cstr(full_handler_path));
  49. furi_string_free(full_handler_path);
  50. if(preload_res != FlipperApplicationPreloadStatusSuccess) {
  51. TOTP_CLI_PRINTF_ERROR("Failed to preload plugin. Code: %d\r\n", preload_res);
  52. break;
  53. }
  54. if(!flipper_application_is_plugin(plugin_app)) {
  55. TOTP_CLI_PRINTF_ERROR("Plugin file is not a library\r\n");
  56. break;
  57. }
  58. FlipperApplicationLoadStatus load_status = flipper_application_map_to_memory(plugin_app);
  59. if(load_status != FlipperApplicationLoadStatusSuccess) {
  60. TOTP_CLI_PRINTF_ERROR("Failed to load plugin file. Code %d\r\n", load_status);
  61. break;
  62. }
  63. const FlipperAppPluginDescriptor* app_descriptor =
  64. flipper_application_plugin_get_descriptor(plugin_app);
  65. if(strcmp(app_descriptor->appid, PLUGIN_APP_ID) != 0) {
  66. TOTP_CLI_PRINTF_ERROR("Plugin doesn't seems to be a valid TOTP CLI plugin\r\n");
  67. break;
  68. }
  69. if(app_descriptor->ep_api_version != PLUGIN_API_VERSION) {
  70. TOTP_CLI_PRINTF_ERROR(
  71. "Plugin version %" PRIu32 " is not compatible with your app version\r\n",
  72. app_descriptor->ep_api_version);
  73. break;
  74. }
  75. const CliPlugin* plugin = app_descriptor->entry_point;
  76. plugin->handle(cli_context->plugin_state, args, pipe);
  77. } while(false);
  78. flipper_application_free(plugin_app);
  79. furi_record_close(RECORD_STORAGE);
  80. }
  81. static void totp_cli_handler(PipeSide* pipe, FuriString* args, void* context) {
  82. TotpCliContext* cli_context = context;
  83. FuriString* cmd = furi_string_alloc();
  84. args_read_string_and_trim(args, cmd);
  85. const char* external_plugin_name = NULL;
  86. if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_HELP) == 0 ||
  87. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_HELP_ALT) == 0 ||
  88. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_HELP_ALT2) == 0 || furi_string_empty(cmd)) {
  89. external_plugin_name = TOTP_CLI_PLUGIN_HELP_FILE_NAME;
  90. } else if(
  91. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_ADD) == 0 ||
  92. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_ADD_ALT) == 0 ||
  93. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_ADD_ALT2) == 0) {
  94. external_plugin_name = TOTP_CLI_PLUGIN_ADD_FILE_NAME;
  95. } else if(
  96. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_LIST) == 0 ||
  97. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_LIST_ALT) == 0) {
  98. external_plugin_name = TOTP_CLI_PLUGIN_LIST_FILE_NAME;
  99. } else if(
  100. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_DELETE) == 0 ||
  101. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_DELETE_ALT) == 0) {
  102. external_plugin_name = TOTP_CLI_PLUGIN_DELETE_FILE_NAME;
  103. } else if(
  104. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_TIMEZONE) == 0 ||
  105. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_TIMEZONE_ALT) == 0) {
  106. external_plugin_name = TOTP_CLI_PLUGIN_TIMEZONE_FILE_NAME;
  107. } else if(
  108. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_MOVE) == 0 ||
  109. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_MOVE_ALT) == 0) {
  110. external_plugin_name = TOTP_CLI_PLUGIN_MOVE_FILE_NAME;
  111. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_PIN) == 0) {
  112. external_plugin_name = TOTP_CLI_PLUGIN_PIN_FILE_NAME;
  113. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_NOTIFICATION) == 0) {
  114. external_plugin_name = TOTP_CLI_PLUGIN_NOTIFICATION_FILE_NAME;
  115. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_AUTOMATION) == 0) {
  116. external_plugin_name = TOTP_CLI_PLUGIN_AUTOMATION_FILE_NAME;
  117. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_RESET) == 0) {
  118. external_plugin_name = TOTP_CLI_PLUGIN_RESET_FILE_NAME;
  119. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_UPDATE) == 0) {
  120. external_plugin_name = TOTP_CLI_PLUGIN_UPDATE_FILE_NAME;
  121. } else if(
  122. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_DETAILS) == 0 ||
  123. furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_DETAILS_ALT) == 0) {
  124. external_plugin_name = TOTP_CLI_PLUGIN_DETAILS_FILE_NAME;
  125. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_VERSION) == 0) {
  126. external_plugin_name = TOTP_CLI_PLUGIN_VERSION_FILE_NAME;
  127. } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_EXPORT) == 0) {
  128. external_plugin_name = TOTP_CLI_PLUGIN_EXPORT_FILE_NAME;
  129. } else {
  130. totp_cli_print_unknown_command(cmd);
  131. }
  132. if(external_plugin_name != NULL) {
  133. run_external_cli_plugin_handler(external_plugin_name, cli_context, args, pipe);
  134. }
  135. furi_string_free(cmd);
  136. }
  137. TotpCliContext* totp_cli_register_command_handler(PluginState* plugin_state) {
  138. CliRegistry* cli = furi_record_open(RECORD_CLI);
  139. TotpCliContext* context = malloc(sizeof(TotpCliContext));
  140. furi_check(context != NULL);
  141. context->plugin_state = plugin_state;
  142. context->plugin_api_resolver = composite_api_resolver_alloc();
  143. composite_api_resolver_add(context->plugin_api_resolver, firmware_api_interface);
  144. composite_api_resolver_add(context->plugin_api_resolver, application_api_interface);
  145. cli_registry_add_command(
  146. cli, TOTP_CLI_COMMAND_NAME, CliCommandFlagParallelSafe, totp_cli_handler, context);
  147. furi_record_close(RECORD_CLI);
  148. return context;
  149. }
  150. void totp_cli_unregister_command_handler(TotpCliContext* context) {
  151. CliRegistry* cli = furi_record_open(RECORD_CLI);
  152. cli_registry_delete_command(cli, TOTP_CLI_COMMAND_NAME);
  153. composite_api_resolver_free(context->plugin_api_resolver);
  154. furi_record_close(RECORD_CLI);
  155. free(context);
  156. }