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 * Provides an generic implementation of PlatformManager features
22 * for use on FreeRTOS platforms.
27 #include <platform/internal/GenericPlatformManagerImpl.h>
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"
42 namespace DeviceLayer {
46 * Provides a generic implementation of PlatformManager features that works on FreeRTOS platforms.
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.
53 template <class ImplClass>
54 class GenericPlatformManagerImpl_FreeRTOS : public GenericPlatformManagerImpl<ImplClass>
58 TimeOut_t mNextTimerBaseTime;
59 TickType_t mNextTimerDurationTicks;
60 SemaphoreHandle_t mChipStackLock;
61 QueueHandle_t mChipEventQueue;
62 TaskHandle_t mEventLoopTask;
63 bool mChipTimerActive;
65 // ===== Methods that implement the PlatformManager abstract interface.
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);
77 // ===== Methods available to the implementation subclass.
79 void PostEventFromISR(const ChipDeviceEvent * event, BaseType_t & yieldRequired);
82 // ===== Private members for use by this class only.
84 inline ImplClass * Impl() { return static_cast<ImplClass *>(this); }
86 static void EventLoopTaskMain(void * arg);
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;
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;
99 // Instruct the compiler to instantiate the template only when explicitly told to do so.
100 extern template class GenericPlatformManagerImpl_FreeRTOS<PlatformManagerImpl>;
102 } // namespace Internal
103 } // namespace DeviceLayer