From ec45f0d562475f5648b67308514ab7cb1c75feda Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Thu, 25 Nov 2021 10:29:35 +0100 Subject: [PATCH] [chip-tool] Get the commissioner to be an optional argument for chip related commands instead of a dedicated command that stores the current commissioner on disk --- examples/chip-tool/BUILD.gn | 2 - .../commands/clusters/ModelCommand.cpp | 3 +- .../chip-tool/commands/common/CHIPCommand.cpp | 111 ++++++++++++++---- .../chip-tool/commands/common/CHIPCommand.h | 18 ++- .../chip-tool/commands/discover/Commands.h | 4 +- .../commands/discover/DiscoverCommand.cpp | 2 +- .../DiscoverCommissionablesCommand.cpp | 4 +- .../chip-tool/commands/pairing/Commands.h | 2 - .../pairing/ConfigureFabricCommand.cpp | 25 ---- .../commands/pairing/ConfigureFabricCommand.h | 33 ------ .../commands/pairing/PairingCommand.cpp | 30 ++--- .../commands/reporting/ReportingCommand.cpp | 3 +- .../chip-tool/commands/tests/TestCommand.cpp | 2 +- .../chip-tool/config/PersistentStorage.cpp | 80 ++++--------- examples/chip-tool/config/PersistentStorage.h | 24 +--- 15 files changed, 152 insertions(+), 191 deletions(-) delete mode 100644 examples/chip-tool/commands/pairing/ConfigureFabricCommand.cpp delete mode 100644 examples/chip-tool/commands/pairing/ConfigureFabricCommand.h diff --git a/examples/chip-tool/BUILD.gn b/examples/chip-tool/BUILD.gn index cfbffd3fcceadd..88f4b2eb3bcb8c 100644 --- a/examples/chip-tool/BUILD.gn +++ b/examples/chip-tool/BUILD.gn @@ -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", diff --git a/examples/chip-tool/commands/clusters/ModelCommand.cpp b/examples/chip-tool/commands/clusters/ModelCommand.cpp index 30816eb6cceaf8..79a3089679fdf4 100644 --- a/examples/chip-tool/commands/clusters/ModelCommand.cpp +++ b/examples/chip-tool/commands/clusters/ModelCommand.cpp @@ -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())); diff --git a/examples/chip-tool/commands/common/CHIPCommand.cpp b/examples/chip-tool/commands/common/CHIPCommand.cpp index 7f39cab591b99b..f45a88bcaec6a8 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.cpp +++ b/examples/chip-tool/commands/common/CHIPCommand.cpp @@ -29,6 +29,14 @@ 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 @@ -36,10 +44,80 @@ CHIP_ERROR CHIPCommand::Run() 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(mDefaultStorage.GetListenPort() + CurrentCommissionerIndex()); + ReturnLogErrorOnFailure(DeviceControllerFactory::GetInstance().Init(factoryInitParams)); + + ReturnLogErrorOnFailure(InitializeCommissioner(CurrentCommissionerName(), CurrentCommissionerIndex())); + + chip::DeviceLayer::PlatformMgr().ScheduleWork(RunQueuedCommand, reinterpret_cast(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 noc; chip::Platform::ScopedMemoryBuffer icac; chip::Platform::ScopedMemoryBuffer rcac; @@ -64,15 +142,14 @@ 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 commissioner = std::make_unique(); chip::Controller::SetupParams commissionerParams; - commissionerParams.storageDelegate = &mStorage; + commissionerParams.storageDelegate = &mCommissionerStorage; commissionerParams.operationalCredentialsDelegate = &mOpCredsIssuer; commissionerParams.ephemeralKeypair = &ephemeralKey; commissionerParams.controllerRCAC = rcacSpan; @@ -80,20 +157,8 @@ CHIP_ERROR CHIPCommand::Run() 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(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; } diff --git a/examples/chip-tool/commands/common/CHIPCommand.h b/examples/chip-tool/commands/common/CHIPCommand.h index 51f3aab0994948..30bee6805141a9 100644 --- a/examples/chip-tool/commands/common/CHIPCommand.h +++ b/examples/chip-tool/commands/common/CHIPCommand.h @@ -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; @@ -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> mCommissioners; + chip::Optional mCommissionerName; + static void RunQueuedCommand(intptr_t commandArg); CHIP_ERROR mCommandExitStatus = CHIP_ERROR_INTERNAL; diff --git a/examples/chip-tool/commands/discover/Commands.h b/examples/chip-tool/commands/discover/Commands.h index 5f74b42ca82037..e4830435c6f3f3 100644 --- a/examples/chip-tool/commands/discover/Commands.h +++ b/examples/chip-tool/commands/discover/Commands.h @@ -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 ///////// diff --git a/examples/chip-tool/commands/discover/DiscoverCommand.cpp b/examples/chip-tool/commands/discover/DiscoverCommand.cpp index 0ac9dd6e557603..1cf6f87a4b8427 100644 --- a/examples/chip-tool/commands/discover/DiscoverCommand.cpp +++ b/examples/chip-tool/commands/discover/DiscoverCommand.cpp @@ -20,6 +20,6 @@ CHIP_ERROR DiscoverCommand::RunCommand() { - mController.RegisterDeviceAddressUpdateDelegate(this); + CurrentCommissioner().RegisterDeviceAddressUpdateDelegate(this); return RunCommand(mNodeId, mFabricId); } diff --git a/examples/chip-tool/commands/discover/DiscoverCommissionablesCommand.cpp b/examples/chip-tool/commands/discover/DiscoverCommissionablesCommand.cpp index 653bc23caef3e8..88f41cc2b8125f 100644 --- a/examples/chip-tool/commands/discover/DiscoverCommissionablesCommand.cpp +++ b/examples/chip-tool/commands/discover/DiscoverCommissionablesCommand.cpp @@ -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) diff --git a/examples/chip-tool/commands/pairing/Commands.h b/examples/chip-tool/commands/pairing/Commands.h index de72e392167703..b6040efc34ed52 100644 --- a/examples/chip-tool/commands/pairing/Commands.h +++ b/examples/chip-tool/commands/pairing/Commands.h @@ -19,7 +19,6 @@ #pragma once #include "CommissionedListCommand.h" -#include "ConfigureFabricCommand.h" #include "PairingCommand.h" #include @@ -189,7 +188,6 @@ void registerCommandsPairing(Commands & commands) make_unique(), // TODO - enable CommissionedListCommand once DNS Cache is implemented // make_unique(), - make_unique(), make_unique(), }; diff --git a/examples/chip-tool/commands/pairing/ConfigureFabricCommand.cpp b/examples/chip-tool/commands/pairing/ConfigureFabricCommand.cpp deleted file mode 100644 index cf486f802d41ca..00000000000000 --- a/examples/chip-tool/commands/pairing/ConfigureFabricCommand.cpp +++ /dev/null @@ -1,25 +0,0 @@ -/* - * 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 "ConfigureFabricCommand.h" - -CHIP_ERROR ConfigureFabricCommand::Run() -{ - ReturnLogErrorOnFailure(mStorage.Init()); - return mStorage.SetFabric(mFabricName); -} diff --git a/examples/chip-tool/commands/pairing/ConfigureFabricCommand.h b/examples/chip-tool/commands/pairing/ConfigureFabricCommand.h deleted file mode 100644 index 043b2bddcc08fa..00000000000000 --- a/examples/chip-tool/commands/pairing/ConfigureFabricCommand.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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 "../../config/PersistentStorage.h" -#include "../common/Command.h" - -class ConfigureFabricCommand : public Command -{ -public: - ConfigureFabricCommand() : Command("configure-fabric") { AddArgument("name", &mFabricName); } - CHIP_ERROR Run() override; - -private: - PersistentStorage mStorage; - char * mFabricName; -}; diff --git a/examples/chip-tool/commands/pairing/PairingCommand.cpp b/examples/chip-tool/commands/pairing/PairingCommand.cpp index 55ef8f23e6b8c9..0d9524f2c84bd1 100644 --- a/examples/chip-tool/commands/pairing/PairingCommand.cpp +++ b/examples/chip-tool/commands/pairing/PairingCommand.cpp @@ -42,8 +42,8 @@ CHIP_ERROR PairingCommand::RunCommand() // callbacks we don't expect and then weird things happen. if (mPairingMode != PairingMode::OpenCommissioningWindow) { - mController.RegisterDeviceAddressUpdateDelegate(this); - mController.RegisterPairingDelegate(this); + CurrentCommissioner().RegisterDeviceAddressUpdateDelegate(this); + CurrentCommissioner().RegisterPairingDelegate(this); } err = RunInternal(mNodeId); @@ -83,7 +83,7 @@ CHIP_ERROR PairingCommand::RunInternal(NodeId remoteId) err = Pair(remoteId, PeerAddress::UDP(mRemoteAddr.address, mRemotePort)); break; case PairingMode::OpenCommissioningWindow: - err = mController.GetConnectedDevice(remoteId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback); + 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, @@ -110,12 +110,12 @@ void PairingCommand::OnDeviceConnectionFailureFn(void * context, NodeId deviceId CHIP_ERROR PairingCommand::PairWithQRCode(NodeId remoteId) { - return mController.PairDevice(remoteId, mOnboardingPayload); + return CurrentCommissioner().PairDevice(remoteId, mOnboardingPayload); } CHIP_ERROR PairingCommand::PairWithManualCode(NodeId remoteId) { - return mController.PairDevice(remoteId, mOnboardingPayload); + return CurrentCommissioner().PairDevice(remoteId, mOnboardingPayload); } CHIP_ERROR PairingCommand::Pair(NodeId remoteId, PeerAddress address) @@ -123,7 +123,7 @@ CHIP_ERROR PairingCommand::Pair(NodeId remoteId, PeerAddress address) RendezvousParameters params = RendezvousParameters().SetSetupPINCode(mSetupPINCode).SetDiscriminator(mDiscriminator).SetPeerAddress(address); - return mController.PairDevice(remoteId, params); + return CurrentCommissioner().PairDevice(remoteId, params); } CHIP_ERROR PairingCommand::PairWithMdns(NodeId remoteId) @@ -151,13 +151,13 @@ CHIP_ERROR PairingCommand::PairWithMdns(NodeId remoteId) break; } - mController.RegisterDeviceDiscoveryDelegate(this); - return mController.DiscoverCommissionableNodes(filter); + CurrentCommissioner().RegisterDeviceDiscoveryDelegate(this); + return CurrentCommissioner().DiscoverCommissionableNodes(filter); } CHIP_ERROR PairingCommand::Unpair(NodeId remoteId) { - CHIP_ERROR err = mController.UnpairDevice(remoteId); + CHIP_ERROR err = CurrentCommissioner().UnpairDevice(remoteId); SetCommandExitStatus(err); return err; } @@ -176,8 +176,8 @@ void PairingCommand::OnOpenCommissioningWindowResponse(void * context, NodeId re CHIP_ERROR PairingCommand::OpenCommissioningWindow() { - return mController.OpenCommissioningWindowWithCallback(mNodeId, mTimeout, mIteration, mDiscriminator, - mCommissioningWindowOption, &mOnOpenCommissioningWindowCallback); + return CurrentCommissioner().OpenCommissioningWindowWithCallback( + mNodeId, mTimeout, mIteration, mDiscriminator, mCommissioningWindowOption, &mOnOpenCommissioningWindowCallback); } void PairingCommand::OnStatusUpdate(DevicePairingDelegate::Status status) @@ -252,7 +252,7 @@ CHIP_ERROR PairingCommand::SetupNetwork() break; case PairingNetworkType::WiFi: case PairingNetworkType::Thread: - err = mController.GetDeviceBeingCommissioned(mNodeId, &mDevice); + err = CurrentCommissioner().GetDeviceBeingCommissioned(mNodeId, &mDevice); VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(chipTool, "Setup failure! No pairing for device: %" PRIu64, mNodeId)); mCluster.Associate(mDevice, mEndpointId); @@ -418,8 +418,8 @@ void PairingCommand::OnEnableNetworkResponse(void * context, uint8_t errorCode, CHIP_ERROR PairingCommand::UpdateNetworkAddress() { ChipLogProgress(chipTool, "Mdns: Updating NodeId: %" PRIx64 " Compressed FabricId: %" PRIx64 " ...", mNodeId, - mController.GetCompressedFabricId()); - return mController.UpdateDevice(mNodeId); + CurrentCommissioner().GetCompressedFabricId()); + return CurrentCommissioner().UpdateDevice(mNodeId); } void PairingCommand::OnAddressUpdateComplete(NodeId nodeId, CHIP_ERROR err) @@ -443,7 +443,7 @@ void PairingCommand::OnDiscoveredDevice(const chip::Dnssd::DiscoveredNodeData & ChipLogProgress(chipTool, "Discovered Device: %s:%u", buf, port); // Stop Mdns discovery. Is it the right method ? - mController.RegisterDeviceDiscoveryDelegate(nullptr); + CurrentCommissioner().RegisterDeviceDiscoveryDelegate(nullptr); Inet::InterfaceId interfaceId = nodeData.ipAddress[0].IsIPv6LinkLocal() ? nodeData.interfaceId[0] : Inet::InterfaceId::Null(); PeerAddress peerAddress = PeerAddress::UDP(nodeData.ipAddress[0], port, interfaceId); diff --git a/examples/chip-tool/commands/reporting/ReportingCommand.cpp b/examples/chip-tool/commands/reporting/ReportingCommand.cpp index 8ff30256a42be8..7e627f4ba2a1b3 100644 --- a/examples/chip-tool/commands/reporting/ReportingCommand.cpp +++ b/examples/chip-tool/commands/reporting/ReportingCommand.cpp @@ -26,7 +26,8 @@ using namespace ::chip; CHIP_ERROR ReportingCommand::RunCommand() { - 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 %s", mNodeId, ErrorStr(err))); diff --git a/examples/chip-tool/commands/tests/TestCommand.cpp b/examples/chip-tool/commands/tests/TestCommand.cpp index 62a86e3e8eea03..be7dc5d6d312ad 100644 --- a/examples/chip-tool/commands/tests/TestCommand.cpp +++ b/examples/chip-tool/commands/tests/TestCommand.cpp @@ -20,7 +20,7 @@ CHIP_ERROR TestCommand::RunCommand() { - return mController.GetConnectedDevice(mNodeId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback); + return CurrentCommissioner().GetConnectedDevice(mNodeId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback); } void TestCommand::OnDeviceConnectedFn(void * context, chip::OperationalDeviceProxy * device) diff --git a/examples/chip-tool/config/PersistentStorage.cpp b/examples/chip-tool/config/PersistentStorage.cpp index 6fdde413fbab61..20444da6d59b02 100644 --- a/examples/chip-tool/config/PersistentStorage.cpp +++ b/examples/chip-tool/config/PersistentStorage.cpp @@ -32,17 +32,21 @@ using namespace ::chip; using namespace ::chip::Controller; using namespace ::chip::Logging; -constexpr const char kFilename[] = "/tmp/chip_tool_config.ini"; constexpr const char kDefaultSectionName[] = "Default"; constexpr const char kPortKey[] = "ListenPort"; constexpr const char kLoggingKey[] = "LoggingLevel"; constexpr const char kLocalNodeIdKey[] = "LocalNodeId"; -constexpr const char kFabricIdKey[] = "LocalFabricId"; -constexpr const FabricId kFabricAlpha = 1; -constexpr const FabricId kFabricBeta = 2; -constexpr const FabricId kFabricGamma = 3; constexpr LogCategory kDefaultLoggingLevel = kLogCategory_Detail; +std::string GetFilename(const char * name) +{ + if (name == nullptr) + { + return "/tmp/chip_tool_config.ini"; + } + return "/tmp/chip_tool_config." + std::string(name) + ".ini"; +} + namespace { std::string StringToBase64(const std::string & value) @@ -74,22 +78,22 @@ std::string Base64ToString(const std::string & b64Value) } // namespace -CHIP_ERROR PersistentStorage::Init() +CHIP_ERROR PersistentStorage::Init(const char * name) { CHIP_ERROR err = CHIP_NO_ERROR; std::ifstream ifs; - ifs.open(kFilename, std::ifstream::in); + ifs.open(GetFilename(name), std::ifstream::in); if (!ifs.good()) { - CommitConfig(); - ifs.open(kFilename, std::ifstream::in); + CommitConfig(name); + ifs.open(GetFilename(name), std::ifstream::in); } VerifyOrExit(ifs.is_open(), err = CHIP_ERROR_OPEN_FAILED); + mName = name; mConfig.parse(ifs); ifs.close(); - exit: return err; } @@ -125,7 +129,7 @@ CHIP_ERROR PersistentStorage::SyncSetKeyValue(const char * key, const void * val section[key] = StringToBase64(std::string(static_cast(value), size)); mConfig.sections[kDefaultSectionName] = section; - return CommitConfig(); + return CommitConfig(mName); } CHIP_ERROR PersistentStorage::SyncDeleteKeyValue(const char * key) @@ -134,16 +138,15 @@ CHIP_ERROR PersistentStorage::SyncDeleteKeyValue(const char * key) section.erase(key); mConfig.sections[kDefaultSectionName] = section; - return CommitConfig(); + return CommitConfig(mName); } -CHIP_ERROR PersistentStorage::CommitConfig() +CHIP_ERROR PersistentStorage::CommitConfig(const char * name) { CHIP_ERROR err = CHIP_NO_ERROR; std::ofstream ofs; - std::string tmpPath = kFilename; - tmpPath.append(".tmp"); + std::string tmpPath = GetFilename(name) + ".tmp"; ofs.open(tmpPath, std::ofstream::out | std::ofstream::trunc); VerifyOrExit(ofs.good(), err = CHIP_ERROR_WRITE_FAILED); @@ -151,7 +154,7 @@ CHIP_ERROR PersistentStorage::CommitConfig() ofs.close(); VerifyOrExit(ofs.good(), err = CHIP_ERROR_WRITE_FAILED); - VerifyOrExit(rename(tmpPath.c_str(), kFilename) == 0, err = CHIP_ERROR_WRITE_FAILED); + VerifyOrExit(rename(tmpPath.c_str(), GetFilename(name).c_str()) == 0, err = CHIP_ERROR_WRITE_FAILED); exit: return err; @@ -161,10 +164,9 @@ uint16_t PersistentStorage::GetListenPort() { CHIP_ERROR err = CHIP_NO_ERROR; - // By default chip-tool listens on CHIP_PORT + N. This is done in order to avoid + // By default chip-tool listens on CHIP_PORT + 1. This is done in order to avoid // having 2 servers listening on CHIP_PORT when one runs an accessory server locally. - FabricId N = GetFabricId(); - uint16_t chipListenPort = static_cast(CHIP_PORT + N); + uint16_t chipListenPort = static_cast(CHIP_PORT + 1); char value[6]; uint16_t size = static_cast(sizeof(value)); @@ -234,43 +236,3 @@ CHIP_ERROR PersistentStorage::SetLocalNodeId(NodeId value) uint64_t nodeId = Encoding::LittleEndian::HostSwap64(value); return SyncSetKeyValue(kLocalNodeIdKey, &nodeId, sizeof(nodeId)); } - -FabricId PersistentStorage::GetFabricId() -{ - CHIP_ERROR err = CHIP_NO_ERROR; - - uint64_t fabricId; - uint16_t size = static_cast(sizeof(fabricId)); - err = SyncGetKeyValue(kFabricIdKey, &fabricId, size); - if (err == CHIP_NO_ERROR) - { - return static_cast(Encoding::LittleEndian::HostSwap64(fabricId)); - } - - return kFabricAlpha; -} - -CHIP_ERROR PersistentStorage::SetFabric(const char * fabricName) -{ - uint64_t fabricId = kFabricAlpha; - - if (strcasecmp(fabricName, "alpha") == 0) - { - fabricId = kFabricAlpha; - } - else if (strcasecmp(fabricName, "beta") == 0) - { - fabricId = kFabricBeta; - } - else if (strcasecmp(fabricName, "gamma") == 0) - { - fabricId = kFabricGamma; - } - else - { - return CHIP_ERROR_INVALID_ARGUMENT; - } - - fabricId = Encoding::LittleEndian::HostSwap64(fabricId); - return SyncSetKeyValue(kFabricIdKey, &fabricId, sizeof(fabricId)); -} diff --git a/examples/chip-tool/config/PersistentStorage.h b/examples/chip-tool/config/PersistentStorage.h index 10f4bb1ad76d5d..fd06571c1b6b61 100644 --- a/examples/chip-tool/config/PersistentStorage.h +++ b/examples/chip-tool/config/PersistentStorage.h @@ -26,7 +26,7 @@ class PersistentStorage : public chip::PersistentStorageDelegate { public: - CHIP_ERROR Init(); + CHIP_ERROR Init(const char * name = nullptr); /////////// PersistentStorageDelegate Interface ///////// CHIP_ERROR SyncGetKeyValue(const char * key, void * buffer, uint16_t & size) override; @@ -42,26 +42,8 @@ class PersistentStorage : public chip::PersistentStorageDelegate // Store local node id. CHIP_ERROR SetLocalNodeId(chip::NodeId nodeId); - /** - * @brief - * Configure the fabric used for pairing and sending commands. - * - * @param[in] fabricName The name of the fabric. It must be one of the following strings: - * - alpha - * - beta - * - gamma - * - * @return CHIP_ERROR CHIP_NO_ERROR on success, or corresponding error code. - */ - CHIP_ERROR SetFabric(const char * fabricName); - - /** - * @brief - * Return the stored fabric id, or the one for the "alpha" fabric if nothing is stored. - */ - chip::FabricId GetFabricId(); - private: - CHIP_ERROR CommitConfig(); + CHIP_ERROR CommitConfig(const char * name); inipp::Ini mConfig; + const char * mName; };