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.c File Reference

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

Data Structures

struct  meshx_nvs_write_list
 Linked list structure used to store key-value pairs that need to be written to NVS. More...

Macros

#define MESHX_NVS_INIT_MAGIC   0x5489
#define MESHX_NVS_NAMESPACE_PID   "MESHX_PID"
#define MESHX_NVS_NAMESPACE_CID   "MESHX_CID"
#define MESHX_NVS_TIMER_NAME   "MESHX_COMMIT_TIMER"
#define MESHX_NVS_ELEMENT_CTX   "MXE_%02x_%04x"
#define MESHX_NVS_RELOAD_ONE_SHOT   0
#define MESHX_KEY_NAME_MAX_SIZE   16
#define MESHX_KEY_VALUE_MAX_SIZE   256
#define MESHX_NVS_UNIT_TEST_KEY   "MESHX_UT"

Typedefs

typedef struct meshx_nvs_write_list meshx_nvs_write_list_t
typedef enum meshx_nvs_cli_cmd meshx_nvs_cli_cmd_t
 MeshX NVS unit test command IDs.

Enumerations

enum  meshx_nvs_cli_cmd {
  MESHX_NVS_CLI_CMD_OPEN ,
  MESHX_NVS_CLI_CMD_SET ,
  MESHX_NVS_CLI_CMD_GET ,
  MESHX_NVS_CLI_CMD_COMMIT ,
  MESHX_NVS_CLI_CMD_REMOVE ,
  MESHX_NVS_CLI_CMD_ERASE ,
  MESHX_NVS_CLI_CMD_CLOSE ,
  MESHX_NVS_CLI_MAX
}
 MeshX NVS unit test command IDs. More...

Functions

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.
static meshx_err_t meshx_nvs_control_msg_handler (dev_struct_t *pdev, control_task_msg_evt_t evt, void *params)
static void meshx_nvs_os_timer_cb (const meshx_os_timer_t *p_timer)
 MeshX NVS Timer callback.
static meshx_err_t meshx_nvs_erase_prod_init (uint16_t cid, uint16_t pid)
 Erase the NVS and set the product ID.
meshx_err_t meshx_nvs_init (void)
 MeshX NVS Initialisation.
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_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_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.

Variables

meshx_nvs_write_list_tmeshx_nvs_write_list_head = NULL
 Head of the linked list used to store key-value pairs that need to be written to NVS.
static meshx_nvs_t meshx_nvs_inst

Detailed Description

Implementation for MeshX Non-Volatile Storage (NVS) operations.

Copyright © 2024 - 2025 MeshX

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_KEY_NAME_MAX_SIZE

#define MESHX_KEY_NAME_MAX_SIZE   16

◆ MESHX_KEY_VALUE_MAX_SIZE

#define MESHX_KEY_VALUE_MAX_SIZE   256

◆ MESHX_NVS_ELEMENT_CTX

#define MESHX_NVS_ELEMENT_CTX   "MXE_%02x_%04x"

◆ MESHX_NVS_INIT_MAGIC

#define MESHX_NVS_INIT_MAGIC   0x5489

◆ MESHX_NVS_NAMESPACE_CID

#define MESHX_NVS_NAMESPACE_CID   "MESHX_CID"

◆ MESHX_NVS_NAMESPACE_PID

#define MESHX_NVS_NAMESPACE_PID   "MESHX_PID"

◆ MESHX_NVS_RELOAD_ONE_SHOT

#define MESHX_NVS_RELOAD_ONE_SHOT   0

◆ MESHX_NVS_TIMER_NAME

#define MESHX_NVS_TIMER_NAME   "MESHX_COMMIT_TIMER"

◆ MESHX_NVS_UNIT_TEST_KEY

#define MESHX_NVS_UNIT_TEST_KEY   "MESHX_UT"

Typedef Documentation

◆ meshx_nvs_cli_cmd_t

MeshX NVS unit test command IDs.

◆ meshx_nvs_write_list_t

Enumeration Type Documentation

◆ meshx_nvs_cli_cmd

MeshX NVS unit test command IDs.

