Firmware  0.0.0
Loading...
Searching...
No Matches
base.hpp
Go to the documentation of this file.
1// Copyright (C) 2025 Vincent Hamp
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
21
22#pragma once
23
24#include <nvs.h>
25#include <cstdint>
26#include <span>
27#include <string_view>
28
29namespace mem::nvs {
30
41class Base {
42public:
44 struct Sentinel {};
45
48 struct Iterator {
49 using value_type = nvs_entry_info_t;
50 using difference_type = std::ptrdiff_t;
52 using iterator_category = std::input_iterator_tag;
53
59 Iterator(char const* namespace_name)
60 : _err{nvs_entry_find(
61 NVS_DEFAULT_PART_NAME, namespace_name, NVS_TYPE_ANY, &_it)} {}
62
66 ~Iterator() { nvs_release_iterator(_it); }
67
69 nvs_entry_info_t info;
70 nvs_entry_info(_it, &info);
71 return info;
72 }
73
75 _err = nvs_entry_next(&_it);
76 return *this;
77 }
78
79 void operator++(int) { this->operator++(); }
80
87 friend constexpr bool operator==(Iterator const& lhs, Sentinel) {
88 return lhs._err;
89 }
90
91 private:
92 nvs_iterator_t _it{};
93 esp_err_t _err{};
94 };
95
96 static_assert(std::input_iterator<Iterator>);
97
98 auto begin() const { return Iterator{_namespace_name}; }
99 auto cbegin() const { return begin(); }
100 auto end() const { return Sentinel{}; }
101 auto cend() const { return end(); }
102
103 esp_err_t erase(std::string const& key);
104 esp_err_t eraseAll();
105
106protected:
107 Base(char const* namespace_name, nvs_open_mode_t open_mode);
108 ~Base();
109
110 std::string getBlob(std::string const& key) const;
111 esp_err_t setBlob(std::string const& key, std::string_view str);
112
113 uint8_t getU8(std::string const& key) const;
114 esp_err_t setU8(std::string const& key, uint8_t value);
115
116 uint8_t getU16(std::string const& key) const;
117 esp_err_t setU16(std::string const& key, uint16_t value);
118
119private:
120 // The getters and setters in this class rely on SSO inside the std::string
121 // class. The default capacity of std::string must be large enough to hold a
122 // string of length NVS_KEY_NAME_MAX_SIZE.
123 //
124 // The memory layout of std::string looks something like this
125 // char*
126 // size_t
127 // char[15+1]
128 static_assert(sizeof(std::string) == sizeof(std::string::pointer) +
129 sizeof(std::string::size_type) +
130 (15uz + 1uz));
131 static_assert(NVS_KEY_NAME_MAX_SIZE <= 15uz + 1uz);
132
134 char const* _namespace_name{};
135
137 nvs_handle_t _handle{};
138
141};
142
143} // namespace mem::nvs
NVS base.
Definition base.hpp:41
esp_err_t setBlob(std::string const &key, std::string_view str)
Set blob value for given key.
Definition base.cpp:96
esp_err_t eraseAll()
Erase all key-value pairs in a namespace.
Definition base.cpp:66
uint8_t getU16(std::string const &key) const
Get uint16_t value for given key.
Definition base.cpp:135
auto end() const
Definition base.hpp:100
Base(char const *namespace_name, nvs_open_mode_t open_mode)
Ctor.
Definition base.cpp:34
esp_err_t setU16(std::string const &key, uint16_t value)
Set uint16_t value for given key.
Definition base.cpp:152
esp_err_t setU8(std::string const &key, uint8_t value)
Set uint8_t value for given key.
Definition base.cpp:124
nvs_handle_t _handle
Opaque pointer type representing non-volatile storage handle.
Definition base.hpp:137
esp_err_t erase(std::string const &key)
Erase key-value pair with given key name.
Definition base.cpp:55
char const * _namespace_name
Store namespace name (mainly for iterator)
Definition base.hpp:134
auto cbegin() const
Definition base.hpp:99
std::string getBlob(std::string const &key) const
Get blob value for given key.
Definition base.cpp:76
uint8_t getU8(std::string const &key) const
Get uint8_t value for given key.
Definition base.cpp:107
~Base()
Dtor.
Definition base.cpp:44
bool _commit_pending
Flag to indicate commit pending.
Definition base.hpp:140
auto begin() const
Definition base.hpp:98
auto cend() const
Definition base.hpp:101
Definition accessories.cpp:27
Wrapper around C-style NVS iterators
Definition base.hpp:48
void operator++(int)
Definition base.hpp:79
reference operator*() const
Definition base.hpp:68
std::input_iterator_tag iterator_category
Definition base.hpp:52
friend constexpr bool operator==(Iterator const &lhs, Sentinel)
Equality comparison.
Definition base.hpp:87
std::ptrdiff_t difference_type
Definition base.hpp:50
esp_err_t _err
Definition base.hpp:93
~Iterator()
Dtor.
Definition base.hpp:66
Iterator(char const *namespace_name)
Ctor.
Definition base.hpp:59
value_type reference
Definition base.hpp:51
Iterator & operator++()
Definition base.hpp:74
nvs_entry_info_t value_type
Definition base.hpp:49
nvs_iterator_t _it
Definition base.hpp:92
Sentinel type for Iterator.
Definition base.hpp:44