Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
swan-amazon committed Dec 11, 2024
1 parent 6118f53 commit 5581214
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 218 deletions.

Large diffs are not rendered by default.

89 changes: 28 additions & 61 deletions src/app/server/DefaultTermsAndConditionsProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,54 +149,6 @@ CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::Init(
return CHIP_NO_ERROR;
}

CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::CheckAcceptance(const Optional<TermsAndConditions> & inTermsAndConditions,
TermsAndConditionsState & outState) const
{
VerifyOrReturnValue(nullptr != mTermsAndConditionsStorageDelegate, CHIP_ERROR_INVALID_ARGUMENT);

// No validation checks required if no required terms and conditions
if (!mRequiredAcknowledgements.HasValue())
{
ChipLogProgress(AppServer, "No terms and conditions required");
outState = TermsAndConditionsState::OK;
return CHIP_NO_ERROR;
}

// Validate if we have received any terms and conditions acceptance
if (!inTermsAndConditions.HasValue())
{
ChipLogError(AppServer, "No terms and conditions have been accepted");
outState = TermsAndConditionsState::TC_ACKNOWLEDGEMENTS_NOT_RECEIVED;
return CHIP_NO_ERROR;
}

const TermsAndConditions requiredTermsAndConditions = mRequiredAcknowledgements.Value();
const TermsAndConditions termsAndConditionsToCheck = inTermsAndConditions.Value();

// Validate the accepted version first...
if (requiredTermsAndConditions.GetVersion() > termsAndConditionsToCheck.GetVersion())
{
ChipLogError(AppServer, "Minimum terms and conditions version, 0x%04x, has not been accepted",
requiredTermsAndConditions.GetVersion());
outState = TermsAndConditionsState::TC_MIN_VERSION_NOT_MET;
return CHIP_NO_ERROR;
}

// Validate the accepted bits second...
if (requiredTermsAndConditions.GetValue() != (requiredTermsAndConditions.GetValue() & termsAndConditionsToCheck.GetValue()))
{
ChipLogError(AppServer, "Required terms and conditions, 0x%04x, have not been accepted",
requiredTermsAndConditions.GetValue());
outState = TermsAndConditionsState::REQUIRED_TC_NOT_ACCEPTED;
return CHIP_NO_ERROR;
}

// All validation check succeeded...
ChipLogProgress(AppServer, "Required terms and conditions, 0x%04x, have been accepted", requiredTermsAndConditions.GetValue());
outState = TermsAndConditionsState::OK;
return CHIP_NO_ERROR;
}

CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::CommitAcceptance()
{
VerifyOrReturnValue(nullptr != mTermsAndConditionsStorageDelegate, CHIP_ERROR_UNINITIALIZED);
Expand Down Expand Up @@ -236,23 +188,43 @@ CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::GetAcceptance(Optional<
return CHIP_NO_ERROR;
}

CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::GetRequirements(Optional<TermsAndConditions> & outTermsAndConditions) const
CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::GetAcknowledgementsRequired(bool & outAcknowledgementsRequired) const
{
VerifyOrReturnValue(nullptr != mTermsAndConditionsStorageDelegate, CHIP_ERROR_UNINITIALIZED);
Optional<TermsAndConditions> requiredTermsAndConditionsMaybe;
ReturnErrorOnFailure(GetRequirements(requiredTermsAndConditionsMaybe));

outTermsAndConditions = mRequiredAcknowledgements;
if (!requiredTermsAndConditionsMaybe.HasValue())
{
outAcknowledgementsRequired = false;
return CHIP_NO_ERROR;
}

Optional<TermsAndConditions> acceptedTermsAndConditionsMaybe;
ReturnErrorOnFailure(GetAcceptance(acceptedTermsAndConditionsMaybe));

if (!acceptedTermsAndConditionsMaybe.HasValue())
{
outAcknowledgementsRequired = true;
return CHIP_NO_ERROR;
}

TermsAndConditions requiredTermsAndConditions = requiredTermsAndConditionsMaybe.Value();
TermsAndConditions acceptedTermsAndConditions = acceptedTermsAndConditionsMaybe.Value();
outAcknowledgementsRequired = requiredTermsAndConditions.Validate(acceptedTermsAndConditions);
return CHIP_NO_ERROR;
}

CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::GetRequirements(Optional<TermsAndConditions> & outTermsAndConditions) const
{
outTermsAndConditions = mRequiredAcknowledgements;
return CHIP_NO_ERROR;
}

CHIP_ERROR
chip::app::DefaultTermsAndConditionsProvider::GetUpdateAcceptanceDeadline(Optional<uint32_t> & outUpdateAcceptanceDeadline) const
{
VerifyOrReturnValue(nullptr != mTermsAndConditionsStorageDelegate, CHIP_ERROR_UNINITIALIZED);

// No-op stub implementation. This feature is not implemented in this default implementation.
outUpdateAcceptanceDeadline = Optional<uint32_t>();

return CHIP_NO_ERROR;
}

Expand All @@ -261,23 +233,18 @@ CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::ResetAcceptance()
VerifyOrReturnValue(nullptr != mTermsAndConditionsStorageDelegate, CHIP_ERROR_UNINITIALIZED);

(void) mTermsAndConditionsStorageDelegate->Delete();
return RevertAcceptance();
ReturnErrorOnFailure(RevertAcceptance());
return CHIP_NO_ERROR;
}

CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::RevertAcceptance()
{
VerifyOrReturnValue(nullptr != mTermsAndConditionsStorageDelegate, CHIP_ERROR_UNINITIALIZED);

mTemporalAcceptance.ClearValue();

return CHIP_NO_ERROR;
}

CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::SetAcceptance(const Optional<TermsAndConditions> & inTermsAndConditions)
{
VerifyOrReturnValue(nullptr != mTermsAndConditionsStorageDelegate, CHIP_ERROR_UNINITIALIZED);

mTemporalAcceptance = inTermsAndConditions;

return CHIP_NO_ERROR;
}
5 changes: 2 additions & 3 deletions src/app/server/DefaultTermsAndConditionsProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,12 @@ class DefaultTermsAndConditionsProvider : public TermsAndConditionsProvider
CHIP_ERROR Init(TermsAndConditionsStorageDelegate * const inStorageDelegate,
const Optional<TermsAndConditions> & inRequiredTermsAndConditions);

CHIP_ERROR CheckAcceptance(const Optional<TermsAndConditions> & inTermsAndConditions,
TermsAndConditionsState & outState) const override;

CHIP_ERROR CommitAcceptance() override;

CHIP_ERROR GetAcceptance(Optional<TermsAndConditions> & outTermsAndConditions) const override;

CHIP_ERROR GetAcknowledgementsRequired(bool & outAcknowledgementsRequired) const override;

CHIP_ERROR GetRequirements(Optional<TermsAndConditions> & outTermsAndConditions) const override;

CHIP_ERROR GetUpdateAcceptanceDeadline(Optional<uint32_t> & outUpdateAcceptanceDeadline) const override;
Expand Down
11 changes: 5 additions & 6 deletions src/app/server/TermsAndConditionsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ CHIP_ERROR chip::app::TermsAndConditionsManager::Init(chip::PersistentStorageDel
return CHIP_NO_ERROR;
}

CHIP_ERROR chip::app::TermsAndConditionsManager::CheckAcceptance(const Optional<TermsAndConditions> & inTermsAndConditions,
TermsAndConditionsState & outState) const
{
return sTermsAndConditionsProviderInstance.CheckAcceptance(inTermsAndConditions, outState);
}

