Skip to content

Commit

Permalink
[app] make CASESessionManager fabric independent (#12766)
Browse files Browse the repository at this point in the history
  • Loading branch information
gjc13 authored and pull[bot] committed Oct 5, 2023
1 parent e36db45 commit 7df95a8
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 47 deletions.
13 changes: 7 additions & 6 deletions src/app/CASESessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@

namespace chip {

CHIP_ERROR CASESessionManager::FindOrEstablishSession(NodeId nodeId, Callback::Callback<OnDeviceConnected> * onConnection,
CHIP_ERROR CASESessionManager::FindOrEstablishSession(FabricInfo * fabric, NodeId nodeId,
Callback::Callback<OnDeviceConnected> * onConnection,
Callback::Callback<OnDeviceConnectionFailure> * 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);

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

Expand All @@ -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;
}
Expand Down
8 changes: 3 additions & 5 deletions src/app/CASESessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,21 @@ 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<OnDeviceConnected> * onConnection,
CHIP_ERROR FindOrEstablishSession(FabricInfo * fabric, NodeId nodeId, Callback::Callback<OnDeviceConnected> * onConnection,
Callback::Callback<OnDeviceConnectionFailure> * 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.
*
* 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.
Expand All @@ -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;
Expand Down
8 changes: 3 additions & 5 deletions src/app/OperationalDeviceProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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() {}
Expand Down
6 changes: 4 additions & 2 deletions src/app/OperationalDeviceProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -198,6 +199,7 @@ class DLL_EXPORT OperationalDeviceProxy : public DeviceProxy, SessionReleaseDele
};

DeviceProxyInitParams mInitParams;
FabricInfo * mFabricInfo;
System::Layer * mSystemLayer;

CASEClient * mCASEClient = nullptr;
Expand Down
30 changes: 8 additions & 22 deletions src/app/clusters/ota-requestor/OTARequestor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Controller::DeviceControllerInteractionModelDelegate>(),
Expand All @@ -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()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/clusters/ota-requestor/OTARequestor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReliableMessageProtocolConfig>::Value(mMRPConfig),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
5 changes: 3 additions & 2 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class DLL_EXPORT DeviceController : public SessionReleaseDelegate,
Callback::Callback<OnDeviceConnectionFailure> * onFailure)
{
VerifyOrReturnError(mState == State::Initialized, CHIP_ERROR_INCORRECT_STATE);
return mCASESessionManager->FindOrEstablishSession(deviceId, onConnection, onFailure);
return mCASESessionManager->FindOrEstablishSession(mFabricInfo, deviceId, onConnection, onFailure);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/lib/shell/commands/Ota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<OperationalDeviceProxy>(initParams, fabric->GetPeerIdForNode(nodeId));
Expand Down

0 comments on commit 7df95a8

Please sign in to comment.