1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include <cstdlib>
15 #include "nvs_handle.hpp"
16 #include "nvs_partition_manager.hpp"
17 
18 namespace nvs {
19 
~NVSHandleSimple()20 NVSHandleSimple::~NVSHandleSimple() {
21     NVSPartitionManager::get_instance()->close_handle(this);
22 }
23 
set_typed_item(ItemType datatype,const char * key,const void * data,size_t dataSize)24 esp_err_t NVSHandleSimple::set_typed_item(ItemType datatype, const char *key, const void* data, size_t dataSize)
25 {
26     if (!valid) return ESP_ERR_NVS_INVALID_HANDLE;
27     if (mReadOnly) return ESP_ERR_NVS_READ_ONLY;
28 
29     return mStoragePtr->writeItem(mNsIndex, datatype, key, data, dataSize);
30 }
31 
get_typed_item(ItemType datatype,const char * key,void * data,size_t dataSize)32 esp_err_t NVSHandleSimple::get_typed_item(ItemType datatype, const char *key, void* data, size_t dataSize)
33 {
34     if (!valid) return ESP_ERR_NVS_INVALID_HANDLE;
35 
36     return mStoragePtr->readItem(mNsIndex, datatype, key, data, dataSize);
37 }
38 
set_string(const char * key,const char * str)39 esp_err_t NVSHandleSimple::set_string(const char *key, const char* str)
40 {
41     if (!valid) return ESP_ERR_NVS_INVALID_HANDLE;
42     if (mReadOnly) return ESP_ERR_NVS_READ_ONLY;
43 
44     return mStoragePtr->writeItem(mNsIndex, nvs::ItemType::SZ, key, str, strlen(str) + 1);
45 }
46 
set_blob(const char * key,const void * blob,size_t len)47 esp_err_t NVSHandleSimple::set_blob(const char *key, const void* blob, size_t len)
48 {
49     if (!valid) return ESP_ERR_NVS_INVALID_HANDLE;
50     if (mReadOnly) return ESP_ERR_NVS_READ_ONLY;
51 
52     return mStoragePtr->writeItem(mNsIndex, nvs::ItemType::BLOB, key, blob, len);
53 }
54 
get_string(const char * key,char * out_str,size_t len)55 esp_err_t NVSHandleSimple::get_string(const char *key, char* out_str, size_t len)
56 {
57     if (!valid) return ESP_ERR_NVS_INVALID_HANDLE;
58 
59     return mStoragePtr->readItem(mNsIndex, nvs::ItemType::SZ, key, out_str, len);
60 }
61 
get_blob(const char * key,void * out_blob,size_t len)62 esp_err_t NVSHandleSimple::get_blob(const char *key, void* out_blob, size_t len)
63 {
64     if (!valid) return ESP_ERR_NVS_INVALID_HANDLE;
65 
66     return mStoragePtr->readItem(mNsIndex, nvs::ItemType::BLOB, key, out_blob, len);
67 }
68 
get_item_size(ItemType datatype,const char * key,size_t & size)69 esp_err_t NVSHandleSimple::get_item_size(ItemType datatype, const char *key, size_t &size)
70 {
71     if (!valid) return ESP_ERR_NVS_INVALID_HANDLE;
72 
73     return mStoragePtr->getItemDataSize(mNsIndex, datatype, key, size);
74 }
75 
erase_item(const char * key)76 esp_err_t NVSHandleSimple::erase_item(const char* key)
77 {
78     if (!valid) return ESP_ERR_NVS_INVALID_HANDLE;
79     if (mReadOnly) return ESP_ERR_NVS_READ_ONLY;
80 
81     return mStoragePtr->eraseItem(mNsIndex, key);
82 }
83 
erase_all()84 esp_err_t NVSHandleSimple::erase_all()
85 {
86     if (!valid) return ESP_ERR_NVS_INVALID_HANDLE;
87     if (mReadOnly) return ESP_ERR_NVS_READ_ONLY;
88 
89     return mStoragePtr->eraseNamespace(mNsIndex);
90 }
91 
commit()92 esp_err_t NVSHandleSimple::commit()
93 {
94     if (!valid) return ESP_ERR_NVS_INVALID_HANDLE;
95 
96     return ESP_OK;
97 }
98 
get_used_entry_count(size_t & used_entries)99 esp_err_t NVSHandleSimple::get_used_entry_count(size_t& used_entries)
100 {
101     used_entries = 0;
102 
103     if (!valid) return ESP_ERR_NVS_INVALID_HANDLE;
104 
105     size_t used_entry_count;
106     esp_err_t err = mStoragePtr->calcEntriesInNamespace(mNsIndex, used_entry_count);
107     if(err == ESP_OK){
108         used_entries = used_entry_count;
109     }
110     return err;
111 }
112 
debugDump()113 void NVSHandleSimple::debugDump() {
114     return mStoragePtr->debugDump();
115 }
116 
fillStats(nvs_stats_t & nvsStats)117 esp_err_t NVSHandleSimple::fillStats(nvs_stats_t& nvsStats) {
118     return mStoragePtr->fillStats(nvsStats);
119 }
120 
calcEntriesInNamespace(size_t & usedEntries)121 esp_err_t NVSHandleSimple::calcEntriesInNamespace(size_t& usedEntries) {
122     return mStoragePtr->calcEntriesInNamespace(mNsIndex, usedEntries);
123 }
124 
findEntry(nvs_opaque_iterator_t * it,const char * name)125 bool NVSHandleSimple::findEntry(nvs_opaque_iterator_t* it, const char* name) {
126     return mStoragePtr->findEntry(it, name);
127 }
128 
nextEntry(nvs_opaque_iterator_t * it)129 bool NVSHandleSimple::nextEntry(nvs_opaque_iterator_t* it) {
130     return mStoragePtr->nextEntry(it);
131 }
132 
get_partition_name() const133 const char *NVSHandleSimple::get_partition_name() const {
134     return mStoragePtr->getPartName();
135 }
136 
137 }
138