From db92cb1213b8653999f7385a8de55022df42da81 Mon Sep 17 00:00:00 2001 From: yunhanw Date: Mon, 4 Dec 2023 17:31:46 -0800 Subject: [PATCH 1/3] Add ICD getEntry API to retrive the ICDClientInfo --- .../icd/client/DefaultICDClientStorage.cpp | 26 +++++++++++++++++-- src/app/icd/client/DefaultICDClientStorage.h | 6 ++++- src/app/icd/client/ICDClientStorage.h | 23 +++++++++++++--- src/app/tests/TestDefaultICDClientStorage.cpp | 7 +++-- 4 files changed, 53 insertions(+), 9 deletions(-) diff --git a/src/app/icd/client/DefaultICDClientStorage.cpp b/src/app/icd/client/DefaultICDClientStorage.cpp index 47319f6c874659..d4b7947eccab89 100644 --- a/src/app/icd/client/DefaultICDClientStorage.cpp +++ b/src/app/icd/client/DefaultICDClientStorage.cpp @@ -287,6 +287,11 @@ CHIP_ERROR DefaultICDClientStorage::SetKey(ICDClientInfo & clientInfo, const Byt return mpKeyStore->CreateKey(keyMaterial, clientInfo.shared_key); } +void DefaultICDClientStorage::RemoveKey(ICDClientInfo & clientInfo) +{ + mpKeyStore->DestroyKey(clientInfo.shared_key); +} + CHIP_ERROR DefaultICDClientStorage::SerializeToTlv(TLV::TLVWriter & writer, const std::vector & clientInfoVector) { TLV::TLVType arrayType; @@ -385,6 +390,23 @@ CHIP_ERROR DefaultICDClientStorage::UpdateEntryCountForFabric(FabricIndex fabric backingBuffer.Get(), static_cast(len)); } +CHIP_ERROR DefaultICDClientStorage::GetEntry(const ScopedNodeId & peerNode, ICDClientInfo & clientInfo) +{ + size_t clientInfoSize = 0; + std::vector clientInfoVector; + ReturnErrorOnFailure(Load(peerNode.GetFabricIndex(), clientInfoVector, clientInfoSize)); + IgnoreUnusedVariable(clientInfoSize); + for (auto & info : clientInfoVector) + { + if (peerNode.GetNodeId() == info.peer_node.GetNodeId()) + { + clientInfo = info; + return CHIP_NO_ERROR; + } + } + return CHIP_ERROR_NOT_FOUND; +} + CHIP_ERROR DefaultICDClientStorage::DeleteEntry(const ScopedNodeId & peerNode) { size_t clientInfoSize = 0; @@ -395,7 +417,7 @@ CHIP_ERROR DefaultICDClientStorage::DeleteEntry(const ScopedNodeId & peerNode) { if (peerNode.GetNodeId() == it->peer_node.GetNodeId()) { - mpKeyStore->DestroyKey(it->shared_key); + RemoveKey(*it); it = clientInfoVector.erase(it); break; } @@ -430,7 +452,7 @@ CHIP_ERROR DefaultICDClientStorage::DeleteAllEntries(FabricIndex fabricIndex) IgnoreUnusedVariable(clientInfoSize); for (auto & clientInfo : clientInfoVector) { - mpKeyStore->DestroyKey(clientInfo.shared_key); + RemoveKey(clientInfo); } ReturnErrorOnFailure( mpClientInfoStore->SyncDeleteKeyValue(DefaultStorageKeyAllocator::ICDClientInfoKey(fabricIndex).KeyName())); diff --git a/src/app/icd/client/DefaultICDClientStorage.h b/src/app/icd/client/DefaultICDClientStorage.h index adc8c69113a700..d175700cecad06 100644 --- a/src/app/icd/client/DefaultICDClientStorage.h +++ b/src/app/icd/client/DefaultICDClientStorage.h @@ -56,9 +56,13 @@ class DefaultICDClientStorage : public ICDClientStorage CHIP_ERROR SetKey(ICDClientInfo & clientInfo, const ByteSpan keyData) override; + void RemoveKey(ICDClientInfo & clientInfo) override; + CHIP_ERROR StoreEntry(const ICDClientInfo & clientInfo) override; - CHIP_ERROR DeleteEntry(const ScopedNodeId & peerNodeId) override; + CHIP_ERROR GetEntry(const ScopedNodeId & peerNode, ICDClientInfo & clientInfo) override; + + CHIP_ERROR DeleteEntry(const ScopedNodeId & peerNode) override; CHIP_ERROR DeleteAllEntries(FabricIndex fabricIndex) override; diff --git a/src/app/icd/client/ICDClientStorage.h b/src/app/icd/client/ICDClientStorage.h index 4df2c961260104..1d3e8edceec9f1 100644 --- a/src/app/icd/client/ICDClientStorage.h +++ b/src/app/icd/client/ICDClientStorage.h @@ -52,7 +52,7 @@ class ICDClientStorage * Called during ICD device registration in commissioning, commissioner/controller * provides raw key data, the shared key handle in clientInfo is updated based upon raw key data * - * @param[inout] aICDClientInfo the ICD Client information to be updated with keyData and be saved + * @param[inout] clientInfo the ICD Client information to be updated with keyData and be saved * @param[in] aKeyData raw key data provided by application */ virtual CHIP_ERROR SetKey(ICDClientInfo & clientInfo, const ByteSpan keyData) = 0; @@ -61,16 +61,31 @@ class ICDClientStorage * Store updated ICD ClientInfo to storage when ICD registration completes or check-in message * comes. * - * @param[in] aICDClientInfo the updated ICD Client Info. + * @param[in] clientInfo the updated ICD Client Info. */ virtual CHIP_ERROR StoreEntry(const ICDClientInfo & clientInfo) = 0; + /** + * Remove ICD key from clientInfo when ICD registration fails + * + * @param[inout] clientInfo the updated ICD Client Info. + */ + virtual void RemoveKey(ICDClientInfo & clientInfo) = 0; + + /** + * Get ICD ClientInfo from storage + * One user case is to retrieve UserActiveModeTriggerHint and inform how user how to wake up sleepy device. + * @param[in] peerNode scoped node with peer node id and fabric index + * @param[out] clientInfo the ICD Client Info. + */ + virtual CHIP_ERROR GetEntry(const ScopedNodeId & peerNode, ICDClientInfo & clientInfo) = 0; + /** * Delete ICD Client persistent information associated with the specified scoped node Id. * when ICD device is unpaired/removed, the corresponding entry in ICD storage is removed. - * @param aPeerNodeId scoped node with peer node id and fabric index + * @param peerNode scoped node with peer node id and fabric index */ - virtual CHIP_ERROR DeleteEntry(const ScopedNodeId & peerNodeId) = 0; + virtual CHIP_ERROR DeleteEntry(const ScopedNodeId & peerNode) = 0; /** * Remove all ICDClient persistent information associated with the specified diff --git a/src/app/tests/TestDefaultICDClientStorage.cpp b/src/app/tests/TestDefaultICDClientStorage.cpp index 399f7689aee2ef..730747232455c6 100644 --- a/src/app/tests/TestDefaultICDClientStorage.cpp +++ b/src/app/tests/TestDefaultICDClientStorage.cpp @@ -57,7 +57,7 @@ void TestClientInfoCount(nlTestSuite * apSuite, void * apContext) FabricIndex fabricId = 1; NodeId nodeId1 = 6666; NodeId nodeId2 = 6667; - + NodeId unknownNodeId = 6668; TestPersistentStorageDelegate clientInfoStorage; TestSessionKeystoreImpl keystore; @@ -89,12 +89,15 @@ void TestClientInfoCount(nlTestSuite * apSuite, void * apContext) err = manager.StoreEntry(clientInfo3); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); + ICDClientInfo clientInfo; + manager.GetEntry(clientInfo3.peer_node, clientInfo); + NL_TEST_ASSERT(apSuite, clientInfo.peer_node.GetNodeId() == nodeId1); + NL_TEST_ASSERT(apSuite, CHIP_ERROR_NOT_FOUND == manager.GetEntry(ScopedNodeId(unknownNodeId, fabricId), clientInfo)); // Make sure iterator counts correctly auto * iterator = manager.IterateICDClientInfo(); // same nodeId for clientInfo2 and clientInfo3, so the new one replace old one NL_TEST_ASSERT(apSuite, iterator->Count() == 2); - ICDClientInfo clientInfo; NL_TEST_ASSERT(apSuite, iterator->Next(clientInfo)); NL_TEST_ASSERT(apSuite, clientInfo.peer_node.GetNodeId() == nodeId2); NL_TEST_ASSERT(apSuite, iterator->Next(clientInfo)); From 659438bdbe79d9c47b50caacb14380aa3e4d6fd6 Mon Sep 17 00:00:00 2001 From: yunhanw Date: Tue, 5 Dec 2023 00:50:31 -0800 Subject: [PATCH 2/3] move iterator out from ICDStorage --- src/app/icd/client/DefaultICDClientStorage.h | 13 ++++++++++--- src/app/icd/client/ICDClientStorage.h | 10 ---------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/app/icd/client/DefaultICDClientStorage.h b/src/app/icd/client/DefaultICDClientStorage.h index d175700cecad06..45e53634c5faeb 100644 --- a/src/app/icd/client/DefaultICDClientStorage.h +++ b/src/app/icd/client/DefaultICDClientStorage.h @@ -18,15 +18,15 @@ #pragma once #include "ICDClientStorage.h" -#include - #include #include #include +#include #include #include #include #include +#include #include #include @@ -48,9 +48,16 @@ class DefaultICDClientStorage : public ICDClientStorage public: static constexpr size_t kIteratorsMax = CHIP_CONFIG_MAX_ICD_CLIENTS_INFO_STORAGE_CONCURRENT_ITERATORS; + using ICDClientInfoIterator = CommonIterator; + CHIP_ERROR Init(PersistentStorageDelegate * clientInfoStore, Crypto::SymmetricKeystore * keyStore); - ICDClientInfoIterator * IterateICDClientInfo() override; + /** + * Iterate through persisted ICD Client Info + * + * @return A valid iterator on success. Use CommonIterator accessor to retrieve ICDClientInfo + */ + ICDClientInfoIterator * IterateICDClientInfo(); CHIP_ERROR UpdateFabricList(FabricIndex fabricIndex); diff --git a/src/app/icd/client/ICDClientStorage.h b/src/app/icd/client/ICDClientStorage.h index 1d3e8edceec9f1..317be934394640 100644 --- a/src/app/icd/client/ICDClientStorage.h +++ b/src/app/icd/client/ICDClientStorage.h @@ -24,7 +24,6 @@ #include #include #include -#include #include namespace chip { @@ -37,17 +36,8 @@ namespace app { class ICDClientStorage { public: - using ICDClientInfoIterator = CommonIterator; - virtual ~ICDClientStorage() = default; - /** - * Iterate through persisted ICD Client Info - * - * @return A valid iterator on success. Use CommonIterator accessor to retrieve ICDClientInfo - */ - virtual ICDClientInfoIterator * IterateICDClientInfo() = 0; - /** * Called during ICD device registration in commissioning, commissioner/controller * provides raw key data, the shared key handle in clientInfo is updated based upon raw key data From 418b12401a4a891cc8447ee8dc3fc694f3a38459 Mon Sep 17 00:00:00 2001 From: yunhanw Date: Tue, 5 Dec 2023 07:11:09 -0800 Subject: [PATCH 3/3] Revert "move iterator out from ICDStorage" This reverts commit 659438bdbe79d9c47b50caacb14380aa3e4d6fd6. --- src/app/icd/client/DefaultICDClientStorage.h | 13 +++---------- src/app/icd/client/ICDClientStorage.h | 10 ++++++++++ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/app/icd/client/DefaultICDClientStorage.h b/src/app/icd/client/DefaultICDClientStorage.h index 45e53634c5faeb..d175700cecad06 100644 --- a/src/app/icd/client/DefaultICDClientStorage.h +++ b/src/app/icd/client/DefaultICDClientStorage.h @@ -18,15 +18,15 @@ #pragma once #include "ICDClientStorage.h" +#include + #include #include #include -#include #include #include #include #include -#include #include #include @@ -48,16 +48,9 @@ class DefaultICDClientStorage : public ICDClientStorage public: static constexpr size_t kIteratorsMax = CHIP_CONFIG_MAX_ICD_CLIENTS_INFO_STORAGE_CONCURRENT_ITERATORS; - using ICDClientInfoIterator = CommonIterator; - CHIP_ERROR Init(PersistentStorageDelegate * clientInfoStore, Crypto::SymmetricKeystore * keyStore); - /** - * Iterate through persisted ICD Client Info - * - * @return A valid iterator on success. Use CommonIterator accessor to retrieve ICDClientInfo - */ - ICDClientInfoIterator * IterateICDClientInfo(); + ICDClientInfoIterator * IterateICDClientInfo() override; CHIP_ERROR UpdateFabricList(FabricIndex fabricIndex); diff --git a/src/app/icd/client/ICDClientStorage.h b/src/app/icd/client/ICDClientStorage.h index 317be934394640..1d3e8edceec9f1 100644 --- a/src/app/icd/client/ICDClientStorage.h +++ b/src/app/icd/client/ICDClientStorage.h @@ -24,6 +24,7 @@ #include #include #include +#include #include namespace chip { @@ -36,8 +37,17 @@ namespace app { class ICDClientStorage { public: + using ICDClientInfoIterator = CommonIterator; + virtual ~ICDClientStorage() = default; + /** + * Iterate through persisted ICD Client Info + * + * @return A valid iterator on success. Use CommonIterator accessor to retrieve ICDClientInfo + */ + virtual ICDClientInfoIterator * IterateICDClientInfo() = 0; + /** * Called during ICD device registration in commissioning, commissioner/controller * provides raw key data, the shared key handle in clientInfo is updated based upon raw key data