3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * Provides an generic implementation of PlatformManager features
21 * for use on Zephyr RTOS platforms.
26 #include <platform/internal/GenericPlatformManagerImpl.h>
28 #include <sys/select.h>
32 namespace DeviceLayer {
36 * Provides a generic implementation of PlatformManager features that works on Zephyr RTOS platforms.
38 * This template contains implementations of selected features from the PlatformManager abstract
39 * interface that are suitable for use on Zephyr-based platforms. It is intended to be inherited
40 * (directly or indirectly) by the PlatformManagerImpl class, which also appears as the template's
41 * ImplClass parameter.
43 template <class ImplClass>
44 class GenericPlatformManagerImpl_Zephyr : public GenericPlatformManagerImpl<ImplClass>
47 using ThreadStack = k_thread_stack_t[K_THREAD_STACK_LEN(CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE)];
49 // Members for select() loop
56 // Lock for the whole CHIP stack
57 k_mutex mChipStackLock;
59 // Members for CHIP event processing
60 ChipDeviceEvent mChipEventRingBuffer[CHIP_DEVICE_CONFIG_MAX_EVENT_QUEUE_SIZE];
61 k_msgq mChipEventQueue;
64 // Although defining thread stack as a class member is feasible it's discouraged according to
65 // the Zephyr documentation (see remarks on K_THREAD_STACK_MEMBER macro). Therefore, this class
66 // requires the stack reference to be passed in the constructor.
67 ThreadStack & mChipThreadStack;
70 // ===== Methods that implement the PlatformManager abstract interface.
72 CHIP_ERROR _InitChipStack();
73 void _LockChipStack(void);
74 bool _TryLockChipStack(void);
75 void _UnlockChipStack(void);
76 void _PostEvent(const ChipDeviceEvent * event);
77 void _RunEventLoop(void);
78 CHIP_ERROR _StartEventLoopTask(void);
79 CHIP_ERROR _StartChipTimer(uint32_t durationMS);
80 CHIP_ERROR _Shutdown(void);
82 // ===== Methods available to the implementation subclass.
83 explicit GenericPlatformManagerImpl_Zephyr(ThreadStack & stack) : mChipThreadStack(stack) {}
86 // ===== Private members for use by this class only.
87 ImplClass * Impl() { return static_cast<ImplClass *>(this); }
90 void ProcessDeviceEvents();
92 static void EventLoopTaskMain(void * thisPtr, void *, void *);
95 // Instruct the compiler to instantiate the template only when explicitly told to do so.
96 extern template class GenericPlatformManagerImpl_Zephyr<PlatformManagerImpl>;
98 } // namespace Internal
99 } // namespace DeviceLayer