Skip to content

Commit

Permalink
Add ability to emplace already-commissioned devices
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
msandstedt committed Dec 31, 2021
1 parent cfb929b commit 88858c7
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 7 deletions.
15 changes: 15 additions & 0 deletions src/app/CASESessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ CHIP_ERROR CASESessionManager::FindOrEstablishSession(PeerId peerId, Callback::C
return err;
}

CHIP_ERROR CASESessionManager::EmplaceSession(const Transport::PeerAddress & addr,
const Optional<ReliableMessageProtocolConfig> & 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));
Expand Down
6 changes: 6 additions & 0 deletions src/app/CASESessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ class CASESessionManager : public SessionReleaseDelegate, public Dnssd::Resolver
CHIP_ERROR FindOrEstablishSession(PeerId peerId, Callback::Callback<OnDeviceConnected> * onConnection,
Callback::Callback<OnDeviceConnectionFailure> * onFailure);

/**
* Instantiate an externally generated session into the session manager.
*/
CHIP_ERROR EmplaceSession(const Transport::PeerAddress & addr, const Optional<ReliableMessageProtocolConfig> & config,
const PeerId & peerId, SessionHolder & session);

OperationalDeviceProxy * FindExistingSession(PeerId peerId);

void ReleaseSession(PeerId peerId);
Expand Down
37 changes: 30 additions & 7 deletions src/app/OperationalDeviceProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,38 @@ CHIP_ERROR OperationalDeviceProxy::Connect(Callback::Callback<OnDeviceConnected>
return err;
}

CHIP_ERROR OperationalDeviceProxy::Emplace(const Transport::PeerAddress & addr,
const Optional<ReliableMessageProtocolConfig> & 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<ReliableMessageProtocolConfig> & 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)
Expand Down Expand Up @@ -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<ReliableMessageProtocolConfig>(config));
}

bool OperationalDeviceProxy::GetAddress(Inet::IPAddress & addr, uint16_t & port) const
{
if (mState == State::Uninitialized || mState == State::NeedsAddress)
Expand Down
15 changes: 15 additions & 0 deletions src/app/OperationalDeviceProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ class DLL_EXPORT OperationalDeviceProxy : public DeviceProxy, SessionReleaseDele
CHIP_ERROR Connect(Callback::Callback<OnDeviceConnected> * onConnection,
Callback::Callback<OnDeviceConnectionFailure> * 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<ReliableMessageProtocolConfig> & config,
const PeerId & peerId, SessionHolder & session);

bool IsConnected() const { return mState == State::SecureConnected; }

bool IsConnecting() const { return mState == State::Connecting; }
Expand Down Expand Up @@ -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<ReliableMessageProtocolConfig> & config);

PeerId GetPeerId() const { return mPeerId; }

bool MatchesSession(const SessionHandle & session) const { return mSecureSession.Contains(session); }
Expand Down
7 changes: 7 additions & 0 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,13 @@ void DeviceController::ReleaseOperationalDevice(NodeId remoteDeviceId)
mCASESessionManager->ReleaseSession(mFabricInfo->GetPeerIdForNode(remoteDeviceId));
}

void DeviceController::EmplaceOperationalDevice(const Transport::PeerAddress & addr,
const Optional<ReliableMessageProtocolConfig> & 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"));
Expand Down
3 changes: 3 additions & 0 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReliableMessageProtocolConfig> & config,
const PeerId & peerId, SessionHolder & session);

private:
void ReleaseOperationalDevice(OperationalDeviceProxy * device);

Expand Down

0 comments on commit 88858c7

Please sign in to comment.