Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chip-tool] Get the commissioner to be an optional argument for chip … #12259

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions examples/chip-tool/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ executable("chip-tool") {
# TODO - enable CommissionedListCommand once DNS Cache is implemented
# "commands/pairing/CommissionedListCommand.cpp",
# "commands/pairing/CommissionedListCommand.h",
"commands/pairing/ConfigureFabricCommand.cpp",
"commands/pairing/ConfigureFabricCommand.h",
"commands/pairing/PairingCommand.cpp",
"commands/payload/AdditionalDataParseCommand.cpp",
"commands/payload/SetupPayloadParseCommand.cpp",
Expand Down
3 changes: 2 additions & 1 deletion examples/chip-tool/commands/clusters/ModelCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ CHIP_ERROR ModelCommand::RunCommand()
{
ChipLogProgress(chipTool, "Sending command to node 0x%" PRIx64, mNodeId);

CHIP_ERROR err = mController.GetConnectedDevice(mNodeId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback);
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()));
Expand Down
111 changes: 88 additions & 23 deletions examples/chip-tool/commands/common/CHIPCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,95 @@

using DeviceControllerFactory = chip::Controller::DeviceControllerFactory;

constexpr const char kCommissionerAlpha[] = "alpha";
constexpr const char kCommissionerBeta[] = "beta";
constexpr const char kCommissionerGamma[] = "gamma";

constexpr chip::FabricId kCommissionerAlphaFabricId = 1;
constexpr chip::FabricId kCommissionerBetaFabricId = 2;
constexpr chip::FabricId kCommissionerGammaFabricId = 3;

CHIP_ERROR CHIPCommand::Run()
{
#if CHIP_DEVICE_LAYER_TARGET_LINUX && CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
// By default, Linux device is configured as a BLE peripheral while the controller needs a BLE central.
ReturnLogErrorOnFailure(chip::DeviceLayer::Internal::BLEMgrImpl().ConfigureBle(0, true));
#endif

ReturnLogErrorOnFailure(mStorage.Init());
ReturnLogErrorOnFailure(mOpCredsIssuer.Initialize(mStorage));
ReturnLogErrorOnFailure(mFabricStorage.Initialize(&mStorage));
ReturnLogErrorOnFailure(mDefaultStorage.Init());
ReturnLogErrorOnFailure(mFabricStorage.Initialize(&mDefaultStorage));

chip::Controller::FactoryInitParams factoryInitParams;
factoryInitParams.fabricStorage = &mFabricStorage;
factoryInitParams.listenPort = static_cast<uint16_t>(mDefaultStorage.GetListenPort() + CurrentCommissionerIndex());
ReturnLogErrorOnFailure(DeviceControllerFactory::GetInstance().Init(factoryInitParams));

ReturnLogErrorOnFailure(InitializeCommissioner(CurrentCommissionerName(), CurrentCommissionerIndex()));

chip::DeviceLayer::PlatformMgr().ScheduleWork(RunQueuedCommand, reinterpret_cast<intptr_t>(this));
ReturnLogErrorOnFailure(StartWaiting(GetWaitDuration()));

Shutdown();

//
// We can call DeviceController::Shutdown() safely without grabbing the stack lock
// since the CHIP thread and event queue have been stopped, preventing any thread
// races.
//
ReturnLogErrorOnFailure(ShutdownCommissioner(CurrentCommissionerName()));

return CHIP_NO_ERROR;
}

std::string CHIPCommand::CurrentCommissionerName()
{
std::string name = mCommissionerName.HasValue() ? mCommissionerName.Value() : kCommissionerAlpha;
if (name.compare(kCommissionerAlpha) != 0 && name.compare(kCommissionerBeta) != 0 && name.compare(kCommissionerGamma) != 0)
{
ChipLogError(chipTool, "Unknown commissioner name: %s. Supported names are [%s, %s, %s]", name.c_str(), kCommissionerAlpha,
kCommissionerBeta, kCommissionerGamma);
chipDie();
}

return name;
}

