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
95 protected:
96 virtual bool init();
97 virtual int readStorage(unsigned int address,
98 unsigned char *buf,
99 int size,
100 bool logs = true) = 0;
101 virtual int writeStorage(unsigned int address,
102 const unsigned char *buf,
103 int size) = 0;
104 virtual void eraseSector(unsigned int address, int size);
105 virtual void commit() = 0;
106
107 virtual int updateStorage(unsigned int, const unsigned char *, int);
108
109 virtual bool saveStateAllowed(uint32_t);
110 virtual void scheduleSave(uint32_t delayMs);
111
112 bool registerSection(
113 int sectionId, int offset, int size, bool addCrc, bool addBackupCopy);
114 bool readSection(int sectionId, unsigned char *data, int size);
115 bool writeSection(int sectionId, const unsigned char *data, int size);
116 bool deleteSection(int sectionId);
117
118 static void WriteElementsState();
119
120 const uint32_t storageStartingOffset = 0;
121 const uint32_t availableSize = 0;
122 const enum WearLevelingMode wearLevelingMode = WearLevelingMode::OFF;
123
124 uint32_t saveStatePeriod = 1000;
125 uint32_t lastWriteTimestamp = 0;
126
127 SpecialSectionInfo *firstSectionInfo = nullptr;
128 StateStorageInterface *stateStorage = nullptr;
129
130 static Storage *instance;
131 static Config *configInstance;
132 bool addChannelNumbers = false;
133};
134
135#pragma pack(push, 1)
136struct Preamble {
137 unsigned char suplaTag[5];
138 uint16_t version;
139 uint8_t sectionsCount;
140};
141
143 uint8_t type;
144 uint16_t size;
145 uint16_t crc1;
146 uint16_t crc2;
147};
148#pragma pack(pop)
149
150}; // namespace Supla
151
152#endif // SRC_SUPLA_STORAGE_STORAGE_H_
Definition config.h:47
Definition storage.cpp:34
Definition state_storage_interface.h:29
Definition storage.h:136
Definition storage.h:142