diff --git a/examples/chip-tool/BUILD.gn b/examples/chip-tool/BUILD.gn index 88f4b2eb3bcb8c..c78ea55662db76 100644 --- a/examples/chip-tool/BUILD.gn +++ b/examples/chip-tool/BUILD.gn @@ -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", diff --git a/examples/chip-tool/commands/clusters/ModelCommand.cpp b/examples/chip-tool/commands/clusters/ModelCommand.cpp index 79a3089679fdf4..01406250d7a360 100644 --- a/examples/chip-tool/commands/clusters/ModelCommand.cpp +++ b/examples/chip-tool/commands/clusters/ModelCommand.cpp @@ -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(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(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); } diff --git a/examples/chip-tool/commands/pairing/Commands.h b/examples/chip-tool/commands/pairing/Commands.h index b6040efc34ed52..564bed55a2a889 100644 --- a/examples/chip-tool/commands/pairing/Commands.h +++ b/examples/chip-tool/commands/pairing/Commands.h @@ -19,6 +19,7 @@ #pragma once #include "CommissionedListCommand.h" +#include "OpenCommissioningWindowCommand.h" #include "PairingCommand.h" #include @@ -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: @@ -185,10 +178,10 @@ void registerCommandsPairing(Commands & commands) make_unique(), make_unique(), make_unique(), - make_unique(), // TODO - enable CommissionedListCommand once DNS Cache is implemented // make_unique(), make_unique(), + make_unique(), }; commands.Register(clusterName, clusterCommands); diff --git a/examples/chip-tool/commands/pairing/OpenCommissioningWindowCommand.cpp b/examples/chip-tool/commands/pairing/OpenCommissioningWindowCommand.cpp new file mode 100644 index 00000000000000..8c8576d9e30311 --- /dev/null +++ b/examples/chip-tool/commands/pairing/OpenCommissioningWindowCommand.cpp @@ -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(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(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(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); +} diff --git a/examples/chip-tool/commands/pairing/OpenCommissioningWindowCommand.h b/examples/chip-tool/commands/pairing/OpenCommissioningWindowCommand.h new file mode 100644 index 00000000000000..92130cdb02ceca --- /dev/null +++ b/examples/chip-tool/commands/pairing/OpenCommissioningWindowCommand.h @@ -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 mOnDeviceConnectedCallback; + chip::Callback::Callback mOnDeviceConnectionFailureCallback; + chip::Callback::Callback mOnOpenCommissioningWindowCallback; +}; diff --git a/examples/chip-tool/commands/pairing/PairingCommand.cpp b/examples/chip-tool/commands/pairing/PairingCommand.cpp index 0d9524f2c84bd1..62ed4e94ca2e36 100644 --- a/examples/chip-tool/commands/pairing/PairingCommand.cpp +++ b/examples/chip-tool/commands/pairing/PairingCommand.cpp @@ -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: @@ -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(context); - command->OpenCommissioningWindow(); -} -void PairingCommand::OnDeviceConnectionFailureFn(void * context, NodeId deviceId, CHIP_ERROR error) -{ - PairingCommand * command = reinterpret_cast(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); @@ -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(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) @@ -267,25 +212,6 @@ CHIP_ERROR PairingCommand::SetupNetwork() return err; } -void PairingCommand::InitCallbacks() -{ - mOnAddThreadNetworkCallback = - new Callback::Callback(OnAddNetworkResponse, this); - mOnAddWiFiNetworkCallback = - new Callback::Callback(OnAddNetworkResponse, this); - mOnEnableNetworkCallback = - new Callback::Callback(OnEnableNetworkResponse, this); - mOnFailureCallback = new Callback::Callback(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(); @@ -293,16 +219,16 @@ CHIP_ERROR PairingCommand::AddNetwork(PairingNetworkType networkType) 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); } @@ -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) diff --git a/examples/chip-tool/commands/pairing/PairingCommand.h b/examples/chip-tool/commands/pairing/PairingCommand.h index 92932efe91246c..e5d0dc36ec00b7 100644 --- a/examples/chip-tool/commands/pairing/PairingCommand.h +++ b/examples/chip-tool/commands/pairing/PairingCommand.h @@ -18,15 +18,12 @@ #pragma once -#include "../../config/PersistentStorage.h" #include "../common/CHIPCommand.h" #include #include -#include #include #include -#include enum class PairingMode { @@ -37,7 +34,6 @@ enum class PairingMode SoftAP, Ethernet, OnNetwork, - OpenCommissioningWindow, }; enum class PairingNetworkType @@ -59,9 +55,8 @@ class PairingCommand : public CHIPCommand, CHIPCommand(commandName), mPairingMode(mode), mNetworkType(networkType), mFilterType(filterType), mRemoteAddr{ IPAddress::Any, chip::Inet::InterfaceId::Null() }, - mOnDeviceConnectedCallback(OnDeviceConnectedFn, this), - mOnDeviceConnectionFailureCallback(OnDeviceConnectionFailureFn, this), - mOnOpenCommissioningWindowCallback(OnOpenCommissioningWindowResponse, this) + mOnAddThreadNetworkCallback(OnAddNetworkResponse, this), mOnAddWiFiNetworkCallback(OnAddNetworkResponse, this), + mOnEnableNetworkCallback(OnEnableNetworkResponse, this), mOnFailureCallback(OnDefaultFailureResponse, this) { AddArgument("node-id", 0, UINT64_MAX, &mNodeId); @@ -88,7 +83,6 @@ class PairingCommand : public CHIPCommand, AddArgument("payload", &mOnboardingPayload); break; case PairingMode::Ble: - AddArgument("fabric-id", 0, UINT64_MAX, &mFabricId); AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode); AddArgument("discriminator", 0, 4096, &mDiscriminator); break; @@ -96,7 +90,6 @@ class PairingCommand : public CHIPCommand, AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode); break; case PairingMode::SoftAP: - AddArgument("fabric-id", 0, UINT64_MAX, &mFabricId); AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode); AddArgument("discriminator", 0, 4096, &mDiscriminator); AddArgument("device-remote-ip", &mRemoteAddr); @@ -108,12 +101,6 @@ class PairingCommand : public CHIPCommand, AddArgument("device-remote-ip", &mRemoteAddr); AddArgument("device-remote-port", 0, UINT16_MAX, &mRemotePort); break; - case PairingMode::OpenCommissioningWindow: - AddArgument("option", 0, UINT8_MAX, &mCommissioningWindowOption); - AddArgument("timeout", 0, UINT16_MAX, &mTimeout); - AddArgument("iteration", 0, UINT16_MAX, &mIteration); - AddArgument("discriminator", 0, 4096, &mDiscriminator); - break; } switch (filterType) @@ -147,7 +134,6 @@ class PairingCommand : public CHIPCommand, /////////// CHIPCommand Interface ///////// CHIP_ERROR RunCommand() override; chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Seconds16(120); } - void Shutdown() override; /////////// DevicePairingDelegate Interface ///////// void OnStatusUpdate(chip::Controller::DevicePairingDelegate::Status status) override; @@ -174,9 +160,7 @@ class PairingCommand : public CHIPCommand, CHIP_ERROR PairWithManualCode(NodeId remoteId); CHIP_ERROR PairWithCode(NodeId remoteId, chip::SetupPayload payload); CHIP_ERROR Unpair(NodeId remoteId); - CHIP_ERROR OpenCommissioningWindow(); - void InitCallbacks(); CHIP_ERROR SetupNetwork(); CHIP_ERROR AddNetwork(PairingNetworkType networkType); CHIP_ERROR AddThreadNetwork(); @@ -192,12 +176,8 @@ class PairingCommand : public CHIPCommand, Command::AddressWithInterface mRemoteAddr; NodeId mNodeId; uint16_t mRemotePort; - uint64_t mFabricId; - uint16_t mTimeout; - uint16_t mIteration; uint16_t mDiscriminator; uint32_t mSetupPINCode; - uint8_t mCommissioningWindowOption; chip::ByteSpan mOperationalDataset; uint8_t mExtendedPanId[chip::Thread::kSizeExtendedPanId]; chip::ByteSpan mSSID; @@ -206,19 +186,11 @@ class PairingCommand : public CHIPCommand, uint64_t mDiscoveryFilterCode; char * mDiscoveryFilterInstanceName; - chip::Callback::Callback * mOnAddThreadNetworkCallback = nullptr; - chip::Callback::Callback * mOnAddWiFiNetworkCallback = nullptr; - chip::Callback::Callback * mOnEnableNetworkCallback = nullptr; - chip::Callback::Callback * mOnFailureCallback = nullptr; + chip::Callback::Callback mOnAddThreadNetworkCallback; + chip::Callback::Callback mOnAddWiFiNetworkCallback; + chip::Callback::Callback mOnEnableNetworkCallback; + chip::Callback::Callback mOnFailureCallback; chip::CommissioneeDeviceProxy * mDevice; chip::Controller::NetworkCommissioningCluster mCluster; chip::EndpointId mEndpointId = 0; - - 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 mOnDeviceConnectedCallback; - chip::Callback::Callback mOnDeviceConnectionFailureCallback; - chip::Callback::Callback mOnOpenCommissioningWindowCallback; };