Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / qpg6100 / ThreadStackManagerImpl.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
5  *    Licensed under the Apache License, Version 2.0 (the "License");
6  *    you may not use this file except in compliance with the License.
7  *    You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *    Unless required by applicable law or agreed to in writing, software
12  *    distributed under the License is distributed on an "AS IS" BASIS,
13  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *    See the License for the specific language governing permissions and
15  *    limitations under the License.
16  */
17
18 /**
19  *    @file
20  *          Provides an implementation of the ThreadStackManager object for the
21  *          QPG6100 platform using the Qorvo QPG6100 library and the OpenThread
22  *          stack.
23  *
24  */
25 /* this file behaves like a config.h, comes first */
26 #include <platform/internal/CHIPDeviceLayerInternal.h>
27
28 #include <platform/FreeRTOS/GenericThreadStackManagerImpl_FreeRTOS.cpp>
29 #include <platform/OpenThread/GenericThreadStackManagerImpl_OpenThread_LwIP.cpp>
30
31 #include <platform/OpenThread/OpenThreadUtils.h>
32 #include <platform/ThreadStackManager.h>
33
34 #include <support/CHIPMem.h>
35 #include <support/CHIPPlatformMemory.h>
36
37 #include <openthread/heap.h>
38 // Qorvo OpenThread functions
39 extern "C" {
40 #include "alarm_qorvo.h"
41 #include "radio_qorvo.h"
42 #include "random_qorvo.h"
43 }
44
45 namespace chip {
46 namespace DeviceLayer {
47
48 using namespace ::chip::DeviceLayer::Internal;
49
50 ThreadStackManagerImpl ThreadStackManagerImpl::sInstance;
51
52 CHIP_ERROR ThreadStackManagerImpl::_InitThreadStack(void)
53 {
54     return InitThreadStack(NULL);
55 }
56
57 CHIP_ERROR ThreadStackManagerImpl::InitThreadStack(otInstance * otInst)
58 {
59     CHIP_ERROR err = CHIP_NO_ERROR;
60
61     // Initialize Low level QPG OpenThread glue
62     qorvoAlarmInit();
63     qorvoRandomInit();
64     qorvoRadioInit();
65
66     // Initialize the generic implementation base classes.
67     err = GenericThreadStackManagerImpl_FreeRTOS<ThreadStackManagerImpl>::DoInit();
68     SuccessOrExit(err);
69     err = GenericThreadStackManagerImpl_OpenThread_LwIP<ThreadStackManagerImpl>::DoInit(otInst);
70     SuccessOrExit(err);
71
72 exit:
73     return err;
74 }
75
76 bool ThreadStackManagerImpl::IsInitialized()
77 {
78     return sInstance.mThreadStackLock != NULL;
79 }
80
81 void ThreadStackManagerImpl::_OnCHIPoBLEAdvertisingStart(void)
82 {
83     // If Thread-over-BLE is enabled, ensure that ToBLE advertising is stopped before
84     // starting CHIPoBLE advertising.  This is accomplished by disabling the OpenThread
85     // IPv6 interface via a call to otIp6SetEnabled(false).
86     //
87     // On platforms where there is no native support for simultaneous BLE advertising
88     // it is necessary to coordinate between the different
89     // advertising modes a CHIP device may employ.  This arises in particular when a
90     // device supports both CHIPoBLE and ToBLE, each of which requires a separate advertising
91     // regime.  The OnCHIPoBLEAdvertisingStart()/OnCHIPoBLEAdvertisingStop() methods handle
92     // the switching between the two modes.
93     //
94 #if OPENTHREAD_CONFIG_ENABLE_TOBLE
95     LockThreadStack();
96     otIp6SetEnabled(OTInstance(), false);
97     UnlockThreadStack();
98 #endif
99 }
100
101 void ThreadStackManagerImpl::_OnCHIPoBLEAdvertisingStop(void)
102 {
103     // If Thread-over-BLE is enabled, and a Thread provision exists, ensure that ToBLE
104     // advertising is re-activated once CHIPoBLE advertising stops.
105     //
106 #if OPENTHREAD_CONFIG_ENABLE_TOBLE
107     LockThreadStack();
108     if (otThreadGetDeviceRole(OTInstance()) != OT_DEVICE_ROLE_DISABLED && otDatasetIsCommissioned(OTInstance()))
109     {
110         otIp6SetEnabled(OTInstance(), true);
111     }
112     UnlockThreadStack();
113 #endif
114 }
115
116 } // namespace DeviceLayer
117 } // namespace chip
118
119 using namespace ::chip::DeviceLayer;
120
121 /**
122  * Glue function called directly by the OpenThread stack when tasklet processing work
123  * is pending.
124  */
125 extern "C" void otTaskletsSignalPending(otInstance * p_instance)
126 {
127     ThreadStackMgrImpl().SignalThreadActivityPending();
128 }
129
130 /**
131  * Glue function called directly by the OpenThread stack when system event processing work
132  * is pending.
133  */
134 extern "C" void otSysEventSignalPending(void)
135 {
136     BaseType_t yieldRequired = ThreadStackMgrImpl().SignalThreadActivityPendingFromISR();
137     portYIELD_FROM_ISR(yieldRequired);
138 }