Skip to content

Commit

Permalink
[general-diagnostics] Use type-safe BootReasonType enum (#16556)
Browse files Browse the repository at this point in the history
* [general-diagnostics] Use type-safe BootReaonType enum

1. Switch from EmberAfBootReasonType to type-safe
   BootReasonType.
2. Use BootReasonType in DiagnosticDataProvider interface.

* Fix K32W build
  • Loading branch information
Damian-Nordic authored and pull[bot] committed Jul 18, 2023
1 parent d6c81b4 commit 3729733
Show file tree
Hide file tree
Showing 27 changed files with 80 additions and 114 deletions.
14 changes: 7 additions & 7 deletions examples/platform/linux/AppMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,26 @@ void OnSignalHandler(int signum)
// The BootReason attribute SHALL indicate the reason for the Node’s most recent boot, the real usecase
// for this attribute is embedded system. In Linux simulation, we use different signals to tell the current
// running process to terminate with different reasons.
BootReasonType bootReason = BootReasonType::Unspecified;
BootReasonType bootReason = BootReasonType::kUnspecified;
switch (signum)
{
case SIGVTALRM:
bootReason = BootReasonType::PowerOnReboot;
bootReason = BootReasonType::kPowerOnReboot;
break;
case SIGALRM:
bootReason = BootReasonType::BrownOutReset;
bootReason = BootReasonType::kBrownOutReset;
break;
case SIGILL:
bootReason = BootReasonType::SoftwareWatchdogReset;
bootReason = BootReasonType::kSoftwareWatchdogReset;
break;
case SIGTRAP:
bootReason = BootReasonType::HardwareWatchdogReset;
bootReason = BootReasonType::kHardwareWatchdogReset;
break;
case SIGIO:
bootReason = BootReasonType::SoftwareUpdateCompleted;
bootReason = BootReasonType::kSoftwareUpdateCompleted;
break;
case SIGINT:
bootReason = BootReasonType::SoftwareReset;
bootReason = BootReasonType::kSoftwareReset;
break;
default:
IgnoreUnusedVariable(bootReason);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ using chip::DeviceLayer::ConnectivityMgr;
using chip::DeviceLayer::DiagnosticDataProvider;
using chip::DeviceLayer::GetDiagnosticDataProvider;

static_assert(sizeof(chip::DeviceLayer::BootReasonType) == sizeof(EmberAfBootReasonType),
"BootReasonType size doesn't match EmberAfBootReasonType size");
static_assert(static_cast<uint8_t>(chip::DeviceLayer::BootReasonType::Unspecified) == EMBER_ZCL_BOOT_REASON_TYPE_UNSPECIFIED &&
static_cast<uint8_t>(chip::DeviceLayer::BootReasonType::SoftwareReset) ==
EMBER_ZCL_BOOT_REASON_TYPE_SOFTWARE_RESET,
"BootReasonType and EmberAfBootReasonType values does not match.");

namespace {

class GeneralDiagosticsAttrAccess : public AttributeAccessInterface
Expand Down Expand Up @@ -71,7 +64,7 @@ CHIP_ERROR GeneralDiagosticsAttrAccess::ReadIfSupported(CHIP_ERROR (DiagnosticDa
CHIP_ERROR err = (GetDiagnosticDataProvider().*getter)(data);
if (err == CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE)
{
data = 0;
data = {};
}
else if (err != CHIP_NO_ERROR)
{
Expand Down Expand Up @@ -195,15 +188,15 @@ class GeneralDiagnosticsDelegate : public DeviceLayer::ConnectivityManagerDelega
}

// Gets called when the device has been rebooted.
void OnDeviceRebooted(chip::DeviceLayer::BootReasonType bootReason) override
void OnDeviceRebooted(BootReasonType bootReason) override
{
ChipLogDetail(Zcl, "GeneralDiagnosticsDelegate: OnDeviceRebooted");

ReportAttributeOnAllEndpoints(GeneralDiagnostics::Attributes::BootReasons::Id);

// GeneralDiagnostics cluster should exist only for endpoint 0.

Events::BootReason::Type event{ static_cast<EmberAfBootReasonType>(bootReason) };
Events::BootReason::Type event{ bootReason };
EventNumber eventNumber;

CHIP_ERROR err = LogEvent(event, 0, eventNumber);
Expand Down
1 change: 0 additions & 1 deletion src/app/zap-templates/templates/app/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,6 @@ function isWeaklyTypedEnum(label)
"AttributeWritePermission",
"BarrierControlBarrierPosition",
"BarrierControlMovingState",
"BootReasonType",
"ColorControlOptions",
"ColorLoopAction",
"ColorLoopDirection",
Expand Down
16 changes: 4 additions & 12 deletions src/include/platform/DiagnosticDataProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#pragma once

#include <app-common/zap-generated/cluster-objects.h>
#include <lib/core/ClusterEnums.h>
#include <platform/CHIPDeviceBuildConfig.h>
#include <platform/GeneralFaults.h>

Expand All @@ -41,16 +42,7 @@ constexpr size_t kMaxIPv6AddrSize = 16;
constexpr size_t kMaxIPv4AddrCount = 4;
constexpr size_t kMaxIPv6AddrCount = 8;

enum BootReasonType : uint8_t
{
Unspecified = 0,
PowerOnReboot = 1,
BrownOutReset = 2,
SoftwareWatchdogReset = 3,
HardwareWatchdogReset = 4,
SoftwareUpdateCompleted = 5,
SoftwareReset = 6,
};
using BootReasonType = app::Clusters::GeneralDiagnostics::BootReasonType;

struct ThreadMetrics : public app::Clusters::SoftwareDiagnostics::Structs::ThreadMetrics::Type
{
Expand Down Expand Up @@ -168,7 +160,7 @@ class DiagnosticDataProvider
virtual CHIP_ERROR GetRebootCount(uint16_t & rebootCount);
virtual CHIP_ERROR GetUpTime(uint64_t & upTime);
virtual CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours);
virtual CHIP_ERROR GetBootReason(uint8_t & bootReason);
virtual CHIP_ERROR GetBootReason(BootReasonType & bootReason);
virtual CHIP_ERROR GetActiveHardwareFaults(GeneralFaults<kMaxHardwareFaults> & hardwareFaults);
virtual CHIP_ERROR GetActiveRadioFaults(GeneralFaults<kMaxRadioFaults> & radioFaults);
virtual CHIP_ERROR GetActiveNetworkFaults(GeneralFaults<kMaxNetworkFaults> & networkFaults);
Expand Down Expand Up @@ -296,7 +288,7 @@ inline CHIP_ERROR DiagnosticDataProvider::GetTotalOperationalHours(uint32_t & to
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}

inline CHIP_ERROR DiagnosticDataProvider::GetBootReason(uint8_t & bootReason)
inline CHIP_ERROR DiagnosticDataProvider::GetBootReason(BootReasonType & bootReason)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}
Expand Down
4 changes: 2 additions & 2 deletions src/include/platform/internal/GenericPlatformManagerImpl.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ void GenericPlatformManagerImpl<ImplClass>::_HandleServerStarted()

if (generalDiagnosticsDelegate != nullptr)
{
uint8_t bootReason;
BootReasonType bootReason;

if (GetDiagnosticDataProvider().GetBootReason(bootReason) == CHIP_NO_ERROR)
generalDiagnosticsDelegate->OnDeviceRebooted(static_cast<BootReasonType>(bootReason));
generalDiagnosticsDelegate->OnDeviceRebooted(bootReason);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/platform/Ameba/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ CHIP_ERROR ConfigurationManagerImpl::Init()

if (!AmebaConfig::ConfigValueExists(AmebaConfig::kCounterKey_BootReason))
{
err = StoreBootReason(BootReasonType::Unspecified);
err = StoreBootReason(to_underlying(BootReasonType::kUnspecified));
SuccessOrExit(err);
}

Expand Down
4 changes: 2 additions & 2 deletions src/platform/Ameba/DiagnosticDataProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetTotalOperationalHours(uint32_t & total
return CHIP_ERROR_INVALID_TIME;
}

CHIP_ERROR DiagnosticDataProviderImpl::GetBootReason(uint8_t & bootReason)
CHIP_ERROR DiagnosticDataProviderImpl::GetBootReason(BootReasonType & bootReason)
{
uint32_t reason = 0;

Expand All @@ -112,7 +112,7 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetBootReason(uint8_t & bootReason)
if (err == CHIP_NO_ERROR)
{
VerifyOrReturnError(reason <= UINT8_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE);
bootReason = static_cast<uint8_t>(reason);
bootReason = static_cast<BootReasonType>(reason);
}

return err;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/Ameba/DiagnosticDataProviderImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class DiagnosticDataProviderImpl : public DiagnosticDataProvider
CHIP_ERROR GetRebootCount(uint16_t & rebootCount) override;
CHIP_ERROR GetUpTime(uint64_t & upTime) override;
CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override;
CHIP_ERROR GetBootReason(uint8_t & bootReason) override;
CHIP_ERROR GetBootReason(BootReasonType & bootReason) override;

CHIP_ERROR GetNetworkInterfaces(NetworkInterface ** netifpp) override;
void ReleaseNetworkInterfaces(NetworkInterface * netifp) override;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/Darwin/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ CHIP_ERROR ConfigurationManagerImpl::Init()

if (!PosixConfig::ConfigValueExists(PosixConfig::kCounterKey_BootReason))
{
ReturnErrorOnFailure(StoreBootReason(BootReasonType::Unspecified));
ReturnErrorOnFailure(StoreBootReason(to_underlying(BootReasonType::kUnspecified)));
}

if (!PosixConfig::ConfigValueExists(PosixConfig::kConfigKey_RegulatoryLocation))
Expand Down
26 changes: 13 additions & 13 deletions src/platform/EFR32/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,57 +107,57 @@ CHIP_ERROR ConfigurationManagerImpl::IncreaseBootCount(void)
uint32_t ConfigurationManagerImpl::GetBootReason(void)
{
// rebootCause is obtained at bootup.
uint32_t matterBootCause;
BootReasonType matterBootCause;
#if defined(_SILICON_LABS_32B_SERIES_1)
if (rebootCause & RMU_RSTCAUSE_PORST || rebootCause & RMU_RSTCAUSE_EXTRST) // PowerOn or External pin reset
{
matterBootCause = BootReasonType::PowerOnReboot;
matterBootCause = BootReasonType::kPowerOnReboot;
}
else if (rebootCause & RMU_RSTCAUSE_AVDDBOD || rebootCause & RMU_RSTCAUSE_DVDDBOD || rebootCause & RMU_RSTCAUSE_DECBOD)
{
matterBootCause = BootReasonType::BrownOutReset;
matterBootCause = BootReasonType::kBrownOutReset;
}
else if (rebootCause & RMU_RSTCAUSE_SYSREQRST)
{
matterBootCause = BootReasonType::SoftwareReset;
matterBootCause = BootReasonType::kSoftwareReset;
}
else if (rebootCause & RMU_RSTCAUSE_WDOGRST)
{
matterBootCause = BootReasonType::SoftwareWatchdogReset;
matterBootCause = BootReasonType::kSoftwareWatchdogReset;
}
else
{
matterBootCause = BootReasonType::Unspecified;
matterBootCause = BootReasonType::kUnspecified;
}
// Not tracked HARDWARE_WATCHDOG_RESET && SOFTWARE_UPDATE_COMPLETED
#elif defined(_SILICON_LABS_32B_SERIES_2)
if (rebootCause & EMU_RSTCAUSE_POR || rebootCause & EMU_RSTCAUSE_PIN) // PowerOn or External pin reset
{
matterBootCause = BootReasonType::PowerOnReboot;
matterBootCause = BootReasonType::kPowerOnReboot;
}
else if (rebootCause & EMU_RSTCAUSE_AVDDBOD || rebootCause & EMU_RSTCAUSE_DVDDBOD || rebootCause & EMU_RSTCAUSE_DECBOD ||
rebootCause & EMU_RSTCAUSE_VREGIN || rebootCause & EMU_RSTCAUSE_IOVDD0BOD || rebootCause & EMU_RSTCAUSE_DVDDLEBOD)
{
matterBootCause = BootReasonType::BrownOutReset;
matterBootCause = BootReasonType::kBrownOutReset;
}
else if (rebootCause & EMU_RSTCAUSE_SYSREQ)
{
matterBootCause = BootReasonType::SoftwareReset;
matterBootCause = BootReasonType::kSoftwareReset;
}
else if (rebootCause & EMU_RSTCAUSE_WDOG0 || rebootCause & EMU_RSTCAUSE_WDOG1)
{
matterBootCause = BootReasonType::SoftwareWatchdogReset;
matterBootCause = BootReasonType::kSoftwareWatchdogReset;
}
else
{
matterBootCause = BootReasonType::Unspecified;
matterBootCause = BootReasonType::kUnspecified;
}
// Not tracked HARDWARE_WATCHDOG_RESET && SOFTWARE_UPDATE_COMPLETED
#else
matterBootCause = BootReasonType::Unspecified;
matterBootCause = BootReasonType::kUnspecified;
#endif

return matterBootCause;
return to_underlying(matterBootCause);
}

CHIP_ERROR ConfigurationManagerImpl::GetTotalOperationalHours(uint32_t & totalOperationalHours)
Expand Down
4 changes: 2 additions & 2 deletions src/platform/EFR32/DiagnosticDataProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetRebootCount(uint16_t & rebootCount)
return err;
}

CHIP_ERROR DiagnosticDataProviderImpl::GetBootReason(uint8_t & bootReason)
CHIP_ERROR DiagnosticDataProviderImpl::GetBootReason(BootReasonType & bootReason)
{
uint32_t reason = 0;
CHIP_ERROR err = ConfigurationMgr().GetBootReason(reason);

if (err == CHIP_NO_ERROR)
{
VerifyOrReturnError(reason <= UINT8_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE);
bootReason = static_cast<uint8_t>(reason);
bootReason = static_cast<BootReasonType>(reason);
}

return err;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/EFR32/DiagnosticDataProviderImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class DiagnosticDataProviderImpl : public DiagnosticDataProvider
CHIP_ERROR GetCurrentHeapUsed(uint64_t & currentHeapUsed) override;
CHIP_ERROR GetCurrentHeapHighWatermark(uint64_t & currentHeapHighWatermark) override;
CHIP_ERROR GetRebootCount(uint16_t & rebootCount) override;
CHIP_ERROR GetBootReason(uint8_t & bootReason) override;
CHIP_ERROR GetBootReason(BootReasonType & bootReason) override;
CHIP_ERROR GetUpTime(uint64_t & upTime) override;
CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override;
CHIP_ERROR GetActiveHardwareFaults(GeneralFaults<kMaxHardwareFaults> & hardwareFaults) override;
Expand Down
14 changes: 7 additions & 7 deletions src/platform/ESP32/DiagnosticDataProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,30 +165,30 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetTotalOperationalHours(uint32_t & total
return CHIP_ERROR_INVALID_TIME;
}

CHIP_ERROR DiagnosticDataProviderImpl::GetBootReason(uint8_t & bootReason)
CHIP_ERROR DiagnosticDataProviderImpl::GetBootReason(BootReasonType & bootReason)
{
bootReason = BootReasonType::Unspecified;
bootReason = BootReasonType::kUnspecified;
uint8_t reason;
reason = static_cast<uint8_t>(esp_reset_reason());
if (reason == ESP_RST_UNKNOWN)
{
bootReason = BootReasonType::Unspecified;
bootReason = BootReasonType::kUnspecified;
}
else if (reason == ESP_RST_POWERON)
{
bootReason = BootReasonType::PowerOnReboot;
bootReason = BootReasonType::kPowerOnReboot;
}
else if (reason == ESP_RST_BROWNOUT)
{
bootReason = BootReasonType::BrownOutReset;
bootReason = BootReasonType::kBrownOutReset;
}
else if (reason == ESP_RST_SW)
{
bootReason = BootReasonType::SoftwareReset;
bootReason = BootReasonType::kSoftwareReset;
}
else if (reason == ESP_RST_INT_WDT)
{
bootReason = BootReasonType::SoftwareWatchdogReset;
bootReason = BootReasonType::kSoftwareWatchdogReset;
/* Reboot can be due to hardware or software watchdog*/
}
return CHIP_NO_ERROR;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/ESP32/DiagnosticDataProviderImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class DiagnosticDataProviderImpl : public DiagnosticDataProvider
CHIP_ERROR GetRebootCount(uint16_t & rebootCount) override;
CHIP_ERROR GetUpTime(uint64_t & upTime) override;
CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override;
CHIP_ERROR GetBootReason(uint8_t & bootReason) override;
CHIP_ERROR GetBootReason(BootReasonType & bootReason) override;

CHIP_ERROR GetNetworkInterfaces(NetworkInterface ** netifpp) override;
void ReleaseNetworkInterfaces(NetworkInterface * netifp) override;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/Linux/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ CHIP_ERROR ConfigurationManagerImpl::Init()

if (!PosixConfig::ConfigValueExists(PosixConfig::kCounterKey_BootReason))
{
err = StoreBootReason(BootReasonType::Unspecified);
err = StoreBootReason(to_underlying(BootReasonType::kUnspecified));
SuccessOrExit(err);
}

Expand Down
4 changes: 2 additions & 2 deletions src/platform/Linux/DiagnosticDataProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetTotalOperationalHours(uint32_t & total
return CHIP_ERROR_INVALID_TIME;
}

CHIP_ERROR DiagnosticDataProviderImpl::GetBootReason(uint8_t & bootReason)
CHIP_ERROR DiagnosticDataProviderImpl::GetBootReason(BootReasonType & bootReason)
{
uint32_t reason = 0;

Expand All @@ -377,7 +377,7 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetBootReason(uint8_t & bootReason)
if (err == CHIP_NO_ERROR)
{
VerifyOrReturnError(reason <= UINT8_MAX, CHIP_ERROR_INVALID_INTEGER_VALUE);
bootReason = static_cast<uint8_t>(reason);
bootReason = static_cast<BootReasonType>(reason);
}

return err;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/Linux/DiagnosticDataProviderImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class DiagnosticDataProviderImpl : public DiagnosticDataProvider
CHIP_ERROR GetRebootCount(uint16_t & rebootCount) override;
CHIP_ERROR GetUpTime(uint64_t & upTime) override;
CHIP_ERROR GetTotalOperationalHours(uint32_t & totalOperationalHours) override;
CHIP_ERROR GetBootReason(uint8_t & bootReason) override;
CHIP_ERROR GetBootReason(BootReasonType & bootReason) override;

CHIP_ERROR GetActiveHardwareFaults(GeneralFaults<kMaxHardwareFaults> & hardwareFaults) override;
CHIP_ERROR GetActiveRadioFaults(GeneralFaults<kMaxRadioFaults> & radioFaults) override;
Expand Down
Loading

0 comments on commit 3729733

Please sign in to comment.