Skip to content

Commit cde93c0

Browse files
authored
Add factory reset event (#37993)
* Add factory reset event Add `kFactoryReset` event and post it at the beginning of `ScheduleFactoryReset`. This event may be useful to application as it provides clean way to perform additional cleanup steps during factory reset. Signed-off-by: Adrian Gielniewski <adrian.gielniewski@nordicsemi.no> * Add unit test for factory reset event Signed-off-by: Adrian Gielniewski <adrian.gielniewski@nordicsemi.no> * Implement event handling for fake Linux target Signed-off-by: Adrian Gielniewski <adrian.gielniewski@nordicsemi.no> --------- Signed-off-by: Adrian Gielniewski <adrian.gielniewski@nordicsemi.no>
1 parent eab5b79 commit cde93c0

File tree

7 files changed

+149
-5
lines changed

7 files changed

+149
-5
lines changed

src/app/server/Server.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,22 @@ void Server::GenerateShutDownEvent()
616616
PlatformMgr().ScheduleWork([](intptr_t) { PlatformMgr().HandleServerShuttingDown(); });
617617
}
618618

619+
void Server::PostFactoryResetEvent()
620+
{
621+
DeviceLayer::ChipDeviceEvent event;
622+
event.Type = DeviceLayer::DeviceEventType::kFactoryReset;
623+
624+
CHIP_ERROR error = DeviceLayer::PlatformMgr().PostEvent(&event);
625+
if (error != CHIP_NO_ERROR)
626+
{
627+
ChipLogError(AppServer, "Posting kFactoryReset event failed with %" CHIP_ERROR_FORMAT, error.Format());
628+
}
629+
}
630+
619631
void Server::ScheduleFactoryReset()
620632
{
633+
PostFactoryResetEvent();
634+
621635
PlatformMgr().ScheduleWork([](intptr_t) {
622636
// Delete all fabrics and emit Leave event.
623637
GetInstance().GetFabricTable().DeleteAllFabrics();

src/app/server/Server.h

+2
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ class Server
458458
void OnPlatformEvent(const DeviceLayer::ChipDeviceEvent & event);
459459
void CheckServerReadyEvent();
460460

461+
void PostFactoryResetEvent();
462+
461463
static void OnPlatformEventWrapper(const DeviceLayer::ChipDeviceEvent * event, intptr_t);
462464

463465
#if CHIP_CONFIG_PERSIST_SUBSCRIPTIONS

src/app/tests/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ chip_test_suite("tests") {
241241
"TestReadInteraction.cpp",
242242
"TestReportScheduler.cpp",
243243
"TestReportingEngine.cpp",
244+
"TestServer.cpp",
244245
"TestStatusIB.cpp",
245246
"TestStatusResponseMessage.cpp",
246247
"TestTestEventTriggerDelegate.cpp",

src/app/tests/TestServer.cpp

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
*
3+
* Copyright (c) 2025 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include <pw_unit_test/framework.h>
20+
21+
#include <app/server/Server.h>
22+
#include <platform/CHIPDeviceLayer.h>
23+
24+
namespace chip {
25+
26+
using namespace chip::DeviceLayer;
27+
28+
namespace app {
29+
namespace server {
30+
31+
class TestServer : public ::testing::Test
32+
{
33+
public:
34+
static void SetUpTestSuite()
35+
{
36+
ASSERT_EQ(Platform::MemoryInit(), CHIP_NO_ERROR);
37+
ASSERT_EQ(PlatformMgr().InitChipStack(), CHIP_NO_ERROR);
38+
}
39+
static void TearDownTestSuite()
40+
{
41+
Platform::MemoryShutdown();
42+
PlatformMgr().Shutdown();
43+
}
44+
};
45+
46+
class TestEventHandler
47+
{
48+
public:
49+
ChipDeviceEvent mEvent{};
50+
51+
static void EventHandler(const ChipDeviceEvent * event, intptr_t arg)
52+
{
53+
reinterpret_cast<TestEventHandler *>(arg)->mEvent = *event;
54+
}
55+
};
56+
57+
TEST_F(TestServer, TestFactoryResetEvent)
58+
{
59+
TestEventHandler handler;
60+
PlatformMgr().AddEventHandler(TestEventHandler::EventHandler, reinterpret_cast<intptr_t>(&handler));
61+
62+
Server::GetInstance().ScheduleFactoryReset();
63+
64+
PlatformMgr().ScheduleWork([](intptr_t) -> void { PlatformMgr().StopEventLoopTask(); });
65+
PlatformMgr().RunEventLoop();
66+
67+
EXPECT_EQ(handler.mEvent.Type, DeviceEventType::kFactoryReset);
68+
69+
PlatformMgr().RemoveEventHandler(TestEventHandler::EventHandler, reinterpret_cast<intptr_t>(&handler));
70+
}
71+
72+
} // namespace server
73+
} // namespace app
74+
} // namespace chip

src/include/platform/CHIPDeviceEvent.h

+5
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ enum PublicEventTypes
255255
* Signals that secure session is established.
256256
*/
257257
kSecureSessionEstablished,
258+
259+
/**
260+
* Signals that factory reset has started.
261+
*/
262+
kFactoryReset,
258263
};
259264

260265
/**

src/platform/fake/PlatformManagerImpl.h

+49-5
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222

2323
#pragma once
2424

25-
#include <platform/PlatformManager.h>
25+
#include <platform/CHIPDeviceLayer.h>
2626

27+
#include <algorithm>
28+
#include <list>
2729
#include <queue>
2830

2931
namespace chip {
@@ -44,11 +46,34 @@ class PlatformManagerImpl final : public PlatformManager
4446
private:
4547
// ===== Methods that implement the PlatformManager abstract interface.
4648

49+
struct EventHandler
50+
{
51+
PlatformManager::EventHandlerFunct Handler;
52+
intptr_t Arg;
53+
54+
bool operator==(const EventHandler & other) const { return Handler == other.Handler && Arg == other.Arg; }
55+
};
56+
4757
CHIP_ERROR _InitChipStack() { return CHIP_NO_ERROR; }
4858
void _Shutdown() {}
4959

50-
CHIP_ERROR _AddEventHandler(EventHandlerFunct handler, intptr_t arg = 0) { return CHIP_ERROR_NOT_IMPLEMENTED; }
51-
void _RemoveEventHandler(EventHandlerFunct handler, intptr_t arg = 0) {}
60+
CHIP_ERROR _AddEventHandler(EventHandlerFunct handler, intptr_t arg = 0)
61+
{
62+
EventHandler eventHandler = { .Handler = handler, .Arg = arg };
63+
if (std::find(mEventHandlers.begin(), mEventHandlers.end(), eventHandler) == mEventHandlers.end())
64+
{
65+
mEventHandlers.push_back(eventHandler);
66+
}
67+
68+
return CHIP_NO_ERROR;
69+
}
70+
71+
void _RemoveEventHandler(EventHandlerFunct handler, intptr_t arg = 0)
72+
{
73+
EventHandler eventHandler = { .Handler = handler, .Arg = arg };
74+
mEventHandlers.remove(eventHandler);
75+
}
76+
5277
void _HandleServerStarted() {}
5378
void _HandleServerShuttingDown() {}
5479

@@ -108,8 +133,26 @@ class PlatformManagerImpl final : public PlatformManager
108133
event->CallWorkFunct.WorkFunct(event->CallWorkFunct.Arg);
109134
break;
110135

111-
default:
112-
break;
136+
default: {
137+
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
138+
BLEMgr().OnPlatformEvent(event);
139+
#endif
140+
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
141+
ThreadStackMgr().OnPlatformEvent(event);
142+
#endif
143+
ConnectivityMgr().OnPlatformEvent(event);
144+
145+
if (!event->IsInternal())
146+
{
147+
// iterate over local copy in case handler unregisters itself
148+
auto handlers = mEventHandlers;
149+
for (auto & handler : handlers)
150+
{
151+
handler.Handler(event, handler.Arg);
152+
}
153+
}
154+
}
155+
break;
113156
}
114157
}
115158

@@ -135,6 +178,7 @@ class PlatformManagerImpl final : public PlatformManager
135178

136179
bool mShouldRunEventLoop = true;
137180
std::queue<ChipDeviceEvent> mQueue;
181+
std::list<EventHandler> mEventHandlers;
138182
};
139183

140184
/**

src/test_driver/nrfconnect/prj.conf

+4
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,7 @@ CONFIG_CHIP_ENABLE_READ_CLIENT=y
8383
CONFIG_CHIP_DEVICE_VENDOR_ID=65521
8484
CONFIG_CHIP_DEVICE_PRODUCT_ID=32768
8585
CONFIG_CHIP_PROJECT_CONFIG="main/include/CHIPProjectConfig.h"
86+
87+
# Don't erase all settings as it deinitializes NVS/ZMS and causes issues with
88+
# testing Server::ScheduleFactoryReset
89+
CONFIG_CHIP_FACTORY_RESET_ERASE_SETTINGS=n

0 commit comments

Comments
 (0)