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 Aug 1, 2024
1 parent f1f337a commit 7730ba6
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,133 +59,105 @@ using Transport::Session;
namespace {

template <typename Provider, typename T>
static CHIP_ERROR ReadInternal(Provider* const provider, CHIP_ERROR (Provider::*const getter)(T&), AttributeValueEncoder& aEncoder)
static CHIP_ERROR ReadIfSupported(Provider * const provider, CHIP_ERROR (Provider::*const nonConstGetter)(T &),
AttributeValueEncoder & aEncoder)
{
T data;

if (nullptr == provider)
{
return CHIP_ERROR_PERSISTED_STORAGE_FAILED;
}

CHIP_ERROR err = (provider->*getter)(data);
if (err == CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE)
{
data = 0;
}
else if (err != CHIP_NO_ERROR)
T value;
CHIP_ERROR err = (provider->*nonConstGetter)(value);
if (err != CHIP_NO_ERROR)
{
return err;
}

return aEncoder.Encode(data);
return aEncoder.Encode(value);
}

template <typename Provider, typename T>
static CHIP_ERROR ReadInternal(Provider* const provider, CHIP_ERROR (Provider::*const getter)(T&) const, AttributeValueEncoder& aEncoder)
static CHIP_ERROR ReadIfSupported(Provider * const provider, CHIP_ERROR (Provider::*const getter)(T &) const,
AttributeValueEncoder & aEncoder)
{
// Removing the const qualifier from the getter function pointer because there are a handful of getter functions that are not correctly marked as const.
using NonConstGetter = CHIP_ERROR (Provider::*)(T&);
// Removing the const qualifier from the getter function pointer because there are a handful of getter functions that are not
// correctly marked as const.
using NonConstGetter = CHIP_ERROR (Provider::*)(T &);
NonConstGetter nonConstGetter = reinterpret_cast<NonConstGetter>(getter);

return ReadInternal(provider, nonConstGetter, aEncoder);
return ReadIfSupported(provider, nonConstGetter, aEncoder);
}

template <typename... Args>
static CHIP_ERROR ReadIfSupported(Args &&... args)
{
return ReadInternal(std::forward<Args>(args)...);
}

class GeneralCommissioningAttrAccess : public AttributeAccessInterface
class _ : public AttributeAccessInterface
{
public:
// Register for the GeneralCommissioning cluster on all endpoints.
GeneralCommissioningAttrAccess() : AttributeAccessInterface(Optional<EndpointId>::Missing(), GeneralCommissioning::Id) {}

CHIP_ERROR Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) override;

private:
CHIP_ERROR ReadBasicCommissioningInfo(AttributeValueEncoder & aEncoder);
CHIP_ERROR ReadSupportsConcurrentConnection(AttributeValueEncoder & aEncoder);
};

GeneralCommissioningAttrAccess gAttrAccess;

