Skip to content

Commit

Permalink
[events] Added generating BootReason event
Browse files Browse the repository at this point in the history
Zephyr platform doesn't support generating
all mandatory events from Basic and General
Diagnostics clusters.

Generic changes:
* Added generating BootReason event
* Added Server::FactoryReset method to dispatch
necessary events and flush pending events before
factory reset
* Added dispatching shutdown event on factory
reset
* Added DeleteAllFabrics in FactoryReset to emit
Leave event.
* Added flushing events on factory reset
* Added for all examples to call Server FactoryReset
instead of platform InitiateFactoryReset

Zephyr platform changes:
* Fixed bug in persisted storage read
implementation that returned wrong error code
and it resulted in PersistedCounter initialization
failure.
* Added calling Shutdown after factory reset

WIP
  • Loading branch information
kkasperczyk-no committed Feb 22, 2022
1 parent e71be1c commit a1d2f56
Show file tree
Hide file tree
Showing 33 changed files with 79 additions and 34 deletions.
2 changes: 1 addition & 1 deletion examples/all-clusters-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ class SetupListModel : public TouchesMatterStackModel
}
else if (i == 1)
{
ConfigurationMgr().InitiateFactoryReset();
chip::Server::GetInstance().FactoryReset();
}
else if (i == 2)
{
Expand Down
2 changes: 1 addition & 1 deletion examples/light-switch-app/efr32/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ void AppTask::FunctionTimerEventHandler(AppEvent * aEvent)
{
// Actually trigger Factory Reset
sAppTask.mFunction = kFunction_NoneSelected;
ConfigurationMgr().InitiateFactoryReset();
chip::Server::GetInstance().FactoryReset();
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/lighting-app/efr32/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ void AppTask::FunctionTimerEventHandler(AppEvent * aEvent)
{
// Actually trigger Factory Reset
sAppTask.mFunction = kFunction_NoneSelected;
ConfigurationMgr().InitiateFactoryReset();
chip::Server::GetInstance().FactoryReset();
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/lighting-app/mbed/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ void AppTask::FunctionTimerEventHandler(AppEvent * aEvent)
// Actually trigger Factory Reset
ChipLogProgress(NotSpecified, "Factory Reset initiated");
sAppTask.mFunction = kFunction_NoneSelected;
ConfigurationMgr().InitiateFactoryReset();
chip::Server::GetInstance().FactoryReset();
}
}

Expand Down
3 changes: 2 additions & 1 deletion examples/lighting-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ void AppTask::FunctionTimerEventHandler(AppEvent * aEvent)
{
// Actually trigger Factory Reset
sAppTask.mFunction = kFunction_NoneSelected;
ConfigurationMgr().InitiateFactoryReset();

chip::Server::GetInstance().FactoryReset();
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/lighting-app/nxp/k32w/k32w0/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ void AppTask::FunctionTimerEventHandler(AppEvent * aEvent)
K32W_LOG("Device will factory reset...");

// Actually trigger Factory Reset
ConfigurationMgr().InitiateFactoryReset();
chip::Server::GetInstance().FactoryReset();
}

void AppTask::ResetActionEventHandler(AppEvent * aEvent)
Expand Down
2 changes: 1 addition & 1 deletion examples/lighting-app/p6/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ void AppTask::FunctionTimerEventHandler(AppEvent * aEvent)
{
// Actually trigger Factory Reset
sAppTask.mFunction = Function::kNoneSelected;
ConfigurationMgr().InitiateFactoryReset();
chip::Server::GetInstance().FactoryReset();
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/lighting-app/qpg/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ void AppTask::FunctionTimerEventHandler(AppEvent * aEvent)
{
// Actually trigger Factory Reset
sAppTask.mFunction = kFunction_NoneSelected;
ConfigurationMgr().InitiateFactoryReset();
chip::Server::GetInstance().FactoryReset();
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/lighting-app/telink/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ void AppTask::FactoryResetButtonEventHandler(void)
void AppTask::FactoryResetHandler(AppEvent * aEvent)
{
LOG_INF("Factory Reset triggered.");
ConfigurationMgr().InitiateFactoryReset();
chip::Server::GetInstance().FactoryReset();
}

void AppTask::StartThreadButtonEventHandler(void)
Expand Down
2 changes: 1 addition & 1 deletion examples/lock-app/efr32/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ void AppTask::FunctionTimerEventHandler(AppEvent * aEvent)
{
// Actually trigger Factory Reset
sAppTask.mFunction = kFunction_NoneSelected;
ConfigurationMgr().InitiateFactoryReset();
chip::Server::GetInstance().FactoryReset();
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/lock-app/esp32/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ void AppTask::FunctionTimerEventHandler(AppEvent * aEvent)
{
// Actually trigger Factory Reset
sAppTask.mFunction = kFunction_NoneSelected;
ConfigurationMgr().InitiateFactoryReset();
chip::Server::GetInstance().FactoryReset();
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/lock-app/mbed/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ void AppTask::FunctionTimerEventHandler(AppEvent * aEvent)
// Actually trigger Factory Reset
ChipLogProgress(NotSpecified, "Factory Reset initiated");
sAppTask.mFunction = kFunction_NoneSelected;
ConfigurationMgr().InitiateFactoryReset();
chip::Server::GetInstance().FactoryReset();
}
}

Expand Down
3 changes: 2 additions & 1 deletion examples/lock-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ void AppTask::FunctionTimerEventHandler(AppEvent * aEvent)
{
// Actually trigger Factory Reset
sAppTask.mFunction = kFunction_NoneSelected;
ConfigurationMgr().InitiateFactoryReset();

chip::Server::GetInstance().FactoryReset();
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/lock-app/nxp/k32w/k32w0/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ void AppTask::FunctionTimerEventHandler(void * aGenericEvent)
K32W_LOG("Device will factory reset...");

// Actually trigger Factory Reset
ConfigurationMgr().InitiateFactoryReset();
chip::Server::GetInstance().FactoryReset();
}

void AppTask::ResetActionEventHandler(void * aGenericEvent)
Expand Down
2 changes: 1 addition & 1 deletion examples/lock-app/p6/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ void AppTask::FunctionTimerEventHandler(AppEvent * event)
{
// Actually trigger Factory Reset
sAppTask.mFunction = Function::kNoneSelected;
ConfigurationMgr().InitiateFactoryReset();
chip::Server::GetInstance().FactoryReset();
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/lock-app/qpg/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ void AppTask::FunctionTimerEventHandler(AppEvent * aEvent)
{
// Actually trigger Factory Reset
sAppTask.mFunction = kFunction_NoneSelected;
ConfigurationMgr().InitiateFactoryReset();
chip::Server::GetInstance().FactoryReset();
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/ota-requestor-app/efr32/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ void AppTask::FunctionTimerEventHandler(AppEvent * aEvent)
{
// Actually trigger Factory Reset
sAppTask.mFunction = kFunction_NoneSelected;
ConfigurationMgr().InitiateFactoryReset();
chip::Server::GetInstance().FactoryReset();
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/ota-requestor-app/mbed/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ void AppTask::FunctionTimerEventHandler(AppEvent * aEvent)
sAppTask.CancelTimer(kFunction_Button_1);
sAppTask.mFunction[kFunction_Button_1] = kFunction_NoneSelected;

ConfigurationMgr().InitiateFactoryReset();
chip::Server::GetInstance().FactoryReset();
}
break;
case kFunction_Button_2:
Expand Down
2 changes: 1 addition & 1 deletion examples/pump-app/cc13x2x7_26x2x7/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ void AppTask::DispatchEvent(AppEvent * aEvent)
}
else if (AppEvent::kAppEventButtonType_LongPressed == aEvent->ButtonEvent.Type)
{
ConfigurationMgr().InitiateFactoryReset();
chip::Server::GetInstance().FactoryReset();
}
break;

Expand Down
3 changes: 2 additions & 1 deletion examples/pump-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ void AppTask::FunctionTimerEventHandler(AppEvent * aEvent)
{
// Actually trigger Factory Reset
sAppTask.mFunction = kFunction_NoneSelected;
ConfigurationMgr().InitiateFactoryReset();

chip::Server::GetInstance().FactoryReset();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ void AppTask::DispatchEvent(AppEvent * aEvent)
}
else if (AppEvent::kAppEventButtonType_LongPressed == aEvent->ButtonEvent.Type)
{
ConfigurationMgr().InitiateFactoryReset();
chip::Server::GetInstance().FactoryReset();
}
break;

Expand Down
3 changes: 2 additions & 1 deletion examples/pump-controller-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ void AppTask::FunctionTimerEventHandler(AppEvent * aEvent)
{
// Actually trigger Factory Reset
sAppTask.mFunction = kFunction_NoneSelected;
ConfigurationMgr().InitiateFactoryReset();

chip::Server::GetInstance().FactoryReset();
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/shell/nxp/k32w/k32w0/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ void AppTask::FunctionTimerEventHandler(AppEvent * aEvent)
K32W_LOG("Device will factory reset...");

// Actually trigger Factory Reset
ConfigurationMgr().InitiateFactoryReset();
chip::Server::GetInstance().FactoryReset();
}

void AppTask::ResetActionEventHandler(AppEvent * aEvent)
Expand Down
2 changes: 1 addition & 1 deletion examples/window-app/common/src/WindowApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ void WindowApp::DispatchEvent(const WindowApp::Event & event)
break;

case EventId::Reset:
ConfigurationMgr().InitiateFactoryReset();
chip::Server::GetInstance().FactoryReset();
break;

case EventId::UpPressed:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,23 @@ class GeneralDiagnosticsDelegate : public DeviceLayer::ConnectivityManagerDelega
}

// Gets called when the device has been rebooted.
void OnDeviceRebooted() override
void OnDeviceRebooted(uint8_t bootReason) override
{
ChipLogProgress(Zcl, "GeneralDiagnosticsDelegate: OnDeviceRebooted");

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

for (auto endpoint : EnabledEndpointsWithServerCluster(GeneralDiagnostics::Id))
{
// If GeneralDiagnostics cluster is implemented on this endpoint
Events::BootReason::Type event{ static_cast<BootReasonType>(bootReason) };
EventNumber eventNumber;

if (CHIP_NO_ERROR != LogEvent(event, endpoint, eventNumber, EventOptions::Type::kUrgent))
{
ChipLogError(Zcl, "GeneralDiagnosticsDelegate: Failed to record BootReason event");
}
}
}

// Get called when the Node detects a hardware fault has been raised.
Expand Down
3 changes: 2 additions & 1 deletion src/app/reporting/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ class Engine
}
}

void ScheduleUrgentEventDeliverySync();

private:
friend class TestReportingEngine;
/**
Expand Down Expand Up @@ -163,7 +165,6 @@ class Engine
CHIP_ERROR ScheduleUrgentEventDelivery(ConcreteEventPath & aPath);
CHIP_ERROR ScheduleBufferPressureEventDelivery(uint32_t aBytesWritten);
void GetMinEventLogPosition(uint32_t & aMinLogPosition);
void ScheduleUrgentEventDeliverySync();

/**
* If the provided path is a superset of our of our existing paths, update that existing path to match the
Expand Down
14 changes: 14 additions & 0 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,20 @@ void Server::DispatchShutDownAndStopEventLoop()
chip::DeviceLayer::PlatformMgr().ScheduleWork(StopEventLoop);
}

void Server::FactoryReset()
{
// Delete all fabrics and emit Leave event.
GetFabricTable().DeleteAllFabrics();

// Emit Shutdown event, as shutdown will come after factory reset.
DispatchShutDownEvent(0);

// Flush all dispatched events.
chip::app::InteractionModelEngine::GetInstance()->GetReportingEngine().ScheduleUrgentEventDeliverySync();

chip::DeviceLayer::ConfigurationMgr().InitiateFactoryReset();
}

void Server::Shutdown()
{
chip::Dnssd::ServiceAdvertiser::Instance().Shutdown();
Expand Down
2 changes: 2 additions & 0 deletions src/app/server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ class Server
*/
void DispatchShutDownAndStopEventLoop();

void FactoryReset();

void Shutdown();

static Server & GetInstance() { return sServer; }
Expand Down
2 changes: 1 addition & 1 deletion src/include/platform/DiagnosticDataProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class GeneralDiagnosticsDelegate
* @brief
* Called after the current device is rebooted.
*/
virtual void OnDeviceRebooted() {}
virtual void OnDeviceRebooted(uint8_t bootReason) {}

/**
* @brief
Expand Down
5 changes: 4 additions & 1 deletion src/include/platform/internal/GenericPlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,10 @@ void GenericPlatformManagerImpl<ImplClass>::HandleDeviceRebooted(intptr_t arg)

if (generalDiagnosticsDelegate != nullptr)
{
generalDiagnosticsDelegate->OnDeviceRebooted();
uint8_t bootReason;

ReturnOnFailure(GetDiagnosticDataProvider().GetBootReason(bootReason));
generalDiagnosticsDelegate->OnDeviceRebooted(bootReason);
}

// The StartUp event SHALL be emitted by a Node after completing a boot or reboot process
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include <system/SystemError.h>
#include <system/SystemLayer.h>

#include <power/reboot.h>

#define DEFAULT_MIN_SLEEP_PERIOD (60 * 60 * 24 * 30) // Month [sec]

namespace chip {
Expand All @@ -46,6 +48,7 @@ System::LayerSocketsLoop & SystemLayerSocketsLoop()
{
return static_cast<System::LayerSocketsLoop &>(DeviceLayer::SystemLayer());
}

} // anonymous namespace

// Fully instantiate the generic implementation class in whatever compilation unit includes this file.
Expand Down Expand Up @@ -106,7 +109,12 @@ CHIP_ERROR GenericPlatformManagerImpl_Zephyr<ImplClass>::_StopEventLoopTask(void
template <class ImplClass>
CHIP_ERROR GenericPlatformManagerImpl_Zephyr<ImplClass>::_Shutdown(void)
{
#if CONFIG_REBOOT
sys_reboot(SYS_REBOOT_WARM);
return CHIP_NO_ERROR;
#else
return CHIP_ERROR_NOT_IMPLEMENTED;
#endif
}

template <class ImplClass>
Expand Down
6 changes: 1 addition & 5 deletions src/platform/Zephyr/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>

#include <power/reboot.h>

namespace chip {
namespace DeviceLayer {

Expand Down Expand Up @@ -204,9 +202,7 @@ void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)
ThreadStackMgr().ErasePersistentInfo();
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD

#if CONFIG_REBOOT
sys_reboot(SYS_REBOOT_WARM);
#endif
PlatformMgr().Shutdown();
}

} // namespace DeviceLayer
Expand Down
7 changes: 6 additions & 1 deletion src/platform/Zephyr/ConfigurationManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ inline bool ConfigurationManagerImpl::CanFactoryReset()

inline CHIP_ERROR ConfigurationManagerImpl::ReadPersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t & value)
{
return Internal::ZephyrConfig::ReadConfigValueCounter(key, value);
CHIP_ERROR err = Internal::ZephyrConfig::ReadConfigValueCounter(key, value);
if (err == CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND)
{
err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
}
return err;
}

inline CHIP_ERROR ConfigurationManagerImpl::WritePersistedStorageValue(::chip::Platform::PersistedStorage::Key key, uint32_t value)
Expand Down

0 comments on commit a1d2f56

Please sign in to comment.