Skip to content

Commit 999c1dd

Browse files
authored
OnOffServer: ignore StartUpOnOff value after reboot due to OTA (#34160)
* feat: restore onOffValue after reboot during OTA applying * fix: style * fix: set onOffValueForStartUp in case of reserved values
1 parent 157aa40 commit 999c1dd

File tree

1 file changed

+47
-27
lines changed

1 file changed

+47
-27
lines changed

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

+47-27
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,16 @@
4545
#endif // MATTER_DM_PLUGIN_MODE_BASE
4646

4747
#include <platform/CHIPDeviceLayer.h>
48+
#include <platform/DiagnosticDataProvider.h>
4849
#include <platform/PlatformManager.h>
4950

5051
using namespace chip;
5152
using namespace chip::app::Clusters;
5253
using namespace chip::app::Clusters::OnOff;
5354
using chip::Protocols::InteractionModel::Status;
5455

56+
using BootReasonType = GeneralDiagnostics::BootReasonEnum;
57+
5558
namespace {
5659

5760
#ifdef MATTER_DM_PLUGIN_MODE_BASE
@@ -95,6 +98,22 @@ bool IsKnownEnumValue(EnumType value)
9598
return (EnsureKnownEnumValue(value) != EnumType::kUnknownEnumValue);
9699
}
97100

101+
BootReasonType GetBootReason()
102+
{
103+
BootReasonType bootReason = BootReasonType::kUnspecified;
104+
105+
CHIP_ERROR error = DeviceLayer::GetDiagnosticDataProvider().GetBootReason(bootReason);
106+
if (error != CHIP_NO_ERROR)
107+
{
108+
ChipLogError(Zcl, "Unable to retrieve boot reason: %" CHIP_ERROR_FORMAT, error.Format());
109+
bootReason = BootReasonType::kUnspecified;
110+
}
111+
112+
ChipLogProgress(Zcl, "Boot reason: %u", to_underlying(bootReason));
113+
114+
return bootReason;
115+
}
116+
98117
} // namespace
99118

100119
#ifdef MATTER_DM_PLUGIN_LEVEL_CONTROL
@@ -537,35 +556,36 @@ Status OnOffServer::getOnOffValueForStartUp(chip::EndpointId endpoint, bool & on
537556
{
538557
app::DataModel::Nullable<OnOff::StartUpOnOffEnum> startUpOnOff;
539558
Status status = Attributes::StartUpOnOff::Get(endpoint, startUpOnOff);
540-
if (status == Status::Success)
559+
VerifyOrReturnError(status == Status::Success, status);
560+
561+
bool currentOnOff = false;
562+
status = Attributes::OnOff::Get(endpoint, &currentOnOff);
563+
VerifyOrReturnError(status == Status::Success, status);
564+
565+
if (startUpOnOff.IsNull() || GetBootReason() == BootReasonType::kSoftwareUpdateCompleted)
541566
{
542-
// Initialise updated value to 0
543-
bool updatedOnOff = false;
544-
status = Attributes::OnOff::Get(endpoint, &updatedOnOff);
545-
if (status == Status::Success)
546-
{
547-
if (!startUpOnOff.IsNull())
548-
{
549-
switch (startUpOnOff.Value())
550-
{
551-
case OnOff::StartUpOnOffEnum::kOff:
552-
updatedOnOff = false; // Off
553-
break;
554-
case OnOff::StartUpOnOffEnum::kOn:
555-
updatedOnOff = true; // On
556-
break;
557-
case OnOff::StartUpOnOffEnum::kToggle:
558-
updatedOnOff = !updatedOnOff;
559-
break;
560-
default:
561-
// All other values 0x03- 0xFE are reserved - no action.
562-
break;
563-
}
564-
}
565-
onOffValueForStartUp = updatedOnOff;
566-
}
567+
onOffValueForStartUp = currentOnOff;
568+
return Status::Success;
567569
}
568-
return status;
570+
571+
switch (startUpOnOff.Value())
572+
{
573+
case OnOff::StartUpOnOffEnum::kOff:
574+
onOffValueForStartUp = false; // Off
575+
break;
576+
case OnOff::StartUpOnOffEnum::kOn:
577+
onOffValueForStartUp = true; // On
578+
break;
579+
case OnOff::StartUpOnOffEnum::kToggle:
580+
onOffValueForStartUp = !currentOnOff;
581+
break;
582+
default:
583+
// All other values 0x03- 0xFE are reserved - no action.
584+
onOffValueForStartUp = currentOnOff;
585+
break;
586+
}
587+
588+
return Status::Success;
569589
}
570590

571591
bool OnOffServer::offCommand(app::CommandHandler * commandObj, const app::ConcreteCommandPath & commandPath)

0 commit comments

Comments
 (0)