Skip to content

Commit

Permalink
Move ble to commissioning state machine (#12909)
Browse files Browse the repository at this point in the history
* Add network setup commands to commissioning parameters.

* Use full commissioning state machine for BLE.

Includes network setup, so removed manual network setup from
PairingCommand and tests.

* Add check for network parameters for BLE

* Move CommissioningParameters to CommissioingDelegate

* Restyled by autopep8

* Add command to TV app

* Wrong function call on cirque.

* Try again for cirque

* fix Incorrect error checking for networking states

* Debugging on CI becuase I can't get cirque workign locally.

* Pass size with thread TLV.

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
2 people authored and pull[bot] committed Oct 19, 2023
1 parent 25017f6 commit 5722993
Show file tree
Hide file tree
Showing 12 changed files with 333 additions and 355 deletions.
200 changes: 17 additions & 183 deletions examples/chip-tool/commands/pairing/PairingCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@
#include <setup_payload/QRCodeSetupPayloadParser.h>

using namespace ::chip;

constexpr uint64_t kBreadcrumb = 0;
using namespace ::chip::Controller;

CHIP_ERROR PairingCommand::RunCommand()
{
CurrentCommissioner().RegisterDeviceAddressUpdateDelegate(this);
CurrentCommissioner().RegisterPairingDelegate(this);
return RunInternal(mNodeId);
}
Expand Down Expand Up @@ -70,6 +68,20 @@ CHIP_ERROR PairingCommand::RunInternal(NodeId remoteId)
return err;
}

CommissioningParameters PairingCommand::GetCommissioningParameters()
{
CommissioningParameters commissioningParams;
if (mNetworkType == PairingNetworkType::WiFi)
{
commissioningParams.SetWifiCredentials(Controller::WifiCredentials(mSSID, mPassword));
}
else if (mNetworkType == PairingNetworkType::Thread)
{
commissioningParams.SetThreadOperationalDataset(mOperationalDataset);
}
return commissioningParams;
}

CHIP_ERROR PairingCommand::PairWithQRCode(NodeId remoteId)
{
return CurrentCommissioner().PairDevice(remoteId, mOnboardingPayload);
Expand All @@ -84,8 +96,8 @@ CHIP_ERROR PairingCommand::Pair(NodeId remoteId, PeerAddress address)
{
RendezvousParameters params =
RendezvousParameters().SetSetupPINCode(mSetupPINCode).SetDiscriminator(mDiscriminator).SetPeerAddress(address);

return CurrentCommissioner().PairDevice(remoteId, params);
CommissioningParameters commissioningParams = GetCommissioningParameters();
return CurrentCommissioner().PairDevice(remoteId, params, commissioningParams);
}

CHIP_ERROR PairingCommand::PairWithMdns(NodeId remoteId)
Expand Down Expand Up @@ -142,7 +154,6 @@ void PairingCommand::OnPairingComplete(CHIP_ERROR err)
if (err == CHIP_NO_ERROR)
{
ChipLogProgress(chipTool, "Pairing Success");
err = SetupNetwork();
}
else
{
Expand Down Expand Up @@ -183,183 +194,6 @@ void PairingCommand::OnCommissioningComplete(NodeId nodeId, CHIP_ERROR err)
SetCommandExitStatus(err);
}

CHIP_ERROR PairingCommand::SetupNetwork()
{

CHIP_ERROR err = CHIP_NO_ERROR;

switch (mNetworkType)
{
case PairingNetworkType::None:
case PairingNetworkType::Ethernet:
// Nothing to do here - device address has been resolved as part of the commissioning process.
break;
case PairingNetworkType::WiFi:
case PairingNetworkType::Thread:
err = CurrentCommissioner().GetDeviceBeingCommissioned(mNodeId, &mDevice);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(chipTool, "Setup failure! No pairing for device: %" PRIu64, mNodeId));

mCluster.Associate(mDevice, mEndpointId);

err = AddNetwork(mNetworkType);
VerifyOrExit(err == CHIP_NO_ERROR,
ChipLogError(chipTool, "Setup failure! Error calling AddOrUpdateWiFiNetwork: %s", ErrorStr(err)));
break;
}

exit:
return err;
}

CHIP_ERROR PairingCommand::AddNetwork(PairingNetworkType networkType)
{
return (networkType == PairingNetworkType::WiFi) ? AddOrUpdateWiFiNetwork() : AddOrUpdateThreadNetwork();
}

