Skip to content

Commit 5335054

Browse files
yunhanw-googlemkardous-silabsrestyled-commits
authored
[ICD] Improve ICDClientStorage (project-chip#36036) (project-chip#36055)
* Improve ICDClientStorage -- when DeleteAllEntries is triggered, and all fabric are removed, we need remove ICDFabricList from persistent storage, if there is at least 1 fabric in table, we would update fabricList vector and persistent storage. -- when fabric does not exist, storeEntry needs to return invalid fabric error, and deleteEntry or deleteAllEntries needs to return no error. -- Add multiple unit tests to cover DeleteAllEntries/StoreEntry/CheckInHandling for multiple fabrics * Update DefaultICDClientStorage.cpp * Update DefaultICDClientStorage.cpp * Update DefaultICDClientStorage.cpp * Update DefaultICDClientStorage.cpp * Update DefaultICDClientStorage.cpp * address comments * Restyled by whitespace * Restyled by clang-format * address comments * address comments and add more tests * Restyled by whitespace * Restyled by clang-format --------- Co-authored-by: mkardous-silabs <84793247+mkardous-silabs@users.noreply.github.com> Co-authored-by: Restyled.io <commits@restyled.io>
1 parent dd6f974 commit 5335054

6 files changed

+531
-8
lines changed

src/app/icd/client/CheckInHandler.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,12 @@ CHIP_ERROR CheckInHandler::Init(Messaging::ExchangeManager * exchangeManager, IC
5252
{
5353
VerifyOrReturnError(exchangeManager != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
5454
VerifyOrReturnError(clientStorage != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
55+
VerifyOrReturnError(delegate != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
56+
VerifyOrReturnError(engine != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
5557
VerifyOrReturnError(mpExchangeManager == nullptr, CHIP_ERROR_INCORRECT_STATE);
5658
VerifyOrReturnError(mpICDClientStorage == nullptr, CHIP_ERROR_INCORRECT_STATE);
59+
VerifyOrReturnError(mpCheckInDelegate == nullptr, CHIP_ERROR_INCORRECT_STATE);
60+
VerifyOrReturnError(mpImEngine == nullptr, CHIP_ERROR_INCORRECT_STATE);
5761

5862
mpExchangeManager = exchangeManager;
5963
mpICDClientStorage = clientStorage;
@@ -113,6 +117,7 @@ CHIP_ERROR CheckInHandler::OnMessageReceived(Messaging::ExchangeContext * ec, co
113117

114118
if (refreshKey)
115119
{
120+
ChipLogProgress(ICD, "Key Refresh is required");
116121
RefreshKeySender * refreshKeySender = mpCheckInDelegate->OnKeyRefreshNeeded(clientInfo, mpICDClientStorage);
117122
if (refreshKeySender == nullptr)
118123
{

src/app/icd/client/DefaultICDClientStorage.cpp

+44-7
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ CHIP_ERROR DefaultICDClientStorage::UpdateFabricList(FabricIndex fabricIndex)
5151

5252
mFabricList.push_back(fabricIndex);
5353

54+
return StoreFabricList();
55+
}
56+
57+
CHIP_ERROR DefaultICDClientStorage::StoreFabricList()
58+
{
5459
Platform::ScopedMemoryBuffer<uint8_t> backingBuffer;
5560
size_t counter = mFabricList.size();
5661
size_t total = kFabricIndexTlvSize * counter + kArrayOverHead;
@@ -68,7 +73,7 @@ CHIP_ERROR DefaultICDClientStorage::UpdateFabricList(FabricIndex fabricIndex)
6873
const auto len = writer.GetLengthWritten();
6974
VerifyOrReturnError(CanCastTo<uint16_t>(len), CHIP_ERROR_BUFFER_TOO_SMALL);
7075

71-
writer.Finalize(backingBuffer);
76+
ReturnErrorOnFailure(writer.Finalize(backingBuffer));
7277
return mpClientInfoStore->SyncSetKeyValue(DefaultStorageKeyAllocator::ICDFabricList().KeyName(), backingBuffer.Get(),
7378
static_cast<uint16_t>(len));
7479
}
@@ -211,6 +216,7 @@ CHIP_ERROR DefaultICDClientStorage::Load(FabricIndex fabricIndex, std::vector<IC
211216
{
212217
size_t count = 0;
213218
ReturnErrorOnFailure(LoadCounter(fabricIndex, count, clientInfoSize));
219+
VerifyOrReturnError(count > 0, CHIP_NO_ERROR);
214220
size_t len = clientInfoSize * count + kArrayOverHead;
215221
Platform::ScopedMemoryBuffer<uint8_t> backingBuffer;
216222
VerifyOrReturnError(CanCastTo<uint16_t>(len), CHIP_ERROR_BUFFER_TOO_SMALL);
@@ -344,8 +350,21 @@ CHIP_ERROR DefaultICDClientStorage::SerializeToTlv(TLV::TLVWriter & writer, cons
344350
return writer.EndContainer(arrayType);
345351
}
346352

353+
bool DefaultICDClientStorage::FabricExists(FabricIndex fabricIndex)
354+
{
355+
for (auto & fabric_idx : mFabricList)
356+
{
357+
if (fabric_idx == fabricIndex)
358+
{
359+
return true;
360+
}
361+
}
362+
return false;
363+
}
364+
347365
CHIP_ERROR DefaultICDClientStorage::StoreEntry(const ICDClientInfo & clientInfo)
348366
{
367+
VerifyOrReturnError(FabricExists(clientInfo.peer_node.GetFabricIndex()), CHIP_ERROR_INVALID_FABRIC_INDEX);
349368
std::vector<ICDClientInfo> clientInfoVector;
350369
size_t clientInfoSize = MaxICDClientInfoSize();
351370
ReturnErrorOnFailure(Load(clientInfo.peer_node.GetFabricIndex(), clientInfoVector, clientInfoSize));
@@ -359,7 +378,6 @@ CHIP_ERROR DefaultICDClientStorage::StoreEntry(const ICDClientInfo & clientInfo)
359378
break;
360379
}
361380
}
362-
363381
clientInfoVector.push_back(clientInfo);
364382
size_t total = clientInfoSize * clientInfoVector.size() + kArrayOverHead;
365383
Platform::ScopedMemoryBuffer<uint8_t> backingBuffer;
@@ -371,7 +389,7 @@ CHIP_ERROR DefaultICDClientStorage::StoreEntry(const ICDClientInfo & clientInfo)
371389
const auto len = writer.GetLengthWritten();
372390
VerifyOrReturnError(CanCastTo<uint16_t>(len), CHIP_ERROR_BUFFER_TOO_SMALL);
373391

374-
writer.Finalize(backingBuffer);
392+
ReturnErrorOnFailure(writer.Finalize(backingBuffer));
375393
ReturnErrorOnFailure(mpClientInfoStore->SyncSetKeyValue(
376394
DefaultStorageKeyAllocator::ICDClientInfoKey(clientInfo.peer_node.GetFabricIndex()).KeyName(), backingBuffer.Get(),
377395
static_cast<uint16_t>(len)));
@@ -416,17 +434,19 @@ CHIP_ERROR DefaultICDClientStorage::UpdateEntryCountForFabric(FabricIndex fabric
416434

417435
const auto len = writer.GetLengthWritten();
418436
VerifyOrReturnError(CanCastTo<uint16_t>(len), CHIP_ERROR_BUFFER_TOO_SMALL);
419-
writer.Finalize(backingBuffer);
437+
ReturnErrorOnFailure(writer.Finalize(backingBuffer));
420438

421439
return mpClientInfoStore->SyncSetKeyValue(DefaultStorageKeyAllocator::FabricICDClientInfoCounter(fabricIndex).KeyName(),
422440
backingBuffer.Get(), static_cast<uint16_t>(len));
423441
}
424442

425443
CHIP_ERROR DefaultICDClientStorage::DeleteEntry(const ScopedNodeId & peerNode)
426444
{
445+
VerifyOrReturnError(FabricExists(peerNode.GetFabricIndex()), CHIP_NO_ERROR);
427446
size_t clientInfoSize = 0;
428447
std::vector<ICDClientInfo> clientInfoVector;
429448
ReturnErrorOnFailure(Load(peerNode.GetFabricIndex(), clientInfoVector, clientInfoSize));
449+
VerifyOrReturnError(clientInfoVector.size() > 0, CHIP_NO_ERROR);
430450

431451
for (auto it = clientInfoVector.begin(); it != clientInfoVector.end(); it++)
432452
{
@@ -440,7 +460,6 @@ CHIP_ERROR DefaultICDClientStorage::DeleteEntry(const ScopedNodeId & peerNode)
440460

441461
ReturnErrorOnFailure(
442462
mpClientInfoStore->SyncDeleteKeyValue(DefaultStorageKeyAllocator::ICDClientInfoKey(peerNode.GetFabricIndex()).KeyName()));
443-
444463
size_t total = clientInfoSize * clientInfoVector.size() + kArrayOverHead;
445464
Platform::ScopedMemoryBuffer<uint8_t> backingBuffer;
446465
ReturnErrorCodeIf(!backingBuffer.Calloc(total), CHIP_ERROR_NO_MEMORY);
@@ -451,7 +470,7 @@ CHIP_ERROR DefaultICDClientStorage::DeleteEntry(const ScopedNodeId & peerNode)
451470
const auto len = writer.GetLengthWritten();
452471
VerifyOrReturnError(CanCastTo<uint16_t>(len), CHIP_ERROR_BUFFER_TOO_SMALL);
453472

454-
writer.Finalize(backingBuffer);
473+
ReturnErrorOnFailure(writer.Finalize(backingBuffer));
455474
ReturnErrorOnFailure(
456475
mpClientInfoStore->SyncSetKeyValue(DefaultStorageKeyAllocator::ICDClientInfoKey(peerNode.GetFabricIndex()).KeyName(),
457476
backingBuffer.Get(), static_cast<uint16_t>(len)));
@@ -461,6 +480,8 @@ CHIP_ERROR DefaultICDClientStorage::DeleteEntry(const ScopedNodeId & peerNode)
461480

462481
CHIP_ERROR DefaultICDClientStorage::DeleteAllEntries(FabricIndex fabricIndex)
463482
{
483+
VerifyOrReturnError(FabricExists(fabricIndex), CHIP_NO_ERROR);
484+
464485
size_t clientInfoSize = 0;
465486
std::vector<ICDClientInfo> clientInfoVector;
466487
ReturnErrorOnFailure(Load(fabricIndex, clientInfoVector, clientInfoSize));
@@ -471,7 +492,23 @@ CHIP_ERROR DefaultICDClientStorage::DeleteAllEntries(FabricIndex fabricIndex)
471492
}
472493
ReturnErrorOnFailure(
473494
mpClientInfoStore->SyncDeleteKeyValue(DefaultStorageKeyAllocator::ICDClientInfoKey(fabricIndex).KeyName()));
474-
return mpClientInfoStore->SyncDeleteKeyValue(DefaultStorageKeyAllocator::FabricICDClientInfoCounter(fabricIndex).KeyName());
495+
ReturnErrorOnFailure(
496+
mpClientInfoStore->SyncDeleteKeyValue(DefaultStorageKeyAllocator::FabricICDClientInfoCounter(fabricIndex).KeyName()));
497+
498+
for (auto fabric = mFabricList.begin(); fabric != mFabricList.end(); fabric++)
499+
{
500+
if (*fabric == fabricIndex)
501+
{
502+
mFabricList.erase(fabric);
503+
break;
504+
}
505+
}
506+
507+
if (mFabricList.size() == 0)
508+
{
509+
return mpClientInfoStore->SyncDeleteKeyValue(DefaultStorageKeyAllocator::ICDFabricList().KeyName());
510+
}
511+
return StoreFabricList();
475512
}
476513

477514
CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo,

src/app/icd/client/DefaultICDClientStorage.h

+9
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ class DefaultICDClientStorage : public ICDClientStorage
120120
CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo,
121121
Protocols::SecureChannel::CounterType & counter) override;
122122

123+
#if CONFIG_BUILD_FOR_HOST_UNIT_TEST
124+
size_t GetFabricListSize() { return mFabricList.size(); }
125+
126+
PersistentStorageDelegate * GetClientInfoStore() { return mpClientInfoStore; }
127+
#endif // CONFIG_BUILD_FOR_HOST_UNIT_TEST
128+
123129
protected:
124130
enum class ClientInfoTag : uint8_t
125131
{
@@ -173,9 +179,12 @@ class DefaultICDClientStorage : public ICDClientStorage
173179

174180
private:
175181
friend class ICDClientInfoIteratorImpl;
182+
CHIP_ERROR StoreFabricList();
176183
CHIP_ERROR LoadFabricList();
177184
CHIP_ERROR LoadCounter(FabricIndex fabricIndex, size_t & count, size_t & clientInfoSize);
178185

186+
bool FabricExists(FabricIndex fabricIndex);
187+
179188
CHIP_ERROR IncreaseEntryCountForFabric(FabricIndex fabricIndex);
180189
CHIP_ERROR DecreaseEntryCountForFabric(FabricIndex fabricIndex);
181190
CHIP_ERROR UpdateEntryCountForFabric(FabricIndex fabricIndex, bool increase);

src/app/tests/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ chip_test_suite("tests") {
194194
"TestBasicCommandPathRegistry.cpp",
195195
"TestBindingTable.cpp",
196196
"TestBuilderParser.cpp",
197+
"TestCheckInHandler.cpp",
197198
"TestCommandHandlerInterfaceRegistry.cpp",
198199
"TestCommandInteraction.cpp",
199200
"TestCommandPathParams.cpp",
@@ -236,6 +237,7 @@ chip_test_suite("tests") {
236237
"${chip_root}/src/app",
237238
"${chip_root}/src/app/codegen-data-model-provider:instance-header",
238239
"${chip_root}/src/app/common:cluster-objects",
240+
"${chip_root}/src/app/icd/client:handler",
239241
"${chip_root}/src/app/icd/client:manager",
240242
"${chip_root}/src/app/tests:helpers",
241243
"${chip_root}/src/app/util/mock:mock_codegen_data_model",

0 commit comments

Comments
 (0)