MeshX 0.4
This repository provides an implementation for Bluetooth Low Energy (BLE) Mesh network nodes. The project allows you to create BLE mesh nodes that can communicate with each other, enabling the development of smart home solutions or other IoT-based applications.
Loading...
Searching...
No Matches
meshx_nvs.h File Reference

Header file for MeshX Non-Volatile Storage (NVS) operations. More...

#include <stdint.h>
#include <meshx_common.h>
#include "meshx_control_task.h"
#include "meshx_os_timer.h"

Go to the source code of this file.

Data Structures

struct  meshx_nvs
 Structure to hold the MeshX NVS data. More...

Macros

#define MESHX_NVS_TIMER_PERIOD_DEF   1000
#define MESHX_NVS_TIMER_PERIOD   MESHX_NVS_TIMER_PERIOD_DEF
#define MESHX_NVS_AUTO_COMMIT   true
#define MESHX_NVS_NO_AUTO_COMMIT   false

Typedefs

typedef struct meshx_nvs meshx_nvs_t

Functions

meshx_err_t meshx_nvs_init (void)
 MeshX NVS Initialisation.
meshx_err_t meshx_nvs_erase (void)
 Erase all key-value pairs stored in the NVS.
meshx_err_t meshx_nvs_commit (void)
 Commit changes to the NVS.
meshx_err_t meshx_nvs_close (void)
 Close the NVS handle.
meshx_err_t meshx_nvs_remove (char const *key)
 Remove a key-value pair from the NVS.
meshx_err_t meshx_nvs_open (uint16_t cid, uint16_t pid, uint32_t commit_timeout_ms)
 Open the NVS with a timeout.
meshx_err_t meshx_nvs_get (char const *key, void *blob, uint16_t blob_size)
 Get a value from the NVS.
meshx_err_t meshx_nvs_set (char const *key, void const *blob, uint16_t blob_size, bool arm_timer)
 Set a value in the NVS.
meshx_err_t meshx_nvs_element_ctx_get (uint16_t element_id, meshx_element_type_t element_type, void *blob, size_t blob_size)
 Retrieve the context of a specific element from NVS.
meshx_err_t meshx_nvs_element_ctx_set (uint16_t element_id, meshx_element_type_t element_type, const void *blob, size_t blob_size)
 Store the context of a specific element to NVS.
meshx_err_t meshx_nvs_element_ctx_remove (uint16_t element_id, meshx_element_type_t element_type)
 Remove the context of a specific element from NVS.

Detailed Description

Header file for MeshX Non-Volatile Storage (NVS) operations.

This file provides APIs to manage the Non-Volatile Storage (NVS) used in the MeshX system. It includes functions to read, write, erase, and manage key-value pairs stored persistently.

Author
Pranjal Chanda

Macro Definition Documentation

◆ MESHX_NVS_AUTO_COMMIT

#define MESHX_NVS_AUTO_COMMIT   true

◆ MESHX_NVS_NO_AUTO_COMMIT

#define MESHX_NVS_NO_AUTO_COMMIT   false

◆ MESHX_NVS_TIMER_PERIOD

#define MESHX_NVS_TIMER_PERIOD   MESHX_NVS_TIMER_PERIOD_DEF

◆ MESHX_NVS_TIMER_PERIOD_DEF

#define MESHX_NVS_TIMER_PERIOD_DEF   1000

Typedef Documentation

◆ meshx_nvs_t

typedef struct meshx_nvs meshx_nvs_t

Function Documentation

◆ meshx_nvs_close()

meshx_err_t meshx_nvs_close ( void )

Close the NVS handle.

This function releases any resources associated with the NVS handle.

Returns
  • MESHX_SUCCESS: Success.
320{
323 return MESHX_INVALID_STATE;
324
325 err = meshx_nvs_plat_close(meshx_nvs_inst.meshx_nvs_handle);
326 if (err)
327 {
328 MESHX_LOGE(MODULE_ID_COMPONENT_MESHX_NVS, "nvs_close %p", (void *)err);
329 }
330
331#if MESHX_NVS_TIMER_PERIOD
332 err = meshx_os_timer_delete(&(meshx_nvs_inst.meshx_nvs_commit_tmr));
333#endif /* MESHX_NVS_TIMER_PERIOD */
334 meshx_nvs_inst.init = 0;
335 return err;
336}
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
@ MESHX_INVALID_STATE
Definition meshx_err.h:49
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:114
**This function is called by the parent element when a state change request *is received It validates the request and returns a result to the element not the model layer **return * MESHX_SUCCESS
Definition meshx_model_level.cpp:385
#define MESHX_NVS_INIT_MAGIC
Definition meshx_nvs.c:19
static meshx_nvs_t meshx_nvs_inst
Definition meshx_nvs.c:57
meshx_err_t meshx_nvs_plat_close(uintptr_t p_nvs_handle)
Close the non-volatile storage handle.
meshx_err_t meshx_os_timer_delete(meshx_os_timer_t **timer_handle)
Delete a timer.
Definition meshx_os_timer.c:419
@ MODULE_ID_COMPONENT_MESHX_NVS
Definition module_id.h:31

