cli.c 7.4 KB

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