Skip to content

Commit

Permalink
[chip-tool] Move OpenCommissioningWindowCommand to a dedicated file (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple authored and pull[bot] committed Feb 8, 2022
1 parent 2a1c630 commit 1573345
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 141 deletions.
2 changes: 2 additions & 0 deletions examples/chip-tool/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ executable("chip-tool") {
# TODO - enable CommissionedListCommand once DNS Cache is implemented
# "commands/pairing/CommissionedListCommand.cpp",
# "commands/pairing/CommissionedListCommand.h",
"commands/pairing/OpenCommissioningWindowCommand.cpp",
"commands/pairing/OpenCommissioningWindowCommand.h",
"commands/pairing/PairingCommand.cpp",
"commands/payload/AdditionalDataParseCommand.cpp",
"commands/payload/SetupPayloadParseCommand.cpp",
Expand Down
22 changes: 7 additions & 15 deletions examples/chip-tool/commands/clusters/ModelCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,21 @@ using namespace ::chip;
CHIP_ERROR ModelCommand::RunCommand()
{
ChipLogProgress(chipTool, "Sending command to node 0x%" PRIx64, mNodeId);

CHIP_ERROR err =
CurrentCommissioner().GetConnectedDevice(mNodeId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback);
VerifyOrExit(err == CHIP_NO_ERROR,
ChipLogError(chipTool, "Failed in initiating connection to the device: %" PRIu64 ", error %" CHIP_ERROR_FORMAT,
mNodeId, err.Format()));

exit:
return err;
return CurrentCommissioner().GetConnectedDevice(mNodeId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback);
}

void ModelCommand::OnDeviceConnectedFn(void * context, ChipDevice * device)
{
ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
VerifyOrReturn(command != nullptr,
ChipLogError(chipTool, "Device connected, but cannot send the command, as the context is null"));
VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "OnDeviceConnectedFn: context is null"));
command->SendCommand(device, command->mEndPointId);
}

void ModelCommand::OnDeviceConnectionFailureFn(void * context, NodeId deviceId, CHIP_ERROR error)
void ModelCommand::OnDeviceConnectionFailureFn(void * context, NodeId deviceId, CHIP_ERROR err)
{
LogErrorOnFailure(err);

ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
ChipLogError(chipTool, "Failed in connecting to the device %" PRIu64 ". Error %" CHIP_ERROR_FORMAT, deviceId, error.Format());
VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "ModelCommand context is null"));
command->SetCommandExitStatus(error);
VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "OnDeviceConnectionFailureFn: context is null"));
command->SetCommandExitStatus(err);
}
11 changes: 2 additions & 9 deletions examples/chip-tool/commands/pairing/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#pragma once

#include "CommissionedListCommand.h"
#include "OpenCommissioningWindowCommand.h"
#include "PairingCommand.h"

#include <app/server/Dnssd.h>
Expand Down Expand Up @@ -143,14 +144,6 @@ class Ethernet : public PairingCommand
Ethernet() : PairingCommand("ethernet", PairingMode::Ethernet, PairingNetworkType::Ethernet) {}
};

class OpenCommissioningWindow : public PairingCommand
{
public:
OpenCommissioningWindow() :
PairingCommand("open-commissioning-window", PairingMode::OpenCommissioningWindow, PairingNetworkType::None)
{}
};

class StartUdcServerCommand : public CHIPCommand
{
public:
Expand Down Expand Up @@ -185,10 +178,10 @@ void registerCommandsPairing(Commands & commands)
make_unique<PairOnNetworkDeviceType>(),
make_unique<PairOnNetworkDeviceType>(),
make_unique<PairOnNetworkInstanceName>(),
make_unique<OpenCommissioningWindow>(),
// TODO - enable CommissionedListCommand once DNS Cache is implemented
// make_unique<CommissionedListCommand>(),
make_unique<StartUdcServerCommand>(),
make_unique<OpenCommissioningWindowCommand>(),
};

commands.Register(clusterName, clusterCommands);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 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 "OpenCommissioningWindowCommand.h"

using namespace ::chip;

CHIP_ERROR OpenCommissioningWindowCommand::RunCommand()
{
return CurrentCommissioner().GetConnectedDevice(mNodeId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback);
}

void OpenCommissioningWindowCommand::OnDeviceConnectedFn(void * context, chip::OperationalDeviceProxy * device)
{
OpenCommissioningWindowCommand * command = reinterpret_cast<OpenCommissioningWindowCommand *>(context);
VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "OnDeviceConnectedFn: context is null"));
command->OpenCommissioningWindow();
}
void OpenCommissioningWindowCommand::OnDeviceConnectionFailureFn(void * context, NodeId remoteId, CHIP_ERROR err)
{
LogErrorOnFailure(err);

OpenCommissioningWindowCommand * command = reinterpret_cast<OpenCommissioningWindowCommand *>(context);
VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "OnDeviceConnectionFailureFn: context is null"));
command->SetCommandExitStatus(err);
}

