From 2020225f6d218114ccd378ebe11cccc2666c9aca Mon Sep 17 00:00:00 2001 From: Yufeng Wang Date: Tue, 12 Apr 2022 01:31:18 -0700 Subject: [PATCH] Share more logic between FailSafeCleanup and actual RemoveFabric (#17262) * Share more logic between FailSafeCleanup and actual RemoveFabric * Update src/app/clusters/operational-credentials-server/operational-credentials-server.cpp Co-authored-by: Boris Zbarsky * Update src/app/clusters/operational-credentials-server/operational-credentials-server.cpp Co-authored-by: Michael Sandstedt Co-authored-by: Boris Zbarsky Co-authored-by: Michael Sandstedt --- .../operational-credentials-server.cpp | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/app/clusters/operational-credentials-server/operational-credentials-server.cpp b/src/app/clusters/operational-credentials-server/operational-credentials-server.cpp index 044bf3cc6eba88..166db42222cdde 100644 --- a/src/app/clusters/operational-credentials-server/operational-credentials-server.cpp +++ b/src/app/clusters/operational-credentials-server/operational-credentials-server.cpp @@ -264,6 +264,23 @@ FabricInfo * RetrieveCurrentFabric(CommandHandler * aCommandHandler) return Server::GetInstance().GetFabricTable().FindFabricWithIndex(index); } +CHIP_ERROR DeleteFabricFromTable(FabricIndex fabricIndex) +{ + ReturnErrorOnFailure(Server::GetInstance().GetFabricTable().Delete(fabricIndex)); + + // We need to withdraw the advertisement for the now-removed fabric, so need + // to restart advertising altogether. + app::DnssdServer::Instance().StartServer(); + + return CHIP_NO_ERROR; +} + +void CleanupFabricContext(SessionManager & sessionMgr, FabricIndex fabricIndex) +{ + InteractionModelEngine::GetInstance()->CloseTransactionsFromFabricIndex(fabricIndex); + sessionMgr.ExpireAllPairingsForFabric(fabricIndex); +} + void FailSafeCleanup(const chip::DeviceLayer::ChipDeviceEvent * event) { emberAfPrintln(EMBER_AF_PRINT_DEBUG, "OpCreds: Call to FailSafeCleanup"); @@ -284,7 +301,8 @@ void FailSafeCleanup(const chip::DeviceLayer::ChipDeviceEvent * event) caseSessionManager->ReleaseSessionsForFabric(fabricInfo->GetCompressedId()); } - Server::GetInstance().GetSecureSessionManager().ExpireAllPairingsForFabric(fabricIndex); + SessionManager & sessionMgr = Server::GetInstance().GetSecureSessionManager(); + CleanupFabricContext(sessionMgr, fabricIndex); } // If an AddNOC command had been successfully invoked, achieve the equivalent effect of invoking the RemoveFabric command @@ -292,7 +310,7 @@ void FailSafeCleanup(const chip::DeviceLayer::ChipDeviceEvent * event) // command. if (event->FailSafeTimerExpired.AddNocCommandHasBeenInvoked) { - Server::GetInstance().GetFabricTable().Delete(fabricIndex); + DeleteFabricFromTable(fabricIndex); } // If an UpdateNOC command had been successfully invoked, revert the state of operational key pair, NOC and ICAC for that @@ -406,9 +424,9 @@ class FabricCleanupExchangeDelegate : public chip::Messaging::ExchangeDelegate void OnResponseTimeout(chip::Messaging::ExchangeContext * ec) override {} void OnExchangeClosing(chip::Messaging::ExchangeContext * ec) override { - FabricIndex currentFabricIndex = ec->GetSessionHandle()->GetFabricIndex(); - InteractionModelEngine::GetInstance()->CloseTransactionsFromFabricIndex(currentFabricIndex); - ec->GetExchangeMgr()->GetSessionManager()->ExpireAllPairingsForFabric(currentFabricIndex); + SessionManager * sessionManager = ec->GetExchangeMgr()->GetSessionManager(); + FabricIndex currentFabricIndex = ec->GetSessionHandle()->GetFabricIndex(); + CleanupFabricContext(*sessionManager, currentFabricIndex); } }; @@ -425,13 +443,9 @@ bool emberAfOperationalCredentialsClusterRemoveFabricCallback(app::CommandHandle emberAfPrintln(EMBER_AF_PRINT_DEBUG, "OpCreds: RemoveFabric"); // TODO: Generate emberAfFabricClusterPrintln - CHIP_ERROR err = Server::GetInstance().GetFabricTable().Delete(fabricBeingRemoved); + CHIP_ERROR err = DeleteFabricFromTable(fabricBeingRemoved); SuccessOrExit(err); - // We need to withdraw the advertisement for the now-removed fabric, so need - // to restart advertising altogether. - app::DnssdServer::Instance().StartServer(); - exit: fabricListChanged(); // Not using ConvertToNOCResponseStatus here because it's pretty @@ -467,8 +481,8 @@ bool emberAfOperationalCredentialsClusterRemoveFabricCallback(app::CommandHandle } else { - InteractionModelEngine::GetInstance()->CloseTransactionsFromFabricIndex(fabricBeingRemoved); - ec->GetExchangeMgr()->GetSessionManager()->ExpireAllPairingsForFabric(fabricBeingRemoved); + SessionManager * sessionManager = ec->GetExchangeMgr()->GetSessionManager(); + CleanupFabricContext(*sessionManager, fabricBeingRemoved); } } return true;