Enumerator
MESHX_NVS_CLI_CMD_OPEN 
MESHX_NVS_CLI_CMD_SET 
MESHX_NVS_CLI_CMD_GET 
MESHX_NVS_CLI_CMD_COMMIT 
MESHX_NVS_CLI_CMD_REMOVE 
MESHX_NVS_CLI_CMD_ERASE 
MESHX_NVS_CLI_CMD_CLOSE 
MESHX_NVS_CLI_MAX 
509 {
510 MESHX_NVS_CLI_CMD_OPEN, /* ut 3 0 0 */
511 MESHX_NVS_CLI_CMD_SET, /* ut 3 1 1 [arm_timer?] */
512 MESHX_NVS_CLI_CMD_GET, /* ut 3 2 0 */
513 MESHX_NVS_CLI_CMD_COMMIT, /* ut 3 3 0 */
514 MESHX_NVS_CLI_CMD_REMOVE, /* ut 3 4 0 */
515 MESHX_NVS_CLI_CMD_ERASE, /* ut 3 5 0 */
516 MESHX_NVS_CLI_CMD_CLOSE, /* ut 3 6 0 */
enum meshx_nvs_cli_cmd meshx_nvs_cli_cmd_t
MeshX NVS unit test command IDs.
@ MESHX_NVS_CLI_CMD_SET
Definition meshx_nvs.c:511
@ MESHX_NVS_CLI_CMD_REMOVE
Definition meshx_nvs.c:514
@ MESHX_NVS_CLI_CMD_COMMIT
Definition meshx_nvs.c:513
@ MESHX_NVS_CLI_CMD_CLOSE
Definition meshx_nvs.c:516
@ MESHX_NVS_CLI_MAX
Definition meshx_nvs.c:517
@ MESHX_NVS_CLI_CMD_ERASE
Definition meshx_nvs.c:515
@ MESHX_NVS_CLI_CMD_OPEN
Definition meshx_nvs.c:510
@ MESHX_NVS_CLI_CMD_GET
Definition meshx_nvs.c:512

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_control_msg_handler()

meshx_err_t meshx_nvs_control_msg_handler ( dev_struct_t * pdev,
control_task_msg_evt_t evt,
void * params )
static
60{
63 }
64 return MESHX_SUCCESS;
65}
CONTROL_TASK_MSG_EVT_SYSTEM_NVS_COMMIT
Definition meshx_control_task.h:131
meshx_err_t meshx_nvs_commit(void)
Commit changes to the NVS.
Definition meshx_nvs.c:277

◆ 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_erase_prod_init()

meshx_err_t meshx_nvs_erase_prod_init ( uint16_t cid,
uint16_t pid )
static

Erase the NVS and set the product ID.

This function erases the NVS and sets the product ID.

Parameters
[in]cidCompany ID
[in]pidProduct ID
Returns
  • MESHX_SUCCESS: Success.
