Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / include / platform / PlatformManager.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2018 Nest Labs, Inc.
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 /**
20  *    @file
21  *          Defines the public interface for the Device Layer PlatformManager object.
22  */
23
24 #pragma once
25
26 #include <platform/CHIPDeviceEvent.h>
27
28 namespace chip {
29 namespace System {
30 namespace Platform {
31 namespace Layer {
32
33 System::Error PostEvent(System::Layer & aLayer, void * aContext, System::Object & aTarget, System::EventType aType,
34                         uintptr_t aArgument);
35 System::Error DispatchEvents(System::Layer & aLayer, void * aContext);
36 System::Error DispatchEvent(System::Layer & aLayer, void * aContext, System::Event aEvent);
37 System::Error StartTimer(System::Layer & aLayer, void * aContext, uint32_t aMilliseconds);
38
39 } // namespace Layer
40 } // namespace Platform
41 } // namespace System
42
43 namespace DeviceLayer {
44
45 class PlatformManagerImpl;
46 class ConnectivityManagerImpl;
47 class ConfigurationManagerImpl;
48 class TraitManager;
49 class ThreadStackManagerImpl;
50 class TimeSyncManager;
51 namespace Internal {
52 class FabricProvisioningServer;
53 class ServiceProvisioningServer;
54 class BLEManagerImpl;
55 template <class>
56 class GenericConfigurationManagerImpl;
57 template <class>
58 class GenericPlatformManagerImpl;
59 template <class>
60 class GenericPlatformManagerImpl_FreeRTOS;
61 template <class>
62 class GenericPlatformManagerImpl_POSIX;
63 template <class>
64 class GenericPlatformManagerImpl_Zephyr;
65 template <class>
66 class GenericConnectivityManagerImpl_Thread;
67 template <class>
68 class GenericThreadStackManagerImpl_OpenThread;
69 template <class>
70 class GenericThreadStackManagerImpl_OpenThread_LwIP;
71 } // namespace Internal
72
73 /**
74  * Provides features for initializing and interacting with the chip network
75  * stack on a chip-enabled device.
76  */
77 class PlatformManager
78 {
79     using ImplClass = ::chip::DeviceLayer::PlatformManagerImpl;
80
81 public:
82     // ===== Members that define the public interface of the PlatformManager
83
84     typedef void (*EventHandlerFunct)(const ChipDeviceEvent * event, intptr_t arg);
85
86     CHIP_ERROR InitChipStack();
87     CHIP_ERROR AddEventHandler(EventHandlerFunct handler, intptr_t arg = 0);
88     void RemoveEventHandler(EventHandlerFunct handler, intptr_t arg = 0);
89     void ScheduleWork(AsyncWorkFunct workFunct, intptr_t arg = 0);
90     void RunEventLoop();
91     CHIP_ERROR StartEventLoopTask();
92     void LockChipStack();
93     bool TryLockChipStack();
94     void UnlockChipStack();
95     CHIP_ERROR Shutdown();
96
97 private:
98     // ===== Members for internal use by the following friends.
99
100     friend class PlatformManagerImpl;
101     friend class ConnectivityManagerImpl;
102     friend class ConfigurationManagerImpl;
103     friend class TraitManager;
104     friend class ThreadStackManagerImpl;
105     friend class TimeSyncManager;
106     friend class Internal::FabricProvisioningServer;
107     friend class Internal::ServiceProvisioningServer;
108     friend class Internal::BLEManagerImpl;
109     template <class>
110     friend class Internal::GenericPlatformManagerImpl;
111     template <class>
112     friend class Internal::GenericPlatformManagerImpl_FreeRTOS;
113     template <class>
114     friend class Internal::GenericPlatformManagerImpl_POSIX;
115     template <class>
116     friend class Internal::GenericPlatformManagerImpl_Zephyr;
117     template <class>
118     friend class Internal::GenericConnectivityManagerImpl_Thread;
119     template <class>
120     friend class Internal::GenericThreadStackManagerImpl_OpenThread;
121     template <class>
122     friend class Internal::GenericThreadStackManagerImpl_OpenThread_LwIP;
123     template <class>
124     friend class Internal::GenericConfigurationManagerImpl;
125     // Parentheses used to fix clang parsing issue with these declarations
126     friend ::chip::System::Error(::chip::System::Platform::Layer::PostEvent)(::chip::System::Layer & aLayer, void * aContext,
127                                                                              ::chip::System::Object & aTarget,
128                                                                              ::chip::System::EventType aType, uintptr_t aArgument);
129     friend ::chip::System::Error(::chip::System::Platform::Layer::DispatchEvents)(::chip::System::Layer & aLayer, void * aContext);
130     friend ::chip::System::Error(::chip::System::Platform::Layer::DispatchEvent)(::chip::System::Layer & aLayer, void * aContext,
131                                                                                  ::chip::System::Event aEvent);
132     friend ::chip::System::Error(::chip::System::Platform::Layer::StartTimer)(::chip::System::Layer & aLayer, void * aContext,
133                                                                               uint32_t aMilliseconds);
134
135     void PostEvent(const ChipDeviceEvent * event);
136     void DispatchEvent(const ChipDeviceEvent * event);
137     CHIP_ERROR StartChipTimer(uint32_t durationMS);
138
139 protected:
140     // Construction/destruction limited to subclasses.
141     PlatformManager()  = default;
142     ~PlatformManager() = default;
143
144     // No copy, move or assignment.
145     PlatformManager(const PlatformManager &)  = delete;
146     PlatformManager(const PlatformManager &&) = delete;
147     PlatformManager & operator=(const PlatformManager &) = delete;
148 };
149
150 /**
151  * Returns the public interface of the PlatformManager singleton object.
152  *
153  * chip applications should use this to access features of the PlatformManager object
154  * that are common to all platforms.
155  */
156 extern PlatformManager & PlatformMgr();
157
158 /**
159  * Returns the platform-specific implementation of the PlatformManager singleton object.
160  *
161  * chip applications can use this to gain access to features of the PlatformManager
162  * that are specific to the selected platform.
163  */
164 extern PlatformManagerImpl & PlatformMgrImpl();
165
166 } // namespace DeviceLayer
167 } // namespace chip
168
169 /* Include a header file containing the implementation of the ConfigurationManager
170  * object for the selected platform.
171  */
172 #ifdef EXTERNAL_PLATFORMMANAGERIMPL_HEADER
173 #include EXTERNAL_PLATFORMMANAGERIMPL_HEADER
174 #elif defined(CHIP_DEVICE_LAYER_TARGET)
175 #define PLATFORMMANAGERIMPL_HEADER <platform/CHIP_DEVICE_LAYER_TARGET/PlatformManagerImpl.h>
176 #include PLATFORMMANAGERIMPL_HEADER
177 #endif // defined(CHIP_DEVICE_LAYER_TARGET)
178
179 namespace chip {
180 namespace DeviceLayer {
181
182 inline CHIP_ERROR PlatformManager::InitChipStack()
183 {
184     return static_cast<ImplClass *>(this)->_InitChipStack();
185 }
186
187 inline CHIP_ERROR PlatformManager::AddEventHandler(EventHandlerFunct handler, intptr_t arg)
188 {
189     return static_cast<ImplClass *>(this)->_AddEventHandler(handler, arg);
190 }
191
192 inline void PlatformManager::RemoveEventHandler(EventHandlerFunct handler, intptr_t arg)
193 {
194     static_cast<ImplClass *>(this)->_RemoveEventHandler(handler, arg);
195 }
196
197 inline void PlatformManager::ScheduleWork(AsyncWorkFunct workFunct, intptr_t arg)
198 {
199     static_cast<ImplClass *>(this)->_ScheduleWork(workFunct, arg);
200 }
201
202 inline void PlatformManager::RunEventLoop()
203 {
204     static_cast<ImplClass *>(this)->_RunEventLoop();
205 }
206
207 inline CHIP_ERROR PlatformManager::StartEventLoopTask()
208 {
209     return static_cast<ImplClass *>(this)->_StartEventLoopTask();
210 }
211
212 inline void PlatformManager::LockChipStack()
213 {
214     static_cast<ImplClass *>(this)->_LockChipStack();
215 }
216
217 inline bool PlatformManager::TryLockChipStack()
218 {
219     return static_cast<ImplClass *>(this)->_TryLockChipStack();
220 }
221
222 inline void PlatformManager::UnlockChipStack()
223 {
224     static_cast<ImplClass *>(this)->_UnlockChipStack();
225 }
226
227 inline void PlatformManager::PostEvent(const ChipDeviceEvent * event)
228 {
229     static_cast<ImplClass *>(this)->_PostEvent(event);
230 }
231
232 inline void PlatformManager::DispatchEvent(const ChipDeviceEvent * event)
233 {
234     static_cast<ImplClass *>(this)->_DispatchEvent(event);
235 }
236
237 inline CHIP_ERROR PlatformManager::StartChipTimer(uint32_t durationMS)
238 {
239     return static_cast<ImplClass *>(this)->_StartChipTimer(durationMS);
240 }
241
242 inline CHIP_ERROR PlatformManager::Shutdown()
243 {
244     return static_cast<ImplClass *>(this)->_Shutdown();
245 }
246
247 } // namespace DeviceLayer
248 } // namespace chip