Skip to content

Commit 6012384

Browse files
committed
Implement event handling for fake Linux target
Signed-off-by: Adrian Gielniewski <adrian.gielniewski@nordicsemi.no>
1 parent 444dfd5 commit 6012384

File tree

1 file changed

+52
-5
lines changed

1 file changed

+52
-5
lines changed

src/platform/fake/PlatformManagerImpl.h

+52-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,37 @@ 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+
void _RemoveEventHandler(EventHandlerFunct handler, intptr_t arg = 0)
71+
{
72+
EventHandler eventHandler = { .Handler = handler, .Arg = arg };
73+
74+
if (std::find(mEventHandlers.begin(), mEventHandlers.end(), eventHandler) != mEventHandlers.end())
75+
{
76+
mEventHandlers.remove(eventHandler);
77+
}
78+
}
79+
5280
void _HandleServerStarted() {}
5381
void _HandleServerShuttingDown() {}
5482

@@ -108,8 +136,26 @@ class PlatformManagerImpl final : public PlatformManager
108136
event->CallWorkFunct.WorkFunct(event->CallWorkFunct.Arg);
109137
break;
110138

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

@@ -135,6 +181,7 @@ class PlatformManagerImpl final : public PlatformManager
135181

136182
bool mShouldRunEventLoop = true;
137183
std::queue<ChipDeviceEvent> mQueue;
184+
std::list<EventHandler> mEventHandlers;
138185
};
139186

140187
/**

0 commit comments

Comments
 (0)