Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport v2.9-branch] [nrf fromtree] Add factory reset event #575

Merged
merged 1 commit into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,22 @@ void Server::GenerateShutDownEvent()
PlatformMgr().ScheduleWork([](intptr_t) { PlatformMgr().HandleServerShuttingDown(); });
}

void Server::PostFactoryResetEvent()
{
DeviceLayer::ChipDeviceEvent event;
event.Type = DeviceLayer::DeviceEventType::kFactoryReset;

CHIP_ERROR error = DeviceLayer::PlatformMgr().PostEvent(&event);
if (error != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "Posting kFactoryReset event failed with %" CHIP_ERROR_FORMAT, error.Format());
}
}

void Server::ScheduleFactoryReset()
{
PostFactoryResetEvent();

PlatformMgr().ScheduleWork([](intptr_t) {
// Delete all fabrics and emit Leave event.
GetInstance().GetFabricTable().DeleteAllFabrics();
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 @@ -453,6 +453,8 @@ class Server
void OnPlatformEvent(const DeviceLayer::ChipDeviceEvent & event);
void CheckServerReadyEvent();

void PostFactoryResetEvent();

static void OnPlatformEventWrapper(const DeviceLayer::ChipDeviceEvent * event, intptr_t);

#if CHIP_CONFIG_PERSIST_SUBSCRIPTIONS
Expand Down
1 change: 1 addition & 0 deletions src/app/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ chip_test_suite("tests") {
"TestReadInteraction.cpp",
"TestReportScheduler.cpp",
"TestReportingEngine.cpp",
"TestServer.cpp",
"TestStatusIB.cpp",
"TestStatusResponseMessage.cpp",
"TestTestEventTriggerDelegate.cpp",
Expand Down
74 changes: 74 additions & 0 deletions src/app/tests/TestServer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
*
* Copyright (c) 2025 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <pw_unit_test/framework.h>

#include <app/server/Server.h>
#include <platform/CHIPDeviceLayer.h>

namespace chip {

using namespace chip::DeviceLayer;

namespace app {
namespace server {

class TestServer : public ::testing::Test
{
public:
static void SetUpTestSuite()
{
ASSERT_EQ(Platform::MemoryInit(), CHIP_NO_ERROR);
ASSERT_EQ(PlatformMgr().InitChipStack(), CHIP_NO_ERROR);
}
static void TearDownTestSuite()
{
Platform::MemoryShutdown();
PlatformMgr().Shutdown();
}
};

class TestEventHandler
{
public:
ChipDeviceEvent mEvent{};

static void EventHandler(const ChipDeviceEvent * event, intptr_t arg)
{
reinterpret_cast<TestEventHandler *>(arg)->mEvent = *event;
}
};

TEST_F(TestServer, TestFactoryResetEvent)
{
TestEventHandler handler;
PlatformMgr().AddEventHandler(TestEventHandler::EventHandler, reinterpret_cast<intptr_t>(&handler));

Server::GetInstance().ScheduleFactoryReset();

PlatformMgr().ScheduleWork([](intptr_t) -> void { PlatformMgr().StopEventLoopTask(); });
PlatformMgr().RunEventLoop();

EXPECT_EQ(handler.mEvent.Type, DeviceEventType::kFactoryReset);

PlatformMgr().RemoveEventHandler(TestEventHandler::EventHandler, reinterpret_cast<intptr_t>(&handler));
}

} // namespace server
} // namespace app
} // namespace chip
5 changes: 5 additions & 0 deletions src/include/platform/CHIPDeviceEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ enum PublicEventTypes
* Signals that secure session is established.
*/
kSecureSessionEstablished,

/**
* Signals that factory reset has started.
*/
kFactoryReset,
};

/**
Expand Down
54 changes: 49 additions & 5 deletions src/platform/fake/PlatformManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

#pragma once

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

#include <algorithm>
#include <list>
#include <queue>

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

struct EventHandler
{
PlatformManager::EventHandlerFunct Handler;
intptr_t Arg;

bool operator==(const EventHandler & other) const { return Handler == other.Handler && Arg == other.Arg; }
};

CHIP_ERROR _InitChipStack() { return CHIP_NO_ERROR; }
void _Shutdown() {}

CHIP_ERROR _AddEventHandler(EventHandlerFunct handler, intptr_t arg = 0) { return CHIP_ERROR_NOT_IMPLEMENTED; }
void _RemoveEventHandler(EventHandlerFunct handler, intptr_t arg = 0) {}
CHIP_ERROR _AddEventHandler(EventHandlerFunct handler, intptr_t arg = 0)
{
EventHandler eventHandler = { .Handler = handler, .Arg = arg };
if (std::find(mEventHandlers.begin(), mEventHandlers.end(), eventHandler) == mEventHandlers.end())
{
mEventHandlers.push_back(eventHandler);
}

return CHIP_NO_ERROR;
}

void _RemoveEventHandler(EventHandlerFunct handler, intptr_t arg = 0)
{
EventHandler eventHandler = { .Handler = handler, .Arg = arg };
mEventHandlers.remove(eventHandler);
}

void _HandleServerStarted() {}
void _HandleServerShuttingDown() {}

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

default:
break;
default: {
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
BLEMgr().OnPlatformEvent(event);
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
ThreadStackMgr().OnPlatformEvent(event);
#endif
ConnectivityMgr().OnPlatformEvent(event);

if (!event->IsInternal())
{
// iterate over local copy in case handler unregisters itself
auto handlers = mEventHandlers;
for (auto & handler : handlers)
{
handler.Handler(event, handler.Arg);
}
}
}
break;
}
}

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

bool mShouldRunEventLoop = true;
std::queue<ChipDeviceEvent> mQueue;
std::list<EventHandler> mEventHandlers;
};

/**
Expand Down
4 changes: 4 additions & 0 deletions src/test_driver/nrfconnect/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,7 @@ CONFIG_CHIP_ENABLE_READ_CLIENT=y
CONFIG_CHIP_DEVICE_VENDOR_ID=65521
CONFIG_CHIP_DEVICE_PRODUCT_ID=32768
CONFIG_CHIP_PROJECT_CONFIG="main/include/CHIPProjectConfig.h"

# Don't erase all settings as it deinitializes NVS/ZMS and causes issues with
# testing Server::ScheduleFactoryReset
CONFIG_CHIP_FACTORY_RESET_ERASE_SETTINGS=n
Loading