Skip to content

Commit

Permalink
Refactor CHIPDevice class to Operational and Commissionee device proxy (
Browse files Browse the repository at this point in the history
#10808)

* Refactor CHIPDevice class to Operational and Commissionee device proxy

* Remove use of storage and serializable in CommissioneeDeviceProxy

* address review comments

* fix build errors

* Fix build of ameba platform

* rebase and fixes

* fixes after rebase

* fix Linux build
  • Loading branch information
pan-apple authored Nov 8, 2021
1 parent b717b36 commit a04576d
Show file tree
Hide file tree
Showing 76 changed files with 3,459 additions and 3,377 deletions.
6 changes: 4 additions & 2 deletions examples/chip-tool/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ executable("chip-tool") {
"commands/discover/DiscoverCommand.cpp",
"commands/discover/DiscoverCommissionablesCommand.cpp",
"commands/discover/DiscoverCommissionersCommand.cpp",
"commands/pairing/CommissionedListCommand.cpp",
"commands/pairing/CommissionedListCommand.h",

# TODO - enable CommissionedListCommand once DNS Cache is implemented
# "commands/pairing/CommissionedListCommand.cpp",
# "commands/pairing/CommissionedListCommand.h",
"commands/pairing/PairingCommand.cpp",
"commands/payload/AdditionalDataParseCommand.cpp",
"commands/payload/SetupPayloadParseCommand.cpp",
Expand Down
2 changes: 1 addition & 1 deletion examples/chip-tool/commands/clusters/ModelCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ CHIP_ERROR ModelCommand::RunCommand()
return err;
}

void ModelCommand::OnDeviceConnectedFn(void * context, chip::Controller::Device * device)
void ModelCommand::OnDeviceConnectedFn(void * context, ChipDevice * device)
{
ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
VerifyOrReturn(command != nullptr,
Expand Down
6 changes: 3 additions & 3 deletions examples/chip-tool/commands/clusters/ModelCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
class ModelCommand : public CHIPCommand
{
public:
using ChipDevice = ::chip::Controller::Device;
using ChipDevice = ::chip::DeviceProxy;

ModelCommand(const char * commandName) :
CHIPCommand(commandName), mOnDeviceConnectedCallback(OnDeviceConnectedFn, this),
Expand All @@ -57,6 +57,6 @@ class ModelCommand : public CHIPCommand
static void OnDeviceConnectedFn(void * context, ChipDevice * device);
static void OnDeviceConnectionFailureFn(void * context, NodeId deviceId, CHIP_ERROR error);

chip::Callback::Callback<chip::Controller::OnDeviceConnected> mOnDeviceConnectedCallback;
chip::Callback::Callback<chip::Controller::OnDeviceConnectionFailure> mOnDeviceConnectionFailureCallback;
chip::Callback::Callback<chip::OnDeviceConnected> mOnDeviceConnectedCallback;
chip::Callback::Callback<chip::OnDeviceConnectionFailure> mOnDeviceConnectionFailureCallback;
};
3 changes: 1 addition & 2 deletions examples/chip-tool/commands/common/CHIPCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ class PersistentStorage;
class CHIPCommand : public Command
{
public:
using ChipDevice = ::chip::Controller::Device;
using ChipDevice = ::chip::DeviceProxy;
using ChipDeviceCommissioner = ::chip::Controller::DeviceCommissioner;
using ChipDeviceController = ::chip::Controller::DeviceController;
using ChipSerializedDevice = ::chip::Controller::SerializedDevice;
using IPAddress = ::chip::Inet::IPAddress;
using NodeId = ::chip::NodeId;
using PeerAddress = ::chip::Transport::PeerAddress;
Expand Down
3 changes: 2 additions & 1 deletion examples/chip-tool/commands/pairing/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ void registerCommandsPairing(Commands & commands)
make_unique<PairOnNetworkDeviceType>(),
make_unique<PairOnNetworkInstanceName>(),
make_unique<OpenCommissioningWindow>(),
make_unique<CommissionedListCommand>(),
// TODO - enable CommissionedListCommand once DNS Cache is implemented
// make_unique<CommissionedListCommand>(),
};

commands.Register(clusterName, clusterCommands);
Expand Down
4 changes: 2 additions & 2 deletions examples/chip-tool/commands/pairing/PairingCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ CHIP_ERROR PairingCommand::RunInternal(NodeId remoteId)
return err;
}

void PairingCommand::OnDeviceConnectedFn(void * context, chip::Controller::Device * device)
void PairingCommand::OnDeviceConnectedFn(void * context, chip::DeviceProxy * device)
{
PairingCommand * command = reinterpret_cast<PairingCommand *>(context);
command->OpenCommissioningWindow();
Expand Down Expand Up @@ -248,7 +248,7 @@ CHIP_ERROR PairingCommand::SetupNetwork()
break;
case PairingNetworkType::WiFi:
case PairingNetworkType::Thread:
err = mController.GetDevice(mNodeId, &mDevice);
err = mController.GetDeviceBeingCommissioned(mNodeId, &mDevice);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(chipTool, "Setup failure! No pairing for device: %" PRIu64, mNodeId));

mCluster.Associate(mDevice, mEndpointId);
Expand Down
8 changes: 4 additions & 4 deletions examples/chip-tool/commands/pairing/PairingCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,15 @@ class PairingCommand : public CHIPCommand,
chip::Callback::Callback<NetworkCommissioningClusterAddWiFiNetworkResponseCallback> * mOnAddWiFiNetworkCallback = nullptr;
chip::Callback::Callback<NetworkCommissioningClusterEnableNetworkResponseCallback> * mOnEnableNetworkCallback = nullptr;
chip::Callback::Callback<DefaultFailureCallback> * mOnFailureCallback = nullptr;
ChipDevice * mDevice;
chip::CommissioneeDeviceProxy * mDevice;
chip::Controller::NetworkCommissioningCluster mCluster;
chip::EndpointId mEndpointId = 0;

static void OnDeviceConnectedFn(void * context, chip::Controller::Device * device);
static void OnDeviceConnectedFn(void * context, chip::DeviceProxy * device);
static void OnDeviceConnectionFailureFn(void * context, NodeId deviceId, CHIP_ERROR error);
static void OnOpenCommissioningWindowResponse(void * context, NodeId deviceId, CHIP_ERROR status, chip::SetupPayload payload);

chip::Callback::Callback<chip::Controller::OnDeviceConnected> mOnDeviceConnectedCallback;
chip::Callback::Callback<chip::Controller::OnDeviceConnectionFailure> mOnDeviceConnectionFailureCallback;
chip::Callback::Callback<chip::OnDeviceConnected> mOnDeviceConnectedCallback;
chip::Callback::Callback<chip::OnDeviceConnectionFailure> mOnDeviceConnectionFailureCallback;
chip::Callback::Callback<chip::Controller::OnOpenCommissioningWindow> mOnOpenCommissioningWindowCallback;
};
2 changes: 1 addition & 1 deletion examples/chip-tool/commands/reporting/ReportingCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ CHIP_ERROR ReportingCommand::RunCommand()
return err;
}

