Skip to content

Commit

Permalink
Share more logic between FailSafeCleanup and actual RemoveFabric (#17262
Browse files Browse the repository at this point in the history
)

* Share more logic between FailSafeCleanup and actual RemoveFabric

* Update src/app/clusters/operational-credentials-server/operational-credentials-server.cpp

Co-authored-by: Boris Zbarsky <[email protected]>

* Update src/app/clusters/operational-credentials-server/operational-credentials-server.cpp

Co-authored-by: Michael Sandstedt <[email protected]>

Co-authored-by: Boris Zbarsky <[email protected]>
Co-authored-by: Michael Sandstedt <[email protected]>
  • Loading branch information
3 people authored and pull[bot] committed Apr 14, 2022
1 parent df461c8 commit 1398562
Showing 1 changed file with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -284,15 +301,16 @@ 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
// against the Fabric Index stored in the Fail-Safe Context for the Fabric Index that was the subject of the AddNOC
// 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
Expand Down Expand Up @@ -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);
}
};

Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 1398562

Please sign in to comment.