Refactor struct Module

This commit is contained in:
Ken Van Hoeylandt 2026-02-06 00:05:23 +01:00
parent 626d0d9776
commit 1de6f52604
52 changed files with 119 additions and 62 deletions

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "btt-panda-touch", .name = "btt-panda-touch",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "cyd-2432s024c", .name = "cyd-2432s024c",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "cyd-2432s028r", .name = "cyd-2432s028r",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "cyd-2432s028rv3", .name = "cyd-2432s028rv3",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "cyd-2432s032c", .name = "cyd-2432s032c",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "cyd-4848s040c", .name = "cyd-4848s040c",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "cyd-8048s043c", .name = "cyd-8048s043c",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "cyd-e32r28t", .name = "cyd-e32r28t",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "cyd-e32r32p", .name = "cyd-e32r32p",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "elecrow-crowpanel-advance-35", .name = "elecrow-crowpanel-advance-35",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "elecrow-crowpanel-advance-50", .name = "elecrow-crowpanel-advance-50",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "elecrow-crowpanel-basic-28", .name = "elecrow-crowpanel-basic-28",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "elecrow-crowpanel-basic-35", .name = "elecrow-crowpanel-basic-35",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "elecrow-crowpanel-basic-50", .name = "elecrow-crowpanel-basic-50",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "generic-esp32", .name = "generic-esp32",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "generic-esp32c6", .name = "generic-esp32c6",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "generic-esp32p4", .name = "generic-esp32p4",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "generic-esp32s3", .name = "generic-esp32s3",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "guition-jc1060p470ciwy", .name = "guition-jc1060p470ciwy",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "guition-jc2432w328c", .name = "guition-jc2432w328c",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "guition-jc3248w535c", .name = "guition-jc3248w535c",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "guition-jc8048w550c", .name = "guition-jc8048w550c",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "heltec-wifi-lora-32-v3", .name = "heltec-wifi-lora-32-v3",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "lilygo-tdeck", .name = "lilygo-tdeck",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "lilygo-tdisplay-s3", .name = "lilygo-tdisplay-s3",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "lilygo-tdisplay", .name = "lilygo-tdisplay",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "lilygo-tdongle-s3", .name = "lilygo-tdongle-s3",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -25,7 +25,8 @@ struct Module device_module = {
.name = "lilygo-tlora-pager", .name = "lilygo-tlora-pager",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "m5stack-cardputer-adv", .name = "m5stack-cardputer-adv",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "m5stack-cardputer", .name = "m5stack-cardputer",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "m5stack-core2", .name = "m5stack-core2",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "m5stack-cores3", .name = "m5stack-cores3",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "m5stack-papers3", .name = "m5stack-papers3",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "m5stack-stickc-plus", .name = "m5stack-stickc-plus",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "m5stack-stickc-plus2", .name = "m5stack-stickc-plus2",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "m5stack-tab5", .name = "m5stack-tab5",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "simulator", .name = "simulator",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "unphone", .name = "unphone",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "waveshare-esp32-s3-geek", .name = "waveshare-esp32-s3-geek",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "waveshare-s3-lcd-13", .name = "waveshare-s3-lcd-13",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "waveshare-s3-touch-lcd-128", .name = "waveshare-s3-touch-lcd-128",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "waveshare-s3-touch-lcd-147", .name = "waveshare-s3-touch-lcd-147",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "waveshare-s3-touch-lcd-43", .name = "waveshare-s3-touch-lcd-43",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -17,7 +17,8 @@ struct Module device_module = {
.name = "wireless-tag-wt32-sc01-plus", .name = "wireless-tag-wt32-sc01-plus",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -25,7 +25,8 @@ struct Module hal_device_module = {
.name = "hal-device", .name = "hal-device",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -64,5 +64,6 @@ struct Module lvgl_module = {
.name = "lvgl", .name = "lvgl",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = (const struct ModuleSymbol*)lvgl_module_symbols .symbols = (const struct ModuleSymbol*)lvgl_module_symbols,
.internal = NULL
}; };

View File

@ -31,7 +31,8 @@ struct Module platform_module = {
.name = "platform-esp32", .name = "platform-esp32",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -18,7 +18,8 @@ struct Module platform_module = {
.name = "platform-posix", .name = "platform-posix",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = nullptr .symbols = nullptr,
.internal = nullptr
}; };
} }

View File

