From 80ef0280c828e757aa7ec894a5421a635d3c686d Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Sat, 30 Apr 2022 13:24:41 +0200 Subject: [PATCH] [Testing] Add a command line argument to the all-clusters-app to Configure the minimal commissioning timeout (#17911) --- .../esp32/main/DeviceWithDisplay.cpp | 5 +-- .../all-clusters-app/linux/AppOptions.cpp | 14 +++++++- examples/all-clusters-app/linux/main.cpp | 1 + .../administrator-commissioning-server.cpp | 33 +++++++++---------- src/app/server/CommissioningWindowManager.cpp | 3 +- src/app/server/CommissioningWindowManager.h | 4 +-- src/app/tests/TestCommissionManager.cpp | 6 ++-- 7 files changed, 39 insertions(+), 27 deletions(-) diff --git a/examples/all-clusters-app/esp32/main/DeviceWithDisplay.cpp b/examples/all-clusters-app/esp32/main/DeviceWithDisplay.cpp index 939046533ff7a7..55a77f4d3aaef4 100644 --- a/examples/all-clusters-app/esp32/main/DeviceWithDisplay.cpp +++ b/examples/all-clusters-app/esp32/main/DeviceWithDisplay.cpp @@ -444,8 +444,9 @@ class SetupListModel : public TouchesMatterStackModel else if (i == 2) { chip::Server::GetInstance().GetFabricTable().DeleteAllFabrics(); - chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow( - CommissioningWindowManager::MaxCommissioningTimeout(), CommissioningWindowAdvertisement::kDnssdOnly); + auto & commissionMgr = chip::Server::GetInstance().GetCommissioningWindowManager(); + commissionMgr.OpenBasicCommissioningWindow(commissionMgr.MaxCommissioningTimeout(), + CommissioningWindowAdvertisement::kDnssdOnly); } } diff --git a/examples/all-clusters-app/linux/AppOptions.cpp b/examples/all-clusters-app/linux/AppOptions.cpp index c780c314194ef9..c3f80a2302a484 100644 --- a/examples/all-clusters-app/linux/AppOptions.cpp +++ b/examples/all-clusters-app/linux/AppOptions.cpp @@ -18,11 +18,15 @@ #include "AppOptions.h" +#include +#include + using chip::ArgParser::OptionDef; using chip::ArgParser::OptionSet; using chip::ArgParser::PrintArgError; -constexpr uint16_t kOptionDacProviderFilePath = 0xFF01; +constexpr uint16_t kOptionDacProviderFilePath = 0xFF01; +constexpr uint16_t kOptionMinCommissioningTimeout = 0xFF02; static chip::Credentials::Examples::TestHarnessDACProvider mDacProvider; @@ -34,6 +38,11 @@ bool AppOptions::HandleOptions(const char * program, OptionSet * options, int id case kOptionDacProviderFilePath: mDacProvider.Init(value); break; + case kOptionMinCommissioningTimeout: { + auto & commissionMgr = chip::Server::GetInstance().GetCommissioningWindowManager(); + commissionMgr.OverrideMinCommissioningTimeout(chip::System::Clock::Seconds16(static_cast(atoi(value)))); + break; + } default: PrintArgError("%s: INTERNAL ERROR: Unhandled option: %s\n", program, name); retval = false; @@ -47,6 +56,7 @@ OptionSet * AppOptions::GetOptions() { static OptionDef optionsDef[] = { { "dac_provider", chip::ArgParser::kArgumentRequired, kOptionDacProviderFilePath }, + { "min_commissioning_timeout", chip::ArgParser::kArgumentRequired, kOptionMinCommissioningTimeout }, {}, }; @@ -54,6 +64,8 @@ OptionSet * AppOptions::GetOptions() AppOptions::HandleOptions, optionsDef, "PROGRAM OPTIONS", " --dac_provider \n" " A json file with data used by the example dac provider to validate device attestation procedure.\n" + " --min_commissioning_timeout \n" + " The minimum time in seconds during which commissioning session establishment is allowed by the Node.\n" }; return &options; diff --git a/examples/all-clusters-app/linux/main.cpp b/examples/all-clusters-app/linux/main.cpp index c44c7061d7685b..ec71f22c42cc0c 100644 --- a/examples/all-clusters-app/linux/main.cpp +++ b/examples/all-clusters-app/linux/main.cpp @@ -26,6 +26,7 @@ int main(int argc, char * argv[]) VerifyOrDie(InitBindingHandlers() == CHIP_NO_ERROR); LinuxDeviceOptions::GetInstance().dacProvider = AppOptions::GetDACProvider(); + ChipLinuxAppMainLoop(); return 0; } diff --git a/src/app/clusters/administrator-commissioning-server/administrator-commissioning-server.cpp b/src/app/clusters/administrator-commissioning-server/administrator-commissioning-server.cpp index a022e26560b6ff..0d0da53e155a06 100644 --- a/src/app/clusters/administrator-commissioning-server/administrator-commissioning-server.cpp +++ b/src/app/clusters/administrator-commissioning-server/administrator-commissioning-server.cpp @@ -105,15 +105,15 @@ bool emberAfAdministratorCommissioningClusterOpenCommissioningWindowCallback( ChipLogProgress(Zcl, "Received command to open commissioning window"); - FabricIndex fabricIndex = commandObj->GetAccessingFabricIndex(); - FabricInfo * fabricInfo = Server::GetInstance().GetFabricTable().FindFabricWithIndex(fabricIndex); - DeviceLayer::FailSafeContext & failSafeContext = DeviceLayer::DeviceControlServer::DeviceControlSvr().GetFailSafeContext(); + FabricIndex fabricIndex = commandObj->GetAccessingFabricIndex(); + FabricInfo * fabricInfo = Server::GetInstance().GetFabricTable().FindFabricWithIndex(fabricIndex); + auto & failSafeContext = DeviceLayer::DeviceControlServer::DeviceControlSvr().GetFailSafeContext(); + auto & commissionMgr = Server::GetInstance().GetCommissioningWindowManager(); VerifyOrExit(fabricInfo != nullptr, status.Emplace(StatusCode::EMBER_ZCL_STATUS_CODE_PAKE_PARAMETER_ERROR)); VerifyOrExit(!failSafeContext.IsFailSafeArmed(), status.Emplace(StatusCode::EMBER_ZCL_STATUS_CODE_BUSY)); - VerifyOrExit(Server::GetInstance().GetCommissioningWindowManager().CommissioningWindowStatus() == - CommissioningWindowStatus::kWindowNotOpen, + VerifyOrExit(commissionMgr.CommissioningWindowStatus() == CommissioningWindowStatus::kWindowNotOpen, status.Emplace(StatusCode::EMBER_ZCL_STATUS_CODE_BUSY)); VerifyOrExit(iterations >= kSpake2p_Min_PBKDF_Iterations, status.Emplace(StatusCode::EMBER_ZCL_STATUS_CODE_PAKE_PARAMETER_ERROR)); @@ -123,16 +123,16 @@ bool emberAfAdministratorCommissioningClusterOpenCommissioningWindowCallback( status.Emplace(StatusCode::EMBER_ZCL_STATUS_CODE_PAKE_PARAMETER_ERROR)); VerifyOrExit(salt.size() <= kSpake2p_Max_PBKDF_Salt_Length, status.Emplace(StatusCode::EMBER_ZCL_STATUS_CODE_PAKE_PARAMETER_ERROR)); - VerifyOrExit(commissioningTimeout <= CommissioningWindowManager::MaxCommissioningTimeout(), + VerifyOrExit(commissioningTimeout <= commissionMgr.MaxCommissioningTimeout(), globalStatus = InteractionModel::Status::InvalidCommand); - VerifyOrExit(commissioningTimeout >= CommissioningWindowManager::MinCommissioningTimeout(), + VerifyOrExit(commissioningTimeout >= commissionMgr.MinCommissioningTimeout(), globalStatus = InteractionModel::Status::InvalidCommand); VerifyOrExit(discriminator <= kMaxDiscriminatorValue, globalStatus = InteractionModel::Status::InvalidCommand); VerifyOrExit(verifier.Deserialize(pakeVerifier) == CHIP_NO_ERROR, status.Emplace(StatusCode::EMBER_ZCL_STATUS_CODE_PAKE_PARAMETER_ERROR)); - VerifyOrExit(Server::GetInstance().GetCommissioningWindowManager().OpenEnhancedCommissioningWindow( - commissioningTimeout, discriminator, verifier, iterations, salt) == CHIP_NO_ERROR, + VerifyOrExit(commissionMgr.OpenEnhancedCommissioningWindow(commissioningTimeout, discriminator, verifier, iterations, salt) == + CHIP_NO_ERROR, status.Emplace(StatusCode::EMBER_ZCL_STATUS_CODE_PAKE_PARAMETER_ERROR)); ChipLogProgress(Zcl, "Commissioning window is now open"); @@ -169,21 +169,20 @@ bool emberAfAdministratorCommissioningClusterOpenBasicCommissioningWindowCallbac FabricIndex fabricIndex = commandObj->GetAccessingFabricIndex(); FabricInfo * fabricInfo = Server::GetInstance().GetFabricTable().FindFabricWithIndex(fabricIndex); - chip::DeviceLayer::FailSafeContext & failSafeContext = - DeviceLayer::DeviceControlServer::DeviceControlSvr().GetFailSafeContext(); + auto & failSafeContext = DeviceLayer::DeviceControlServer::DeviceControlSvr().GetFailSafeContext(); + auto & commissionMgr = Server::GetInstance().GetCommissioningWindowManager(); VerifyOrExit(fabricInfo != nullptr, status.Emplace(StatusCode::EMBER_ZCL_STATUS_CODE_PAKE_PARAMETER_ERROR)); - VerifyOrExit(Server::GetInstance().GetCommissioningWindowManager().CommissioningWindowStatus() == - CommissioningWindowStatus::kWindowNotOpen, + VerifyOrExit(commissionMgr.CommissioningWindowStatus() == CommissioningWindowStatus::kWindowNotOpen, status.Emplace(StatusCode::EMBER_ZCL_STATUS_CODE_BUSY)); VerifyOrExit(!failSafeContext.IsFailSafeArmed(), status.Emplace(StatusCode::EMBER_ZCL_STATUS_CODE_BUSY)); - VerifyOrExit(commissioningTimeout <= CommissioningWindowManager::MaxCommissioningTimeout(), + VerifyOrExit(commissioningTimeout <= commissionMgr.MaxCommissioningTimeout(), globalStatus = InteractionModel::Status::InvalidCommand); - VerifyOrExit(commissioningTimeout >= CommissioningWindowManager::MinCommissioningTimeout(), + VerifyOrExit(commissioningTimeout >= commissionMgr.MinCommissioningTimeout(), globalStatus = InteractionModel::Status::InvalidCommand); - VerifyOrExit(Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow( - commissioningTimeout, CommissioningWindowAdvertisement::kDnssdOnly) == CHIP_NO_ERROR, + VerifyOrExit(commissionMgr.OpenBasicCommissioningWindow(commissioningTimeout, CommissioningWindowAdvertisement::kDnssdOnly) == + CHIP_NO_ERROR, status.Emplace(StatusCode::EMBER_ZCL_STATUS_CODE_PAKE_PARAMETER_ERROR)); ChipLogProgress(Zcl, "Commissioning window is now open"); diff --git a/src/app/server/CommissioningWindowManager.cpp b/src/app/server/CommissioningWindowManager.cpp index 4fa210d1737515..eeb20e7e96b5da 100644 --- a/src/app/server/CommissioningWindowManager.cpp +++ b/src/app/server/CommissioningWindowManager.cpp @@ -182,8 +182,7 @@ void CommissioningWindowManager::OnSessionEstablished(const SessionHandle & sess CHIP_ERROR CommissioningWindowManager::OpenCommissioningWindow(Seconds16 commissioningTimeout) { - VerifyOrReturnError(commissioningTimeout <= MaxCommissioningTimeout() && - commissioningTimeout >= mMinCommissioningTimeoutOverride.ValueOr(MinCommissioningTimeout()), + VerifyOrReturnError(commissioningTimeout <= MaxCommissioningTimeout() && commissioningTimeout >= MinCommissioningTimeout(), CHIP_ERROR_INVALID_ARGUMENT); DeviceLayer::FailSafeContext & failSafeContext = DeviceLayer::DeviceControlServer::DeviceControlSvr().GetFailSafeContext(); VerifyOrReturnError(!failSafeContext.IsFailSafeArmed(), CHIP_ERROR_INCORRECT_STATE); diff --git a/src/app/server/CommissioningWindowManager.h b/src/app/server/CommissioningWindowManager.h index 21f41ce2d9435e..744ebbd8e63328 100644 --- a/src/app/server/CommissioningWindowManager.h +++ b/src/app/server/CommissioningWindowManager.h @@ -56,10 +56,10 @@ class CommissioningWindowManager : public SessionEstablishmentDelegate, public a return System::Clock::Seconds16(15 * 60); } - static constexpr System::Clock::Seconds16 MinCommissioningTimeout() + System::Clock::Seconds16 MinCommissioningTimeout() const { // Specification section 5.4.2.3. Announcement Duration says 3 minutes. - return System::Clock::Seconds16(3 * 60); + return mMinCommissioningTimeoutOverride.ValueOr(System::Clock::Seconds16(3 * 60)); } void SetAppDelegate(AppDelegate * delegate) { mAppDelegate = delegate; } diff --git a/src/app/tests/TestCommissionManager.cpp b/src/app/tests/TestCommissionManager.cpp index cda96445bc9516..159b50b6ba3d49 100644 --- a/src/app/tests/TestCommissionManager.cpp +++ b/src/app/tests/TestCommissionManager.cpp @@ -87,7 +87,7 @@ void CheckCommissioningWindowManagerBasicWindowOpenCloseTask(intptr_t context) { nlTestSuite * suite = reinterpret_cast(context); CommissioningWindowManager & commissionMgr = Server::GetInstance().GetCommissioningWindowManager(); - CHIP_ERROR err = commissionMgr.OpenBasicCommissioningWindow(CommissioningWindowManager::MaxCommissioningTimeout(), + CHIP_ERROR err = commissionMgr.OpenBasicCommissioningWindow(commissionMgr.MaxCommissioningTimeout(), CommissioningWindowAdvertisement::kDnssdOnly); NL_TEST_ASSERT(suite, err == CHIP_NO_ERROR); NL_TEST_ASSERT(suite, @@ -196,8 +196,8 @@ void CheckCommissioningWindowManagerEnhancedWindowTask(intptr_t context) uint8_t salt[chip::kSpake2p_Min_PBKDF_Salt_Length]; chip::ByteSpan saltData(salt); - err = commissionMgr.OpenEnhancedCommissioningWindow(CommissioningWindowManager::MaxCommissioningTimeout(), newDiscriminator, - verifier, kIterations, saltData); + err = commissionMgr.OpenEnhancedCommissioningWindow(commissionMgr.MaxCommissioningTimeout(), newDiscriminator, verifier, + kIterations, saltData); NL_TEST_ASSERT(suite, err == CHIP_NO_ERROR); NL_TEST_ASSERT(suite, commissionMgr.CommissioningWindowStatus() ==