created ServiceManifest (#5)

based on AppManifest
This commit is contained in:
Ken Van Hoeylandt 2024-01-05 19:38:39 +01:00 committed by GitHub
parent 3b9986fcef
commit e842e30ab3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
39 changed files with 134 additions and 352 deletions

View File

@ -10,20 +10,11 @@ extern "C" {
typedef struct _lv_obj_t lv_obj_t; typedef struct _lv_obj_t lv_obj_t;
typedef enum { typedef enum {
AppTypeService,
AppTypeSystem, AppTypeSystem,
AppTypeDesktop, AppTypeSettings,
AppTypeUser AppTypeUser
} AppType; } AppType;
typedef enum {
AppStackSizeTiny = 512,
AppStackSizeSmall = 1024,
AppStackSizeNormal = 2048,
AppStackSizeLarge = 4096,
AppStackSizeHuge = 8192,
} AppStackSize;
typedef void (*AppOnStart)(void _Nonnull* parameter); typedef void (*AppOnStart)(void _Nonnull* parameter);
typedef void (*AppOnStop)(); typedef void (*AppOnStop)();
typedef void (*AppOnShow)(lv_obj_t* parent, void* context); typedef void (*AppOnShow)(lv_obj_t* parent, void* context);

View File

@ -6,10 +6,6 @@
#define TAG "app_registry" #define TAG "app_registry"
typedef struct {
const AppManifest* manifest;
} AppEntry;
DICT_DEF2(AppManifestDict, const char*, M_CSTR_DUP_OPLIST, const AppManifest*, M_PTR_OPLIST) DICT_DEF2(AppManifestDict, const char*, M_CSTR_DUP_OPLIST, const AppManifest*, M_PTR_OPLIST)
#define APP_REGISTRY_FOR_EACH(manifest_var_name, code_to_execute) \ #define APP_REGISTRY_FOR_EACH(manifest_var_name, code_to_execute) \

View File

@ -37,7 +37,7 @@ FURI_NORETURN void __furi_halt_implementation();
/** Crash system with message. */ /** Crash system with message. */
#define __furi_crash(message) \ #define __furi_crash(message) \
do { \ do { \
ESP_LOGE("crash", "%s\n\tat %s:%d", ((message) ? ((const char*)message) : ""), __FILE__, __LINE__); \ ESP_LOGE("crash", "%s\n\tat %s:%d", ((message) ? (message) : ""), __FILE__, __LINE__); \
__furi_crash_implementation(); \ __furi_crash_implementation(); \
} while (0) } while (0)
@ -50,7 +50,7 @@ FURI_NORETURN void __furi_halt_implementation();
/** Halt system with message. */ /** Halt system with message. */
#define __furi_halt(message) \ #define __furi_halt(message) \
do { \ do { \
ESP_LOGE("halt", "%s\n\tat %s:%d", ((message) ? ((const char*)message) : ""), __FILE__, __LINE__); \ ESP_LOGE("halt", "%s\n\tat %s:%d", ((message) ? (message) : ""), __FILE__, __LINE__); \
__furi_halt_implementation(); \ __furi_halt_implementation(); \
} while (0) } while (0)

View File

@ -1,19 +1,18 @@
#include "furi.h" #include "furi.h"
#include "app_manifest_registry.h" #include "app_manifest_registry.h"
#include <string.h>
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/queue.h" #include "freertos/queue.h"
#define TAG "furi"
void furi_init() { void furi_init() {
FURI_LOG_I(TAG, "init start");
furi_assert(!furi_kernel_is_irq()); furi_assert(!furi_kernel_is_irq());
if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) { if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) {
vTaskSuspendAll(); vTaskSuspendAll();
} }
furi_record_init();
xTaskResumeAll(); xTaskResumeAll();
#if defined(__ARM_ARCH_7A__) && (__ARM_ARCH_7A__ == 0U) #if defined(__ARM_ARCH_7A__) && (__ARM_ARCH_7A__ == 0U)
@ -24,4 +23,5 @@ void furi_init() {
#endif #endif
app_manifest_registry_init(); app_manifest_registry_init();
FURI_LOG_I(TAG, "init complete");
} }

View File

@ -10,7 +10,6 @@
#include "message_queue.h" #include "message_queue.h"
#include "mutex.h" #include "mutex.h"
#include "pubsub.h" #include "pubsub.h"
#include "record.h"
#include "semaphore.h" #include "semaphore.h"
#include "stream_buffer.h" #include "stream_buffer.h"
#include "string.h" #include "string.h"

View File

@ -1,151 +0,0 @@
#include "record.h"
#include "check.h"
#include "event_flag.h"
#include "mutex.h"
#include "m-dict.h"
#include "m_cstr_dup.h"
#include "log.h"
#define TAG "record"
#define FURI_RECORD_FLAG_READY (0x1)
typedef struct {
FuriEventFlag* flags;
void* data;
size_t holders_count;
} FuriRecordData;
DICT_DEF2(FuriRecordDataDict, const char*, M_CSTR_DUP_OPLIST, FuriRecordData, M_POD_OPLIST)
typedef struct {
FuriMutex* mutex;
FuriRecordDataDict_t records;
} FuriRecord;
static FuriRecord* furi_record = NULL;
static FuriRecordData* furi_record_get(const char* name) {
return FuriRecordDataDict_get(furi_record->records, name);
}
static void furi_record_put(const char* name, FuriRecordData* record_data) {
FuriRecordDataDict_set_at(furi_record->records, name, *record_data);
}
static void furi_record_erase(const char* name, FuriRecordData* record_data) {
furi_event_flag_free(record_data->flags);
FuriRecordDataDict_erase(furi_record->records, name);
}
void furi_record_init() {
furi_record = malloc(sizeof(FuriRecord));
furi_record->mutex = furi_mutex_alloc(FuriMutexTypeRecursive);
furi_check(furi_record->mutex);
FuriRecordDataDict_init(furi_record->records);
}
static FuriRecordData* furi_record_data_get_or_create(const char* name) {
furi_assert(furi_record);
FuriRecordData* record_data = furi_record_get(name);
if (!record_data) {
FuriRecordData new_record;
new_record.flags = furi_event_flag_alloc();
new_record.data = NULL;
new_record.holders_count = 0;
furi_record_put(name, &new_record);
record_data = furi_record_get(name);
}
return record_data;
}
static void furi_record_lock() {
furi_check(furi_mutex_acquire(furi_record->mutex, FuriWaitForever) == FuriStatusOk);
}
static void furi_record_unlock() {
furi_check(furi_mutex_release(furi_record->mutex) == FuriStatusOk);
}
bool furi_record_exists(const char* name) {
furi_assert(furi_record);
furi_assert(name);
bool ret = false;
furi_record_lock();
ret = (furi_record_get(name) != NULL);
furi_record_unlock();
return ret;
}
void furi_record_create(const char* name, void* data) {
furi_assert(furi_record);
furi_record_lock();
// Get record data and fill it
FuriRecordData* record_data = furi_record_data_get_or_create(name);
furi_assert(record_data->data == NULL);
record_data->data = data;
furi_event_flag_set(record_data->flags, FURI_RECORD_FLAG_READY);
furi_record_unlock();
}
bool furi_record_destroy(const char* name) {
furi_assert(furi_record);
bool ret = false;
furi_record_lock();
FuriRecordData* record_data = furi_record_get(name);
furi_assert(record_data);
if (record_data->holders_count == 0) {
furi_record_erase(name, record_data);
ret = true;
}
furi_record_unlock();
return ret;
}
void* furi_record_open(const char* name) {
furi_assert(name);
furi_assert(furi_record);
furi_record_lock();
FuriRecordData* record_data = furi_record_data_get_or_create(name);
record_data->holders_count++;
furi_record_unlock();
// Wait for record to become ready
furi_check(
furi_event_flag_wait(
record_data->flags,
FURI_RECORD_FLAG_READY,
FuriFlagWaitAny | FuriFlagNoClear,
FuriWaitForever
) == FURI_RECORD_FLAG_READY
);
return record_data->data;
}
void furi_record_close(const char* name) {
furi_assert(name);
furi_assert(furi_record);
furi_record_lock();
FuriRecordData* record_data = furi_record_get(name);
furi_assert(record_data);
record_data->holders_count--;
furi_record_unlock();
}

View File

@ -1,80 +0,0 @@
/**
* @file record.h
* Furi: record API
*/
#pragma once
#include "furi_extra_defines.h"
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* Opens a record, calls the code and then closes the record.
* @param record_name const char* that contains the name of the record
* @param variable_name the name of the variable that is used in the `code`
* @param code the code to execute: consider putting it between {}
*/
#define FURI_RECORD_TRANSACTION(record_name, variable_type, variable_name, code) \
{ \
variable_type (variable_name) = (variable_type)furi_record_open(record_name); \
code; \
furi_record_close(record_name); \
}
/** Initialize record storage For internal use only.
*/
void furi_record_init();
/** Check if record exists
*
* @param name record name
* @note Thread safe. Create and destroy must be executed from the same
* thread.
*/
bool furi_record_exists(const char* name);
/** Create record
*
* @param name record name
* @param data data pointer
* @note Thread safe. Create and destroy must be executed from the same
* thread.
*/
void furi_record_create(const char* name, void* data);
/** Destroy record
*
* @param name record name
*
* @return true if successful, false if still have holders or thread is not
* owner.
* @note Thread safe. Create and destroy must be executed from the same
* thread.
*/
bool furi_record_destroy(const char* name);
/** Open record
*
* @param name record name
*
* @return pointer to the record
* @note Thread safe. Open and close must be executed from the same
* thread. Suspends caller thread till record is available
*/
FURI_RETURNS_NONNULL void* furi_record_open(const char* name);
/** Close record
*
* @param name record name
* @note Thread safe. Open and close must be executed from the same
* thread.
*/
void furi_record_close(const char* name);
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,32 @@
#pragma once
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef void (*ServiceOnStart)(void _Nonnull* parameter);
typedef void (*ServiceOnStop)();
typedef struct {
/**
* The identifier by which the app is launched by the system and other apps.
*/
const char* _Nonnull id;
/**
* Non-blocking method to call when service is started.
*/
const ServiceOnStart _Nullable on_start;
/**
* Non-blocking method to call when service is stopped.
*/
const ServiceOnStop _Nullable on_stop;
} ServiceManifest;
#ifdef __cplusplus
}
#endif