CHIP_ERROR GeneralCommissioningAttrAccess::Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
{
if (aPath.mClusterId != GeneralCommissioning::Id)
{
// We shouldn't have been called at all.
return CHIP_ERROR_INVALID_ARGUMENT;
}
_() : AttributeAccessInterface(Optional<EndpointId>::Missing(), GeneralCommissioning::Id) {}

switch (aPath.mAttributeId)
CHIP_ERROR Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder)
{
case RegulatoryConfig::Id: {
return ReadIfSupported(&DeviceLayer::ConfigurationMgr(), &ConfigurationManager::GetRegulatoryLocation, aEncoder);
}
case LocationCapability::Id: {
return ReadIfSupported(&DeviceLayer::ConfigurationMgr(), &ConfigurationManager::GetLocationCapability, aEncoder);
}
case BasicCommissioningInfo::Id: {
return ReadBasicCommissioningInfo(aEncoder);
}
case SupportsConcurrentConnection::Id: {
return ReadSupportsConcurrentConnection(aEncoder);
}
#if defined (CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS) && defined (CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION)
case TCAcceptedVersion::Id: {
app::EnhancedSetupFlowProvider * provider = Server::GetInstance().GetEnhancedSetupFlowProvider();
auto getter = &EnhancedSetupFlowProvider::GetTermsAndConditionsAcceptedAcknowledgementsVersion;
return ReadIfSupported(provider, getter, aEncoder);
}
case TCMinRequiredVersion::Id: {
auto provider = Server::GetInstance().GetEnhancedSetupFlowProvider();
auto getter = &EnhancedSetupFlowProvider::GetTermsAndConditionsRequiredAcknowledgementsVersion;
return ReadIfSupported(provider, getter, aEncoder);
}
case TCAcknowledgements::Id: {
auto provider = Server::GetInstance().GetEnhancedSetupFlowProvider();
auto getter = &EnhancedSetupFlowProvider::GetTermsAndConditionsAcceptedAcknowledgements;
return ReadIfSupported(provider, getter, aEncoder);
}
case TCAcknowledgementsRequired::Id: {
auto provider = Server::GetInstance().GetEnhancedSetupFlowProvider();
auto getter = &EnhancedSetupFlowProvider::GetTermsAndConditionsRequiredAcknowledgements;
return ReadIfSupported(provider, getter, aEncoder);
}
#endif
default:
break;
}
return CHIP_NO_ERROR;
}

CHIP_ERROR GeneralCommissioningAttrAccess::ReadBasicCommissioningInfo(AttributeValueEncoder & aEncoder)
{
BasicCommissioningInfo::TypeInfo::Type basicCommissioningInfo;
if (aPath.mClusterId != GeneralCommissioning::Id)
{
// We shouldn't have been called at all.
return CHIP_ERROR_INVALID_ARGUMENT;
}

// TODO: The commissioner might use the critical parameters in BasicCommissioningInfo to initialize
// the CommissioningParameters at the beginning of commissioning flow.
basicCommissioningInfo.failSafeExpiryLengthSeconds = CHIP_DEVICE_CONFIG_FAILSAFE_EXPIRY_LENGTH_SEC;
basicCommissioningInfo.maxCumulativeFailsafeSeconds = CHIP_DEVICE_CONFIG_MAX_CUMULATIVE_FAILSAFE_SEC;
static_assert(CHIP_DEVICE_CONFIG_MAX_CUMULATIVE_FAILSAFE_SEC >= CHIP_DEVICE_CONFIG_FAILSAFE_EXPIRY_LENGTH_SEC,
"Max cumulative failsafe seconds must be larger than failsafe expiry length seconds");
switch (aPath.mAttributeId)
{
case RegulatoryConfig::Id: {
return ReadIfSupported(&DeviceLayer::ConfigurationMgr(), &ConfigurationManager::GetRegulatoryLocation, aEncoder);
}
case LocationCapability::Id: {
return ReadIfSupported(&DeviceLayer::ConfigurationMgr(), &ConfigurationManager::GetLocationCapability, aEncoder);
}
case BasicCommissioningInfo::Id: {
BasicCommissioningInfo::TypeInfo::Type basicCommissioningInfo;

return aEncoder.Encode(basicCommissioningInfo);
}
// TODO: The commissioner might use the critical parameters in BasicCommissioningInfo to initialize
// the CommissioningParameters at the beginning of commissioning flow.
basicCommissioningInfo.failSafeExpiryLengthSeconds = CHIP_DEVICE_CONFIG_FAILSAFE_EXPIRY_LENGTH_SEC;
basicCommissioningInfo.maxCumulativeFailsafeSeconds = CHIP_DEVICE_CONFIG_MAX_CUMULATIVE_FAILSAFE_SEC;
static_assert(CHIP_DEVICE_CONFIG_MAX_CUMULATIVE_FAILSAFE_SEC >= CHIP_DEVICE_CONFIG_FAILSAFE_EXPIRY_LENGTH_SEC,
"Max cumulative failsafe seconds must be larger than failsafe expiry length seconds");

CHIP_ERROR GeneralCommissioningAttrAccess::ReadSupportsConcurrentConnection(AttributeValueEncoder & aEncoder)
{
SupportsConcurrentConnection::TypeInfo::Type supportsConcurrentConnection;
return aEncoder.Encode(basicCommissioningInfo);
}
case SupportsConcurrentConnection::Id: {
SupportsConcurrentConnection::TypeInfo::Type supportsConcurrentConnection;

// TODO: The commissioner might use the critical parameters in BasicCommissioningInfo to initialize
// the CommissioningParameters at the beginning of commissioning flow.
supportsConcurrentConnection = (CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION) != 0;
// TODO: The commissioner might use the critical parameters in BasicCommissioningInfo to initialize
// the CommissioningParameters at the beginning of commissioning flow.
supportsConcurrentConnection = (CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION) != 0;

return aEncoder.Encode(supportsConcurrentConnection);
}
return aEncoder.Encode(supportsConcurrentConnection);
}
#if defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS) && defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION)
case TCAcceptedVersion::Id: {
auto provider = Server::GetInstance().GetEnhancedSetupFlowProvider();
auto getter = &EnhancedSetupFlowProvider::GetTermsAndConditionsAcceptedAcknowledgementsVersion;
return ReadIfSupported(provider, getter, aEncoder);
}
case TCMinRequiredVersion::Id: {
auto provider = Server::GetInstance().GetEnhancedSetupFlowProvider();
auto getter = &EnhancedSetupFlowProvider::GetTermsAndConditionsRequiredAcknowledgementsVersion;
return ReadIfSupported(provider, getter, aEncoder);
}
case TCAcknowledgements::Id: {
auto provider = Server::GetInstance().GetEnhancedSetupFlowProvider();
auto getter = &EnhancedSetupFlowProvider::GetTermsAndConditionsAcceptedAcknowledgements;
return ReadIfSupported(provider, getter, aEncoder);
}
case TCAcknowledgementsRequired::Id: {
auto provider = Server::GetInstance().GetEnhancedSetupFlowProvider();
auto getter = &EnhancedSetupFlowProvider::GetTermsAndConditionsRequiredAcknowledgements;
return ReadIfSupported(provider, getter, aEncoder);
}
#endif
default:
break;
}
return CHIP_NO_ERROR;
}
} gAttributeAccessInstance;

} // anonymous namespace