CHIP_ERROR chip::app::TermsAndConditionsManager::CommitAcceptance()
{
return sTermsAndConditionsProviderInstance.CommitAcceptance();
Expand All @@ -55,6 +49,11 @@ CHIP_ERROR chip::app::TermsAndConditionsManager::GetAcceptance(Optional<TermsAnd
return sTermsAndConditionsProviderInstance.GetAcceptance(outTermsAndConditions);
}

CHIP_ERROR chip::app::TermsAndConditionsManager::GetAcknowledgementsRequired(bool & outAcknowledgementsRequired) const
{
return sTermsAndConditionsProviderInstance.GetAcknowledgementsRequired(outAcknowledgementsRequired);
}

CHIP_ERROR chip::app::TermsAndConditionsManager::GetRequirements(Optional<TermsAndConditions> & outTermsAndConditions) const
{
return sTermsAndConditionsProviderInstance.GetRequirements(outTermsAndConditions);
Expand Down
2 changes: 1 addition & 1 deletion src/app/server/TermsAndConditionsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class TermsAndConditionsManager : public TermsAndConditionsProvider
static TermsAndConditionsManager * GetInstance();
CHIP_ERROR Init(PersistentStorageDelegate * const inPersistentStorageDelegate,
const Optional<TermsAndConditions> & inRequiredTermsAndConditions);
CHIP_ERROR CheckAcceptance(const Optional<TermsAndConditions> & inTermsAndConditions, TermsAndConditionsState & outState) const;
CHIP_ERROR CommitAcceptance();
CHIP_ERROR GetAcceptance(Optional<TermsAndConditions> & outTermsAndConditions) const;
CHIP_ERROR GetAcknowledgementsRequired(bool & outAcknowledgementsRequired) const;
CHIP_ERROR GetRequirements(Optional<TermsAndConditions> & outTermsAndConditions) const;
CHIP_ERROR GetUpdateAcceptanceDeadline(Optional<uint32_t> & outUpdateAcceptanceDeadline) const;
CHIP_ERROR ResetAcceptance();
Expand Down
97 changes: 70 additions & 27 deletions src/app/server/TermsAndConditionsProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,75 @@
namespace chip {
namespace app {

typedef enum eTermsAndConditionsState
{
OK = 0,
TC_ACKNOWLEDGEMENTS_NOT_RECEIVED = 1,
TC_MIN_VERSION_NOT_MET = 2,
REQUIRED_TC_NOT_ACCEPTED = 3,
} TermsAndConditionsState;

/**
* @brief Represents a pair of terms and conditions value and version.
*
* This class encapsulates terms and conditions with methods to validate a user's accepted value and version against required
* criteria.
*/
class TermsAndConditions
{
public:
TermsAndConditions(uint16_t inValue, uint16_t inVersion) : value(inValue), version(inValue) {}
TermsAndConditions(uint16_t inValue, uint16_t inVersion) : value(inValue), version(inVersion) {}

bool operator==(const TermsAndConditions & other) const { return value == other.value && version == other.version; }
bool operator!=(const TermsAndConditions & other) const { return !(*this == other); }

/**
* @brief Retrieves the terms and conditions value (accepted bits).
*
* @return The value of the terms and conditions.
*/
uint16_t GetValue() const { return value; }

/**
* @brief Retrieves the terms and conditions version.
*
* @return The version of the terms and conditions.
*/
uint16_t GetVersion() const { return version; }

/**
* @brief Validates the terms and conditions value.
*
* Checks whether all required bits are set in the accepted terms and conditions.
*
* @param acceptedTermsAndConditions The user's accepted terms and conditions.
* @return True if all required bits are set, false otherwise.
*/
bool ValidateValue(const TermsAndConditions & acceptedTermsAndConditions) const
{
// Check if all required bits are set in the user-accepted value.
return ((value & acceptedTermsAndConditions.GetValue()) == value);
}

/**
* @brief Validates the terms and conditions version.
*
* Checks whether the accepted version is greater than or equal to the required version.
*
* @param acceptedTermsAndConditions The user's accepted terms and conditions.
* @return True if the accepted version is valid, false otherwise.
*/
bool ValidateVersion(const TermsAndConditions & acceptedTermsAndConditions) const
{
// Check if the version is below the minimum required version.
return (acceptedTermsAndConditions.GetVersion() >= version);
}

/**
* @brief Validates the terms and conditions.
*
* Combines validation of both value and version to ensure compliance with requirements.
*
* @param acceptedTermsAndConditions The user's accepted terms and conditions.
* @return True if both value and version validations pass, false otherwise.
*/
bool Validate(const TermsAndConditions & acceptedTermsAndConditions) const
{
return ValidateVersion(acceptedTermsAndConditions) && ValidateValue(acceptedTermsAndConditions);
}

private:
const uint16_t value;
const uint16_t version;
Expand All @@ -62,24 +112,6 @@ class TermsAndConditionsProvider
public:
virtual ~TermsAndConditionsProvider() = default;

/**
* @brief Verifies the acceptance of the terms and conditions.
*
* This function checks whether the provided terms and conditions meet the required criteria without
* affecting any persistent or temporary acceptance states. The function validates both the version
* and content of the terms and conditions against the required values.
*
* @param[in] inTermsAndConditions The terms and conditions to verify.
* @param[out] outState The result of the validation: whether the terms are accepted or
* why they are invalid (e.g., version mismatch or required terms not accepted).
*
* @retval CHIP_NO_ERROR if the terms are successfully validated.
* @retval CHIP_ERROR_UNINITIALIZED if the module has not been initialized.
* @retval CHIP_ERROR_* for other errors.
*/
virtual CHIP_ERROR CheckAcceptance(const Optional<TermsAndConditions> & inTermsAndConditions,
TermsAndConditionsState & outState) const = 0;

/**
* @brief Persists the acceptance of the terms and conditions.
*
Expand Down Expand Up @@ -107,6 +139,17 @@ class TermsAndConditionsProvider
*/
virtual CHIP_ERROR GetAcceptance(Optional<TermsAndConditions> & outTermsAndConditions) const = 0;

/**
* @brief Determines if acknowledgments are required.
*
* @param[out] outAcknowledgementsRequired True if acknowledgments are required, false otherwise.
*
* @retval CHIP_NO_ERROR if successful.
* @retval CHIP_ERROR_UNINITIALIZED if the module has not been initialized.
* @retval CHIP_ERROR_* for other errors.
*/
virtual CHIP_ERROR GetAcknowledgementsRequired(bool & outAcknowledgementsRequired) const = 0;

/**
* @brief Retrieves the requirements for the terms and conditions.
*
Expand Down

0 comments on commit 5581214

Please sign in to comment.