Skip to content

Commit

Permalink
feat: restore onOffValue after reboot during OTA applying
Browse files Browse the repository at this point in the history
  • Loading branch information
j0tunn committed Jul 3, 2024
1 parent 005f1b4 commit 7ebc1c0
Showing 1 changed file with 50 additions and 27 deletions.
77 changes: 50 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 @@ -89,6 +92,21 @@ void UpdateModeBaseCurrentModeToOnMode(EndpointId endpoint)

#endif // MATTER_DM_PLUGIN_MODE_BASE

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 @@ -541,35 +559,40 @@ 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)
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, &currentOnOff);
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)
Expand Down

0 comments on commit 7ebc1c0

Please sign in to comment.