supla-device
Loading...
Searching...
No Matches
state_wear_leveling_byte.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_STATE_WEAR_LEVELING_BYTE_H_
5#define SRC_SUPLA_STORAGE_STATE_WEAR_LEVELING_BYTE_H_
6
7/*
8 Class used for writing state data storage with wear leveling in byte mode.
9 "Byte mode" should be used for random access memory types (like EEPROM), where
10 memory allows to write data to any byte without clearing whole sector of
11 memory.
12
13 Storage is organized in following way:
14 1. Preamble (8 B)
15 2. State storage section preamble (7 B)
16 3. StateEntryAddress with address of current element state slot and its own
17 crc (8 B)
18 4. Copy of StateEntryAddress (8 B)
19 5. State data with:
20 - StateWlByteHeader (4 B)
21 - State data (size depends on used Element configuration)
22
23 State data is saved always at slot pointed by StateEntryAddress and to
24 followin slot (next one). It is done in order to ensure that state data
25 will have backup copy.
26 writeCount is incremented each time slot is written.
27 On storage init, state is read from slot pointed by StateEntryAddress
28 or from next slot after it (depending on which writeCount is higher).
29 StateEndryAddress is incremented each time when writeCount is higher
30 than repeatBeforeSwitchToAnotherSlot. Then next slot use writeCount == 1.
31 Slot with writeCount == 1 is considered as newer than slot with
32 writeCount == repeatBeforeSwitchToAnotherSlot.
33
34 repeatBeforeSwitchToAnotherSlot is configured to be equal to number of
35 slots in state storage, rounded up to the next odd number.
36
37 Slots are used in round-robin way.
38
39 Exact sequnce of writes is done in a way that there will be always correct
40 copy of data stored.
41 */
42
43#include "state_storage_interface.h"
44
45namespace Supla {
46
47#pragma pack(push, 1)
48// StateEntryAddress is stored after section preamble in two copies.
49// It contain address of current element state slot and its own crc.
51 uint32_t address;
52 uint16_t elementStateSize;
53 uint16_t crc;
54};
55
56// StateWlByteHeader is stored at the begining of each state data slot.
57// It contain information how many times slot was used for writing and
58// crc of state data that follows it.
60 uint16_t writeCount;
61 uint16_t crc;
62};
63#pragma pack(pop)
64
65struct SectionPreamble;
66
68 public:
69 explicit StateWearLevelingByte(Storage *storage,
70 uint32_t offset);
72
73 void initSectionPreamble(SectionPreamble *preamble) override;
74 bool writeSectionPreamble() override;
75 bool initFromStorage() override;
76 void deleteAll() override;
77 bool prepareSaveState() override;
78 bool prepareSizeCheck() override;
79 bool prepareLoadState() override;
80 bool readState(unsigned char *, int) override;
81 bool writeState(const unsigned char *, int) override;
82 bool finalizeSaveState() override;
83 bool finalizeSizeCheck() override;
84 bool finalizeLoadState() override;
85 void notifyUpdate() override;
86
87 private:
88 uint16_t getSizeValue(uint16_t availableSize) override;
89 void checkIfIsEnoughSpaceForState();
90 bool isStateEntryAddressValid(const StateEntryAddress &entry) const;
91 uint32_t getFirstSlotAddress() const;
92 uint32_t getNextSlotAddress(uint32_t slotAddress) const;
93 uint64_t slotSize() const;
94 uint32_t updateStateEntryAddress();
95 bool isDataDifferent(uint32_t firstAddress, uint32_t secondAddress, int size);
96 uint32_t sectionOffset = 0;
97 uint32_t reservedSize;
98 uint32_t elementStateSize = 0;
99 uint32_t stateSlotNewSize = 0;
100 uint32_t currentStateOffset = 0;
101 uint16_t crc = 0; // value calculated on each save/read
102 bool elementStateCrcCValid = false;
103 bool dryRun = false;
104 bool storageStateOk = false;
105 bool initDone = false;
106 bool dataChanged = false;
107 int repeatBeforeSwitchToAnotherSlot = 0;
108
109 uint16_t writeCount = 0;
110 uint32_t currentSlotAddress = 0;
111};
112
113} // namespace Supla
114#endif // SRC_SUPLA_STORAGE_STATE_WEAR_LEVELING_BYTE_H_
Definition state_storage_interface.h:14
Definition state_wear_leveling_byte.h:67
Definition storage.h:23
Definition storage.h:147
Definition state_wear_leveling_byte.h:50
Definition state_wear_leveling_byte.h:59