uint16_t CHIPCommand::CurrentCommissionerIndex()
{
uint16_t index = 0;

std::string name = CurrentCommissionerName();
if (name.compare(kCommissionerAlpha) == 0)
{
index = kCommissionerAlphaFabricId;
}
else if (name.compare(kCommissionerBeta) == 0)
{
index = kCommissionerBetaFabricId;
}
else if (name.compare(kCommissionerGamma) == 0)
{
index = kCommissionerGammaFabricId;
}

VerifyOrDieWithMsg(index != 0, chipTool, "Unknown commissioner name: %s. Supported names are [%s, %s, %s]", name.c_str(),
kCommissionerAlpha, kCommissionerBeta, kCommissionerGamma);
return index;
}

chip::Controller::DeviceCommissioner & CHIPCommand::CurrentCommissioner()
{
auto item = mCommissioners.find(CurrentCommissionerName());
return *item->second.get();
}

CHIP_ERROR CHIPCommand::ShutdownCommissioner(std::string key)
{
return mCommissioners[key].get()->Shutdown();
}

CHIP_ERROR CHIPCommand::InitializeCommissioner(std::string key, chip::FabricId fabricId)
{
chip::Platform::ScopedMemoryBuffer<uint8_t> noc;
chip::Platform::ScopedMemoryBuffer<uint8_t> icac;
chip::Platform::ScopedMemoryBuffer<uint8_t> rcac;
Expand All @@ -64,36 +142,23 @@ CHIP_ERROR CHIPCommand::Run()
// TODO - OpCreds should only be generated for pairing command
// store the credentials in persistent storage, and
// generate when not available in the storage.
ReturnLogErrorOnFailure(mOpCredsIssuer.GenerateNOCChainAfterValidation(mStorage.GetLocalNodeId(), mStorage.GetFabricId(),
ReturnLogErrorOnFailure(mCommissionerStorage.Init(key.c_str()));
ReturnLogErrorOnFailure(mOpCredsIssuer.Initialize(mCommissionerStorage));
ReturnLogErrorOnFailure(mOpCredsIssuer.GenerateNOCChainAfterValidation(mCommissionerStorage.GetLocalNodeId(), fabricId,
ephemeralKey.Pubkey(), rcacSpan, icacSpan, nocSpan));

chip::Controller::FactoryInitParams factoryInitParams;
factoryInitParams.fabricStorage = &mFabricStorage;
factoryInitParams.listenPort = mStorage.GetListenPort();

std::unique_ptr<ChipDeviceCommissioner> commissioner = std::make_unique<ChipDeviceCommissioner>();
chip::Controller::SetupParams commissionerParams;
commissionerParams.storageDelegate = &mStorage;
commissionerParams.storageDelegate = &mCommissionerStorage;
commissionerParams.operationalCredentialsDelegate = &mOpCredsIssuer;
commissionerParams.ephemeralKeypair = &ephemeralKey;
commissionerParams.controllerRCAC = rcacSpan;
commissionerParams.controllerICAC = icacSpan;
commissionerParams.controllerNOC = nocSpan;
commissionerParams.controllerVendorId = chip::VendorId::TestVendor1;

ReturnLogErrorOnFailure(DeviceControllerFactory::GetInstance().Init(factoryInitParams));
ReturnLogErrorOnFailure(DeviceControllerFactory::GetInstance().SetupCommissioner(commissionerParams, mController));

chip::DeviceLayer::PlatformMgr().ScheduleWork(RunQueuedCommand, reinterpret_cast<intptr_t>(this));
ReturnLogErrorOnFailure(StartWaiting(GetWaitDuration()));

Shutdown();

//
// We can call DeviceController::Shutdown() safely without grabbing the stack lock
// since the CHIP thread and event queue have been stopped, preventing any thread
// races.
//
ReturnLogErrorOnFailure(mController.Shutdown());
ReturnLogErrorOnFailure(DeviceControllerFactory::GetInstance().SetupCommissioner(commissionerParams, *(commissioner.get())));
mCommissioners[key] = std::move(commissioner);

return CHIP_NO_ERROR;
}
Expand Down
18 changes: 15 additions & 3 deletions examples/chip-tool/commands/common/CHIPCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CHIPCommand : public Command
using NodeId = ::chip::NodeId;
using PeerAddress = ::chip::Transport::PeerAddress;

CHIPCommand(const char * commandName) : Command(commandName) {}
CHIPCommand(const char * commandName) : Command(commandName) { AddArgument("commissioner-name", &mCommissionerName); }

/////////// Command Interface /////////
CHIP_ERROR Run() override;
Expand All @@ -62,11 +62,23 @@ class CHIPCommand : public Command
// loop has been stopped.
virtual void Shutdown() {}

ChipDeviceCommissioner mController;
PersistentStorage mStorage;
PersistentStorage mDefaultStorage;
PersistentStorage mCommissionerStorage;
chip::SimpleFabricStorage mFabricStorage;

// This method returns the commissioner instance to be used for running the command.
// The default commissioner instance name is "alpha", but it can be overriden by passing
// --identity "instance name" when running a command.
ChipDeviceCommissioner & CurrentCommissioner();

private:
CHIP_ERROR InitializeCommissioner(std::string key, chip::FabricId fabricId);
CHIP_ERROR ShutdownCommissioner(std::string key);
std::string CurrentCommissionerName();
uint16_t CurrentCommissionerIndex();
std::map<std::string, std::unique_ptr<ChipDeviceCommissioner>> mCommissioners;
chip::Optional<char *> mCommissionerName;

static void RunQueuedCommand(intptr_t commandArg);

CHIP_ERROR mCommandExitStatus = CHIP_ERROR_INTERNAL;
Expand Down
4 changes: 2 additions & 2 deletions examples/chip-tool/commands/discover/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ class Update : public DiscoverCommand
CHIP_ERROR RunCommand(NodeId remoteId, uint64_t fabricId) override
{
ChipLogProgress(chipTool, "Mdns: Updating NodeId: %" PRIx64 " Compressed FabricId: %" PRIx64 " ...", remoteId,
mController.GetCompressedFabricId());
return mController.UpdateDevice(remoteId);
CurrentCommissioner().GetCompressedFabricId());
return CurrentCommissioner().UpdateDevice(remoteId);
}

/////////// DeviceAddressUpdateDelegate Interface /////////
Expand Down
2 changes: 1 addition & 1 deletion examples/chip-tool/commands/discover/DiscoverCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@

CHIP_ERROR DiscoverCommand::RunCommand()
{
mController.RegisterDeviceAddressUpdateDelegate(this);
CurrentCommissioner().RegisterDeviceAddressUpdateDelegate(this);
return RunCommand(mNodeId, mFabricId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ using namespace ::chip;

CHIP_ERROR DiscoverCommissionablesCommand::RunCommand()
{
mController.RegisterDeviceDiscoveryDelegate(this);
CurrentCommissioner().RegisterDeviceDiscoveryDelegate(this);
Dnssd::DiscoveryFilter filter(Dnssd::DiscoveryFilterType::kNone, (uint64_t) 0);
return mController.DiscoverCommissionableNodes(filter);
return CurrentCommissioner().DiscoverCommissionableNodes(filter);
}

void DiscoverCommissionablesCommand::OnDiscoveredDevice(const chip::Dnssd::DiscoveredNodeData & nodeData)
Expand Down
2 changes: 0 additions & 2 deletions examples/chip-tool/commands/pairing/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#pragma once

#include "CommissionedListCommand.h"
#include "ConfigureFabricCommand.h"
#include "PairingCommand.h"

#include <app/server/Dnssd.h>
Expand Down Expand Up @@ -189,7 +188,6 @@ void registerCommandsPairing(Commands & commands)
make_unique<OpenCommissioningWindow>(),
// TODO - enable CommissionedListCommand once DNS Cache is implemented
// make_unique<CommissionedListCommand>(),
make_unique<ConfigureFabricCommand>(),
make_unique<StartUdcServerCommand>(),
};

Expand Down
25 changes: 0 additions & 25 deletions examples/chip-tool/commands/pairing/ConfigureFabricCommand.cpp

This file was deleted.

33 changes: 0 additions & 33 deletions examples/chip-tool/commands/pairing/ConfigureFabricCommand.h

This file was deleted.

Loading