From 6d889cffab14ca4901ec7ce085b4f0d4c052b978 Mon Sep 17 00:00:00 2001 From: James Swan <122404367+swan-amazon@users.noreply.github.com> Date: Thu, 1 Aug 2024 21:42:01 +0000 Subject: [PATCH] wip --- .../server/DefaultEnhancedSetupFlowProvider.h | 1 - .../DefaultTermsAndConditionsProvider.cpp | 16 +++++- .../DefaultTermsAndConditionsProvider.h | 1 - src/app/server/EnhancedSetupFlowProvider.h | 10 +--- src/app/server/Server.cpp | 8 +-- src/app/server/Server.h | 8 +-- src/app/server/TermsAndConditionsProvider.h | 5 -- src/system/SystemConfig.h | 54 +++++++++++++++++++ 8 files changed, 77 insertions(+), 26 deletions(-) diff --git a/src/app/server/DefaultEnhancedSetupFlowProvider.h b/src/app/server/DefaultEnhancedSetupFlowProvider.h index 19cfa7f77791c8..d5bd31f6ecb35f 100644 --- a/src/app/server/DefaultEnhancedSetupFlowProvider.h +++ b/src/app/server/DefaultEnhancedSetupFlowProvider.h @@ -37,7 +37,6 @@ class DefaultEnhancedSetupFlowProvider : public EnhancedSetupFlowProvider * @brief Initializes the EnhancedSetupFlowProvider. * * @param[in] inTermsAndConditionsProvider The terms and conditions provide dependency. - * @return CHIP_ERROR On success returns CHIP_NO_ERROR, otherwise returns an error code. */ CHIP_ERROR Init(TermsAndConditionsProvider * const inTermsAndConditionsProvider); diff --git a/src/app/server/DefaultTermsAndConditionsProvider.cpp b/src/app/server/DefaultTermsAndConditionsProvider.cpp index 0ce37729b058ba..e57734346cd1e7 100644 --- a/src/app/server/DefaultTermsAndConditionsProvider.cpp +++ b/src/app/server/DefaultTermsAndConditionsProvider.cpp @@ -23,6 +23,7 @@ #include #include #include +#include namespace { constexpr chip::TLV::Tag kSerializationVersionTag = chip::TLV::ContextTag(1); @@ -58,6 +59,7 @@ CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::ClearAcceptance() CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::GetAcceptance(uint16_t & outAcknowledgementsValue, uint16_t & outAcknowledgementsVersionValue) const { + uint8_t serializationVersion = 0; uint16_t acknowledgements = 0; uint16_t acknowledgementsVersion = 0; @@ -81,15 +83,22 @@ CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::GetAcceptance(uint16_t VerifyOrReturnError(CHIP_NO_ERROR == err, err); - tlvReader.Init(buffer); + tlvReader.Init(buffer, bufferSize); ReturnErrorOnFailure(tlvReader.Next(chip::TLV::kTLVType_Structure, chip::TLV::AnonymousTag())); ReturnErrorOnFailure(tlvReader.EnterContainer(tlvContainer)); + ReturnErrorOnFailure(tlvReader.Next(kSerializationVersionTag)); + ReturnErrorOnFailure(tlvReader.Get(serializationVersion)); ReturnErrorOnFailure(tlvReader.Next(kAcceptedAcknowledgementsTag)); ReturnErrorOnFailure(tlvReader.Get(acknowledgements)); ReturnErrorOnFailure(tlvReader.Next(kAcceptedAcknowledgementsVersionTag)); ReturnErrorOnFailure(tlvReader.Get(acknowledgementsVersion)); ReturnErrorOnFailure(tlvReader.ExitContainer(tlvContainer)); + if (kSerializationVersion != serializationVersion) + { + return CHIP_ERROR_VERSION_MISMATCH; + } + outAcknowledgementsValue = acknowledgements; outAcknowledgementsVersionValue = acknowledgementsVersion; @@ -121,9 +130,12 @@ CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::SetAcceptance(uint16_t ReturnErrorOnFailure(tlvWriter.Put(kAcceptedAcknowledgementsVersionTag, inAcceptedAcknowledgementsVersionValue)); ReturnErrorOnFailure(tlvWriter.EndContainer(tlvContainer)); ReturnErrorOnFailure(tlvWriter.Finalize()); + uint32_t lengthWritten = tlvWriter.GetLengthWritten(); + VerifyOrReturnError(CanCastTo(lengthWritten), CHIP_ERROR_BUFFER_TOO_SMALL); const chip::StorageKeyName storageKey = DefaultStorageKeyAllocator::TermsAndConditionsAcceptance(); - ReturnErrorOnFailure(mPersistentStorageDelegate->SyncSetKeyValue(storageKey.KeyName(), buffer, sizeof(buffer))); + ReturnErrorOnFailure( + mPersistentStorageDelegate->SyncSetKeyValue(storageKey.KeyName(), buffer, static_cast(lengthWritten))); return CHIP_NO_ERROR; } diff --git a/src/app/server/DefaultTermsAndConditionsProvider.h b/src/app/server/DefaultTermsAndConditionsProvider.h index d70d0f124db6df..49f59bdc661cf8 100644 --- a/src/app/server/DefaultTermsAndConditionsProvider.h +++ b/src/app/server/DefaultTermsAndConditionsProvider.h @@ -38,7 +38,6 @@ class DefaultTermsAndConditionsProvider : public 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. - * @return CHIP_ERROR On success returns CHIP_NO_ERROR, otherwise returns an error code. */ CHIP_ERROR Init(PersistentStorageDelegate * const inPersistentStorageDelegate, uint16_t inRequiredAcknowledgementsValue, uint16_t inRequiredAcknowledgementsVersionValue); diff --git a/src/app/server/EnhancedSetupFlowProvider.h b/src/app/server/EnhancedSetupFlowProvider.h index 5e8d276a3d3cd7..9a52075f8bdd75 100644 --- a/src/app/server/EnhancedSetupFlowProvider.h +++ b/src/app/server/EnhancedSetupFlowProvider.h @@ -30,7 +30,7 @@ namespace app { /** * @brief Feature state access layer for the EnhancedSetupFlowProvider. * - * This class provides access to the state of the feature through the TermsAndConditionsProvider. + * This class provides access to the state of the Enhanced Setup Flow feature. */ class EnhancedSetupFlowProvider { @@ -44,7 +44,6 @@ class EnhancedSetupFlowProvider * @brief Checks if the required terms and conditions acknowledgements have been accepted. * * @param[out] outAccepted true if the required acknowledgements have been accepted, false otherwise. - * @return CHIP_ERROR On success returns CHIP_NO_ERROR, otherwise returns an error code. */ virtual CHIP_ERROR HasTermsAndConditionsRequiredAcknowledgementsBeenAccepted(bool & outAccepted) const = 0; @@ -52,7 +51,6 @@ class EnhancedSetupFlowProvider * @brief Checks if the required terms and conditions acknowledgements version has been accepted. * * @param[out] outAccepted true if the required acknowledgements version has been accepted, false otherwise. - * @return CHIP_ERROR On success returns CHIP_NO_ERROR, otherwise returns an error code. */ virtual CHIP_ERROR HasTermsAndConditionsRequiredAcknowledgementsVersionBeenAccepted(bool & outAccepted) const = 0; @@ -60,7 +58,6 @@ class EnhancedSetupFlowProvider * @brief Retrieves the required terms and conditions acknowledgements. * * @param[out] outValue The version of the required acknowledgements. - * @return CHIP_ERROR On success returns CHIP_NO_ERROR, otherwise returns an error code. */ virtual CHIP_ERROR GetTermsAndConditionsRequiredAcknowledgements(uint16_t & outValue) const = 0; @@ -68,7 +65,6 @@ class EnhancedSetupFlowProvider * @brief Retrieves the required terms and conditions acknowledgements version. * * @param[out] outValue The outValue of the required acknowledgements version. - * @return CHIP_ERROR On success returns CHIP_NO_ERROR, otherwise returns an error code. */ virtual CHIP_ERROR GetTermsAndConditionsRequiredAcknowledgementsVersion(uint16_t & outValue) const = 0; @@ -76,7 +72,6 @@ class EnhancedSetupFlowProvider * @brief Retrieves the accepted terms and conditions acknowledgements. * * @param[out] outValue The outValue of the accepted acknowledgements. - * @return CHIP_ERROR On success returns CHIP_NO_ERROR, otherwise returns an error code. */ virtual CHIP_ERROR GetTermsAndConditionsAcceptedAcknowledgements(uint16_t & outValue) const = 0; @@ -84,7 +79,6 @@ class EnhancedSetupFlowProvider * @brief Retrieves the accepted terms and conditions acknowledgements version. * * @param[out] outValue The outValue of the accepted acknowledgements version. - * @return CHIP_ERROR On success returns CHIP_NO_ERROR, otherwise returns an error code. */ virtual CHIP_ERROR GetTermsAndConditionsAcceptedAcknowledgementsVersion(uint16_t & outValue) const = 0; @@ -93,7 +87,6 @@ class EnhancedSetupFlowProvider * * @param[in] inTCAcknowledgements The acknowledgements to accept. * @param[in] inTCAcknowledgementsVersionValue The version of the acknowledgements to accept. - * @return CHIP_ERROR On success returns CHIP_NO_ERROR, otherwise returns an error code. */ virtual CHIP_ERROR SetTermsAndConditionsAcceptance(uint16_t inTCAcknowledgementsValue, uint16_t inTCAcknowledgementsVersionValue) = 0; @@ -101,7 +94,6 @@ class EnhancedSetupFlowProvider /** * @brief Clears the acceptance status of the terms and conditions. * - * @return CHIP_ERROR On success returns CHIP_NO_ERROR, otherwise returns an error code. */ virtual CHIP_ERROR ClearTermsAndConditionsAcceptance() = 0; }; diff --git a/src/app/server/Server.cpp b/src/app/server/Server.cpp index 46d523a6757e0e..58ff786ad40c2a 100644 --- a/src/app/server/Server.cpp +++ b/src/app/server/Server.cpp @@ -107,7 +107,7 @@ static ::chip::PersistedCounter sGlobalEventIdCounter; static ::chip::app::CircularEventBuffer sLoggingBuffer[CHIP_NUM_EVENT_LOGGING_BUFFERS]; #endif // CHIP_CONFIG_ENABLE_SERVER_IM_EVENT -#if defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS) && defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION) +#if CHIP_CONFIG_TC_REQUIRED app::DefaultEnhancedSetupFlowProvider sDefaultEnhancedSetupFlowProviderInstance; app::EnhancedSetupFlowProvider * CommonCaseDeviceServerInitParams::sDefaultEnhancedSetupFlowProvider = &sDefaultEnhancedSetupFlowProviderInstance; @@ -155,7 +155,7 @@ CHIP_ERROR CommonCaseDeviceServerInitParams::InitializeStaticResourcesBeforeServ reportScheduler = &sReportScheduler; } -#if defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS) && defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION) +#if CHIP_CONFIG_TC_REQUIRED if (this->termsAndConditionsProvider == nullptr) { ReturnErrorOnFailure(sDefaultTermsAndConditionsProviderInstance.Init(this->persistentStorageDelegate, @@ -235,7 +235,7 @@ CHIP_ERROR Server::Init(const ServerInitParams & initParams) VerifyOrExit(initParams.operationalKeystore != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT); VerifyOrExit(initParams.opCertStore != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT); VerifyOrExit(initParams.reportScheduler != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT); -#if defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS) && defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION) +#if CHIP_CONFIG_TC_REQUIRED VerifyOrExit(initParams.enhancedSetupFlowProvider != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT); VerifyOrExit(initParams.termsAndConditionsProvider != nullptr, err = CHIP_ERROR_INVALID_ARGUMENT); #endif @@ -295,7 +295,7 @@ CHIP_ERROR Server::Init(const ServerInitParams & initParams) mReportScheduler = initParams.reportScheduler; -#if defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS) && defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION) +#if CHIP_CONFIG_TC_REQUIRED mTermsAndConditionsProvider = initParams.termsAndConditionsProvider; mEnhancedSetupFlowProvider = initParams.enhancedSetupFlowProvider; #endif diff --git a/src/app/server/Server.h b/src/app/server/Server.h index 739cffcd445476..b4141c7afca239 100644 --- a/src/app/server/Server.h +++ b/src/app/server/Server.h @@ -181,7 +181,7 @@ struct ServerInitParams // Optional. Support for the ICD Check-In BackOff strategy. Must be initialized before being provided. // If the ICD Check-In protocol use-case is supported and no strategy is provided, server will use the default strategy. app::ICDCheckInBackOffStrategy * icdCheckInBackOffStrategy = nullptr; -#if defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS) && defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION) +#if CHIP_CONFIG_TC_REQUIRED // Optional. Enhanced setup flow provider to support terms and conditions acceptance check. app::EnhancedSetupFlowProvider * enhancedSetupFlowProvider = nullptr; // Optional. Terms and conditions provider to support enhanced setup flow feature. @@ -254,7 +254,7 @@ struct CommonCaseDeviceServerInitParams : public ServerInitParams #if CHIP_CONFIG_ENABLE_ICD_CIP static app::DefaultICDCheckInBackOffStrategy sDefaultICDCheckInBackOffStrategy; #endif -#if defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS) && defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION) +#if CHIP_CONFIG_TC_REQUIRED static app::EnhancedSetupFlowProvider * sDefaultEnhancedSetupFlowProvider; static app::TermsAndConditionsProvider * sDefaultTermsAndConditionsProvider; #endif @@ -338,7 +338,7 @@ class Server app::reporting::ReportScheduler * GetReportScheduler() { return mReportScheduler; } -#if defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS) && defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION) +#if CHIP_CONFIG_TC_REQUIRED app::EnhancedSetupFlowProvider * GetEnhancedSetupFlowProvider() { return mEnhancedSetupFlowProvider; } #endif @@ -615,7 +615,7 @@ class Server GroupDataProviderListener mListener; ServerFabricDelegate mFabricDelegate; app::reporting::ReportScheduler * mReportScheduler; -#if defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS) && defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION) +#if CHIP_CONFIG_TC_REQUIRED app::EnhancedSetupFlowProvider * mEnhancedSetupFlowProvider; app::TermsAndConditionsProvider * mTermsAndConditionsProvider; #endif diff --git a/src/app/server/TermsAndConditionsProvider.h b/src/app/server/TermsAndConditionsProvider.h index 55e00d3c74e04d..99dfbebc3caa2d 100644 --- a/src/app/server/TermsAndConditionsProvider.h +++ b/src/app/server/TermsAndConditionsProvider.h @@ -40,8 +40,6 @@ class TermsAndConditionsProvider /** * @brief Sets the acceptance status of the required terms and conditions. - * - * @return CHIP_ERROR On success returns CHIP_NO_ERROR, otherwise returns an error code. */ virtual CHIP_ERROR ClearAcceptance() = 0; @@ -50,7 +48,6 @@ class TermsAndConditionsProvider * * @param[out] outAcknowledgementsValue The bitmask of acknowledgements accepted. * @param[out] outAcknowledgementsVersionValue The version of the accepted acknowledgements. - * @return CHIP_ERROR On success returns CHIP_NO_ERROR, otherwise returns an error code. */ virtual CHIP_ERROR GetAcceptance(uint16_t & outAcknowledgementsValue, uint16_t & outAcknowledgementsVersionValue) const = 0; @@ -59,7 +56,6 @@ class TermsAndConditionsProvider * * @param[out] outAcknowledgementsValue The bitmask of required acknowledgements. * @param[out] outAcknowledgementsVersionValue The version of the required acknowledgements. - * @return CHIP_ERROR On success returns CHIP_NO_ERROR, otherwise returns an error code. */ virtual CHIP_ERROR GetRequirements(uint16_t & outAcknowledgementsValue, uint16_t & outAcknowledgementsVersionValue) const = 0; @@ -68,7 +64,6 @@ class TermsAndConditionsProvider * * @param[in] inAcknowledgementsValue The bitmask of acknowledgements that was accepted. * @param[in] inAcknowledgementsVersionValue The version of the acknowledgements that was accepted. - * @return CHIP_ERROR On success returns CHIP_NO_ERROR, otherwise returns an error code. */ virtual CHIP_ERROR SetAcceptance(uint16_t inAcknowledgementsValue, uint16_t inAcknowledgementsVersionValue) = 0; }; diff --git a/src/system/SystemConfig.h b/src/system/SystemConfig.h index 58339df4be1e47..cb6192ebbb2b5d 100644 --- a/src/system/SystemConfig.h +++ b/src/system/SystemConfig.h @@ -808,3 +808,57 @@ struct LwIPEvent; #ifndef CHIP_SYSTEM_CONFIG_MAX_LARGE_BUFFER_SIZE_BYTES #define CHIP_SYSTEM_CONFIG_MAX_LARGE_BUFFER_SIZE_BYTES (64000) #endif + +/** + * @file + * Configuration settings for Terms and Conditions (TC) acknowledgements during device commissioning. + */ + +/** + * @def CHIP_CONFIG_TC_REQUIRED + * + * @brief Indicates whether terms and conditions are required during commissioning. + * + * This macro defines whether the device commissioning process requires the user to acknowledge terms and conditions. + * - 1: Terms and conditions are required. + * - 0: Terms and conditions are not required. + * + * If this is set to 1, both CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS and + * CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION must be defined. + */ +#ifndef CHIP_CONFIG_TC_REQUIRED +#define CHIP_CONFIG_TC_REQUIRED (0) +#endif + +#if CHIP_CONFIG_TC_REQUIRED + +/** + * @def CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS + * + * @brief Configures the required terms and conditions acknowledgements bitmask. + * + * This macro defines the required terms and conditions acknowledgements bitmask. The bit-field is 16 bits long, so the possible + * value range is [0, 65535). This setting can be used to require that terms and conditions are presented to the user during + * commissioning. + */ +#ifndef CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS +#error "CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS must be defined when CHIP_CONFIG_TC_REQUIRED is enabled." +#endif + +/** + * @def CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION + * + * @brief Configures the latest known version of the terms and conditions. + * + * This macro defines the version number of the latest terms and conditions. It allows the application to iterate on revisions of + * the terms and conditions. A value of 0 indicates that no specific version is required. This setting can be used to enforce + * version-specific terms and conditions acknowledgements in the application. When the set of terms and conditions needs to be + * changed, the version number should be monotonically increased. If the latest terms and conditions version is updated (most + * likely during an OTA), then this may signal to the Administrator that updated terms and conditions should be presented to the + * user. + */ +#ifndef CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION +#error "CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION must be defined when CHIP_CONFIG_TC_REQUIRED is enabled." +#endif + +#endif // CHIP_CONFIG_TC_REQUIRED