CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
Plugin.h
Go to the documentation of this file.
1
12
13#pragma once
14
16#include "plugin_manager/Raii.h"
17
18#include <cstddef>
19#include <cstdint>
20#include <initializer_list>
21#include <memory>
22#include <set>
23#include <string>
24#include <vector>
25
26// Forward-declare WAMR types here so consumers of this header don't pull in
27// wasm_export.h (which clashes with cdc_log's log_level_t).
28struct WASMModuleCommon;
29struct WASMModuleInstanceCommon;
30struct WASMExecEnv;
31
32namespace cdc::plugin_manager {
33
35 void operator()(WASMModuleCommon* m) const noexcept;
36};
38 void operator()(WASMModuleInstanceCommon* m) const noexcept;
39};
41 void operator()(WASMExecEnv* e) const noexcept;
42};
43
44using WasmModulePtr = std::unique_ptr<WASMModuleCommon, WasmModuleDeleter>;
45using WasmInstancePtr = std::unique_ptr<WASMModuleInstanceCommon, WasmInstanceDeleter>;
46using WasmExecEnvPtr = std::unique_ptr<WASMExecEnv, WasmExecEnvDeleter>;
47
48class Plugin {
49public:
50 Plugin() noexcept;
52
53 Plugin(const Plugin&) = delete;
54 Plugin& operator=(const Plugin&) = delete;
55 Plugin(Plugin&&) noexcept = default;
56 Plugin& operator=(Plugin&&) noexcept = default;
57
62 [[nodiscard]] bool load(const std::string& id, const PluginManifest& manifest);
63
65 void unload() noexcept;
66
74 [[nodiscard]] bool callI(const char* name,
75 std::initializer_list<int32_t> args = {},
76 int32_t* out_i32 = nullptr);
77
78 [[nodiscard]] bool hasExport(const char* name) const;
79
82 [[nodiscard]] bool lastCallTrapped() const noexcept { return last_call_trapped_; }
83
86 [[nodiscard]] const char* lastTrapMessage() const noexcept { return last_trap_; }
87
88 [[nodiscard]] const PluginManifest& manifest() const noexcept { return manifest_; }
89 [[nodiscard]] const std::string& id() const noexcept { return id_; }
90 [[nodiscard]] bool isLoaded() const noexcept
91 {
92 return module_inst_ != nullptr;
93 }
94
106 bool loadLangOverlay(const char* path = nullptr);
107
114 [[nodiscard]] const char* trKey(const char* key) const noexcept;
115
116 // Resources acquired during prerequisite setup; released in reverse order
117 // by PluginManager on exit. Stored as opaque prerequisite names for now.
118 std::vector<std::string> acquired_prereqs;
119
120private:
121 struct OverlayEntry {
122 std::string key;
123 std::string value;
124 };
125
126 std::string id_;
127 PluginManifest manifest_;
128 PsramUniquePtr<uint8_t> bytecode_;
129 std::size_t bytecode_len_ = 0;
130 WasmModulePtr module_;
131 WasmInstancePtr module_inst_;
132 WasmExecEnvPtr exec_env_;
133
134 // Per-plugin translation overlay, sorted by key. Loaded from `<id>.lang`.
135 // Active language only; reload on language switch.
136 std::vector<OverlayEntry> langOverlay_;
137 std::string langOverlayLang_;
138
139 std::set<std::string> missingExports_;
140
141 bool last_call_trapped_ = false;
142 char last_trap_[160] = {0};
143};
144
145} // namespace cdc::plugin_manager
In-memory representation of a plugin's meta.json.
char name[cdc::hal::ISecureElement::RMEM_NAME_LEN]
bool load(const std::string &id, const PluginManifest &manifest)
Load bytecode + instantiate the module. Does NOT run plugin_init yet - the manager handles ordering o...
Definition Plugin.cpp:78
std::vector< std::string > acquired_prereqs
Definition Plugin.h:118
bool hasExport(const char *name) const
Definition Plugin.cpp:128
const char * trKey(const char *key) const noexcept
Look up a plugin-local translation key in the loaded overlay.
Definition Plugin.cpp:260
bool lastCallTrapped() const noexcept
Definition Plugin.h:82
bool isLoaded() const noexcept
Definition Plugin.h:90
bool callI(const char *name, std::initializer_list< int32_t > args={}, int32_t *out_i32=nullptr)
Call an exported i32(i32...)->i32 function by name.
Definition Plugin.cpp:145
const char * lastTrapMessage() const noexcept
Definition Plugin.h:86
void unload() noexcept
Destroy WAMR instance + free bytecode buffer. Idempotent.
Definition Plugin.cpp:134
bool loadLangOverlay(const char *path=nullptr)
Load the plugin's translation overlay from disk into PSRAM.
Definition Plugin.cpp:192
const std::string & id() const noexcept
Definition Plugin.h:89
const PluginManifest & manifest() const noexcept
Definition Plugin.h:88
::cdc::core::PsramUniquePtr< T > PsramUniquePtr
Definition Raii.h:21
std::unique_ptr< WASMExecEnv, WasmExecEnvDeleter > WasmExecEnvPtr
Definition Plugin.h:46
std::unique_ptr< WASMModuleCommon, WasmModuleDeleter > WasmModulePtr
Definition Plugin.h:44
std::unique_ptr< WASMModuleInstanceCommon, WasmInstanceDeleter > WasmInstancePtr
Definition Plugin.h:45
Thin alias layer that re-exports cdc::core RAII wrappers in the cdc::plugin_manager namespace for sou...
void operator()(WASMExecEnv *e) const noexcept
Definition Plugin.cpp:32
void operator()(WASMModuleInstanceCommon *m) const noexcept
Definition Plugin.cpp:27
void operator()(WASMModuleCommon *m) const noexcept
Definition Plugin.cpp:22