Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / K32W / ThreadStackManagerImpl.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2020 Nest Labs, Inc.
5  *    All rights reserved.
6  *
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
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 /**
21  *    @file
22  *          Provides an implementation of the ThreadStackManager object for
23  *          K32W platforms using the NXP SDK and the OpenThread
24  *          stack.
25  *
26  */
27
28 /* this file behaves like a config.h, comes first */
29 #include <platform/internal/CHIPDeviceLayerInternal.h>
30
31 #include <platform/OpenThread/OpenThreadUtils.h>
32 #include <platform/ThreadStackManager.h>
33
34 #include <platform/FreeRTOS/GenericThreadStackManagerImpl_FreeRTOS.cpp>
35 #include <platform/OpenThread/GenericThreadStackManagerImpl_OpenThread_LwIP.cpp>
36
37 namespace chip {
38 namespace DeviceLayer {
39
40 using namespace ::chip::DeviceLayer::Internal;
41
42 ThreadStackManagerImpl ThreadStackManagerImpl::sInstance;
43
44 CHIP_ERROR ThreadStackManagerImpl::_InitThreadStack(void)
45 {
46     return InitThreadStack(NULL);
47 }
48
49 CHIP_ERROR ThreadStackManagerImpl::InitThreadStack(otInstance * otInst)
50 {
51     CHIP_ERROR err = CHIP_NO_ERROR;
52
53     // Initialize the generic implementation base classes.
54     err = GenericThreadStackManagerImpl_FreeRTOS<ThreadStackManagerImpl>::DoInit();
55     SuccessOrExit(err);
56     err = GenericThreadStackManagerImpl_OpenThread_LwIP<ThreadStackManagerImpl>::DoInit(otInst);
57     SuccessOrExit(err);
58
59     otCliUartInit(OTInstance());
60
61 exit:
62     return err;
63 }
64
65 bool ThreadStackManagerImpl::IsInitialized()
66 {
67     return sInstance.mThreadStackLock != NULL;
68 }
69
70 } // namespace DeviceLayer
71 } // namespace chip
72
73 using namespace ::chip::DeviceLayer;
74
75 /**
76  * Glue function called directly by the OpenThread stack when tasklet processing work
77  * is pending.
78  */
79 extern "C" void otTaskletsSignalPending(otInstance * p_instance)
80 {
81     ThreadStackMgrImpl().SignalThreadActivityPending();
82 }
83
84 /**
85  * Glue function called directly by the OpenThread stack when system event processing work
86  * is pending.
87  */
88 extern "C" void otSysEventSignalPending(void)
89 {
90     BaseType_t yieldRequired = ThreadStackMgrImpl().SignalThreadActivityPendingFromISR();
91     portYIELD_FROM_ISR(yieldRequired);
92 }
93
94 extern "C" void * pvPortCallocRtos(size_t num, size_t size)
95 {
96     size_t totalAllocSize = (size_t)(num * size);
97
98     if (size && totalAllocSize / size != num)
99         return nullptr;
100
101     void * p = pvPortMalloc(totalAllocSize);
102
103     if (p)
104     {
105         memset(p, 0, totalAllocSize);
106     }
107
108     return p;
109 }
110
111 extern "C" void * otPlatCAlloc(size_t aNum, size_t aSize)
112 {
113     return pvPortCallocRtos(aNum, aSize);
114 }
115
116 extern "C" void otPlatFree(void * aPtr)
117 {
118     return vPortFree(aPtr);
119 }