supla-device
Loading...
Searching...
No Matches
storage.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19#ifndef SRC_SUPLA_STORAGE_STORAGE_H_
20#define SRC_SUPLA_STORAGE_STORAGE_H_
21
22#include <stdint.h>
23
24#define SUPLA_STORAGE_VERSION 1
25
26#define STORAGE_SECTION_TYPE_DEVICE_CONFIG 1
27#define STORAGE_SECTION_TYPE_ELEMENT_CONFIG 2
28#define STORAGE_SECTION_TYPE_ELEMENT_STATE 3
29#define STORAGE_SECTION_TYPE_ELEMENT_STATE_WL_BYTE 4
30#define STORAGE_SECTION_TYPE_ELEMENT_STATE_WL_SECTOR 5
31
32namespace Supla {
33
34class Config;
37
38class Storage {
39 public:
40 static bool storageInitDone;
41
42 enum class WearLevelingMode {
43 OFF = 0,
44 BYTE_WRITE_MODE = 1, // Used i.e. for EEPROM memory
45 SECTOR_WRITE_MODE = 2 // Used i.e. for FLASH memory
46 };
47
48 friend class StateStorageInterface;
49 static Storage *Instance();
50 static Config *ConfigInstance();
51 static void SetConfigInstance(Config *instance);
52 static bool IsConfigStorageAvailable();
53
54 static bool Init();
55 static bool SaveStateAllowed(uint32_t);
56 static void ScheduleSave(uint32_t delayMs);
57 static bool IsStateStorageValid();
58 static void LoadStateStorage();
59 static void WriteStateStorage();
60
61 static bool ReadState(unsigned char *, int);
62 static bool WriteState(const unsigned char *, int);
63
64 static void EraseSector(unsigned int offset, int size);
65
66 // Register special section in storage data (outside of State storage)
67 // sectionId - user selected sectionId
68 // offset - storage memory offset - absolute value. Please make sure that it
69 // doesn't overlap with other sections and state section
70 // size - amount of bytes reserved
71 // addCrc - tell if Storage class should add CRC at the end of section
72 // Actual size of section is size + 2 bytes for CRC (if enabled)
73 static bool RegisterSection(int sectionId, int offset, int size,
74 bool addCrc, bool addBackupCopy);
75 // Reads data section. Returns false when size doesn't match and when crc
76 // check failed (if enabled)
77 static bool ReadSection(int sectionId, unsigned char *data, int size);
78 // Writes data section. Returns false when size doesn't match with
79 // registration info
80 static bool WriteSection(int sectionId, const unsigned char *data, int size);
81 // Delete content of section
82 static bool DeleteSection(int sectionId);
83
84 explicit Storage(unsigned int storageStartingOffset = 0,
85 unsigned int availableSize = 0,
86 enum WearLevelingMode = WearLevelingMode::OFF);
87 virtual ~Storage();
88
89 // Changes default state save period time
90 virtual void setStateSavePeriod(uint32_t periodMs);
91
92 virtual void deleteAll();
93 virtual void eraseSector(unsigned int address, int size);
94
95 void enableChannelNumbers();
96 bool isAddChannelNumbersEnabled() const;
97
105 void setDeleteAllMethodEnabled(bool value);
106
107 protected:
108 virtual bool init();
109 virtual int readStorage(unsigned int address,
110 unsigned char *buf,
111 int size,
112 bool logs = true) = 0;
113 virtual int writeStorage(unsigned int address,
114 const unsigned char *buf,
115 int size) = 0;
116 virtual void commit() = 0;
117
118 virtual int updateStorage(unsigned int, const unsigned char *, int);
119
120 virtual bool saveStateAllowed(uint32_t);
121 virtual void scheduleSave(uint32_t delayMs);
122
123 bool registerSection(
124 int sectionId, int offset, int size, bool addCrc, bool addBackupCopy);
125 bool readSection(int sectionId, unsigned char *data, int size);
126 bool writeSection(int sectionId, const unsigned char *data, int size);
127 bool deleteSection(int sectionId);
128
129 static void WriteElementsState();
130
131 const uint32_t storageStartingOffset = 0;
132 const uint32_t availableSize = 0;
133 const enum WearLevelingMode wearLevelingMode = WearLevelingMode::OFF;
134
135 uint32_t saveStatePeriod = 1000;
136 uint32_t lastWriteTimestamp = 0;
137
138 SpecialSectionInfo *firstSectionInfo = nullptr;
139 StateStorageInterface *stateStorage = nullptr;
140
141 static Storage *instance;
142 static Config *configInstance;
143 bool addChannelNumbers = false;
144 bool deleteAllMethodEnabled = true;
145};
146
147#pragma pack(push, 1)
148struct Preamble {
149 unsigned char suplaTag[5];
150 uint16_t version;
151 uint8_t sectionsCount;
152};
153
155 uint8_t type;
156 uint16_t size;
157 uint16_t crc1;
158 uint16_t crc2;
159};
160#pragma pack(pop)
161
162}; // namespace Supla
163
164#endif // SRC_SUPLA_STORAGE_STORAGE_H_
Definition config.h:56
Definition storage.cpp:34
Definition state_storage_interface.h:29
void setDeleteAllMethodEnabled(bool value)
Enables or disables delete all method which is called during factory reset.
Definition storage.cpp:580
Definition storage.h:148
Definition storage.h:154