View File

@ -1,10 +1,10 @@
idf_component_register( idf_component_register(
SRC_DIRS "src" SRC_DIRS "src"
"src/apps/system/system_info" "src/apps/system/system_info"
"src/apps/services/desktop" "src/services/desktop"
"src/apps/services/loader" "src/services/loader"
"src/apps/services/gui" "src/services/gui"
"src/apps/services/gui/widgets" "src/services/gui/widgets"
INCLUDE_DIRS "src" INCLUDE_DIRS "src"
REQUIRES esp_lvgl_port esp_lcd esp_lcd_touch driver mlib cmsis_core furi nvs_flash spiffs fatfs REQUIRES esp_lvgl_port esp_lcd esp_lcd_touch driver mlib cmsis_core furi nvs_flash spiffs fatfs
) )

View File

@ -1,13 +0,0 @@
#pragma once
#include "app_manifest.h"
#ifdef __cplusplus
extern "C" {
#endif
extern const AppManifest desktop_app;
#ifdef __cplusplus
}
#endif

View File

@ -4,52 +4,66 @@
#include "furi.h" #include "furi.h"
#include "graphics_i.h" #include "graphics_i.h"
#include "partitions.h" #include "partitions.h"
#include "apps/services/gui/gui.h" #include "services/gui/gui.h"
#define TAG "nanobake" #define TAG "nanobake"
Gui* gui_alloc(); Gui* gui_alloc();
// System services // System services
extern const AppManifest gui_app; extern const ServiceManifest gui_service;
extern const AppManifest loader_app; extern const ServiceManifest loader_service;
extern const ServiceManifest desktop_service;
// Desktop
extern const AppManifest desktop_app;
// System apps // System apps
extern const AppManifest system_info_app; extern const AppManifest system_info_app;
void start_service(const AppManifest* _Nonnull manifest, void* _Nullable context) { void start_service(const ServiceManifest* _Nonnull manifest) {
UNUSED(context); FURI_LOG_I(TAG, "Starting service %s", manifest->id);
FURI_LOG_I(TAG, "Starting service %s", manifest->name);
furi_check(manifest->on_start, "service must define on_start"); furi_check(manifest->on_start, "service must define on_start");
manifest->on_start(NULL); manifest->on_start(NULL);
// TODO: keep track of running services // TODO: keep track of running services
} }
static void register_apps(Config* _Nonnull config) { static void register_system_apps() {
FURI_LOG_I(TAG, "Registering core apps"); FURI_LOG_I(TAG, "Registering default apps");
app_manifest_registry_add(&gui_app);
app_manifest_registry_add(&desktop_app);
app_manifest_registry_add(&loader_app);
app_manifest_registry_add(&system_info_app); app_manifest_registry_add(&system_info_app);
}
static void register_user_apps(const Config* _Nonnull config) {
FURI_LOG_I(TAG, "Registering user apps"); FURI_LOG_I(TAG, "Registering user apps");
for (size_t i = 0; i < config->apps_count; i++) { for (size_t i = 0; i < CONFIG_APPS_LIMIT; i++) {
app_manifest_registry_add(config->apps[i]); const AppManifest* manifest = config->apps[i];
if (manifest != NULL) {
app_manifest_registry_add(manifest);
} else {
// reached end of list
break;
}
} }
} }
static void start_services() { static void start_system_services() {
FURI_LOG_I(TAG, "Starting services"); FURI_LOG_I(TAG, "Starting system services");
app_manifest_registry_for_each_of_type(AppTypeService, NULL, start_service); start_service(&gui_service);
FURI_LOG_I(TAG, "Startup complete"); start_service(&loader_service);
start_service(&desktop_service);
FURI_LOG_I(TAG, "System services started");
} }
static void start_desktop() { static void start_user_services(const Config* _Nonnull config) {
FURI_LOG_I(TAG, "Starting desktop"); FURI_LOG_I(TAG, "Starting user services");
desktop_app.on_start(NULL); for (size_t i = 0; i < CONFIG_SERVICES_LIMIT; i++) {
FURI_LOG_I(TAG, "Startup complete"); const ServiceManifest* manifest = config->services[i];
if (manifest != NULL) {
// TODO: keep track of running services
manifest->on_start(NULL);
} else {
// reached end of list
break;
}
}
FURI_LOG_I(TAG, "User services started");
} }
__attribute__((unused)) extern void nanobake_start(const Config* _Nonnull config) { __attribute__((unused)) extern void nanobake_start(const Config* _Nonnull config) {
@ -60,8 +74,11 @@ __attribute__((unused)) extern void nanobake_start(const Config* _Nonnull config
Hardware hardware = nb_hardware_init(config->hardware); Hardware hardware = nb_hardware_init(config->hardware);
/*NbLvgl lvgl =*/nb_graphics_init(&hardware); /*NbLvgl lvgl =*/nb_graphics_init(&hardware);
register_apps(config); // Register all apps
register_system_apps();
register_user_apps(config);
start_services(); // Start all services
start_desktop(); start_system_services();
start_user_services(config);
} }

View File

@ -3,11 +3,15 @@
#include "app_manifest.h" #include "app_manifest.h"
#include "devices.h" #include "devices.h"
#include "furi_extra_defines.h" #include "furi_extra_defines.h"
#include "service_manifest.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#define CONFIG_APPS_LIMIT 32
#define CONFIG_SERVICES_LIMIT 32
// Forward declarations // Forward declarations
typedef void* FuriThreadId; typedef void* FuriThreadId;
typedef void (*Bootstrap)(); typedef void (*Bootstrap)();
@ -26,8 +30,8 @@ typedef struct {
typedef struct { typedef struct {
const HardwareConfig* hardware; const HardwareConfig* hardware;
// List of user applications // List of user applications
const size_t apps_count; const AppManifest* const apps[CONFIG_APPS_LIMIT];
const AppManifest* const apps[]; const ServiceManifest* const services[CONFIG_SERVICES_LIMIT];
} Config; } Config;
__attribute__((unused)) extern void nanobake_start(const Config _Nonnull* config); __attribute__((unused)) extern void nanobake_start(const Config _Nonnull* config);

View File

@ -1,10 +1,9 @@
#include "desktop.h"
#include "lvgl.h"
#include "check.h"
#include "apps/services/loader/loader.h"
#include "apps/services/gui/gui.h"
#include "apps/services/gui/view_port.h"
#include "app_manifest_registry.h" #include "app_manifest_registry.h"
#include "check.h"
#include "lvgl.h"
#include "services/gui/gui.h"
#include "services/gui/view_port.h"
#include "services/loader/loader.h"
static void on_open_app(lv_event_t* e) { static void on_open_app(lv_event_t* e) {
lv_event_code_t code = lv_event_get_code(e); lv_event_code_t code = lv_event_get_code(e);
@ -42,12 +41,8 @@ static void desktop_stop() {
furi_crash("desktop_stop is not implemented"); furi_crash("desktop_stop is not implemented");
} }
const AppManifest desktop_app = { const ServiceManifest desktop_service = {
.id = "desktop", .id = "desktop",
.name = "Desktop",
.icon = NULL,
.type = AppTypeDesktop,
.on_start = &desktop_start, .on_start = &desktop_start,
.on_stop = &desktop_stop, .on_stop = &desktop_stop
.on_show = NULL
}; };

View File

@ -3,7 +3,6 @@
#include "furi_extra_defines.h" #include "furi_extra_defines.h"
#include "gui_i.h" #include "gui_i.h"
#include "log.h" #include "log.h"
#include "record.h"
#include "kernel.h" #include "kernel.h"
#define TAG "gui" #define TAG "gui"
@ -21,7 +20,7 @@ Gui* gui_alloc() {
furi_check(instance != NULL); furi_check(instance != NULL);
instance->thread = furi_thread_alloc_ex( instance->thread = furi_thread_alloc_ex(
"gui", "gui",
AppStackSizeLarge, // Last known minimum was 2800 for launching desktop 4096, // Last known minimum was 2800 for launching desktop
&gui_main, &gui_main,
NULL NULL
); );
@ -165,14 +164,10 @@ static void gui_stop() {
gui_free(gui); gui_free(gui);
} }
const AppManifest gui_app = { const ServiceManifest gui_service = {
.id = "gui", .id = "gui",
.name = "GUI",
.icon = NULL,
.type = AppTypeService,
.on_start = &gui_start, .on_start = &gui_start,
.on_stop = &gui_stop, .on_stop = &gui_stop
.on_show = NULL
}; };
// endregion // endregion

View File

@ -1,14 +1,12 @@
#pragma once #pragma once
#include "app_manifest.h" #include "service_manifest.h"
#include "view_port.h" #include "view_port.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
extern const AppManifest gui_app;
/** Gui layers */ /** Gui layers */
typedef enum { typedef enum {
GuiLayerDesktop, /**< Desktop layer for internal use. Like fullscreen but with status bar */ GuiLayerDesktop, /**< Desktop layer for internal use. Like fullscreen but with status bar */

View File

@ -1,10 +1,9 @@
#include "check.h" #include "check.h"
#include "esp_lvgl_port.h"
#include "gui_i.h" #include "gui_i.h"
#include "log.h" #include "log.h"
#include "record.h" #include "services/gui/widgets/widgets.h"
#include "esp_lvgl_port.h" #include "services/loader/loader.h"
#include "apps/services/gui/widgets/widgets.h"
#include "apps/services/loader/loader.h"
#define TAG "gui" #define TAG "gui"

View File

@ -1,8 +1,7 @@
#include "apps/services/gui/widgets/widgets.h"
#include "check.h" #include "check.h"
#include "esp_lvgl_port.h"
#include "gui.h" #include "gui.h"
#include "gui_i.h" #include "gui_i.h"
#include "services/gui/widgets/widgets.h"
#include "view_port_i.h" #include "view_port_i.h"
#define TAG "viewport" #define TAG "viewport"

View File

@ -1,7 +1,6 @@
#include "toolbar.h" #include "toolbar.h"
#include "record.h" #include "services/gui/widgets/widgets.h"
#include "apps/services/gui/widgets/widgets.h" #include "services/loader/loader.h"
#include "apps/services/loader/loader.h"
static void app_toolbar_close(lv_event_t* event) { static void app_toolbar_close(lv_event_t* event) {
loader_stop_app(); loader_stop_app();

View File

@ -1,12 +1,13 @@
#include "loader.h"
#include "app_i.h" #include "app_i.h"
#include "app_manifest.h" #include "app_manifest.h"
#include "app_manifest_registry.h" #include "app_manifest_registry.h"
#include "loader_i.h"
#include <sys/cdefs.h>
#include "esp_heap_caps.h" #include "esp_heap_caps.h"
#include "apps/services/gui/gui.h" #include "freertos/FreeRTOS.h"
#include "freertos/semphr.h" #include "freertos/semphr.h"
#include "loader_i.h"
#include "service_manifest.h"
#include "services/gui/gui.h"
#include <sys/cdefs.h>
#define TAG "Loader" #define TAG "Loader"
@ -22,7 +23,7 @@ static Loader* loader_alloc() {
loader->queue = furi_message_queue_alloc(1, sizeof(LoaderMessage)); loader->queue = furi_message_queue_alloc(1, sizeof(LoaderMessage));
loader->thread = furi_thread_alloc_ex( loader->thread = furi_thread_alloc_ex(
"loader", "loader",
AppStackSizeLarge, // Last known minimum was 2400 for starting Hello World app 4096, // Last known minimum was 2400 for starting Hello World app
&loader_main, &loader_main,
NULL NULL
); );
@ -307,14 +308,10 @@ static void loader_stop() {
loader = NULL; loader = NULL;
} }
const AppManifest loader_app = { const ServiceManifest loader_service = {
.id = "loader", .id = "loader",
.name = "Loader",
.icon = NULL,
.type = AppTypeService,
.on_start = &loader_start, .on_start = &loader_start,
.on_stop = &loader_stop, .on_stop = &loader_stop
.on_show = NULL
}; };
// endregion // endregion

View File

@ -1,8 +1,9 @@
#pragma once #pragma once
#include "app_manifest.h"
#include "furi_core.h" #include "furi_core.h"
#include "furi_string.h" #include "furi_string.h"
#include "pubsub.h" #include "pubsub.h"
#include "app_manifest.h" #include "service_manifest.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {

View File

@ -1,12 +1,13 @@
#pragma once #pragma once
#include "api_lock.h" #include "api_lock.h"
#include "app_manifest.h" #include "app_manifest.h"
#include "apps/services/gui/view_port.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/semphr.h" #include "freertos/semphr.h"
#include "loader.h" #include "loader.h"
#include "message_queue.h" #include "message_queue.h"
#include "pubsub.h" #include "pubsub.h"
#include "services/gui/view_port.h"
#include "thread.h" #include "thread.h"
typedef struct { typedef struct {

View File

@ -1,6 +1,6 @@
#include "hello_world.h" #include "hello_world.h"
#include "apps/services/gui/gui.h" #include "services/gui/gui.h"
#include "apps/services/loader/loader.h" #include "services/loader/loader.h"
static void app_show(lv_obj_t* parent, void* context) { static void app_show(lv_obj_t* parent, void* context) {
UNUSED(context); UNUSED(context);

View File

@ -8,9 +8,12 @@ __attribute__((unused)) void app_main(void) {
static const Config config = { static const Config config = {
.hardware = NB_BOARD_HARDWARE, .hardware = NB_BOARD_HARDWARE,
.apps = { .apps = {
&hello_world_app &hello_world_app,
NULL // NULL terminator - do not remove
},
.services = {
NULL // NULL terminator - do not remove
}, },
.apps_count = 1
}; };
nanobake_start(&config); nanobake_start(&config);