supla-device
Toggle main menu visibility
Loading...
Searching...
No Matches
src
supla
storage
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
32
namespace
Supla {
33
34
class
Config
;
35
class
SpecialSectionInfo
;
36
class
StateStorageInterface
;
37
38
class
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);
62
static
void
ScheduleSave
(uint32_t delayMsMax, uint32_t delayMsMin = 0);
63
static
bool
IsStateStorageValid();
64
static
void
LoadStateStorage();
65
static
void
WriteStateStorage();
66
67
static
bool
ReadState(
unsigned
char
*,
int
);
68
static
bool
WriteState(
const
unsigned
char
*,
int
);
69
70
static
void
EraseSector(
unsigned
int
offset,
int
size);
71
72
// Register special section in storage data (outside of State storage)
73
// sectionId - user selected sectionId
74
// offset - storage memory offset - absolute value. Please make sure that it
75
// doesn't overlap with other sections and state section
76
// size - amount of bytes reserved
77
// addCrc - tell if Storage class should add CRC at the end of section
78
// Actual size of section is size + 2 bytes for CRC (if enabled)
79
static
bool
RegisterSection(
int
sectionId,
int
offset,
int
size,
80
bool
addCrc,
bool
addBackupCopy);
81
// Reads data section. Returns false when size doesn't match and when crc
82
// check failed (if enabled)
83
static
bool
ReadSection(
int
sectionId,
unsigned
char
*data,
int
size);
84
// Writes data section. Returns false when size doesn't match with
85
// registration info
86
static
bool
WriteSection(
int
sectionId,
const
unsigned
char
*data,
int
size);
87
// Delete content of section
88
static
bool
DeleteSection(
int
sectionId);
89
90
explicit
Storage(
unsigned
int
storageStartingOffset = 0,
91
unsigned
int
availableSize = 0,
92
enum
WearLevelingMode = WearLevelingMode::OFF);
93
virtual
~Storage();
94
95
// Changes default state save period time
96
virtual
void
setStateSavePeriod(uint32_t periodMs);
97
98
virtual
void
deleteAll();
99
virtual
void
eraseSector(
unsigned
int
address,
int
size);
100
101
void
enableChannelNumbers();
102
bool
isAddChannelNumbersEnabled()
const
;
103
111
void
setDeleteAllMethodEnabled
(
bool
value);
112
113
protected
:
114
virtual
bool
init();
115
bool
getInitResult()
const
;
116
virtual
int
readStorage(
unsigned
int
address,
117
unsigned
char
*buf,
118
unsigned
int
size,
119
bool
logs =
true
) = 0;
120
virtual
int
writeStorage(
unsigned
int
address,
121
const
unsigned
char
*buf,
122
unsigned
int
size) = 0;
123
virtual
void
commit() = 0;
124
125
virtual
int
updateStorage(
unsigned
int
,
const
unsigned
char
*,
int
);
126
127
virtual
bool
saveStateAllowed(uint32_t);
128
virtual
void
scheduleSave(uint32_t delayMsMax, uint32_t delayMsMin);
129
130
bool
registerSection(
131
int
sectionId,
int
offset,
int
size,
bool
addCrc,
bool
addBackupCopy);
132
bool
readSection(
int
sectionId,
unsigned
char
*data,
int
size);
133
bool
writeSection(
int
sectionId,
const
unsigned
char
*data,
int
size);
134
bool
deleteSection(
int
sectionId);
135
136
static
void
WriteElementsState();
137
138
const
uint32_t storageStartingOffset = 0;
139
const
uint32_t availableSize = 0;
140
const
enum
WearLevelingMode wearLevelingMode = WearLevelingMode::OFF;
141
142
uint32_t saveStatePeriod = 1000;
143
uint32_t lastWriteTimestamp = 0;
144
145
SpecialSectionInfo
*firstSectionInfo =
nullptr
;
146
StateStorageInterface *stateStorage =
nullptr
;
147
148
static
Storage *instance;
149
static
Config
*configInstance;
150
bool
addChannelNumbers =
false
;
151
bool
deleteAllMethodEnabled =
true
;
152
bool
initResult =
false
;
153
};
154
155
#pragma pack(push, 1)
156
struct
Preamble
{
157
unsigned
char
suplaTag[5];
158
uint16_t version;
159
uint8_t sectionsCount;
160
};
161
162
struct
SectionPreamble
{
163
uint8_t type;
164
uint16_t size;
165
uint16_t crc1;
166
uint16_t crc2;
167
};
168
#pragma pack(pop)
169
170
};
// namespace Supla
171
172
#endif
// SRC_SUPLA_STORAGE_STORAGE_H_
Supla::Config
Definition
config.h:59
Supla::SpecialSectionInfo
Definition
storage.cpp:34
Supla::StateStorageInterface
Definition
state_storage_interface.h:29
Supla::Storage::setDeleteAllMethodEnabled
void setDeleteAllMethodEnabled(bool value)
Enables or disables delete all method which is called during factory reset.
Definition
storage.cpp:591
Supla::Storage::ScheduleSave
static void ScheduleSave(uint32_t delayMsMax, uint32_t delayMsMin=0)
Schedules save of state storage in given time range.
Definition
storage.cpp:108
Supla::Preamble
Definition
storage.h:156
Supla::SectionPreamble
Definition
storage.h:162
Generated by
1.17.0