◆ meshx_nvs_commit()

meshx_err_t meshx_nvs_commit ( void )

Commit changes to the NVS.

This function ensures that any pending changes to the NVS are flushed to persistent storage.

Returns
  • MESHX_SUCCESS: Success.
278{
280 return MESHX_INVALID_STATE;
281
285
286 if (!cur) return MESHX_SUCCESS;
287
288 MESHX_LOGD(MODULE_ID_COMPONENT_MESHX_NVS, "Committing pending NVS writes...");
289
290 while (cur) {
291 err = meshx_nvs_plat_write(meshx_nvs_inst.meshx_nvs_handle, cur->key, cur->data, cur->len);
292 if (err) {
293 MESHX_LOGE(MODULE_ID_COMPONENT_MESHX_NVS, "commit failed for key: %s", cur->key);
294 }
295 prev = cur;
296 cur = cur->next;
297 free(prev);
298 }
299
301
302 if (err == MESHX_SUCCESS) {
303 // Only commit to platform once, after all writes
304 err = meshx_nvs_plat_commit(meshx_nvs_inst.meshx_nvs_handle);
305 }
306
307 return err;
308}
#define MESHX_LOGD(module_id, format,...)
Definition meshx_log.h:132
struct meshx_nvs_write_list meshx_nvs_write_list_t
meshx_nvs_write_list_t * meshx_nvs_write_list_head
Head of the linked list used to store key-value pairs that need to be written to NVS.
Definition meshx_nvs.c:49
meshx_err_t meshx_nvs_plat_write(uintptr_t p_nvs_handle, char const *key, uint8_t const *p_data, uint16_t len)
Write a blob value to the non-volatile storage with a given key and namespace.
meshx_err_t meshx_nvs_plat_commit(uintptr_t p_nvs_handle)
Commit changes to the non-volatile storage.
struct meshx_nvs_write_list * next
Definition meshx_nvs.c:42
char key[16]
Definition meshx_nvs.c:40
uint8_t data[256]
Definition meshx_nvs.c:41
uint16_t len
Definition meshx_nvs.c:39

◆ meshx_nvs_element_ctx_get()

meshx_err_t meshx_nvs_element_ctx_get ( uint16_t element_id,
meshx_element_type_t element_type,
void * blob,
size_t blob_size )

Retrieve the context of a specific element from NVS.

Parameters
[in]element_idThe ID of the element whose context is to be retrieved.
[in]element_typeThe type of the element.
[out]blobPointer to the buffer where the retrieved context will be stored.
[in]blob_sizeSize of the buffer provided to store the context.
Returns
  • MESHX_SUCCESS: Successfully retrieved the context.
448{
449 char key[MESHX_KEY_NAME_MAX_SIZE];
450 meshx_tiny_snprintf(key, MESHX_KEY_NAME_MAX_SIZE, MESHX_NVS_ELEMENT_CTX, (uint8_t)element_type, element_id);
451 MESHX_LOGD(MODULE_ID_COMPONENT_MESHX_NVS, "meshx_nvs_element_ctx_get: key=%s, size=%d", key, (int)blob_size);
452 return meshx_nvs_get(key, blob, blob_size);
453}
#define MESHX_KEY_NAME_MAX_SIZE
Definition meshx_nvs.c:30
#define MESHX_NVS_ELEMENT_CTX
Definition meshx_nvs.c:28
meshx_err_t meshx_nvs_get(char const *key, void *blob, uint16_t blob_size)
Get a value from the NVS.
Definition meshx_nvs.c:368
int meshx_tiny_snprintf(char *buf, size_t size, const char *fmt,...)
Lightweight snprintf replacement.
Definition meshx_tiny_printf.c:149

◆ meshx_nvs_element_ctx_remove()

meshx_err_t meshx_nvs_element_ctx_remove ( uint16_t element_id,
meshx_element_type_t element_type )

Remove the context of a specific element from NVS.

