forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersistedCounter.h
290 lines (251 loc) · 10.5 KB
/
PersistedCounter.h
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2016-2017 Nest Labs, Inc.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file
*
* @brief
* Class declarations for a monotonically-increasing counter that is periodically
* saved to the provided storage.
*/
#pragma once
#include <lib/core/CHIPEncoding.h>
#include <lib/core/CHIPPersistentStorageDelegate.h>
#include <lib/support/CHIPCounter.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/DefaultStorageKeyAllocator.h>
#include <limits>
namespace chip {
/**
* @class PersistedCounter
*
* @brief
* A class for managing a counter as an integer value intended to persist
* across reboots.
*
* Counter values are always set to start at a multiple of a bootup value
* "epoch".
*
* Example:
*
* - Assuming epoch is 100 via PersistedCounter::Init(_, 100) and GetValue +
* AdvanceValue is called, we get the following outputs:
*
* - Output: 0, 1, 2, 3, 4 <reboot/reinit>
* - Output: 100, 101, 102, 103, 104, 105 <reboot/reinit>
* - Output: 200, 201, 202, ...., 299, 300, 301, 302 <reboot/reinit>
* - Output: 400, 401 ...
*
*/
template <typename T>
class PersistedCounter : public MonotonicallyIncreasingCounter<T>
{
public:
PersistedCounter() : mKey(StorageKeyName::Uninitialized()) {}
~PersistedCounter() override {}
/**
* @brief
* Initialize a PersistedCounter object.
*
* @param[in] aStorage the storage to use for the counter values.
* @param[in] aKey the key to use for storing the counter values.
* @param[in] aEpoch On bootup, values we vend will start at a
* multiple of this parameter.
*
* @return CHIP_ERROR_INVALID_ARGUMENT if aStorageDelegate or aKey is NULL
* CHIP_ERROR_INVALID_INTEGER_VALUE if aEpoch is 0.
* CHIP_NO_ERROR otherwise
*/
CHIP_ERROR Init(PersistentStorageDelegate * aStorage, StorageKeyName aKey, T aEpoch)
{
VerifyOrReturnError(aStorage != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(aKey.IsInitialized(), CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(aEpoch > 0, CHIP_ERROR_INVALID_INTEGER_VALUE);
mStorage = aStorage;
mKey = aKey;
mEpoch = aEpoch;
T startValue;
// Read our previously-stored starting value.
ReturnErrorOnFailure(ReadStartValue(startValue));
#if CHIP_CONFIG_PERSISTED_COUNTER_DEBUG_LOGGING
if constexpr (std::is_same_v<decltype(startValue), uint64_t>)
{
ChipLogDetail(EventLogging, "PersistedCounter::Init() aEpoch 0x" ChipLogFormatX64 " startValue 0x" ChipLogFormatX64,
ChipLogValueX64(aEpoch), ChipLogValueX64(startValue));
}
else if (std::is_same_v<decltype(startValue), uint32_t>)
{
ChipLogDetail(EventLogging, "PersistedCounter::Init() aEpoch 0x%" PRIx32 " startValue 0x%" PRIx32,
static_cast<uint32_t>(aEpoch), static_cast<uint32_t>(startValue));
}
#endif
ReturnErrorOnFailure(PersistNextEpochStart(static_cast<T>(startValue + aEpoch)));
// This will set the starting value, after which we're ready.
return MonotonicallyIncreasingCounter<T>::Init(startValue);
}
/**
* @brief Increment the counter by N and write to persisted storage if we've completed the current epoch.
*
* @param value value of N
*
* @return Any error returned by a write to persisted storage.
*/
CHIP_ERROR AdvanceBy(T value) override
{
VerifyOrReturnError(mStorage != nullptr, CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(mKey.IsInitialized(), CHIP_ERROR_INCORRECT_STATE);
// If value is 0, we do not need to do anything
VerifyOrReturnError(value > 0, CHIP_NO_ERROR);
// We should update the persisted epoch value if :
// 1- Sum of the current counter and value is greater or equal to the mNextEpoch.
// This is the standard operating case.
// 2- Increasing the current counter by value would cause a roll over. This would cause the current value to be < to the
// mNextEpoch so we force an update.
bool shouldDoEpochUpdate = ((MonotonicallyIncreasingCounter<T>::GetValue() + value) >= mNextEpoch) ||
(MonotonicallyIncreasingCounter<T>::GetValue() > std::numeric_limits<T>::max() - value);
ReturnErrorOnFailure(MonotonicallyIncreasingCounter<T>::AdvanceBy(value));
if (shouldDoEpochUpdate)
{
// Since AdvanceBy allows the counter to be increased by an arbitrary value, it is possible that the new counter value
// is greater than mNextEpoch + mEpoch. As such, we want the next Epoch value to be calculated from the new current
// value.
PersistAndVerifyNextEpochStart(MonotonicallyIncreasingCounter<T>::GetValue());
}
return CHIP_NO_ERROR;
}
/**
* @brief Increment the counter and write to persisted storage if we've completed the current epoch.
*
* @return Any error returned by a write to persisted storage.
*/
CHIP_ERROR Advance() override
{
VerifyOrReturnError(mStorage != nullptr, CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(mKey.IsInitialized(), CHIP_ERROR_INCORRECT_STATE);
ReturnErrorOnFailure(MonotonicallyIncreasingCounter<T>::Advance());
if (MonotonicallyIncreasingCounter<T>::GetValue() >= mNextEpoch)
{
ReturnErrorOnFailure(PersistAndVerifyNextEpochStart(mNextEpoch));
}
return CHIP_NO_ERROR;
}
private:
CHIP_ERROR PersistAndVerifyNextEpochStart(T refEpoch)
{
// Value advanced past the previously persisted "start point".
// Ensure that a new starting point is persisted.
ReturnErrorOnFailure(PersistNextEpochStart(static_cast<T>(refEpoch + mEpoch)));
// Advancing the epoch should have ensured that the current value is valid
VerifyOrReturnError(static_cast<T>(MonotonicallyIncreasingCounter<T>::GetValue() + mEpoch) == mNextEpoch,
CHIP_ERROR_INTERNAL);
// Previous check did not take into consideration that the counter value can be equal to the max counter value or
// rollover.
// TODO(#33175): PersistedCounter allows rollover so this check is incorrect. We need a Counter class that adequatly
// manages rollover behavior for counters that cannot rollover.
// VerifyOrReturnError(MonotonicallyIncreasingCounter<T>::GetValue() < mNextEpoch, CHIP_ERROR_INTERNAL);
return CHIP_NO_ERROR;
}
/**
* @brief
* Write out the counter value to persistent storage.
*
* @param[in] aStartValue The counter value to write out.
*
* @return Any error returned by a write to persistent storage.
*/
CHIP_ERROR
PersistNextEpochStart(T aStartValue)
{
mNextEpoch = aStartValue;
#if CHIP_CONFIG_PERSISTED_COUNTER_DEBUG_LOGGING
if constexpr (std::is_same_v<decltype(aStartValue), uint64_t>)
{
ChipLogDetail(EventLogging, "PersistedCounter::WriteStartValue() aStartValue 0x" ChipLogFormatX64,
ChipLogValueX64(aStartValue));
}
else
{
ChipLogDetail(EventLogging, "PersistedCounter::WriteStartValue() aStartValue 0x%" PRIx32,
static_cast<uint32_t>(aStartValue));
}
#endif
T valueLE = Encoding::LittleEndian::HostSwap<T>(aStartValue);
return mStorage->SyncSetKeyValue(mKey.KeyName(), &valueLE, sizeof(valueLE));
}
/**
* @brief
* Read our starting counter value (if we have one) in from persistent storage.
*
* @param[in,out] aStartValue The value read out.
*
* @return Any error returned by a read from persistent storage.
*/
CHIP_ERROR ReadStartValue(T & aStartValue)
{
T valueLE = GetInitialCounterValue();
uint16_t size = sizeof(valueLE);
// clang-tidy claims that we're returning without writing to 'aStartValue',
// assign 0 to supppress the warning. In case of error, the value wont't be
// used anyway.
aStartValue = 0;
VerifyOrReturnError(mKey.IsInitialized(), CHIP_ERROR_INCORRECT_STATE);
CHIP_ERROR err = mStorage->SyncGetKeyValue(mKey.KeyName(), &valueLE, size);
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
{
// No previously-stored value, no worries, the counter is initialized to zero.
// Suppress the error.
err = CHIP_NO_ERROR;
}
else
{
// TODO: Figure out how to avoid a bootloop here. Maybe we should just
// init to 0? Or a random value?
ReturnErrorOnFailure(err);
}
if (size != sizeof(valueLE))
{
// TODO: Again, figure out whether this could lead to bootloops.
return CHIP_ERROR_INCORRECT_STATE;
}
aStartValue = Encoding::LittleEndian::HostSwap<T>(valueLE);
#if CHIP_CONFIG_PERSISTED_COUNTER_DEBUG_LOGGING
if constexpr (std::is_same_v<decltype(aStartValue), uint64_t>)
{
ChipLogDetail(EventLogging, "PersistedCounter::ReadStartValue() aStartValue 0x" ChipLogFormatX64,
ChipLogValueX64(aStartValue));
}
else
{
ChipLogDetail(EventLogging, "PersistedCounter::ReadStartValue() aStartValue 0x%" PRIx32,
static_cast<uint32_t>(aStartValue));
}
#endif
return CHIP_NO_ERROR;
}
/**
* @brief Get the Initial Counter Value
*
* By default, persisted counters start off at 0.
*/
virtual inline T GetInitialCounterValue() { return 0; }
PersistentStorageDelegate * mStorage = nullptr; // start value is stored here
StorageKeyName mKey;
T mEpoch = 0; // epoch modulus value
T mNextEpoch = 0; // next epoch start
};
} // namespace chip