Uniot Core
0.9.0
Loading...
Searching...
No Matches
CBORStorage.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2020 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
19#pragma once
20
21// doc: https://arduino-esp8266.readthedocs.io/en/latest/filesystem.html
22#include <Storage.h>
23#include <CBORObject.h>
24
25namespace uniot
26{
38class CBORStorage : public Storage
39{
40public:
46 CBORStorage(const String &path) : Storage(path)
47 {
48 }
49
53 virtual ~CBORStorage() {}
54
61 {
62 return mCbor;
63 }
64
74 virtual bool store() override
75 {
76 if (mCbor.dirty())
77 {
78 mData = mCbor.build();
79
80 // A map encodes to at least one byte, so an empty build means encoding failed, in
81 // practice an allocation. Writing it would truncate the file, or on NVS remove the
82 // key, so the stored contents are kept and the failure is reported instead.
83 if (!mData.size())
84 {
85 UNIOT_LOG_ERROR("CBOR build failed, %s not stored", mPath.c_str());
86 return false;
87 }
88
89 return Storage::store();
90 }
91 return true;
92 }
93
102 virtual bool restore() override
103 {
104 auto success = Storage::restore();
105 if (success)
106 {
107 mCbor.read(mData);
108 }
109 return success;
110 }
111
120 virtual bool clean() override
121 {
122 mCbor.clean();
123 return Storage::clean();
124 }
125
126protected:
128};
129
130} // namespace uniot
Definition CBORObject.h:40
virtual bool restore() override
Restore the CBOR object from the filesystem.
Definition CBORStorage.h:102
virtual ~CBORStorage()
Virtual destructor.
Definition CBORStorage.h:53
virtual bool clean() override
Clean the CBOR object and remove the storage file.
Definition CBORStorage.h:120
CBORObject mCbor
The CBOR object used for data serialization/deserialization.
Definition CBORStorage.h:127
virtual bool store() override
Store the CBOR object to the filesystem.
Definition CBORStorage.h:74
CBORStorage(const String &path)
Constructs a new CBORStorage object.
Definition CBORStorage.h:46
CBORObject & object()
Get access to the underlying CBORObject.
Definition CBORStorage.h:60
Storage(const String &path)
Constructs a Storage object for a specific file.
Definition Storage.h:74
String mPath
The file path where data is stored.
Definition Storage.h:212
virtual bool clean()
Clears data and removes the file from the file system.
Definition Storage.h:189
Bytes mData
The byte array containing the data to be stored or the loaded data.
Definition Storage.h:205
virtual bool store()
Writes the current data to the file system.
Definition Storage.h:135
virtual bool restore()
Loads data from the file system into memory.
Definition Storage.h:165
#define UNIOT_LOG_ERROR(...)
Log an ERROR level message Used for critical errors that may prevent normal operation....
Definition Logger.h:273
Contains all classes and functions related to the Uniot Core.