24std::string activeNamespace()
27 if (!plugin)
return {};
30 return std::string(
"plugin_") + plugin->id();
33bool namespaceOk(
const std::string& ns)
35 constexpr size_t NVS_MAX = 15;
36 if (ns.empty() || ns.size() > NVS_MAX)
return false;
37 if (ns.rfind(
"plg_", 0) != 0 && ns.rfind(
"plugin_", 0) != 0)
return false;
39 bool ok = (c >=
'a' && c <=
'z') || (c >=
'0' && c <=
'9') || c ==
'_';
40 if (!ok)
return false;
47 std::string ns = activeNamespace();
57int translateErr(esp_err_t err)
69 if (writeErr == ESP_OK) writeErr = h.
commit();
70 return translateErr(writeErr);
81 int rc = openScope(
false, h);
83 return translateErr(nvs_get_blob(h, key, buf, len));
90 int rc = openScope(
true, h);
92 return writeAndCommit(h, nvs_set_blob(h, key, buf, len));
99 int rc = openScope(
false, h);
101 return translateErr(nvs_get_u32(h, key, out));
108 int rc = openScope(
true, h);
110 return writeAndCommit(h, nvs_set_u32(h, key, value));
117 int rc = openScope(
false, h);
120 return translateErr(nvs_get_str(h, key, buf, &n));
127 int rc = openScope(
true, h);
129 return writeAndCommit(h, nvs_set_str(h, key, value));
136 int rc = openScope(
true, h);
138 return writeAndCommit(h, nvs_erase_key(h, key));
144 int rc = openScope(
true, h);
146 return writeAndCommit(h, nvs_erase_all(h));
152 std::string ns = activeNamespace();
155 nvs_iterator_t it =
nullptr;
156 esp_err_t err = nvs_entry_find(NVS_DEFAULT_PART_NAME, ns.c_str(), NVS_TYPE_ANY, &it);
158 const size_t max = (out ? *out_len : 0);
159 while (err == ESP_OK && it) {
160 nvs_entry_info_t info;
161 nvs_entry_info(it, &info);
162 size_t need = std::strlen(info.key) + 1;
163 if (out && (written + need) <= max) {
164 std::memcpy(out + written, info.key, need - 1);
165 out[written + need - 1] =
'\n';
168 err = nvs_entry_next(&it);
170 nvs_release_iterator(it);
Owned WAMR module instance + per-plugin state.
Shared RAII wrappers for firmware resources.
RAII wrapper for an NVS handle.
esp_err_t commit() noexcept
Commit pending writes. Caller checks the return value if needed.
esp_err_t status() const noexcept
esp_err_t result of the original nvs_open call.
const PluginManifest & manifest() const noexcept
int host_nvs_list_keys(char *out, size_t *out_len)
Enumerate the keys in the plugin's namespace.
int host_nvs_erase(const char *key)
Delete a single key.
int host_nvs_set_u32(const char *key, uint32_t value)
Write a uint32 value.
int host_nvs_get_blob(const char *key, uint8_t *buf, size_t *len)
Read a binary blob from NVS.
int host_nvs_get_str(const char *key, char *buf, size_t buf_size)
Read a NUL-terminated string.
int host_nvs_get_u32(const char *key, uint32_t *out)
Read a uint32 value.
int host_nvs_erase_all(void)
Erase every key in the plugin's namespace.
int host_nvs_set_blob(const char *key, const uint8_t *buf, size_t len)
Write a binary blob to NVS.
int host_nvs_set_str(const char *key, const char *value)
Write a NUL-terminated string.
CDC Badge OS plugin host API - canonical C ABI contract.
#define HOST_ERR_NO_CAPABILITY
#define HOST_ERR_INVALID_ARG
#define HOST_ERR_NO_MEMORY
#define HOST_ERR_NOT_FOUND
void plg_log_warn(const char *msg)
void * plg_get_active_plugin(void)
std::string nvs_namespace
PluginCapabilities capabilities