From de1c261792d473fcec551e2f9c7fefd7093fa408 Mon Sep 17 00:00:00 2001 From: dkurek Date: Thu, 29 Aug 2024 16:55:35 +0200 Subject: [PATCH 1/2] Add force send parameter to setOnOffValue and send it on matter start --- .../clusters/on-off-server/on-off-server.cpp | 39 +++++++++++++------ .../clusters/on-off-server/on-off-server.h | 2 +- 2 files changed, 29 insertions(+), 12 deletions(-) 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 e4ba82bb8bdc2f..cd8d2dd90e24f7 100644 --- a/src/app/clusters/on-off-server/on-off-server.cpp +++ b/src/app/clusters/on-off-server/on-off-server.cpp @@ -340,8 +340,10 @@ Status OnOffServer::getOnOffValue(chip::EndpointId endpoint, bool * currentOnOff * @param endpoint Ver.: always * @param command Ver.: always * @param initiatedByLevelChange Ver.: always + * @param forceSend Send value of the On/Off even when it is the same as current On/Off value. + * This parameter is useful at the start when you try to determine startup state of On/Off relay that does not store state after losing power */ -Status OnOffServer::setOnOffValue(chip::EndpointId endpoint, chip::CommandId command, bool initiatedByLevelChange) +Status OnOffServer::setOnOffValue(chip::EndpointId endpoint, chip::CommandId command, bool initiatedByLevelChange, bool forceSend) { MATTER_TRACE_SCOPE("setOnOffValue", "OnOff"); Status status; @@ -355,18 +357,33 @@ Status OnOffServer::setOnOffValue(chip::EndpointId endpoint, chip::CommandId com return status; } - // if the value is already what we want to set it to then do nothing - if ((!currentValue && command == Commands::Off::Id) || (currentValue && command == Commands::On::Id)) + if (forceSend) { - ChipLogProgress(Zcl, "Endpoint %x On/off already set to new value", endpoint); - return Status::Success; - } + if (command == Commands::Off::Id) + { + newValue = false; + } + else if (command == Commands::On::Id) + { + newValue = true; - // we either got a toggle, or an on when off, or an off when on, - // so we need to swap the value - newValue = !currentValue; - ChipLogProgress(Zcl, "Toggle ep%x on/off from state %x to %x", endpoint, currentValue, newValue); + } + // I guess that, I can only get ON/OFF at startup + } + else + { + // if the value is already what we want to set it to then do nothing + if ((!currentValue && command == Commands::Off::Id) || (currentValue && command == Commands::On::Id)) + { + ChipLogProgress(Zcl, "Endpoint %x On/off already set to new value", endpoint); + return Status::Success; + } + // we either got a toggle, or an on when off, or an off when on, + // so we need to swap the value + newValue = !currentValue; + ChipLogProgress(Zcl, "Toggle ep%x on/off from state %x to %x", endpoint, currentValue, newValue); + } // the sequence of updating on/off attribute and kick off level change effect should // be depend on whether we are turning on or off. If we are turning on the light, we // should update the on/off attribute before kicking off level change, if we are @@ -499,7 +516,7 @@ void OnOffServer::initOnOffServer(chip::EndpointId endpoint) Status status = getOnOffValueForStartUp(endpoint, onOffValueForStartUp); if (status == Status::Success) { - status = setOnOffValue(endpoint, onOffValueForStartUp, true); + status = setOnOffValue(endpoint, onOffValueForStartUp, true, true); } #if defined(MATTER_DM_PLUGIN_SCENES_MANAGEMENT) && CHIP_CONFIG_SCENES_USE_DEFAULT_HANDLERS 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 2cb5b62958a6c7..2dd7ea59924dbe 100644 --- a/src/app/clusters/on-off-server/on-off-server.h +++ b/src/app/clusters/on-off-server/on-off-server.h @@ -64,7 +64,7 @@ class OnOffServer void updateOnOffTimeCommand(chip::EndpointId endpoint); chip::Protocols::InteractionModel::Status getOnOffValue(chip::EndpointId endpoint, bool * currentOnOffValue); chip::Protocols::InteractionModel::Status setOnOffValue(chip::EndpointId endpoint, chip::CommandId command, - bool initiatedByLevelChange); + bool initiatedByLevelChange, bool forceSend=false); chip::Protocols::InteractionModel::Status getOnOffValueForStartUp(chip::EndpointId endpoint, bool & onOffValueForStartUp); bool HasFeature(chip::EndpointId endpoint, Feature feature); From 570b93cd17e77e30d7b362796ba3c3987d9d147f Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Thu, 29 Aug 2024 15:12:07 +0000 Subject: [PATCH 2/2] Restyled by clang-format --- src/app/clusters/on-off-server/on-off-server.cpp | 4 ++-- src/app/clusters/on-off-server/on-off-server.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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 cd8d2dd90e24f7..f86713d14e0466 100644 --- a/src/app/clusters/on-off-server/on-off-server.cpp +++ b/src/app/clusters/on-off-server/on-off-server.cpp @@ -341,7 +341,8 @@ Status OnOffServer::getOnOffValue(chip::EndpointId endpoint, bool * currentOnOff * @param command Ver.: always * @param initiatedByLevelChange Ver.: always * @param forceSend Send value of the On/Off even when it is the same as current On/Off value. - * This parameter is useful at the start when you try to determine startup state of On/Off relay that does not store state after losing power + * This parameter is useful at the start when you try to determine startup state of On/Off relay that does not store state after + * losing power */ Status OnOffServer::setOnOffValue(chip::EndpointId endpoint, chip::CommandId command, bool initiatedByLevelChange, bool forceSend) { @@ -366,7 +367,6 @@ Status OnOffServer::setOnOffValue(chip::EndpointId endpoint, chip::CommandId com else if (command == Commands::On::Id) { newValue = true; - } // I guess that, I can only get ON/OFF at startup } 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 2dd7ea59924dbe..4c152fef0c19d9 100644 --- a/src/app/clusters/on-off-server/on-off-server.h +++ b/src/app/clusters/on-off-server/on-off-server.h @@ -64,7 +64,7 @@ class OnOffServer void updateOnOffTimeCommand(chip::EndpointId endpoint); chip::Protocols::InteractionModel::Status getOnOffValue(chip::EndpointId endpoint, bool * currentOnOffValue); chip::Protocols::InteractionModel::Status setOnOffValue(chip::EndpointId endpoint, chip::CommandId command, - bool initiatedByLevelChange, bool forceSend=false); + bool initiatedByLevelChange, bool forceSend = false); chip::Protocols::InteractionModel::Status getOnOffValueForStartUp(chip::EndpointId endpoint, bool & onOffValueForStartUp); bool HasFeature(chip::EndpointId endpoint, Feature feature);