Skip to content

Commit

Permalink
fixs
Browse files Browse the repository at this point in the history
  • Loading branch information
swan-amazon committed Aug 27, 2024
1 parent f87984b commit 9ad9560
Show file tree
Hide file tree
Showing 16 changed files with 547 additions and 470 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,5 @@ examples/*/esp32/dependencies.lock
# jupyter temporary files
.ipynb_checkpoints

separate/
src/python_testing/matter_testing_infrastructure/build/
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,93 +169,58 @@ 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))
// No validation checks required if no required terms and conditions
if (!requiredTermsAndConditions.HasValue())
{
ChipLogError(AppServer, "Failed to HasReceivedTermsAndConditionscknowledgements");
errorCode = CommissioningErrorEnum::kTCAcknowledgementsNotReceived;
return err;
return CommissioningErrorEnum::kOk;
}

err = enhancedSetupFlowProvider->GetTermsAndConditionsAcceptedAcknowledgements(termsAndConditionsAcceptedAcknowledgements);
if (!::chip::ChipError::IsSuccess(err))
// Validate if we have received any terms and conditions acceptance
if (!acceptedTermsAndConditions.HasValue())
{
ChipLogError(AppServer, "Failed to GetTermsAndConditionsAcceptedAcknowledgements");
return err;
ChipLogError(AppServer, "Failed to HasReceivedTermsAndConditionsAcknowledgements");
return CommissioningErrorEnum::kTCAcknowledgementsNotReceived;
}

err = enhancedSetupFlowProvider->HasTermsAndConditionsRequiredAcknowledgementsBeenAccepted(hasRequiredTermAccepted);
if (!::chip::ChipError::IsSuccess(err))
// Validate the accepted version first...
if (requiredTermsAndConditions.Value().version > acceptedTermsAndConditions.Value().version)
{
ChipLogError(AppServer, "Failed to HasTermsAndConditionsRequiredAcknowledgementsBeenAccepted");
return err;
ChipLogProgress(AppServer, "Minimum terms and conditions version, 0x%04x, has not been accepted",
requiredTermsAndConditions.Value().version);
return CommissioningErrorEnum::kTCMinVersionNotMet;
}

err =
enhancedSetupFlowProvider->HasTermsAndConditionsRequiredAcknowledgementsVersionBeenAccepted(hasRequiredTermVersionAccepted);
if (!::chip::ChipError::IsSuccess(err))
// Validate the accepted bits second...
if (requiredTermsAndConditions.Value().value != (requiredTermsAndConditions.Value().value & acceptedTermsAndConditions.Value().value))
{
ChipLogError(AppServer, "Failed to HasTermsAndConditionsRequiredAcknowledgementsVersionBeenAccepted");
return err;
ChipLogProgress(AppServer, "Required terms and conditions, 0x%04x,have not been accepted", requiredTermsAndConditions.Value().value);
return CommissioningErrorEnum::kRequiredTCNotAccepted;
}

if (!hasRequiredTermVersionAccepted)
{
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;
}
// All validation check succeeded...
return CommissioningErrorEnum::kOk;
}

if (!hasRequiredTermAccepted)
{
uint16_t requiredAcknowledgements = 0;
(void) enhancedSetupFlowProvider->GetTermsAndConditionsRequiredAcknowledgements(requiredAcknowledgements);
#if 0
CHIP_ERROR CheckTermsAndConditionsAcknowledgementsStateOnCommissioningComplete(CommissioningErrorEnum & errorCode)
{
EnhancedSetupFlowProvider * const enhancedSetupFlowProvider = Server::GetInstance().GetEnhancedSetupFlowProvider();

ChipLogProgress(AppServer, "Required terms and conditions, 0x%04x,have not been accepted", requiredAcknowledgements);
errorCode = (0 == termsAndConditionsAcceptedAcknowledgements) ? CommissioningErrorEnum::kTCAcknowledgementsNotReceived
: CommissioningErrorEnum::kRequiredTCNotAccepted;
return CHIP_NO_ERROR;
}
Optional<TermsAndConditions> acceptedTermsAndConditions;
Optional<TermsAndConditions> requiredTermsAndConditions;

enhancedSetupFlowProvider->GetTermsAndConditionsAcknowledgements(acceptedTermsAndConditions);
enhancedSetupFlowProvider->GetTermsAndConditionsRequirements(requiredTermsAndConditions);

errorCode = CheckTermsAndConditionsAcknowledgementsState(requiredTermsAndConditions, acceptedTermsAndConditions);
return (CommissioningErrorEnum::kOk == errorCode) ? CHIP_NO_ERROR : CHIP_ERROR_INCORRECT_STATE;

errorCode = CommissioningErrorEnum::kOk;
return CHIP_NO_ERROR;
}
#endif
#endif

} // anonymous namespace

Expand Down Expand Up @@ -356,7 +318,29 @@ 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);

ChipLogError(AppServer, "acceptedTermsAndConditions HasValue= %d", acceptedTermsAndConditions.HasValue());
if (acceptedTermsAndConditions.HasValue())
{
ChipLogError(AppServer, "acceptedTermsAndConditions Value= %d", acceptedTermsAndConditions.Value().value);
ChipLogError(AppServer, "acceptedTermsAndConditions Version= %d", acceptedTermsAndConditions.Value().version);
}

response.errorCode = CheckTermsAndConditionsAcknowledgementsState(requiredTermsAndConditions, acceptedTermsAndConditions);
if (CommissioningErrorEnum::kOk != response.errorCode)
{
commandObj->AddResponse(commandPath, response);
return true;
}
#endif

if (failSafe.NocCommandHasBeenInvoked())
Expand All @@ -375,6 +359,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 +448,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 +500,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 9ad9560

Please sign in to comment.