void OpenCommissioningWindowCommand::OnOpenCommissioningWindowResponse(void * context, NodeId remoteId, CHIP_ERROR err,
chip::SetupPayload payload)
{
LogErrorOnFailure(err);

OpenCommissioningWindowCommand * command = reinterpret_cast<OpenCommissioningWindowCommand *>(context);
VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "OnOpenCommissioningWindowCommand: context is null"));
command->SetCommandExitStatus(err);
}

CHIP_ERROR OpenCommissioningWindowCommand::OpenCommissioningWindow()
{
return CurrentCommissioner().OpenCommissioningWindowWithCallback(
mNodeId, mTimeout, mIteration, mDiscriminator, mCommissioningWindowOption, &mOnOpenCommissioningWindowCallback);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 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 "../common/CHIPCommand.h"

class OpenCommissioningWindowCommand : public CHIPCommand
{
public:
OpenCommissioningWindowCommand() :
CHIPCommand("open-commissioning-window"), mOnDeviceConnectedCallback(OnDeviceConnectedFn, this),
mOnDeviceConnectionFailureCallback(OnDeviceConnectionFailureFn, this),
mOnOpenCommissioningWindowCallback(OnOpenCommissioningWindowResponse, this)
{
AddArgument("node-id", 0, UINT64_MAX, &mNodeId);
AddArgument("option", 0, UINT8_MAX, &mCommissioningWindowOption);
AddArgument("timeout", 0, UINT16_MAX, &mTimeout);
AddArgument("iteration", 0, UINT16_MAX, &mIteration);
AddArgument("discriminator", 0, 4096, &mDiscriminator);
}

/////////// CHIPCommand Interface /////////
CHIP_ERROR RunCommand() override;
chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Seconds16(5); }

private:
NodeId mNodeId;
uint8_t mCommissioningWindowOption;
uint16_t mTimeout;
uint16_t mIteration;
uint16_t mDiscriminator;

CHIP_ERROR OpenCommissioningWindow();
static void OnDeviceConnectedFn(void * context, chip::OperationalDeviceProxy * 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::OnDeviceConnected> mOnDeviceConnectedCallback;
chip::Callback::Callback<chip::OnDeviceConnectionFailure> mOnDeviceConnectionFailureCallback;
chip::Callback::Callback<chip::Controller::OnOpenCommissioningWindow> mOnOpenCommissioningWindowCallback;
};
92 changes: 9 additions & 83 deletions examples/chip-tool/commands/pairing/PairingCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,15 @@ constexpr uint32_t kTimeoutMs = 6000;

CHIP_ERROR PairingCommand::RunCommand()
{
CHIP_ERROR err = CHIP_NO_ERROR;

// If we're OpenCommissioningWindow we don't need to be registered as a
// delegate; we just get notified directly via the callbacks we pass to
// GetConnectedDevice. In fact, if we _do_ register as a delegate we get
// callbacks we don't expect and then weird things happen.
if (mPairingMode != PairingMode::OpenCommissioningWindow)
{
CurrentCommissioner().RegisterDeviceAddressUpdateDelegate(this);
CurrentCommissioner().RegisterPairingDelegate(this);
}

err = RunInternal(mNodeId);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(chipTool, "Init Failure! PairDevice: %s", ErrorStr(err)));

exit:
return err;
CurrentCommissioner().RegisterDeviceAddressUpdateDelegate(this);
CurrentCommissioner().RegisterPairingDelegate(this);
return RunInternal(mNodeId);
}

CHIP_ERROR PairingCommand::RunInternal(NodeId remoteId)
{
CHIP_ERROR err = CHIP_NO_ERROR;

InitCallbacks();

switch (mPairingMode)
{
case PairingMode::None:
Expand All @@ -81,33 +65,12 @@ CHIP_ERROR PairingCommand::RunInternal(NodeId remoteId)
break;
case PairingMode::Ethernet:
err = Pair(remoteId, PeerAddress::UDP(mRemoteAddr.address, mRemotePort));
break;
case PairingMode::OpenCommissioningWindow:
err = CurrentCommissioner().GetConnectedDevice(remoteId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback);
if (err != CHIP_NO_ERROR)
{
ChipLogError(chipTool, "Failed in initiating connection to the device: %" PRIu64 ", error %" CHIP_ERROR_FORMAT,
remoteId, err.Format());
}

break;
}

return err;
}

