arduino-esp32/libraries/ESP32NVS/src/ESP32NVS.cpp

428 lines
11 KiB
C++
Raw Normal View History

2017-02-08 22:09:42 +01:00
// 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.
#include "ESP32NVS.h"
#include "nvs.h"
const char * nvs_errors[] = { "OTHER", "NOT_INITIALIZED", "NOT_FOUND", "TYPE_MISMATCH", "READ_ONLY", "NOT_ENOUGH_SPACE", "INVALID_NAME", "INVALID_HANDLE", "REMOVE_FAILED", "KEY_TOO_LONG", "PAGE_FULL", "INVALID_STATE", "INVALID_LENGHT"};
#define nvs_error(e) (((e)>ESP_ERR_NVS_BASE)?nvs_errors[(e)&~(ESP_ERR_NVS_BASE)]:nvs_errors[0])
NVSClass::NVSClass()
:_handle(0)
,_started(false)
,_readOnly(false)
{}
NVSClass::~NVSClass(){
end();
}
bool NVSClass::begin(const char * name, bool readOnly){
if(_started){
return false;
}
_readOnly = readOnly;
esp_err_t err = nvs_open(name, readOnly?NVS_READONLY:NVS_READWRITE, &_handle);
if(err){
log_e("nvs_open failed: %s", nvs_error(err));
return false;
}
_started = true;
return true;
}
void NVSClass::end(){
if(!_started){
return;
}
nvs_close(_handle);
_started = false;
}
/*
* Clear the storage
2017-02-08 22:09:42 +01:00
* */
bool NVSClass::clear(){
2017-02-08 22:09:42 +01:00
if(!_started || _readOnly){
return false;
}
esp_err_t err = nvs_erase_all(_handle);
if(err){
log_e("nvs_erase_all fail: %s", nvs_error(err));
return false;
}
return true;
}
/*
* Remove the key
* */
bool NVSClass::remove(const char * key){
2017-02-08 22:09:42 +01:00
if(!_started || !key || _readOnly){
return false;
}
esp_err_t err = nvs_erase_key(_handle, key);
if(err){
log_e("nvs_erase_key fail: %s %s", key, nvs_error(err));
return false;
}
return true;
}
/*
* Write
* */
size_t NVSClass::setChar(const char* key, int8_t value){
2017-02-08 22:09:42 +01:00
if(!_started || !key || _readOnly){
return 0;
}
esp_err_t err = nvs_set_i8(_handle, key, value);
if(err){
log_e("nvs_set_i8 fail: %s %s", key, nvs_error(err));
return 0;
}
err = nvs_commit(_handle);
if(err){
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
return 0;
}
return 1;
}
size_t NVSClass::setUChar(const char* key, uint8_t value){
2017-02-08 22:09:42 +01:00
if(!_started || !key || _readOnly){
return 0;
}
esp_err_t err = nvs_set_u8(_handle, key, value);
if(err){
log_e("nvs_set_u8 fail: %s %s", key, nvs_error(err));
return 0;
}
err = nvs_commit(_handle);
if(err){
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
return 0;
}
return 1;
}
size_t NVSClass::setShort(const char* key, int16_t value){
2017-02-08 22:09:42 +01:00
if(!_started || !key || _readOnly){
return 0;
}
esp_err_t err = nvs_set_i16(_handle, key, value);
if(err){
log_e("nvs_set_i16 fail: %s %s", key, nvs_error(err));
return 0;
}
err = nvs_commit(_handle);
if(err){
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
return 0;
}
return 2;
}
size_t NVSClass::setUShort(const char* key, uint16_t value){
2017-02-08 22:09:42 +01:00
if(!_started || !key || _readOnly){
return 0;
}
esp_err_t err = nvs_set_u16(_handle, key, value);
if(err){
log_e("nvs_set_u16 fail: %s %s", key, nvs_error(err));
return 0;
}
err = nvs_commit(_handle);
if(err){
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
return 0;
}
return 2;
}
size_t NVSClass::setInt(const char* key, int32_t value){
2017-02-08 22:09:42 +01:00
if(!_started || !key || _readOnly){
return 0;
}
esp_err_t err = nvs_set_i32(_handle, key, value);
if(err){
log_e("nvs_set_i32 fail: %s %s", key, nvs_error(err));
return 0;
}
err = nvs_commit(_handle);
if(err){
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
return 0;
}
return 4;
}
size_t NVSClass::setUInt(const char* key, uint32_t value){
2017-02-08 22:09:42 +01:00
if(!_started || !key || _readOnly){
return 0;
}
esp_err_t err = nvs_set_u32(_handle, key, value);
if(err){
log_e("nvs_set_u32 fail: %s %s", key, nvs_error(err));
return 0;
}
err = nvs_commit(_handle);
if(err){
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
return 0;
}
return 4;
}
size_t NVSClass::setLong(const char* key, int64_t value){
2017-02-08 22:09:42 +01:00
if(!_started || !key || _readOnly){
return 0;
}
esp_err_t err = nvs_set_i64(_handle, key, value);
if(err){
log_e("nvs_set_i64 fail: %s %s", key, nvs_error(err));
return 0;
}
err = nvs_commit(_handle);
if(err){
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
return 0;
}
return 8;
}
size_t NVSClass::setULong(const char* key, uint64_t value){
2017-02-08 22:09:42 +01:00
if(!_started || !key || _readOnly){
return 0;
}
esp_err_t err = nvs_set_u64(_handle, key, value);
if(err){
log_e("nvs_set_u64 fail: %s %s", key, nvs_error(err));
return 0;
}
err = nvs_commit(_handle);
if(err){
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
return 0;
}
return 8;
}
size_t NVSClass::setString(const char* key, const char* value){
2017-02-08 22:09:42 +01:00
if(!_started || !key || !value || _readOnly){
return 0;
}
esp_err_t err = nvs_set_str(_handle, key, value);
if(err){
log_e("nvs_set_str fail: %s %s", key, nvs_error(err));
return 0;
}
err = nvs_commit(_handle);
if(err){
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
return 0;
}
return strlen(value);
}
size_t NVSClass::setString(const char* key, const String value){
return setString(key, value.c_str());
}
size_t NVSClass::setBytes(const char* key, const void* value, size_t len){
2017-02-08 22:09:42 +01:00
if(!_started || !key || !value || !len || _readOnly){
return 0;
}
esp_err_t err = nvs_set_blob(_handle, key, value, len);
if(err){
log_e("nvs_set_blob fail: %s %s", key, nvs_error(err));
return 0;
}
err = nvs_commit(_handle);
if(err){
log_e("nvs_commit fail: %s %s", key, nvs_error(err));
return 0;
}
return len;
}
/*
* Read
* */
int8_t NVSClass::getChar(const char* key, const int8_t defaultValue){
int8_t value = defaultValue;
2017-02-08 22:09:42 +01:00
if(!_started || !key){
return value;
}
esp_err_t err = nvs_get_i8(_handle, key, &value);
if(err){
log_e("nvs_get_i8 fail: %s %s", key, nvs_error(err));
}
return value;
}
uint8_t NVSClass::getUChar(const char* key, const uint8_t defaultValue){
uint8_t value = defaultValue;
2017-02-08 22:09:42 +01:00
if(!_started || !key){
return value;
}
esp_err_t err = nvs_get_u8(_handle, key, &value);
if(err){
log_e("nvs_get_u8 fail: %s %s", key, nvs_error(err));
}
return value;
}
int16_t NVSClass::getShort(const char* key, const int16_t defaultValue){
int16_t value = defaultValue;
2017-02-08 22:09:42 +01:00
if(!_started || !key){
return value;
}
esp_err_t err = nvs_get_i16(_handle, key, &value);
if(err){
log_e("nvs_get_i16 fail: %s %s", key, nvs_error(err));
}
return value;
}
uint16_t NVSClass::getUShort(const char* key, const uint16_t defaultValue){
uint16_t value = defaultValue;
2017-02-08 22:09:42 +01:00
if(!_started || !key){
return value;
}
esp_err_t err = nvs_get_u16(_handle, key, &value);
if(err){
log_e("nvs_get_u16 fail: %s %s", key, nvs_error(err));
}
return value;
}
int32_t NVSClass::getInt(const char* key, const int32_t defaultValue){
int32_t value = defaultValue;
2017-02-08 22:09:42 +01:00
if(!_started || !key){
return value;
}
esp_err_t err = nvs_get_i32(_handle, key, &value);
if(err){
log_e("nvs_get_i32 fail: %s %s", key, nvs_error(err));
}
return value;
}
uint32_t NVSClass::getUInt(const char* key, const uint32_t defaultValue){
uint32_t value = defaultValue;
2017-02-08 22:09:42 +01:00
if(!_started || !key){
return value;
}
esp_err_t err = nvs_get_u32(_handle, key, &value);
if(err){
log_e("nvs_get_u32 fail: %s %s", key, nvs_error(err));
}
return value;
}
int64_t NVSClass::getLong(const char* key, const int64_t defaultValue){
int64_t value = defaultValue;
2017-02-08 22:09:42 +01:00
if(!_started || !key){
return value;
}
esp_err_t err = nvs_get_i64(_handle, key, &value);
if(err){
log_e("nvs_get_i64 fail: %s %s", key, nvs_error(err));
}
return value;
}
uint64_t NVSClass::getULong(const char* key, const uint64_t defaultValue){
uint64_t value = defaultValue;
2017-02-08 22:09:42 +01:00
if(!_started || !key){
return value;
}
esp_err_t err = nvs_get_u64(_handle, key, &value);
if(err){
log_e("nvs_get_u64 fail: %s %s", key, nvs_error(err));
}
return value;
}
size_t NVSClass::getString(const char* key, char* value, const size_t maxLen){
size_t len = 0;
if(!_started || !key || !value || !maxLen){
return 0;
}
esp_err_t err = nvs_get_str(_handle, key, NULL, &len);
if(err){
log_e("nvs_get_str len fail: %s %s", key, nvs_error(err));
return 0;
}
if(len > maxLen){
log_e("not enough space in value: %u < %u", maxLen, len);
return 0;
}
err = nvs_get_str(_handle, key, value, &len);
if(err){
log_e("nvs_get_str fail: %s %s", key, nvs_error(err));
return 0;
}
return len;
}
String NVSClass::getString(const char* key, const String defaultValue){
2017-02-08 22:09:42 +01:00
char * value = NULL;
size_t len = 0;
if(!_started || !key){
return String(defaultValue);
2017-02-08 22:09:42 +01:00
}
esp_err_t err = nvs_get_str(_handle, key, value, &len);
if(err){
log_e("nvs_get_str len fail: %s %s", key, nvs_error(err));
return String(defaultValue);
2017-02-08 22:09:42 +01:00
}
char buf[len];
value = buf;
err = nvs_get_str(_handle, key, value, &len);
if(err){
log_e("nvs_get_str fail: %s %s", key, nvs_error(err));
return String(defaultValue);
2017-02-08 22:09:42 +01:00
}
return String(buf);
}
size_t NVSClass::getBytes(const char* key, void * buf, size_t maxLen){
2017-02-08 22:09:42 +01:00
size_t len = 0;
if(!_started || !key || !buf || !maxLen){
return 0;
}
esp_err_t err = nvs_get_blob(_handle, key, NULL, &len);
if(err){
log_e("nvs_get_blob len fail: %s %s", key, nvs_error(err));
return 0;
}
if(len > maxLen){
log_e("not enough space in buffer: %u < %u", maxLen, len);
return 0;
}
err = nvs_get_blob(_handle, key, buf, &len);
if(err){
log_e("nvs_get_blob fail: %s %s", key, nvs_error(err));
return 0;
}
return len;
}