Parameters
[in]element_idThe ID of the element whose context is to be removed.
[in]element_typeThe type of the element.
Returns
  • MESHX_SUCCESS: Successfully removed the context.
483{
484 char key[MESHX_KEY_NAME_MAX_SIZE];
485 if (element_type == MESHX_ELEMENT_TYPE_ALL)
486 {
488 for (uint8_t t = 0; t < (uint8_t)MESHX_ELEMENT_TYPE_MAX; t++)
489 {
490 snprintf(key, MESHX_KEY_NAME_MAX_SIZE, MESHX_NVS_ELEMENT_CTX, t, element_id);
491 meshx_err_t temp_err = meshx_nvs_remove(key);
492 if (temp_err != MESHX_SUCCESS && temp_err != MESHX_NOT_FOUND)
493 {
494 err = temp_err;
495 }
496 }
497 return err;
498 }
499 snprintf(key, MESHX_KEY_NAME_MAX_SIZE, MESHX_NVS_ELEMENT_CTX, (uint8_t)element_type, element_id);
500 meshx_tiny_snprintf(key, MESHX_KEY_NAME_MAX_SIZE, MESHX_NVS_ELEMENT_CTX, (uint8_t)element_type, element_id);
501 return meshx_nvs_remove(key);
502}
@ MESHX_NOT_FOUND
Definition meshx_err.h:50
meshx_err_t meshx_nvs_remove(char const *key)
Remove a key-value pair from the NVS.
Definition meshx_nvs.c:348

◆ meshx_nvs_element_ctx_set()

meshx_err_t meshx_nvs_element_ctx_set ( uint16_t element_id,
meshx_element_type_t element_type,
const void * blob,
size_t blob_size )

Store the context of a specific element to NVS.

Parameters
[in]element_idThe ID of the element whose context is to be stored.
[in]element_typeThe type of the element.
[in]blobPointer to the buffer containing the context to be stored.
[in]blob_sizeSize of the buffer containing the context.
Returns
  • MESHX_SUCCESS: Successfully stored the context.
467{
468 char key[MESHX_KEY_NAME_MAX_SIZE];
469 meshx_tiny_snprintf(key, MESHX_KEY_NAME_MAX_SIZE, MESHX_NVS_ELEMENT_CTX, (uint8_t)element_type, element_id);
470 return meshx_nvs_set(key, blob, (uint16_t) blob_size, MESHX_NVS_AUTO_COMMIT);
471}
meshx_err_t meshx_nvs_set(char const *key, void const *blob, uint16_t blob_size, bool arm_timer)
Set a value in the NVS.
Definition meshx_nvs.c:388
#define MESHX_NVS_AUTO_COMMIT
Definition meshx_nvs.h:30

◆ meshx_nvs_erase()

meshx_err_t meshx_nvs_erase ( void )

Erase all key-value pairs stored in the NVS.

This function clears all data stored in the Non-Volatile Storage.

Returns
  • MESHX_SUCCESS: Success.
262{
264 return MESHX_INVALID_STATE;
265
266 return meshx_nvs_plat_erase(meshx_nvs_inst.meshx_nvs_handle);
267}
meshx_err_t meshx_nvs_plat_erase(uintptr_t p_nvs_handle)
Erase all key-value pairs in the given namespace.

◆ meshx_nvs_get()

meshx_err_t meshx_nvs_get ( char const * key,
void * blob,
uint16_t blob_size )

Get a value from the NVS.

This function retrieves a value associated with the given key from the NVS.

Parameters
[in]keyThe key identifying the value to be retrieved.
[out]blobPointer to the buffer where the value will be stored.
[in]blob_sizeSize of the buffer in bytes.
Returns
  • MESHX_SUCCESS: Success.
369{
371 return MESHX_INVALID_STATE;
372 return meshx_nvs_plat_read(meshx_nvs_inst.meshx_nvs_handle, key, blob, blob_size);
373}
meshx_err_t meshx_nvs_plat_read(uintptr_t p_nvs_handle, char const *key, uint8_t *p_data, uint16_t len)
Read blob value for given key from non-volatile storage.

◆ meshx_nvs_init()

meshx_err_t meshx_nvs_init ( void )

MeshX NVS Initialisation.

Returns
  • MESHX_SUCCESS: Success.
