3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2018 Nest Labs, Inc.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * Defines the public interface for the Device Layer PlatformManager object.
26 #include <platform/CHIPDeviceEvent.h>
33 System::Error PostEvent(System::Layer & aLayer, void * aContext, System::Object & aTarget, System::EventType aType,
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);
40 } // namespace Platform
43 namespace DeviceLayer {
45 class PlatformManagerImpl;
46 class ConnectivityManagerImpl;
47 class ConfigurationManagerImpl;
49 class ThreadStackManagerImpl;
50 class TimeSyncManager;
52 class FabricProvisioningServer;
53 class ServiceProvisioningServer;
56 class GenericConfigurationManagerImpl;
58 class GenericPlatformManagerImpl;
60 class GenericPlatformManagerImpl_FreeRTOS;
62 class GenericPlatformManagerImpl_POSIX;
64 class GenericPlatformManagerImpl_Zephyr;
66 class GenericConnectivityManagerImpl_Thread;
68 class GenericThreadStackManagerImpl_OpenThread;
70 class GenericThreadStackManagerImpl_OpenThread_LwIP;
71 } // namespace Internal
74 * Provides features for initializing and interacting with the chip network
75 * stack on a chip-enabled device.
79 using ImplClass = ::chip::DeviceLayer::PlatformManagerImpl;
82 // ===== Members that define the public interface of the PlatformManager
84 typedef void (*EventHandlerFunct)(const ChipDeviceEvent * event, intptr_t arg);
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);
91 CHIP_ERROR StartEventLoopTask();
93 bool TryLockChipStack();
94 void UnlockChipStack();
95 CHIP_ERROR Shutdown();
98 // ===== Members for internal use by the following friends.
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;
110 friend class Internal::GenericPlatformManagerImpl;
112 friend class Internal::GenericPlatformManagerImpl_FreeRTOS;
114 friend class Internal::GenericPlatformManagerImpl_POSIX;
116 friend class Internal::GenericPlatformManagerImpl_Zephyr;
118 friend class Internal::GenericConnectivityManagerImpl_Thread;
120 friend class Internal::GenericThreadStackManagerImpl_OpenThread;
122 friend class Internal::GenericThreadStackManagerImpl_OpenThread_LwIP;
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);
135 void PostEvent(const ChipDeviceEvent * event);
136 void DispatchEvent(const ChipDeviceEvent * event);
137 CHIP_ERROR StartChipTimer(uint32_t durationMS);
140 // Construction/destruction limited to subclasses.
141 PlatformManager() = default;
142 ~PlatformManager() = default;
144 // No copy, move or assignment.
145 PlatformManager(const PlatformManager &) = delete;
146 PlatformManager(const PlatformManager &&) = delete;
147 PlatformManager & operator=(const PlatformManager &) = delete;
151 * Returns the public interface of the PlatformManager singleton object.
153 * chip applications should use this to access features of the PlatformManager object
154 * that are common to all platforms.
156 extern PlatformManager & PlatformMgr();
159 * Returns the platform-specific implementation of the PlatformManager singleton object.
161 * chip applications can use this to gain access to features of the PlatformManager
162 * that are specific to the selected platform.
164 extern PlatformManagerImpl & PlatformMgrImpl();
166 } // namespace DeviceLayer
169 /* Include a header file containing the implementation of the ConfigurationManager
170 * object for the selected platform.
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)
180 namespace DeviceLayer {
182 inline CHIP_ERROR PlatformManager::InitChipStack()
184 return static_cast<ImplClass *>(this)->_InitChipStack();
187 inline CHIP_ERROR PlatformManager::AddEventHandler(EventHandlerFunct handler, intptr_t arg)
189 return static_cast<ImplClass *>(this)->_AddEventHandler(handler, arg);
192 inline void PlatformManager::RemoveEventHandler(EventHandlerFunct handler, intptr_t arg)
194 static_cast<ImplClass *>(this)->_RemoveEventHandler(handler, arg);
197 inline void PlatformManager::ScheduleWork(AsyncWorkFunct workFunct, intptr_t arg)
199 static_cast<ImplClass *>(this)->_ScheduleWork(workFunct, arg);
202 inline void PlatformManager::RunEventLoop()
204 static_cast<ImplClass *>(this)->_RunEventLoop();
207 inline CHIP_ERROR PlatformManager::StartEventLoopTask()
209 return static_cast<ImplClass *>(this)->_StartEventLoopTask();
212 inline void PlatformManager::LockChipStack()
214 static_cast<ImplClass *>(this)->_LockChipStack();
217 inline bool PlatformManager::TryLockChipStack()
219 return static_cast<ImplClass *>(this)->_TryLockChipStack();
222 inline void PlatformManager::UnlockChipStack()
224 static_cast<ImplClass *>(this)->_UnlockChipStack();
227 inline void PlatformManager::PostEvent(const ChipDeviceEvent * event)
229 static_cast<ImplClass *>(this)->_PostEvent(event);
232 inline void PlatformManager::DispatchEvent(const ChipDeviceEvent * event)
234 static_cast<ImplClass *>(this)->_DispatchEvent(event);
237 inline CHIP_ERROR PlatformManager::StartChipTimer(uint32_t durationMS)
239 return static_cast<ImplClass *>(this)->_StartChipTimer(durationMS);
242 inline CHIP_ERROR PlatformManager::Shutdown()
244 return static_cast<ImplClass *>(this)->_Shutdown();
247 } // namespace DeviceLayer