example_plugins.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * An example of a plugin host application.
  3. * Loads a single plugin and calls its methods.
  4. */
  5. #include "plugin_interface.h"
  6. #include <furi.h>
  7. #include <flipper_application/flipper_application.h>
  8. #include <loader/firmware_api/firmware_api.h>
  9. #include <storage/storage.h>
  10. #define TAG "example_plugins"
  11. int32_t example_plugins_app(void* p) {
  12. UNUSED(p);
  13. FURI_LOG_I(TAG, "Starting");
  14. Storage* storage = furi_record_open(RECORD_STORAGE);
  15. FlipperApplication* app = flipper_application_alloc(storage, firmware_api_interface);
  16. do {
  17. FlipperApplicationPreloadStatus preload_res =
  18. flipper_application_preload(app, APP_DATA_PATH("plugins/example_plugin1.fal"));
  19. if(preload_res != FlipperApplicationPreloadStatusSuccess) {
  20. FURI_LOG_E(TAG, "Failed to preload plugin");
  21. break;
  22. }
  23. if(!flipper_application_is_plugin(app)) {
  24. FURI_LOG_E(TAG, "Plugin file is not a library");
  25. break;
  26. }
  27. FlipperApplicationLoadStatus load_status = flipper_application_map_to_memory(app);
  28. if(load_status != FlipperApplicationLoadStatusSuccess) {
  29. FURI_LOG_E(TAG, "Failed to load plugin file");
  30. break;
  31. }
  32. const FlipperAppPluginDescriptor* app_descriptor =
  33. flipper_application_plugin_get_descriptor(app);
  34. FURI_LOG_I(
  35. TAG,
  36. "Loaded plugin for appid '%s', API %lu",
  37. app_descriptor->appid,
  38. app_descriptor->ep_api_version);
  39. furi_check(app_descriptor->ep_api_version == PLUGIN_API_VERSION);
  40. furi_check(strcmp(app_descriptor->appid, PLUGIN_APP_ID) == 0);
  41. const ExamplePlugin* plugin = app_descriptor->entry_point;
  42. FURI_LOG_I(TAG, "Plugin name: %s", plugin->name);
  43. FURI_LOG_I(TAG, "Plugin method1: %d", plugin->method1());
  44. FURI_LOG_I(TAG, "Plugin method2(7,8): %d", plugin->method2(7, 8));
  45. FURI_LOG_I(TAG, "Plugin method2(1337,228): %d", plugin->method2(1337, 228));
  46. } while(false);
  47. flipper_application_free(app);
  48. furi_record_close(RECORD_STORAGE);
  49. FURI_LOG_I(TAG, "Goodbye!");
  50. return 0;
  51. }