143{
145#if CONFIG_ENABLE_UNIT_TEST
147 if (err)
148 {
149 MESHX_LOGE(MODULE_ID_COMPONENT_MESHX_NVS, "unit_test reg failed: (%d)", err);
150 return err;
151 }
152#endif /* CONFIG_ENABLE_UNIT_TEST */
153
155 if (err)
156 {
157 MESHX_LOGE(MODULE_ID_COMPONENT_MESHX_NVS, "control_task_msg_subscribe failed: (%d)", err);
158 return err;
159 }
160
161 return err;
162}
CONTROL_TASK_MSG_EVT_SYSTEM_NVS_COMMIT
Definition meshx_control_task.h:131
@ CONTROL_TASK_MSG_CODE_SYSTEM
Definition meshx_control_task.h:66
meshx_err_t control_task_msg_subscribe(control_task_msg_code_t msg_code, control_task_msg_evt_t evt_bmap, control_task_msg_handle_t callback)
Subscribe to a control task message.
Definition meshx_control_task.c:142
static meshx_err_t meshx_nvs_control_msg_handler(dev_struct_t *pdev, control_task_msg_evt_t evt, void *params)
Definition meshx_nvs.c:59
static meshx_err_t meshx_nvs_unit_test_cb_handler(int cmd_id, int argc, char **argv)
Callback handler for MeshX NVS unit test command.
Definition meshx_nvs.c:535
meshx_err_t register_unit_test(module_id_t module_id, module_callback_t callback)
Register a unit test for a specific module.
Definition unit_test.c:137

◆ meshx_nvs_open()

meshx_err_t meshx_nvs_open ( uint16_t cid,
uint16_t pid,
uint32_t commit_timeout_ms )

Open the NVS with a timeout.

This function initializes the NVS and sets a timeout for stability operations.

Note
NVS Namespace: MESHX_NVS_NAMESPACE
Parameters
[in]cidCompany ID
[in]pidProduct ID
[in]commit_timeout_msTimeout for stability operations in milliseconds.
Note
commit_timeout_ms = 0 -> use MESHX_NVS_TIMER_PERIOD
Returns
  • MESHX_SUCCESS: Success.
181{
183 {
184 return MESHX_INVALID_STATE;
185 }
186
187 meshx_err_t err;
188
189 err = meshx_nvs_plat_open(&(meshx_nvs_inst.meshx_nvs_handle));
190 if (err)
191 {
192 MESHX_LOGE(MODULE_ID_COMPONENT_MESHX_NVS, "nvs_open %p", (void *)err);
193 return err;
194 }
195
196#if MESHX_NVS_TIMER_PERIOD
197 if(commit_timeout_ms == 0)
198 {
199 commit_timeout_ms = MESHX_NVS_TIMER_PERIOD;
200 }
203 commit_timeout_ms,
206 &(meshx_nvs_inst.meshx_nvs_commit_tmr));
207 if (err)
208 {
209 MESHX_LOGE(MODULE_ID_COMPONENT_MESHX_NVS, "os_timer_create %p", (void *)err);
210 return err;
211 }
212#else
213 MESHX_UNUSED(commit_timeout_ms);
214#endif /* MESHX_NVS_TIMER_PERIOD */
215
217
218 err = meshx_nvs_get(
220 &(meshx_nvs_inst.cid),
221 sizeof(meshx_nvs_inst.cid));
222
223 if (err == MESHX_SUCCESS)
224 {
225 err = meshx_nvs_get(
227 &(meshx_nvs_inst.pid),
228 sizeof(meshx_nvs_inst.pid));
229 }
230
231 if(err != MESHX_SUCCESS)
232 {
233 MESHX_LOGW(MODULE_ID_COMPONENT_MESHX_NVS, "Product ID not found in NVS reinitializing MeshX NVS (err: 0x%x)", err);
234 err = meshx_nvs_erase_prod_init(cid, pid);
235 }
236
237 else
238 {
239 if (meshx_nvs_inst.cid == cid && meshx_nvs_inst.pid == pid)
240 {
241 MESHX_LOGD(MODULE_ID_COMPONENT_MESHX_NVS, "Product ID match: %x|%x", meshx_nvs_inst.pid, meshx_nvs_inst.cid);
242 }
243 else
244 {
245 MESHX_LOGW(MODULE_ID_COMPONENT_MESHX_NVS, "Product ID mismatch: %x|%x (Expected: %x|%x)", meshx_nvs_inst.pid, meshx_nvs_inst.cid, pid, cid);
246 err = meshx_nvs_erase_prod_init(cid, pid);
247 }
248 }
249
250 return err;
251}
#define MESHX_UNUSED(x)
Definition meshx_err.h:19
#define MESHX_LOGW(module_id, format,...)
Definition meshx_log.h:120
static void meshx_nvs_os_timer_cb(const meshx_os_timer_t *p_timer)
MeshX NVS Timer callback.
Definition meshx_nvs.c:74
#define MESHX_NVS_RELOAD_ONE_SHOT
Definition meshx_nvs.c:29
#define MESHX_NVS_NAMESPACE_PID
Definition meshx_nvs.c:25
#define MESHX_NVS_TIMER_NAME
Definition meshx_nvs.c:27
#define MESHX_NVS_NAMESPACE_CID
Definition meshx_nvs.c:26
static meshx_err_t meshx_nvs_erase_prod_init(uint16_t cid, uint16_t pid)
Erase the NVS and set the product ID.
Definition meshx_nvs.c:94
#define MESHX_NVS_TIMER_PERIOD
Definition meshx_nvs.h:27
meshx_err_t meshx_nvs_plat_open(uintptr_t *p_nvs_handle)
Open non-volatile storage with a given namespace from the default partition.
meshx_err_t meshx_os_timer_create(const char *name, uint32_t period, bool reload, meshx_os_timer_cb_t cb, meshx_os_timer_t **timer_handle)
Create a timer.
Definition meshx_os_timer.c:264

