Skip to content

Commit

Permalink
Update to new SubscribeAttribute API (project-chip#13015)
Browse files Browse the repository at this point in the history
  • Loading branch information
austinh0 authored and rcasallas-silabs committed Dec 17, 2021
1 parent 7283e3a commit 5f04f0e
Show file tree
Hide file tree
Showing 11 changed files with 1,107 additions and 588 deletions.
4 changes: 4 additions & 0 deletions examples/chip-tool/commands/clusters/ModelCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,23 @@ using namespace ::chip;

CHIP_ERROR ModelCommand::RunCommand()
{
HERE(". ")
printf("\n#### ModelCommand::RunCommand %u\n", IsGroupId(mNodeId));
ChipLogProgress(chipTool, "Sending command to node 0x%" PRIx64, mNodeId);
return CurrentCommissioner().GetConnectedDevice(mNodeId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback);
}

void ModelCommand::OnDeviceConnectedFn(void * context, ChipDevice * device)
{
HERE(".")
ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "OnDeviceConnectedFn: context is null"));
command->SendCommand(device, command->mEndPointId);
}

void ModelCommand::OnDeviceConnectionFailureFn(void * context, PeerId peerId, CHIP_ERROR err)
{
HERE(".")
LogErrorOnFailure(err);

ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
Expand Down
2 changes: 2 additions & 0 deletions src/app/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ static_library("app") {
"CASEClientPool.h",
"CASESessionManager.cpp",
"CASESessionManager.h",
"GroupSessionManager.cpp",
"GroupSessionManager.h",
"CommandHandler.cpp",
"CommandSender.cpp",
"DeviceProxy.cpp",
Expand Down
136 changes: 136 additions & 0 deletions src/app/GroupSessionManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
*
* Copyright (c) 2020-2021 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <app/GroupSessionManager.h>

namespace chip {

CHIP_ERROR GroupSessionManager::FindOrEstablishSession(FabricInfo * fabric, NodeId nodeId,
Callback::Callback<OnDeviceConnected> * onConnection,
Callback::Callback<OnDeviceConnectionFailure> * onFailure)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Dnssd::ResolvedNodeData resolutionData;

PeerId peerId = fabric->GetPeerIdForNode(nodeId);

OperationalDeviceProxy * session = FindExistingSession(nodeId);
if (session == nullptr)
{
session = mDevicePool->Allocate(mParams, peerId, resolutionData);

if (session == nullptr)
{
onFailure->mCall(onFailure->mContext, nodeId, CHIP_ERROR_NO_MEMORY);
return CHIP_ERROR_NO_MEMORY;
}
}
else
{
session->OnNodeIdResolved(resolutionData);
}

if (onConnection != nullptr)
{
onConnection->mCall(onConnection->mContext, session);
}

// CHIP_ERROR err = session->Connect(onConnection, onFailure, mConfig.dnsResolver);
// if (err != CHIP_NO_ERROR)
// {
// ReleaseSession(session);
// }

return err;
}

void GroupSessionManager::ReleaseSession(NodeId nodeId)
{
ReleaseSession(FindExistingSession(nodeId));
}

// CHIP_ERROR GroupSessionManager::ResolveDeviceAddress(FabricInfo * fabric, NodeId nodeId)
// {
// return mConfig.dnsResolver->ResolveNodeId(fabric->GetPeerIdForNode(nodeId), Inet::IPAddressType::kAny,
// Dnssd::Resolver::CacheBypass::On);
// }

// void GroupSessionManager::OnNodeIdResolved(const Dnssd::ResolvedNodeData & nodeData)
// {
// ChipLogProgress(Controller, "Address resolved for node: 0x" ChipLogFormatX64, ChipLogValueX64(nodeData.mPeerId.GetNodeId()));

// if (mConfig.dnsCache != nullptr)
// {
// LogErrorOnFailure(mConfig.dnsCache->Insert(nodeData));
// }

// OperationalDeviceProxy * session = FindExistingSession(nodeData.mPeerId.GetNodeId());
// VerifyOrReturn(session != nullptr,
// ChipLogDetail(Controller, "OnNodeIdResolved was called for a device with no active sessions, ignoring it."));

// LogErrorOnFailure(session->UpdateDeviceData(session->ToPeerAddress(nodeData), nodeData.GetMRPConfig()));
// }

// void GroupSessionManager::OnNodeIdResolutionFailed(const PeerId & peer, CHIP_ERROR error)
// {
// ChipLogError(Controller, "Error resolving node id: %s", ErrorStr(error));
// }

// CHIP_ERROR GroupSessionManager::GetPeerAddress(FabricInfo * fabric, NodeId nodeId, Transport::PeerAddress & addr)
// {
// // if (mConfig.dnsCache != nullptr)
// // {
// // Dnssd::ResolvedNodeData resolutionData;
// // ReturnErrorOnFailure(mConfig.dnsCache->Lookup(fabric->GetPeerIdForNode(nodeId), resolutionData));
// // addr = OperationalDeviceProxy::ToPeerAddress(resolutionData);
// // return CHIP_NO_ERROR;
// // }

// OperationalDeviceProxy * session = FindExistingSession(nodeId);
// VerifyOrReturnError(session != nullptr, CHIP_ERROR_NOT_CONNECTED);
// addr = session->GetPeerAddress();
// return CHIP_NO_ERROR;
// }

void GroupSessionManager::OnSessionReleased(SessionHandle sessionHandle)
{
OperationalDeviceProxy * session = FindSession(sessionHandle);
VerifyOrReturn(session != nullptr, ChipLogDetail(Controller, "OnSessionReleased was called for unknown device, ignoring it."));

session->OnSessionReleased(sessionHandle);
}

OperationalDeviceProxy * GroupSessionManager::FindSession(SessionHandle session)
{
return mDevicePool->FindDevice(session);
}

OperationalDeviceProxy * GroupSessionManager::FindExistingSession(NodeId id)
{
return mDevicePool->FindDevice(id);
}

void GroupSessionManager::ReleaseSession(OperationalDeviceProxy * session)
{
if (session != nullptr)
{
mDevicePool->Release(session);
}
}

} // namespace chip
86 changes: 86 additions & 0 deletions src/app/GroupSessionManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
*
* Copyright (c) 2020-2021 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <app/OperationalDeviceProxy.h>
#include <lib/core/CHIPConfig.h>
#include <lib/core/CHIPCore.h>
#include <lib/dnssd/DnssdCache.h>
#include <lib/support/Pool.h>
#include <transport/SessionDelegate.h>
#include <app/OperationalDeviceProxyPool.h>

#include <lib/dnssd/Resolver.h>

namespace chip {


class GroupSessionManager : public SessionReleaseDelegate
{
public:
GroupSessionManager() = delete;

GroupSessionManager(DeviceProxyInitParams & params, OperationalDeviceProxyPoolDelegate * devicePool) : mDevicePool(devicePool)
{
mParams = params;
}

virtual ~GroupSessionManager() {}

/**
* Find an existing session for the given node ID, or trigger a new session request.
* The caller can optionally provide `onConnection` and `onFailure` callback objects. If provided,
* 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(FabricInfo * fabric, NodeId nodeId, Callback::Callback<OnDeviceConnected> * onConnection,
Callback::Callback<OnDeviceConnectionFailure> * onFailure);

OperationalDeviceProxy * FindExistingSession(NodeId nodeId);

void ReleaseSession(NodeId nodeId);

/**
* This API returns the address for the given node ID.
* If the GroupSessionManager is configured with a DNS-SD cache, the cache is looked up
* for the node ID.
* If the DNS-SD cache is not available, the GroupSessionManager looks up the list for
* 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);

//////////// SessionReleaseDelegate Implementation ///////////////
void OnSessionReleased(SessionHandle session) override;


private:
// FabricInfo * GetFabricInfo() { return mDeviceInitParams.fabricInfo; }

OperationalDeviceProxy * FindSession(SessionHandle session);
void ReleaseSession(OperationalDeviceProxy * device);

BitMapObjectPool<OperationalDeviceProxy, CHIP_CONFIG_CONTROLLER_MAX_ACTIVE_DEVICES> mActiveSessions;

DeviceProxyInitParams mParams;
OperationalDeviceProxyPoolDelegate * mDevicePool;

};

} // namespace chip
2 changes: 2 additions & 0 deletions src/controller/CHIPCluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class DLL_EXPORT ClusterBase
CommandResponseSuccessCallback<typename RequestDataT::ResponseType> successCb,
CommandResponseFailureCallback failureCb, const Optional<uint16_t> & timedInvokeTimeoutMs)
{
HERE(".")
VerifyOrReturnError(mDevice != nullptr, CHIP_ERROR_INCORRECT_STATE);

auto onSuccessCb = [context, successCb](const app::ConcreteCommandPath & commandPath, const app::StatusIB & aStatus,
Expand Down Expand Up @@ -115,6 +116,7 @@ class DLL_EXPORT ClusterBase
WriteResponseSuccessCallback successCb, WriteResponseFailureCallback failureCb,
const Optional<uint16_t> & aTimedWriteTimeoutMs, WriteResponseDoneCallback doneCb = nullptr)
{
HERE(".")
VerifyOrReturnError(mDevice != nullptr, CHIP_ERROR_INCORRECT_STATE);

auto onSuccessCb = [context, successCb](const app::ConcreteAttributePath & commandPath) {
Expand Down
4 changes: 4 additions & 0 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ CHIP_ERROR DeviceController::Init(ControllerInitParams params)
mCASESessionManager = chip::Platform::New<CASESessionManager>(sessionManagerConfig);
VerifyOrReturnError(mCASESessionManager != nullptr, CHIP_ERROR_NO_MEMORY);

mGroupSessionManager = chip::Platform::New<GroupSessionManager>(deviceInitParams, &mDevicePool);
VerifyOrReturnError(mGroupSessionManager != nullptr, CHIP_ERROR_NO_MEMORY);

mSystemState = params.systemState->Retain();
mState = State::Initialized;
return CHIP_NO_ERROR;
Expand Down Expand Up @@ -744,6 +747,7 @@ CHIP_ERROR DeviceCommissioner::GetDeviceBeingCommissioned(NodeId deviceId, Commi
CHIP_ERROR DeviceCommissioner::GetConnectedDevice(NodeId deviceId, Callback::Callback<OnDeviceConnected> * onConnection,
Callback::Callback<OnDeviceConnectionFailure> * onFailure)
{
HERE(".")
return DeviceController::GetConnectedDevice(deviceId, onConnection, onFailure);
}

Expand Down
18 changes: 16 additions & 2 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include <app/CASEClientPool.h>
#include <app/CASESessionManager.h>
#include <app/GroupSessionManager.h>
#include <app/DeviceControllerInteractionModelDelegate.h>
#include <app/InteractionModelDelegate.h>
#include <app/OperationalDeviceProxy.h>
Expand Down Expand Up @@ -220,8 +221,20 @@ class DLL_EXPORT DeviceController : public SessionReleaseDelegate,
virtual CHIP_ERROR GetConnectedDevice(NodeId deviceId, Callback::Callback<OnDeviceConnected> * onConnection,
Callback::Callback<OnDeviceConnectionFailure> * onFailure)
{
VerifyOrReturnError(mState == State::Initialized, CHIP_ERROR_INCORRECT_STATE);
return mCASESessionManager->FindOrEstablishSession(mFabricInfo->GetPeerIdForNode(deviceId), onConnection, onFailure);
HERE(".")
if(IsGroupId(deviceId))
{
// Group device
HERE("GROUP DEVICE!")
VerifyOrReturnError(mState == State::Initialized, CHIP_ERROR_INCORRECT_STATE);
return mGroupSessionManager->FindOrEstablishSession(mFabricInfo, deviceId, onConnection, onFailure);
}
else
{
HERE(".")
VerifyOrReturnError(mState == State::Initialized, CHIP_ERROR_INCORRECT_STATE);
return mCASESessionManager->FindOrEstablishSession(mFabricInfo, deviceId, onConnection, onFailure);
}
}

/**
Expand Down Expand Up @@ -335,6 +348,7 @@ class DLL_EXPORT DeviceController : public SessionReleaseDelegate,
State mState;

CASESessionManager * mCASESessionManager = nullptr;
GroupSessionManager * mGroupSessionManager = nullptr;

Dnssd::DnssdCache<CHIP_CONFIG_MDNS_CACHE_SIZE> mDNSCache;
CASEClientPool<CHIP_CONFIG_CONTROLLER_MAX_ACTIVE_CASE_CLIENTS> mCASEClientPool;
Expand Down
Loading

0 comments on commit 5f04f0e

Please sign in to comment.