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

Implementation for Non-Volatile Storage (NVS) drivers Insterface APIs. More...

#include "interface/utils/meshx_nvs_interface.h"
#include "interface/logging/meshx_log.h"
#include "module_id.h"
#include "esp_err.h"
#include "nvs.h"
Include dependency graph for esp_nvs.c:

Go to the source code of this file.

Macros

#define MESHX_NVS_NAMESPACE   "MESHX_NVS"
 

Functions

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_nvs_plat_close (uintptr_t p_nvs_handle)
 Close the non-volatile storage handle.
 
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_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_erase (uintptr_t p_nvs_handle)
 Erase all key-value pairs in the given namespace.
 
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_err_t meshx_nvs_plat_commit (uintptr_t p_nvs_handle)
 Commit changes to the non-volatile storage.
 

Detailed Description

Implementation for Non-Volatile Storage (NVS) drivers Insterface APIs.

Implementation for ESP Non-Volatile Storage (NVS) drivers operations.

Copyright © 2024 - 2025 MeshX

Author
Pranjal Chanda

Copyright © 2024 - 2025 MeshX

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

Author
Pranjal Chanda

Definition in file esp_nvs.c.

Macro Definition Documentation

◆ MESHX_NVS_NAMESPACE

#define MESHX_NVS_NAMESPACE   "MESHX_NVS"

Definition at line 19 of file esp_nvs.c.

Function Documentation

◆ meshx_nvs_plat_close()

meshx_err_t meshx_nvs_plat_close ( uintptr_t p_nvs_handle)

Close the non-volatile storage handle.

Parameters
[in]p_nvs_handleHandle obtained with meshx_nvs_plat_open.
Returns
  • MESHX_SUCCESS if the handle was closed successfully
  • MESHX_ERR_PLAT for any platform error

Definition at line 72 of file esp_nvs.c.

73{
74
75 nvs_close((nvs_handle_t)p_nvs_handle);
76
77 return MESHX_SUCCESS;
78}
@ MESHX_SUCCESS
Definition meshx_err.h:40
Here is the caller graph for this function:

◆ meshx_nvs_plat_commit()

meshx_err_t meshx_nvs_plat_commit ( uintptr_t p_nvs_handle)

Commit changes to the non-volatile storage.

Note
This function is a prototype and can be returned with MESHX_SUCCESS if no such platform function exists
Parameters
[in]p_nvs_handlePointer to the opened handle.
Returns
  • MESHX_SUCCESS if the changes were committed successfully
  • MESHX_ERR_PLAT for any platform error

Definition at line 208 of file esp_nvs.c.

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_PLAT
Definition meshx_err.h:43
#define MESHX_LOGE(module_id, format,...)
Definition meshx_log.h:73
@ MODULE_ID_COMMON
Definition module_id.h:32
Here is the caller graph for this function:

◆ meshx_nvs_plat_erase()

meshx_err_t meshx_nvs_plat_erase ( uintptr_t p_nvs_handle)

Erase all key-value pairs in the given namespace.

Parameters
[in]p_nvs_handlePointer to the opened handle.
Returns
  • MESHX_SUCCESS if all key-value pairs were erased successfully
  • MESHX_ERR_PLAT for any platform error

Definition at line 156 of file esp_nvs.c.

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}
Here is the caller graph for this function:

◆ meshx_nvs_plat_open()

meshx_err_t meshx_nvs_plat_open ( uintptr_t * p_nvs_handle)

Open non-volatile storage with a given namespace from the default partition.

If CONFIG_BLE_MESH_SPECIFIC_PARTITION is not defined, this function will open the namespace from the default partition. Otherwise, it will open the namespace from the MESHX_NVS_PARTITION.

Parameters
[out]p_nvs_handlePointer to the output variable populated with the opened handle.
Returns
  • MESHX_SUCCESS if the storage handle was opened successfully
  • MESHX_ERR_PLAT if p_nvs_handle is NULL

Definition at line 34 of file esp_nvs.c.

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}
#define MESHX_NVS_NAMESPACE
Definition esp_nvs.c:19
Here is the caller graph for this function:

◆ meshx_nvs_plat_read()

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.

Parameters
[in]p_nvs_handleHandle obtained with meshx_nvs_plat_open.
[in]keyKey name. Maximum length is (NVS_KEY_NAME_MAX_SIZE-1) characters. Shouldn't be empty.
[out]p_dataPointer to the output variable populated with the read blob value.
[in]lenLength of the read blob value.
Returns
  • MESHX_SUCCESS if the blob value was read successfully
  • MESHX_ERR_PLAT if there is an internal error;

Definition at line 92 of file esp_nvs.c.

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}
@ MESHX_INVALID_ARG
Definition meshx_err.h:42
Here is the caller graph for this function:

◆ meshx_nvs_plat_remove()

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.

Parameters
[in]p_nvs_handlePointer to the opened handle.
[in]keyKey name. Maximum length is (NVS_KEY_NAME_MAX_SIZE-1) characters. Shouldn't be empty.
Returns
  • MESHX_SUCCESS if the key-value pair was removed successfully
  • MESHX_ERR_PLAT for any platform error

Definition at line 181 of file esp_nvs.c.

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}
Here is the caller graph for this function:

◆ meshx_nvs_plat_write()

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.

Parameters
[in]p_nvs_handlePointer to the opened handle.
[in]keyKey name. Maximum length is (NVS_KEY_NAME_MAX_SIZE-1) characters. Shouldn't be empty.
[in]p_dataPointer to the data to write.
[in]lenNumber of bytes to write.
Returns
  • MESHX_SUCCESS if the value was written successfully
  • MESHX_ERR_PLAT for any platform error

Definition at line 126 of file esp_nvs.c.

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}
Here is the caller graph for this function: