From 88858c768b1310a4f10ef7f9c101317807689909 Mon Sep 17 00:00:00 2001 From: Michael Sandstedt Date: Tue, 21 Dec 2021 10:10:04 -0600 Subject: [PATCH] Add ability to emplace already-commissioned devices The Commissioning state machine can work without dependence on DevciceCommissioner, CASESessionManager, CASEClient or OperationalDeviceProxy. But for compatibility with chip-tool, this commit adds interfaces to emplace already-commissioned devices into a device proxy, CASE session manager, and controller. --- src/app/CASESessionManager.cpp | 15 ++++++++++ src/app/CASESessionManager.h | 6 ++++ src/app/OperationalDeviceProxy.cpp | 37 ++++++++++++++++++++----- src/app/OperationalDeviceProxy.h | 15 ++++++++++ src/controller/CHIPDeviceController.cpp | 7 +++++ src/controller/CHIPDeviceController.h | 3 ++ 6 files changed, 76 insertions(+), 7 deletions(-) diff --git a/src/app/CASESessionManager.cpp b/src/app/CASESessionManager.cpp index 5d3ed0e5e3f07e..89fbfbfbf1595e 100644 --- a/src/app/CASESessionManager.cpp +++ b/src/app/CASESessionManager.cpp @@ -61,6 +61,21 @@ CHIP_ERROR CASESessionManager::FindOrEstablishSession(PeerId peerId, Callback::C return err; } +CHIP_ERROR CASESessionManager::EmplaceSession(const Transport::PeerAddress & addr, + const Optional & config, const PeerId & peerId, + SessionHolder & session) +{ + CHIP_ERROR err; + OperationalDeviceProxy * proxy = mConfig.devicePool->Allocate(mConfig.sessionInitParams, peerId); + VerifyOrReturnError(proxy != nullptr, CHIP_ERROR_NO_MEMORY); + err = proxy->Emplace(addr, config, peerId, session); + if (err != CHIP_NO_ERROR) + { + ReleaseSession(proxy); + } + return err; +} + void CASESessionManager::ReleaseSession(PeerId peerId) { ReleaseSession(FindExistingSession(peerId)); diff --git a/src/app/CASESessionManager.h b/src/app/CASESessionManager.h index 382fcd493784e8..7f42925c442c04 100644 --- a/src/app/CASESessionManager.h +++ b/src/app/CASESessionManager.h @@ -82,6 +82,12 @@ class CASESessionManager : public SessionReleaseDelegate, public Dnssd::Resolver CHIP_ERROR FindOrEstablishSession(PeerId peerId, Callback::Callback * onConnection, Callback::Callback * onFailure); + /** + * Instantiate an externally generated session into the session manager. + */ + CHIP_ERROR EmplaceSession(const Transport::PeerAddress & addr, const Optional & config, + const PeerId & peerId, SessionHolder & session); + OperationalDeviceProxy * FindExistingSession(PeerId peerId); void ReleaseSession(PeerId peerId); diff --git a/src/app/OperationalDeviceProxy.cpp b/src/app/OperationalDeviceProxy.cpp index 9cf2234098ca51..d0d477b4be596b 100644 --- a/src/app/OperationalDeviceProxy.cpp +++ b/src/app/OperationalDeviceProxy.cpp @@ -90,21 +90,38 @@ CHIP_ERROR OperationalDeviceProxy::Connect(Callback::Callback return err; } +CHIP_ERROR OperationalDeviceProxy::Emplace(const Transport::PeerAddress & addr, + const Optional & config, const PeerId & peerId, + SessionHolder & session) +{ + mPeerId = peerId; + mState = State::Initialized; + if (session && mInitParams.sessionManager->GetSecureSession(session.Get())) + { + mSecureSession.Grab(session.Get()); + mState = State::SecureConnected; + } + return UpdateDeviceData(addr, config); +} + CHIP_ERROR OperationalDeviceProxy::UpdateDeviceData(const Transport::PeerAddress & addr, - const ReliableMessageProtocolConfig & config) + const Optional & config) { VerifyOrReturnLogError(mState != State::Uninitialized, CHIP_ERROR_INCORRECT_STATE); CHIP_ERROR err = CHIP_NO_ERROR; mDeviceAddress = addr; - mMRPConfig = config; - - // Initialize CASE session state with any MRP parameters that DNS-SD has provided. - // It can be overridden by CASE session protocol messages that include MRP parameters. - if (mCASEClient) + if (config.HasValue()) { - mCASEClient->SetMRPIntervals(mMRPConfig); + mMRPConfig = config.Value(); + + // Initialize CASE session state with any MRP parameters that DNS-SD has provided. + // It can be overridden by CASE session protocol messages that include MRP parameters. + if (mCASEClient) + { + mCASEClient->SetMRPIntervals(mMRPConfig); + } } if (mState == State::NeedsAddress) @@ -137,6 +154,12 @@ CHIP_ERROR OperationalDeviceProxy::UpdateDeviceData(const Transport::PeerAddress return err; } +CHIP_ERROR OperationalDeviceProxy::UpdateDeviceData(const Transport::PeerAddress & addr, + const ReliableMessageProtocolConfig & config) +{ + return UpdateDeviceData(addr, Optional(config)); +} + bool OperationalDeviceProxy::GetAddress(Inet::IPAddress & addr, uint16_t & port) const { if (mState == State::Uninitialized || mState == State::NeedsAddress) diff --git a/src/app/OperationalDeviceProxy.h b/src/app/OperationalDeviceProxy.h index c56a92453c94f0..5c28f0d209914b 100644 --- a/src/app/OperationalDeviceProxy.h +++ b/src/app/OperationalDeviceProxy.h @@ -116,6 +116,13 @@ class DLL_EXPORT OperationalDeviceProxy : public DeviceProxy, SessionReleaseDele CHIP_ERROR Connect(Callback::Callback * onConnection, Callback::Callback * onFailure, Dnssd::ResolverProxy * resolver); + /* + * Instantiate an operational device proxy with session and peer information + * generated elsewhere. + */ + CHIP_ERROR Emplace(const Transport::PeerAddress & addr, const Optional & config, + const PeerId & peerId, SessionHolder & session); + bool IsConnected() const { return mState == State::SecureConnected; } bool IsConnecting() const { return mState == State::Connecting; } @@ -162,6 +169,14 @@ class DLL_EXPORT OperationalDeviceProxy : public DeviceProxy, SessionReleaseDele */ CHIP_ERROR UpdateDeviceData(const Transport::PeerAddress & addr, const ReliableMessageProtocolConfig & config); + /** + * Update data of the device. + * + * Overload of UpdateDeviceData with optional MRP config. Only stores + * MRP config if that passed is non-empty. + */ + CHIP_ERROR UpdateDeviceData(const Transport::PeerAddress & addr, const Optional & config); + PeerId GetPeerId() const { return mPeerId; } bool MatchesSession(const SessionHandle & session) const { return mSecureSession.Contains(session); } diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp index 9589bcf0f46cfa..943549d2d98832 100644 --- a/src/controller/CHIPDeviceController.cpp +++ b/src/controller/CHIPDeviceController.cpp @@ -268,6 +268,13 @@ void DeviceController::ReleaseOperationalDevice(NodeId remoteDeviceId) mCASESessionManager->ReleaseSession(mFabricInfo->GetPeerIdForNode(remoteDeviceId)); } +void DeviceController::EmplaceOperationalDevice(const Transport::PeerAddress & addr, + const Optional & config, const PeerId & peerId, + SessionHolder & session) +{ + mCASESessionManager->EmplaceSession(addr, config, peerId, session); +} + void DeviceController::OnSessionReleased(const SessionHandle & session) { VerifyOrReturn(mState == State::Initialized, ChipLogError(Controller, "OnConnectionExpired was called in incorrect state")); diff --git a/src/controller/CHIPDeviceController.h b/src/controller/CHIPDeviceController.h index 22cdf05e2bc74e..7a7a881cd80bd3 100644 --- a/src/controller/CHIPDeviceController.h +++ b/src/controller/CHIPDeviceController.h @@ -390,6 +390,9 @@ class DLL_EXPORT DeviceController : public SessionReleaseDelegate, DiscoveredNodeList GetDiscoveredNodes() override { return DiscoveredNodeList(mCommissionableNodes); } #endif // CHIP_DEVICE_CONFIG_ENABLE_DNSSD + void EmplaceOperationalDevice(const Transport::PeerAddress & addr, const Optional & config, + const PeerId & peerId, SessionHolder & session); + private: void ReleaseOperationalDevice(OperationalDeviceProxy * device);