Skip to content

Commit 2ce77ce

Browse files
committed
feat: restore onOffValue after reboot during OTA applying
1 parent 157aa40 commit 2ce77ce

File tree

1 file changed

+50
-27
lines changed

1 file changed

+50
-27
lines changed

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

+50-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,21 @@ bool IsKnownEnumValue(EnumType value)
9598
return (EnsureKnownEnumValue(value) != EnumType::kUnknownEnumValue);
9699
}
97100

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

100118
#ifdef MATTER_DM_PLUGIN_LEVEL_CONTROL
@@ -537,35 +555,40 @@ Status OnOffServer::getOnOffValueForStartUp(chip::EndpointId endpoint, bool & on
537555
{
538556
app::DataModel::Nullable<OnOff::StartUpOnOffEnum> startUpOnOff;
539557
Status status = Attributes::StartUpOnOff::Get(endpoint, startUpOnOff);
540-
if (status == Status::Success)
558+
if (status != Status::Success)
541559
{
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-
}
560+
return status;
567561
}
568-
return status;
562+
563+
bool currentOnOff = false;
564+
status = Attributes::OnOff::Get(endpoint, &currentOnOff);
565+
if (status != Status::Success) {
566+
return status;
567+
}
568+
569+
if (startUpOnOff.IsNull() || GetBootReason() == BootReasonType::kSoftwareUpdateCompleted)
570+
{
571+
onOffValueForStartUp = currentOnOff;
572+
return Status::Success;
573+
}
574+
575+
switch (startUpOnOff.Value())
576+
{
577+
case OnOff::StartUpOnOffEnum::kOff:
578+
onOffValueForStartUp = false; // Off
579+
break;
580+
case OnOff::StartUpOnOffEnum::kOn:
581+
onOffValueForStartUp = true; // On
582+
break;
583+
case OnOff::StartUpOnOffEnum::kToggle:
584+
onOffValueForStartUp = !currentOnOff;
585+
break;
586+
default:
587+
// All other values 0x03- 0xFE are reserved - no action.
588+
break;
589+
}
590+
591+
return Status::Success;
569592
}
570593

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

0 commit comments

Comments
 (0)