diff --git a/src/app/CommandHandler.cpp b/src/app/CommandHandler.cpp index cc0b19bc3f7adc..e21a1462d2feee 100644 --- a/src/app/CommandHandler.cpp +++ b/src/app/CommandHandler.cpp @@ -370,6 +370,11 @@ TLV::TLVWriter * CommandHandler::GetCommandDataIBTLVWriter() } } +FabricIndex CommandHandler::GetAccessingFabricIndex() const +{ + return mpExchangeCtx->GetSessionHandle().GetFabricIndex(); +} + CommandHandler * CommandHandler::Handle::Get() { return (mMagic == InteractionModelEngine::GetInstance()->GetMagicNumber()) ? mpHandler : nullptr; diff --git a/src/app/CommandHandler.h b/src/app/CommandHandler.h index 544d4e46710521..85a5573cae452a 100644 --- a/src/app/CommandHandler.h +++ b/src/app/CommandHandler.h @@ -143,6 +143,7 @@ class CommandHandler : public Command CHIP_ERROR PrepareStatus(const CommandPathParams & aCommandPathParams); CHIP_ERROR FinishStatus(); TLV::TLVWriter * GetCommandDataIBTLVWriter(); + FabricIndex GetAccessingFabricIndex() const; /** * API for adding a data response. The template parameter T is generally 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 a4818ab0791cf4..f96a3893cf4e49 100644 --- a/src/app/clusters/operational-credentials-server/operational-credentials-server.cpp +++ b/src/app/clusters/operational-credentials-server/operational-credentials-server.cpp @@ -175,14 +175,9 @@ void fabricListChanged() OperationalCredentials::Attributes::CommissionedFabrics::Id); } -static FabricInfo * retrieveCurrentFabric() +static FabricInfo * retrieveCurrentFabric(CommandHandler * aCommandHandler) { - if (emberAfCurrentCommand()->source == nullptr) - { - return nullptr; - } - - FabricIndex index = emberAfCurrentCommand()->source->GetSessionHandle().GetFabricIndex(); + FabricIndex index = aCommandHandler->GetAccessingFabricIndex(); emberAfPrintln(EMBER_AF_PRINT_DEBUG, "OpCreds: Finding fabric with fabricIndex %d", index); return Server::GetInstance().GetFabricTable().FindFabricWithIndex(index); } @@ -278,7 +273,7 @@ bool emberAfOperationalCredentialsClusterRemoveFabricCallback(app::CommandHandle if (err == CHIP_NO_ERROR) { chip::Messaging::ExchangeContext * ec = commandObj->GetExchangeContext(); - FabricIndex currentFabricIndex = ec->GetSessionHandle().GetFabricIndex(); + FabricIndex currentFabricIndex = commandObj->GetAccessingFabricIndex(); if (currentFabricIndex == fabricBeingRemoved) { // If the current fabric is being removed, expiring all the secure sessions causes crashes as @@ -309,7 +304,7 @@ bool emberAfOperationalCredentialsClusterUpdateFabricLabelCallback(app::CommandH CHIP_ERROR err; // Fetch current fabric - FabricInfo * fabric = retrieveCurrentFabric(); + FabricInfo * fabric = retrieveCurrentFabric(commandObj); VerifyOrExit(fabric != nullptr, status = EMBER_ZCL_STATUS_FAILURE); // Set Label on fabric @@ -442,7 +437,7 @@ bool emberAfOperationalCredentialsClusterUpdateNOCCallback(app::CommandHandler * emberAfPrintln(EMBER_AF_PRINT_DEBUG, "OpCreds: an administrator has updated the Op Cert"); // Fetch current fabric - FabricInfo * fabric = retrieveCurrentFabric(); + FabricInfo * fabric = retrieveCurrentFabric(commandObj); VerifyOrExit(fabric != nullptr, nocResponse = ConvertToNOCResponseStatus(CHIP_ERROR_INVALID_FABRIC_ID)); err = fabric->SetNOCCert(NOCValue);