3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2019 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 implementation of the ThreadStackManager object for
22 * EFR32 platforms using the Silicon Labs SDK and the OpenThread
26 /* this file behaves like a config.h, comes first */
27 #include <platform/internal/CHIPDeviceLayerInternal.h>
29 #include <platform/FreeRTOS/GenericThreadStackManagerImpl_FreeRTOS.cpp>
30 #include <platform/OpenThread/GenericThreadStackManagerImpl_OpenThread_LwIP.cpp>
32 #include <platform/OpenThread/OpenThreadUtils.h>
33 #include <platform/ThreadStackManager.h>
35 #include <openthread/platform/entropy.h>
37 #include <support/CHIPPlatformMemory.h>
40 namespace DeviceLayer {
42 using namespace ::chip::DeviceLayer::Internal;
44 ThreadStackManagerImpl ThreadStackManagerImpl::sInstance;
46 CHIP_ERROR ThreadStackManagerImpl::_InitThreadStack(void)
48 return InitThreadStack(NULL);
51 CHIP_ERROR ThreadStackManagerImpl::InitThreadStack(otInstance * otInst)
53 CHIP_ERROR err = CHIP_NO_ERROR;
55 // Initialize the generic implementation base classes.
56 err = GenericThreadStackManagerImpl_FreeRTOS<ThreadStackManagerImpl>::DoInit();
58 err = GenericThreadStackManagerImpl_OpenThread_LwIP<ThreadStackManagerImpl>::DoInit(otInst);
65 bool ThreadStackManagerImpl::IsInitialized()
67 return sInstance.mThreadStackLock != NULL;
70 void ThreadStackManagerImpl::_OnCHIPoBLEAdvertisingStart(void)
72 // If Thread-over-BLE is enabled, ensure that ToBLE advertising is stopped before
73 // starting CHIPoBLE advertising. This is accomplished by disabling the OpenThread
74 // IPv6 interface via a call to otIp6SetEnabled(false).
76 // On platforms where there is no native support for simultaneous BLE advertising
77 // it is necessary to coordinate between the different
78 // advertising modes a CHIP device may employ. This arises in particular when a
79 // device supports both CHIPoBLE and ToBLE, each of which requires a separate advertising
80 // regime. The OnCHIPoBLEAdvertisingStart()/OnCHIPoBLEAdvertisingStop() methods handle
81 // the switching between the two modes.
83 #if OPENTHREAD_CONFIG_ENABLE_TOBLE
85 otIp6SetEnabled(OTInstance(), false);
90 void ThreadStackManagerImpl::_OnCHIPoBLEAdvertisingStop(void)
92 // If Thread-over-BLE is enabled, and a Thread provision exists, ensure that ToBLE
93 // advertising is re-activated once CHIPoBLE advertising stops.
95 #if OPENTHREAD_CONFIG_ENABLE_TOBLE
97 if (otThreadGetDeviceRole(OTInstance()) != OT_DEVICE_ROLE_DISABLED && otDatasetIsCommissioned(OTInstance()))
99 otIp6SetEnabled(OTInstance(), true);
105 } // namespace DeviceLayer
108 using namespace ::chip::DeviceLayer;
111 * Glue function called directly by the OpenThread stack when tasklet processing work
114 extern "C" void otTaskletsSignalPending(otInstance * p_instance)
116 ThreadStackMgrImpl().SignalThreadActivityPending();
120 * Glue function called directly by the OpenThread stack when system event processing work
123 extern "C" void otSysEventSignalPending(void)
125 BaseType_t yieldRequired = ThreadStackMgrImpl().SignalThreadActivityPendingFromISR();
126 portYIELD_FROM_ISR(yieldRequired);
129 extern "C" void * otPlatCAlloc(size_t aNum, size_t aSize)
131 return CHIPPlatformMemoryCalloc(aNum, aSize);
134 extern "C" void otPlatFree(void * aPtr)
136 CHIPPlatformMemoryFree(aPtr);
140 * @brief Openthread UART implementation for the CLI is conflicting
141 * with the UART implemented for Pigweed RPC as they use the same UART port
143 * We now only build the uart as implemented in
144 * connectedhomeip/examples/platform/efr32/uart.c
145 * and remap OT functions to use our uart api.
147 * For now OT CLI isn't usable when the examples are built with pw_rpc
150 #ifndef PW_RPC_ENABLED
154 extern "C" __WEAK otError otPlatUartEnable(void)
156 #ifdef PW_RPC_ENABLED
157 return OT_ERROR_NOT_IMPLEMENTED;
160 return OT_ERROR_NONE;
164 extern "C" __WEAK otError otPlatUartSend(const uint8_t * aBuf, uint16_t aBufLength)
166 #ifdef PW_RPC_ENABLED
167 return OT_ERROR_NOT_IMPLEMENTED;
169 if (uartConsoleWrite((const char *) aBuf, aBufLength) > 0)
171 otPlatUartSendDone();
172 return OT_ERROR_NONE;
174 return OT_ERROR_FAILED;
178 extern "C" __WEAK void efr32UartProcess(void)
180 #ifndef PW_RPC_ENABLED
181 uint8_t tempBuf[128] = { 0 };
182 // will read the data available up to 128bytes
183 uint16_t count = uartConsoleRead((char *) tempBuf, 128);
186 // ot process Received data for CLI cmds
187 otPlatUartReceived(tempBuf, count);
192 extern "C" __WEAK otError otPlatUartFlush(void)
194 return OT_ERROR_NOT_IMPLEMENTED;
197 extern "C" __WEAK otError otPlatUartDisable(void)
199 return OT_ERROR_NOT_IMPLEMENTED;