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
Go to the documentation of this file.
1/**
2 * @file meshx_nvs.h
3 * @brief Header file for MeshX Non-Volatile Storage (NVS) operations.
4 *
5 * This file provides APIs to manage the Non-Volatile Storage (NVS) used in the MeshX system.
6 * It includes functions to read, write, erase, and manage key-value pairs stored persistently.
7 *
8 * @author Pranjal Chanda
9 *
10 */
11
12#ifndef __MESHX_NVS_H__
13#define __MESHX_NVS_H__
14
15#include <stdint.h>
16#include <meshx_common.h>
17#include "meshx_control_task.h"
18#include "meshx_os_timer.h"
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24#define MESHX_NVS_TIMER_PERIOD_DEF 1000
25
26#ifndef MESHX_NVS_TIMER_PERIOD
27#define MESHX_NVS_TIMER_PERIOD MESHX_NVS_TIMER_PERIOD_DEF
28#endif /* MESHX_NVS_TIMER_PERIOD */
29
30#define MESHX_NVS_AUTO_COMMIT true
31#define MESHX_NVS_NO_AUTO_COMMIT false
32
33/**
34 * @struct meshx_nvs
35 * @brief Structure to hold the MeshX NVS data.
36 */
37typedef struct meshx_nvs {
38 uint16_t init; /**< NVS initialization flag */
39 uint16_t cid; /**< Company ID */
40 uint16_t pid; /**< Product ID */
41 uintptr_t meshx_nvs_handle; /**< NVS handle */
42#ifdef MESHX_NVS_TIMER_PERIOD
43 meshx_os_timer_t *meshx_nvs_commit_tmr; /**< NVS stability timer */
44#endif /* MESHX_NVS_TIMER_PERIOD */
46
47/**
48 * @brief MeshX NVS Initialisation
49 *
50 * @return
51 * - MESHX_SUCCESS: Success.
52 */
54
55/**
56 * @brief Erase all key-value pairs stored in the NVS.
57 *
58 * This function clears all data stored in the Non-Volatile Storage.
59 *
60 * @return
61 * - MESHX_SUCCESS: Success.
62 */
64
65/**
66 * @brief Commit changes to the NVS.
67 *
68 * This function ensures that any pending changes to the NVS are flushed to persistent storage.
69 *
70 * @return
71 * - MESHX_SUCCESS: Success.
72 */
74
75/**
76 * @brief Close the NVS handle.
77 *
78 * This function releases any resources associated with the NVS handle.
79 *
80 * @return
81 * - MESHX_SUCCESS: Success.
82 */
84
85/**
86 * @brief Remove a key-value pair from the NVS.
87 *
88 * This function deletes a specific key-value pair from the NVS based on the provided key.
89 *
90 * @param[in] key The key identifying the value to be removed.
91 *
92 * @return
93 * - MESHX_SUCCESS: Success.
94 */
95meshx_err_t meshx_nvs_remove(char const* key);
96
97/**
98 * @brief Open the NVS with a timeout.
99 *
100 * This function initializes the NVS and sets a timeout for stability operations.
101 * @note NVS Namespace: MESHX_NVS_NAMESPACE
102 *
103 * @param[in] cid Company ID
104 * @param[in] pid Product ID
105 * @param[in] commit_timeout_ms Timeout for stability operations in milliseconds.
106 *
107 *
108 * @note commit_timeout_ms = 0 -> use MESHX_NVS_TIMER_PERIOD
109 *
110 * @return
111 * - MESHX_SUCCESS: Success.
112 */
113meshx_err_t meshx_nvs_open(uint16_t cid, uint16_t pid, uint32_t commit_timeout_ms);
114
115/**
116 * @brief Get a value from the NVS.
117 *
118 * This function retrieves a value associated with the given key from the NVS.
119 *
120 * @param[in] key The key identifying the value to be retrieved.
121 * @param[out] blob Pointer to the buffer where the value will be stored.
122 * @param[in] blob_size Size of the buffer in bytes.
123 *
124 * @return
125 * - MESHX_SUCCESS: Success.
126 */
127meshx_err_t meshx_nvs_get(char const *key, void *blob, uint16_t blob_size);
128
129/**
130 * @brief Set a value in the NVS.
131 *
132 * This function stores a value associated with the given key in the NVS.
133 *
134 * @param[in] key The key identifying the value to be stored.
135 * @param[in] blob Pointer to the buffer containing the value.
136 * @param[in] blob_size Size of the buffer in bytes.
137 * @param[in] arm_timer Re-arm stability timer and auto commit
138 *
139 * @return
140 * - MESHX_SUCCESS: Success.
141 */
142meshx_err_t meshx_nvs_set(char const* key, void const* blob, uint16_t blob_size, bool arm_timer);
143
144/**
145 * @brief Retrieve the context of a specific element from NVS.
146 *
147 * @param[in] element_id The ID of the element whose context is to be retrieved.
148 * @param[in] element_type The type of the element.
149 * @param[out] blob Pointer to the buffer where the retrieved context will be stored.
150 * @param[in] blob_size Size of the buffer provided to store the context.
151 *
152 * @return
153 * - MESHX_SUCCESS: Successfully retrieved the context.
154 */
155meshx_err_t meshx_nvs_element_ctx_get(uint16_t element_id, meshx_element_type_t element_type, void *blob, size_t blob_size);
156
157/**
158 * @brief Store the context of a specific element to NVS.
159 *
160 * @param[in] element_id The ID of the element whose context is to be stored.
161 * @param[in] element_type The type of the element.
162 * @param[in] blob Pointer to the buffer containing the context to be stored.
163 * @param[in] blob_size Size of the buffer containing the context.
164 *
165 * @return
166 * - MESHX_SUCCESS: Successfully stored the context.
167 */
168meshx_err_t meshx_nvs_element_ctx_set(uint16_t element_id, meshx_element_type_t element_type, const void *blob, size_t blob_size);
169
170/**
171 * @brief Remove the context of a specific element from NVS.
172 *
173 * @param[in] element_id The ID of the element whose context is to be removed.
174 * @param[in] element_type The type of the element.
175 *
176 * @return
177 * - MESHX_SUCCESS: Successfully removed the context.
178 */
179meshx_err_t meshx_nvs_element_ctx_remove(uint16_t element_id, meshx_element_type_t element_type);
180
181#ifdef __cplusplus
182} /* extern "C" */
183#endif
184
185#endif /* __MESHX_NVS_H__ */
Common application definitions and includes for BLE Mesh Node.
Header file for the control task in the BLE mesh node application.
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
meshx_err_t meshx_nvs_init(void)
MeshX NVS Initialisation.
Definition meshx_nvs.c:142
meshx_err_t meshx_nvs_close(void)
Close the NVS handle.
Definition meshx_nvs.c:319
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.
Definition meshx_nvs.c:447
struct meshx_nvs meshx_nvs_t
meshx_err_t meshx_nvs_remove(char const *key)
Remove a key-value pair from the NVS.
Definition meshx_nvs.c:348
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
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
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.
Definition meshx_nvs.c:466
meshx_err_t meshx_nvs_commit(void)
Commit changes to the NVS.
Definition meshx_nvs.c:277
meshx_err_t meshx_nvs_erase(void)
Erase all key-value pairs stored in the NVS.
Definition meshx_nvs.c:261
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
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.
Definition meshx_nvs.c:482
Header file for OS timer utilities.
struct meshx_os_timer meshx_os_timer_t
Alias for the meshx_os_timer structure.
Definition meshx_os_timer.h:47
Structure to hold the MeshX NVS data.
Definition meshx_nvs.h:37
uintptr_t meshx_nvs_handle
Definition meshx_nvs.h:41
uint16_t pid
Definition meshx_nvs.h:40
uint16_t init
Definition meshx_nvs.h:38
meshx_os_timer_t * meshx_nvs_commit_tmr
Definition meshx_nvs.h:43
uint16_t cid
Definition meshx_nvs.h:39