@ -59,9 +59,7 @@ struct Module {
*/ */
const struct ModuleSymbol* symbols; const struct ModuleSymbol* symbols;
struct { void* internal;
bool started;
} internal;
}; };
/** /**

View File

@ -23,7 +23,8 @@ struct Module root_module = {
.name = "kernel", .name = "kernel",
.start = start, .start = start,
.stop = stop, .stop = stop,
.symbols = (const struct ModuleSymbol*)KERNEL_SYMBOLS .symbols = (const struct ModuleSymbol*)KERNEL_SYMBOLS,
.internal = nullptr
}; };
error_t kernel_init(struct Module* platform_module, struct Module* device_module, struct CompatibleDevice devicetree_devices[]) { error_t kernel_init(struct Module* platform_module, struct Module* device_module, struct CompatibleDevice devicetree_devices[]) {

View File

@ -3,9 +3,14 @@
#include <algorithm> #include <algorithm>
#include <tactility/concurrent/mutex.h> #include <tactility/concurrent/mutex.h>
#include <tactility/module.h> #include <tactility/module.h>
#include <new>
#define TAG "module" #define TAG "module"
struct ModuleInternal {
bool started;
};
struct ModuleLedger { struct ModuleLedger {
std::vector<struct Module*> modules; std::vector<struct Module*> modules;
struct Mutex mutex = { 0 }; struct Mutex mutex = { 0 };
@ -19,11 +24,14 @@ static ModuleLedger ledger;
extern "C" { extern "C" {
error_t module_construct(struct Module* module) { error_t module_construct(struct Module* module) {
module->internal.started = false; module->internal = new (std::nothrow) ModuleInternal { .started = false };
if (module->internal == nullptr) return ERROR_OUT_OF_MEMORY;
return ERROR_NONE; return ERROR_NONE;
} }
error_t module_destruct(struct Module* module) { error_t module_destruct(struct Module* module) {
delete static_cast<ModuleInternal*>(module->internal);
module->internal = nullptr;
return ERROR_NONE; return ERROR_NONE;
} }
@ -44,28 +52,30 @@ error_t module_remove(struct Module* module) {
error_t module_start(struct Module* module) { error_t module_start(struct Module* module) {
LOG_I(TAG, "start %s", module->name); LOG_I(TAG, "start %s", module->name);
if (module->internal.started) return ERROR_NONE; auto* internal = static_cast<ModuleInternal*>(module->internal);
if (internal->started) return ERROR_NONE;
error_t error = module->start(); error_t error = module->start();
module->internal.started = (error == ERROR_NONE); internal->started = (error == ERROR_NONE);
return error; return error;
} }
bool module_is_started(struct Module* module) { bool module_is_started(struct Module* module) {
return module->internal.started; return static_cast<ModuleInternal*>(module->internal)->started;
} }
error_t module_stop(struct Module* module) { error_t module_stop(struct Module* module) {
LOG_I(TAG, "stop %s", module->name); LOG_I(TAG, "stop %s", module->name);
if (!module->internal.started) return ERROR_NONE; auto* internal = static_cast<ModuleInternal*>(module->internal);
if (!internal->started) return ERROR_NONE;
error_t error = module->stop(); error_t error = module->stop();
if (error != ERROR_NONE) { if (error != ERROR_NONE) {
return error; return error;
} }
module->internal.started = false; internal->started = false;
return error; return error;
} }

View File

@ -23,7 +23,7 @@ TEST_CASE("Module construction and destruction") {
.start = test_start, .start = test_start,
.stop = test_stop, .stop = test_stop,
.symbols = nullptr, .symbols = nullptr,
.internal = {.started = false} .internal = nullptr
}; };
// Test successful construction // Test successful construction
@ -40,7 +40,7 @@ TEST_CASE("Module registration") {
.start = test_start, .start = test_start,
.stop = test_stop, .stop = test_stop,
.symbols = nullptr, .symbols = nullptr,
.internal = {.started = false} .internal = nullptr
}; };
// module_add should succeed // module_add should succeed
@ -61,7 +61,7 @@ TEST_CASE("Module lifecycle") {
.start = test_start, .start = test_start,
.stop = test_stop, .stop = test_stop,
.symbols = nullptr, .symbols = nullptr,
.internal = {.started = false} .internal = nullptr
}; };
// 1. Successful start (no parent required anymore) // 1. Successful start (no parent required anymore)
@ -117,7 +117,7 @@ TEST_CASE("Global symbol resolution") {
.start = test_start, .start = test_start,
.stop = test_stop, .stop = test_stop,
.symbols = test_symbols, .symbols = test_symbols,
.internal = {.started = false} .internal = nullptr
}; };
uintptr_t addr; uintptr_t addr;