[FL-3097] fbt, faploader: minimal app module implementation (#2420)
* fbt, faploader: minimal app module implementation * faploader, libs: moved API hashtable core to flipper_application * example: compound api * lib: flipper_application: naming fixes, doxygen comments * fbt: changed `requires` manifest field behavior for app extensions * examples: refactored plugin apps; faploader: changed new API naming; fbt: changed PLUGIN app type meaning * loader: dropped support for debug apps & plugin menus * moved applications/plugins -> applications/external * Restored x bit on chiplist_convert.py * git: fixed free-dap submodule path * pvs: updated submodule paths * examples: example_advanced_plugins.c: removed potential memory leak on errors * examples: example_plugins: refined requires * fbt: not deploying app modules for debug/sample apps; extra validation for .PLUGIN-type apps * apps: removed cdefines for external apps * fbt: moved ext app path definition * fbt: reworked fap_dist handling; f18: synced api_symbols.csv * fbt: removed resources_paths for extapps * scripts: reworked storage * scripts: reworked runfap.py & selfupdate.py to use new api * wip: fal runner * fbt: moved file packaging into separate module * scripts: storage: fixes * scripts: storage: minor fixes for new api * fbt: changed internal artifact storage details for external apps * scripts: storage: additional fixes and better error reporting; examples: using APP_DATA_PATH() * fbt, scripts: reworked launch_app to deploy plugins; moved old runfap.py to distfap.py * fbt: extra check for plugins descriptors * fbt: additional checks in emitter * fbt: better info message on SDK rebuild * scripts: removed requirements.txt * loader: removed remnants of plugins & debug menus * post-review fixes
This commit is contained in:
52
lib/flipper_application/plugins/composite_resolver.c
Normal file
52
lib/flipper_application/plugins/composite_resolver.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include "composite_resolver.h"
|
||||
|
||||
#include <m-list.h>
|
||||
#include <m-algo.h>
|
||||
|
||||
LIST_DEF(ElfApiInterfaceList, const ElfApiInterface*, M_POD_OPLIST)
|
||||
#define M_OPL_ElfApiInterfaceList_t() LIST_OPLIST(ElfApiInterfaceList, M_POD_OPLIST)
|
||||
|
||||
struct CompositeApiResolver {
|
||||
ElfApiInterface api_interface;
|
||||
ElfApiInterfaceList_t interfaces;
|
||||
};
|
||||
|
||||
static bool composite_api_resolver_callback(
|
||||
const ElfApiInterface* interface,
|
||||
const char* name,
|
||||
Elf32_Addr* address) {
|
||||
CompositeApiResolver* resolver = (CompositeApiResolver*)interface;
|
||||
for
|
||||
M_EACH(interface, resolver->interfaces, ElfApiInterfaceList_t) {
|
||||
if((*interface)->resolver_callback(*interface, name, address)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
CompositeApiResolver* composite_api_resolver_alloc() {
|
||||
CompositeApiResolver* resolver = malloc(sizeof(CompositeApiResolver));
|
||||
resolver->api_interface.api_version_major = 0;
|
||||
resolver->api_interface.api_version_minor = 0;
|
||||
resolver->api_interface.resolver_callback = &composite_api_resolver_callback;
|
||||
ElfApiInterfaceList_init(resolver->interfaces);
|
||||
return resolver;
|
||||
}
|
||||
|
||||
void composite_api_resolver_free(CompositeApiResolver* resolver) {
|
||||
ElfApiInterfaceList_clear(resolver->interfaces);
|
||||
free(resolver);
|
||||
}
|
||||
|
||||
void composite_api_resolver_add(CompositeApiResolver* resolver, const ElfApiInterface* interface) {
|
||||
if(ElfApiInterfaceList_empty_p(resolver->interfaces)) {
|
||||
resolver->api_interface.api_version_major = interface->api_version_major;
|
||||
resolver->api_interface.api_version_minor = interface->api_version_minor;
|
||||
}
|
||||
ElfApiInterfaceList_push_back(resolver->interfaces, interface);
|
||||
}
|
||||
|
||||
const ElfApiInterface* composite_api_resolver_get(CompositeApiResolver* resolver) {
|
||||
return &resolver->api_interface;
|
||||
}
|
46
lib/flipper_application/plugins/composite_resolver.h
Normal file
46
lib/flipper_application/plugins/composite_resolver.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <flipper_application/elf/elf_api_interface.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Composite API resolver
|
||||
* Resolves API interface by calling all resolvers in order
|
||||
* Uses API version from first resolver
|
||||
* Note: when using hashtable resolvers, collisions between tables are not detected
|
||||
* Can be cast to ElfApiInterface*
|
||||
*/
|
||||
typedef struct CompositeApiResolver CompositeApiResolver;
|
||||
|
||||
/**
|
||||
* @brief Allocate composite API resolver
|
||||
* @return CompositeApiResolver* instance
|
||||
*/
|
||||
CompositeApiResolver* composite_api_resolver_alloc();
|
||||
|
||||
/**
|
||||
* @brief Free composite API resolver
|
||||
* @param resolver Instance
|
||||
*/
|
||||
void composite_api_resolver_free(CompositeApiResolver* resolver);
|
||||
|
||||
/**
|
||||
* @brief Add API resolver to composite resolver
|
||||
* @param resolver Instance
|
||||
* @param interface API resolver
|
||||
*/
|
||||
void composite_api_resolver_add(CompositeApiResolver* resolver, const ElfApiInterface* interface);
|
||||
|
||||
/**
|
||||
* @brief Get API interface from composite resolver
|
||||
* @param resolver Instance
|
||||
* @return API interface
|
||||
*/
|
||||
const ElfApiInterface* composite_api_resolver_get(CompositeApiResolver* resolver);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
153
lib/flipper_application/plugins/plugin_manager.c
Normal file
153
lib/flipper_application/plugins/plugin_manager.c
Normal file
@@ -0,0 +1,153 @@
|
||||
#include "plugin_manager.h"
|
||||
|
||||
#include <loader/firmware_api/firmware_api.h>
|
||||
#include <storage/storage.h>
|
||||
#include <toolbox/path.h>
|
||||
|
||||
#include <m-array.h>
|
||||
#include <m-algo.h>
|
||||
|
||||
#include <furi.h>
|
||||
|
||||
#define TAG "libmgr"
|
||||
|
||||
ARRAY_DEF(FlipperApplicationList, FlipperApplication*, M_PTR_OPLIST)
|
||||
#define M_OPL_FlipperApplicationList_t() ARRAY_OPLIST(FlipperApplicationList, M_PTR_OPLIST)
|
||||
|
||||
struct PluginManager {
|
||||
const char* application_id;
|
||||
uint32_t api_version;
|
||||
Storage* storage;
|
||||
FlipperApplicationList_t libs;
|
||||
const ElfApiInterface* api_interface;
|
||||
};
|
||||
|
||||
PluginManager* plugin_manager_alloc(
|
||||
const char* application_id,
|
||||
uint32_t api_version,
|
||||
const ElfApiInterface* api_interface) {
|
||||
PluginManager* manager = malloc(sizeof(PluginManager));
|
||||
manager->application_id = application_id;
|
||||
manager->api_version = api_version;
|
||||
manager->api_interface = api_interface ? api_interface : firmware_api_interface;
|
||||
manager->storage = furi_record_open(RECORD_STORAGE);
|
||||
FlipperApplicationList_init(manager->libs);
|
||||
return manager;
|
||||
}
|
||||
|
||||
void plugin_manager_free(PluginManager* manager) {
|
||||
for
|
||||
M_EACH(loaded_lib, manager->libs, FlipperApplicationList_t) {
|
||||
flipper_application_free(*loaded_lib);
|
||||
}
|
||||
FlipperApplicationList_clear(manager->libs);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
free(manager);
|
||||
}
|
||||
|
||||
PluginManagerError plugin_manager_load_single(PluginManager* manager, const char* path) {
|
||||
FlipperApplication* lib = flipper_application_alloc(manager->storage, manager->api_interface);
|
||||
|
||||
PluginManagerError error = PluginManagerErrorNone;
|
||||
do {
|
||||
FlipperApplicationPreloadStatus preload_res = flipper_application_preload(lib, path);
|
||||
|
||||
if(preload_res != FlipperApplicationPreloadStatusSuccess) {
|
||||
FURI_LOG_E(TAG, "Failed to preload %s", path);
|
||||
error = PluginManagerErrorLoaderError;
|
||||
break;
|
||||
}
|
||||
|
||||
if(!flipper_application_is_plugin(lib)) {
|
||||
FURI_LOG_E(TAG, "Not a plugin %s", path);
|
||||
error = PluginManagerErrorLoaderError;
|
||||
break;
|
||||
}
|
||||
|
||||
FlipperApplicationLoadStatus load_status = flipper_application_map_to_memory(lib);
|
||||
if(load_status != FlipperApplicationLoadStatusSuccess) {
|
||||
FURI_LOG_E(TAG, "Failed to load module_demo_plugin1.fal");
|
||||
break;
|
||||
}
|
||||
|
||||
const FlipperAppPluginDescriptor* app_descriptor =
|
||||
flipper_application_plugin_get_descriptor(lib);
|
||||
|
||||
if(!app_descriptor) {
|
||||
FURI_LOG_E(TAG, "Failed to get descriptor %s", path);
|
||||
error = PluginManagerErrorLoaderError;
|
||||
break;
|
||||
}
|
||||
|
||||
if(strcmp(app_descriptor->appid, manager->application_id) != 0) {
|
||||
FURI_LOG_E(TAG, "Application id mismatch %s", path);
|
||||
error = PluginManagerErrorApplicationIdMismatch;
|
||||
break;
|
||||
}
|
||||
|
||||
if(app_descriptor->ep_api_version != manager->api_version) {
|
||||
FURI_LOG_E(TAG, "API version mismatch %s", path);
|
||||
error = PluginManagerErrorAPIVersionMismatch;
|
||||
break;
|
||||
}
|
||||
|
||||
FlipperApplicationList_push_back(manager->libs, lib);
|
||||
} while(false);
|
||||
|
||||
if(error != PluginManagerErrorNone) {
|
||||
flipper_application_free(lib);
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
PluginManagerError plugin_manager_load_all(PluginManager* manager, const char* path) {
|
||||
File* directory = storage_file_alloc(manager->storage);
|
||||
char file_name_buffer[256];
|
||||
FuriString* file_name = furi_string_alloc();
|
||||
do {
|
||||
if(!storage_dir_open(directory, path)) {
|
||||
FURI_LOG_E(TAG, "Failed to open directory %s", path);
|
||||
break;
|
||||
}
|
||||
while(true) {
|
||||
if(!storage_dir_read(directory, NULL, file_name_buffer, sizeof(file_name_buffer))) {
|
||||
break;
|
||||
}
|
||||
|
||||
furi_string_set(file_name, file_name_buffer);
|
||||
if(!furi_string_end_with_str(file_name, ".fal")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
path_concat(path, file_name_buffer, file_name);
|
||||
FURI_LOG_D(TAG, "Loading %s", furi_string_get_cstr(file_name));
|
||||
PluginManagerError error =
|
||||
plugin_manager_load_single(manager, furi_string_get_cstr(file_name));
|
||||
|
||||
if(error != PluginManagerErrorNone) {
|
||||
FURI_LOG_E(TAG, "Failed to load %s", furi_string_get_cstr(file_name));
|
||||
break;
|
||||
}
|
||||
}
|
||||
} while(false);
|
||||
storage_dir_close(directory);
|
||||
storage_file_free(directory);
|
||||
furi_string_free(file_name);
|
||||
return PluginManagerErrorNone;
|
||||
}
|
||||
|
||||
uint32_t plugin_manager_get_count(PluginManager* manager) {
|
||||
return FlipperApplicationList_size(manager->libs);
|
||||
}
|
||||
|
||||
const FlipperAppPluginDescriptor* plugin_manager_get(PluginManager* manager, uint32_t index) {
|
||||
FlipperApplication* app = *FlipperApplicationList_get(manager->libs, index);
|
||||
return flipper_application_plugin_get_descriptor(app);
|
||||
}
|
||||
|
||||
const void* plugin_manager_get_ep(PluginManager* manager, uint32_t index) {
|
||||
const FlipperAppPluginDescriptor* lib_descr = plugin_manager_get(manager, index);
|
||||
furi_check(lib_descr);
|
||||
return lib_descr->entry_point;
|
||||
}
|
82
lib/flipper_application/plugins/plugin_manager.h
Normal file
82
lib/flipper_application/plugins/plugin_manager.h
Normal file
@@ -0,0 +1,82 @@
|
||||
#pragma once
|
||||
|
||||
#include <flipper_application/flipper_application.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Object that manages plugins for an application
|
||||
* Implements mass loading of plugins and provides access to their descriptors
|
||||
*/
|
||||
typedef struct PluginManager PluginManager;
|
||||
|
||||
typedef enum {
|
||||
PluginManagerErrorNone = 0,
|
||||
PluginManagerErrorLoaderError,
|
||||
PluginManagerErrorApplicationIdMismatch,
|
||||
PluginManagerErrorAPIVersionMismatch,
|
||||
} PluginManagerError;
|
||||
|
||||
/**
|
||||
* @brief Allocates new PluginManager
|
||||
* @param application_id Application ID filter - only plugins with matching ID will be loaded
|
||||
* @param api_version Application API version filter - only plugins with matching API version
|
||||
* @param api_interface Application API interface - used to resolve plugins' API imports
|
||||
* If plugin uses private application's API, use CompoundApiInterface
|
||||
* @return new PluginManager instance
|
||||
*/
|
||||
PluginManager* plugin_manager_alloc(
|
||||
const char* application_id,
|
||||
uint32_t api_version,
|
||||
const ElfApiInterface* api_interface);
|
||||
|
||||
/**
|
||||
* @brief Frees PluginManager
|
||||
* @param manager PluginManager instance
|
||||
*/
|
||||
void plugin_manager_free(PluginManager* manager);
|
||||
|
||||
/**
|
||||
* @brief Loads single plugin by full path
|
||||
* @param manager PluginManager instance
|
||||
* @param path Path to plugin
|
||||
* @return Error code
|
||||
*/
|
||||
PluginManagerError plugin_manager_load_single(PluginManager* manager, const char* path);
|
||||
|
||||
/**
|
||||
* @brief Loads all plugins from specified directory
|
||||
* @param manager PluginManager instance
|
||||
* @param path Path to directory
|
||||
* @return Error code
|
||||
*/
|
||||
PluginManagerError plugin_manager_load_all(PluginManager* manager, const char* path);
|
||||
|
||||
/**
|
||||
* @brief Returns number of loaded plugins
|
||||
* @param manager PluginManager instance
|
||||
* @return Number of loaded plugins
|
||||
*/
|
||||
uint32_t plugin_manager_get_count(PluginManager* manager);
|
||||
|
||||
/**
|
||||
* @brief Returns plugin descriptor by index
|
||||
* @param manager PluginManager instance
|
||||
* @param index Plugin index
|
||||
* @return Plugin descriptor
|
||||
*/
|
||||
const FlipperAppPluginDescriptor* plugin_manager_get(PluginManager* manager, uint32_t index);
|
||||
|
||||
/**
|
||||
* @brief Returns plugin entry point by index
|
||||
* @param manager PluginManager instance
|
||||
* @param index Plugin index
|
||||
* @return Plugin entry point
|
||||
*/
|
||||
const void* plugin_manager_get_ep(PluginManager* manager, uint32_t index);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user