diff --git a/src/app/CASESessionManager.cpp b/src/app/CASESessionManager.cpp index 199d25effb654e..f82c571832b7e3 100644 --- a/src/app/CASESessionManager.cpp +++ b/src/app/CASESessionManager.cpp @@ -20,12 +20,13 @@ namespace chip { -CHIP_ERROR CASESessionManager::FindOrEstablishSession(NodeId nodeId, Callback::Callback * onConnection, +CHIP_ERROR CASESessionManager::FindOrEstablishSession(FabricInfo * fabric, NodeId nodeId, + Callback::Callback * onConnection, Callback::Callback * onFailure) { Dnssd::ResolvedNodeData resolutionData; - PeerId peerId = GetFabricInfo()->GetPeerIdForNode(nodeId); + PeerId peerId = fabric->GetPeerIdForNode(nodeId); bool nodeIDWasResolved = (mConfig.dnsCache != nullptr && mConfig.dnsCache->Lookup(peerId, resolutionData) == CHIP_NO_ERROR); @@ -67,9 +68,9 @@ void CASESessionManager::ReleaseSession(NodeId nodeId) ReleaseSession(FindExistingSession(nodeId)); } -CHIP_ERROR CASESessionManager::ResolveDeviceAddress(NodeId nodeId) +CHIP_ERROR CASESessionManager::ResolveDeviceAddress(FabricInfo * fabric, NodeId nodeId) { - return mConfig.dnsResolver->ResolveNodeId(GetFabricInfo()->GetPeerIdForNode(nodeId), Inet::IPAddressType::kAny, + return mConfig.dnsResolver->ResolveNodeId(fabric->GetPeerIdForNode(nodeId), Inet::IPAddressType::kAny, Dnssd::Resolver::CacheBypass::On); } @@ -94,12 +95,12 @@ void CASESessionManager::OnNodeIdResolutionFailed(const PeerId & peer, CHIP_ERRO ChipLogError(Controller, "Error resolving node id: %s", ErrorStr(error)); } -CHIP_ERROR CASESessionManager::GetPeerAddress(NodeId nodeId, Transport::PeerAddress & addr) +CHIP_ERROR CASESessionManager::GetPeerAddress(FabricInfo * fabric, NodeId nodeId, Transport::PeerAddress & addr) { if (mConfig.dnsCache != nullptr) { Dnssd::ResolvedNodeData resolutionData; - ReturnErrorOnFailure(mConfig.dnsCache->Lookup(GetFabricInfo()->GetPeerIdForNode(nodeId), resolutionData)); + ReturnErrorOnFailure(mConfig.dnsCache->Lookup(fabric->GetPeerIdForNode(nodeId), resolutionData)); addr = OperationalDeviceProxy::ToPeerAddress(resolutionData); return CHIP_NO_ERROR; } diff --git a/src/app/CASESessionManager.h b/src/app/CASESessionManager.h index 757d2b48708758..9d5bf11424a887 100644 --- a/src/app/CASESessionManager.h +++ b/src/app/CASESessionManager.h @@ -70,15 +70,13 @@ class CASESessionManager : public SessionReleaseDelegate, public Dnssd::Resolver * these will be used to inform the caller about successful or failed connection establishment. * If the connection is already established, the `onConnection` callback will be immediately called. */ - CHIP_ERROR FindOrEstablishSession(NodeId nodeId, Callback::Callback * onConnection, + CHIP_ERROR FindOrEstablishSession(FabricInfo * fabric, NodeId nodeId, Callback::Callback * onConnection, Callback::Callback * onFailure); OperationalDeviceProxy * FindExistingSession(NodeId nodeId); void ReleaseSession(NodeId nodeId); - FabricInfo * GetFabricInfo() { return mConfig.sessionInitParams.fabricInfo; } - /** * This API triggers the DNS-SD resolution for the given node ID. The node ID will be looked up * on the fabric that was configured for the CASESessionManager object. @@ -86,7 +84,7 @@ class CASESessionManager : public SessionReleaseDelegate, public Dnssd::Resolver * The results of the DNS-SD resolution request is provided to the class via `ResolverDelegate` * implementation of CASESessionManager. */ - CHIP_ERROR ResolveDeviceAddress(NodeId nodeId); + CHIP_ERROR ResolveDeviceAddress(FabricInfo * fabric, NodeId nodeId); /** * This API returns the address for the given node ID. @@ -96,7 +94,7 @@ class CASESessionManager : public SessionReleaseDelegate, public Dnssd::Resolver * an ongoing session with the peer node. If the session doesn't exist, the API will return * `CHIP_ERROR_NOT_CONNECTED` error. */ - CHIP_ERROR GetPeerAddress(NodeId nodeId, Transport::PeerAddress & addr); + CHIP_ERROR GetPeerAddress(FabricInfo * fabric, NodeId nodeId, Transport::PeerAddress & addr); //////////// SessionReleaseDelegate Implementation /////////////// void OnSessionReleased(SessionHandle session) override; diff --git a/src/app/OperationalDeviceProxy.cpp b/src/app/OperationalDeviceProxy.cpp index 8f2859ad3bc51f..f6e458dbba718d 100644 --- a/src/app/OperationalDeviceProxy.cpp +++ b/src/app/OperationalDeviceProxy.cpp @@ -151,9 +151,8 @@ bool OperationalDeviceProxy::GetAddress(Inet::IPAddress & addr, uint16_t & port) CHIP_ERROR OperationalDeviceProxy::EstablishConnection() { - mCASEClient = mInitParams.clientPool->Allocate(CASEClientInitParams{ mInitParams.sessionManager, mInitParams.exchangeMgr, - mInitParams.idAllocator, mInitParams.fabricInfo, - mInitParams.mrpLocalConfig }); + mCASEClient = mInitParams.clientPool->Allocate(CASEClientInitParams{ + mInitParams.sessionManager, mInitParams.exchangeMgr, mInitParams.idAllocator, mFabricInfo, mInitParams.mrpLocalConfig }); ReturnErrorCodeIf(mCASEClient == nullptr, CHIP_ERROR_NO_MEMORY); CHIP_ERROR err = mCASEClient->EstablishSession(mPeerId, mDeviceAddress, mMRPConfig, HandleCASEConnected, HandleCASEConnectionFailure, this); @@ -301,8 +300,7 @@ void OperationalDeviceProxy::OnSessionReleased(SessionHandle session) CHIP_ERROR OperationalDeviceProxy::ShutdownSubscriptions() { - return app::InteractionModelEngine::GetInstance()->ShutdownSubscriptions(mInitParams.fabricInfo->GetFabricIndex(), - GetDeviceId()); + return app::InteractionModelEngine::GetInstance()->ShutdownSubscriptions(mFabricInfo->GetFabricIndex(), GetDeviceId()); } OperationalDeviceProxy::~OperationalDeviceProxy() {} diff --git a/src/app/OperationalDeviceProxy.h b/src/app/OperationalDeviceProxy.h index 7ee309fbfeb6aa..5fc28c56d31252 100644 --- a/src/app/OperationalDeviceProxy.h +++ b/src/app/OperationalDeviceProxy.h @@ -52,7 +52,7 @@ struct DeviceProxyInitParams SessionManager * sessionManager = nullptr; Messaging::ExchangeManager * exchangeMgr = nullptr; SessionIDAllocator * idAllocator = nullptr; - FabricInfo * fabricInfo = nullptr; + FabricTable * fabricTable = nullptr; CASEClientPoolDelegate * clientPool = nullptr; Controller::DeviceControllerInteractionModelDelegate * imDelegate = nullptr; @@ -64,7 +64,7 @@ struct DeviceProxyInitParams ReturnErrorCodeIf(sessionManager == nullptr, CHIP_ERROR_INCORRECT_STATE); ReturnErrorCodeIf(exchangeMgr == nullptr, CHIP_ERROR_INCORRECT_STATE); ReturnErrorCodeIf(idAllocator == nullptr, CHIP_ERROR_INCORRECT_STATE); - ReturnErrorCodeIf(fabricInfo == nullptr, CHIP_ERROR_INCORRECT_STATE); + ReturnErrorCodeIf(fabricTable == nullptr, CHIP_ERROR_INCORRECT_STATE); ReturnErrorCodeIf(clientPool == nullptr, CHIP_ERROR_INCORRECT_STATE); return CHIP_NO_ERROR; @@ -87,6 +87,7 @@ class DLL_EXPORT OperationalDeviceProxy : public DeviceProxy, SessionReleaseDele mSystemLayer = params.exchangeMgr->GetSessionManager()->SystemLayer(); mInitParams = params; mPeerId = peerId; + mFabricInfo = params.fabricTable->FindFabricWithCompressedId(peerId.GetCompressedFabricId()); mState = State::NeedsAddress; } @@ -198,6 +199,7 @@ class DLL_EXPORT OperationalDeviceProxy : public DeviceProxy, SessionReleaseDele }; DeviceProxyInitParams mInitParams; + FabricInfo * mFabricInfo; System::Layer * mSystemLayer; CASEClient * mCASEClient = nullptr; diff --git a/src/app/clusters/ota-requestor/OTARequestor.cpp b/src/app/clusters/ota-requestor/OTARequestor.cpp index 61d0bbd5fed0e5..d8918c82bd6095 100644 --- a/src/app/clusters/ota-requestor/OTARequestor.cpp +++ b/src/app/clusters/ota-requestor/OTARequestor.cpp @@ -193,39 +193,22 @@ EmberAfStatus OTARequestor::HandleAnnounceOTAProvider(app::CommandHandler * comm return EMBER_ZCL_STATUS_SUCCESS; } -CHIP_ERROR OTARequestor::SetupCASESessionManager(FabricIndex fabricIndex) +CHIP_ERROR OTARequestor::SetupCASESessionManager() { // A previous CASE session had been established if (mCASESessionManager != nullptr) { - if (mCASESessionManager->GetFabricInfo()->GetFabricIndex() != fabricIndex) - { - // CSM is per fabric so if fabric index does not match the previous session, CSM needs to be set up again - Platform::Delete(mCASESessionManager); - mCASESessionManager = nullptr; - } - else - { - // Fabric index matches so use previous instance - return CHIP_NO_ERROR; - } + return CHIP_NO_ERROR; } // CSM has not been setup so create a new instance of it if (mCASESessionManager == nullptr) { - FabricInfo * fabricInfo = mServer->GetFabricTable().FindFabricWithIndex(fabricIndex); - if (fabricInfo == nullptr) - { - ChipLogError(SoftwareUpdate, "Did not find fabric for index %d", fabricIndex); - return CHIP_ERROR_INVALID_ARGUMENT; - } - DeviceProxyInitParams initParams = { .sessionManager = &(mServer->GetSecureSessionManager()), .exchangeMgr = &(mServer->GetExchangeManager()), .idAllocator = &(mServer->GetSessionIDAllocator()), - .fabricInfo = fabricInfo, + .fabricTable = &(mServer->GetFabricTable()), .clientPool = mServer->GetCASEClientPool(), // TODO: Determine where this should be instantiated .imDelegate = Platform::New(), @@ -252,16 +235,19 @@ CHIP_ERROR OTARequestor::SetupCASESessionManager(FabricIndex fabricIndex) void OTARequestor::ConnectToProvider(OnConnectedAction onConnectedAction) { - CHIP_ERROR err = SetupCASESessionManager(mProviderFabricIndex); + CHIP_ERROR err = SetupCASESessionManager(); + FabricInfo * fabricInfo = mServer->GetFabricTable().FindFabricWithIndex(mProviderFabricIndex); VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(SoftwareUpdate, "Cannot setup CASESessionManager: %" CHIP_ERROR_FORMAT, err.Format())); + VerifyOrReturn(fabricInfo != nullptr, ChipLogError(SoftwareUpdate, "Cannot find fabric")); // Set the action to take once connection is successfully established mOnConnectedAction = onConnectedAction; ChipLogDetail(SoftwareUpdate, "Establishing session to provider node ID 0x" ChipLogFormatX64 " on fabric index %d", ChipLogValueX64(mProviderNodeId), mProviderFabricIndex); - err = mCASESessionManager->FindOrEstablishSession(mProviderNodeId, &mOnConnectedCallback, &mOnConnectionFailureCallback); + err = mCASESessionManager->FindOrEstablishSession(fabricInfo, mProviderNodeId, &mOnConnectedCallback, + &mOnConnectionFailureCallback); VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(SoftwareUpdate, "Cannot establish connection to provider: %" CHIP_ERROR_FORMAT, err.Format())); } diff --git a/src/app/clusters/ota-requestor/OTARequestor.h b/src/app/clusters/ota-requestor/OTARequestor.h index 77eb0dcd9163e5..270e80c020187c 100644 --- a/src/app/clusters/ota-requestor/OTARequestor.h +++ b/src/app/clusters/ota-requestor/OTARequestor.h @@ -170,7 +170,7 @@ class OTARequestor : public OTARequestorInterface /** * Setup CASESessionManager used to establish a session with the provider */ - CHIP_ERROR SetupCASESessionManager(chip::FabricIndex fabricIndex); + CHIP_ERROR SetupCASESessionManager(); /** * Create a QueryImage request using values from the Basic cluster attributes diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp index 5c9a0167c4c7a0..68da54c18fdba0 100644 --- a/src/controller/CHIPDeviceController.cpp +++ b/src/controller/CHIPDeviceController.cpp @@ -143,11 +143,14 @@ CHIP_ERROR DeviceController::Init(ControllerInitParams params) mFabricIndex = params.fabricIndex; ReturnErrorOnFailure(ProcessControllerNOCChain(params)); + mFabricInfo = params.systemState->Fabrics()->FindFabricWithIndex(mFabricIndex); + VerifyOrReturnError(mFabricInfo != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + DeviceProxyInitParams deviceInitParams = { .sessionManager = params.systemState->SessionMgr(), .exchangeMgr = params.systemState->ExchangeMgr(), .idAllocator = &mIDAllocator, - .fabricInfo = params.systemState->Fabrics()->FindFabricWithIndex(mFabricIndex), + .fabricTable = params.systemState->Fabrics(), .clientPool = &mCASEClientPool, .imDelegate = params.systemState->IMDelegate(), .mrpLocalConfig = Optional::Value(mMRPConfig), @@ -348,7 +351,7 @@ CHIP_ERROR DeviceController::GetPeerAddressAndPort(PeerId peerId, Inet::IPAddres { VerifyOrReturnError(mState == State::Initialized, CHIP_ERROR_INCORRECT_STATE); Transport::PeerAddress peerAddr; - ReturnErrorOnFailure(mCASESessionManager->GetPeerAddress(peerId.GetNodeId(), peerAddr)); + ReturnErrorOnFailure(mCASESessionManager->GetPeerAddress(mFabricInfo, peerId.GetNodeId(), peerAddr)); addr = peerAddr.GetIPAddress(); port = peerAddr.GetPort(); return CHIP_NO_ERROR; @@ -1658,7 +1661,7 @@ void DeviceCommissioner::OnNodeIdResolved(const chip::Dnssd::ResolvedNodeData & mDNSCache.Insert(nodeData); - mCASESessionManager->FindOrEstablishSession(nodeData.mPeerId.GetNodeId(), &mOnDeviceConnectedCallback, + mCASESessionManager->FindOrEstablishSession(mFabricInfo, nodeData.mPeerId.GetNodeId(), &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback); DeviceController::OnNodeIdResolved(nodeData); } diff --git a/src/controller/CHIPDeviceController.h b/src/controller/CHIPDeviceController.h index c23c93d7da99bd..09c62f2958d14d 100644 --- a/src/controller/CHIPDeviceController.h +++ b/src/controller/CHIPDeviceController.h @@ -239,7 +239,7 @@ class DLL_EXPORT DeviceController : public SessionReleaseDelegate, Callback::Callback * onFailure) { VerifyOrReturnError(mState == State::Initialized, CHIP_ERROR_INCORRECT_STATE); - return mCASESessionManager->FindOrEstablishSession(deviceId, onConnection, onFailure); + return mCASESessionManager->FindOrEstablishSession(mFabricInfo, deviceId, onConnection, onFailure); } /** @@ -253,7 +253,7 @@ class DLL_EXPORT DeviceController : public SessionReleaseDelegate, CHIP_ERROR UpdateDevice(NodeId deviceId) { VerifyOrReturnError(mState == State::Initialized, CHIP_ERROR_INCORRECT_STATE); - return mCASESessionManager->ResolveDeviceAddress(deviceId); + return mCASESessionManager->ResolveDeviceAddress(mFabricInfo, deviceId); } /** @@ -363,6 +363,7 @@ class DLL_EXPORT DeviceController : public SessionReleaseDelegate, PeerId mLocalId = PeerId(); FabricId mFabricId = kUndefinedFabricId; + FabricInfo * mFabricInfo; PersistentStorageDelegate * mStorageDelegate = nullptr; #if CHIP_DEVICE_CONFIG_ENABLE_DNSSD diff --git a/src/lib/shell/commands/Ota.cpp b/src/lib/shell/commands/Ota.cpp index 20c398a51a8ed6..512a8b29888bed 100644 --- a/src/lib/shell/commands/Ota.cpp +++ b/src/lib/shell/commands/Ota.cpp @@ -313,7 +313,7 @@ CHIP_ERROR ConnectProvider(FabricIndex fabricIndex, NodeId nodeId, const Transpo DeviceProxyInitParams initParams = { .sessionManager = &Server::GetInstance().GetSecureSessionManager(), .exchangeMgr = &Server::GetInstance().GetExchangeManager(), .idAllocator = &Server::GetInstance().GetSessionIDAllocator(), - .fabricInfo = fabric, + .fabricTable = &Server::GetInstance().GetFabricTable(), .imDelegate = &sIMDelegate }; auto deviceProxy = Platform::New(initParams, fabric->GetPeerIdForNode(nodeId));