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 // Register special section in storage data (outside of State storage)
65 // sectionId - user selected sectionId
66 // offset - storage memory offset - absolute value. Please make sure that it
67 // doesn't overlap with other sections and state section
68 // size - amount of bytes reserved
69 // addCrc - tell if Storage class should add CRC at the end of section
70 // Actual size of section is size + 2 bytes for CRC (if enabled)
71 static bool RegisterSection(int sectionId, int offset, int size,
72 bool addCrc, bool addBackupCopy);
73 // Reads data section. Returns false when size doesn't match and when crc
74 // check failed (if enabled)
75 static bool ReadSection(int sectionId, unsigned char *data, int size);
76 // Writes data section. Returns false when size doesn't match with
77 // registration info
78 static bool WriteSection(int sectionId, const unsigned char *data, int size);
79 // Delete content of section
80 static bool DeleteSection(int sectionId);
81
82 explicit Storage(unsigned int storageStartingOffset = 0,
83 unsigned int availableSize = 0,
84 enum WearLevelingMode = WearLevelingMode::OFF);
85 virtual ~Storage();
86
87 // Changes default state save period time
88 virtual void setStateSavePeriod(uint32_t periodMs);
89
90 virtual void deleteAll();
91
92 void enableChannelNumbers();
93 bool isAddChannelNumbersEnabled() const;
94
102 void setDeleteAllMethodEnabled(bool value);
103
104 protected:
105 virtual bool init();
106 virtual int readStorage(unsigned int address,
107 unsigned char *buf,
108 int size,
109 bool logs = true) = 0;
110 virtual int writeStorage(unsigned int address,
111 const unsigned char *buf,
112 int size) = 0;
113 virtual void eraseSector(unsigned int address, int size);
114 virtual void commit() = 0;
115
116 virtual int updateStorage(unsigned int, const unsigned char *, int);
117
118 virtual bool saveStateAllowed(uint32_t);
119 virtual void scheduleSave(uint32_t delayMs);
120
121 bool registerSection(
122 int sectionId, int offset, int size, bool addCrc, bool addBackupCopy);
123 bool readSection(int sectionId, unsigned char *data, int size);
124 bool writeSection(int sectionId, const unsigned char *data, int size);
125 bool deleteSection(int sectionId);
126
127 static void WriteElementsState();
128
129 const uint32_t storageStartingOffset = 0;
130 const uint32_t availableSize = 0;
131 const enum WearLevelingMode wearLevelingMode = WearLevelingMode::OFF;
132
133 uint32_t saveStatePeriod = 1000;
134 uint32_t lastWriteTimestamp = 0;
135
136 SpecialSectionInfo *firstSectionInfo = nullptr;
137 StateStorageInterface *stateStorage = nullptr;
138
139 static Storage *instance;
140 static Config *configInstance;
141 bool addChannelNumbers = false;
142 bool deleteAllMethodEnabled = true;
143};
144
145#pragma pack(push, 1)
146struct Preamble {
147 unsigned char suplaTag[5];
148 uint16_t version;
149 uint8_t sectionsCount;
150};
151
153 uint8_t type;
154 uint16_t size;
155 uint16_t crc1;
156 uint16_t crc2;
157};
158#pragma pack(pop)
159
160}; // namespace Supla
161
162#endif // SRC_SUPLA_STORAGE_STORAGE_H_
Definition config.h:47
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:574
Definition storage.h:146
Definition storage.h:152