1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
44
45
46
47
50
51
52
53
56
57
60
61
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* ... */
#ifndef FLASH_HPP_
#define FLASH_HPP_
#include "openthread-core-config.h"
#if OPENTHREAD_CONFIG_PLATFORM_FLASH_API_ENABLE
#include <stdint.h>
#include <string.h>
#include <openthread/platform/toolchain.h>
#include "common/debug.hpp"
#include "common/error.hpp"
#include "common/locator.hpp"6 includes
namespace ot {
/* ... */
class Flash : public InstanceLocator
{
public:
/* ... */
explicit Flash(Instance &aInstance)
: InstanceLocator(aInstance)
{
}{...}
/* ... */
void Init(void);
/* ... */
Error Get(uint16_t aKey, int aIndex, uint8_t *aValue, uint16_t *aValueLength) const;
/* ... */
Error Set(uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength);
/* ... */
Error Add(uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength);
/* ... */
Error Delete(uint16_t aKey, int aIndex);
/* ... */
void Wipe(void);
...
private:
static constexpr uint32_t kSwapMarkerSize = 4;
static const uint32_t sSwapActive = 0xbe5cc5ee;
static const uint32_t sSwapInactive = 0xbe5cc5ec;
OT_TOOL_PACKED_BEGIN
class RecordHeader
{
public:
void Init(uint16_t aKey, bool aFirst)
{
mKey = aKey;
mFlags = kFlagsInit & ~kFlagAddBegin;
if (aFirst)
{
mFlags &= ~kFlagFirst;
}{...}
mLength = 0;
mReserved = 0xffff;
}{...}
uint16_t GetKey(void) const { return mKey; }
void SetKey(uint16_t aKey) { mKey = aKey; }
uint16_t GetLength(void) const { return mLength; }
void SetLength(uint16_t aLength) { mLength = aLength; }
uint16_t GetSize(void) const { return sizeof(*this) + ((mLength + 3) & 0xfffc); }
bool IsValid(void) const { return ((mFlags & (kFlagAddComplete | kFlagDelete)) == kFlagDelete); }
bool IsAddBeginSet(void) const { return (mFlags & kFlagAddBegin) == 0; }
void SetAddBeginFlag(void) { mFlags &= ~kFlagAddBegin; }
bool IsAddCompleteSet(void) const { return (mFlags & kFlagAddComplete) == 0; }
void SetAddCompleteFlag(void) { mFlags &= ~kFlagAddComplete; }
bool IsDeleted(void) const { return (mFlags & kFlagDelete) == 0; }
void SetDeleted(void) { mFlags &= ~kFlagDelete; }
bool IsFirst(void) const { return (mFlags & kFlagFirst) == 0; }
void SetFirst(void) { mFlags &= ~kFlagFirst; }
...
private:
static constexpr uint16_t kFlagsInit = 0xffff;
static constexpr uint16_t kFlagAddBegin = 1 << 0;
static constexpr uint16_t kFlagAddComplete = 1 << 1;
static constexpr uint16_t kFlagDelete = 1 << 2;
static constexpr uint16_t kFlagFirst = 1 << 3;
uint16_t mKey;
uint16_t mFlags;
uint16_t mLength;
uint16_t mReserved;...
}{...} OT_TOOL_PACKED_END;
OT_TOOL_PACKED_BEGIN
class Record : public RecordHeader
{
public:
const uint8_t *GetData(void) const { return mData; }
void SetData(const uint8_t *aData, uint16_t aDataLength)
{
OT_ASSERT(aDataLength <= kMaxDataSize);
memcpy(mData, aData, aDataLength);
SetLength(aDataLength);
}{...}
...
private:
static constexpr uint16_t kMaxDataSize = 255;
uint8_t mData[kMaxDataSize];...
}{...} OT_TOOL_PACKED_END;
Error Add(uint16_t aKey, bool aFirst, const uint8_t *aValue, uint16_t aValueLength);
bool DoesValidRecordExist(uint32_t aOffset, uint16_t aKey) const;
void SanitizeFreeSpace(void);
void Swap(void);
uint32_t mSwapSize;
uint32_t mSwapUsed;
uint8_t mSwapIndex;...
}{...};
}{...}
/* ... */
#endif
/* ... */
#endif