@@ -72,7 +72,9 @@ void ICDManager::Init(PersistentStorageDelegate * storage, FabricTable * fabricT
72
72
mExchangeManager = exchangeManager;
73
73
mSubManager = manager;
74
74
75
- VerifyOrDie (InitCounter () == CHIP_NO_ERROR);
75
+ VerifyOrDie (ICDConfigurationData::GetInstance ().GetICDCounter ().Init (mStorage , DefaultStorageKeyAllocator::ICDCheckInCounter (),
76
+ ICDConfigurationData::kICDCounterPersistenceIncrement ) ==
77
+ CHIP_NO_ERROR);
76
78
77
79
UpdateICDMode ();
78
80
UpdateOperationState (OperationalState::IdleMode);
@@ -115,7 +117,8 @@ void ICDManager::SendCheckInMsgs()
115
117
#if !CONFIG_BUILD_FOR_HOST_UNIT_TEST
116
118
VerifyOrDie (mStorage != nullptr );
117
119
VerifyOrDie (mFabricTable != nullptr );
118
- uint32_t counter = ICDConfigurationData::GetInstance ().GetICDCounter ();
120
+
121
+ uint32_t counterValue = ICDConfigurationData::GetInstance ().GetICDCounter ().GetNextCheckInCounterValue ();
119
122
bool counterIncremented = false ;
120
123
121
124
for (const auto & fabricInfo : *mFabricTable )
@@ -156,7 +159,7 @@ void ICDManager::SendCheckInMsgs()
156
159
{
157
160
counterIncremented = true ;
158
161
159
- if (CHIP_NO_ERROR != IncrementCounter ())
162
+ if (CHIP_NO_ERROR != ICDConfigurationData::GetInstance (). GetICDCounter (). Advance ())
160
163
{
161
164
ChipLogError (AppServer, " Incremented ICDCounter but failed to access/save to Persistent storage" );
162
165
}
@@ -167,7 +170,7 @@ void ICDManager::SendCheckInMsgs()
167
170
ICDCheckInSender * sender = mICDSenderPool .CreateObject (mExchangeManager );
168
171
VerifyOrReturn (sender != nullptr , ChipLogError (AppServer, " Failed to allocate ICDCheckinSender" ));
169
172
170
- if (CHIP_NO_ERROR != sender->RequestResolve (entry, mFabricTable , counter ))
173
+ if (CHIP_NO_ERROR != sender->RequestResolve (entry, mFabricTable , counterValue ))
171
174
{
172
175
ChipLogError (AppServer, " Failed to send ICD Check-In" );
173
176
}
@@ -176,54 +179,6 @@ void ICDManager::SendCheckInMsgs()
176
179
#endif // CONFIG_BUILD_FOR_HOST_UNIT_TEST
177
180
}
178
181
179
- CHIP_ERROR ICDManager::InitCounter ()
180
- {
181
- CHIP_ERROR err;
182
- uint32_t temp;
183
- uint16_t size = static_cast <uint16_t >(sizeof (uint32_t ));
184
-
185
- err = mStorage ->SyncGetKeyValue (DefaultStorageKeyAllocator::ICDCheckInCounter ().KeyName (), &temp, size);
186
- if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
187
- {
188
- // First time retrieving the counter
189
- temp = chip::Crypto::GetRandU32 ();
190
- }
191
- else if (err != CHIP_NO_ERROR)
192
- {
193
- return err;
194
- }
195
-
196
- ICDConfigurationData::GetInstance ().SetICDCounter (temp);
197
- temp += ICDConfigurationData::ICD_CHECK_IN_COUNTER_MIN_INCREMENT;
198
-
199
- // Increment the count directly to minimize flash write.
200
- return mStorage ->SyncSetKeyValue (DefaultStorageKeyAllocator::ICDCheckInCounter ().KeyName (), &temp, size);
201
- }
202
-
203
- CHIP_ERROR ICDManager::IncrementCounter ()
204
- {
205
- uint32_t temp = 0 ;
206
- StorageKeyName key = DefaultStorageKeyAllocator::ICDCheckInCounter ();
207
- uint16_t size = static_cast <uint16_t >(sizeof (uint32_t ));
208
-
209
- ICDConfigurationData::GetInstance ().mICDCounter ++;
210
-
211
- if (mStorage == nullptr )
212
- {
213
- return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
214
- }
215
-
216
- ReturnErrorOnFailure (mStorage ->SyncGetKeyValue (key.KeyName (), &temp, size));
217
-
218
- if (temp == ICDConfigurationData::GetInstance ().mICDCounter )
219
- {
220
- temp = ICDConfigurationData::GetInstance ().mICDCounter + ICDConfigurationData::ICD_CHECK_IN_COUNTER_MIN_INCREMENT;
221
- return mStorage ->SyncSetKeyValue (key.KeyName (), &temp, size);
222
- }
223
-
224
- return CHIP_NO_ERROR;
225
- }
226
-
227
182
void ICDManager::UpdateICDMode ()
228
183
{
229
184
assertChipStackLockedByCurrentThread ();
0 commit comments