cli.c 7.2 KB

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