95{
97 err = meshx_nvs_erase();
98 if (err)
99 {
100 MESHX_LOGE(MODULE_ID_COMPONENT_MESHX_NVS, "meshx_nvs_erase %p", (void *)err);
101 return err;
102 }
103
104 meshx_nvs_inst.pid = pid;
105 meshx_nvs_inst.cid = cid;
106
107 err = meshx_nvs_set(
109 &(meshx_nvs_inst.pid),
110 sizeof(meshx_nvs_inst.pid),
112
113 if (err)
114 {
115 MESHX_LOGE(MODULE_ID_COMPONENT_MESHX_NVS, "meshx_nvs_set %p", (void *)err);
116 return err;
117 }
118
119 err = meshx_nvs_set(
121 &(meshx_nvs_inst.cid),
122 sizeof(meshx_nvs_inst.cid),
124
125 if (err)
126 {
127 MESHX_LOGE(MODULE_ID_COMPONENT_MESHX_NVS, "meshx_nvs_set %p", (void *)err);
128 return err;
129 }
130
132
133 return err;
134}
#define MESHX_NVS_NAMESPACE_PID
Definition meshx_nvs.c:25
#define MESHX_NVS_NAMESPACE_CID
Definition meshx_nvs.c:26
meshx_err_t meshx_nvs_erase(void)
Erase all key-value pairs stored in the NVS.
Definition meshx_nvs.c:261
#define MESHX_NVS_NO_AUTO_COMMIT
Definition meshx_nvs.h:31

◆ 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_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_TIMER_NAME
Definition meshx_nvs.c:27
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_os_timer_cb()

void meshx_nvs_os_timer_cb ( const meshx_os_timer_t * p_timer)
static

MeshX NVS Timer callback.

Note
This is responsible for performing the commit after stability timeout
Parameters
[in]p_timermeshx_os_timer param pointer *
75{
76 MESHX_UNUSED(p_timer);
77 MESHX_LOGD(MODULE_ID_COMPONENT_MESHX_NVS, "%s fire -> Offloading commit to control task", OS_TMER_GET_TIMER_NAME(p_timer));
78
80}
meshx_err_t control_task_msg_publish(control_task_msg_code_t msg_code, control_task_msg_evt_t msg_evt, const void *msg_evt_params, size_t sizeof_msg_evt_params)
Publish a control task message.
Definition meshx_control_task.c:84
#define OS_TMER_GET_TIMER_NAME(timer)
return timer registered name pointer
Definition meshx_os_timer.h:29

◆ 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_os_timer_restart(const meshx_os_timer_t *timer_handle)
Restart a timer.
Definition meshx_os_timer.c:340

◆ meshx_nvs_unit_test_cb_handler()

meshx_err_t meshx_nvs_unit_test_cb_handler ( int cmd_id,
int argc,
char ** argv )
static

Callback handler for MeshX NVS unit test command.

This function handles MeshX NVS unit test command by processing the provided command ID and arguments.

Parameters
[in]cmd_idThe command ID to be processed.
[in]argcThe number of arguments provided.
[in]argvThe array of arguments.
Returns
  • MESHX_SUCCESS: Success
  • MESHX_INVALID_ARG: Invalid arguments
  • Other error codes depending on the implementation
536{
538 uint32_t ut_blob = 0xDEAD;
539 uint32_t ut_blob_get = 0x00;
540 bool arm_timer;
541
542 MESHX_UNUSED(argc);
543 MESHX_LOGD(MODULE_ID_COMPONENT_MESHX_NVS, "argc|cmd_id: %d|%d", argc, cmd_id);
544 if (cmd_id >= MESHX_NVS_CLI_MAX)
545 {
546 MESHX_LOGE(MODULE_ID_COMPONENT_MESHX_NVS, "Invalid number of arguments");
547 return MESHX_INVALID_ARG;
548 }
549
550 switch(cmd_id)
551 {
553 /* MeshX NVS Open uses defaut values */
555 break;
557 arm_timer = UT_GET_ARG(0, bool, argv) == 0 ? MESHX_NVS_NO_AUTO_COMMIT : MESHX_NVS_AUTO_COMMIT;
558 err = meshx_nvs_set(MESHX_NVS_UNIT_TEST_KEY, &ut_blob, sizeof(ut_blob), arm_timer);
559 break;
561 err = meshx_nvs_get(MESHX_NVS_UNIT_TEST_KEY, &ut_blob_get, sizeof(ut_blob_get));
562 if(err == MESHX_SUCCESS && ut_blob != ut_blob_get)
563 MESHX_LOGE(MODULE_ID_COMPONENT_MESHX_NVS, "MESHX NVS Integrety Test Failed");
564 break;
566 err = meshx_nvs_commit();
567 break;
570 break;
572 err = meshx_nvs_erase();
573 break;
575 err = meshx_nvs_close();
576 break;
577 default:
578 err = MESHX_INVALID_ARG;
579 break;
580 }
581
582 return err;
583}
#define CONFIG_PID_ID
Definition meshx_config_internal.h:37
#define CONFIG_CID_ID
Application Main.
Definition meshx_config_internal.h:33
meshx_err_t meshx_nvs_close(void)
Close the NVS handle.
Definition meshx_nvs.c:319
#define MESHX_NVS_UNIT_TEST_KEY
Definition meshx_nvs.c:52
meshx_err_t meshx_nvs_open(uint16_t cid, uint16_t pid, uint32_t commit_timeout_ms)
Open the NVS with a timeout.
Definition meshx_nvs.c:180
#define UT_GET_ARG(_x, _type, _argv)
Macro to extract an argument from the argument list.
Definition unit_test.h:31

Variable Documentation

◆ meshx_nvs_inst

meshx_nvs_t meshx_nvs_inst
static

◆ meshx_nvs_write_list_head

meshx_nvs_write_list_t* meshx_nvs_write_list_head = NULL

Head of the linked list used to store key-value pairs that need to be written to NVS.