void ReportingCommand::OnDeviceConnectedFn(void * context, chip::Controller::Device * device)
void ReportingCommand::OnDeviceConnectedFn(void * context, chip::DeviceProxy * device)
{
ReportingCommand * command = reinterpret_cast<ReportingCommand *>(context);
VerifyOrReturn(command != nullptr,
Expand Down
6 changes: 3 additions & 3 deletions examples/chip-tool/commands/reporting/ReportingCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ class ReportingCommand : public CHIPCommand
NodeId mNodeId;
uint8_t mEndPointId;

static void OnDeviceConnectedFn(void * context, chip::Controller::Device * device);
static void OnDeviceConnectedFn(void * context, chip::DeviceProxy * device);
static void OnDeviceConnectionFailureFn(void * context, NodeId deviceId, CHIP_ERROR error);

chip::Callback::Callback<chip::Controller::OnDeviceConnected> mOnDeviceConnectedCallback;
chip::Callback::Callback<chip::Controller::OnDeviceConnectionFailure> mOnDeviceConnectionFailureCallback;
chip::Callback::Callback<chip::OnDeviceConnected> mOnDeviceConnectedCallback;
chip::Callback::Callback<chip::OnDeviceConnectionFailure> mOnDeviceConnectionFailureCallback;
};
2 changes: 1 addition & 1 deletion examples/chip-tool/commands/tests/TestCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ CHIP_ERROR TestCommand::RunCommand()
return mController.GetConnectedDevice(mNodeId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback);
}

