Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / include / platform / internal / GenericPlatformManagerImpl_FreeRTOS.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  *          Provides an generic implementation of PlatformManager features
22  *          for use on FreeRTOS platforms.
23  */
24
25 #pragma once
26
27 #include <platform/internal/GenericPlatformManagerImpl.h>
28
29 #if defined(ESP_PLATFORM)
30 #include "freertos/FreeRTOS.h"
31 #include "freertos/queue.h"
32 #include "freertos/semphr.h"
33 #include "freertos/task.h"
34 #else
35 #include "FreeRTOS.h"
36 #include "queue.h"
37 #include "semphr.h"
38 #include "task.h"
39 #endif
40
41 namespace chip {
42 namespace DeviceLayer {
43 namespace Internal {
44
45 /**
46  * Provides a generic implementation of PlatformManager features that works on FreeRTOS platforms.
47  *
48  * This template contains implementations of select features from the PlatformManager abstract
49  * interface that are suitable for use on FreeRTOS-based platforms.  It is intended to be inherited
50  * (directly or indirectly) by the PlatformManagerImpl class, which also appears as the template's
51  * ImplClass parameter.
52  */
53 template <class ImplClass>
54 class GenericPlatformManagerImpl_FreeRTOS : public GenericPlatformManagerImpl<ImplClass>
55 {
56
57 protected:
58     TimeOut_t mNextTimerBaseTime;
59     TickType_t mNextTimerDurationTicks;
60     SemaphoreHandle_t mChipStackLock;
61     QueueHandle_t mChipEventQueue;
62     TaskHandle_t mEventLoopTask;
63     bool mChipTimerActive;
64
65     // ===== Methods that implement the PlatformManager abstract interface.
66
67     CHIP_ERROR _InitChipStack();
68     void _LockChipStack(void);
69     bool _TryLockChipStack(void);
70     void _UnlockChipStack(void);
71     void _PostEvent(const ChipDeviceEvent * event);
72     void _RunEventLoop(void);
73     CHIP_ERROR _StartEventLoopTask(void);
74     CHIP_ERROR _StartChipTimer(uint32_t durationMS);
75     CHIP_ERROR _Shutdown(void);
76
77     // ===== Methods available to the implementation subclass.
78
79     void PostEventFromISR(const ChipDeviceEvent * event, BaseType_t & yieldRequired);
80
81 private:
82     // ===== Private members for use by this class only.
83
84     inline ImplClass * Impl() { return static_cast<ImplClass *>(this); }
85
86     static void EventLoopTaskMain(void * arg);
87
88 #if defined(CHIP_CONFIG_FREERTOS_USE_STATIC_QUEUE) && CHIP_CONFIG_FREERTOS_USE_STATIC_QUEUE
89     uint8_t mEventQueueBuffer[CHIP_DEVICE_CONFIG_MAX_EVENT_QUEUE_SIZE * sizeof(ChipDeviceEvent)];
90     StaticQueue_t mEventQueueStruct;
91 #endif
92
93 #if defined(CHIP_CONFIG_FREERTOS_USE_STATIC_TASK) && CHIP_CONFIG_FREERTOS_USE_STATIC_TASK
94     StackType_t mEventLoopStack[CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE / sizeof(StackType_t)];
95     StaticTask_t mventLoopTaskStruct;
96 #endif
97 };
98
99 // Instruct the compiler to instantiate the template only when explicitly told to do so.
100 extern template class GenericPlatformManagerImpl_FreeRTOS<PlatformManagerImpl>;
101
102 } // namespace Internal
103 } // namespace DeviceLayer
104 } // namespace chip