Uniot Core
0.8.1
Loading...
Searching...
No Matches
NVSFS.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2025 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
28
29#pragma once
30
31#ifdef ESP32
32
33#include <Bytes.h>
34#include <Logger.h>
35#include <Preferences.h>
36
37namespace uniot {
38
61class NVSFile {
62 public:
77 NVSFile(const String& path, const String& mode, Preferences& prefs)
78 : mKey(path), mMode(mode), mPrefs(prefs), mValid(true) {
79 mKey = normalizePath(mKey);
80 if (mMode == "r") {
81 _loadData();
82 } else if (mMode == "w") {
83 mBuffer = Bytes();
84 } else {
85 mValid = false;
86 }
87 }
88
95 operator bool() const { return mValid; }
96
112 size_t write(const uint8_t* data, size_t size) {
113 if (!mValid || mMode != "w") {
114 return 0;
115 }
116
117 mBuffer = Bytes(data, size);
118 return size;
119 }
120
134 if (!mValid || mMode != "r") {
135 return Bytes();
136 }
137
138 return mBuffer;
139 }
140
154 void close() {
155 if (!mValid) {
156 return;
157 }
158
159 if (mMode == "w") {
160 if (mBuffer.size() == 0) {
161 mPrefs.remove(mKey.c_str());
162 } else {
163 mPrefs.putBytes(mKey.c_str(), mBuffer.raw(), mBuffer.size());
164 }
165 }
166
167 mValid = false;
168 }
169
184 static String normalizePath(const String& path) {
185 String normalized = path;
186 if (normalized.startsWith("/")) {
187 normalized = normalized.substring(1);
188 }
189 if (normalized.length() > 15) {
190 normalized = normalized.substring(0, 15);
191 UNIOT_LOG_WARN("NVS key truncated to 15 chars: %s", normalized.c_str());
192 }
193 return normalized;
194 }
195
196 private:
197 String mKey;
198 String mMode;
199 Bytes mBuffer;
200 Preferences& mPrefs;
201 bool mValid;
202
213 void _loadData() {
214 if (!mPrefs.isKey(mKey.c_str())) {
215 mBuffer = Bytes();
216 return;
217 }
218
219 size_t dataSize = mPrefs.getBytesLength(mKey.c_str());
220 if (dataSize == 0) {
221 mBuffer = Bytes();
222 return;
223 }
224
225 mBuffer = Bytes(nullptr, dataSize);
226 mBuffer.fill([this, dataSize](uint8_t* buf, size_t size) {
227 return mPrefs.getBytes(mKey.c_str(), buf, dataSize);
228 });
229 }
230};
231
232
261 public:
268 NVSFileSystem() : mInitialized(false) {}
269
283 bool begin(bool formatOnFail = false) {
284 if (mInitialized) return true;
285
286 mInitialized = mPrefs.begin("uniot_files", false);
287 if (!mInitialized) {
288 UNIOT_LOG_ERROR("Failed to open NVS namespace 'uniot_files'");
289 }
290
291 return mInitialized;
292 }
293
300 void end() {
301 if (mInitialized) {
302 mPrefs.end();
303 mInitialized = false;
304 }
305 }
306
323 NVSFile open(const String& path, const String& mode) {
324 if (!mInitialized) {
325 UNIOT_LOG_WARN("NVS not initialized");
326 return NVSFile("", "", mPrefs);
327 }
328 return NVSFile(path, mode, mPrefs);
329 }
330
344 bool remove(const String& path) {
345 if (!mInitialized) {
346 return false;
347 }
348
349 String key = NVSFile::normalizePath(path);
350 return mPrefs.remove(key.c_str());
351 }
352
353 private:
354 Preferences mPrefs;
355 bool mInitialized;
356};
357
358
370extern NVSFileSystem NVSFS;
371
372} // namespace uniot
373
374#else
375#error "NVSFS is only supported on ESP32. Please define UNIOT_USE_NVSFS=1 for ESP32 projects."
376#endif
Definition Bytes.h:38
Definition NVSFS.h:61
size_t write(const uint8_t *data, size_t size)
Write data to the file buffer.
Definition NVSFS.h:112
static String normalizePath(const String &path)
Normalize a file path to a valid NVS key.
Definition NVSFS.h:184
Bytes getBytes()
Get the entire file content as Bytes object.
Definition NVSFS.h:133
void close()
Close the file and commit any pending write operations.
Definition NVSFS.h:154
NVSFile(const String &path, const String &mode, Preferences &prefs)
Constructs an NVSFile object for a specific path and mode.
Definition NVSFS.h:77
NVSFileSystem()
Default constructor.
Definition NVSFS.h:268
bool remove(const String &path)
Remove a file from NVS storage.
Definition NVSFS.h:344
void end()
Deinitialize the NVS file system.
Definition NVSFS.h:300
bool begin(bool formatOnFail=false)
Initialize the NVS file system.
Definition NVSFS.h:283
NVSFile open(const String &path, const String &mode)
Open a file for reading or writing.
Definition NVSFS.h:323
#define UNIOT_LOG_WARN(...)
Log an WARN level message Used for warnings about potentially problematic situations....
Definition Logger.h:247
#define UNIOT_LOG_ERROR(...)
Log an ERROR level message Used for critical errors that may prevent normal operation....
Definition Logger.h:226
Contains all classes and functions related to the Uniot Core.
NVSFileSystem NVSFS
Global NVS file system instance.
Definition NVSFS.cpp:5