Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
swan-amazon committed Dec 11, 2024
1 parent 9f30ca7 commit d37ea0a
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,12 @@ CHIP_ERROR GeneralCommissioningAttrAccess::Read(const ConcreteReadAttributePath
}
case TCAcknowledgementsRequired::Id: {
TermsAndConditionsProvider * const tcProvider = TermsAndConditionsManager::GetInstance();
Optional<TermsAndConditions> outTermsAndConditions;
TermsAndConditionsState tcState;
bool acknowledgementsRequired;

VerifyOrReturnError(nullptr != tcProvider, CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE);
ReturnErrorOnFailure(tcProvider->GetAcknowledgementsRequired(acknowledgementsRequired));

ReturnErrorOnFailure(tcProvider->GetAcceptance(outTermsAndConditions));
ReturnErrorOnFailure(tcProvider->CheckAcceptance(outTermsAndConditions, tcState));

bool setTCCallRequiredBeforeCommissioningCompleteSuccess = tcState != TermsAndConditionsState::OK;
return aEncoder.Encode(setTCCallRequiredBeforeCommissioningCompleteSuccess);
return aEncoder.Encode(acknowledgementsRequired);
}
case TCUpdateDeadline::Id: {
TermsAndConditionsProvider * const tcProvider = TermsAndConditionsManager::GetInstance();
Expand Down Expand Up @@ -209,66 +205,6 @@ CHIP_ERROR GeneralCommissioningAttrAccess::ReadSupportsConcurrentConnection(Attr
}

#if CHIP_CONFIG_TC_REQUIRED
CommissioningErrorEnum CheckTermsAndConditionsAcknowledgementsState(TermsAndConditionsProvider * const tcProvider,
const Optional<TermsAndConditions> & tcUserInput)
{
TermsAndConditionsState tcState;

VerifyOrReturnValue(CHIP_NO_ERROR == tcProvider->CheckAcceptance(tcUserInput, tcState),
CommissioningErrorEnum::kRequiredTCNotAccepted);

switch (tcState)
{
case TermsAndConditionsState::OK:
return CommissioningErrorEnum::kOk;
case TermsAndConditionsState::TC_ACKNOWLEDGEMENTS_NOT_RECEIVED:
return CommissioningErrorEnum::kTCAcknowledgementsNotReceived;
case TermsAndConditionsState::TC_MIN_VERSION_NOT_MET:
return CommissioningErrorEnum::kTCMinVersionNotMet;
case TermsAndConditionsState::REQUIRED_TC_NOT_ACCEPTED:
return CommissioningErrorEnum::kRequiredTCNotAccepted;
}

return CommissioningErrorEnum::kOk;
}

CommissioningErrorEnum CheckTermsAndConditionsAcknowledgements(TermsAndConditionsProvider * const tcProvider)
{
Optional<TermsAndConditions> tcUserInput;

VerifyOrReturnValue(CHIP_NO_ERROR == tcProvider->GetAcceptance(tcUserInput),
CommissioningErrorEnum::kTCAcknowledgementsNotReceived);

return CheckTermsAndConditionsAcknowledgementsState(tcProvider, tcUserInput);
}

void NotifyTermsAndConditionsAttributesChange(TermsAndConditionsProvider * const tcProvider, chip::EndpointId endpoint,
Optional<TermsAndConditions> currentTCAcceptance,
Optional<TermsAndConditions> updatedTCAccpetance)
{
if (currentTCAcceptance != updatedTCAccpetance)
{
if (currentTCAcceptance.ValueOr(TermsAndConditions(0, 0)).GetVersion() !=
updatedTCAccpetance.ValueOr(TermsAndConditions(0, 0)).GetVersion())
{
MatterReportingAttributeChangeCallback(endpoint, GeneralCommissioning::Id, Attributes::TCAcceptedVersion::Id);
}

if (currentTCAcceptance.ValueOr(TermsAndConditions(0, 0)).GetValue() !=
updatedTCAccpetance.ValueOr(TermsAndConditions(0, 0)).GetValue())
{
MatterReportingAttributeChangeCallback(endpoint, GeneralCommissioning::Id, Attributes::TCAcknowledgements::Id);
}

CommissioningErrorEnum previousState = CheckTermsAndConditionsAcknowledgementsState(tcProvider, currentTCAcceptance);
CommissioningErrorEnum updatedState = CheckTermsAndConditionsAcknowledgementsState(tcProvider, updatedTCAccpetance);

if (previousState != updatedState)
{
MatterReportingAttributeChangeCallback(endpoint, GeneralCommissioning::Id, Attributes::TCAcknowledgementsRequired::Id);
}
}
}
#endif // CHIP_CONFIG_TC_REQUIRED

} // anonymous namespace
Expand Down Expand Up @@ -365,14 +301,39 @@ bool emberAfGeneralCommissioningClusterCommissioningCompleteCallback(
// Ensure required terms and conditions have been accepted, then attempt to commit
if (nullptr != tcProvider)
{
response.errorCode = CheckTermsAndConditionsAcknowledgements(tcProvider);
if (CommissioningErrorEnum::kOk != response.errorCode)
Optional<TermsAndConditions> requiredTermsAndConditionsMaybe;
Optional<TermsAndConditions> acceptedTermsAndConditionsMaybe;

CheckSuccess(tcProvider->GetRequirements(requiredTermsAndConditionsMaybe), Failure);
CheckSuccess(tcProvider->GetAcceptance(acceptedTermsAndConditionsMaybe), Failure);

if (requiredTermsAndConditionsMaybe.HasValue() && !acceptedTermsAndConditionsMaybe.HasValue())
{
ChipLogError(FailSafe, "GeneralCommissioning: Terms and conditions not accepted");
response.errorCode = CommissioningErrorEnum::kTCAcknowledgementsNotReceived;
commandObj->AddResponse(commandPath, response);
return true;
}

if (requiredTermsAndConditionsMaybe.HasValue() && acceptedTermsAndConditionsMaybe.HasValue())
{
TermsAndConditions requiredTermsAndConditions = requiredTermsAndConditionsMaybe.Value();
TermsAndConditions acceptedTermsAndConditions = acceptedTermsAndConditionsMaybe.Value();

if (!requiredTermsAndConditions.ValidateVersion(acceptedTermsAndConditions))
{
response.errorCode = CommissioningErrorEnum::kTCMinVersionNotMet;
commandObj->AddResponse(commandPath, response);
return true;
}

if (!requiredTermsAndConditions.ValidateValue(acceptedTermsAndConditions))
{
response.errorCode = CommissioningErrorEnum::kRequiredTCNotAccepted;
commandObj->AddResponse(commandPath, response);
return true;
}
}

if (failSafe.UpdateTermsAndConditionsHasBeenInvoked())
{
// Commit terms and conditions acceptance on commissioning complete
Expand Down Expand Up @@ -489,40 +450,65 @@ bool emberAfGeneralCommissioningClusterSetTCAcknowledgementsCallback(
chip::app::CommandHandler * commandObj, const chip::app::ConcreteCommandPath & commandPath,
const GeneralCommissioning::Commands::SetTCAcknowledgements::DecodableType & commandData)
{
#if CHIP_CONFIG_TC_REQUIRED
MATTER_TRACE_SCOPE("SetTCAcknowledgements", "GeneralCommissioning");

#if CHIP_CONFIG_TC_REQUIRED
auto & failSafeContext = Server::GetInstance().GetFailSafeContext();
TermsAndConditionsProvider * const tcProvider = TermsAndConditionsManager::GetInstance();
VerifyOrReturnValue(nullptr != tcProvider, false);

Commands::SetTCAcknowledgementsResponse::Type response = {
.errorCode = CommissioningErrorEnum::kOk,
};
if (nullptr == tcProvider)
{
commandObj->AddStatus(commandPath, Protocols::InteractionModel::Status::Failure);
return true;
}

Optional<TermsAndConditions> tcUserInput =
Optional<TermsAndConditions>(TermsAndConditions(commandData.TCUserResponse, commandData.TCVersion));
Optional<TermsAndConditions> requiredTermsAndConditionsMaybe;
Optional<TermsAndConditions> previousAcceptedTermsAndConditionsMaybe;
CheckSuccess(tcProvider->GetRequirements(requiredTermsAndConditionsMaybe), Failure);
CheckSuccess(tcProvider->GetAcceptance(previousAcceptedTermsAndConditionsMaybe), Failure);
TermsAndConditions acceptedTermsAndConditions = TermsAndConditions(commandData.TCUserResponse, commandData.TCVersion);
Optional<TermsAndConditions> acceptedTermsAndConditionsPresent = Optional<TermsAndConditions>(acceptedTermsAndConditions);

Optional<TermsAndConditions> currentTCAcceptance;
(void) tcProvider->GetAcceptance(currentTCAcceptance);
Commands::SetTCAcknowledgementsResponse::Type response;

if (currentTCAcceptance != tcUserInput)
if (requiredTermsAndConditionsMaybe.HasValue())
{
CheckSuccess(tcProvider->SetAcceptance(tcUserInput), Failure);
TermsAndConditions requiredTermsAndConditions = requiredTermsAndConditionsMaybe.Value();

NotifyTermsAndConditionsAttributesChange(tcProvider, commandPath.mEndpointId, currentTCAcceptance, tcUserInput);
if (!requiredTermsAndConditions.ValidateVersion(acceptedTermsAndConditions))
{
response.errorCode = CommissioningErrorEnum::kTCMinVersionNotMet;
commandObj->AddResponse(commandPath, response);
return true;
}

if (failSafeContext.IsFailSafeArmed())
if (!requiredTermsAndConditions.ValidateValue(acceptedTermsAndConditions))
{
failSafeContext.SetUpdateTermsAndConditionsHasBeenInvoked();
response.errorCode = CommissioningErrorEnum::kRequiredTCNotAccepted;
commandObj->AddResponse(commandPath, response);
return true;
}
else
}

if (previousAcceptedTermsAndConditionsMaybe != acceptedTermsAndConditionsPresent)
{
CheckSuccess(tcProvider->SetAcceptance(acceptedTermsAndConditionsPresent), Failure);

// Commit or defer based on fail-safe state
if (!failSafeContext.IsFailSafeArmed())
{
CheckSuccess(tcProvider->CommitAcceptance(), Failure);
}
else
{
failSafeContext.SetUpdateTermsAndConditionsHasBeenInvoked();
}
}

response.errorCode = CommissioningErrorEnum::kOk;
commandObj->AddResponse(commandPath, response);
return true;

#endif // CHIP_CONFIG_TC_REQUIRED
return true;
}
Expand All @@ -538,11 +524,10 @@ void OnPlatformEventHandler(const DeviceLayer::ChipDeviceEvent * event, intptr_t
if (event->FailSafeTimerExpired.updateTermsAndConditionsHasBeenInvoked)
{
#if CHIP_CONFIG_TC_REQUIRED
// Clear terms and conditions acceptance on failsafe timer expiration
TermsAndConditionsProvider * const tcProvider = TermsAndConditionsManager::GetInstance();
VerifyOrReturn(nullptr != tcProvider);

// Clear terms and conditions acceptance on failsafe timer expiration
tcProvider->RevertAcceptance();
VerifyOrReturn(CHIP_NO_ERROR == tcProvider->RevertAcceptance());
#endif // CHIP_CONFIG_TC_REQUIRED
}
}
Expand Down
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
Loading

0 comments on commit d37ea0a

Please sign in to comment.