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 5158ff489799c8..4c5f457e4b4840 100644 --- a/src/app/clusters/on-off-server/on-off-server.cpp +++ b/src/app/clusters/on-off-server/on-off-server.cpp @@ -457,22 +457,17 @@ uint32_t OnOffServer::calculateNextWaitTimeMS(void) const chip::System::Clock::Timestamp currentTime = chip::System::SystemClock().GetMonotonicTimestamp(); chip::System::Clock::Timestamp waitTime = UPDATE_TIME_MS; chip::System::Clock::Timestamp latency; - chip::System::Clock::Timestamp elapsed; - if (currentTime > onWithTimedOffStartTimestamp) + if (currentTime > nextDesiredOnWithTimedOffTimestamp) { - elapsed = currentTime - onWithTimedOffStartTimestamp; - if (elapsed >= UPDATE_TIME_MS) - { - latency = elapsed - UPDATE_TIME_MS; - if (waitTime > latency) - waitTime -= latency; - else if (waitTime == latency) - waitTime = chip::System::Clock::Milliseconds32(1); - } + latency = currentTime - nextDesiredOnWithTimedOffTimestamp; + if (latency >= UPDATE_TIME_MS) + waitTime = chip::System::Clock::Milliseconds32(1); + else + waitTime -= latency; } - onWithTimedOffStartTimestamp = currentTime; + nextDesiredOnWithTimedOffTimestamp += UPDATE_TIME_MS; return (uint32_t)waitTime.count(); } @@ -528,7 +523,7 @@ bool OnOffServer::OnWithTimedOffCommand(const app::ConcreteCommandPath & command if (currentOnTime < MAX_TIME_VALUE && currentOffWaitTime < MAX_TIME_VALUE) { - onWithTimedOffStartTimestamp = chip::System::SystemClock().GetMonotonicTimestamp(); + nextDesiredOnWithTimedOffTimestamp = chip::System::SystemClock().GetMonotonicTimestamp() + UPDATE_TIME_MS; emberEventControlSetDelayMS(configureEventControl(endpoint), (uint32_t)UPDATE_TIME_MS.count()); } 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 a3340282898078..9fbe635602e9b1 100644 --- a/src/app/clusters/on-off-server/on-off-server.h +++ b/src/app/clusters/on-off-server/on-off-server.h @@ -86,7 +86,7 @@ class OnOffServer static OnOffServer instance; EmberEventControl eventControls[EMBER_AF_ON_OFF_CLUSTER_SERVER_ENDPOINT_COUNT]; - chip::System::Clock::Timestamp onWithTimedOffStartTimestamp; + chip::System::Clock::Timestamp nextDesiredOnWithTimedOffTimestamp; }; struct OnOffEffect