void TestCommand::OnDeviceConnectedFn(void * context, chip::Controller::Device * device)
void TestCommand::OnDeviceConnectedFn(void * context, chip::DeviceProxy * device)
{
ChipLogProgress(chipTool, " **** Test Setup: Device Connected\n");
auto * command = static_cast<TestCommand *>(context);
Expand Down
6 changes: 3 additions & 3 deletions examples/chip-tool/commands/tests/TestCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class TestCommand : public CHIPCommand
ChipDevice * mDevice;
chip::NodeId mNodeId;

static void OnDeviceConnectedFn(void * context, chip::Controller::Device * device);
static void OnDeviceConnectedFn(void * context, chip::DeviceProxy * device);
static void OnDeviceConnectionFailureFn(void * context, NodeId deviceId, CHIP_ERROR error);
static void OnWaitForMsFn(chip::System::Layer * systemLayer, void * context);

Expand Down Expand Up @@ -230,8 +230,8 @@ class TestCommand : public CHIPCommand
return false;
}

chip::Callback::Callback<chip::Controller::OnDeviceConnected> mOnDeviceConnectedCallback;
chip::Callback::Callback<chip::Controller::OnDeviceConnectionFailure> mOnDeviceConnectionFailureCallback;
chip::Callback::Callback<chip::OnDeviceConnected> mOnDeviceConnectedCallback;
chip::Callback::Callback<chip::OnDeviceConnectionFailure> mOnDeviceConnectionFailureCallback;

