Skip to content

Commit

Permalink
fixup! Implemented Enhanced setup flow provider
Browse files Browse the repository at this point in the history
  • Loading branch information
swan-amazon committed May 21, 2024
1 parent e8884d2 commit c22c7df
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 85 deletions.
6 changes: 5 additions & 1 deletion examples/chip-tool/commands/pairing/PairingCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,13 @@ CommissioningParameters PairingCommand::GetCommissioningParameters()
params.SetCountryCode(CharSpan::fromCharString(mCountryCode.Value()));
}

if (mTCAcknowledgements.HasValue() && mTCAcknowledgementVersion.HasValue())
if (mTCAcknowledgements.HasValue())
{
params.SetTCAcknowledgements(mTCAcknowledgements.Value());
}

if (mTCAcknowledgementVersion.HasValue())
{
params.SetTCAcknowledgementVersion(mTCAcknowledgementVersion.Value());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,25 @@ CHIP_ERROR GeneralCommissioningAttrAccess::Read(const ConcreteReadAttributePath
}
case TCAcceptedVersion::Id: {
uint16_t tcAcceptedVersion;
CHIP_ERROR err = EnhancedSetupFlowProvider::GetInstance().GetTCAcceptedVersion(tcAcceptedVersion);
CHIP_ERROR err =
EnhancedSetupFlowProvider::GetInstance().GetTermsAndConditionsAcceptedAcknowledgementsVersion(tcAcceptedVersion);
return (CHIP_NO_ERROR != err) ? err : aEncoder.Encode(tcAcceptedVersion);
}
case TCMinRequiredVersion::Id: {
uint16_t tcMinRequiredVersion;
CHIP_ERROR err = EnhancedSetupFlowProvider::GetInstance().GetTCMinRequiredVersion(tcMinRequiredVersion);
return (CHIP_NO_ERROR != err) ? err : aEncoder.Encode(tcMinRequiredVersion);
uint16_t tcRequiredVersion;
CHIP_ERROR err =
EnhancedSetupFlowProvider::GetInstance().GetTermsAndConditionsRequiredAcknowledgementsVersion(tcRequiredVersion);
return (CHIP_NO_ERROR != err) ? err : aEncoder.Encode(tcRequiredVersion);
}
case TCAcknowledgements::Id: {
uint16_t tcAcknowledgements;
CHIP_ERROR err = EnhancedSetupFlowProvider::GetInstance().GetTCAcknowledgements(tcAcknowledgements);
CHIP_ERROR err = EnhancedSetupFlowProvider::GetInstance().GetTermsAndConditionsAcceptedAcknowledgements(tcAcknowledgements);
return (CHIP_NO_ERROR != err) ? err : aEncoder.Encode(tcAcknowledgements);
}
case TCAcknowledgementsRequired::Id: {
uint16_t tcAcknowledgementsRequired;
CHIP_ERROR err = EnhancedSetupFlowProvider::GetInstance().GetTCAcknowledgementsRequired(tcAcknowledgementsRequired);
CHIP_ERROR err =
EnhancedSetupFlowProvider::GetInstance().GetTermsAndConditionsRequiredAcknowledgements(tcAcknowledgementsRequired);
return (CHIP_NO_ERROR != err) ? err : aEncoder.Encode(tcAcknowledgementsRequired);
}
default:
Expand Down Expand Up @@ -261,14 +264,14 @@ bool emberAfGeneralCommissioningClusterCommissioningCompleteCallback(
{
CHIP_ERROR err;

if (!EnhancedSetupFlowProvider::GetInstance().HasRequiredTermsAndConditionsBeenAcknowledged())
if (!EnhancedSetupFlowProvider::GetInstance().HasTermsAndConditionsRequiredAcknowledgementsBeenAccepted())
{
ChipLogError(AppServer, "Required terms and conditions have not been accepted");
Breadcrumb::Set(commandPath.mEndpointId, 0);
response.errorCode = CommissioningErrorEnum::kRequiredTCNotAccepted;
}

else if (!EnhancedSetupFlowProvider::GetInstance().HasRequiredTermsAndConditionsMinVersionBeenAcknowledged())
else if (!EnhancedSetupFlowProvider::GetInstance().HasTermsAndConditionsRequiredAcknowledgementsVersionBeenAccepted())
{
ChipLogError(AppServer, "Minimium terms and conditions version has not been accepted");
Breadcrumb::Set(commandPath.mEndpointId, 0);
Expand Down Expand Up @@ -365,8 +368,9 @@ bool emberAfGeneralCommissioningClusterSetTCAcknowledgementsCallback(

Commands::SetTCAcknowledgementsResponse::Type response;

CheckSuccess(EnhancedSetupFlowProvider::GetInstance().SetTCAcknowledgements(commandData.TCVersion, commandData.TCUserResponse),
Failure);
CheckSuccess(
EnhancedSetupFlowProvider::GetInstance().SetTermsAndConditionsAcceptance(commandData.TCUserResponse, commandData.TCVersion),
Failure);
response.errorCode = CommissioningErrorEnum::kOk;

commandObj->AddResponse(commandPath, response);
Expand All @@ -376,19 +380,20 @@ bool emberAfGeneralCommissioningClusterSetTCAcknowledgementsCallback(
namespace {
void OnPlatformEventHandler(const DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
{
switch (event->Type) {
case DeviceLayer::DeviceEventType::kFailSafeTimerExpired: {
// Spec says to reset Breadcrumb attribute to 0.
Breadcrumb::Set(0, 0);
break;
}
case DeviceLayer::DeviceEventType::kServerReady: {
EnhancedSetupFlowProvider::GetInstance().Init();
break;
}
default: {
break;
}
switch (event->Type)
{
case DeviceLayer::DeviceEventType::kFailSafeTimerExpired: {
// Spec says to reset Breadcrumb attribute to 0.
Breadcrumb::Set(0, 0);
break;
}
case DeviceLayer::DeviceEventType::kServerReady: {
EnhancedSetupFlowProvider::GetInstance().Init();
break;
}
default: {
break;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/app/server/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ static_library("server") {
"OnboardingCodesUtil.h",
"Server.cpp",
"Server.h",
"TermsAndConditionsProvider.cpp",
"TermsAndConditionsProvider.h",
]

Expand Down
96 changes: 52 additions & 44 deletions src/app/server/EnhancedSetupFlowProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,77 +21,85 @@
#include <lib/core/CHIPConfig.h>
#include <platform/KeyValueStoreManager.h>

chip::app::EnhancedSetupFlowProvider chip::app::EnhancedSetupFlowProvider::sEnhancedSetupFlowProviderInstance(kRootEndpointId);

namespace {
constexpr char kTCUserAcceptedVersionKeyName[] = "tc-useracceptedversion";
constexpr char kTCUserAcceptedAcknowledgementsKeyName[] = "tc-useracceptedacknowledgements";
} // namespace
chip::app::EnhancedSetupFlowProvider chip::app::EnhancedSetupFlowProvider::sInstance(kRootEndpointId);

CHIP_ERROR chip::app::EnhancedSetupFlowProvider::Init()
{
uint16_t tcAcknowledgements = 0;
uint16_t tcAcceptedVersion = 0;

chip::DeviceLayer::PersistedStorage::KeyValueStoreManager & kvsManager =
chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr();

kvsManager.Get(kTCUserAcceptedVersionKeyName, &tcAcceptedVersion, sizeof(tcAcceptedVersion));
kvsManager.Get(kTCUserAcceptedAcknowledgementsKeyName, &tcAcknowledgements, sizeof(tcAcknowledgements));

mTCRequiredVersion = CHIP_CONFIG_TC_REQUIRED_VERSION;
mTCRequiredAcknowledgements = CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS;
mTCUserAcceptedVersion = tcAcceptedVersion;
mTCUserAcceptedAcknowledgements = tcAcknowledgements;

mTermsAndConditionsProvider.Init();
return CHIP_NO_ERROR;
}

bool chip::app::EnhancedSetupFlowProvider::HasRequiredTermsAndConditionsBeenAcknowledged()
bool chip::app::EnhancedSetupFlowProvider::HasTermsAndConditionsRequiredAcknowledgementsBeenAccepted()
{
return (mTCUserAcceptedAcknowledgements & mTCRequiredAcknowledgements) == mTCRequiredAcknowledgements;
}
uint16_t requiredAcknowledgements;
uint16_t requiredAcknowledgementsVersion;
uint16_t acceptedAcknowledgements;
uint16_t acceptedAcknowledgementsVersion;

bool chip::app::EnhancedSetupFlowProvider::HasRequiredTermsAndConditionsMinVersionBeenAcknowledged()
{
return (mTCUserAcceptedVersion >= mTCRequiredVersion);
mTermsAndConditionsProvider.GetRequirements(requiredAcknowledgements, requiredAcknowledgementsVersion);
mTermsAndConditionsProvider.GetAcceptance(acceptedAcknowledgements, acceptedAcknowledgementsVersion);

return (requiredAcknowledgements & acceptedAcknowledgements) == requiredAcknowledgements;
}

CHIP_ERROR chip::app::EnhancedSetupFlowProvider::GetTCAcceptedVersion(uint16_t & value)
bool chip::app::EnhancedSetupFlowProvider::HasTermsAndConditionsRequiredAcknowledgementsVersionBeenAccepted()
{
value = mTCUserAcceptedVersion;
return CHIP_NO_ERROR;
uint16_t requiredAcknowledgements;
uint16_t requiredAcknowledgementsVersion;
uint16_t acceptedAcknowledgements;
uint16_t acceptedAcknowledgementsVersion;

mTermsAndConditionsProvider.GetRequirements(requiredAcknowledgements, requiredAcknowledgementsVersion);
mTermsAndConditionsProvider.GetAcceptance(acceptedAcknowledgements, acceptedAcknowledgementsVersion);

return acceptedAcknowledgementsVersion >= requiredAcknowledgementsVersion;
}

CHIP_ERROR chip::app::EnhancedSetupFlowProvider::GetTCMinRequiredVersion(uint16_t & value)
CHIP_ERROR chip::app::EnhancedSetupFlowProvider::GetTermsAndConditionsRequiredAcknowledgements(uint16_t & value)
{
value = mTCRequiredVersion;
uint16_t requiredAcknowledgements;
uint16_t requiredAcknowledgementsVersion;

mTermsAndConditionsProvider.GetRequirements(requiredAcknowledgements, requiredAcknowledgementsVersion);

value = requiredAcknowledgements;
return CHIP_NO_ERROR;
}

CHIP_ERROR chip::app::EnhancedSetupFlowProvider::GetTCAcknowledgements(uint16_t & value)
CHIP_ERROR chip::app::EnhancedSetupFlowProvider::GetTermsAndConditionsRequiredAcknowledgementsVersion(uint16_t & value)
{
value = mTCUserAcceptedAcknowledgements;
uint16_t requiredAcknowledgements;
uint16_t requiredAcknowledgementsVersion;

mTermsAndConditionsProvider.GetRequirements(requiredAcknowledgements, requiredAcknowledgementsVersion);

value = requiredAcknowledgementsVersion;
return CHIP_NO_ERROR;
}

CHIP_ERROR chip::app::EnhancedSetupFlowProvider::GetTCAcknowledgementsRequired(uint16_t & value)
CHIP_ERROR chip::app::EnhancedSetupFlowProvider::GetTermsAndConditionsAcceptedAcknowledgements(uint16_t & value)
{
value = mTCUserAcceptedAcknowledgements;
uint16_t acceptedAcknowledgements;
uint16_t acceptedAcknowledgementsVersion;

mTermsAndConditionsProvider.GetAcceptance(acceptedAcknowledgements, acceptedAcknowledgementsVersion);

value = acceptedAcknowledgements;
return CHIP_NO_ERROR;
}

CHIP_ERROR chip::app::EnhancedSetupFlowProvider::SetTCAcknowledgements(uint16_t tcVersion, uint16_t tcUserResponse)
CHIP_ERROR chip::app::EnhancedSetupFlowProvider::GetTermsAndConditionsAcceptedAcknowledgementsVersion(uint16_t & value)
{
chip::DeviceLayer::PersistedStorage::KeyValueStoreManager & kvsManager =
chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr();

mTCUserAcceptedVersion = tcVersion;
mTCUserAcceptedAcknowledgements = tcUserResponse;
uint16_t acceptedAcknowledgements;
uint16_t acceptedAcknowledgementsVersion;

kvsManager.Put(kTCUserAcceptedVersionKeyName, &mTCUserAcceptedVersion, sizeof(mTCUserAcceptedVersion));
kvsManager.Put(kTCUserAcceptedAcknowledgementsKeyName, &mTCUserAcceptedAcknowledgements,
sizeof(mTCUserAcceptedAcknowledgements));
mTermsAndConditionsProvider.GetAcceptance(acceptedAcknowledgements, acceptedAcknowledgementsVersion);

value = acceptedAcknowledgementsVersion;
return CHIP_NO_ERROR;
}

CHIP_ERROR chip::app::EnhancedSetupFlowProvider::SetTermsAndConditionsAcceptance(uint16_t aTCAcknowledgements, uint16_t aTCAcknowledgementsVersion)
{
return mTermsAndConditionsProvider.SetAcceptance(aTCAcknowledgements, aTCAcknowledgementsVersion);
}
31 changes: 16 additions & 15 deletions src/app/server/EnhancedSetupFlowProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#pragma once

#include "TermsAndConditionsProvider.h"

#include <stdint.h>

#include <app/AttributePersistenceProvider.h>
Expand All @@ -29,29 +31,28 @@ namespace app {
class EnhancedSetupFlowProvider
{
public:
static EnhancedSetupFlowProvider & GetInstance() { return sEnhancedSetupFlowProviderInstance; }
static EnhancedSetupFlowProvider & GetInstance() { return sInstance; }

EnhancedSetupFlowProvider(const chip::EndpointId & endpointId = kRootEndpointId) : mEndpointId(endpointId){};
CHIP_ERROR Init();

bool HasRequiredTermsAndConditionsBeenAcknowledged();
bool HasRequiredTermsAndConditionsMinVersionBeenAcknowledged();
virtual ~EnhancedSetupFlowProvider() = default;

CHIP_ERROR GetTCAcceptedVersion(uint16_t & value);
CHIP_ERROR GetTCMinRequiredVersion(uint16_t & value);
CHIP_ERROR GetTCAcknowledgements(uint16_t & value);
CHIP_ERROR GetTCAcknowledgementsRequired(uint16_t & value);
virtual CHIP_ERROR Init();

CHIP_ERROR SetTCAcknowledgements(uint16_t tcVersion, uint16_t tcUserResponse);
virtual bool HasTermsAndConditionsRequiredAcknowledgementsBeenAccepted();
virtual bool HasTermsAndConditionsRequiredAcknowledgementsVersionBeenAccepted();

private:
static EnhancedSetupFlowProvider sEnhancedSetupFlowProviderInstance;
virtual CHIP_ERROR GetTermsAndConditionsRequiredAcknowledgements(uint16_t & value);
virtual CHIP_ERROR GetTermsAndConditionsRequiredAcknowledgementsVersion(uint16_t & value);
virtual CHIP_ERROR GetTermsAndConditionsAcceptedAcknowledgements(uint16_t & value);
virtual CHIP_ERROR GetTermsAndConditionsAcceptedAcknowledgementsVersion(uint16_t & value);
virtual CHIP_ERROR SetTermsAndConditionsAcceptance(uint16_t aTCAcknowledgements, uint16_t aTCAcknowledgementsVersion);

private:
static EnhancedSetupFlowProvider sInstance;
const chip::EndpointId mEndpointId;
uint16_t mTCRequiredVersion;
uint16_t mTCRequiredAcknowledgements;
uint16_t mTCUserAcceptedVersion;
uint16_t mTCUserAcceptedAcknowledgements;

TermsAndConditionsProvider mTermsAndConditionsProvider;
};

}; // namespace app
Expand Down
79 changes: 79 additions & 0 deletions src/app/server/TermsAndConditionsProvider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
*
* Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "TermsAndConditionsProvider.h"

#include <lib/core/CHIPConfig.h>
#include <platform/KeyValueStoreManager.h>

namespace {
constexpr char kAcceptedAcknowledgementsKeyName[] = "tc-acceptedacknowledgements";
constexpr char kAcceptedAcknowledgementsVersionKeyName[] = "tc-acceptedacknowledgementsversion";
} // namespace

CHIP_ERROR chip::app::TermsAndConditionsProvider::Init()
{
uint16_t acceptedAcknowledgements = 0;
uint16_t acceptedAcknowledgementsVersion = 0;

chip::DeviceLayer::PersistedStorage::KeyValueStoreManager & kvsManager =
chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr();

kvsManager.Get(kAcceptedAcknowledgementsKeyName, &acceptedAcknowledgements, sizeof(acceptedAcknowledgements));
kvsManager.Get(kAcceptedAcknowledgementsVersionKeyName, &acceptedAcknowledgementsVersion,
sizeof(acceptedAcknowledgementsVersion));

mRequiredAcknowledgements = CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS;
mRequiredAcknowledgementsVersion = CHIP_CONFIG_TC_REQUIRED_VERSION;
mAcceptedAcknowledgements = acceptedAcknowledgements;
mAcceptedAcknowledgementsVersion = acceptedAcknowledgementsVersion;

return CHIP_NO_ERROR;
}

CHIP_ERROR chip::app::TermsAndConditionsProvider::GetAcceptance(uint16_t & aAcknowledgements, uint16_t & aAcknowledgementsVersion)
{
aAcknowledgements = mAcceptedAcknowledgements;
aAcknowledgementsVersion = mAcceptedAcknowledgementsVersion;

return CHIP_NO_ERROR;
}

CHIP_ERROR chip::app::TermsAndConditionsProvider::GetRequirements(uint16_t & aAcknowledgements, uint16_t & aAcknowledgementsVersion)
{
aAcknowledgements = mRequiredAcknowledgements;
aAcknowledgementsVersion = mRequiredAcknowledgementsVersion;

return CHIP_NO_ERROR;
}

CHIP_ERROR chip::app::TermsAndConditionsProvider::SetAcceptance(uint16_t aTCAcceptedAcknowledgements,
uint16_t aTCAcceptedAcknowledgementsVersion)
{
chip::DeviceLayer::PersistedStorage::KeyValueStoreManager & kvsManager =
chip::DeviceLayer::PersistedStorage::KeyValueStoreMgr();

mAcceptedAcknowledgements = aTCAcceptedAcknowledgements;
mAcceptedAcknowledgementsVersion = aTCAcceptedAcknowledgementsVersion;

kvsManager.Put(kAcceptedAcknowledgementsKeyName, &mAcceptedAcknowledgements, sizeof(mAcceptedAcknowledgements));
kvsManager.Put(kAcceptedAcknowledgementsVersionKeyName, &mAcceptedAcknowledgementsVersion,
sizeof(mAcceptedAcknowledgementsVersion));

return CHIP_NO_ERROR;
}
Loading

0 comments on commit c22c7df

Please sign in to comment.