Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / include / platform / internal / GenericPlatformManagerImpl_Zephyr.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 /**
19  *    @file
20  *          Provides an generic implementation of PlatformManager features
21  *          for use on Zephyr RTOS platforms.
22  */
23
24 #pragma once
25
26 #include <platform/internal/GenericPlatformManagerImpl.h>
27
28 #include <sys/select.h>
29 #include <zephyr.h>
30
31 namespace chip {
32 namespace DeviceLayer {
33 namespace Internal {
34
35 /**
36  * Provides a generic implementation of PlatformManager features that works on Zephyr RTOS platforms.
37  *
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.
42  */
43 template <class ImplClass>
44 class GenericPlatformManagerImpl_Zephyr : public GenericPlatformManagerImpl<ImplClass>
45 {
46 protected:
47     using ThreadStack = k_thread_stack_t[K_THREAD_STACK_LEN(CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE)];
48
49     // Members for select() loop
50     int mMaxFd;
51     fd_set mReadSet;
52     fd_set mWriteSet;
53     fd_set mErrorSet;
54     timeval mNextTimeout;
55
56     // Lock for the whole CHIP stack
57     k_mutex mChipStackLock;
58
59     // Members for CHIP event processing
60     ChipDeviceEvent mChipEventRingBuffer[CHIP_DEVICE_CONFIG_MAX_EVENT_QUEUE_SIZE];
61     k_msgq mChipEventQueue;
62
63     // Main CHIP thread
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;
68     k_thread mChipThread;
69
70     // ===== Methods that implement the PlatformManager abstract interface.
71
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);
81
82     // ===== Methods available to the implementation subclass.
83     explicit GenericPlatformManagerImpl_Zephyr(ThreadStack & stack) : mChipThreadStack(stack) {}
84
85 private:
86     // ===== Private members for use by this class only.
87     ImplClass * Impl() { return static_cast<ImplClass *>(this); }
88     void SysUpdate();
89     void SysProcess();
90     void ProcessDeviceEvents();
91
92     static void EventLoopTaskMain(void * thisPtr, void *, void *);
93 };
94
95 // Instruct the compiler to instantiate the template only when explicitly told to do so.
96 extern template class GenericPlatformManagerImpl_Zephyr<PlatformManagerImpl>;
97
98 } // namespace Internal
99 } // namespace DeviceLayer
100 } // namespace chip