◆ meshx_nvs_remove()

meshx_err_t meshx_nvs_remove ( char const * key)

Remove a key-value pair from the NVS.

This function deletes a specific key-value pair from the NVS based on the provided key.

Parameters
[in]keyThe key identifying the value to be removed.
Returns
  • MESHX_SUCCESS: Success.
349{
351 return MESHX_INVALID_STATE;
352
353 return meshx_nvs_plat_remove(meshx_nvs_inst.meshx_nvs_handle, key);
354}
meshx_err_t meshx_nvs_plat_remove(uintptr_t p_nvs_handle, char const *key)
Remove a key-value pair from the non-volatile storage with a given key and namespace.

◆ meshx_nvs_set()

meshx_err_t meshx_nvs_set ( char const * key,
void const * blob,
uint16_t blob_size,
bool arm_timer )

Set a value in the NVS.

This function stores a value associated with the given key in the NVS.

Parameters
[in]keyThe key identifying the value to be stored.
[in]blobPointer to the buffer containing the value.
[in]blob_sizeSize of the buffer in bytes.
[in]arm_timerRe-arm stability timer and auto commit
Returns
  • MESHX_SUCCESS: Success.
389{
391 return MESHX_INVALID_STATE;
392
393 if (blob_size > MESHX_KEY_VALUE_MAX_SIZE)
394 return MESHX_INVALID_ARG;
395
396 // Check if key already exists in list → overwrite instead of duplicate
398 while (cur) {
399 if (strncmp(cur->key, key, MESHX_KEY_NAME_MAX_SIZE-1) == 0)
400 {
401 cur->key[MESHX_KEY_NAME_MAX_SIZE - 1] = '\0';
402 memcpy(cur->data, blob, blob_size);
403 cur->len = blob_size;
404 goto restart_timer; // skip new node alloc
405 }
406 cur = cur->next;
407 }
408
409 // Allocate new node
410 meshx_nvs_write_list_t *node = malloc(sizeof(meshx_nvs_write_list_t));
411 if (!node) return MESHX_NO_MEM;
412
413 strncpy(node->key, key, MESHX_KEY_NAME_MAX_SIZE);
414 memcpy(node->data, blob, blob_size);
415 node->len = blob_size;
418
419restart_timer:
420 if (arm_timer)
421 {
422#if MESHX_NVS_TIMER_PERIOD
423 meshx_err_t err = meshx_os_timer_restart(meshx_nvs_inst.meshx_nvs_commit_tmr);
424 if (err)
425 {
426 MESHX_LOGE(MODULE_ID_COMPONENT_MESHX_NVS, "meshx_os_timer_restart failed: (%d)", err);
427 }
428#else
430#endif /* MESHX_NVS_TIMER_PERIOD */
431 }
432
433 return MESHX_SUCCESS;
434}
@ MESHX_INVALID_ARG
Definition meshx_err.h:46
@ MESHX_NO_MEM
Definition meshx_err.h:48
#define MESHX_KEY_VALUE_MAX_SIZE
Definition meshx_nvs.c:31
meshx_err_t meshx_nvs_commit(void)
Commit changes to the NVS.
Definition meshx_nvs.c:277
meshx_err_t meshx_os_timer_restart(const meshx_os_timer_t *timer_handle)
Restart a timer.
Definition meshx_os_timer.c:340