From b425959c54d23ac65c989bae96029fd505f2fcd7 Mon Sep 17 00:00:00 2001 From: crlonxp <88241281+crlonxp@users.noreply.github.com> Date: Fri, 25 Aug 2023 12:03:28 +0800 Subject: [PATCH] Add the Pre-Attribute checking for laundry washer controls cluster (#28618) * Add the PreAttribute checking for laundry washer controls cluster Signed-off-by: Chin-Ran Lo * Restyled by clang-format * Fix the warning in Darwin platform Signed-off-by: Chin-Ran Lo * Move the Pre-Attribute checking from the delegate to cluster handler Signed-off-by: Chin-Ran Lo * Restyled by clang-format * * Remove the redundant checking for read-only attributes * Not adding the API to get the size of the spin-speed list * Add the nullable attribute checking Signed-off-by: Chin-Ran Lo * Fix by follow review's comment - Replace a infinit for loop with a while true loop - Use the meaning variables to access Signed-off-by: Chin-Ran Lo * Restyled by clang-format * Fix the tidy warning for "Build on Linux" Signed-off-by: Chin-Ran Lo * Abort the program if delegate does not exist while writing the attribute Signed-off-by: Chin-Ran Lo --------- Signed-off-by: Chin-Ran Lo Co-authored-by: Restyled.io --- .../all-clusters-app/linux/main-common.cpp | 2 +- .../laundry-washer-controls-server.cpp | 47 +++++++++++++++++++ src/app/common/templates/config-data.yaml | 1 + 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/examples/all-clusters-app/linux/main-common.cpp b/examples/all-clusters-app/linux/main-common.cpp index 2eae63b5541f4d..39173399038ece 100644 --- a/examples/all-clusters-app/linux/main-common.cpp +++ b/examples/all-clusters-app/linux/main-common.cpp @@ -210,7 +210,7 @@ void ApplicationShutdown() using namespace chip::app::Clusters::LaundryWasherControls; void emberAfLaundryWasherControlsClusterInitCallback(EndpointId endpoint) { - LaundryWasherControlsServer::SetDefaultDelegate(1, &LaundryWasherControlDelegate::getLaundryWasherControlDelegate()); + LaundryWasherControlsServer::SetDefaultDelegate(endpoint, &LaundryWasherControlDelegate::getLaundryWasherControlDelegate()); } void emberAfLowPowerClusterInitCallback(EndpointId endpoint) diff --git a/src/app/clusters/laundry-washer-controls-server/laundry-washer-controls-server.cpp b/src/app/clusters/laundry-washer-controls-server/laundry-washer-controls-server.cpp index f7cb0cd06d42a6..156d181a15931e 100644 --- a/src/app/clusters/laundry-washer-controls-server/laundry-washer-controls-server.cpp +++ b/src/app/clusters/laundry-washer-controls-server/laundry-washer-controls-server.cpp @@ -192,3 +192,50 @@ void MatterLaundryWasherControlsPluginServerInitCallback() LaundryWasherControlsServer & laundryWasherControlsServer = LaundryWasherControlsServer::Instance(); registerAttributeAccessOverride(&laundryWasherControlsServer); } + +Status MatterLaundryWasherControlsClusterServerPreAttributeChangedCallback(const chip::app::ConcreteAttributePath & attributePath, + EmberAfAttributeType attributeType, uint16_t size, + uint8_t * value) +{ + Delegate * delegate = GetDelegate(attributePath.mEndpointId); + VerifyOrDie((delegate != nullptr) && "Washer Controls implementation requires a registered delegate for validation."); + switch (attributePath.mAttributeId) + { + case Attributes::SpinSpeedCurrent::Id: { + if (NumericAttributeTraits::IsNullValue(*value)) + { + return Status::Success; + } + char buffer[LaundryWasherControlsServer::kMaxSpinSpeedLength]; + MutableCharSpan spinSpeed(buffer); + uint8_t spinSpeedIndex = *value; + auto err = delegate->GetSpinSpeedAtIndex(spinSpeedIndex, spinSpeed); + if (err == CHIP_NO_ERROR) + { + return Status::Success; + } + return Status::ConstraintError; + } + case Attributes::NumberOfRinses::Id: { + uint8_t supportedRinseIdx = 0; + while (true) + { + NumberOfRinsesEnum supportedRinse; + auto err = delegate->GetSupportedRinseAtIndex(supportedRinseIdx, supportedRinse); + if (err != CHIP_NO_ERROR) + { + // Can't find the attribute to be written in the supported list (CHIP_ERROR_PROVIDER_LIST_EXHAUSTED) + // Or can't get the correct supported list + return Status::InvalidInState; + } + if (supportedRinse == static_cast(*value)) + { + // The written attribute is one of the supported item + return Status::Success; + } + supportedRinseIdx++; + } + } + } + return Status::Success; +} diff --git a/src/app/common/templates/config-data.yaml b/src/app/common/templates/config-data.yaml index 0e19b31138f27c..b6e8bf1135f051 100644 --- a/src/app/common/templates/config-data.yaml +++ b/src/app/common/templates/config-data.yaml @@ -73,3 +73,4 @@ ClustersWithPreAttributeChangeFunctions: - Mode Select - Fan Control - Thermostat + - Laundry Washer Controls