Skip to content

Commit de1c261

Browse files
Add force send parameter to setOnOffValue and send it on matter start
1 parent a9027ca commit de1c261

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

src/app/clusters/on-off-server/on-off-server.cpp

+28-11
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,10 @@ Status OnOffServer::getOnOffValue(chip::EndpointId endpoint, bool * currentOnOff
340340
* @param endpoint Ver.: always
341341
* @param command Ver.: always
342342
* @param initiatedByLevelChange Ver.: always
343+
* @param forceSend Send value of the On/Off even when it is the same as current On/Off value.
344+
* 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
343345
*/
344-
Status OnOffServer::setOnOffValue(chip::EndpointId endpoint, chip::CommandId command, bool initiatedByLevelChange)
346+
Status OnOffServer::setOnOffValue(chip::EndpointId endpoint, chip::CommandId command, bool initiatedByLevelChange, bool forceSend)
345347
{
346348
MATTER_TRACE_SCOPE("setOnOffValue", "OnOff");
347349
Status status;
@@ -355,18 +357,33 @@ Status OnOffServer::setOnOffValue(chip::EndpointId endpoint, chip::CommandId com
355357
return status;
356358
}
357359

358-
// if the value is already what we want to set it to then do nothing
359-
if ((!currentValue && command == Commands::Off::Id) || (currentValue && command == Commands::On::Id))
360+
if (forceSend)
360361
{
361-
ChipLogProgress(Zcl, "Endpoint %x On/off already set to new value", endpoint);
362-
return Status::Success;
363-
}
362+
if (command == Commands::Off::Id)
363+
{
364+
newValue = false;
365+
}
366+
else if (command == Commands::On::Id)
367+
{
368+
newValue = true;
364369

365-
// we either got a toggle, or an on when off, or an off when on,
366-
// so we need to swap the value
367-
newValue = !currentValue;
368-
ChipLogProgress(Zcl, "Toggle ep%x on/off from state %x to %x", endpoint, currentValue, newValue);
370+
}
371+
// I guess that, I can only get ON/OFF at startup
372+
}
373+
else
374+
{
375+
// if the value is already what we want to set it to then do nothing
376+
if ((!currentValue && command == Commands::Off::Id) || (currentValue && command == Commands::On::Id))
377+
{
378+
ChipLogProgress(Zcl, "Endpoint %x On/off already set to new value", endpoint);
379+
return Status::Success;
380+
}
369381

382+
// we either got a toggle, or an on when off, or an off when on,
383+
// so we need to swap the value
384+
newValue = !currentValue;
385+
ChipLogProgress(Zcl, "Toggle ep%x on/off from state %x to %x", endpoint, currentValue, newValue);
386+
}
370387
// the sequence of updating on/off attribute and kick off level change effect should
371388
// be depend on whether we are turning on or off. If we are turning on the light, we
372389
// should update the on/off attribute before kicking off level change, if we are
@@ -499,7 +516,7 @@ void OnOffServer::initOnOffServer(chip::EndpointId endpoint)
499516
Status status = getOnOffValueForStartUp(endpoint, onOffValueForStartUp);
500517
if (status == Status::Success)
501518
{
502-
status = setOnOffValue(endpoint, onOffValueForStartUp, true);
519+
status = setOnOffValue(endpoint, onOffValueForStartUp, true, true);
503520
}
504521

505522
#if defined(MATTER_DM_PLUGIN_SCENES_MANAGEMENT) && CHIP_CONFIG_SCENES_USE_DEFAULT_HANDLERS

src/app/clusters/on-off-server/on-off-server.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class OnOffServer
6464
void updateOnOffTimeCommand(chip::EndpointId endpoint);
6565
chip::Protocols::InteractionModel::Status getOnOffValue(chip::EndpointId endpoint, bool * currentOnOffValue);
6666
chip::Protocols::InteractionModel::Status setOnOffValue(chip::EndpointId endpoint, chip::CommandId command,
67-
bool initiatedByLevelChange);
67+
bool initiatedByLevelChange, bool forceSend=false);
6868
chip::Protocols::InteractionModel::Status getOnOffValueForStartUp(chip::EndpointId endpoint, bool & onOffValueForStartUp);
6969

7070
bool HasFeature(chip::EndpointId endpoint, Feature feature);

0 commit comments

Comments
 (0)