supla-device
Loading...
Searching...
No Matches
state_wear_leveling_sector.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_SECTOR_H_
5#define SRC_SUPLA_STORAGE_STATE_WEAR_LEVELING_SECTOR_H_
6
7/*
8 Class used for writing state data storage with wear leveling in sector mode.
9 "Sector mode" should be used for memory where write requires earlier
10 "sector erase" operation (like flash).
11
12 Storage is organized in following way:
13 First sector 4096 B:
14 1. Preamble (8 B)
15 2. State storage section preamble (7 B)
16 3. StateWlSectorConfig with size of state data and crc (4 B)
17 4. All remmaining bytes are used as bitmap to store last written state
18 slot number.
19 Second sector 4096 B:
20 Backup copy of first sector
21 Third and next sectors:
22 Array of state data slots with:
23 - StateWlSectorHeader (2 B) (crc)
24 - State data (size depends on used Element configuration)
25
26 On each write, new state data is written to next free slot. If it will not
27 fit in current sector, the next sector is erased and data is written to
28 current sector and next one. After each save, state bitmap is updated by
29 writing bit 0 to next bit in StateBitmapAddress, and then the same is written
30 to backup copy of first sector.
31
32 Count of zeroed bits in StateBitmapAddress is a number of last written state
33 slot.
34
35 */
36
37#include "state_storage_interface.h"
38
39namespace Supla {
40
41#pragma pack(push, 1)
43 uint16_t stateSlotSize;
44 uint16_t crc;
45};
46// StateWlSectorHeader is stored at the begining of each state data slot.
47// It contain crc of state data that follows it.
49 uint16_t crc;
50};
51#pragma pack(pop)
52
53struct SectionPreamble;
54
56 public:
57 enum class State : uint8_t {
58 NONE,
59 ERROR,
60 SIZE_CHECK,
61 WRITE,
62 LOAD
63 };
64
65 explicit StateWearLevelingSector(Storage *storage,
66 uint32_t offset, // address of StateWlSectorConfig in
67 // first sector
68 uint32_t availableSize);
70
71 bool loadPreambles(uint32_t storageStartingOffset, uint16_t size) override;
72 void initSectionPreamble(SectionPreamble *preamble) override;
73 bool writeSectionPreamble() override;
74 bool initFromStorage() override;
75 void deleteAll() override;
76 bool prepareSaveState() override;
77 bool prepareSizeCheck() override;
78 bool prepareLoadState() override;
79 bool readState(unsigned char *, int) override;
80 bool writeState(const unsigned char *, int) override;
81 bool finalizeSaveState() override;
82 bool finalizeSizeCheck() override;
83 bool finalizeLoadState() override;
84 void notifyUpdate() override;
85
86 protected:
87 virtual uint16_t getSectorSize() const;
88
89 private:
90 uint16_t getSizeValue(uint16_t availableSize) override;
91 bool tryLoadPreamblesFrom(uint32_t offset);
92 bool isDataDifferent(uint32_t address, const uint8_t *data, uint32_t size);
93 bool isSlotValid(uint32_t address, uint8_t *buffer);
94 uint32_t getPhysicalSlotCount() const;
95 uint32_t getMaxBitmapSlotCount() const;
96 int getSlotSize() const;
97 uint32_t getFirstSlotAddress() const;
98 uint32_t getNextSlotAddress(uint32_t slotAddress) const;
99 uint32_t getPreviousSlotAddress(uint32_t slotAddress) const;
100 uint16_t slotSize() const;
101 uint32_t updateStateEntryAddress();
102 uint32_t sectionOffset = 0;
103 uint32_t availableSize;
104 uint16_t elementStateSize = 0xFFFF;
105 uint16_t stateSlotNewSize = 0;
106 uint32_t currentStateBufferOffset = 0;
107 uint16_t crc = 0; // value calculated on each save/read
108 bool elementStateCrcCValid = false;
109 bool storageStateOk = false;
110 bool initDone = false;
111 bool currentSlotPreparedForFirstWrite = false;
112 int repeatBeforeSwitchToAnotherSlot = 0;
113
114 uint32_t currentSlotAddress = 0;
115 uint32_t lastStoredSlotAddress = 0;
116 uint32_t lastValidAddress = 0;
117 uint8_t *dataBuffer = nullptr;
118
119 State state = State::NONE;
120};
121
122} // namespace Supla
123
124#endif // SRC_SUPLA_STORAGE_STATE_WEAR_LEVELING_SECTOR_H_
Definition state_storage_interface.h:14
Definition state_wear_leveling_sector.h:55
Definition storage.h:23
Definition storage.h:147
Definition state_wear_leveling_sector.h:42
Definition state_wear_leveling_sector.h:48