cli.c 7.0 KB

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