22
22
23
23
#pragma once
24
24
25
- #include < platform/PlatformManager .h>
25
+ #include < platform/CHIPDeviceLayer .h>
26
26
27
+ #include < algorithm>
28
+ #include < list>
27
29
#include < queue>
28
30
29
31
namespace chip {
@@ -44,11 +46,37 @@ class PlatformManagerImpl final : public PlatformManager
44
46
private:
45
47
// ===== Methods that implement the PlatformManager abstract interface.
46
48
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
+
47
57
CHIP_ERROR _InitChipStack () { return CHIP_NO_ERROR; }
48
58
void _Shutdown () {}
49
59
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
+
52
80
void _HandleServerStarted () {}
53
81
void _HandleServerShuttingDown () {}
54
82
@@ -108,8 +136,26 @@ class PlatformManagerImpl final : public PlatformManager
108
136
event->CallWorkFunct .WorkFunct (event->CallWorkFunct .Arg );
109
137
break ;
110
138
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 ;
113
159
}
114
160
}
115
161
@@ -135,6 +181,7 @@ class PlatformManagerImpl final : public PlatformManager
135
181
136
182
bool mShouldRunEventLoop = true ;
137
183
std::queue<ChipDeviceEvent> mQueue ;
184
+ std::list<EventHandler> mEventHandlers ;
138
185
};
139
186
140
187
/* *
0 commit comments