mirror of
https://github.com/0xFEEDC0DE64/arduino-esp32.git
synced 2025-07-03 22:06:32 +02:00
Initial Esp32c3 Support (#5060)
This commit is contained in:
675
tools/sdk/esp32c3/include/nvs_flash/include/nvs.h
Normal file
675
tools/sdk/esp32c3/include/nvs_flash/include/nvs.h
Normal file
@ -0,0 +1,675 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#ifndef ESP_NVS_H
|
||||
#define ESP_NVS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include "esp_attr.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Opaque pointer type representing non-volatile storage handle
|
||||
*/
|
||||
typedef uint32_t nvs_handle_t;
|
||||
|
||||
/*
|
||||
* Pre-IDF V4.0 uses nvs_handle, so leaving the original typedef here for compatibility.
|
||||
*/
|
||||
typedef nvs_handle_t nvs_handle IDF_DEPRECATED("Replace with nvs_handle_t");
|
||||
|
||||
#define ESP_ERR_NVS_BASE 0x1100 /*!< Starting number of error codes */
|
||||
#define ESP_ERR_NVS_NOT_INITIALIZED (ESP_ERR_NVS_BASE + 0x01) /*!< The storage driver is not initialized */
|
||||
#define ESP_ERR_NVS_NOT_FOUND (ESP_ERR_NVS_BASE + 0x02) /*!< Id namespace doesn’t exist yet and mode is NVS_READONLY */
|
||||
#define ESP_ERR_NVS_TYPE_MISMATCH (ESP_ERR_NVS_BASE + 0x03) /*!< The type of set or get operation doesn't match the type of value stored in NVS */
|
||||
#define ESP_ERR_NVS_READ_ONLY (ESP_ERR_NVS_BASE + 0x04) /*!< Storage handle was opened as read only */
|
||||
#define ESP_ERR_NVS_NOT_ENOUGH_SPACE (ESP_ERR_NVS_BASE + 0x05) /*!< There is not enough space in the underlying storage to save the value */
|
||||
#define ESP_ERR_NVS_INVALID_NAME (ESP_ERR_NVS_BASE + 0x06) /*!< Namespace name doesn’t satisfy constraints */
|
||||
#define ESP_ERR_NVS_INVALID_HANDLE (ESP_ERR_NVS_BASE + 0x07) /*!< Handle has been closed or is NULL */
|
||||
#define ESP_ERR_NVS_REMOVE_FAILED (ESP_ERR_NVS_BASE + 0x08) /*!< The value wasn’t updated because flash write operation has failed. The value was written however, and update will be finished after re-initialization of nvs, provided that flash operation doesn’t fail again. */
|
||||
#define ESP_ERR_NVS_KEY_TOO_LONG (ESP_ERR_NVS_BASE + 0x09) /*!< Key name is too long */
|
||||
#define ESP_ERR_NVS_PAGE_FULL (ESP_ERR_NVS_BASE + 0x0a) /*!< Internal error; never returned by nvs API functions */
|
||||
#define ESP_ERR_NVS_INVALID_STATE (ESP_ERR_NVS_BASE + 0x0b) /*!< NVS is in an inconsistent state due to a previous error. Call nvs_flash_init and nvs_open again, then retry. */
|
||||
#define ESP_ERR_NVS_INVALID_LENGTH (ESP_ERR_NVS_BASE + 0x0c) /*!< String or blob length is not sufficient to store data */
|
||||
#define ESP_ERR_NVS_NO_FREE_PAGES (ESP_ERR_NVS_BASE + 0x0d) /*!< NVS partition doesn't contain any empty pages. This may happen if NVS partition was truncated. Erase the whole partition and call nvs_flash_init again. */
|
||||
#define ESP_ERR_NVS_VALUE_TOO_LONG (ESP_ERR_NVS_BASE + 0x0e) /*!< String or blob length is longer than supported by the implementation */
|
||||
#define ESP_ERR_NVS_PART_NOT_FOUND (ESP_ERR_NVS_BASE + 0x0f) /*!< Partition with specified name is not found in the partition table */
|
||||
|
||||
#define ESP_ERR_NVS_NEW_VERSION_FOUND (ESP_ERR_NVS_BASE + 0x10) /*!< NVS partition contains data in new format and cannot be recognized by this version of code */
|
||||
#define ESP_ERR_NVS_XTS_ENCR_FAILED (ESP_ERR_NVS_BASE + 0x11) /*!< XTS encryption failed while writing NVS entry */
|
||||
#define ESP_ERR_NVS_XTS_DECR_FAILED (ESP_ERR_NVS_BASE + 0x12) /*!< XTS decryption failed while reading NVS entry */
|
||||
#define ESP_ERR_NVS_XTS_CFG_FAILED (ESP_ERR_NVS_BASE + 0x13) /*!< XTS configuration setting failed */
|
||||
#define ESP_ERR_NVS_XTS_CFG_NOT_FOUND (ESP_ERR_NVS_BASE + 0x14) /*!< XTS configuration not found */
|
||||
#define ESP_ERR_NVS_ENCR_NOT_SUPPORTED (ESP_ERR_NVS_BASE + 0x15) /*!< NVS encryption is not supported in this version */
|
||||
#define ESP_ERR_NVS_KEYS_NOT_INITIALIZED (ESP_ERR_NVS_BASE + 0x16) /*!< NVS key partition is uninitialized */
|
||||
#define ESP_ERR_NVS_CORRUPT_KEY_PART (ESP_ERR_NVS_BASE + 0x17) /*!< NVS key partition is corrupt */
|
||||
#define ESP_ERR_NVS_WRONG_ENCRYPTION (ESP_ERR_NVS_BASE + 0x19) /*!< NVS partition is marked as encrypted with generic flash encryption. This is forbidden since the NVS encryption works differently. */
|
||||
|
||||
#define ESP_ERR_NVS_CONTENT_DIFFERS (ESP_ERR_NVS_BASE + 0x18) /*!< Internal error; never returned by nvs API functions. NVS key is different in comparison */
|
||||
|
||||
#define NVS_DEFAULT_PART_NAME "nvs" /*!< Default partition name of the NVS partition in the partition table */
|
||||
|
||||
#define NVS_PART_NAME_MAX_SIZE 16 /*!< maximum length of partition name (excluding null terminator) */
|
||||
#define NVS_KEY_NAME_MAX_SIZE 16 /*!< Maximal length of NVS key name (including null terminator) */
|
||||
|
||||
/**
|
||||
* @brief Mode of opening the non-volatile storage
|
||||
*/
|
||||
typedef enum {
|
||||
NVS_READONLY, /*!< Read only */
|
||||
NVS_READWRITE /*!< Read and write */
|
||||
} nvs_open_mode_t;
|
||||
|
||||
/*
|
||||
* Pre-IDF V4.0 uses nvs_open_mode, so leaving the original typedef here for compatibility.
|
||||
*/
|
||||
typedef nvs_open_mode_t nvs_open_mode IDF_DEPRECATED("Replace with nvs_open_mode_t");
|
||||
|
||||
|
||||
/**
|
||||
* @brief Types of variables
|
||||
*
|
||||
*/
|
||||
typedef enum {
|
||||
NVS_TYPE_U8 = 0x01, /*!< Type uint8_t */
|
||||
NVS_TYPE_I8 = 0x11, /*!< Type int8_t */
|
||||
NVS_TYPE_U16 = 0x02, /*!< Type uint16_t */
|
||||
NVS_TYPE_I16 = 0x12, /*!< Type int16_t */
|
||||
NVS_TYPE_U32 = 0x04, /*!< Type uint32_t */
|
||||
NVS_TYPE_I32 = 0x14, /*!< Type int32_t */
|
||||
NVS_TYPE_U64 = 0x08, /*!< Type uint64_t */
|
||||
NVS_TYPE_I64 = 0x18, /*!< Type int64_t */
|
||||
NVS_TYPE_STR = 0x21, /*!< Type string */
|
||||
NVS_TYPE_BLOB = 0x42, /*!< Type blob */
|
||||
NVS_TYPE_ANY = 0xff /*!< Must be last */
|
||||
} nvs_type_t;
|
||||
|
||||
/**
|
||||
* @brief information about entry obtained from nvs_entry_info function
|
||||
*/
|
||||
typedef struct {
|
||||
char namespace_name[16]; /*!< Namespace to which key-value belong */
|
||||
char key[16]; /*!< Key of stored key-value pair */
|
||||
nvs_type_t type; /*!< Type of stored key-value pair */
|
||||
} nvs_entry_info_t;
|
||||
|
||||
/**
|
||||
* Opaque pointer type representing iterator to nvs entries
|
||||
*/
|
||||
typedef struct nvs_opaque_iterator_t *nvs_iterator_t;
|
||||
|
||||
/**
|
||||
* @brief Open non-volatile storage with a given namespace from the default NVS partition
|
||||
*
|
||||
* Multiple internal ESP-IDF and third party application modules can store
|
||||
* their key-value pairs in the NVS module. In order to reduce possible
|
||||
* conflicts on key names, each module can use its own namespace.
|
||||
* The default NVS partition is the one that is labelled "nvs" in the partition
|
||||
* table.
|
||||
*
|
||||
* @param[in] name Namespace name. Maximal length is (NVS_KEY_NAME_MAX_SIZE-1) characters. Shouldn't be empty.
|
||||
* @param[in] open_mode NVS_READWRITE or NVS_READONLY. If NVS_READONLY, will
|
||||
* open a handle for reading only. All write requests will
|
||||
* be rejected for this handle.
|
||||
* @param[out] out_handle If successful (return code is zero), handle will be
|
||||
* returned in this argument.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if storage handle was opened successfully
|
||||
* - ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized
|
||||
* - ESP_ERR_NVS_PART_NOT_FOUND if the partition with label "nvs" is not found
|
||||
* - ESP_ERR_NVS_NOT_FOUND id namespace doesn't exist yet and
|
||||
* mode is NVS_READONLY
|
||||
* - ESP_ERR_NVS_INVALID_NAME if namespace name doesn't satisfy constraints
|
||||
* - ESP_ERR_NO_MEM in case memory could not be allocated for the internal structures
|
||||
* - other error codes from the underlying storage driver
|
||||
*/
|
||||
esp_err_t nvs_open(const char* name, nvs_open_mode_t open_mode, nvs_handle_t *out_handle);
|
||||
|
||||
/**
|
||||
* @brief Open non-volatile storage with a given namespace from specified partition
|
||||
*
|
||||
* The behaviour is same as nvs_open() API. However this API can operate on a specified NVS
|
||||
* partition instead of default NVS partition. Note that the specified partition must be registered
|
||||
* with NVS using nvs_flash_init_partition() API.
|
||||
*
|
||||
* @param[in] part_name Label (name) of the partition of interest for object read/write/erase
|
||||
* @param[in] name Namespace name. Maximal length is (NVS_KEY_NAME_MAX_SIZE-1) characters. Shouldn't be empty.
|
||||
* @param[in] open_mode NVS_READWRITE or NVS_READONLY. If NVS_READONLY, will
|
||||
* open a handle for reading only. All write requests will
|
||||
* be rejected for this handle.
|
||||
* @param[out] out_handle If successful (return code is zero), handle will be
|
||||
* returned in this argument.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if storage handle was opened successfully
|
||||
* - ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized
|
||||
* - ESP_ERR_NVS_PART_NOT_FOUND if the partition with specified name is not found
|
||||
* - ESP_ERR_NVS_NOT_FOUND id namespace doesn't exist yet and
|
||||
* mode is NVS_READONLY
|
||||
* - ESP_ERR_NVS_INVALID_NAME if namespace name doesn't satisfy constraints
|
||||
* - ESP_ERR_NO_MEM in case memory could not be allocated for the internal structures
|
||||
* - other error codes from the underlying storage driver
|
||||
*/
|
||||
esp_err_t nvs_open_from_partition(const char *part_name, const char* name, nvs_open_mode_t open_mode, nvs_handle_t *out_handle);
|
||||
|
||||
/**@{*/
|
||||
/**
|
||||
* @brief set int8_t value for given key
|
||||
*
|
||||
* Set value for the key, given its name. Note that the actual storage will not be updated
|
||||
* until \c nvs_commit is called.
|
||||
*
|
||||
* @param[in] handle Handle obtained from nvs_open function.
|
||||
* Handles that were opened read only cannot be used.
|
||||
* @param[in] key Key name. Maximal length is (NVS_KEY_NAME_MAX_SIZE-1) characters. Shouldn't be empty.
|
||||
* @param[in] value The value to set.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if value was set successfully
|
||||
* - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
|
||||
* - ESP_ERR_NVS_READ_ONLY if storage handle was opened as read only
|
||||
* - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
|
||||
* - ESP_ERR_NVS_NOT_ENOUGH_SPACE if there is not enough space in the
|
||||
* underlying storage to save the value
|
||||
* - ESP_ERR_NVS_REMOVE_FAILED if the value wasn't updated because flash
|
||||
* write operation has failed. The value was written however, and
|
||||
* update will be finished after re-initialization of nvs, provided that
|
||||
* flash operation doesn't fail again.
|
||||
*/
|
||||
esp_err_t nvs_set_i8 (nvs_handle_t handle, const char* key, int8_t value);
|
||||
|
||||
/**
|
||||
* @brief set uint8_t value for given key
|
||||
*
|
||||
* This function is the same as \c nvs_set_i8 except for the data type.
|
||||
*/
|
||||
esp_err_t nvs_set_u8 (nvs_handle_t handle, const char* key, uint8_t value);
|
||||
|
||||
/**
|
||||
* @brief set int16_t value for given key
|
||||
*
|
||||
* This function is the same as \c nvs_set_i8 except for the data type.
|
||||
*/
|
||||
esp_err_t nvs_set_i16 (nvs_handle_t handle, const char* key, int16_t value);
|
||||
|
||||
/**
|
||||
* @brief set uint16_t value for given key
|
||||
*
|
||||
* This function is the same as \c nvs_set_i8 except for the data type.
|
||||
*/
|
||||
esp_err_t nvs_set_u16 (nvs_handle_t handle, const char* key, uint16_t value);
|
||||
|
||||
/**
|
||||
* @brief set int32_t value for given key
|
||||
*
|
||||
* This function is the same as \c nvs_set_i8 except for the data type.
|
||||
*/
|
||||
esp_err_t nvs_set_i32 (nvs_handle_t handle, const char* key, int32_t value);
|
||||
|
||||
/**
|
||||
* @brief set uint32_t value for given key
|
||||
*
|
||||
* This function is the same as \c nvs_set_i8 except for the data type.
|
||||
*/
|
||||
esp_err_t nvs_set_u32 (nvs_handle_t handle, const char* key, uint32_t value);
|
||||
|
||||
/**
|
||||
* @brief set int64_t value for given key
|
||||
*
|
||||
* This function is the same as \c nvs_set_i8 except for the data type.
|
||||
*/
|
||||
esp_err_t nvs_set_i64 (nvs_handle_t handle, const char* key, int64_t value);
|
||||
|
||||
/**
|
||||
* @brief set uint64_t value for given key
|
||||
*
|
||||
* This function is the same as \c nvs_set_i8 except for the data type.
|
||||
*/
|
||||
esp_err_t nvs_set_u64 (nvs_handle_t handle, const char* key, uint64_t value);
|
||||
|
||||
/**
|
||||
* @brief set string for given key
|
||||
*
|
||||
* Set value for the key, given its name. Note that the actual storage will not be updated
|
||||
* until \c nvs_commit is called.
|
||||
*
|
||||
* @param[in] handle Handle obtained from nvs_open function.
|
||||
* Handles that were opened read only cannot be used.
|
||||
* @param[in] key Key name. Maximal length is (NVS_KEY_NAME_MAX_SIZE-1) characters. Shouldn't be empty.
|
||||
* @param[in] value The value to set.
|
||||
* For strings, the maximum length (including null character) is
|
||||
* 4000 bytes, if there is one complete page free for writing.
|
||||
* This decreases, however, if the free space is fragmented.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if value was set successfully
|
||||
* - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
|
||||
* - ESP_ERR_NVS_READ_ONLY if storage handle was opened as read only
|
||||
* - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
|
||||
* - ESP_ERR_NVS_NOT_ENOUGH_SPACE if there is not enough space in the
|
||||
* underlying storage to save the value
|
||||
* - ESP_ERR_NVS_REMOVE_FAILED if the value wasn't updated because flash
|
||||
* write operation has failed. The value was written however, and
|
||||
* update will be finished after re-initialization of nvs, provided that
|
||||
* flash operation doesn't fail again.
|
||||
* - ESP_ERR_NVS_VALUE_TOO_LONG if the string value is too long
|
||||
*/
|
||||
esp_err_t nvs_set_str (nvs_handle_t handle, const char* key, const char* value);
|
||||
/**@}*/
|
||||
|
||||
/**
|
||||
* @brief set variable length binary value for given key
|
||||
*
|
||||
* This family of functions set value for the key, given its name. Note that
|
||||
* actual storage will not be updated until nvs_commit function is called.
|
||||
*
|
||||
* @param[in] handle Handle obtained from nvs_open function.
|
||||
* Handles that were opened read only cannot be used.
|
||||
* @param[in] key Key name. Maximal length is (NVS_KEY_NAME_MAX_SIZE-1) characters. Shouldn't be empty.
|
||||
* @param[in] value The value to set.
|
||||
* @param[in] length length of binary value to set, in bytes; Maximum length is
|
||||
* 508000 bytes or (97.6% of the partition size - 4000) bytes
|
||||
* whichever is lower.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if value was set successfully
|
||||
* - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
|
||||
* - ESP_ERR_NVS_READ_ONLY if storage handle was opened as read only
|
||||
* - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
|
||||
* - ESP_ERR_NVS_NOT_ENOUGH_SPACE if there is not enough space in the
|
||||
* underlying storage to save the value
|
||||
* - ESP_ERR_NVS_REMOVE_FAILED if the value wasn't updated because flash
|
||||
* write operation has failed. The value was written however, and
|
||||
* update will be finished after re-initialization of nvs, provided that
|
||||
* flash operation doesn't fail again.
|
||||
* - ESP_ERR_NVS_VALUE_TOO_LONG if the value is too long
|
||||
*/
|
||||
esp_err_t nvs_set_blob(nvs_handle_t handle, const char* key, const void* value, size_t length);
|
||||
|
||||
/**@{*/
|
||||
/**
|
||||
* @brief get int8_t value for given key
|
||||
*
|
||||
* These functions retrieve value for the key, given its name. If \c key does not
|
||||
* exist, or the requested variable type doesn't match the type which was used
|
||||
* when setting a value, an error is returned.
|
||||
*
|
||||
* In case of any error, out_value is not modified.
|
||||
*
|
||||
* \c out_value has to be a pointer to an already allocated variable of the given type.
|
||||
*
|
||||
* \code{c}
|
||||
* // Example of using nvs_get_i32:
|
||||
* int32_t max_buffer_size = 4096; // default value
|
||||
* esp_err_t err = nvs_get_i32(my_handle, "max_buffer_size", &max_buffer_size);
|
||||
* assert(err == ESP_OK || err == ESP_ERR_NVS_NOT_FOUND);
|
||||
* // if ESP_ERR_NVS_NOT_FOUND was returned, max_buffer_size will still
|
||||
* // have its default value.
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* @param[in] handle Handle obtained from nvs_open function.
|
||||
* @param[in] key Key name. Maximal length is (NVS_KEY_NAME_MAX_SIZE-1) characters. Shouldn't be empty.
|
||||
* @param out_value Pointer to the output value.
|
||||
* May be NULL for nvs_get_str and nvs_get_blob, in this
|
||||
* case required length will be returned in length argument.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if the value was retrieved successfully
|
||||
* - ESP_ERR_NVS_NOT_FOUND if the requested key doesn't exist
|
||||
* - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
|
||||
* - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
|
||||
* - ESP_ERR_NVS_INVALID_LENGTH if length is not sufficient to store data
|
||||
*/
|
||||
esp_err_t nvs_get_i8 (nvs_handle_t handle, const char* key, int8_t* out_value);
|
||||
|
||||
/**
|
||||
* @brief get uint8_t value for given key
|
||||
*
|
||||
* This function is the same as \c nvs_get_i8 except for the data type.
|
||||
*/
|
||||
esp_err_t nvs_get_u8 (nvs_handle_t handle, const char* key, uint8_t* out_value);
|
||||
|
||||
/**
|
||||
* @brief get int16_t value for given key
|
||||
*
|
||||
* This function is the same as \c nvs_get_i8 except for the data type.
|
||||
*/
|
||||
esp_err_t nvs_get_i16 (nvs_handle_t handle, const char* key, int16_t* out_value);
|
||||
|
||||
/**
|
||||
* @brief get uint16_t value for given key
|
||||
*
|
||||
* This function is the same as \c nvs_get_i8 except for the data type.
|
||||
*/
|
||||
esp_err_t nvs_get_u16 (nvs_handle_t handle, const char* key, uint16_t* out_value);
|
||||
|
||||
/**
|
||||
* @brief get int32_t value for given key
|
||||
*
|
||||
* This function is the same as \c nvs_get_i8 except for the data type.
|
||||
*/
|
||||
esp_err_t nvs_get_i32 (nvs_handle_t handle, const char* key, int32_t* out_value);
|
||||
|
||||
/**
|
||||
* @brief get uint32_t value for given key
|
||||
*
|
||||
* This function is the same as \c nvs_get_i8 except for the data type.
|
||||
*/
|
||||
esp_err_t nvs_get_u32 (nvs_handle_t handle, const char* key, uint32_t* out_value);
|
||||
|
||||
/**
|
||||
* @brief get int64_t value for given key
|
||||
*
|
||||
* This function is the same as \c nvs_get_i8 except for the data type.
|
||||
*/
|
||||
esp_err_t nvs_get_i64 (nvs_handle_t handle, const char* key, int64_t* out_value);
|
||||
|
||||
/**
|
||||
* @brief get uint64_t value for given key
|
||||
*
|
||||
* This function is the same as \c nvs_get_i8 except for the data type.
|
||||
*/
|
||||
esp_err_t nvs_get_u64 (nvs_handle_t handle, const char* key, uint64_t* out_value);
|
||||
/**@}*/
|
||||
|
||||
/**@{*/
|
||||
/**
|
||||
* @brief get string value for given key
|
||||
*
|
||||
* These functions retrieve the data of an entry, given its key. If key does not
|
||||
* exist, or the requested variable type doesn't match the type which was used
|
||||
* when setting a value, an error is returned.
|
||||
*
|
||||
* In case of any error, out_value is not modified.
|
||||
*
|
||||
* All functions expect out_value to be a pointer to an already allocated variable
|
||||
* of the given type.
|
||||
*
|
||||
* nvs_get_str and nvs_get_blob functions support WinAPI-style length queries.
|
||||
* To get the size necessary to store the value, call nvs_get_str or nvs_get_blob
|
||||
* with zero out_value and non-zero pointer to length. Variable pointed to
|
||||
* by length argument will be set to the required length. For nvs_get_str,
|
||||
* this length includes the zero terminator. When calling nvs_get_str and
|
||||
* nvs_get_blob with non-zero out_value, length has to be non-zero and has to
|
||||
* point to the length available in out_value.
|
||||
* It is suggested that nvs_get/set_str is used for zero-terminated C strings, and
|
||||
* nvs_get/set_blob used for arbitrary data structures.
|
||||
*
|
||||
* \code{c}
|
||||
* // Example (without error checking) of using nvs_get_str to get a string into dynamic array:
|
||||
* size_t required_size;
|
||||
* nvs_get_str(my_handle, "server_name", NULL, &required_size);
|
||||
* char* server_name = malloc(required_size);
|
||||
* nvs_get_str(my_handle, "server_name", server_name, &required_size);
|
||||
*
|
||||
* // Example (without error checking) of using nvs_get_blob to get a binary data
|
||||
* into a static array:
|
||||
* uint8_t mac_addr[6];
|
||||
* size_t size = sizeof(mac_addr);
|
||||
* nvs_get_blob(my_handle, "dst_mac_addr", mac_addr, &size);
|
||||
* \endcode
|
||||
*
|
||||
* @param[in] handle Handle obtained from nvs_open function.
|
||||
* @param[in] key Key name. Maximal length is (NVS_KEY_NAME_MAX_SIZE-1) characters. Shouldn't be empty.
|
||||
* @param[out] out_value Pointer to the output value.
|
||||
* May be NULL for nvs_get_str and nvs_get_blob, in this
|
||||
* case required length will be returned in length argument.
|
||||
* @param[inout] length A non-zero pointer to the variable holding the length of out_value.
|
||||
* In case out_value a zero, will be set to the length
|
||||
* required to hold the value. In case out_value is not
|
||||
* zero, will be set to the actual length of the value
|
||||
* written. For nvs_get_str this includes zero terminator.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if the value was retrieved successfully
|
||||
* - ESP_ERR_NVS_NOT_FOUND if the requested key doesn't exist
|
||||
* - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
|
||||
* - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
|
||||
* - ESP_ERR_NVS_INVALID_LENGTH if \c length is not sufficient to store data
|
||||
*/
|
||||
esp_err_t nvs_get_str (nvs_handle_t handle, const char* key, char* out_value, size_t* length);
|
||||
|
||||
/**
|
||||
* @brief get blob value for given key
|
||||
*
|
||||
* This function behaves the same as \c nvs_get_str, except for the data type.
|
||||
*/
|
||||
esp_err_t nvs_get_blob(nvs_handle_t handle, const char* key, void* out_value, size_t* length);
|
||||
/**@}*/
|
||||
|
||||
/**
|
||||
* @brief Erase key-value pair with given key name.
|
||||
*
|
||||
* Note that actual storage may not be updated until nvs_commit function is called.
|
||||
*
|
||||
* @param[in] handle Storage handle obtained with nvs_open.
|
||||
* Handles that were opened read only cannot be used.
|
||||
*
|
||||
* @param[in] key Key name. Maximal length is (NVS_KEY_NAME_MAX_SIZE-1) characters. Shouldn't be empty.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if erase operation was successful
|
||||
* - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
|
||||
* - ESP_ERR_NVS_READ_ONLY if handle was opened as read only
|
||||
* - ESP_ERR_NVS_NOT_FOUND if the requested key doesn't exist
|
||||
* - other error codes from the underlying storage driver
|
||||
*/
|
||||
esp_err_t nvs_erase_key(nvs_handle_t handle, const char* key);
|
||||
|
||||
/**
|
||||
* @brief Erase all key-value pairs in a namespace
|
||||
*
|
||||
* Note that actual storage may not be updated until nvs_commit function is called.
|
||||
*
|
||||
* @param[in] handle Storage handle obtained with nvs_open.
|
||||
* Handles that were opened read only cannot be used.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if erase operation was successful
|
||||
* - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
|
||||
* - ESP_ERR_NVS_READ_ONLY if handle was opened as read only
|
||||
* - other error codes from the underlying storage driver
|
||||
*/
|
||||
esp_err_t nvs_erase_all(nvs_handle_t handle);
|
||||
|
||||
/**
|
||||
* @brief Write any pending changes to non-volatile storage
|
||||
*
|
||||
* After setting any values, nvs_commit() must be called to ensure changes are written
|
||||
* to non-volatile storage. Individual implementations may write to storage at other times,
|
||||
* but this is not guaranteed.
|
||||
*
|
||||
* @param[in] handle Storage handle obtained with nvs_open.
|
||||
* Handles that were opened read only cannot be used.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if the changes have been written successfully
|
||||
* - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL
|
||||
* - other error codes from the underlying storage driver
|
||||
*/
|
||||
esp_err_t nvs_commit(nvs_handle_t handle);
|
||||
|
||||
/**
|
||||
* @brief Close the storage handle and free any allocated resources
|
||||
*
|
||||
* This function should be called for each handle opened with nvs_open once
|
||||
* the handle is not in use any more. Closing the handle may not automatically
|
||||
* write the changes to nonvolatile storage. This has to be done explicitly using
|
||||
* nvs_commit function.
|
||||
* Once this function is called on a handle, the handle should no longer be used.
|
||||
*
|
||||
* @param[in] handle Storage handle to close
|
||||
*/
|
||||
void nvs_close(nvs_handle_t handle);
|
||||
|
||||
/**
|
||||
* @note Info about storage space NVS.
|
||||
*/
|
||||
typedef struct {
|
||||
size_t used_entries; /**< Amount of used entries. */
|
||||
size_t free_entries; /**< Amount of free entries. */
|
||||
size_t total_entries; /**< Amount all available entries. */
|
||||
size_t namespace_count; /**< Amount name space. */
|
||||
} nvs_stats_t;
|
||||
|
||||
/**
|
||||
* @brief Fill structure nvs_stats_t. It provides info about used memory the partition.
|
||||
*
|
||||
* This function calculates to runtime the number of used entries, free entries, total entries,
|
||||
* and amount namespace in partition.
|
||||
*
|
||||
* \code{c}
|
||||
* // Example of nvs_get_stats() to get the number of used entries and free entries:
|
||||
* nvs_stats_t nvs_stats;
|
||||
* nvs_get_stats(NULL, &nvs_stats);
|
||||
* printf("Count: UsedEntries = (%d), FreeEntries = (%d), AllEntries = (%d)\n",
|
||||
nvs_stats.used_entries, nvs_stats.free_entries, nvs_stats.total_entries);
|
||||
* \endcode
|
||||
*
|
||||
* @param[in] part_name Partition name NVS in the partition table.
|
||||
* If pass a NULL than will use NVS_DEFAULT_PART_NAME ("nvs").
|
||||
*
|
||||
* @param[out] nvs_stats Returns filled structure nvs_states_t.
|
||||
* It provides info about used memory the partition.
|
||||
*
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if the changes have been written successfully.
|
||||
* Return param nvs_stats will be filled.
|
||||
* - ESP_ERR_NVS_PART_NOT_FOUND if the partition with label "name" is not found.
|
||||
* Return param nvs_stats will be filled 0.
|
||||
* - ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized.
|
||||
* Return param nvs_stats will be filled 0.
|
||||
* - ESP_ERR_INVALID_ARG if nvs_stats equal to NULL.
|
||||
* - ESP_ERR_INVALID_STATE if there is page with the status of INVALID.
|
||||
* Return param nvs_stats will be filled not with correct values because
|
||||
* not all pages will be counted. Counting will be interrupted at the first INVALID page.
|
||||
*/
|
||||
esp_err_t nvs_get_stats(const char *part_name, nvs_stats_t *nvs_stats);
|
||||
|
||||
/**
|
||||
* @brief Calculate all entries in a namespace.
|
||||
*
|
||||
* An entry represents the smallest storage unit in NVS.
|
||||
* Strings and blobs may occupy more than one entry.
|
||||
* Note that to find out the total number of entries occupied by the namespace,
|
||||
* add one to the returned value used_entries (if err is equal to ESP_OK).
|
||||
* Because the name space entry takes one entry.
|
||||
*
|
||||
* \code{c}
|
||||
* // Example of nvs_get_used_entry_count() to get amount of all key-value pairs in one namespace:
|
||||
* nvs_handle_t handle;
|
||||
* nvs_open("namespace1", NVS_READWRITE, &handle);
|
||||
* ...
|
||||
* size_t used_entries;
|
||||
* size_t total_entries_namespace;
|
||||
* if(nvs_get_used_entry_count(handle, &used_entries) == ESP_OK){
|
||||
* // the total number of entries occupied by the namespace
|
||||
* total_entries_namespace = used_entries + 1;
|
||||
* }
|
||||
* \endcode
|
||||
*
|
||||
* @param[in] handle Handle obtained from nvs_open function.
|
||||
*
|
||||
* @param[out] used_entries Returns amount of used entries from a namespace.
|
||||
*
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if the changes have been written successfully.
|
||||
* Return param used_entries will be filled valid value.
|
||||
* - ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized.
|
||||
* Return param used_entries will be filled 0.
|
||||
* - ESP_ERR_NVS_INVALID_HANDLE if handle has been closed or is NULL.
|
||||
* Return param used_entries will be filled 0.
|
||||
* - ESP_ERR_INVALID_ARG if used_entries equal to NULL.
|
||||
* - Other error codes from the underlying storage driver.
|
||||
* Return param used_entries will be filled 0.
|
||||
*/
|
||||
esp_err_t nvs_get_used_entry_count(nvs_handle_t handle, size_t* used_entries);
|
||||
|
||||
/**
|
||||
* @brief Create an iterator to enumerate NVS entries based on one or more parameters
|
||||
*
|
||||
* \code{c}
|
||||
* // Example of listing all the key-value pairs of any type under specified partition and namespace
|
||||
* nvs_iterator_t it = nvs_entry_find(partition, namespace, NVS_TYPE_ANY);
|
||||
* while (it != NULL) {
|
||||
* nvs_entry_info_t info;
|
||||
* nvs_entry_info(it, &info);
|
||||
* it = nvs_entry_next(it);
|
||||
* printf("key '%s', type '%d' \n", info.key, info.type);
|
||||
* };
|
||||
* // Note: no need to release iterator obtained from nvs_entry_find function when
|
||||
* // nvs_entry_find or nvs_entry_next function return NULL, indicating no other
|
||||
* // element for specified criteria was found.
|
||||
* }
|
||||
* \endcode
|
||||
*
|
||||
* @param[in] part_name Partition name
|
||||
*
|
||||
* @param[in] namespace_name Set this value if looking for entries with
|
||||
* a specific namespace. Pass NULL otherwise.
|
||||
*
|
||||
* @param[in] type One of nvs_type_t values.
|
||||
*
|
||||
* @return
|
||||
* Iterator used to enumerate all the entries found,
|
||||
* or NULL if no entry satisfying criteria was found.
|
||||
* Iterator obtained through this function has to be released
|
||||
* using nvs_release_iterator when not used any more.
|
||||
*/
|
||||
nvs_iterator_t nvs_entry_find(const char *part_name, const char *namespace_name, nvs_type_t type);
|
||||
|
||||
/**
|
||||
* @brief Returns next item matching the iterator criteria, NULL if no such item exists.
|
||||
*
|
||||
* Note that any copies of the iterator will be invalid after this call.
|
||||
*
|
||||
* @param[in] iterator Iterator obtained from nvs_entry_find function. Must be non-NULL.
|
||||
*
|
||||
* @return
|
||||
* NULL if no entry was found, valid nvs_iterator_t otherwise.
|
||||
*/
|
||||
nvs_iterator_t nvs_entry_next(nvs_iterator_t iterator);
|
||||
|
||||
/**
|
||||
* @brief Fills nvs_entry_info_t structure with information about entry pointed to by the iterator.
|
||||
*
|
||||
* @param[in] iterator Iterator obtained from nvs_entry_find or nvs_entry_next function. Must be non-NULL.
|
||||
*
|
||||
* @param[out] out_info Structure to which entry information is copied.
|
||||
*/
|
||||
void nvs_entry_info(nvs_iterator_t iterator, nvs_entry_info_t *out_info);
|
||||
|
||||
/**
|
||||
* @brief Release iterator
|
||||
*
|
||||
* @param[in] iterator Release iterator obtained from nvs_entry_find function. NULL argument is allowed.
|
||||
*
|
||||
*/
|
||||
void nvs_release_iterator(nvs_iterator_t iterator);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
#endif //ESP_NVS_H
|
257
tools/sdk/esp32c3/include/nvs_flash/include/nvs_flash.h
Normal file
257
tools/sdk/esp32c3/include/nvs_flash/include/nvs_flash.h
Normal file
@ -0,0 +1,257 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#ifndef nvs_flash_h
|
||||
#define nvs_flash_h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "nvs.h"
|
||||
#include "esp_partition.h"
|
||||
|
||||
|
||||
#define NVS_KEY_SIZE 32 // AES-256
|
||||
|
||||
/**
|
||||
* @brief Key for encryption and decryption
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t eky[NVS_KEY_SIZE]; /*!< XTS encryption and decryption key*/
|
||||
uint8_t tky[NVS_KEY_SIZE]; /*!< XTS tweak key */
|
||||
} nvs_sec_cfg_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize the default NVS partition.
|
||||
*
|
||||
* This API initialises the default NVS partition. The default NVS partition
|
||||
* is the one that is labeled "nvs" in the partition table.
|
||||
*
|
||||
* When "NVS_ENCRYPTION" is enabled in the menuconfig, this API enables
|
||||
* the NVS encryption for the default NVS partition as follows
|
||||
* 1. Read security configurations from the first NVS key
|
||||
* partition listed in the partition table. (NVS key partition is
|
||||
* any "data" type partition which has the subtype value set to "nvs_keys")
|
||||
* 2. If the NVS key partiton obtained in the previous step is empty,
|
||||
* generate and store new keys in that NVS key partiton.
|
||||
* 3. Internally call "nvs_flash_secure_init()" with
|
||||
* the security configurations obtained/generated in the previous steps.
|
||||
*
|
||||
* Post initialization NVS read/write APIs
|
||||
* remain the same irrespective of NVS encryption.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if storage was successfully initialized.
|
||||
* - ESP_ERR_NVS_NO_FREE_PAGES if the NVS storage contains no empty pages
|
||||
* (which may happen if NVS partition was truncated)
|
||||
* - ESP_ERR_NOT_FOUND if no partition with label "nvs" is found in the partition table
|
||||
* - ESP_ERR_NO_MEM in case memory could not be allocated for the internal structures
|
||||
* - one of the error codes from the underlying flash storage driver
|
||||
* - error codes from nvs_flash_read_security_cfg API (when "NVS_ENCRYPTION" is enabled).
|
||||
* - error codes from nvs_flash_generate_keys API (when "NVS_ENCRYPTION" is enabled).
|
||||
* - error codes from nvs_flash_secure_init_partition API (when "NVS_ENCRYPTION" is enabled) .
|
||||
*/
|
||||
esp_err_t nvs_flash_init(void);
|
||||
|
||||
/**
|
||||
* @brief Initialize NVS flash storage for the specified partition.
|
||||
*
|
||||
* @param[in] partition_label Label of the partition. Must be no longer than 16 characters.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if storage was successfully initialized.
|
||||
* - ESP_ERR_NVS_NO_FREE_PAGES if the NVS storage contains no empty pages
|
||||
* (which may happen if NVS partition was truncated)
|
||||
* - ESP_ERR_NOT_FOUND if specified partition is not found in the partition table
|
||||
* - ESP_ERR_NO_MEM in case memory could not be allocated for the internal structures
|
||||
* - one of the error codes from the underlying flash storage driver
|
||||
*/
|
||||
esp_err_t nvs_flash_init_partition(const char *partition_label);
|
||||
|
||||
/**
|
||||
* @brief Initialize NVS flash storage for the partition specified by partition pointer.
|
||||
*
|
||||
* @param[in] partition pointer to a partition obtained by the ESP partition API.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if storage was successfully initialized
|
||||
* - ESP_ERR_NVS_NO_FREE_PAGES if the NVS storage contains no empty pages
|
||||
* (which may happen if NVS partition was truncated)
|
||||
* - ESP_ERR_INVALID_ARG in case partition is NULL
|
||||
* - ESP_ERR_NO_MEM in case memory could not be allocated for the internal structures
|
||||
* - one of the error codes from the underlying flash storage driver
|
||||
*/
|
||||
esp_err_t nvs_flash_init_partition_ptr(const esp_partition_t *partition);
|
||||
|
||||
/**
|
||||
* @brief Deinitialize NVS storage for the default NVS partition
|
||||
*
|
||||
* Default NVS partition is the partition with "nvs" label in the partition table.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK on success (storage was deinitialized)
|
||||
* - ESP_ERR_NVS_NOT_INITIALIZED if the storage was not initialized prior to this call
|
||||
*/
|
||||
esp_err_t nvs_flash_deinit(void);
|
||||
|
||||
/**
|
||||
* @brief Deinitialize NVS storage for the given NVS partition
|
||||
*
|
||||
* @param[in] partition_label Label of the partition
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK on success
|
||||
* - ESP_ERR_NVS_NOT_INITIALIZED if the storage for given partition was not
|
||||
* initialized prior to this call
|
||||
*/
|
||||
esp_err_t nvs_flash_deinit_partition(const char* partition_label);
|
||||
|
||||
/**
|
||||
* @brief Erase the default NVS partition
|
||||
*
|
||||
* Erases all contents of the default NVS partition (one with label "nvs").
|
||||
*
|
||||
* @note If the partition is initialized, this function first de-initializes it. Afterwards, the partition has to
|
||||
* be initialized again to be used.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK on success
|
||||
* - ESP_ERR_NOT_FOUND if there is no NVS partition labeled "nvs" in the
|
||||
* partition table
|
||||
* - different error in case de-initialization fails (shouldn't happen)
|
||||
*/
|
||||
esp_err_t nvs_flash_erase(void);
|
||||
|
||||
/**
|
||||
* @brief Erase specified NVS partition
|
||||
*
|
||||
* Erase all content of a specified NVS partition
|
||||
*
|
||||
* @note If the partition is initialized, this function first de-initializes it. Afterwards, the partition has to
|
||||
* be initialized again to be used.
|
||||
*
|
||||
* @param[in] part_name Name (label) of the partition which should be erased
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK on success
|
||||
* - ESP_ERR_NOT_FOUND if there is no NVS partition with the specified name
|
||||
* in the partition table
|
||||
* - different error in case de-initialization fails (shouldn't happen)
|
||||
*/
|
||||
esp_err_t nvs_flash_erase_partition(const char *part_name);
|
||||
|
||||
/**
|
||||
* @brief Erase custom partition.
|
||||
*
|
||||
* Erase all content of specified custom partition.
|
||||
*
|
||||
* @note
|
||||
* If the partition is initialized, this function first de-initializes it.
|
||||
* Afterwards, the partition has to be initialized again to be used.
|
||||
*
|
||||
* @param[in] partition pointer to a partition obtained by the ESP partition API.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK on success
|
||||
* - ESP_ERR_NOT_FOUND if there is no partition with the specified
|
||||
* parameters in the partition table
|
||||
* - ESP_ERR_INVALID_ARG in case partition is NULL
|
||||
* - one of the error codes from the underlying flash storage driver
|
||||
*/
|
||||
esp_err_t nvs_flash_erase_partition_ptr(const esp_partition_t *partition);
|
||||
|
||||
/**
|
||||
* @brief Initialize the default NVS partition.
|
||||
*
|
||||
* This API initialises the default NVS partition. The default NVS partition
|
||||
* is the one that is labeled "nvs" in the partition table.
|
||||
*
|
||||
* @param[in] cfg Security configuration (keys) to be used for NVS encryption/decryption.
|
||||
* If cfg is NULL, no encryption is used.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if storage has been initialized successfully.
|
||||
* - ESP_ERR_NVS_NO_FREE_PAGES if the NVS storage contains no empty pages
|
||||
* (which may happen if NVS partition was truncated)
|
||||
* - ESP_ERR_NOT_FOUND if no partition with label "nvs" is found in the partition table
|
||||
* - ESP_ERR_NO_MEM in case memory could not be allocated for the internal structures
|
||||
* - one of the error codes from the underlying flash storage driver
|
||||
*/
|
||||
esp_err_t nvs_flash_secure_init(nvs_sec_cfg_t* cfg);
|
||||
|
||||
/**
|
||||
* @brief Initialize NVS flash storage for the specified partition.
|
||||
*
|
||||
* @param[in] partition_label Label of the partition. Note that internally, a reference to
|
||||
* passed value is kept and it should be accessible for future operations
|
||||
*
|
||||
* @param[in] cfg Security configuration (keys) to be used for NVS encryption/decryption.
|
||||
* If cfg is null, no encryption/decryption is used.
|
||||
* @return
|
||||
* - ESP_OK if storage has been initialized successfully.
|
||||
* - ESP_ERR_NVS_NO_FREE_PAGES if the NVS storage contains no empty pages
|
||||
* (which may happen if NVS partition was truncated)
|
||||
* - ESP_ERR_NOT_FOUND if specified partition is not found in the partition table
|
||||
* - ESP_ERR_NO_MEM in case memory could not be allocated for the internal structures
|
||||
* - one of the error codes from the underlying flash storage driver
|
||||
*/
|
||||
esp_err_t nvs_flash_secure_init_partition(const char *partition_label, nvs_sec_cfg_t* cfg);
|
||||
|
||||
/**
|
||||
* @brief Generate and store NVS keys in the provided esp partition
|
||||
*
|
||||
* @param[in] partition Pointer to partition structure obtained using
|
||||
* esp_partition_find_first or esp_partition_get.
|
||||
* Must be non-NULL.
|
||||
* @param[out] cfg Pointer to nvs security configuration structure.
|
||||
* Pointer must be non-NULL.
|
||||
* Generated keys will be populated in this structure.
|
||||
*
|
||||
*
|
||||
* @return
|
||||
* -ESP_OK, if cfg was read successfully;
|
||||
* -ESP_INVALID_ARG, if partition or cfg;
|
||||
* -or error codes from esp_partition_write/erase APIs.
|
||||
*/
|
||||
|
||||
esp_err_t nvs_flash_generate_keys(const esp_partition_t* partition, nvs_sec_cfg_t* cfg);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Read NVS security configuration from a partition.
|
||||
*
|
||||
* @param[in] partition Pointer to partition structure obtained using
|
||||
* esp_partition_find_first or esp_partition_get.
|
||||
* Must be non-NULL.
|
||||
* @param[out] cfg Pointer to nvs security configuration structure.
|
||||
* Pointer must be non-NULL.
|
||||
*
|
||||
* @note Provided partition is assumed to be marked 'encrypted'.
|
||||
*
|
||||
* @return
|
||||
* -ESP_OK, if cfg was read successfully;
|
||||
* -ESP_INVALID_ARG, if partition or cfg;
|
||||
* -ESP_ERR_NVS_KEYS_NOT_INITIALIZED, if the partition is not yet written with keys.
|
||||
* -ESP_ERR_NVS_CORRUPT_KEY_PART, if the partition containing keys is found to be corrupt
|
||||
* -or error codes from esp_partition_read API.
|
||||
*/
|
||||
|
||||
esp_err_t nvs_flash_read_security_cfg(const esp_partition_t* partition, nvs_sec_cfg_t* cfg);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* nvs_flash_h */
|
280
tools/sdk/esp32c3/include/nvs_flash/include/nvs_handle.hpp
Normal file
280
tools/sdk/esp32c3/include/nvs_flash/include/nvs_handle.hpp
Normal file
@ -0,0 +1,280 @@
|
||||
#ifndef NVS_HANDLE_HPP_
|
||||
#define NVS_HANDLE_HPP_
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
|
||||
#include "nvs.h"
|
||||
|
||||
namespace nvs {
|
||||
|
||||
/**
|
||||
* The possible blob types. This is a helper definition for template functions.
|
||||
*/
|
||||
enum class ItemType : uint8_t {
|
||||
U8 = NVS_TYPE_U8,
|
||||
I8 = NVS_TYPE_I8,
|
||||
U16 = NVS_TYPE_U16,
|
||||
I16 = NVS_TYPE_I16,
|
||||
U32 = NVS_TYPE_U32,
|
||||
I32 = NVS_TYPE_I32,
|
||||
U64 = NVS_TYPE_U64,
|
||||
I64 = NVS_TYPE_I64,
|
||||
SZ = NVS_TYPE_STR,
|
||||
BLOB = 0x41,
|
||||
BLOB_DATA = NVS_TYPE_BLOB,
|
||||
BLOB_IDX = 0x48,
|
||||
ANY = NVS_TYPE_ANY
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief A handle allowing nvs-entry related operations on the NVS.
|
||||
*
|
||||
* @note The scope of this handle may vary depending on the implementation, but normally would be the namespace of
|
||||
* a particular partition. Outside that scope, nvs entries can't be accessed/altered.
|
||||
*/
|
||||
class NVSHandle {
|
||||
public:
|
||||
virtual ~NVSHandle() { }
|
||||
|
||||
/**
|
||||
* @brief set value for given key
|
||||
*
|
||||
* Sets value for key. Note that physical storage will not be updated until nvs_commit function is called.
|
||||
*
|
||||
* @param[in] key Key name. Maximal length is (NVS_KEY_NAME_MAX_SIZE-1) characters. Shouldn't be empty.
|
||||
* @param[in] value The value to set. Allowed types are the ones declared in ItemType as well as enums.
|
||||
* For strings, the maximum length (including null character) is
|
||||
* 4000 bytes, if there is one complete page free for writing.
|
||||
* This decreases, however, if the free space is fragmented.
|
||||
* Note that enums loose their type information when stored in NVS. Ensure that the correct
|
||||
* enum type is used during retrieval with \ref get_item!
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if value was set successfully
|
||||
* - ESP_ERR_NVS_READ_ONLY if storage handle was opened as read only
|
||||
* - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
|
||||
* - ESP_ERR_NVS_NOT_ENOUGH_SPACE if there is not enough space in the
|
||||
* underlying storage to save the value
|
||||
* - ESP_ERR_NVS_REMOVE_FAILED if the value wasn't updated because flash
|
||||
* write operation has failed. The value was written however, and
|
||||
* update will be finished after re-initialization of nvs, provided that
|
||||
* flash operation doesn't fail again.
|
||||
* - ESP_ERR_NVS_VALUE_TOO_LONG if the string value is too long
|
||||
*/
|
||||
template<typename T>
|
||||
esp_err_t set_item(const char *key, T value);
|
||||
virtual
|
||||
esp_err_t set_string(const char *key, const char* value) = 0;
|
||||
|
||||
/**
|
||||
* @brief get value for given key
|
||||
*
|
||||
* These functions retrieve value for the key, given its name. If key does not
|
||||
* exist, or the requested variable type doesn't match the type which was used
|
||||
* when setting a value, an error is returned.
|
||||
*
|
||||
* In case of any error, out_value is not modified.
|
||||
*
|
||||
* @param[in] key Key name. Maximal length is (NVS_KEY_NAME_MAX_SIZE-1) characters. Shouldn't be empty.
|
||||
* @param value The output value. All integral types which are declared in ItemType as well as enums
|
||||
* are allowed. Note however that enums lost their type information when stored in NVS.
|
||||
* Ensure that the correct enum type is used during retrieval with \ref get_item!
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if the value was retrieved successfully
|
||||
* - ESP_ERR_NVS_NOT_FOUND if the requested key doesn't exist
|
||||
* - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
|
||||
* - ESP_ERR_NVS_INVALID_LENGTH if length is not sufficient to store data
|
||||
*/
|
||||
template<typename T>
|
||||
esp_err_t get_item(const char *key, T &value);
|
||||
|
||||
/**
|
||||
* @brief set variable length binary value for given key
|
||||
*
|
||||
* This family of functions set value for the key, given its name. Note that
|
||||
* actual storage will not be updated until nvs_commit function is called.
|
||||
*
|
||||
* @param[in] key Key name. Maximal length is (NVS_KEY_NAME_MAX_SIZE-1) characters. Shouldn't be empty.
|
||||
* @param[in] blob The blob value to set.
|
||||
* @param[in] len length of binary value to set, in bytes; Maximum length is
|
||||
* 508000 bytes or (97.6% of the partition size - 4000) bytes
|
||||
* whichever is lower.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if value was set successfully
|
||||
* - ESP_ERR_NVS_READ_ONLY if storage handle was opened as read only
|
||||
* - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
|
||||
* - ESP_ERR_NVS_NOT_ENOUGH_SPACE if there is not enough space in the
|
||||
* underlying storage to save the value
|
||||
* - ESP_ERR_NVS_REMOVE_FAILED if the value wasn't updated because flash
|
||||
* write operation has failed. The value was written however, and
|
||||
* update will be finished after re-initialization of nvs, provided that
|
||||
* flash operation doesn't fail again.
|
||||
* - ESP_ERR_NVS_VALUE_TOO_LONG if the value is too long
|
||||
*
|
||||
* @note compare to \ref nvs_set_blob in nvs.h
|
||||
*/
|
||||
virtual esp_err_t set_blob(const char *key, const void* blob, size_t len) = 0;
|
||||
|
||||
/**
|
||||
* @brief get value for given key
|
||||
*
|
||||
* These functions retrieve the data of an entry, given its key. If key does not
|
||||
* exist, or the requested variable type doesn't match the type which was used
|
||||
* when setting a value, an error is returned.
|
||||
*
|
||||
* In case of any error, out_value is not modified.
|
||||
*
|
||||
* Both functions expect out_value to be a pointer to an already allocated variable
|
||||
* of the given type.
|
||||
*
|
||||
* It is suggested that nvs_get/set_str is used for zero-terminated short C strings, and
|
||||
* nvs_get/set_blob is used for arbitrary data structures and long C strings.
|
||||
*
|
||||
* @param[in] key Key name. Maximum length is (NVS_KEY_NAME_MAX_SIZE-1) characters. Shouldn't be empty.
|
||||
* @param out_str/ Pointer to the output value.
|
||||
* out_blob
|
||||
* @param[inout] len The length of the output buffer pointed to by out_str/out_blob.
|
||||
* Use \c get_item_size to query the size of the item beforehand.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if the value was retrieved successfully
|
||||
* - ESP_ERR_NVS_NOT_FOUND if the requested key doesn't exist
|
||||
* - ESP_ERR_NVS_INVALID_NAME if key name doesn't satisfy constraints
|
||||
* - ESP_ERR_NVS_INVALID_LENGTH if length is not sufficient to store data
|
||||
*/
|
||||
virtual esp_err_t get_string(const char *key, char* out_str, size_t len) = 0;
|
||||
virtual esp_err_t get_blob(const char *key, void* out_blob, size_t len) = 0;
|
||||
|
||||
/**
|
||||
* @brief Look up the size of an entry's data.
|
||||
*
|
||||
* @param[in] datatype Data type to search for.
|
||||
* @param[in] key Key name. Maximum length is (NVS_KEY_NAME_MAX_SIZE-1) characters. Shouldn't be empty.
|
||||
* @param[out] size Size of the item, if it exists.
|
||||
* For strings, this size includes the zero terminator.
|
||||
*
|
||||
* @return - ESP_OK if the item with specified type and key exists. Its size will be returned via \c size.
|
||||
* - ESP_ERR_NVS_NOT_FOUND if an item with the requested key and type doesn't exist or any other
|
||||
* error occurs.
|
||||
*/
|
||||
virtual esp_err_t get_item_size(ItemType datatype, const char *key, size_t &size) = 0;
|
||||
|
||||
/**
|
||||
* @brief Erases an entry.
|
||||
*/
|
||||
virtual esp_err_t erase_item(const char* key) = 0;
|
||||
|
||||
/**
|
||||
* Erases all entries in the scope of this handle. The scope may vary, depending on the implementation.
|
||||
*
|
||||
* @not If you want to erase the whole nvs flash (partition), refer to \ref
|
||||
*/
|
||||
virtual esp_err_t erase_all() = 0;
|
||||
|
||||
/**
|
||||
* Commits all changes done through this handle so far.
|
||||
* Currently, NVS writes to storage right after the set and get functions,
|
||||
* but this is not guaranteed.
|
||||
*/
|
||||
virtual esp_err_t commit() = 0;
|
||||
|
||||
/**
|
||||
* @brief Calculate all entries in the scope of the handle.
|
||||
*
|
||||
* @param[out] used_entries Returns amount of used entries from a namespace on success.
|
||||
*
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK if the changes have been written successfully.
|
||||
* Return param used_entries will be filled valid value.
|
||||
* - ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized.
|
||||
* Return param used_entries will be filled 0.
|
||||
* - ESP_ERR_INVALID_ARG if nvs_stats equal to NULL.
|
||||
* - Other error codes from the underlying storage driver.
|
||||
* Return param used_entries will be filled 0.
|
||||
*/
|
||||
virtual esp_err_t get_used_entry_count(size_t& usedEntries) = 0;
|
||||
|
||||
protected:
|
||||
virtual esp_err_t set_typed_item(ItemType datatype, const char *key, const void* data, size_t dataSize) = 0;
|
||||
|
||||
virtual esp_err_t get_typed_item(ItemType datatype, const char *key, void* data, size_t dataSize) = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Opens non-volatile storage and returns a handle object.
|
||||
*
|
||||
* The handle is automatically closed on desctruction. The scope of the handle is the namespace ns_name
|
||||
* in a particular partition partition_name.
|
||||
* The parameters partition_name, ns_name and open_mode have the same meaning and restrictions as the parameters
|
||||
* part_name, name and open_mode in \ref nvs_open_from_partition, respectively.
|
||||
*
|
||||
* @param err an optional pointer to an esp_err_t result of the open operation, having the same meaning as the return
|
||||
* value in \ref nvs_open_from_partition:
|
||||
* - ESP_OK if storage handle was opened successfully
|
||||
* - ESP_ERR_NVS_NOT_INITIALIZED if the storage driver is not initialized
|
||||
* - ESP_ERR_NVS_PART_NOT_FOUND if the partition with label "nvs" is not found
|
||||
* - ESP_ERR_NVS_NOT_FOUND id namespace doesn't exist yet and
|
||||
* mode is NVS_READONLY
|
||||
* - ESP_ERR_NVS_INVALID_NAME if namespace name doesn't satisfy constraints
|
||||
* - other error codes from the underlying storage driver
|
||||
*
|
||||
* @return shared pointer of an nvs handle on success, an empty shared pointer otherwise
|
||||
*/
|
||||
std::unique_ptr<NVSHandle> open_nvs_handle_from_partition(const char *partition_name,
|
||||
const char *ns_name,
|
||||
nvs_open_mode_t open_mode,
|
||||
esp_err_t *err = nullptr);
|
||||
|
||||
/**
|
||||
* @brief This function does the same as \ref open_nvs_handle_from_partition but uses the default nvs partition
|
||||
* instead of a partition_name parameter.
|
||||
*/
|
||||
std::unique_ptr<NVSHandle> open_nvs_handle(const char *ns_name,
|
||||
nvs_open_mode_t open_mode,
|
||||
esp_err_t *err = nullptr);
|
||||
|
||||
// Helper functions for template usage
|
||||
/**
|
||||
* Help to translate all integral types into ItemType.
|
||||
*/
|
||||
template<typename T, typename std::enable_if<std::is_integral<T>::value, void*>::type = nullptr>
|
||||
constexpr ItemType itemTypeOf()
|
||||
{
|
||||
return static_cast<ItemType>(((std::is_signed<T>::value)?0x10:0x00) | sizeof(T));
|
||||
}
|
||||
|
||||
/**
|
||||
* Help to translate all enum types into integral ItemType.
|
||||
*/
|
||||
template<typename T, typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
|
||||
constexpr ItemType itemTypeOf()
|
||||
{
|
||||
return static_cast<ItemType>(((std::is_signed<T>::value)?0x10:0x00) | sizeof(T));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr ItemType itemTypeOf(const T&)
|
||||
{
|
||||
return itemTypeOf<T>();
|
||||
}
|
||||
|
||||
// Template Implementations
|
||||
template<typename T>
|
||||
esp_err_t NVSHandle::set_item(const char *key, T value) {
|
||||
return set_typed_item(itemTypeOf(value), key, &value, sizeof(value));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
esp_err_t NVSHandle::get_item(const char *key, T &value) {
|
||||
return get_typed_item(itemTypeOf(value), key, &value, sizeof(value));
|
||||
}
|
||||
|
||||
} // nvs
|
||||
|
||||
#endif // NVS_HANDLE_HPP_
|
Reference in New Issue
Block a user