Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
swan-amazon committed Aug 27, 2024
1 parent 10089de commit 56391a0
Show file tree
Hide file tree
Showing 13 changed files with 249 additions and 399 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename Provider, typename T>
CHIP_ERROR ReadFromProvider(Provider * const aProvider, CHIP_ERROR (Provider::*const aConstGetter)(T &) const,
AttributeValueEncoder & aEncoder);
};

GeneralCommissioningAttrAccess gAttrAccess;
Expand Down Expand Up @@ -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<TermsAndConditions> 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<TermsAndConditions> 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<TermsAndConditions> 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<TermsAndConditions> outTermsAndConditions;
Server::GetInstance().GetEnhancedSetupFlowProvider()->GetTermsAndConditionsRequirements(outTermsAndConditions);
return aEncoder.Encode(outTermsAndConditions.HasValue());
}
#endif
default: {
Expand Down Expand Up @@ -172,91 +169,42 @@ CHIP_ERROR GeneralCommissioningAttrAccess::ReadSupportsConcurrentConnection(Attr
return aEncoder.Encode(supportsConcurrentConnection);
}

template <typename Provider, typename T>
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<TermsAndConditions> & requiredTermsAndConditions,
const Optional<TermsAndConditions> & 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

Expand Down Expand Up @@ -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<TermsAndConditions> acceptedTermsAndConditions;
Optional<TermsAndConditions> 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())
Expand All @@ -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.
Expand Down Expand Up @@ -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<TermsAndConditions> requiredTermsAndConditions;

enhancedSetupFlowProvider->GetTermsAndConditionsRequirements(requiredTermsAndConditions);

Optional<TermsAndConditions> acceptedTermsAndConditions = Optional<TermsAndConditions>({
.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;

Expand All @@ -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
}
}
Expand Down
Loading

0 comments on commit 56391a0

Please sign in to comment.