From 8f45a9eb725d329d16f8c2a16ff5296d89b17298 Mon Sep 17 00:00:00 2001 From: jmartinez-silabs <67972863+jmartinez-silabs@users.noreply.github.com> Date: Mon, 25 Apr 2022 17:24:48 -0400 Subject: [PATCH] Use the stored on off attribute state to initialize led state (#17704) --- examples/lighting-app/efr32/src/AppTask.cpp | 2 -- .../lighting-app/efr32/src/LightingManager.cpp | 13 ++++++++++++- src/app/clusters/on-off-server/on-off-server.cpp | 14 ++++++++++++++ src/app/clusters/on-off-server/on-off-server.h | 1 + 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/examples/lighting-app/efr32/src/AppTask.cpp b/examples/lighting-app/efr32/src/AppTask.cpp index 21f47ad76072e4..c61198b6954ce9 100644 --- a/examples/lighting-app/efr32/src/AppTask.cpp +++ b/examples/lighting-app/efr32/src/AppTask.cpp @@ -279,8 +279,6 @@ CHIP_ERROR AppTask::Init() sLightLED.Init(LIGHT_LED); sLightLED.Set(LightMgr().IsLightOn()); - chip::DeviceLayer::PlatformMgr().ScheduleWork(UpdateClusterState, reinterpret_cast(nullptr)); - ConfigurationMgr().LogDeviceConfig(); // Print setup info on LCD if available diff --git a/examples/lighting-app/efr32/src/LightingManager.cpp b/examples/lighting-app/efr32/src/LightingManager.cpp index 7b206fd1ab6f37..3857a0d50e7fe5 100644 --- a/examples/lighting-app/efr32/src/LightingManager.cpp +++ b/examples/lighting-app/efr32/src/LightingManager.cpp @@ -23,6 +23,11 @@ #include "AppTask.h" #include +#include + +using namespace chip; +using namespace ::chip::DeviceLayer; + LightingManager LightingManager::sLight; TimerHandle_t sLightTimer; @@ -43,7 +48,13 @@ CHIP_ERROR LightingManager::Init() return APP_ERROR_CREATE_TIMER_FAILED; } - mState = kState_OffCompleted; + bool currentLedState; + // read current on/off value on endpoint one. + chip::DeviceLayer::PlatformMgr().LockChipStack(); + OnOffServer::Instance().getOnOffValue(1, ¤tLedState); + chip::DeviceLayer::PlatformMgr().UnlockChipStack(); + + mState = currentLedState ? kState_OnCompleted : kState_OffCompleted; mAutoTurnOffTimerArmed = false; mAutoTurnOff = false; mAutoTurnOffDuration = 0; diff --git a/src/app/clusters/on-off-server/on-off-server.cpp b/src/app/clusters/on-off-server/on-off-server.cpp index 54eafddc6cf118..96ccf644d89e2e 100644 --- a/src/app/clusters/on-off-server/on-off-server.cpp +++ b/src/app/clusters/on-off-server/on-off-server.cpp @@ -86,6 +86,20 @@ bool OnOffServer::HasFeature(chip::EndpointId endpoint, OnOffFeature feature) return success ? ((featureMap & to_underlying(feature)) != 0) : false; } +EmberAfStatus OnOffServer::getOnOffValue(chip::EndpointId endpoint, bool * currentOnOffValue) +{ + // read current on/off value + EmberAfStatus status = Attributes::OnOff::Get(endpoint, currentOnOffValue); + if (status != EMBER_ZCL_STATUS_SUCCESS) + { + emberAfOnOffClusterPrintln("ERR: reading on/off %x", status); + } + + emberAfOnOffClusterPrintln("On/Off ep%d value: %d", endpoint, *currentOnOffValue); + + return status; +} + /** @brief On/off Cluster Set Value * * This function is called when the on/off value needs to be set, either through diff --git a/src/app/clusters/on-off-server/on-off-server.h b/src/app/clusters/on-off-server/on-off-server.h index 0bab9ea4df97d8..a91588015bca9a 100644 --- a/src/app/clusters/on-off-server/on-off-server.h +++ b/src/app/clusters/on-off-server/on-off-server.h @@ -58,6 +58,7 @@ class OnOffServer bool OnWithTimedOffCommand(const chip::app::ConcreteCommandPath & commandPath, const chip::app::Clusters::OnOff::Commands::OnWithTimedOff::DecodableType & commandData); void updateOnOffTimeCommand(chip::EndpointId endpoint); + EmberAfStatus getOnOffValue(chip::EndpointId endpoint, bool * currentOnOffValue); EmberAfStatus setOnOffValue(chip::EndpointId endpoint, uint8_t command, bool initiatedByLevelChange); EmberAfStatus getOnOffValueForStartUp(chip::EndpointId endpoint, bool & onOffValueForStartUp);