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 #ifndef NVS_HANDLE_SIMPLE_HPP_
15 #define NVS_HANDLE_SIMPLE_HPP_
16 
17 #include "intrusive_list.h"
18 #include "nvs_storage.hpp"
19 #include "nvs_platform.hpp"
20 
21 #include "nvs_handle.hpp"
22 
23 namespace nvs {
24 
25 /**
26  * @brief This class implements NVSHandle according to the ESP32's flash and partitioning scheme.
27  *
28  * It is used by both the C API and the C++ API. The main responsibility is to check whether the handle is valid
29  * and in the right read/write mode and then forward the calls to the storage object.
30  *
31  * For more details about the general member functions, see nvs_handle.hpp.
32  */
33 class NVSHandleSimple : public intrusive_list_node<NVSHandleSimple>, public NVSHandle {
34     friend class NVSPartitionManager;
35 public:
NVSHandleSimple(bool readOnly,uint8_t nsIndex,Storage * StoragePtr)36     NVSHandleSimple(bool readOnly, uint8_t nsIndex, Storage *StoragePtr) :
37         mStoragePtr(StoragePtr),
38         mNsIndex(nsIndex),
39         mReadOnly(readOnly),
40         valid(1)
41     { }
42 
43     ~NVSHandleSimple();
44 
45     esp_err_t set_typed_item(ItemType datatype, const char *key, const void *data, size_t dataSize) override;
46 
47     esp_err_t get_typed_item(ItemType datatype, const char *key, void *data, size_t dataSize) override;
48 
49     esp_err_t set_string(const char *key, const char *str) override;
50 
51     esp_err_t set_blob(const char *key, const void *blob, size_t len) override;
52 
53     esp_err_t get_string(const char *key, char *out_str, size_t len) override;
54 
55     esp_err_t get_blob(const char *key, void *out_blob, size_t len) override;
56 
57     esp_err_t get_item_size(ItemType datatype, const char *key, size_t &size) override;
58 
59     esp_err_t erase_item(const char *key) override;
60 
61     esp_err_t erase_all() override;
62 
63     esp_err_t commit() override;
64 
65     esp_err_t get_used_entry_count(size_t &usedEntries) override;
66 
67     esp_err_t getItemDataSize(ItemType datatype, const char *key, size_t &dataSize);
68 
69     void debugDump();
70 
71     esp_err_t fillStats(nvs_stats_t &nvsStats);
72 
73     esp_err_t calcEntriesInNamespace(size_t &usedEntries);
74 
75     bool findEntry(nvs_opaque_iterator_t *it, const char *name);
76 
77     bool nextEntry(nvs_opaque_iterator_t *it);
78 
79     const char *get_partition_name() const;
80 
81 private:
82     /**
83      * The underlying storage's object.
84      */
85     Storage *mStoragePtr;
86 
87     /**
88      * Numeric representation of the namespace as it is saved in flash (see README.rst for further details).
89      */
90     uint8_t mNsIndex;
91 
92     /**
93      * Whether this handle is marked as read-only or read-write.
94      * 0 indicates read-only, any other value read-write.
95      */
96     uint8_t mReadOnly;
97 
98     /**
99      * Indicates the validity of this handle.
100      * Upon opening, a handle is valid. It becomes invalid if the underlying storage is de-initialized.
101      */
102     uint8_t valid;
103 };
104 
105 } // nvs
106 
107 #endif // NVS_HANDLE_SIMPLE_HPP_
108