3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2018 Nest Labs, Inc.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 * Provides an generic implementation of ThreadStackManager features
23 * for use on FreeRTOS platforms.
28 #if defined(ESP_PLATFORM)
29 #include "freertos/FreeRTOS.h"
30 #include "freertos/semphr.h"
31 #include "freertos/task.h"
41 namespace DeviceLayer {
43 class ThreadStackManagerImpl;
48 * Provides a generic implementation of ThreadStackManager features that works on FreeRTOS platforms.
50 * This template contains implementations of select features from the ThreadStackManager abstract
51 * interface that are suitable for use on FreeRTOS-based platforms. It is intended to be
52 * inherited, directly or indirectly, by the ThreadStackManagerImpl class, which also appears as
53 * the template's ImplClass parameter.
55 template <class ImplClass>
56 class GenericThreadStackManagerImpl_FreeRTOS
60 // ===== Methods that implement the ThreadStackManager abstract interface.
62 CHIP_ERROR _StartThreadTask(void);
63 void _LockThreadStack(void);
64 bool _TryLockThreadStack(void);
65 void _UnlockThreadStack(void);
67 // ===== Members available to the implementation subclass.
69 SemaphoreHandle_t mThreadStackLock;
70 TaskHandle_t mThreadTask;
73 void SignalThreadActivityPending();
74 BaseType_t SignalThreadActivityPendingFromISR();
77 // ===== Private members for use by this class only.
79 inline ImplClass * Impl() { return static_cast<ImplClass *>(this); }
81 static void ThreadTaskMain(void * arg);
82 static void OnJoinerTimer(TimerHandle_t xTimer);
84 portTickType mJoinerExpire;
85 bool mJoinerStartPending = false;
87 #if defined(CHIP_CONFIG_FREERTOS_USE_STATIC_TASK) && CHIP_CONFIG_FREERTOS_USE_STATIC_TASK
88 StackType_t mThreadStack[CHIP_DEVICE_CONFIG_THREAD_TASK_STACK_SIZE / sizeof(StackType_t)];
89 StaticTask_t mThreadTaskStruct;
93 // Instruct the compiler to instantiate the template only when explicitly told to do so.
94 extern template class GenericThreadStackManagerImpl_FreeRTOS<ThreadStackManagerImpl>;
96 } // namespace Internal
97 } // namespace DeviceLayer