diff --git a/src/app/icd/client/DefaultICDClientStorage.cpp b/src/app/icd/client/DefaultICDClientStorage.cpp index 503e042cd4af43..324532750b8c1a 100644 --- a/src/app/icd/client/DefaultICDClientStorage.cpp +++ b/src/app/icd/client/DefaultICDClientStorage.cpp @@ -307,6 +307,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; @@ -414,6 +419,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; @@ -424,7 +446,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; } @@ -459,7 +481,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 f9af7d35eca687..f71d89d0072aa7 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 7d05a1bfc28373..0eb17d97b70bd6 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; @@ -94,12 +94,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, clientInfo.has_instruction);