Skip to content

Commit

Permalink
OnOffServer: ignore StartUpOnOff value after reboot due to OTA (#34160)
Browse files Browse the repository at this point in the history
* feat: restore onOffValue after reboot during OTA applying

* fix: style

* fix: set onOffValueForStartUp in case of reserved values
  • Loading branch information
j0tunn authored and pull[bot] committed Sep 4, 2024
1 parent 8fb6088 commit 1331685
Showing 1 changed file with 47 additions and 27 deletions.
74 changes: 47 additions & 27 deletions src/app/clusters/on-off-server/on-off-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@
#endif // MATTER_DM_PLUGIN_MODE_BASE

#include <platform/CHIPDeviceLayer.h>
#include <platform/DiagnosticDataProvider.h>
#include <platform/PlatformManager.h>

using namespace chip;
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
Expand Down Expand Up @@ -95,6 +98,22 @@ 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
Expand Down Expand Up @@ -537,35 +556,36 @@ Status OnOffServer::getOnOffValueForStartUp(chip::EndpointId endpoint, bool & on
{
app::DataModel::Nullable<OnOff::StartUpOnOffEnum> startUpOnOff;
Status status = Attributes::StartUpOnOff::Get(endpoint, startUpOnOff);
if (status == Status::Success)
VerifyOrReturnError(status == Status::Success, status);

bool currentOnOff = false;
status = Attributes::OnOff::Get(endpoint, &currentOnOff);
VerifyOrReturnError(status == Status::Success, status);

if (startUpOnOff.IsNull() || GetBootReason() == BootReasonType::kSoftwareUpdateCompleted)
{
// 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;
}
onOffValueForStartUp = currentOnOff;
return Status::Success;
}
return status;

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.
onOffValueForStartUp = currentOnOff;
break;
}

return Status::Success;
}

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

0 comments on commit 1331685

Please sign in to comment.