mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-06-21 20:42:15 -07:00
Helper to load plugins internally, save some DFU?
This commit is contained in:
@@ -1,14 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <furi_hal.h>
|
||||
#include <flipper_application/flipper_application.h>
|
||||
#include <flipper_application/plugins/plugin_manager.h>
|
||||
|
||||
#define RX_BUF_SIZE 1024
|
||||
|
||||
typedef struct SubGhzGPS SubGhzGPS;
|
||||
|
||||
struct SubGhzGPS {
|
||||
FlipperApplication* plugin_app;
|
||||
PluginManager* plugin_manager;
|
||||
|
||||
FuriThread* thread;
|
||||
FuriStreamBuffer* rx_stream;
|
||||
uint8_t rx_buf[RX_BUF_SIZE];
|
||||
|
||||
@@ -1,77 +1,39 @@
|
||||
#include "subghz_gps.h"
|
||||
|
||||
#include <expansion/expansion.h>
|
||||
#include <loader/firmware_api/firmware_api.h>
|
||||
#include <inttypes.h>
|
||||
#include <lib/toolbox/load_plugin.h>
|
||||
|
||||
#define TAG "SubGhzGPS"
|
||||
|
||||
SubGhzGPS* subghz_gps_plugin_init(uint32_t baudrate) {
|
||||
bool connected = expansion_is_connected(furi_record_open(RECORD_EXPANSION));
|
||||
furi_record_close(RECORD_EXPANSION);
|
||||
if(connected) return NULL;
|
||||
Expansion* expansion = furi_record_open(RECORD_EXPANSION);
|
||||
bool connected = expansion_is_connected(expansion);
|
||||
if(connected) {
|
||||
furi_record_close(RECORD_EXPANSION);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
expansion_disable(furi_record_open(RECORD_EXPANSION));
|
||||
furi_record_close(RECORD_EXPANSION);
|
||||
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
FlipperApplication* plugin_app = flipper_application_alloc(storage, firmware_api_interface);
|
||||
do {
|
||||
FlipperApplicationPreloadStatus preload_res = flipper_application_preload(
|
||||
plugin_app, EXT_PATH("apps_data/subghz_gps/plugins/subghz_gps.fal"));
|
||||
|
||||
if(preload_res != FlipperApplicationPreloadStatusSuccess) {
|
||||
FURI_LOG_E(TAG, "Failed to preload GPS plugin. Code: %d\r\n", preload_res);
|
||||
break;
|
||||
}
|
||||
|
||||
if(!flipper_application_is_plugin(plugin_app)) {
|
||||
FURI_LOG_E(TAG, "GPS plugin file is not a library\r\n");
|
||||
break;
|
||||
}
|
||||
|
||||
FlipperApplicationLoadStatus load_status = flipper_application_map_to_memory(plugin_app);
|
||||
if(load_status != FlipperApplicationLoadStatusSuccess) {
|
||||
FURI_LOG_E(TAG, "Failed to load GPS plugin file. Code %d\r\n", load_status);
|
||||
break;
|
||||
}
|
||||
|
||||
const FlipperAppPluginDescriptor* app_descriptor =
|
||||
flipper_application_plugin_get_descriptor(plugin_app);
|
||||
|
||||
if(strcmp(app_descriptor->appid, "subghz_gps") != 0) {
|
||||
FURI_LOG_E(TAG, "GPS plugin type doesn't match\r\n");
|
||||
break;
|
||||
}
|
||||
|
||||
if(app_descriptor->ep_api_version != 1) {
|
||||
FURI_LOG_E(
|
||||
TAG,
|
||||
"GPS plugin version %" PRIu32 " doesn't match\r\n",
|
||||
app_descriptor->ep_api_version);
|
||||
break;
|
||||
}
|
||||
|
||||
void (*subghz_gps_init)(SubGhzGPS* subghz_gps, uint32_t baudrate) =
|
||||
app_descriptor->entry_point;
|
||||
expansion_disable(expansion);
|
||||
|
||||
void (*subghz_gps_init)(SubGhzGPS* subghz_gps, uint32_t baudrate) = NULL;
|
||||
PluginManager* manager = NULL;
|
||||
if(load_plugin("subghz_gps", 1, "subghz_gps", &subghz_gps_init, &manager)) {
|
||||
SubGhzGPS* subghz_gps = malloc(sizeof(SubGhzGPS));
|
||||
subghz_gps->plugin_app = plugin_app;
|
||||
subghz_gps->plugin_manager = manager;
|
||||
subghz_gps_init(subghz_gps, baudrate);
|
||||
|
||||
furi_record_close(RECORD_EXPANSION);
|
||||
return subghz_gps;
|
||||
}
|
||||
|
||||
} while(false);
|
||||
flipper_application_free(plugin_app);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
|
||||
expansion_enable(furi_record_open(RECORD_EXPANSION));
|
||||
expansion_enable(expansion);
|
||||
furi_record_close(RECORD_EXPANSION);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void subghz_gps_plugin_deinit(SubGhzGPS* subghz_gps) {
|
||||
subghz_gps->deinit(subghz_gps);
|
||||
flipper_application_free(subghz_gps->plugin_app);
|
||||
plugin_manager_free(subghz_gps->plugin_manager);
|
||||
free(subghz_gps);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
|
||||
|
||||
@@ -5,9 +5,7 @@
|
||||
#include <furi_hal_version.h>
|
||||
#include <loader/loader.h>
|
||||
|
||||
#include <flipper_application/plugins/plugin_manager.h>
|
||||
#include <loader/firmware_api/firmware_api.h>
|
||||
#include <inttypes.h>
|
||||
#include <lib/toolbox/load_plugin.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define TAG "CliSrv"
|
||||
@@ -629,20 +627,12 @@ int32_t cli_srv(void* p) {
|
||||
}
|
||||
|
||||
void cli_plugin_wrapper(const char* name, Cli* cli, FuriString* args, void* context) {
|
||||
PluginManager* manager =
|
||||
plugin_manager_alloc(CLI_PLUGIN_APP_ID, CLI_PLUGIN_API_VERSION, firmware_api_interface);
|
||||
FuriString* path =
|
||||
furi_string_alloc_printf(EXT_PATH("apps_data/cli/plugins/%s_cli.fal"), name);
|
||||
PluginManagerError error = plugin_manager_load_single(manager, furi_string_get_cstr(path));
|
||||
if(error == PluginManagerErrorNone) {
|
||||
const CliCallback handler = plugin_manager_get_ep(manager, 0);
|
||||
CliCallback handler = NULL;
|
||||
PluginManager* manager = NULL;
|
||||
if(load_plugin(CLI_PLUGIN_APP_ID, CLI_PLUGIN_API_VERSION, name, &handler, &manager)) {
|
||||
handler(cli, args, context);
|
||||
plugin_manager_free(manager);
|
||||
} else {
|
||||
printf(
|
||||
"CLI plugin '%s' failed (code %" PRIu16 "), reinstall firmware or check logs\r\n",
|
||||
name,
|
||||
error);
|
||||
printf("CLI plugin '%s' load failed, reinstall firmware or check logs\r\n", name);
|
||||
}
|
||||
furi_string_free(path);
|
||||
plugin_manager_free(manager);
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ void cli_plugin_wrapper(const char* name, Cli* cli, FuriString* args, void* cont
|
||||
#define CLI_PLUGIN_API_VERSION 1
|
||||
#define CLI_PLUGIN_WRAPPER(plugin_name_without_cli_suffix, cli_command_callback) \
|
||||
void cli_command_callback##_wrapper(Cli* cli, FuriString* args, void* context) { \
|
||||
cli_plugin_wrapper(plugin_name_without_cli_suffix, cli, args, context); \
|
||||
cli_plugin_wrapper(plugin_name_without_cli_suffix "_cli", cli, args, context); \
|
||||
} \
|
||||
static const FlipperAppPluginDescriptor cli_command_callback##_plugin_descriptor = { \
|
||||
.appid = CLI_PLUGIN_APP_ID, \
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#include "load_plugin.h"
|
||||
|
||||
bool load_plugin(
|
||||
const char* appid,
|
||||
uint32_t version,
|
||||
const char* name,
|
||||
void* entry_point,
|
||||
PluginManager** plugin_manager) {
|
||||
*plugin_manager = plugin_manager_alloc(appid, version, NULL);
|
||||
char path[64];
|
||||
snprintf(path, sizeof(path), EXT_PATH("apps_data/%s/plugins/%s.fal"), appid, name);
|
||||
PluginManagerError error = plugin_manager_load_single(*plugin_manager, path);
|
||||
if(error == PluginManagerErrorNone) {
|
||||
*(const void**)entry_point = plugin_manager_get_ep(*plugin_manager, 0);
|
||||
return true;
|
||||
}
|
||||
plugin_manager_free(*plugin_manager);
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <flipper_application/plugins/plugin_manager.h>
|
||||
|
||||
bool load_plugin(
|
||||
const char* appid,
|
||||
uint32_t version,
|
||||
const char* name,
|
||||
void* entry_point,
|
||||
PluginManager** plugin_manager);
|
||||
Reference in New Issue
Block a user