From 2ce77ce1a2a8b080cb4e5270fd1c61307626fe82 Mon Sep 17 00:00:00 2001 From: Anton Usmansky Date: Wed, 3 Jul 2024 13:22:39 +0300 Subject: [PATCH 1/3] feat: restore onOffValue after reboot during OTA applying --- .../clusters/on-off-server/on-off-server.cpp | 77 ++++++++++++------- 1 file changed, 50 insertions(+), 27 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 98dfaf9da02c3e..798cd5a1b2ed99 100644 --- a/src/app/clusters/on-off-server/on-off-server.cpp +++ b/src/app/clusters/on-off-server/on-off-server.cpp @@ -45,6 +45,7 @@ #endif // MATTER_DM_PLUGIN_MODE_BASE #include +#include #include using namespace chip; @@ -52,6 +53,8 @@ using namespace chip::app::Clusters; using namespace chip::app::Clusters::OnOff; using chip::Protocols::InteractionModel::Status; +using BootReasonType = GeneralDiagnostics::BootReasonEnum; + namespace { #ifdef MATTER_DM_PLUGIN_MODE_BASE @@ -95,6 +98,21 @@ bool IsKnownEnumValue(EnumType value) return (EnsureKnownEnumValue(value) != EnumType::kUnknownEnumValue); } +BootReasonType GetBootReason() { + BootReasonType bootReason = BootReasonType::kUnspecified; + + CHIP_ERROR error = DeviceLayer::GetDiagnosticDataProvider().GetBootReason(bootReason); + if (error != CHIP_NO_ERROR) + { + ChipLogError(Zcl, "Unable to retrieve boot reason: %" CHIP_ERROR_FORMAT, error.Format()); + bootReason = BootReasonType::kUnspecified; + } + + ChipLogProgress(Zcl, "Boot reason: %u", to_underlying(bootReason)); + + return bootReason; +} + } // namespace #ifdef MATTER_DM_PLUGIN_LEVEL_CONTROL @@ -537,35 +555,40 @@ Status OnOffServer::getOnOffValueForStartUp(chip::EndpointId endpoint, bool & on { app::DataModel::Nullable startUpOnOff; Status status = Attributes::StartUpOnOff::Get(endpoint, startUpOnOff); - if (status == Status::Success) + if (status != Status::Success) { - // Initialise updated value to 0 - bool updatedOnOff = false; - status = Attributes::OnOff::Get(endpoint, &updatedOnOff); - if (status == Status::Success) - { - if (!startUpOnOff.IsNull()) - { - switch (startUpOnOff.Value()) - { - case OnOff::StartUpOnOffEnum::kOff: - updatedOnOff = false; // Off - break; - case OnOff::StartUpOnOffEnum::kOn: - updatedOnOff = true; // On - break; - case OnOff::StartUpOnOffEnum::kToggle: - updatedOnOff = !updatedOnOff; - break; - default: - // All other values 0x03- 0xFE are reserved - no action. - break; - } - } - onOffValueForStartUp = updatedOnOff; - } + return status; } - return status; + + bool currentOnOff = false; + status = Attributes::OnOff::Get(endpoint, ¤tOnOff); + if (status != Status::Success) { + return status; + } + + if (startUpOnOff.IsNull() || GetBootReason() == BootReasonType::kSoftwareUpdateCompleted) + { + onOffValueForStartUp = currentOnOff; + return Status::Success; + } + + switch (startUpOnOff.Value()) + { + case OnOff::StartUpOnOffEnum::kOff: + onOffValueForStartUp = false; // Off + break; + case OnOff::StartUpOnOffEnum::kOn: + onOffValueForStartUp = true; // On + break; + case OnOff::StartUpOnOffEnum::kToggle: + onOffValueForStartUp = !currentOnOff; + break; + default: + // All other values 0x03- 0xFE are reserved - no action. + break; + } + + return Status::Success; } bool OnOffServer::offCommand(app::CommandHandler * commandObj, const app::ConcreteCommandPath & commandPath) From 77020904a6a7e830bfb0b03be79e39341dd62fa9 Mon Sep 17 00:00:00 2001 From: Anton Usmansky Date: Wed, 3 Jul 2024 13:31:08 +0300 Subject: [PATCH 2/3] fix: style --- src/app/clusters/on-off-server/on-off-server.cpp | 8 +++++--- 1 file changed, 5 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 798cd5a1b2ed99..511d38d33fa7cc 100644 --- a/src/app/clusters/on-off-server/on-off-server.cpp +++ b/src/app/clusters/on-off-server/on-off-server.cpp @@ -98,7 +98,8 @@ bool IsKnownEnumValue(EnumType value) return (EnsureKnownEnumValue(value) != EnumType::kUnknownEnumValue); } -BootReasonType GetBootReason() { +BootReasonType GetBootReason() +{ BootReasonType bootReason = BootReasonType::kUnspecified; CHIP_ERROR error = DeviceLayer::GetDiagnosticDataProvider().GetBootReason(bootReason); @@ -561,8 +562,9 @@ Status OnOffServer::getOnOffValueForStartUp(chip::EndpointId endpoint, bool & on } bool currentOnOff = false; - status = Attributes::OnOff::Get(endpoint, ¤tOnOff); - if (status != Status::Success) { + status = Attributes::OnOff::Get(endpoint, ¤tOnOff); + if (status != Status::Success) + { return status; } From c7761e15482543cc33f20bbcfc412d2da6fc9627 Mon Sep 17 00:00:00 2001 From: Anton Usmansky Date: Thu, 4 Jul 2024 10:36:15 +0300 Subject: [PATCH 3/3] fix: set onOffValueForStartUp in case of reserved values --- src/app/clusters/on-off-server/on-off-server.cpp | 11 +++-------- 1 file changed, 3 insertions(+), 8 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 511d38d33fa7cc..d18aa09383a0d5 100644 --- a/src/app/clusters/on-off-server/on-off-server.cpp +++ b/src/app/clusters/on-off-server/on-off-server.cpp @@ -556,17 +556,11 @@ Status OnOffServer::getOnOffValueForStartUp(chip::EndpointId endpoint, bool & on { app::DataModel::Nullable startUpOnOff; Status status = Attributes::StartUpOnOff::Get(endpoint, startUpOnOff); - if (status != Status::Success) - { - return status; - } + VerifyOrReturnError(status == Status::Success, status); bool currentOnOff = false; status = Attributes::OnOff::Get(endpoint, ¤tOnOff); - if (status != Status::Success) - { - return status; - } + VerifyOrReturnError(status == Status::Success, status); if (startUpOnOff.IsNull() || GetBootReason() == BootReasonType::kSoftwareUpdateCompleted) { @@ -587,6 +581,7 @@ Status OnOffServer::getOnOffValueForStartUp(chip::EndpointId endpoint, bool & on break; default: // All other values 0x03- 0xFE are reserved - no action. + onOffValueForStartUp = currentOnOff; break; }