Apply Upstream code (2021-03-15)
[platform/upstream/connectedhomeip.git] / examples / lock-app / k32w / main / main.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Google LLC.
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 // Main Code
21 // ================================================================================
22
23 #include "openthread/platform/logging.h"
24 #include "openthread/platform/uart.h"
25 #include <mbedtls/platform.h>
26 #include <openthread-system.h>
27 #include <openthread/cli.h>
28 #include <openthread/error.h>
29
30 #include <core/CHIPError.h>
31 #include <platform/CHIPDeviceLayer.h>
32 #include <platform/ThreadStackManager.h>
33 #include <support/CHIPMem.h>
34 #include <support/logging/CHIPLogging.h>
35
36 #include "FreeRtosMbedtlsUtils.h"
37 #include "app_config.h"
38
39 #include "radio.h"
40
41 using namespace ::chip;
42 using namespace ::chip::Inet;
43 using namespace ::chip::DeviceLayer;
44 using namespace ::chip::Logging;
45
46 #include <AppTask.h>
47
48 typedef void (*InitFunc)(void);
49 extern InitFunc __init_array_start;
50 extern InitFunc __init_array_end;
51
52 /* needed for FreeRtos Heap 4 */
53 uint8_t __attribute__((section(".heap"))) ucHeap[0xF000];
54
55 extern "C" void * pvPortCallocRtos(size_t num, size_t size);
56
57 extern "C" void main_task(void const * argument)
58 {
59     CHIP_ERROR ret = CHIP_ERROR_MAX;
60
61     /* Call C++ constructors */
62     InitFunc * pFunc = &__init_array_start;
63     for (; pFunc < &__init_array_end; ++pFunc)
64     {
65         (*pFunc)();
66     }
67
68     mbedtls_platform_set_calloc_free(pvPortCallocRtos, vPortFree);
69
70     /* Used for HW initializations */
71     otSysInit(0, NULL);
72
73     /* UART needs to be enabled so early for getting the Weave Init Logs.
74      * Otherwise, some logs are lost because the UART gets enabled later
75      * during the initialization of the Thread stack */
76     otPlatUartEnable();
77
78     K32W_LOG("Welcome to NXP ELock Demo App");
79
80     /* Mbedtls Threading support is needed because both
81      * Thread and Weave tasks are using it */
82     freertos_mbedtls_mutex_init();
83
84     // Init Chip memory management before the stack
85     chip::Platform::MemoryInit();
86
87     ret = PlatformMgr().InitChipStack();
88     if (ret != CHIP_NO_ERROR)
89     {
90         K32W_LOG("Error during PlatformMgr().InitWeaveStack()");
91         goto exit;
92     }
93
94     ret = ThreadStackMgr().InitThreadStack();
95     if (ret != CHIP_NO_ERROR)
96     {
97         K32W_LOG("Error during ThreadStackMgr().InitThreadStack()");
98         goto exit;
99     }
100
101     ret = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_SleepyEndDevice);
102     if (ret != CHIP_NO_ERROR)
103     {
104         goto exit;
105     }
106
107     // Configure the Thread polling behavior for the device.
108     {
109         ConnectivityManager::ThreadPollingConfig pollingConfig;
110         pollingConfig.Clear();
111         pollingConfig.ActivePollingIntervalMS   = THREAD_ACTIVE_POLLING_INTERVAL_MS;
112         pollingConfig.InactivePollingIntervalMS = THREAD_INACTIVE_POLLING_INTERVAL_MS;
113
114         ret = ConnectivityMgr().SetThreadPollingConfig(pollingConfig);
115         if (ret != CHIP_NO_ERROR)
116         {
117             K32W_LOG("Error during ConnectivityMgr().SetThreadPollingConfig(pollingConfig)");
118             goto exit;
119         }
120     }
121
122     ret = PlatformMgr().StartEventLoopTask();
123     if (ret != CHIP_NO_ERROR)
124     {
125         K32W_LOG("Error during PlatformMgr().StartEventLoopTask();");
126         goto exit;
127     }
128
129     // Start OpenThread task
130     ret = ThreadStackMgrImpl().StartThreadTask();
131     if (ret != CHIP_NO_ERROR)
132     {
133         K32W_LOG("Error during ThreadStackMgrImpl().StartThreadTask()");
134         goto exit;
135     }
136
137     ret = GetAppTask().StartAppTask();
138     if (ret != CHIP_NO_ERROR)
139     {
140         K32W_LOG("Error during GetAppTask().StartAppTask()");
141         goto exit;
142     }
143
144     GetAppTask().AppTaskMain(NULL);
145
146 exit:
147     return;
148 }