From c22c7dfdff707fc81df2f001236fd5043297e309 Mon Sep 17 00:00:00 2001 From: James Swan <122404367+swan-amazon@users.noreply.github.com> Date: Tue, 21 May 2024 00:47:25 +0000 Subject: [PATCH] fixup! Implemented Enhanced setup flow provider --- .../commands/pairing/PairingCommand.cpp | 6 +- .../general-commissioning-server.cpp | 51 +++++----- src/app/server/BUILD.gn | 1 + src/app/server/EnhancedSetupFlowProvider.cpp | 96 ++++++++++--------- src/app/server/EnhancedSetupFlowProvider.h | 31 +++--- src/app/server/TermsAndConditionsProvider.cpp | 79 +++++++++++++++ src/app/server/TermsAndConditionsProvider.h | 14 ++- 7 files changed, 193 insertions(+), 85 deletions(-) create mode 100644 src/app/server/TermsAndConditionsProvider.cpp diff --git a/examples/chip-tool/commands/pairing/PairingCommand.cpp b/examples/chip-tool/commands/pairing/PairingCommand.cpp index 0f30a12c8dc2c5..8bcd8b6ee4c5eb 100644 --- a/examples/chip-tool/commands/pairing/PairingCommand.cpp +++ b/examples/chip-tool/commands/pairing/PairingCommand.cpp @@ -118,9 +118,13 @@ CommissioningParameters PairingCommand::GetCommissioningParameters() params.SetCountryCode(CharSpan::fromCharString(mCountryCode.Value())); } - if (mTCAcknowledgements.HasValue() && mTCAcknowledgementVersion.HasValue()) + if (mTCAcknowledgements.HasValue()) { params.SetTCAcknowledgements(mTCAcknowledgements.Value()); + } + + if (mTCAcknowledgementVersion.HasValue()) + { params.SetTCAcknowledgementVersion(mTCAcknowledgementVersion.Value()); } diff --git a/src/app/clusters/general-commissioning-server/general-commissioning-server.cpp b/src/app/clusters/general-commissioning-server/general-commissioning-server.cpp index 9dec1a5da1bb96..0a2d9cfcbd25c0 100644 --- a/src/app/clusters/general-commissioning-server/general-commissioning-server.cpp +++ b/src/app/clusters/general-commissioning-server/general-commissioning-server.cpp @@ -98,22 +98,25 @@ CHIP_ERROR GeneralCommissioningAttrAccess::Read(const ConcreteReadAttributePath } case TCAcceptedVersion::Id: { uint16_t tcAcceptedVersion; - CHIP_ERROR err = EnhancedSetupFlowProvider::GetInstance().GetTCAcceptedVersion(tcAcceptedVersion); + CHIP_ERROR err = + EnhancedSetupFlowProvider::GetInstance().GetTermsAndConditionsAcceptedAcknowledgementsVersion(tcAcceptedVersion); return (CHIP_NO_ERROR != err) ? err : aEncoder.Encode(tcAcceptedVersion); } case TCMinRequiredVersion::Id: { - uint16_t tcMinRequiredVersion; - CHIP_ERROR err = EnhancedSetupFlowProvider::GetInstance().GetTCMinRequiredVersion(tcMinRequiredVersion); - return (CHIP_NO_ERROR != err) ? err : aEncoder.Encode(tcMinRequiredVersion); + uint16_t tcRequiredVersion; + CHIP_ERROR err = + EnhancedSetupFlowProvider::GetInstance().GetTermsAndConditionsRequiredAcknowledgementsVersion(tcRequiredVersion); + return (CHIP_NO_ERROR != err) ? err : aEncoder.Encode(tcRequiredVersion); } case TCAcknowledgements::Id: { uint16_t tcAcknowledgements; - CHIP_ERROR err = EnhancedSetupFlowProvider::GetInstance().GetTCAcknowledgements(tcAcknowledgements); + CHIP_ERROR err = EnhancedSetupFlowProvider::GetInstance().GetTermsAndConditionsAcceptedAcknowledgements(tcAcknowledgements); return (CHIP_NO_ERROR != err) ? err : aEncoder.Encode(tcAcknowledgements); } case TCAcknowledgementsRequired::Id: { uint16_t tcAcknowledgementsRequired; - CHIP_ERROR err = EnhancedSetupFlowProvider::GetInstance().GetTCAcknowledgementsRequired(tcAcknowledgementsRequired); + CHIP_ERROR err = + EnhancedSetupFlowProvider::GetInstance().GetTermsAndConditionsRequiredAcknowledgements(tcAcknowledgementsRequired); return (CHIP_NO_ERROR != err) ? err : aEncoder.Encode(tcAcknowledgementsRequired); } default: @@ -261,14 +264,14 @@ bool emberAfGeneralCommissioningClusterCommissioningCompleteCallback( { CHIP_ERROR err; - if (!EnhancedSetupFlowProvider::GetInstance().HasRequiredTermsAndConditionsBeenAcknowledged()) + if (!EnhancedSetupFlowProvider::GetInstance().HasTermsAndConditionsRequiredAcknowledgementsBeenAccepted()) { ChipLogError(AppServer, "Required terms and conditions have not been accepted"); Breadcrumb::Set(commandPath.mEndpointId, 0); response.errorCode = CommissioningErrorEnum::kRequiredTCNotAccepted; } - else if (!EnhancedSetupFlowProvider::GetInstance().HasRequiredTermsAndConditionsMinVersionBeenAcknowledged()) + else if (!EnhancedSetupFlowProvider::GetInstance().HasTermsAndConditionsRequiredAcknowledgementsVersionBeenAccepted()) { ChipLogError(AppServer, "Minimium terms and conditions version has not been accepted"); Breadcrumb::Set(commandPath.mEndpointId, 0); @@ -365,8 +368,9 @@ bool emberAfGeneralCommissioningClusterSetTCAcknowledgementsCallback( Commands::SetTCAcknowledgementsResponse::Type response; - CheckSuccess(EnhancedSetupFlowProvider::GetInstance().SetTCAcknowledgements(commandData.TCVersion, commandData.TCUserResponse), - Failure); + CheckSuccess( + EnhancedSetupFlowProvider::GetInstance().SetTermsAndConditionsAcceptance(commandData.TCUserResponse, commandData.TCVersion), + Failure); response.errorCode = CommissioningErrorEnum::kOk; commandObj->AddResponse(commandPath, response); @@ -376,19 +380,20 @@ bool emberAfGeneralCommissioningClusterSetTCAcknowledgementsCallback( namespace { void OnPlatformEventHandler(const DeviceLayer::ChipDeviceEvent * event, intptr_t arg) { - switch (event->Type) { - case DeviceLayer::DeviceEventType::kFailSafeTimerExpired: { - // Spec says to reset Breadcrumb attribute to 0. - Breadcrumb::Set(0, 0); - break; - } - case DeviceLayer::DeviceEventType::kServerReady: { - EnhancedSetupFlowProvider::GetInstance().Init(); - break; - } - default: { - break; - } + switch (event->Type) + { + case DeviceLayer::DeviceEventType::kFailSafeTimerExpired: { + // Spec says to reset Breadcrumb attribute to 0. + Breadcrumb::Set(0, 0); + break; + } + case DeviceLayer::DeviceEventType::kServerReady: { + EnhancedSetupFlowProvider::GetInstance().Init(); + break; + } + default: { + break; + } } } diff --git a/src/app/server/BUILD.gn b/src/app/server/BUILD.gn index a07c61b398f054..b22f4a5bd7c1a7 100644 --- a/src/app/server/BUILD.gn +++ b/src/app/server/BUILD.gn @@ -46,6 +46,7 @@ static_library("server") { "OnboardingCodesUtil.h", "Server.cpp", "Server.h", + "TermsAndConditionsProvider.cpp", "TermsAndConditionsProvider.h", ] diff --git a/src/app/server/EnhancedSetupFlowProvider.cpp b/src/app/server/EnhancedSetupFlowProvider.cpp index 9810629ec4a18f..e938872e633fcb 100644 --- a/src/app/server/EnhancedSetupFlowProvider.cpp +++ b/src/app/server/EnhancedSetupFlowProvider.cpp @@ -21,77 +21,85 @@ #include #include -chip::app::EnhancedSetupFlowProvider chip::app::EnhancedSetupFlowProvider::sEnhancedSetupFlowProviderInstance(kRootEndpointId); - -namespace { -constexpr char kTCUserAcceptedVersionKeyName[] = "tc-useracceptedversion"; -constexpr char kTCUserAcceptedAcknowledgementsKeyName[] = "tc-useracceptedacknowledgements"; -} // namespace +chip::app::EnhancedSetupFlowProvider chip::app::EnhancedSetupFlowProvider::sInstance(kRootEndpointId); CHIP_ERROR chip::app::EnhancedSetupFlowProvider::Init() { - uint16_t tcAcknowledgements = 0; - uint16_t tcAcceptedVersion = 0; - - chip::DeviceLayer::PersistedStorage::KeyValueStoreManager & kvsManager = - chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr(); - - kvsManager.Get(kTCUserAcceptedVersionKeyName, &tcAcceptedVersion, sizeof(tcAcceptedVersion)); - kvsManager.Get(kTCUserAcceptedAcknowledgementsKeyName, &tcAcknowledgements, sizeof(tcAcknowledgements)); - - mTCRequiredVersion = CHIP_CONFIG_TC_REQUIRED_VERSION; - mTCRequiredAcknowledgements = CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS; - mTCUserAcceptedVersion = tcAcceptedVersion; - mTCUserAcceptedAcknowledgements = tcAcknowledgements; - + mTermsAndConditionsProvider.Init(); return CHIP_NO_ERROR; } -bool chip::app::EnhancedSetupFlowProvider::HasRequiredTermsAndConditionsBeenAcknowledged() +bool chip::app::EnhancedSetupFlowProvider::HasTermsAndConditionsRequiredAcknowledgementsBeenAccepted() { - return (mTCUserAcceptedAcknowledgements & mTCRequiredAcknowledgements) == mTCRequiredAcknowledgements; -} + uint16_t requiredAcknowledgements; + uint16_t requiredAcknowledgementsVersion; + uint16_t acceptedAcknowledgements; + uint16_t acceptedAcknowledgementsVersion; -bool chip::app::EnhancedSetupFlowProvider::HasRequiredTermsAndConditionsMinVersionBeenAcknowledged() -{ - return (mTCUserAcceptedVersion >= mTCRequiredVersion); + mTermsAndConditionsProvider.GetRequirements(requiredAcknowledgements, requiredAcknowledgementsVersion); + mTermsAndConditionsProvider.GetAcceptance(acceptedAcknowledgements, acceptedAcknowledgementsVersion); + + return (requiredAcknowledgements & acceptedAcknowledgements) == requiredAcknowledgements; } -CHIP_ERROR chip::app::EnhancedSetupFlowProvider::GetTCAcceptedVersion(uint16_t & value) +bool chip::app::EnhancedSetupFlowProvider::HasTermsAndConditionsRequiredAcknowledgementsVersionBeenAccepted() { - value = mTCUserAcceptedVersion; - return CHIP_NO_ERROR; + uint16_t requiredAcknowledgements; + uint16_t requiredAcknowledgementsVersion; + uint16_t acceptedAcknowledgements; + uint16_t acceptedAcknowledgementsVersion; + + mTermsAndConditionsProvider.GetRequirements(requiredAcknowledgements, requiredAcknowledgementsVersion); + mTermsAndConditionsProvider.GetAcceptance(acceptedAcknowledgements, acceptedAcknowledgementsVersion); + + return acceptedAcknowledgementsVersion >= requiredAcknowledgementsVersion; } -CHIP_ERROR chip::app::EnhancedSetupFlowProvider::GetTCMinRequiredVersion(uint16_t & value) +CHIP_ERROR chip::app::EnhancedSetupFlowProvider::GetTermsAndConditionsRequiredAcknowledgements(uint16_t & value) { - value = mTCRequiredVersion; + uint16_t requiredAcknowledgements; + uint16_t requiredAcknowledgementsVersion; + + mTermsAndConditionsProvider.GetRequirements(requiredAcknowledgements, requiredAcknowledgementsVersion); + + value = requiredAcknowledgements; return CHIP_NO_ERROR; } -CHIP_ERROR chip::app::EnhancedSetupFlowProvider::GetTCAcknowledgements(uint16_t & value) +CHIP_ERROR chip::app::EnhancedSetupFlowProvider::GetTermsAndConditionsRequiredAcknowledgementsVersion(uint16_t & value) { - value = mTCUserAcceptedAcknowledgements; + uint16_t requiredAcknowledgements; + uint16_t requiredAcknowledgementsVersion; + + mTermsAndConditionsProvider.GetRequirements(requiredAcknowledgements, requiredAcknowledgementsVersion); + + value = requiredAcknowledgementsVersion; return CHIP_NO_ERROR; } -CHIP_ERROR chip::app::EnhancedSetupFlowProvider::GetTCAcknowledgementsRequired(uint16_t & value) +CHIP_ERROR chip::app::EnhancedSetupFlowProvider::GetTermsAndConditionsAcceptedAcknowledgements(uint16_t & value) { - value = mTCUserAcceptedAcknowledgements; + uint16_t acceptedAcknowledgements; + uint16_t acceptedAcknowledgementsVersion; + + mTermsAndConditionsProvider.GetAcceptance(acceptedAcknowledgements, acceptedAcknowledgementsVersion); + + value = acceptedAcknowledgements; return CHIP_NO_ERROR; } -CHIP_ERROR chip::app::EnhancedSetupFlowProvider::SetTCAcknowledgements(uint16_t tcVersion, uint16_t tcUserResponse) +CHIP_ERROR chip::app::EnhancedSetupFlowProvider::GetTermsAndConditionsAcceptedAcknowledgementsVersion(uint16_t & value) { - chip::DeviceLayer::PersistedStorage::KeyValueStoreManager & kvsManager = - chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr(); - - mTCUserAcceptedVersion = tcVersion; - mTCUserAcceptedAcknowledgements = tcUserResponse; + uint16_t acceptedAcknowledgements; + uint16_t acceptedAcknowledgementsVersion; - kvsManager.Put(kTCUserAcceptedVersionKeyName, &mTCUserAcceptedVersion, sizeof(mTCUserAcceptedVersion)); - kvsManager.Put(kTCUserAcceptedAcknowledgementsKeyName, &mTCUserAcceptedAcknowledgements, - sizeof(mTCUserAcceptedAcknowledgements)); + mTermsAndConditionsProvider.GetAcceptance(acceptedAcknowledgements, acceptedAcknowledgementsVersion); + value = acceptedAcknowledgementsVersion; return CHIP_NO_ERROR; } + +CHIP_ERROR chip::app::EnhancedSetupFlowProvider::SetTermsAndConditionsAcceptance(uint16_t aTCAcknowledgements, uint16_t aTCAcknowledgementsVersion) +{ + return mTermsAndConditionsProvider.SetAcceptance(aTCAcknowledgements, aTCAcknowledgementsVersion); +} diff --git a/src/app/server/EnhancedSetupFlowProvider.h b/src/app/server/EnhancedSetupFlowProvider.h index eb70ed1db502b1..4230a97bdd5bdd 100644 --- a/src/app/server/EnhancedSetupFlowProvider.h +++ b/src/app/server/EnhancedSetupFlowProvider.h @@ -18,6 +18,8 @@ #pragma once +#include "TermsAndConditionsProvider.h" + #include #include @@ -29,29 +31,28 @@ namespace app { class EnhancedSetupFlowProvider { public: - static EnhancedSetupFlowProvider & GetInstance() { return sEnhancedSetupFlowProviderInstance; } + static EnhancedSetupFlowProvider & GetInstance() { return sInstance; } EnhancedSetupFlowProvider(const chip::EndpointId & endpointId = kRootEndpointId) : mEndpointId(endpointId){}; - CHIP_ERROR Init(); - bool HasRequiredTermsAndConditionsBeenAcknowledged(); - bool HasRequiredTermsAndConditionsMinVersionBeenAcknowledged(); + virtual ~EnhancedSetupFlowProvider() = default; - CHIP_ERROR GetTCAcceptedVersion(uint16_t & value); - CHIP_ERROR GetTCMinRequiredVersion(uint16_t & value); - CHIP_ERROR GetTCAcknowledgements(uint16_t & value); - CHIP_ERROR GetTCAcknowledgementsRequired(uint16_t & value); + virtual CHIP_ERROR Init(); - CHIP_ERROR SetTCAcknowledgements(uint16_t tcVersion, uint16_t tcUserResponse); + virtual bool HasTermsAndConditionsRequiredAcknowledgementsBeenAccepted(); + virtual bool HasTermsAndConditionsRequiredAcknowledgementsVersionBeenAccepted(); -private: - static EnhancedSetupFlowProvider sEnhancedSetupFlowProviderInstance; + virtual CHIP_ERROR GetTermsAndConditionsRequiredAcknowledgements(uint16_t & value); + virtual CHIP_ERROR GetTermsAndConditionsRequiredAcknowledgementsVersion(uint16_t & value); + virtual CHIP_ERROR GetTermsAndConditionsAcceptedAcknowledgements(uint16_t & value); + virtual CHIP_ERROR GetTermsAndConditionsAcceptedAcknowledgementsVersion(uint16_t & value); + virtual CHIP_ERROR SetTermsAndConditionsAcceptance(uint16_t aTCAcknowledgements, uint16_t aTCAcknowledgementsVersion); +private: + static EnhancedSetupFlowProvider sInstance; const chip::EndpointId mEndpointId; - uint16_t mTCRequiredVersion; - uint16_t mTCRequiredAcknowledgements; - uint16_t mTCUserAcceptedVersion; - uint16_t mTCUserAcceptedAcknowledgements; + + TermsAndConditionsProvider mTermsAndConditionsProvider; }; }; // namespace app diff --git a/src/app/server/TermsAndConditionsProvider.cpp b/src/app/server/TermsAndConditionsProvider.cpp new file mode 100644 index 00000000000000..278b8ddaabc4d9 --- /dev/null +++ b/src/app/server/TermsAndConditionsProvider.cpp @@ -0,0 +1,79 @@ +/* + * + * Copyright (c) 2024 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 "TermsAndConditionsProvider.h" + +#include +#include + +namespace { +constexpr char kAcceptedAcknowledgementsKeyName[] = "tc-acceptedacknowledgements"; +constexpr char kAcceptedAcknowledgementsVersionKeyName[] = "tc-acceptedacknowledgementsversion"; +} // namespace + +CHIP_ERROR chip::app::TermsAndConditionsProvider::Init() +{ + uint16_t acceptedAcknowledgements = 0; + uint16_t acceptedAcknowledgementsVersion = 0; + + chip::DeviceLayer::PersistedStorage::KeyValueStoreManager & kvsManager = + chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr(); + + kvsManager.Get(kAcceptedAcknowledgementsKeyName, &acceptedAcknowledgements, sizeof(acceptedAcknowledgements)); + kvsManager.Get(kAcceptedAcknowledgementsVersionKeyName, &acceptedAcknowledgementsVersion, + sizeof(acceptedAcknowledgementsVersion)); + + mRequiredAcknowledgements = CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS; + mRequiredAcknowledgementsVersion = CHIP_CONFIG_TC_REQUIRED_VERSION; + mAcceptedAcknowledgements = acceptedAcknowledgements; + mAcceptedAcknowledgementsVersion = acceptedAcknowledgementsVersion; + + return CHIP_NO_ERROR; +} + +CHIP_ERROR chip::app::TermsAndConditionsProvider::GetAcceptance(uint16_t & aAcknowledgements, uint16_t & aAcknowledgementsVersion) +{ + aAcknowledgements = mAcceptedAcknowledgements; + aAcknowledgementsVersion = mAcceptedAcknowledgementsVersion; + + return CHIP_NO_ERROR; +} + +CHIP_ERROR chip::app::TermsAndConditionsProvider::GetRequirements(uint16_t & aAcknowledgements, uint16_t & aAcknowledgementsVersion) +{ + aAcknowledgements = mRequiredAcknowledgements; + aAcknowledgementsVersion = mRequiredAcknowledgementsVersion; + + return CHIP_NO_ERROR; +} + +CHIP_ERROR chip::app::TermsAndConditionsProvider::SetAcceptance(uint16_t aTCAcceptedAcknowledgements, + uint16_t aTCAcceptedAcknowledgementsVersion) +{ + chip::DeviceLayer::PersistedStorage::KeyValueStoreManager & kvsManager = + chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr(); + + mAcceptedAcknowledgements = aTCAcceptedAcknowledgements; + mAcceptedAcknowledgementsVersion = aTCAcceptedAcknowledgementsVersion; + + kvsManager.Put(kAcceptedAcknowledgementsKeyName, &mAcceptedAcknowledgements, sizeof(mAcceptedAcknowledgements)); + kvsManager.Put(kAcceptedAcknowledgementsVersionKeyName, &mAcceptedAcknowledgementsVersion, + sizeof(mAcceptedAcknowledgementsVersion)); + + return CHIP_NO_ERROR; +} diff --git a/src/app/server/TermsAndConditionsProvider.h b/src/app/server/TermsAndConditionsProvider.h index ca7cb4eefb13d5..dbfddc45e4bf11 100644 --- a/src/app/server/TermsAndConditionsProvider.h +++ b/src/app/server/TermsAndConditionsProvider.h @@ -29,8 +29,18 @@ class TermsAndConditionsProvider { public: virtual ~TermsAndConditionsProvider() = default; - virtual CHIP_ERROR GetTCMinRequiredVersion(uint16_t & value); - virtual CHIP_ERROR GetTCAcknowledgementsRequired(uint16_t & value); + + virtual CHIP_ERROR Init(); + + virtual CHIP_ERROR GetAcceptance(uint16_t & aAcknowledgements, uint16_t & aAcknowledgementsVersion); + virtual CHIP_ERROR GetRequirements(uint16_t & aAcknowledgements, uint16_t & aAcknowledgementsVersion); + virtual CHIP_ERROR SetAcceptance(uint16_t aAcknowledgements, uint16_t aAcknowledgementsVersion); + +private: + uint16_t mRequiredAcknowledgements; + uint16_t mRequiredAcknowledgementsVersion; + uint16_t mAcceptedAcknowledgements; + uint16_t mAcceptedAcknowledgementsVersion; }; }; // namespace app