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 6c1465873de433..a8d8b979643a9b 100644 --- a/src/app/clusters/general-commissioning-server/general-commissioning-server.cpp +++ b/src/app/clusters/general-commissioning-server/general-commissioning-server.cpp @@ -72,9 +72,6 @@ class GeneralCommissioningAttrAccess : public AttributeAccessInterface CHIP_ERROR ReadIfSupported(CHIP_ERROR (ConfigurationManager::*getter)(uint8_t &), AttributeValueEncoder & aEncoder); CHIP_ERROR ReadBasicCommissioningInfo(AttributeValueEncoder & aEncoder); CHIP_ERROR ReadSupportsConcurrentConnection(AttributeValueEncoder & aEncoder); - template - CHIP_ERROR ReadFromProvider(Provider * const aProvider, CHIP_ERROR (Provider::*const aConstGetter)(T &) const, - AttributeValueEncoder & aEncoder); }; GeneralCommissioningAttrAccess gAttrAccess; @@ -103,24 +100,24 @@ CHIP_ERROR GeneralCommissioningAttrAccess::Read(const ConcreteReadAttributePath } #if CHIP_CONFIG_TC_REQUIRED case TCAcceptedVersion::Id: { - auto provider = Server::GetInstance().GetEnhancedSetupFlowProvider(); - auto getter = &EnhancedSetupFlowProvider::GetTermsAndConditionsAcceptedAcknowledgementsVersion; - return ReadFromProvider(provider, getter, aEncoder); + Optional outTermsAndConditions; + Server::GetInstance().GetEnhancedSetupFlowProvider()->GetTermsAndConditionsAcknowledgements(outTermsAndConditions); + return !outTermsAndConditions.HasValue() ? aEncoder.Encode(0) : aEncoder.Encode(outTermsAndConditions.Value().version); } case TCMinRequiredVersion::Id: { - auto provider = Server::GetInstance().GetEnhancedSetupFlowProvider(); - auto getter = &EnhancedSetupFlowProvider::GetTermsAndConditionsRequiredAcknowledgementsVersion; - return ReadFromProvider(provider, getter, aEncoder); + Optional outTermsAndConditions; + Server::GetInstance().GetEnhancedSetupFlowProvider()->GetTermsAndConditionsRequirements(outTermsAndConditions); + return !outTermsAndConditions.HasValue() ? aEncoder.Encode(0) : aEncoder.Encode(outTermsAndConditions.Value().version); } case TCAcknowledgements::Id: { - auto provider = Server::GetInstance().GetEnhancedSetupFlowProvider(); - auto getter = &EnhancedSetupFlowProvider::GetTermsAndConditionsAcceptedAcknowledgements; - return ReadFromProvider(provider, getter, aEncoder); + Optional outTermsAndConditions; + Server::GetInstance().GetEnhancedSetupFlowProvider()->GetTermsAndConditionsAcknowledgements(outTermsAndConditions); + return !outTermsAndConditions.HasValue() ? aEncoder.Encode(0) : aEncoder.Encode(outTermsAndConditions.Value().value); } case TCAcknowledgementsRequired::Id: { - auto provider = Server::GetInstance().GetEnhancedSetupFlowProvider(); - auto getter = &EnhancedSetupFlowProvider::IsTermsAndConditionsAcceptanceRequired; - return ReadFromProvider(provider, getter, aEncoder); + Optional outTermsAndConditions; + Server::GetInstance().GetEnhancedSetupFlowProvider()->GetTermsAndConditionsRequirements(outTermsAndConditions); + return aEncoder.Encode(outTermsAndConditions.HasValue()); } #endif default: { @@ -172,91 +169,42 @@ CHIP_ERROR GeneralCommissioningAttrAccess::ReadSupportsConcurrentConnection(Attr return aEncoder.Encode(supportsConcurrentConnection); } -template -CHIP_ERROR GeneralCommissioningAttrAccess::ReadFromProvider(Provider * const aProvider, - CHIP_ERROR (Provider::*const aConstGetter)(T &) const, - AttributeValueEncoder & aEncoder) -{ - if (nullptr == aProvider) - { - return CHIP_ERROR_PERSISTED_STORAGE_FAILED; - } - - T value; - CHIP_ERROR err = (aProvider->*aConstGetter)(value); - if (err != CHIP_NO_ERROR) - { - return err; - } - - return aEncoder.Encode(value); -} - #if CHIP_CONFIG_TC_REQUIRED -CHIP_ERROR checkTermsAndConditionsAcknowledgementsState(CommissioningErrorEnum & errorCode) +CommissioningErrorEnum CheckTermsAndConditionsAcknowledgementsState(const Optional & requiredTermsAndConditions, + const Optional & acceptedTermsAndConditions) { - EnhancedSetupFlowProvider * enhancedSetupFlowProvider = Server::GetInstance().GetEnhancedSetupFlowProvider(); - - CHIP_ERROR err; - - uint16_t termsAndConditionsAcceptedAcknowledgements; - bool hasAnyAcknowledgements; - bool hasRequiredTermAccepted; - bool hasRequiredTermVersionAccepted; - - err = enhancedSetupFlowProvider->HasReceivedTermsAndConditionscknowledgements(hasAnyAcknowledgements); - if (!::chip::ChipError::IsSuccess(err)) - { - ChipLogError(AppServer, "Failed to HasReceivedTermsAndConditionscknowledgements"); - errorCode = CommissioningErrorEnum::kTCAcknowledgementsNotReceived; - return err; - } - - err = enhancedSetupFlowProvider->GetTermsAndConditionsAcceptedAcknowledgements(termsAndConditionsAcceptedAcknowledgements); - if (!::chip::ChipError::IsSuccess(err)) - { - ChipLogError(AppServer, "Failed to GetTermsAndConditionsAcceptedAcknowledgements"); - return err; - } - - err = enhancedSetupFlowProvider->HasTermsAndConditionsRequiredAcknowledgementsBeenAccepted(hasRequiredTermAccepted); - if (!::chip::ChipError::IsSuccess(err)) + // No validation checks required if no required terms and conditions + if (!requiredTermsAndConditions.HasValue()) { - ChipLogError(AppServer, "Failed to HasTermsAndConditionsRequiredAcknowledgementsBeenAccepted"); - return err; + return CommissioningErrorEnum::kOk; } - err = - enhancedSetupFlowProvider->HasTermsAndConditionsRequiredAcknowledgementsVersionBeenAccepted(hasRequiredTermVersionAccepted); - if (!::chip::ChipError::IsSuccess(err)) + // Validate if we have received any terms and conditions acceptance + if (!acceptedTermsAndConditions.HasValue()) { - ChipLogError(AppServer, "Failed to HasTermsAndConditionsRequiredAcknowledgementsVersionBeenAccepted"); - return err; + ChipLogError(AppServer, "Failed to HasReceivedTermsAndConditionsAcknowledgements"); + return CommissioningErrorEnum::kTCAcknowledgementsNotReceived; } - if (!hasRequiredTermVersionAccepted) + // Validate the accepted version first... + if (requiredTermsAndConditions.Value().version > acceptedTermsAndConditions.Value().version) { - uint16_t requiredAcknowledgementsVersion = 0; - (void) enhancedSetupFlowProvider->GetTermsAndConditionsRequiredAcknowledgementsVersion(requiredAcknowledgementsVersion); ChipLogProgress(AppServer, "Minimum terms and conditions version, 0x%04x, has not been accepted", - requiredAcknowledgementsVersion); - errorCode = CommissioningErrorEnum::kTCMinVersionNotMet; - return CHIP_NO_ERROR; + requiredTermsAndConditions.Value().version); + return CommissioningErrorEnum::kTCMinVersionNotMet; } - if (!hasRequiredTermAccepted) + // Validate the accepted bits second... + if (requiredTermsAndConditions.Value().value != + (requiredTermsAndConditions.Value().value & acceptedTermsAndConditions.Value().value)) { - uint16_t requiredAcknowledgements = 0; - (void) enhancedSetupFlowProvider->GetTermsAndConditionsRequiredAcknowledgements(requiredAcknowledgements); - - ChipLogProgress(AppServer, "Required terms and conditions, 0x%04x,have not been accepted", requiredAcknowledgements); - errorCode = (0 == termsAndConditionsAcceptedAcknowledgements) ? CommissioningErrorEnum::kTCAcknowledgementsNotReceived - : CommissioningErrorEnum::kRequiredTCNotAccepted; - return CHIP_NO_ERROR; + ChipLogProgress(AppServer, "Required terms and conditions, 0x%04x,have not been accepted", + requiredTermsAndConditions.Value().value); + return CommissioningErrorEnum::kRequiredTCNotAccepted; } - errorCode = CommissioningErrorEnum::kOk; - return CHIP_NO_ERROR; + // All validation check succeeded... + return CommissioningErrorEnum::kOk; } #endif @@ -356,7 +304,23 @@ bool emberAfGeneralCommissioningClusterCommissioningCompleteCallback( else { #if CHIP_CONFIG_TC_REQUIRED - CheckSuccess(checkTermsAndConditionsAcknowledgementsState(response.errorCode), Failure); + // CheckSuccess(CheckTermsAndConditionsAcknowledgementsStateOnCommissioningComplete(response.errorCode), Failure); + + EnhancedSetupFlowProvider * const enhancedSetupFlowProvider = Server::GetInstance().GetEnhancedSetupFlowProvider(); + + Optional acceptedTermsAndConditions; + Optional requiredTermsAndConditions; + + enhancedSetupFlowProvider->GetTermsAndConditionsAcknowledgements(acceptedTermsAndConditions); + enhancedSetupFlowProvider->GetTermsAndConditionsRequirements(requiredTermsAndConditions); + + response.errorCode = + CheckTermsAndConditionsAcknowledgementsState(requiredTermsAndConditions, acceptedTermsAndConditions); + if (CommissioningErrorEnum::kOk != response.errorCode) + { + commandObj->AddResponse(commandPath, response); + return true; + } #endif if (failSafe.NocCommandHasBeenInvoked()) @@ -375,6 +339,14 @@ bool emberAfGeneralCommissioningClusterCommissioningCompleteCallback( CheckSuccess(err, Failure); } +#if CHIP_CONFIG_TC_REQUIRED + if (failSafe.UpdateTermsAndConditionsHasBeenInvoked()) + { + // Commit terms and conditions acceptance on commissioning complete + Server::GetInstance().GetEnhancedSetupFlowProvider()->CommitTermsAndConditionsAcceptance(); + } +#endif + /* * Pass fabric of commissioner to DeviceControlSvr. * This allows device to send messages back to commissioner. @@ -456,16 +428,38 @@ bool emberAfGeneralCommissioningClusterSetTCAcknowledgementsCallback( return false; #else + MATTER_TRACE_SCOPE("SetTCAcknowledgements", "GeneralCommissioning"); + auto & failSafeContext = Server::GetInstance().GetFailSafeContext(); - MATTER_TRACE_SCOPE("SetTCAcknowledgements", "GeneralCommissioning"); Commands::SetTCAcknowledgementsResponse::Type response; EnhancedSetupFlowProvider * const enhancedSetupFlowProvider = Server::GetInstance().GetEnhancedSetupFlowProvider(); - uint16_t acknowledgements = commandData.TCUserResponse; - uint16_t acknowledgementsVersion = commandData.TCVersion; - CheckSuccess(enhancedSetupFlowProvider->SetTermsAndConditionsAcceptance(acknowledgements, acknowledgementsVersion), Failure); - CheckSuccess(checkTermsAndConditionsAcknowledgementsState(response.errorCode), Failure); - failSafeContext.SetUpdateTermsAndConditionsHasBeenInvoked(); + + Optional requiredTermsAndConditions; + + enhancedSetupFlowProvider->GetTermsAndConditionsRequirements(requiredTermsAndConditions); + + Optional acceptedTermsAndConditions = Optional({ + .value = commandData.TCUserResponse, + .version = commandData.TCVersion, + }); + + response.errorCode = CheckTermsAndConditionsAcknowledgementsState(requiredTermsAndConditions, acceptedTermsAndConditions); + + if (CommissioningErrorEnum::kOk == response.errorCode) + { + CheckSuccess(enhancedSetupFlowProvider->SetTermsAndConditionsAcceptance(acceptedTermsAndConditions), Failure); + + if (failSafeContext.IsFailSafeArmed()) + { + failSafeContext.SetUpdateTermsAndConditionsHasBeenInvoked(); + } + else + { + enhancedSetupFlowProvider->CommitTermsAndConditionsAcceptance(); + } + } + commandObj->AddResponse(commandPath, response); return true; @@ -486,18 +480,6 @@ void OnPlatformEventHandler(const DeviceLayer::ChipDeviceEvent * event, intptr_t // Clear terms and conditions acceptance on failsafe timer expiration Server::GetInstance().GetEnhancedSetupFlowProvider()->RevertTermsAndConditionsAcceptance(); } -#endif - } - - if (event->Type == DeviceLayer::DeviceEventType::kCommissioningComplete) - { -#if CHIP_CONFIG_TC_REQUIRED - auto & failSafeContext = Server::GetInstance().GetFailSafeContext(); - if (failSafeContext.UpdateTermsAndConditionsHasBeenInvoked()) - { - // Commit terms and conditions acceptance on commissioning complete - Server::GetInstance().GetEnhancedSetupFlowProvider()->CommitTermsAndConditionsAcceptance(); - } #endif } } diff --git a/src/app/server/DefaultEnhancedSetupFlowProvider.cpp b/src/app/server/DefaultEnhancedSetupFlowProvider.cpp index 9bcd0e4dc1acbc..f90ca2cb609140 100644 --- a/src/app/server/DefaultEnhancedSetupFlowProvider.cpp +++ b/src/app/server/DefaultEnhancedSetupFlowProvider.cpp @@ -30,129 +30,28 @@ CHIP_ERROR chip::app::DefaultEnhancedSetupFlowProvider::Init(TermsAndConditionsP return CHIP_NO_ERROR; } -CHIP_ERROR chip::app::DefaultEnhancedSetupFlowProvider::HasReceivedTermsAndConditionscknowledgements(bool & outReceived) const +CHIP_ERROR chip::app::DefaultEnhancedSetupFlowProvider::GetTermsAndConditionsAcknowledgements( + Optional & outTermsAndConditions) const { VerifyOrReturnError(nullptr != mTermsAndConditionsProvider, CHIP_ERROR_UNINITIALIZED); - ReturnErrorOnFailure(mTermsAndConditionsProvider->HasAcceptance(outReceived)); + ReturnErrorOnFailure(mTermsAndConditionsProvider->GetAcceptance(outTermsAndConditions)); return CHIP_NO_ERROR; } CHIP_ERROR -chip::app::DefaultEnhancedSetupFlowProvider::HasTermsAndConditionsRequiredAcknowledgementsBeenAccepted(bool & outAccepted) const +chip::app::DefaultEnhancedSetupFlowProvider::GetTermsAndConditionsRequirements( + Optional & outTermsAndConditions) const { - uint16_t requiredAcknowledgements; - uint16_t requiredAcknowledgementsVersion; - uint16_t acceptedAcknowledgements; - uint16_t acceptedAcknowledgementsVersion; - VerifyOrReturnError(nullptr != mTermsAndConditionsProvider, CHIP_ERROR_UNINITIALIZED); - ReturnErrorOnFailure(mTermsAndConditionsProvider->GetRequirements(requiredAcknowledgements, requiredAcknowledgementsVersion)); - - if (0 == requiredAcknowledgements) - { - outAccepted = true; - return CHIP_NO_ERROR; - } - - ReturnErrorOnFailure(mTermsAndConditionsProvider->GetAcceptance(acceptedAcknowledgements, acceptedAcknowledgementsVersion)); - - outAccepted = ((requiredAcknowledgements & acceptedAcknowledgements) == requiredAcknowledgements); - + ReturnErrorOnFailure(mTermsAndConditionsProvider->GetRequirements(outTermsAndConditions)); return CHIP_NO_ERROR; } -CHIP_ERROR chip::app::DefaultEnhancedSetupFlowProvider::HasTermsAndConditionsRequiredAcknowledgementsVersionBeenAccepted( - bool & outAccepted) const +CHIP_ERROR chip::app::DefaultEnhancedSetupFlowProvider::SetTermsAndConditionsAcceptance( + const Optional & inTermsAndConditions) { - uint16_t requiredAcknowledgements; - uint16_t requiredAcknowledgementsVersion; - uint16_t acceptedAcknowledgements; - uint16_t acceptedAcknowledgementsVersion; - VerifyOrReturnError(nullptr != mTermsAndConditionsProvider, CHIP_ERROR_UNINITIALIZED); - ReturnErrorOnFailure(mTermsAndConditionsProvider->GetRequirements(requiredAcknowledgements, requiredAcknowledgementsVersion)); - - if (0 == requiredAcknowledgementsVersion) - { - outAccepted = true; - return CHIP_NO_ERROR; - } - - ReturnErrorOnFailure(mTermsAndConditionsProvider->GetAcceptance(acceptedAcknowledgements, acceptedAcknowledgementsVersion)); - - outAccepted = (acceptedAcknowledgementsVersion >= requiredAcknowledgementsVersion); - - return CHIP_NO_ERROR; -} - -CHIP_ERROR chip::app::DefaultEnhancedSetupFlowProvider::IsTermsAndConditionsAcceptanceRequired(bool & outValue) const -{ - // Default implementation requires terms and conditions check only if not previously accepted. Other implementations may skip - // requiring a terms and conditions check on secondary commissioning, in the case that the required terms and conditions may - // have changed. - return HasTermsAndConditionsRequiredAcknowledgementsVersionBeenAccepted(outValue); -} - -CHIP_ERROR chip::app::DefaultEnhancedSetupFlowProvider::GetTermsAndConditionsRequiredAcknowledgements(uint16_t & outValue) const -{ - uint16_t requiredAcknowledgements; - uint16_t requiredAcknowledgementsVersion; - - VerifyOrReturnError(nullptr != mTermsAndConditionsProvider, CHIP_ERROR_UNINITIALIZED); - ReturnErrorOnFailure(mTermsAndConditionsProvider->GetRequirements(requiredAcknowledgements, requiredAcknowledgementsVersion)); - - outValue = requiredAcknowledgements; - - return CHIP_NO_ERROR; -} - -CHIP_ERROR -chip::app::DefaultEnhancedSetupFlowProvider::GetTermsAndConditionsRequiredAcknowledgementsVersion(uint16_t & outValue) const -{ - uint16_t requiredAcknowledgements; - uint16_t requiredAcknowledgementsVersion; - - VerifyOrReturnError(nullptr != mTermsAndConditionsProvider, CHIP_ERROR_UNINITIALIZED); - ReturnErrorOnFailure(mTermsAndConditionsProvider->GetRequirements(requiredAcknowledgements, requiredAcknowledgementsVersion)); - - outValue = requiredAcknowledgementsVersion; - - return CHIP_NO_ERROR; -} - -CHIP_ERROR chip::app::DefaultEnhancedSetupFlowProvider::GetTermsAndConditionsAcceptedAcknowledgements(uint16_t & outValue) const -{ - uint16_t acceptedAcknowledgements; - uint16_t acceptedAcknowledgementsVersion; - - VerifyOrReturnError(nullptr != mTermsAndConditionsProvider, CHIP_ERROR_UNINITIALIZED); - ReturnErrorOnFailure(mTermsAndConditionsProvider->GetAcceptance(acceptedAcknowledgements, acceptedAcknowledgementsVersion)); - - outValue = acceptedAcknowledgements; - - return CHIP_NO_ERROR; -} - -CHIP_ERROR -chip::app::DefaultEnhancedSetupFlowProvider::GetTermsAndConditionsAcceptedAcknowledgementsVersion(uint16_t & outValue) const -{ - uint16_t acceptedAcknowledgements; - uint16_t acceptedAcknowledgementsVersion; - - VerifyOrReturnError(nullptr != mTermsAndConditionsProvider, CHIP_ERROR_UNINITIALIZED); - ReturnErrorOnFailure(mTermsAndConditionsProvider->GetAcceptance(acceptedAcknowledgements, acceptedAcknowledgementsVersion)); - - outValue = acceptedAcknowledgementsVersion; - - return CHIP_NO_ERROR; -} - -CHIP_ERROR chip::app::DefaultEnhancedSetupFlowProvider::SetTermsAndConditionsAcceptance(uint16_t inTCAcknowledgementsValue, - uint16_t inTCAcknowledgementsVersionValue) -{ - VerifyOrReturnError(nullptr != mTermsAndConditionsProvider, CHIP_ERROR_UNINITIALIZED); - ReturnErrorOnFailure(mTermsAndConditionsProvider->SetAcceptance(inTCAcknowledgementsValue, inTCAcknowledgementsVersionValue)); - + ReturnErrorOnFailure(mTermsAndConditionsProvider->SetAcceptance(inTermsAndConditions)); return CHIP_NO_ERROR; } diff --git a/src/app/server/DefaultEnhancedSetupFlowProvider.h b/src/app/server/DefaultEnhancedSetupFlowProvider.h index 01a04269723b79..0dba132d306ec4 100644 --- a/src/app/server/DefaultEnhancedSetupFlowProvider.h +++ b/src/app/server/DefaultEnhancedSetupFlowProvider.h @@ -39,27 +39,15 @@ class DefaultEnhancedSetupFlowProvider : public EnhancedSetupFlowProvider */ CHIP_ERROR Init(TermsAndConditionsProvider * const inTermsAndConditionsProvider); - CHIP_ERROR HasReceivedTermsAndConditionscknowledgements(bool & outReceived) const override; + virtual CHIP_ERROR GetTermsAndConditionsAcknowledgements(Optional & outTermsAndConditions) const override; - CHIP_ERROR HasTermsAndConditionsRequiredAcknowledgementsBeenAccepted(bool & outAccepted) const override; + virtual CHIP_ERROR GetTermsAndConditionsRequirements(Optional & outTermsAndConditions) const override; - CHIP_ERROR HasTermsAndConditionsRequiredAcknowledgementsVersionBeenAccepted(bool & outAccepted) const override; + virtual CHIP_ERROR SetTermsAndConditionsAcceptance(const Optional & inTermsAndConditions) override; - CHIP_ERROR IsTermsAndConditionsAcceptanceRequired(bool & outValue) const override; + virtual CHIP_ERROR RevertTermsAndConditionsAcceptance() override; - CHIP_ERROR GetTermsAndConditionsRequiredAcknowledgements(uint16_t & outValue) const override; - - CHIP_ERROR GetTermsAndConditionsRequiredAcknowledgementsVersion(uint16_t & outValue) const override; - - CHIP_ERROR GetTermsAndConditionsAcceptedAcknowledgements(uint16_t & outValue) const override; - - CHIP_ERROR GetTermsAndConditionsAcceptedAcknowledgementsVersion(uint16_t & outValue) const override; - - CHIP_ERROR SetTermsAndConditionsAcceptance(uint16_t aTCAcknowledgements, uint16_t inTCAcknowledgementsVersionValue) override; - - CHIP_ERROR CommitTermsAndConditionsAcceptance() override; - - CHIP_ERROR RevertTermsAndConditionsAcceptance() override; + virtual CHIP_ERROR CommitTermsAndConditionsAcceptance() override; private: TermsAndConditionsProvider * mTermsAndConditionsProvider; diff --git a/src/app/server/DefaultTermsAndConditionsProvider.cpp b/src/app/server/DefaultTermsAndConditionsProvider.cpp index b32a507b3c52fa..e3acb855948ce5 100644 --- a/src/app/server/DefaultTermsAndConditionsProvider.cpp +++ b/src/app/server/DefaultTermsAndConditionsProvider.cpp @@ -17,6 +17,7 @@ */ #include "DefaultTermsAndConditionsProvider.h" +#include "TermsAndConditionsProvider.h" #include #include @@ -38,25 +39,20 @@ constexpr size_t kEstimatedTlvBufferSize = chip::TLV::EstimateStructOverhead(siz 2; // Extra space for rollback compatibility } // namespace -CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::Init(chip::PersistentStorageDelegate * const inPersistentStorageDelegate, - uint16_t inRequiredAcknowledgementsValue, - uint16_t inRequiredAcknowledgementsVersionValue) +CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::Init(chip::PersistentStorageDelegate * const inPersistentStorageDelegate) { VerifyOrReturnError(nullptr != inPersistentStorageDelegate, CHIP_ERROR_INVALID_ARGUMENT); - mPersistentStorageDelegate = inPersistentStorageDelegate; - mRequiredAcknowledgementsValue = inRequiredAcknowledgementsValue; - mRequiredAcknowledgementsVersionValue = inRequiredAcknowledgementsVersionValue; + mPersistentStorageDelegate = inPersistentStorageDelegate; - uint16_t acknowledgementsAcceptanceValue = 0; - uint16_t acknowledgementsAcceptanceVersionValue = 0; + mRequiredAcknowledgements = Optional({ + .value = CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS, + .version = CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION, + }); - if (CHIP_NO_ERROR == LoadAcceptance(acknowledgementsAcceptanceValue, acknowledgementsAcceptanceVersionValue)) + if (CHIP_NO_ERROR == LoadAcceptance(mLatchedAcceptance)) { - mLatchedAcknowledgementsAcceptanceValue.SetValue(acknowledgementsAcceptanceValue); - mLatchedAcknowledgementsAcceptanceVersionValue.SetValue(acknowledgementsAcceptanceVersionValue); - mTemporaryAcknowledgementsAcceptanceValue = mLatchedAcknowledgementsAcceptanceValue; - mTemporaryAcknowledgementsAcceptanceVersionValue = mLatchedAcknowledgementsAcceptanceVersionValue; + mTemporalAcceptance.SetValue(mLatchedAcceptance.Value()); } return CHIP_NO_ERROR; @@ -64,62 +60,40 @@ CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::Init(chip::PersistentSt CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::RevertAcceptance() { - mTemporaryAcknowledgementsAcceptanceValue = mLatchedAcknowledgementsAcceptanceValue; - mTemporaryAcknowledgementsAcceptanceVersionValue = mLatchedAcknowledgementsAcceptanceVersionValue; - + mTemporalAcceptance.SetValue(mLatchedAcceptance.Value()); return CHIP_NO_ERROR; } CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::CommitAcceptance() { - VerifyOrReturnError(mLatchedAcknowledgementsAcceptanceValue.HasValue() || mTemporaryAcknowledgementsAcceptanceValue.HasValue(), - CHIP_ERROR_INCORRECT_STATE); - VerifyOrReturnError(mLatchedAcknowledgementsAcceptanceVersionValue.HasValue() || - mTemporaryAcknowledgementsAcceptanceVersionValue.HasValue(), - CHIP_ERROR_INCORRECT_STATE); - - uint16_t acknowledgementsAcceptanceValue = - mTemporaryAcknowledgementsAcceptanceValue.ValueOr(mLatchedAcknowledgementsAcceptanceValue.HasValue()); - uint16_t acknowledgementsAcceptanceVersionValue = - mTemporaryAcknowledgementsAcceptanceVersionValue.ValueOr(mLatchedAcknowledgementsAcceptanceVersionValue.HasValue()); - - CHIP_ERROR err = StoreAcceptance(acknowledgementsAcceptanceValue, acknowledgementsAcceptanceVersionValue); - - if (CHIP_NO_ERROR == err) + if (!mTemporalAcceptance.HasValue()) { - mLatchedAcknowledgementsAcceptanceValue.SetValue(acknowledgementsAcceptanceValue); - mLatchedAcknowledgementsAcceptanceVersionValue.SetValue(acknowledgementsAcceptanceVersionValue); + return CHIP_NO_ERROR; } - return err; + mLatchedAcceptance.SetValue(mTemporalAcceptance.Value()); + return StoreAcceptance(mLatchedAcceptance); } -CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::GetAcceptance(uint16_t & outAcknowledgementsValue, - uint16_t & outAcknowledgementsVersionValue) const +CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::GetAcceptance(Optional & outTermsAndConditions) const { - VerifyOrReturnError(mTemporaryAcknowledgementsAcceptanceValue.HasValue(), CHIP_ERROR_INCORRECT_STATE); - VerifyOrReturnError(mTemporaryAcknowledgementsAcceptanceVersionValue.HasValue(), CHIP_ERROR_INCORRECT_STATE); - - outAcknowledgementsValue = mTemporaryAcknowledgementsAcceptanceValue.Value(); - outAcknowledgementsVersionValue = mTemporaryAcknowledgementsAcceptanceVersionValue.Value(); - - return CHIP_NO_ERROR; -} - -CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::GetRequirements(uint16_t & outAcknowledgementsValue, - uint16_t & outAcknowledgementsVersionValue) const -{ - outAcknowledgementsValue = mRequiredAcknowledgementsValue; - outAcknowledgementsVersionValue = mRequiredAcknowledgementsVersionValue; + if (!mTemporalAcceptance.HasValue()) + { + return CHIP_ERROR_INCORRECT_STATE; + } + outTermsAndConditions.SetValue(mTemporalAcceptance.Value()); return CHIP_NO_ERROR; } -CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::HasAcceptance(bool & outHasAcceptance) const +CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::GetRequirements(Optional & outTermsAndConditions) const { - outHasAcceptance = - mTemporaryAcknowledgementsAcceptanceValue.HasValue() && mTemporaryAcknowledgementsAcceptanceVersionValue.HasValue(); + if (!mRequiredAcknowledgements.HasValue()) + { + return CHIP_ERROR_INCORRECT_STATE; + } + outTermsAndConditions.SetValue(mRequiredAcknowledgements.Value()); return CHIP_NO_ERROR; } @@ -130,23 +104,19 @@ CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::ResetAcceptance() const chip::StorageKeyName storageKey = DefaultStorageKeyAllocator::TermsAndConditionsAcceptance(); ReturnErrorOnFailure(mPersistentStorageDelegate->SyncDeleteKeyValue(storageKey.KeyName())); - mLatchedAcknowledgementsAcceptanceValue.ClearValue(); - mLatchedAcknowledgementsAcceptanceVersionValue.ClearValue(); - ReturnErrorOnFailure(RevertAcceptance()); + mLatchedAcceptance.ClearValue(); + mTemporalAcceptance.ClearValue(); return CHIP_NO_ERROR; } -CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::SetAcceptance(uint16_t inAcceptedAcknowledgementsValue, - uint16_t inAcceptedAcknowledgementsVersionValue) +CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::SetAcceptance(const Optional & inTermsAndConditions) { - mTemporaryAcknowledgementsAcceptanceValue.SetValue(inAcceptedAcknowledgementsValue); - mTemporaryAcknowledgementsAcceptanceVersionValue.SetValue(inAcceptedAcknowledgementsVersionValue); + mTemporalAcceptance.SetValue(inTermsAndConditions.Value()); return CHIP_NO_ERROR; } -CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::LoadAcceptance(uint16_t & outAcknowledgementsValue, - uint16_t & outAcknowledgementsVersionValue) +CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::LoadAcceptance(Optional & outTermsAndConditions) { uint8_t serializationVersion = 0; uint16_t acknowledgements = 0; @@ -164,10 +134,7 @@ CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::LoadAcceptance(uint16_t CHIP_ERROR err = mPersistentStorageDelegate->SyncGetKeyValue(storageKey.KeyName(), &buffer, bufferSize); if (CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND == err) { - outAcknowledgementsValue = acknowledgements; - outAcknowledgementsVersionValue = acknowledgementsVersion; - - return CHIP_NO_ERROR; + return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND; } VerifyOrReturnError(CHIP_NO_ERROR == err, err); @@ -188,28 +155,28 @@ CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::LoadAcceptance(uint16_t return CHIP_ERROR_VERSION_MISMATCH; } - outAcknowledgementsValue = acknowledgements; - outAcknowledgementsVersionValue = acknowledgementsVersion; + outTermsAndConditions.SetValue({ + .value = acknowledgements, + .version = acknowledgementsVersion, + }); return CHIP_NO_ERROR; } -CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::StoreAcceptance(uint16_t inAcknowledgementsValue, - uint16_t inAcknowledgementsVersionValue) +CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::StoreAcceptance(const Optional & inTermsAndConditions) { uint8_t buffer[kEstimatedTlvBufferSize] = { 0 }; chip::TLV::TLVWriter tlvWriter; chip::TLV::TLVType tlvContainer; VerifyOrReturnError(nullptr != mPersistentStorageDelegate, CHIP_ERROR_UNINITIALIZED); - VerifyOrReturnError(mTemporaryAcknowledgementsAcceptanceValue.HasValue(), CHIP_ERROR_INCORRECT_STATE); - VerifyOrReturnError(mTemporaryAcknowledgementsAcceptanceVersionValue.HasValue(), CHIP_ERROR_INCORRECT_STATE); + VerifyOrReturnError(inTermsAndConditions.HasValue(), CHIP_ERROR_INCORRECT_STATE); tlvWriter.Init(buffer, sizeof(buffer)); ReturnErrorOnFailure(tlvWriter.StartContainer(chip::TLV::AnonymousTag(), chip::TLV::kTLVType_Structure, tlvContainer)); ReturnErrorOnFailure(tlvWriter.Put(kSerializationVersionTag, kSerializationVersion)); - ReturnErrorOnFailure(tlvWriter.Put(kAcceptedAcknowledgementsTag, inAcknowledgementsValue)); - ReturnErrorOnFailure(tlvWriter.Put(kAcceptedAcknowledgementsVersionTag, inAcknowledgementsVersionValue)); + ReturnErrorOnFailure(tlvWriter.Put(kAcceptedAcknowledgementsTag, inTermsAndConditions.Value().value)); + ReturnErrorOnFailure(tlvWriter.Put(kAcceptedAcknowledgementsVersionTag, inTermsAndConditions.Value().version)); ReturnErrorOnFailure(tlvWriter.EndContainer(tlvContainer)); ReturnErrorOnFailure(tlvWriter.Finalize()); uint32_t lengthWritten = tlvWriter.GetLengthWritten(); diff --git a/src/app/server/DefaultTermsAndConditionsProvider.h b/src/app/server/DefaultTermsAndConditionsProvider.h index c68293d5143642..af86f8a4c95fed 100644 --- a/src/app/server/DefaultTermsAndConditionsProvider.h +++ b/src/app/server/DefaultTermsAndConditionsProvider.h @@ -35,40 +35,31 @@ class DefaultTermsAndConditionsProvider : public TermsAndConditionsProvider * @brief Initializes the TermsAndConditionsProvider. * * @param[in] inPersistentStorageDelegate Persistent storage delegate dependency. - * @param[in] inRequiredAcknowledgementsValue The bitmask of required acknowledgements. - * @param[in] inRequiredAcknowledgementsVersionValue The version of the required acknowledgements. */ - CHIP_ERROR Init(PersistentStorageDelegate * const inPersistentStorageDelegate, uint16_t inRequiredAcknowledgementsValue, - uint16_t inRequiredAcknowledgementsVersionValue); + CHIP_ERROR Init(PersistentStorageDelegate * const inPersistentStorageDelegate); - CHIP_ERROR CommitAcceptance() override; - - CHIP_ERROR GetAcceptance(uint16_t & outAcknowledgementsValue, uint16_t & outAcknowledgementsVersionValue) const override; + CHIP_ERROR SetAcceptance(const Optional & inTermsAndConditions) override; - CHIP_ERROR GetRequirements(uint16_t & outAcknowledgementsValue, uint16_t & outAcknowledgementsVersionValue) const override; + CHIP_ERROR CommitAcceptance() override; - CHIP_ERROR HasAcceptance(bool & outHasAcceptance) const override; + CHIP_ERROR RevertAcceptance() override; CHIP_ERROR ResetAcceptance() override; - CHIP_ERROR RevertAcceptance() override; + CHIP_ERROR GetAcceptance(Optional & outTermsAndConditions) const override; - CHIP_ERROR SetAcceptance(uint16_t inAcknowledgementsValue, uint16_t inAcknowledgementsVersionValue) override; + CHIP_ERROR GetRequirements(Optional & outTermsAndConditions) const override; private: - CHIP_ERROR LoadAcceptance(uint16_t & outAcknowledgementsValue, uint16_t & outAcknowledgementsVersionValue); + CHIP_ERROR LoadAcceptance(Optional & outTermsAndConditions); - CHIP_ERROR StoreAcceptance(uint16_t inAcknowledgementsValue, uint16_t inAcknowledgementsVersionValue); + CHIP_ERROR StoreAcceptance(const Optional & inTermsAndConditions); PersistentStorageDelegate * mPersistentStorageDelegate; - uint16_t mRequiredAcknowledgementsValue; - uint16_t mRequiredAcknowledgementsVersionValue; - - Optional mLatchedAcknowledgementsAcceptanceValue; - Optional mLatchedAcknowledgementsAcceptanceVersionValue; - Optional mTemporaryAcknowledgementsAcceptanceValue; - Optional mTemporaryAcknowledgementsAcceptanceVersionValue; + Optional mLatchedAcceptance; + Optional mTemporalAcceptance; + Optional mRequiredAcknowledgements; }; } // namespace app diff --git a/src/app/server/EnhancedSetupFlowProvider.h b/src/app/server/EnhancedSetupFlowProvider.h index fd9862fd69ef2c..e4b5ce7ce89864 100644 --- a/src/app/server/EnhancedSetupFlowProvider.h +++ b/src/app/server/EnhancedSetupFlowProvider.h @@ -22,6 +22,8 @@ #include +#include "TermsAndConditionsProvider.h" + namespace chip { namespace app { @@ -33,55 +35,12 @@ class EnhancedSetupFlowProvider public: virtual ~EnhancedSetupFlowProvider() = default; - virtual CHIP_ERROR HasReceivedTermsAndConditionscknowledgements(bool & outReceived) const = 0; - - /** - * @param[out] outAccepted true if the required acknowledgements have been accepted, false otherwise. - */ - virtual CHIP_ERROR HasTermsAndConditionsRequiredAcknowledgementsBeenAccepted(bool & outAccepted) const = 0; - - /** - * @param[out] outAccepted true if the required acknowledgements version has been accepted, false otherwise. - */ - virtual CHIP_ERROR HasTermsAndConditionsRequiredAcknowledgementsVersionBeenAccepted(bool & outAccepted) const = 0; - - /** - * @param[out] outValue true if terms and conditions acceptance is required before commissioning complete may succeed, false - * otherwise. - */ - virtual CHIP_ERROR IsTermsAndConditionsAcceptanceRequired(bool & outValue) const = 0; - - /** - * @param[out] outValue The version of the required acknowledgements. - */ - virtual CHIP_ERROR GetTermsAndConditionsRequiredAcknowledgements(uint16_t & outValue) const = 0; - - /** - * @param[out] outValue The outValue of the required acknowledgements version. - */ - virtual CHIP_ERROR GetTermsAndConditionsRequiredAcknowledgementsVersion(uint16_t & outValue) const = 0; - - /** - * @param[out] outValue The outValue of the accepted acknowledgements. - */ - virtual CHIP_ERROR GetTermsAndConditionsAcceptedAcknowledgements(uint16_t & outValue) const = 0; + virtual CHIP_ERROR GetTermsAndConditionsAcknowledgements(Optional & outTermsAndConditions) const = 0; - /** - * @param[out] outValue The outValue of the accepted acknowledgements version. - */ - virtual CHIP_ERROR GetTermsAndConditionsAcceptedAcknowledgementsVersion(uint16_t & outValue) const = 0; + virtual CHIP_ERROR GetTermsAndConditionsRequirements(Optional & outTermsAndConditions) const = 0; - /** - * @param[in] inTCAcknowledgements The acknowledgements to accept. - * @param[in] inTCAcknowledgementsVersionValue The version of the acknowledgements to accept. - */ - virtual CHIP_ERROR SetTermsAndConditionsAcceptance(uint16_t inTCAcknowledgementsValue, - uint16_t inTCAcknowledgementsVersionValue) = 0; + virtual CHIP_ERROR SetTermsAndConditionsAcceptance(const Optional & inTermsAndConditions) = 0; - /** - * Reset the terms and conditions acceptance. The terms and conditions should be cleared on factory reset or if failure occurs - * during a failsafe context. - */ virtual CHIP_ERROR RevertTermsAndConditionsAcceptance() = 0; virtual CHIP_ERROR CommitTermsAndConditionsAcceptance() = 0; diff --git a/src/app/server/Server.h b/src/app/server/Server.h index cb8ab12f40f547..d6e19cb6b6f767 100644 --- a/src/app/server/Server.h +++ b/src/app/server/Server.h @@ -317,9 +317,7 @@ struct CommonCaseDeviceServerInitParams : public ServerInitParams static app::DefaultTermsAndConditionsProvider sDefaultTermsAndConditionsProviderInstance; if (this->termsAndConditionsProvider == nullptr) { - ReturnErrorOnFailure(sDefaultTermsAndConditionsProviderInstance.Init(this->persistentStorageDelegate, - CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS, - CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION)); + ReturnErrorOnFailure(sDefaultTermsAndConditionsProviderInstance.Init(this->persistentStorageDelegate)); this->termsAndConditionsProvider = &sDefaultTermsAndConditionsProviderInstance; } diff --git a/src/app/server/TermsAndConditionsProvider.h b/src/app/server/TermsAndConditionsProvider.h index 45268d78baf23f..5a1909e9c51e6b 100644 --- a/src/app/server/TermsAndConditionsProvider.h +++ b/src/app/server/TermsAndConditionsProvider.h @@ -22,10 +22,17 @@ #include #include +#include namespace chip { namespace app { +typedef struct sTermsAndConditions +{ + uint16_t value; + uint16_t version; +} TermsAndConditions; + /** * @brief Data access layer for the required terms and conditions and the store for the user acceptance. */ @@ -35,9 +42,9 @@ class TermsAndConditionsProvider virtual ~TermsAndConditionsProvider() = default; /** - * @brief Clear the temporary acceptance status of the required terms and conditions. + * @brief Sets the acceptance status of the required terms and conditions. */ - virtual CHIP_ERROR RevertAcceptance() = 0; + virtual CHIP_ERROR SetAcceptance(const Optional & inTermsAndConditions) = 0; /** * @brief Commit the persistent acceptance status of the required terms and conditions. @@ -45,40 +52,24 @@ class TermsAndConditionsProvider virtual CHIP_ERROR CommitAcceptance() = 0; /** - * @brief Retrieves the latest acceptance status of the required terms and conditions. - * - * @param[out] outAcknowledgementsValue The bitmask of acknowledgements accepted. - * @param[out] outAcknowledgementsVersionValue The version of the accepted acknowledgements. - */ - virtual CHIP_ERROR GetAcceptance(uint16_t & outAcknowledgementsValue, uint16_t & outAcknowledgementsVersionValue) const = 0; - - /** - * @brief Retrieves the requirements of the terms and conditions. - * - * @param[out] outAcknowledgementsValue The bitmask of required acknowledgements. - * @param[out] outAcknowledgementsVersionValue The version of the required acknowledgements. + * @brief Clear the temporary acceptance status of the required terms and conditions. */ - virtual CHIP_ERROR GetRequirements(uint16_t & outAcknowledgementsValue, uint16_t & outAcknowledgementsVersionValue) const = 0; + virtual CHIP_ERROR RevertAcceptance() = 0; /** - * @brief Retrieve if any terms and conditions has been set. - * - * @param[out] outHasAcceptance True if any acceptance has been set. + * @brief Reset the persisted acceptance status of the required terms and conditions. */ - virtual CHIP_ERROR HasAcceptance(bool & outHasAcceptance) const = 0; + virtual CHIP_ERROR ResetAcceptance() = 0; /** - * @brief Reset the persisted acceptance status of the required terms and conditions. + * @brief Retrieves the latest acceptance status of the required terms and conditions. */ - virtual CHIP_ERROR ResetAcceptance() = 0; + virtual CHIP_ERROR GetAcceptance(Optional & outTermsAndConditions) const = 0; /** - * @brief Sets the acceptance status of the required terms and conditions. - * - * @param[in] inAcknowledgementsValue The bitmask of acknowledgements that was accepted. - * @param[in] inAcknowledgementsVersionValue The version of the acknowledgements that was accepted. + * @brief Retrieves the requirements of the terms and conditions. */ - virtual CHIP_ERROR SetAcceptance(uint16_t inAcknowledgementsValue, uint16_t inAcknowledgementsVersionValue) = 0; + virtual CHIP_ERROR GetRequirements(Optional & outTermsAndConditions) const = 0; }; } // namespace app diff --git a/src/app/tests/TestDefaultEnhancedSetupFlowProvider.cpp b/src/app/tests/TestDefaultEnhancedSetupFlowProvider.cpp index 8ef94119ef54ee..24213068e6b1a2 100644 --- a/src/app/tests/TestDefaultEnhancedSetupFlowProvider.cpp +++ b/src/app/tests/TestDefaultEnhancedSetupFlowProvider.cpp @@ -54,6 +54,8 @@ class FakeTermsAndConditionsProvider : public chip::app::TermsAndConditionsProvi return CHIP_NO_ERROR; } + CHIP_ERROR HasAcceptance(bool & outHasAcceptance) const override { return CHIP_NO_ERROR; } + CHIP_ERROR SetAcceptance(uint16_t inAcknowledgements, uint16_t inAcknowledgementsVersion) override { mAcceptedAcknowledgements = inAcknowledgements; diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp index c1b788aca643bd..8f2448a720977c 100644 --- a/src/controller/CHIPDeviceController.cpp +++ b/src/controller/CHIPDeviceController.cpp @@ -3207,6 +3207,7 @@ void DeviceCommissioner::PerformCommissioningStep(DeviceProxy * proxy, Commissio if (!params.GetRequireTermsAndConditionsAcknowledgement()) { + ChipLogProgress(Controller, "Setting Terms and Conditions: Skipped"); CommissioningStageComplete(CHIP_NO_ERROR); return; } @@ -3222,6 +3223,8 @@ void DeviceCommissioner::PerformCommissioningStep(DeviceProxy * proxy, Commissio TermsAndConditionsAcknowledgement termsAndConditionsAcknowledgement = params.GetTermsAndConditionsAcknowledgement().Value(); request.TCUserResponse = termsAndConditionsAcknowledgement.acceptedTermsAndConditions; request.TCVersion = termsAndConditionsAcknowledgement.acceptedTermsAndConditionsVersion; + + ChipLogProgress(Controller, "Setting Terms and Conditions: %hu, %hu", request.TCUserResponse, request.TCVersion); CHIP_ERROR err = SendCommissioningCommand(proxy, request, OnSetTCAcknowledgementsResponse, OnBasicFailure, endpoint, timeout); if (err != CHIP_NO_ERROR) diff --git a/src/controller/python/ChipDeviceController-ScriptBinding.cpp b/src/controller/python/ChipDeviceController-ScriptBinding.cpp index b31cb5b7b8a4e6..d2d5e12a2f0160 100644 --- a/src/controller/python/ChipDeviceController-ScriptBinding.cpp +++ b/src/controller/python/ChipDeviceController-ScriptBinding.cpp @@ -181,6 +181,9 @@ PyChipError pychip_DeviceController_OpenCommissioningWindow(chip::Controller::De bool pychip_DeviceController_GetIPForDiscoveredDevice(chip::Controller::DeviceCommissioner * devCtrl, int idx, char * addrStr, uint32_t len); +PyChipError pychip_DeviceController_SetRequireTermsAndConditionsAcknowledgement(bool tcRequired); +PyChipError pychip_DeviceController_SetTermsAcknowledgements(uint16_t tcVersion, uint16_t tcUserResponse); +PyChipError pychip_DeviceController_SetSkipCommissioningComplete(bool skipCommissioningComplete); // Pairing Delegate PyChipError @@ -570,6 +573,25 @@ PyChipError pychip_DeviceController_SetDefaultNtp(const char * defaultNTP) return ToPyChipError(CHIP_NO_ERROR); } +PyChipError pychip_DeviceController_SetRequireTermsAndConditionsAcknowledgement(bool tcRequired) +{ + sCommissioningParameters.SetRequireTermsAndConditionsAcknowledgement(tcRequired); + return ToPyChipError(CHIP_NO_ERROR); +} + +PyChipError pychip_DeviceController_SetTermsAcknowledgements(uint16_t tcVersion, uint16_t tcUserResponse) +{ + sCommissioningParameters.SetTermsAndConditionsAcknowledgement( + { .acceptedTermsAndConditions = tcUserResponse, .acceptedTermsAndConditionsVersion = tcVersion }); + return ToPyChipError(CHIP_NO_ERROR); +} + +PyChipError pychip_DeviceController_SetSkipCommissioningComplete(bool skipCommissioningComplete) +{ + sCommissioningParameters.SetSkipCommissioningComplete(skipCommissioningComplete); + return ToPyChipError(CHIP_NO_ERROR); +} + PyChipError pychip_DeviceController_SetTrustedTimeSource(chip::NodeId nodeId, chip::EndpointId endpoint) { chip::app::Clusters::TimeSynchronization::Structs::FabricScopedTrustedTimeSourceStruct::Type timeSource = { .nodeID = nodeId, diff --git a/src/controller/python/chip/ChipDeviceCtrl.py b/src/controller/python/chip/ChipDeviceCtrl.py index 24fe59486f78d5..6b59193a505410 100644 --- a/src/controller/python/chip/ChipDeviceCtrl.py +++ b/src/controller/python/chip/ChipDeviceCtrl.py @@ -1938,6 +1938,15 @@ def _InitLib(self): self._dmLib.pychip_DeviceProxy_GetRemoteSessionParameters.restype = PyChipError self._dmLib.pychip_DeviceProxy_GetRemoteSessionParameters.argtypes = [c_void_p, c_char_p] + self._dmLib.pychip_DeviceController_SetSkipCommissioningComplete.restype = PyChipError + self._dmLib.pychip_DeviceController_SetSkipCommissioningComplete.argtypes = [c_bool] + + self._dmLib.pychip_DeviceController_SetRequireTermsAndConditionsAcknowledgement.restype = PyChipError + self._dmLib.pychip_DeviceController_SetRequireTermsAndConditionsAcknowledgement.argtypes = [c_bool] + + self._dmLib.pychip_DeviceController_SetTermsAcknowledgements.restype = PyChipError + self._dmLib.pychip_DeviceController_SetTermsAcknowledgements.argtypes = [c_uint16, c_uint16] + class ChipDeviceController(ChipDeviceControllerBase): ''' The ChipDeviceCommissioner binding, named as ChipDeviceController @@ -2066,6 +2075,27 @@ def SetDSTOffset(self, offset: int, validStarting: int, validUntil: int): lambda: self._dmLib.pychip_DeviceController_SetDSTOffset(offset, validStarting, validUntil) ).raise_on_error() + def SetTCRequired(self, tcRequired: bool): + ''' Set whether TC Acknowledgements should be set during commissioning''' + self.CheckIsActive() + self._ChipStack.Call( + lambda: self._dmLib.pychip_DeviceController_SetRequireTermsAndConditionsAcknowledgement(tcRequired) + ).raise_on_error() + + def SetTCAcknowledgements(self, tcAcceptedVersion: int, tcUserResponse: int): + ''' Set the TC acknowledgements to set during commissioning''' + self.CheckIsActive() + self._ChipStack.Call( + lambda: self._dmLib.pychip_DeviceController_SetTermsAcknowledgements(tcAcceptedVersion, tcUserResponse) + ).raise_on_error() + + def SetSkipCommissioningComplete(self, skipCommissioningComplete: bool): + ''' Set whether to skip the commissioning complete callback''' + self.CheckIsActive() + self._ChipStack.Call( + lambda: self._dmLib.pychip_DeviceController_SetSkipCommissioningComplete(skipCommissioningComplete) + ).raise_on_error() + def SetDefaultNTP(self, defaultNTP: str): ''' Set the DefaultNTP to set during commissioning''' self.CheckIsActive() diff --git a/src/python_testing/matter_testing_support.py b/src/python_testing/matter_testing_support.py index 4127ead64c3083..b085750bc44821 100644 --- a/src/python_testing/matter_testing_support.py +++ b/src/python_testing/matter_testing_support.py @@ -573,6 +573,10 @@ class MatterTestConfig: trace_to: List[str] = field(default_factory=list) + # Accepted Terms and Conditions if used + tc_version: int = None + tc_user_response: int = None + class ClusterMapper: """Describe clusters/attributes using schema names.""" @@ -1666,6 +1670,9 @@ def convert_args_to_matter_config(args: argparse.Namespace) -> MatterTestConfig: config.controller_node_id = args.controller_node_id config.trace_to = args.trace_to + config.tc_version = args.tc_version + config.tc_user_response = args.tc_user_response + # Accumulate all command-line-passed named args all_global_args = [] argsets = [item for item in (args.int_arg, args.float_arg, args.string_arg, args.json_arg, @@ -1759,6 +1766,10 @@ def parse_matter_test_args(argv: Optional[List[str]] = None) -> MatterTestConfig commission_group.add_argument('--commission-only', action="store_true", default=False, help="If true, test exits after commissioning without running subsequent tests") + commission_group.add_argument('--tc-version', type=int, help="Terms and conditions version") + + commission_group.add_argument('--tc-user-response', type=int, help="Terms and conditions acknowledgements") + code_group = parser.add_mutually_exclusive_group(required=False) code_group.add_argument('-q', '--qr-code', type=str, @@ -2037,6 +2048,13 @@ async def _commission_device(self, i) -> bool: info = self.get_setup_payload_info()[i] + if conf.tc_version is not None and conf.tc_user_response is not None: + logging.debug(f"Setting TC Acknowledgements to version {conf.tc_version} with user response {conf.tc_user_response}.") + dev_ctrl.SetTCAcknowledgements(conf.tc_version, conf.tc_user_response) + dev_ctrl.SetTCRequired(True) + else: + dev_ctrl.SetTCRequired(False) + if conf.commissioning_method == "on-network": try: await dev_ctrl.CommissionOnNetwork(