|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2021-2024 Project CHIP Authors |
| 4 | + * All rights reserved. |
| 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 platform using CMSISOS 2 OS Abstraction APIs. |
| 23 | + */ |
| 24 | + |
| 25 | +#pragma once |
| 26 | + |
| 27 | +#include <platform/CHIPDeviceConfig.h> |
| 28 | +#include <platform/internal/GenericPlatformManagerImpl.h> |
| 29 | + |
| 30 | +#include <atomic> |
| 31 | +#include <cmsis_os2.h> |
| 32 | + |
| 33 | +namespace chip { |
| 34 | +namespace DeviceLayer { |
| 35 | +namespace Internal { |
| 36 | + |
| 37 | +/** |
| 38 | + * Provides a generic implementation of PlatformManager features for platforms using CMSISOS 2 OS Abstraction APIs |
| 39 | + * |
| 40 | + * This template contains implementations of select features from the PlatformManager abstract |
| 41 | + * interface that are suitable for use on CMSISOS-based platforms. It is intended to be inherited |
| 42 | + * (directly or indirectly) by the PlatformManagerImpl class, which also appears as the template's |
| 43 | + * ImplClass parameter. |
| 44 | + */ |
| 45 | +template <class ImplClass> |
| 46 | +class GenericPlatformManagerImpl_CMSISOS : public GenericPlatformManagerImpl<ImplClass> |
| 47 | +{ |
| 48 | + |
| 49 | +protected: |
| 50 | + osMutexId_t mChipStackLock = NULL; |
| 51 | + osMessageQueueId_t mChipEventQueue = NULL; |
| 52 | + osThreadId_t mEventLoopTask = NULL; |
| 53 | + bool mChipTimerActive; |
| 54 | + |
| 55 | +#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING |
| 56 | + osMessageQueueId_t mBackgroundEventQueue = NULL; |
| 57 | + osThreadId_t mBackgroundEventLoopTask = NULL; |
| 58 | +#endif |
| 59 | + |
| 60 | + // ===== Methods that implement the PlatformManager abstract interface. |
| 61 | + |
| 62 | + CHIP_ERROR _InitChipStack(); |
| 63 | + |
| 64 | + void _LockChipStack(void); |
| 65 | + bool _TryLockChipStack(void); |
| 66 | + void _UnlockChipStack(void); |
| 67 | + |
| 68 | + CHIP_ERROR _PostEvent(const ChipDeviceEvent * event); |
| 69 | + void _RunEventLoop(void); |
| 70 | + CHIP_ERROR _StartEventLoopTask(void); |
| 71 | + CHIP_ERROR _StopEventLoopTask(); |
| 72 | + CHIP_ERROR _StartChipTimer(System::Clock::Timeout duration); |
| 73 | + void _Shutdown(void); |
| 74 | + |
| 75 | +#if CHIP_STACK_LOCK_TRACKING_ENABLED |
| 76 | + bool _IsChipStackLockedByCurrentThread() const; |
| 77 | +#endif |
| 78 | + |
| 79 | + CHIP_ERROR _PostBackgroundEvent(const ChipDeviceEvent * event); |
| 80 | + void _RunBackgroundEventLoop(void); |
| 81 | + CHIP_ERROR _StartBackgroundEventLoopTask(void); |
| 82 | + CHIP_ERROR _StopBackgroundEventLoopTask(); |
| 83 | + |
| 84 | + // ===== Methods available to the implementation subclass. |
| 85 | + |
| 86 | +private: |
| 87 | + // ===== Private members for use by this class only. |
| 88 | + |
| 89 | + inline ImplClass * Impl() { return static_cast<ImplClass *>(this); } |
| 90 | + |
| 91 | + static void EventLoopTaskMain(void * pvParameter); |
| 92 | + uint32_t SyncNextChipTimerHandling(); |
| 93 | + uint32_t mNextTimerBaseTime; |
| 94 | + uint32_t mNextTimerDurationTicks; |
| 95 | + std::atomic<bool> mShouldRunEventLoop; |
| 96 | + |
| 97 | +#if defined(CHIP_CONFIG_CMSISOS_USE_STATIC_QUEUE) && CHIP_CONFIG_CMSISOS_USE_STATIC_QUEUE |
| 98 | + uint8_t mEventQueueBuffer[CHIP_DEVICE_CONFIG_MAX_EVENT_QUEUE_SIZE * sizeof(ChipDeviceEvent)]; |
| 99 | + osMessageQueue_t mEventQueueStruct; |
| 100 | + const osMessageQueueAttr_t mEventQueueAttr = { .cb_mem = &mEventQueueStruct, |
| 101 | + .cb_size = osMessageQueueCbSize, |
| 102 | + .mq_mem = mEventQueueBuffer, |
| 103 | + .mq_size = sizeof(mEventQueueBuffer) }; |
| 104 | + |
| 105 | + const osMessageQueueAttr_t * mEventQueueAttrPtr = &mEventQueueAttr; |
| 106 | +#else |
| 107 | + // Nothing to configure for queues, Just use this to avoid #ifdef in the class implementation |
| 108 | + const osMessageQueueAttr_t * mEventQueueAttrPtr = nullptr; |
| 109 | +#endif // CHIP_CONFIG_CMSISOS_USE_STATIC_QUEUE |
| 110 | + |
| 111 | +#if defined(CHIP_CONFIG_CMSISOS_USE_STATIC_TASK) && CHIP_CONFIG_CMSISOS_USE_STATIC_TASK |
| 112 | + uint8_t mEventLoopStack[CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE]; |
| 113 | + osThread_t mEventLoopTaskControlBlock; |
| 114 | +#endif // CHIP_CONFIG_CMSISOS_USE_STATIC_TASK |
| 115 | + |
| 116 | + const osThreadAttr_t mEventLoopTaskAttr = { |
| 117 | + .name = CHIP_DEVICE_CONFIG_CHIP_TASK_NAME, |
| 118 | + .attr_bits = osThreadDetached, |
| 119 | +#if defined(CHIP_CONFIG_CMSISOS_USE_STATIC_TASK) && CHIP_CONFIG_CMSISOS_USE_STATIC_TASK |
| 120 | + .cb_mem = &mEventLoopTaskControlBlock, |
| 121 | + .cb_size = osThreadCbSize, |
| 122 | + .stack_mem = mEventLoopStack, |
| 123 | +#endif // CHIP_CONFIG_CMSISOS_USE_STATIC_TASK |
| 124 | + .stack_size = CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE, |
| 125 | + .priority = CHIP_DEVICE_CONFIG_CHIP_TASK_PRIORITY |
| 126 | + }; |
| 127 | + |
| 128 | +#if defined(CHIP_CONFIG_CMSISOS_USE_STATIC_MUTEX) && CHIP_CONFIG_CMSISOS_USE_STATIC_MUTEX |
| 129 | + osMutexCbSize uint8_t mMutexControlBlock[osMutexCbSize]; |
| 130 | +#endif // CHIP_CONFIG_CMSISOS_USE_STATIC_MUTEX |
| 131 | + const osMutexAttr_t mChipStackMutexAttr = { |
| 132 | + .name = "", |
| 133 | +#if defined(CHIP_CONFIG_CMSISOS_USE_STATIC_MUTEX) && CHIP_CONFIG_CMSISOS_USE_STATIC_MUTEX |
| 134 | + .cb_mem = &mMutexControlBlock, |
| 135 | + .cb_size = osMutexCbSize, |
| 136 | +#endif // CHIP_CONFIG_CMSISOS_USE_STATIC_MUTEX |
| 137 | + .attr_bits = osMutexRecursive | osMutexPrioInherit, |
| 138 | + }; |
| 139 | + |
| 140 | +#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING |
| 141 | + static void BackgroundEventLoopTaskMain(void * pvParameter); |
| 142 | + std::atomic<bool> mShouldRunBackgroundEventLoop; |
| 143 | + |
| 144 | +#if defined(CHIP_CONFIG_CMSISOS_USE_STATIC_QUEUE) && CHIP_CONFIG_CMSISOS_USE_STATIC_QUEUE |
| 145 | + uint8_t mBackgroundQueueBuffer[CHIP_DEVICE_CONFIG_BG_MAX_EVENT_QUEUE_SIZE * sizeof(ChipDeviceEvent)]; |
| 146 | + osMessageQueue_t mBackgroundQueueStruct; |
| 147 | + const osMessageQueueAttr_t mBgQueueAttr = { .cb_mem = &mBackgroundQueueStruct, |
| 148 | + .cb_size = osMessageQueueCbSize, |
| 149 | + .mq_mem = mBackgroundQueueBuffer, |
| 150 | + .mq_size = sizeof(mBackgroundQueueBuffer) }; |
| 151 | + |
| 152 | + const osMessageQueueAttr_t * mBgQueueAttrPtr = &mBgQueueAttr; |
| 153 | +#else |
| 154 | + // Nothing to configure for queues, Just use this to avoid #ifdef in the class implementation |
| 155 | + const osMessageQueueAttr_t * mBgQueueAttrPtr = nullptr; |
| 156 | +#endif // CHIP_CONFIG_CMSISOS_USE_STATIC_QUEUE |
| 157 | + |
| 158 | +#if defined(CHIP_CONFIG_CMSISOS_USE_STATIC_TASK) && CHIP_CONFIG_CMSISOS_USE_STATIC_TASK |
| 159 | + uint8_t mBackgroundEventLoopStack[CHIP_DEVICE_CONFIG_BG_TASK_STACK_SIZE]; |
| 160 | + osThread_t mBackgroundEventLoopTaskControlBlock; |
| 161 | +#endif // CHIP_CONFIG_CMSISOS_USE_STATIC_TASK |
| 162 | + |
| 163 | + const osThreadAttr_t mBgEventLoopTaskAttr = { |
| 164 | + .name = CHIP_DEVICE_CONFIG_BG_TASK_NAME, |
| 165 | + .attr_bits = osThreadDetached, |
| 166 | +#if defined(CHIP_CONFIG_CMSISOS_USE_STATIC_TASK) && CHIP_CONFIG_CMSISOS_USE_STATIC_TASK |
| 167 | + .cb_mem = &mBackgroundEventLoopTaskControlBlock, |
| 168 | + .cb_size = osThreadCbSize, |
| 169 | + .stack_mem = mBackgroundEventLoopStack, |
| 170 | +#endif // CHIP_CONFIG_CMSISOS_USE_STATIC_TASK |
| 171 | + .stack_size = CHIP_DEVICE_CONFIG_BG_TASK_STACK_SIZE, |
| 172 | + .priority = CHIP_DEVICE_CONFIG_BG_TASK_PRIORITY |
| 173 | + }; |
| 174 | +#endif // CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING |
| 175 | +}; |
| 176 | + |
| 177 | +// Instruct the compiler to instantiate the template only when explicitly told to do so. |
| 178 | +extern template class GenericPlatformManagerImpl_CMSISOS<PlatformManagerImpl>; |
| 179 | + |
| 180 | +} // namespace Internal |
| 181 | +} // namespace DeviceLayer |
| 182 | +} // namespace chip |
0 commit comments