Expand Down Expand Up @@ -257,7 +229,7 @@ bool emberAfGeneralCommissioningClusterCommissioningCompleteCallback(
{
MATTER_TRACE_SCOPE("CommissioningComplete", "GeneralCommissioning");

#if defined (CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS) && defined (CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION)
#if defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS) && defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION)
EnhancedSetupFlowProvider * enhancedSetupFlowProvider = Server::GetInstance().GetEnhancedSetupFlowProvider();
#endif
DeviceControlServer * const devCtrl = &DeviceLayer::DeviceControlServer::DeviceControlSvr();
Expand Down Expand Up @@ -287,7 +259,7 @@ bool emberAfGeneralCommissioningClusterCommissioningCompleteCallback(
{
CHIP_ERROR err;

#if defined (CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS) && defined (CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION)
#if defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS) && defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION)

uint16_t termsAndConditionsAcceptedAcknowledgements;
bool hasRequiredTermAccepted;
Expand All @@ -297,8 +269,7 @@ bool emberAfGeneralCommissioningClusterCommissioningCompleteCallback(
termsAndConditionsAcceptedAcknowledgements);
CheckSuccess(err, Failure);

err = enhancedSetupFlowProvider->HasTermsAndConditionsRequiredAcknowledgementsBeenAccepted(
hasRequiredTermAccepted);
err = enhancedSetupFlowProvider->HasTermsAndConditionsRequiredAcknowledgementsBeenAccepted(hasRequiredTermAccepted);
CheckSuccess(err, Failure);

err = enhancedSetupFlowProvider->HasTermsAndConditionsRequiredAcknowledgementsVersionBeenAccepted(
Expand Down Expand Up @@ -408,7 +379,7 @@ bool emberAfGeneralCommissioningClusterSetTCAcknowledgementsCallback(
chip::app::CommandHandler * commandObj, const chip::app::ConcreteCommandPath & commandPath,
const chip::app::Clusters::GeneralCommissioning::Commands::SetTCAcknowledgements::DecodableType & commandData)
{
#if defined (CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS) && defined (CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION)
#if defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS) && defined(CHIP_CONFIG_TC_REQUIRED_ACKNOWLEDGEMENTS_VERSION)
MATTER_TRACE_SCOPE("SetTCAcknowledgements", "GeneralCommissioning");
Commands::SetTCAcknowledgementsResponse::Type response;
EnhancedSetupFlowProvider * const enhancedSetupFlowProvider = Server::GetInstance().GetEnhancedSetupFlowProvider();
Expand Down Expand Up @@ -437,7 +408,7 @@ void OnPlatformEventHandler(const DeviceLayer::ChipDeviceEvent * event, intptr_t
void MatterGeneralCommissioningPluginServerInitCallback()
{
Breadcrumb::Set(0, 0);
registerAttributeAccessOverride(&gAttrAccess);
registerAttributeAccessOverride(&gAttributeAccessInstance);
DeviceLayer::PlatformMgrImpl().AddEventHandler(OnPlatformEventHandler);
}

Expand Down
1 change: 0 additions & 1 deletion src/app/server/DefaultEnhancedSetupFlowProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

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

CHIP_ERROR chip::app::DefaultEnhancedSetupFlowProvider::Init(TermsAndConditionsProvider * const inTermsAndConditionsProvider)
{
Expand Down
2 changes: 1 addition & 1 deletion src/app/server/DefaultEnhancedSetupFlowProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class DefaultEnhancedSetupFlowProvider : public EnhancedSetupFlowProvider
/**
* @copydoc EnhancedSetupFlowProvider::SetTermsAndConditionsAcceptance
*/
CHIP_ERROR SetTermsAndConditionsAcceptance(uint16_t aTCAcknowledgements, uint16_t aTCAcknowledgementsVersion) override;
CHIP_ERROR SetTermsAndConditionsAcceptance(uint16_t aTCAcknowledgements, uint16_t inTCAcknowledgementsVersionValue) override;

/**
* @copydoc EnhancedSetupFlowProvider::ClearTermsAndConditionsAcceptance
Expand Down
16 changes: 8 additions & 8 deletions src/app/server/DefaultTermsAndConditionsProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
#include <lib/core/TLVTypes.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/DefaultStorageKeyAllocator.h>
#include <platform/KeyValueStoreManager.h>

namespace {
static constexpr chip::TLV::Tag kAcceptedAcknowledgementsTag = chip::TLV::ContextTag(1);
static constexpr chip::TLV::Tag kAcceptedAcknowledgementsVersionTag = chip::TLV::ContextTag(2);
static constexpr size_t kEstimatedTlvBufferSize = chip::TLV::EstimateStructOverhead(sizeof(uint16_t), sizeof(uint16_t));
constexpr chip::TLV::Tag kSerializationVersionTag = chip::TLV::ContextTag(1);
constexpr chip::TLV::Tag kAcceptedAcknowledgementsTag = chip::TLV::ContextTag(2);
constexpr chip::TLV::Tag kAcceptedAcknowledgementsVersionTag = chip::TLV::ContextTag(3);
constexpr uint8_t kSerializationVersion = 1;
constexpr size_t kEstimatedTlvBufferSize = chip::TLV::EstimateStructOverhead(sizeof(uint8_t), sizeof(uint16_t), sizeof(uint16_t));
}; // namespace

CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::Init(chip::PersistentStorageDelegate * const inPersistentStorageDelegate,
Expand Down Expand Up @@ -83,11 +84,9 @@ CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::GetAcceptance(uint16_t
tlvReader.Init(buffer);
ReturnErrorOnFailure(tlvReader.Next(chip::TLV::kTLVType_Structure, chip::TLV::AnonymousTag()));
ReturnErrorOnFailure(tlvReader.EnterContainer(tlvContainer));
ReturnErrorOnFailure(tlvReader.Next());
ReturnErrorOnFailure(tlvReader.Expect(kAcceptedAcknowledgementsTag));
ReturnErrorOnFailure(tlvReader.Next(kAcceptedAcknowledgementsTag));
ReturnErrorOnFailure(tlvReader.Get(acknowledgements));
ReturnErrorOnFailure(tlvReader.Next());
ReturnErrorOnFailure(tlvReader.Expect(kAcceptedAcknowledgementsVersionTag));
ReturnErrorOnFailure(tlvReader.Next(kAcceptedAcknowledgementsVersionTag));
ReturnErrorOnFailure(tlvReader.Get(acknowledgementsVersion));
ReturnErrorOnFailure(tlvReader.ExitContainer(tlvContainer));

Expand Down Expand Up @@ -117,6 +116,7 @@ CHIP_ERROR chip::app::DefaultTermsAndConditionsProvider::SetAcceptance(uint16_t

tlvWriter.Init(buffer, sizeof(buffer));
ReturnErrorOnFailure(tlvWriter.StartContainer(chip::TLV::AnonymousTag(), chip::TLV::kTLVType_Structure, tlvContainer));
ReturnErrorOnFailure(tlvWriter.Put(kSerializationVersionTag, kSerializationVersion));
ReturnErrorOnFailure(tlvWriter.Put(kAcceptedAcknowledgementsTag, inAcceptedAcknowledgementsValue));
ReturnErrorOnFailure(tlvWriter.Put(kAcceptedAcknowledgementsVersionTag, inAcceptedAcknowledgementsVersionValue));
ReturnErrorOnFailure(tlvWriter.EndContainer(tlvContainer));
Expand Down
4 changes: 2 additions & 2 deletions src/app/server/DefaultTermsAndConditionsProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class DefaultTermsAndConditionsProvider : public TermsAndConditionsProvider
* @param[in] inRequiredAcknowledgementsVersionValue The version of the required acknowledgements.
* @return CHIP_ERROR On success returns CHIP_NO_ERROR, otherwise returns an error code.
*/
CHIP_ERROR Init(chip::PersistentStorageDelegate * const inPersistentStorageDelegate, uint16_t inRequiredAcknowledgementsValue,
CHIP_ERROR Init(PersistentStorageDelegate * const inPersistentStorageDelegate, uint16_t inRequiredAcknowledgementsValue,
uint16_t inRequiredAcknowledgementsVersionValue);

/**
Expand All @@ -64,7 +64,7 @@ class DefaultTermsAndConditionsProvider : public TermsAndConditionsProvider
CHIP_ERROR SetAcceptance(uint16_t inAcknowledgementsValue, uint16_t inAcknowledgementsVersionValue) override;

private:
chip::PersistentStorageDelegate * mPersistentStorageDelegate;
PersistentStorageDelegate * mPersistentStorageDelegate;
uint16_t mRequiredAcknowledgementsValue;
uint16_t mRequiredAcknowledgementsVersionValue;
};
Expand Down
4 changes: 2 additions & 2 deletions src/app/server/EnhancedSetupFlowProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ class EnhancedSetupFlowProvider
* @brief Sets the acceptance status of the terms and conditions.
*
* @param[in] inTCAcknowledgements The acknowledgements to accept.
* @param[in] inTCAcknowledgementsoutValue The version of the acknowledgements to accept.
* @param[in] inTCAcknowledgementsVersionValue The version of the acknowledgements to accept.
* @return CHIP_ERROR On success returns CHIP_NO_ERROR, otherwise returns an error code.
*/
virtual CHIP_ERROR SetTermsAndConditionsAcceptance(uint16_t inTCAcknowledgementsValue,
uint16_t inTCAcknowledgementsoutValue) = 0;
uint16_t inTCAcknowledgementsVersionValue) = 0;

/**
* @brief Clears the acceptance status of the terms and conditions.
Expand Down
Loading

0 comments on commit 7730ba6

Please sign in to comment.