supla-device
Loading...
Searching...
No Matches
storage.h
1// SPDX-FileCopyrightText: AC SOFTWARE SP. Z O.O.
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#ifndef SRC_SUPLA_STORAGE_STORAGE_H_
5#define SRC_SUPLA_STORAGE_STORAGE_H_
6
7#include <stdint.h>
8
9#define SUPLA_STORAGE_VERSION 1
10
11#define STORAGE_SECTION_TYPE_DEVICE_CONFIG 1
12#define STORAGE_SECTION_TYPE_ELEMENT_CONFIG 2
13#define STORAGE_SECTION_TYPE_ELEMENT_STATE 3
14#define STORAGE_SECTION_TYPE_ELEMENT_STATE_WL_BYTE 4
15#define STORAGE_SECTION_TYPE_ELEMENT_STATE_WL_SECTOR 5
16
17namespace Supla {
18
19class Config;
20class SpecialSectionInfo;
21class StateStorageInterface;
22
23class Storage {
24 public:
25 static bool storageInitDone;
26
27 enum class WearLevelingMode {
28 OFF = 0,
29 BYTE_WRITE_MODE = 1, // Used i.e. for EEPROM memory
30 SECTOR_WRITE_MODE = 2 // Used i.e. for FLASH memory
31 };
32
33 friend class StateStorageInterface;
34 static Storage *Instance();
35 static Config *ConfigInstance();
36 static void SetConfigInstance(Config *instance);
37 static bool IsConfigStorageAvailable();
38
39 static bool Init();
40 static bool SaveStateAllowed(uint32_t);
47 static void ScheduleSave(uint32_t delayMsMax, uint32_t delayMsMin = 0);
48 static bool IsStateStorageValid();
49 static void LoadStateStorage();
50 static void WriteStateStorage();
51
52 static bool ReadState(unsigned char *, int);
53 static bool WriteState(const unsigned char *, int);
54
55 static void EraseSector(unsigned int offset, int size);
56
57 // Register special section in storage data (outside of State storage)
58 // sectionId - user selected sectionId
59 // offset - storage memory offset - absolute value. Please make sure that it
60 // doesn't overlap with other sections and state section
61 // size - amount of bytes reserved
62 // addCrc - tell if Storage class should add CRC at the end of section
63 // Actual size of section is size + 2 bytes for CRC (if enabled)
64 static bool RegisterSection(int sectionId, int offset, int size,
65 bool addCrc, bool addBackupCopy);
66 // Reads data section. Returns false when size doesn't match and when crc
67 // check failed (if enabled)
68 static bool ReadSection(int sectionId, unsigned char *data, int size);
69 // Writes data section. Returns false when size doesn't match with
70 // registration info
71 static bool WriteSection(int sectionId, const unsigned char *data, int size);
72 // Delete content of section
73 static bool DeleteSection(int sectionId);
74
75 explicit Storage(unsigned int storageStartingOffset = 0,
76 unsigned int availableSize = 0,
77 enum WearLevelingMode = WearLevelingMode::OFF);
78 virtual ~Storage();
79
80 // Changes default state save period time
81 virtual void setStateSavePeriod(uint32_t periodMs);
82
83 virtual void deleteAll();
84 virtual void eraseSector(unsigned int address, int size);
85
86 void enableChannelNumbers();
87 bool isAddChannelNumbersEnabled() const;
88
96 void setDeleteAllMethodEnabled(bool value);
97
98 protected:
99 virtual bool init();
100 bool getInitResult() const;
101 virtual int readStorage(unsigned int address,
102 unsigned char *buf,
103 unsigned int size,
104 bool logs = true) = 0;
105 virtual int writeStorage(unsigned int address,
106 const unsigned char *buf,
107 unsigned int size) = 0;
108 virtual void commit() = 0;
109
110 virtual int updateStorage(unsigned int, const unsigned char *, int);
111
112 virtual bool saveStateAllowed(uint32_t);
113 virtual void scheduleSave(uint32_t delayMsMax, uint32_t delayMsMin);
114
115 bool registerSection(
116 int sectionId, int offset, int size, bool addCrc, bool addBackupCopy);
117 bool readSection(int sectionId, unsigned char *data, int size);
118 bool writeSection(int sectionId, const unsigned char *data, int size);
119 bool deleteSection(int sectionId);
120
121 static void WriteElementsState();
122
123 const uint32_t storageStartingOffset = 0;
124 const uint32_t availableSize = 0;
125 const enum WearLevelingMode wearLevelingMode = WearLevelingMode::OFF;
126
127 uint32_t saveStatePeriod = 1000;
128 uint32_t lastWriteTimestamp = 0;
129
130 SpecialSectionInfo *firstSectionInfo = nullptr;
131 StateStorageInterface *stateStorage = nullptr;
132
133 static Storage *instance;
134 static Config *configInstance;
135 bool addChannelNumbers = false;
136 bool deleteAllMethodEnabled = true;
137 bool initResult = false;
138};
139
140#pragma pack(push, 1)
141struct Preamble {
142 unsigned char suplaTag[5];
143 uint16_t version;
144 uint8_t sectionsCount;
145};
146
148 uint8_t type;
149 uint16_t size;
150 uint16_t crc1;
151 uint16_t crc2;
152};
153#pragma pack(pop)
154
155}; // namespace Supla
156
157#endif // SRC_SUPLA_STORAGE_STORAGE_H_
Definition config.h:44
Definition storage.cpp:19
Definition state_storage_interface.h:14
Definition storage.h:23
void setDeleteAllMethodEnabled(bool value)
Enables or disables delete all method which is called during factory reset.
Definition storage.cpp:576
static void ScheduleSave(uint32_t delayMsMax, uint32_t delayMsMin=0)
Schedules save of state storage in given time range.
Definition storage.cpp:93
Definition storage.h:141
Definition storage.h:147