MeshX 0.3
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
esp_nvs.c
Go to the documentation of this file.
1
15#include "module_id.h"
16#include "esp_err.h"
17#include "nvs.h"
18
19#define MESHX_NVS_NAMESPACE "MESHX_NVS"
20
34meshx_err_t meshx_nvs_plat_open(uintptr_t *p_nvs_handle)
35{
36 if (p_nvs_handle == NULL)
37 {
38 MESHX_LOGE(MODULE_ID_COMMON, "Invalid argument");
39 }
40
41 esp_err_t err = ESP_OK;
42#ifndef CONFIG_BLE_MESH_SPECIFIC_PARTITION
43 err = nvs_open(
45 NVS_READWRITE,
46 (nvs_handle_t*)p_nvs_handle);
47#else
48 err = nvs_open_from_partition(
49 MESHX_NVS_PARTITION,
51 NVS_READWRITE,
52 (nvs_handle_t*)p_nvs_handle);
53#endif /* CONFIG_BLE_MESH_SPECIFIC_PARTITION */
54
55 if (err != ESP_OK)
56 {
57 MESHX_LOGE(MODULE_ID_COMMON, "Failed to open NVS handle: %d", err);
58 return MESHX_ERR_PLAT;
59 }
60 return MESHX_SUCCESS;
61}
62
72meshx_err_t meshx_nvs_plat_close(uintptr_t p_nvs_handle)
73{
74
75 nvs_close((nvs_handle_t)p_nvs_handle);
76
77 return MESHX_SUCCESS;
78}
79
92meshx_err_t meshx_nvs_plat_read(uintptr_t p_nvs_handle, char const *key , uint8_t *p_data, uint16_t len)
93{
94 esp_err_t err = ESP_OK;
95
96 if (key == NULL || p_data == NULL || len == 0)
97 {
98 MESHX_LOGE(MODULE_ID_COMMON, "Invalid argument");
99 return MESHX_INVALID_ARG;
100 }
101
102 size_t read_len = len;
103 err = nvs_get_blob((nvs_handle_t)p_nvs_handle, key, p_data, &read_len);
104
105 if (err != ESP_OK)
106 {
107 MESHX_LOGE(MODULE_ID_COMMON, "nvs_get_blob failed");
108 return MESHX_ERR_PLAT;
109 }
110
111 return MESHX_SUCCESS;
112}
113
126meshx_err_t meshx_nvs_plat_write(uintptr_t p_nvs_handle, char const * key, uint8_t const *p_data, uint16_t len)
127{
128 if (key == NULL || p_data == NULL || len == 0)
129 {
130 MESHX_LOGE(MODULE_ID_COMMON, "Invalid argument");
131 return MESHX_INVALID_ARG;
132 }
133
134 esp_err_t err = ESP_OK;
135
136 err = nvs_set_blob((nvs_handle_t)p_nvs_handle, key, p_data, len);
137
138 if (err != ESP_OK)
139 {
140 MESHX_LOGE(MODULE_ID_COMMON, "nvs_set_blob failed");
141 return MESHX_ERR_PLAT;
142 }
143
144 return MESHX_SUCCESS;
145}
146
156meshx_err_t meshx_nvs_plat_erase(uintptr_t p_nvs_handle)
157{
158 esp_err_t err = ESP_OK;
159
160 err = nvs_erase_all((nvs_handle_t)p_nvs_handle);
161
162 if (err != ESP_OK)
163 {
164 MESHX_LOGE(MODULE_ID_COMMON, "nvs_erase_all failed");
165 return MESHX_ERR_PLAT;
166 }
167
168 return MESHX_SUCCESS;
169}
170
181meshx_err_t meshx_nvs_plat_remove(uintptr_t p_nvs_handle, char const* key)
182{
183 esp_err_t err = ESP_OK;
184
185 err = nvs_erase_key((nvs_handle_t)p_nvs_handle, key);
186
187 if (err != ESP_OK)
188 {
189 MESHX_LOGE(MODULE_ID_COMMON, "nvs_erase_key failed");
190 return MESHX_ERR_PLAT;
191 }
192
193 return MESHX_SUCCESS;
194}
195
208meshx_err_t meshx_nvs_plat_commit(uintptr_t p_nvs_handle)
209{
210 esp_err_t err = ESP_OK;
211
212 err = nvs_commit((nvs_handle_t)p_nvs_handle);
213
214 if (err != ESP_OK)
215 {
216 MESHX_LOGE(MODULE_ID_COMMON, "nvs_commit failed");
217 return MESHX_ERR_PLAT;
218 }
219
220 return MESHX_SUCCESS;
221}
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.
Definition esp_nvs.c:92
#define MESHX_NVS_NAMESPACE
Definition esp_nvs.c:19
meshx_err_t meshx_nvs_plat_open(uintptr_t *p_nvs_handle)
Open non-volatile storage with a given namespace from the default partition.
Definition esp_nvs.c:34
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.
Definition esp_nvs.c:126
meshx_err_t meshx_nvs_plat_commit(uintptr_t p_nvs_handle)
Commit changes to the non-volatile storage.
Definition esp_nvs.c:208
meshx_err_t meshx_nvs_plat_erase(uintptr_t p_nvs_handle)
Erase all key-value pairs in the given namespace.
Definition esp_nvs.c:156
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.
Definition esp_nvs.c:181
meshx_err_t meshx_nvs_plat_close(uintptr_t p_nvs_handle)
Close the non-volatile storage handle.
Definition esp_nvs.c:72
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:39
@ MESHX_SUCCESS
Definition meshx_err.h:40
@ MESHX_INVALID_ARG
Definition meshx_err.h:42
@ MESHX_ERR_PLAT
Definition meshx_err.h:43
Logging interface for MeshX with color-coded output.
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:73
Defines module IDs for different elements in the BLE mesh node application.
@ MODULE_ID_COMMON
Definition module_id.h:32