CHIP_ERROR PairingCommand::AddOrUpdateThreadNetwork()
{
Callback::Cancelable * successCallback = mOnAddOrUpdateThreadNetworkCallback.Cancel();
Callback::Cancelable * failureCallback = mOnFailureCallback.Cancel();

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

CHIP_ERROR PairingCommand::AddOrUpdateWiFiNetwork()
{
Callback::Cancelable * successCallback = mOnAddOrUpdateWiFiNetworkCallback.Cancel();
Callback::Cancelable * failureCallback = mOnFailureCallback.Cancel();

return mCluster.AddOrUpdateWiFiNetwork(successCallback, failureCallback, mSSID, mPassword, kBreadcrumb);
}

chip::ByteSpan PairingCommand::GetThreadNetworkId()
{
// For Thread devices the networkId is the extendedPanId and it is
// part of the dataset defined by OpenThread

Thread::OperationalDataset dataset;

if (dataset.Init(mOperationalDataset) != CHIP_NO_ERROR)
{
return ByteSpan();
}

if (dataset.GetExtendedPanId(mExtendedPanId) != CHIP_NO_ERROR)
{
return ByteSpan();
}

return ByteSpan(mExtendedPanId);
}

CHIP_ERROR PairingCommand::ConnectNetwork()
{
Callback::Cancelable * successCallback = mOnConnectNetworkCallback.Cancel();
Callback::Cancelable * failureCallback = mOnFailureCallback.Cancel();

ByteSpan networkId;
if (mNetworkType == PairingNetworkType::WiFi)
{
networkId = mSSID;
}
else
{
networkId = GetThreadNetworkId();
}

if (networkId.empty())
{
return CHIP_ERROR_INVALID_ARGUMENT;
}

return mCluster.ConnectNetwork(successCallback, failureCallback, networkId, kBreadcrumb);
}

void PairingCommand::OnDefaultFailureResponse(void * context, uint8_t status)
{
ChipLogProgress(chipTool, "Default Failure Response: 0x%02x", status);

PairingCommand * command = reinterpret_cast<PairingCommand *>(context);
command->SetCommandExitStatus(CHIP_ERROR_INTERNAL);
}

void PairingCommand::OnAddNetworkResponse(void * context, uint8_t errorCode, CharSpan debugText)
{
ChipLogProgress(chipTool, "AddNetworkResponse");

PairingCommand * command = reinterpret_cast<PairingCommand *>(context);

// Normally, the errorCode should be checked, but the current codebase send a default response
// instead of the command specific response. So errorCode is not set correctly.
// if (NetworkCommissioningStatus::kSuccess != errorCode)
// {
// ChipLogError(chipTool, "Setup failure. Error calling ConnectNetwork: %d", errorCode);
// command->SetCommandExitStatus(CHIP_ERROR_INTERNAL);
// return;
// }

CHIP_ERROR err = command->ConnectNetwork();
if (CHIP_NO_ERROR != err)
{
ChipLogError(chipTool, "Setup failure. Internal error calling ConnectNetwork: %s", ErrorStr(err));
command->SetCommandExitStatus(err);
return;
}

// When the accessory is configured as a SoftAP and WiFi is configured to an other network
// there won't be any response since the WiFi network is changing.
// So returns early, assuming everything goes well
// and the device address will be updated once a command is issued.
if (command->mPairingMode == PairingMode::SoftAP && command->mNetworkType == PairingNetworkType::WiFi)
{
command->SetCommandExitStatus(CHIP_NO_ERROR);
}
}

void PairingCommand::OnConnectNetworkResponse(void * context, uint8_t errorCode, CharSpan debugText, int32_t errorValue)
{
ChipLogProgress(chipTool, "ConnectNetworkResponse");

PairingCommand * command = reinterpret_cast<PairingCommand *>(context);

// Normally, the errorCode should be checked, but the current codebase send a default response
// instead of the command specific response. So errorCode is not set correctly.
// if (NetworkCommissioningStatus::kSuccess != errorCode)
// {
// ChipLogError(chipTool, "Setup failure. Error calling ConnectNetwork: %d", errorCode);
// command->SetCommandExitStatus(CHIP_ERROR_INTERNAL);
// return;
// }

CHIP_ERROR err = command->UpdateNetworkAddress();
if (CHIP_NO_ERROR != err)
{
ChipLogError(chipTool, "Setup failure. Internal error calling UpdateNetworkAddress: %s", ErrorStr(err));
command->SetCommandExitStatus(err);
return;
}
}

CHIP_ERROR PairingCommand::UpdateNetworkAddress()
{
ChipLogProgress(chipTool, "Mdns: Updating NodeId: %" PRIx64 " Compressed FabricId: %" PRIx64 " ...", mNodeId,
CurrentCommissioner().GetCompressedFabricId());
return CurrentCommissioner().UpdateDevice(mNodeId);
}

void PairingCommand::OnAddressUpdateComplete(NodeId nodeId, CHIP_ERROR err)
{
ChipLogProgress(chipTool, "OnAddressUpdateComplete: %" PRIx64 ": %s", nodeId, ErrorStr(err));
if (err != CHIP_NO_ERROR)
{
// For some devices, it may take more time to appear on the network and become discoverable
// over DNS-SD, so don't give up on failure and restart the address update. Note that this
// will not be repeated endlessly as each chip-tool command has a timeout (in the case of
// the `pairing` command it equals 120s).
UpdateNetworkAddress();
}
}

void PairingCommand::OnDiscoveredDevice(const chip::Dnssd::DiscoveredNodeData & nodeData)
{
const uint16_t port = nodeData.port;
Expand Down
31 changes: 3 additions & 28 deletions examples/chip-tool/commands/pairing/PairingCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#pragma once

#include "../common/CHIPCommand.h"
#include <controller/CommissioningDelegate.h>
#include <zap-generated/CHIPClientCallbacks.h>
#include <zap-generated/CHIPClusters.h>

Expand Down Expand Up @@ -46,18 +47,14 @@ enum class PairingNetworkType

class PairingCommand : public CHIPCommand,
public chip::Controller::DevicePairingDelegate,
public chip::Controller::DeviceAddressUpdateDelegate,
public chip::Controller::DeviceDiscoveryDelegate
{
public:
PairingCommand(const char * commandName, PairingMode mode, PairingNetworkType networkType,
chip::Dnssd::DiscoveryFilterType filterType = chip::Dnssd::DiscoveryFilterType::kNone) :
CHIPCommand(commandName),
mPairingMode(mode), mNetworkType(networkType),
mFilterType(filterType), mRemoteAddr{ IPAddress::Any, chip::Inet::InterfaceId::Null() },
mOnAddOrUpdateThreadNetworkCallback(OnAddNetworkResponse, this),
mOnAddOrUpdateWiFiNetworkCallback(OnAddNetworkResponse, this), mOnConnectNetworkCallback(OnConnectNetworkResponse, this),
mOnFailureCallback(OnDefaultFailureResponse, this)
mFilterType(filterType), mRemoteAddr{ IPAddress::Any, chip::Inet::InterfaceId::Null() }
{
AddArgument("node-id", 0, UINT64_MAX, &mNodeId);

Expand Down Expand Up @@ -145,14 +142,6 @@ class PairingCommand : public CHIPCommand,
/////////// DeviceDiscoveryDelegate Interface /////////
void OnDiscoveredDevice(const chip::Dnssd::DiscoveredNodeData & nodeData) override;

/////////// DeviceAddressUpdateDelegate Interface /////////
void OnAddressUpdateComplete(NodeId nodeId, CHIP_ERROR error) override;

/////////// Network Commissioning Callbacks /////////
static void OnDefaultFailureResponse(void * context, uint8_t status);
static void OnAddNetworkResponse(void * context, uint8_t errorCode, chip::CharSpan debugText);
static void OnConnectNetworkResponse(void * context, uint8_t errorCode, chip::CharSpan debugText, int32_t errorValue);

private:
CHIP_ERROR RunInternal(NodeId remoteId);
CHIP_ERROR Pair(NodeId remoteId, PeerAddress address);
Expand All @@ -161,15 +150,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 SetupNetwork();
CHIP_ERROR AddNetwork(PairingNetworkType networkType);
CHIP_ERROR AddOrUpdateThreadNetwork();
CHIP_ERROR AddOrUpdateWiFiNetwork();
CHIP_ERROR ConnectNetwork();
CHIP_ERROR UpdateNetworkAddress();

chip::ByteSpan GetThreadNetworkId();
chip::Controller::CommissioningParameters GetCommissioningParameters();

const PairingMode mPairingMode;
const PairingNetworkType mNetworkType;
Expand All @@ -180,18 +161,12 @@ class PairingCommand : public CHIPCommand,
uint16_t mDiscriminator;
uint32_t mSetupPINCode;
chip::ByteSpan mOperationalDataset;
uint8_t mExtendedPanId[chip::Thread::kSizeExtendedPanId];
chip::ByteSpan mSSID;
chip::ByteSpan mPassword;
char * mOnboardingPayload;
uint64_t mDiscoveryFilterCode;
char * mDiscoveryFilterInstanceName;

chip::Callback::Callback<NetworkCommissioningClusterNetworkConfigResponseCallback> mOnAddOrUpdateThreadNetworkCallback;
chip::Callback::Callback<NetworkCommissioningClusterNetworkConfigResponseCallback> mOnAddOrUpdateWiFiNetworkCallback;
chip::Callback::Callback<NetworkCommissioningClusterConnectNetworkResponseCallback> mOnConnectNetworkCallback;
chip::Callback::Callback<DefaultFailureCallback> mOnFailureCallback;
chip::CommissioneeDeviceProxy * mDevice;
chip::Controller::NetworkCommissioningCluster mCluster;
chip::EndpointId mEndpointId = 0;
};
Loading

0 comments on commit 5722993

Please sign in to comment.