void Wait()
{
Expand Down
1 change: 0 additions & 1 deletion examples/ota-requestor-app/linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ executable("chip-ota-requestor-app") {
deps = [
"${chip_root}/examples/ota-requestor-app/ota-requestor-common",
"${chip_root}/examples/platform/linux:app-main",
"${chip_root}/src/app/device",
"${chip_root}/src/app/server",
"${chip_root}/src/lib",
]
Expand Down
67 changes: 42 additions & 25 deletions examples/ota-requestor-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@

#include <app-common/zap-generated/callback.h>
#include <app-common/zap-generated/cluster-objects.h>
#include <app/device/OperationalDeviceProxy.h>
#include <app/OperationalDeviceProxy.h>
#include <app/server/Server.h>
#include <app/util/util.h>
#include <controller/CHIPDeviceControllerFactory.h>
#include <controller/CommissioneeDeviceProxy.h>
#include <controller/ExampleOperationalCredentialsIssuer.h>
#include <credentials/DeviceAttestationCredsProvider.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>
#include <lib/support/CHIPArgParser.hpp>
#include <platform/CHIPDeviceLayer.h>
Expand All @@ -31,9 +36,13 @@

using chip::ByteSpan;
using chip::CharSpan;
using chip::DeviceProxy;
using chip::EndpointId;
using chip::FabricIndex;
using chip::NodeId;
using chip::OnDeviceConnected;
using chip::OnDeviceConnectionFailure;
using chip::PeerId;
using chip::Server;
using chip::VendorId;
using chip::bdx::TransferSession;
Expand All @@ -43,21 +52,19 @@ using chip::System::Layer;
using chip::Transport::PeerAddress;
using namespace chip::ArgParser;
using namespace chip::Messaging;
using namespace chip::app::device;
using namespace chip::app::Clusters::OtaSoftwareUpdateProvider::Commands;

void OnQueryImageResponse(void * context, const QueryImageResponse::DecodableType & response);
void OnQueryImageFailure(void * context, EmberAfStatus status);
void OnConnected(void * context, OperationalDeviceProxy * operationalDeviceProxy);
void OnConnectionFailure(void * context, OperationalDeviceProxy * operationalDeviceProxy, CHIP_ERROR error);
void OnConnected(void * context, chip::DeviceProxy * deviceProxy);
void OnConnectionFailure(void * context, NodeId deviceId, CHIP_ERROR error);
bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier, const char * aName, const char * aValue);

// TODO: would be nicer to encapsulate these globals and the callbacks in some sort of class
OperationalDeviceProxy gOperationalDeviceProxy;
ExchangeContext * exchangeCtx = nullptr;
BdxDownloader bdxDownloader;
Callback<OnOperationalDeviceConnected> mOnConnectedCallback(OnConnected, nullptr);
Callback<OnOperationalDeviceConnectionFailure> mOnConnectionFailureCallback(OnConnectionFailure, nullptr);
Callback<OnDeviceConnected> mOnConnectedCallback(OnConnected, nullptr);
Callback<OnDeviceConnectionFailure> mOnConnectionFailureCallback(OnConnectionFailure, nullptr);

constexpr uint16_t kOptionProviderNodeId = 'n';
constexpr uint16_t kOptionProviderFabricIndex = 'f';
Expand Down Expand Up @@ -117,9 +124,11 @@ void OnQueryImageResponse(void * context, const QueryImageResponse::DecodableTyp
initOptions.FileDesLength = static_cast<uint16_t>(strlen(testFileDes));
initOptions.FileDesignator = reinterpret_cast<uint8_t *>(testFileDes);

chip::OperationalDeviceProxy * operationalDeviceProxy = Server::GetInstance().GetOperationalDeviceProxy();
if (operationalDeviceProxy != nullptr)
{
chip::Messaging::ExchangeManager * exchangeMgr = gOperationalDeviceProxy.GetDevice().GetExchangeManager();
chip::Optional<chip::SessionHandle> session = gOperationalDeviceProxy.GetDevice().GetSecureSession();
chip::Messaging::ExchangeManager * exchangeMgr = operationalDeviceProxy->GetExchangeManager();
chip::Optional<chip::SessionHandle> session = operationalDeviceProxy->GetSecureSession();
if (exchangeMgr != nullptr && session.HasValue())
{
exchangeCtx = exchangeMgr->NewContext(session.Value(), &bdxDownloader);
Expand All @@ -144,7 +153,7 @@ void OnQueryImageFailure(void * context, EmberAfStatus status)
ChipLogDetail(SoftwareUpdate, "QueryImage failure response %" PRIu8, status);
}

void OnConnected(void * context, OperationalDeviceProxy * operationalDeviceProxy)
void OnConnected(void * context, chip::DeviceProxy * deviceProxy)
{
CHIP_ERROR err = CHIP_NO_ERROR;
chip::Controller::OtaSoftwareUpdateProviderCluster cluster;
Expand All @@ -161,7 +170,7 @@ void OnConnected(void * context, OperationalDeviceProxy * operationalDeviceProxy
constexpr bool kExampleClientCanConsent = false;
ByteSpan metadata;

err = cluster.Associate(&(operationalDeviceProxy->GetDevice()), kOtaProviderEndpoint);
err = cluster.Associate(deviceProxy, kOtaProviderEndpoint);
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "Associate() failed: %" CHIP_ERROR_FORMAT, err.Format());
Expand All @@ -183,9 +192,9 @@ void OnConnected(void * context, OperationalDeviceProxy * operationalDeviceProxy
}
}

void OnConnectionFailure(void * context, OperationalDeviceProxy * operationalDeviceProxy, CHIP_ERROR error)
void OnConnectionFailure(void * context, NodeId deviceId, CHIP_ERROR error)
{
ChipLogError(SoftwareUpdate, "failed to connect: %" CHIP_ERROR_FORMAT, error.Format());
ChipLogError(SoftwareUpdate, "failed to connect to 0x%" PRIX64 ": %" CHIP_ERROR_FORMAT, deviceId, error.Format());
}

bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier, const char * aName, const char * aValue)
Expand Down Expand Up @@ -245,24 +254,32 @@ bool HandleOptions(const char * aProgram, OptionSet * aOptions, int aIdentifier,

void SendQueryImageCommand(chip::NodeId peerNodeId = providerNodeId, chip::FabricIndex peerFabricIndex = providerFabricIndex)
{
// Explicitly calling UpdateAddress() should not be needed once OperationalDeviceProxy can resolve IP address from node ID and
// fabric index
IPAddress ipAddr;
IPAddress::FromString(ipAddress, ipAddr);
PeerAddress addr = PeerAddress::UDP(ipAddr, CHIP_PORT);
gOperationalDeviceProxy.UpdateAddress(addr);

Server * server = &(Server::GetInstance());
OperationalDeviceProxyInitParams initParams = {
Server * server = &(Server::GetInstance());
chip::DeviceProxyInitParams initParams = {
.sessionManager = &(server->GetSecureSessionManager()),
.exchangeMgr = &(server->GetExchangeManager()),
.idAllocator = &(server->GetSessionIDAllocator()),
.fabricsTable = &(server->GetFabricTable()),
.fabricInfo = server->GetFabricTable().FindFabricWithIndex(providerFabricIndex),
// TODO: Determine where this should be instantiated
.imDelegate = chip::Platform::New<chip::Controller::DeviceControllerInteractionModelDelegate>(),
};

chip::OperationalDeviceProxy * operationalDeviceProxy =
new chip::OperationalDeviceProxy(initParams, PeerId().SetNodeId(providerNodeId));
server->SetOperationalDeviceProxy(operationalDeviceProxy);

// Explicitly calling UpdateDeviceData() should not be needed once OperationalDeviceProxy can resolve IP address from node ID
// and fabric index
IPAddress ipAddr;
IPAddress::FromString(ipAddress, ipAddr);
PeerAddress addr = PeerAddress::UDP(ipAddr, CHIP_PORT);
uint32_t idleInterval;
uint32_t activeInterval;
operationalDeviceProxy->GetMRPIntervals(idleInterval, activeInterval);
operationalDeviceProxy->UpdateDeviceData(addr, idleInterval, activeInterval);

CHIP_ERROR err = CHIP_NO_ERROR;
gOperationalDeviceProxy.Init(peerNodeId, peerFabricIndex, initParams);
err = gOperationalDeviceProxy.Connect(&mOnConnectedCallback, &mOnConnectionFailureCallback);
err = operationalDeviceProxy->Connect(&mOnConnectedCallback, &mOnConnectionFailureCallback);
if (err != CHIP_NO_ERROR)
{
ChipLogError(SoftwareUpdate, "Cannot establish connection to peer device: %" CHIP_ERROR_FORMAT, err.Format());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,8 @@

#include <ExampleOTARequestor.h>

#include <app-common/zap-generated/enums.h>
#include <app/util/af-enums.h>
#include <app/util/util.h>
#include <controller/CHIPDevice.h>
#include <lib/core/NodeId.h>
#include <lib/support/BufferReader.h>
#include <lib/support/Span.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/CHIPDeviceLayer.h>
#include <system/SystemClock.h>
#include <transport/FabricTable.h>

using chip::FabricInfo;

ExampleOTARequestor ExampleOTARequestor::sInstance;

Expand All @@ -53,9 +42,8 @@ ExampleOTARequestor::ExampleOTARequestor()
mProviderFabricIndex = chip::kUndefinedFabricIndex;
}

void ExampleOTARequestor::Init(chip::Controller::ControllerDeviceInitParams connectParams, uint32_t startDelayMs)
void ExampleOTARequestor::Init(uint32_t startDelayMs)
{
mConnectParams = connectParams;
mOtaStartDelayMs = startDelayMs;
}

Expand Down Expand Up @@ -128,14 +116,3 @@ void ExampleOTARequestor::StartDelayTimerHandler(chip::System::Layer * systemLay
VerifyOrReturn(appState != nullptr);
static_cast<ExampleOTARequestor *>(appState)->ConnectToProvider();
}

chip::FabricInfo * ExampleOTARequestor::GetProviderFabricInfo()
{
if (mConnectParams.fabricsTable == nullptr)
{
ChipLogError(SoftwareUpdate, "FabricTable is null!");
return nullptr;
}

return mConnectParams.fabricsTable->FindFabricWithIndex(mProviderFabricIndex);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
#pragma once

#include <app-common/zap-generated/cluster-objects.h>
#include <app/Command.h>
#include <app/util/basic-types.h>
#include <controller/CHIPDevice.h>
#include <lib/core/NodeId.h>
#include <lib/core/Optional.h>
#include <app/CommandHandler.h>
#include <app/util/af-enums.h>
#include <transport/FabricTable.h>

// An example implementation for how an application might handle receiving an AnnounceOTAProvider command. In this case, the
// AnnounceOTAProvider command will be used as a trigger to send a QueryImage command and begin the OTA process. This class also
Expand All @@ -33,7 +31,7 @@ class ExampleOTARequestor
public:
static ExampleOTARequestor & GetInstance() { return sInstance; }

void Init(chip::Controller::ControllerDeviceInitParams connectParams, uint32_t startDelayMs);
void Init(uint32_t startDelayMs);

EmberAfStatus HandleAnnounceOTAProvider(
chip::app::CommandHandler * commandObj, const chip::app::ConcreteCommandPath & commandPath,
Expand All @@ -47,12 +45,9 @@ class ExampleOTARequestor

static void StartDelayTimerHandler(chip::System::Layer * systemLayer, void * appState);
void ConnectToProvider();
chip::FabricInfo * GetProviderFabricInfo();

static ExampleOTARequestor sInstance;

chip::Controller::Device mProviderDevice;
chip::Controller::ControllerDeviceInitParams mConnectParams;
chip::NodeId mProviderNodeId;
chip::FabricIndex mProviderFabricIndex;
uint32_t mOtaStartDelayMs;
Expand Down
Loading

0 comments on commit a04576d

Please sign in to comment.