void PairingCommand::OnDeviceConnectedFn(void * context, chip::OperationalDeviceProxy * device)
{
PairingCommand * command = reinterpret_cast<PairingCommand *>(context);
command->OpenCommissioningWindow();
}
void PairingCommand::OnDeviceConnectionFailureFn(void * context, NodeId deviceId, CHIP_ERROR error)
{
PairingCommand * command = reinterpret_cast<PairingCommand *>(context);
ChipLogError(chipTool, "Failed in connecting to the device %" PRIu64 ". Error %" CHIP_ERROR_FORMAT, deviceId, error.Format());
command->SetCommandExitStatus(error);
}

CHIP_ERROR PairingCommand::PairWithQRCode(NodeId remoteId)
{
return CurrentCommissioner().PairDevice(remoteId, mOnboardingPayload);
Expand Down Expand Up @@ -162,24 +125,6 @@ CHIP_ERROR PairingCommand::Unpair(NodeId remoteId)
return err;
}

void PairingCommand::OnOpenCommissioningWindowResponse(void * context, NodeId remoteId, CHIP_ERROR err, chip::SetupPayload payload)
{
PairingCommand * command = reinterpret_cast<PairingCommand *>(context);
if (err != CHIP_NO_ERROR)
{
ChipLogError(chipTool,
"Failed in opening commissioning window on the device: 0x" ChipLogFormatX64 ", error %" CHIP_ERROR_FORMAT,
ChipLogValueX64(remoteId), err.Format());
}
command->SetCommandExitStatus(err);
}

CHIP_ERROR PairingCommand::OpenCommissioningWindow()
{
return CurrentCommissioner().OpenCommissioningWindowWithCallback(
mNodeId, mTimeout, mIteration, mDiscriminator, mCommissioningWindowOption, &mOnOpenCommissioningWindowCallback);
}

void PairingCommand::OnStatusUpdate(DevicePairingDelegate::Status status)
{
switch (status)
Expand Down Expand Up @@ -267,42 +212,23 @@ CHIP_ERROR PairingCommand::SetupNetwork()
return err;
}

void PairingCommand::InitCallbacks()
{
mOnAddThreadNetworkCallback =
new Callback::Callback<NetworkCommissioningClusterAddThreadNetworkResponseCallback>(OnAddNetworkResponse, this);
mOnAddWiFiNetworkCallback =
new Callback::Callback<NetworkCommissioningClusterAddWiFiNetworkResponseCallback>(OnAddNetworkResponse, this);
mOnEnableNetworkCallback =
new Callback::Callback<NetworkCommissioningClusterEnableNetworkResponseCallback>(OnEnableNetworkResponse, this);
mOnFailureCallback = new Callback::Callback<DefaultFailureCallback>(OnDefaultFailureResponse, this);
}

void PairingCommand::Shutdown()
{
delete mOnAddThreadNetworkCallback;
delete mOnAddWiFiNetworkCallback;
delete mOnEnableNetworkCallback;
delete mOnFailureCallback;
}

CHIP_ERROR PairingCommand::AddNetwork(PairingNetworkType networkType)
{
return (networkType == PairingNetworkType::WiFi) ? AddWiFiNetwork() : AddThreadNetwork();
}

CHIP_ERROR PairingCommand::AddThreadNetwork()
{
Callback::Cancelable * successCallback = mOnAddThreadNetworkCallback->Cancel();
Callback::Cancelable * failureCallback = mOnFailureCallback->Cancel();
Callback::Cancelable * successCallback = mOnAddThreadNetworkCallback.Cancel();
Callback::Cancelable * failureCallback = mOnFailureCallback.Cancel();

return mCluster.AddThreadNetwork(successCallback, failureCallback, mOperationalDataset, kBreadcrumb, kTimeoutMs);
}

CHIP_ERROR PairingCommand::AddWiFiNetwork()
{
Callback::Cancelable * successCallback = mOnAddWiFiNetworkCallback->Cancel();
Callback::Cancelable * failureCallback = mOnFailureCallback->Cancel();
Callback::Cancelable * successCallback = mOnAddWiFiNetworkCallback.Cancel();
Callback::Cancelable * failureCallback = mOnFailureCallback.Cancel();

return mCluster.AddWiFiNetwork(successCallback, failureCallback, mSSID, mPassword, kBreadcrumb, kTimeoutMs);
}
Expand All @@ -329,8 +255,8 @@ chip::ByteSpan PairingCommand::GetThreadNetworkId()

CHIP_ERROR PairingCommand::EnableNetwork()
{
Callback::Cancelable * successCallback = mOnEnableNetworkCallback->Cancel();
Callback::Cancelable * failureCallback = mOnFailureCallback->Cancel();
Callback::Cancelable * successCallback = mOnEnableNetworkCallback.Cancel();
Callback::Cancelable * failureCallback = mOnFailureCallback.Cancel();

ByteSpan networkId;
if (mNetworkType == PairingNetworkType::WiFi)
Expand Down
Loading

0 comments on commit 1573345

Please sign in to comment.