3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2016-2017 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 * This file contains declarations of the
22 * chip::System::Layer class and its related types, data and
28 // Include configuration headers
29 #include <system/SystemConfig.h>
31 #include <core/CHIPCallback.h>
33 #include <support/DLLUtil.h>
34 #include <system/SystemError.h>
35 #include <system/SystemEvent.h>
36 #include <system/SystemObject.h>
38 // Include dependent headers
39 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
40 #include <system/SystemWakeEvent.h>
42 #include <sys/select.h>
43 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
45 #if CHIP_SYSTEM_CONFIG_POSIX_LOCKING
47 #endif // CHIP_SYSTEM_CONFIG_POSIX_LOCKING
55 #if CHIP_SYSTEM_CONFIG_USE_LWIP
57 #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
62 using ::chip::System::Error;
63 using ::chip::System::Layer;
65 #if CHIP_SYSTEM_CONFIG_USE_LWIP
66 using ::chip::System::Object;
67 #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
69 extern Error WillInit(Layer & aLayer, void * aContext);
70 extern Error WillShutdown(Layer & aLayer, void * aContext);
72 extern void DidInit(Layer & aLayer, void * aContext, Error aStatus);
73 extern void DidShutdown(Layer & aLayer, void * aContext, Error aStatus);
75 #if CHIP_SYSTEM_CONFIG_USE_LWIP
76 extern Error PostEvent(Layer & aLayer, void * aContext, Object & aTarget, EventType aType, uintptr_t aArgument);
77 extern Error DispatchEvents(Layer & aLayer, void * aContext);
78 extern Error DispatchEvent(Layer & aLayer, void * aContext, Event aEvent);
79 extern Error StartTimer(Layer & aLayer, void * aContext, uint32_t aMilliseconds);
80 #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
83 } // namespace Platform
88 * The state of a Layer object.
92 kLayerState_NotInitialized = 0, /**< Not initialized state. */
93 kLayerState_Initialized = 1 /**< Initialized state. */
96 #if CHIP_SYSTEM_CONFIG_USE_LWIP
97 typedef Error (*LwIPEventHandlerFunction)(Object & aTarget, EventType aEventType, uintptr_t aArgument);
99 class LwIPEventHandlerDelegate
104 bool IsInitialized(void) const;
105 void Init(LwIPEventHandlerFunction aFunction);
106 void Prepend(const LwIPEventHandlerDelegate *& aDelegateList);
109 LwIPEventHandlerFunction mFunction;
110 const LwIPEventHandlerDelegate * mNextDelegate;
112 #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
118 * This provides access to timers according to the configured event handling model.
120 * For \c CHIP_SYSTEM_CONFIG_USE_SOCKETS, event readiness notification is handled via traditional poll/select implementation on
121 * the platform adaptation.
123 * For \c CHIP_SYSTEM_CONFIG_USE_LWIP, event readiness notification is handle via events / messages and platform- and
124 * system-specific hooks for the event/message system.
126 class DLL_EXPORT Layer
131 Error Init(void * aContext);
134 void * GetPlatformData() const;
135 void SetPlatformData(void * aPlatformData);
137 LayerState State() const;
139 Error NewTimer(Timer *& aTimerPtr);
141 void StartTimer(uint32_t aMilliseconds, chip::Callback::Callback<> * aCallback);
142 void DispatchTimerCallbacks(uint64_t kCurrentEpoch);
144 typedef void (*TimerCompleteFunct)(Layer * aLayer, void * aAppState, Error aError);
145 Error StartTimer(uint32_t aMilliseconds, TimerCompleteFunct aComplete, void * aAppState);
146 void CancelTimer(TimerCompleteFunct aOnComplete, void * aAppState);
148 Error ScheduleWork(TimerCompleteFunct aComplete, void * aAppState);
150 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
151 void PrepareSelect(int & aSetSize, fd_set * aReadSet, fd_set * aWriteSet, fd_set * aExceptionSet, struct timeval & aSleepTime);
152 void HandleSelectResult(int aSetSize, fd_set * aReadSet, fd_set * aWriteSet, fd_set * aExceptionSet);
154 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
156 #if CHIP_SYSTEM_CONFIG_USE_LWIP
157 typedef Error (*EventHandler)(Object & aTarget, EventType aEventType, uintptr_t aArgument);
158 Error AddEventHandlerDelegate(LwIPEventHandlerDelegate & aDelegate);
161 Error PostEvent(Object & aTarget, EventType aEventType, uintptr_t aArgument);
162 Error DispatchEvents(void);
163 Error DispatchEvent(Event aEvent);
164 Error HandleEvent(Object & aTarget, EventType aEventType, uintptr_t aArgument);
167 Error HandlePlatformTimer(void);
168 #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
170 static uint64_t GetClock_Monotonic();
171 static uint64_t GetClock_MonotonicMS();
172 static uint64_t GetClock_MonotonicHiRes();
173 static Error GetClock_RealTime(uint64_t & curTime);
174 static Error GetClock_RealTimeMS(uint64_t & curTimeMS);
175 static Error SetClock_RealTime(uint64_t newCurTime);
178 LayerState mLayerState;
180 void * mPlatformData;
181 chip::Callback::CallbackDeque mTimerCallbacks;
183 #if CHIP_SYSTEM_CONFIG_USE_LWIP
184 static LwIPEventHandlerDelegate sSystemEventHandlerDelegate;
186 const LwIPEventHandlerDelegate * mEventDelegateList;
189 #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
191 #if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
192 SystemWakeEvent mWakeEvent;
193 #if CHIP_SYSTEM_CONFIG_POSIX_LOCKING
194 pthread_t mHandleSelectThread;
195 #endif // CHIP_SYSTEM_CONFIG_POSIX_LOCKING
196 #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
198 #if CHIP_SYSTEM_CONFIG_USE_LWIP
199 static Error HandleSystemLayerEvent(Object & aTarget, EventType aEventType, uintptr_t aArgument);
201 Error StartPlatformTimer(uint32_t aDelayMilliseconds);
203 friend Error Platform::Layer::PostEvent(Layer & aLayer, void * aContext, Object & aTarget, EventType aType,
204 uintptr_t aArgument);
205 friend Error Platform::Layer::DispatchEvents(Layer & aLayer, void * aContext);
206 friend Error Platform::Layer::DispatchEvent(Layer & aLayer, void * aContext, Event aEvent);
207 friend Error Platform::Layer::StartTimer(Layer & aLayer, void * aContext, uint32_t aMilliseconds);
208 #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
210 // Copy and assignment NOT DEFINED
211 Layer(const Layer &) = delete;
212 Layer & operator=(const Layer &) = delete;
218 * This returns the current state of the layer object.
220 inline LayerState Layer::State() const
222 return this->mLayerState;
225 } // namespace System