3 * Copyright (c) 2020 Google LLC.
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.
19 // ================================================================================
21 // ================================================================================
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>
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>
36 #include "FreeRtosMbedtlsUtils.h"
37 #include "app_config.h"
41 using namespace ::chip;
42 using namespace ::chip::Inet;
43 using namespace ::chip::DeviceLayer;
44 using namespace ::chip::Logging;
48 typedef void (*InitFunc)(void);
49 extern InitFunc __init_array_start;
50 extern InitFunc __init_array_end;
52 /* needed for FreeRtos Heap 4 */
53 uint8_t __attribute__((section(".heap"))) ucHeap[0xF000];
55 extern "C" void * pvPortCallocRtos(size_t num, size_t size);
57 extern "C" void main_task(void const * argument)
59 CHIP_ERROR ret = CHIP_ERROR_MAX;
61 /* Call C++ constructors */
62 InitFunc * pFunc = &__init_array_start;
63 for (; pFunc < &__init_array_end; ++pFunc)
68 mbedtls_platform_set_calloc_free(pvPortCallocRtos, vPortFree);
70 /* Used for HW initializations */
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 */
78 K32W_LOG("Welcome to NXP ELock Demo App");
80 /* Mbedtls Threading support is needed because both
81 * Thread and Weave tasks are using it */
82 freertos_mbedtls_mutex_init();
84 // Init Chip memory management before the stack
85 chip::Platform::MemoryInit();
87 ret = PlatformMgr().InitChipStack();
88 if (ret != CHIP_NO_ERROR)
90 K32W_LOG("Error during PlatformMgr().InitWeaveStack()");
94 ret = ThreadStackMgr().InitThreadStack();
95 if (ret != CHIP_NO_ERROR)
97 K32W_LOG("Error during ThreadStackMgr().InitThreadStack()");
101 ret = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_SleepyEndDevice);
102 if (ret != CHIP_NO_ERROR)
107 // Configure the Thread polling behavior for the device.
109 ConnectivityManager::ThreadPollingConfig pollingConfig;
110 pollingConfig.Clear();
111 pollingConfig.ActivePollingIntervalMS = THREAD_ACTIVE_POLLING_INTERVAL_MS;
112 pollingConfig.InactivePollingIntervalMS = THREAD_INACTIVE_POLLING_INTERVAL_MS;
114 ret = ConnectivityMgr().SetThreadPollingConfig(pollingConfig);
115 if (ret != CHIP_NO_ERROR)
117 K32W_LOG("Error during ConnectivityMgr().SetThreadPollingConfig(pollingConfig)");
122 ret = PlatformMgr().StartEventLoopTask();
123 if (ret != CHIP_NO_ERROR)
125 K32W_LOG("Error during PlatformMgr().StartEventLoopTask();");
129 // Start OpenThread task
130 ret = ThreadStackMgrImpl().StartThreadTask();
131 if (ret != CHIP_NO_ERROR)
133 K32W_LOG("Error during ThreadStackMgrImpl().StartThreadTask()");
137 ret = GetAppTask().StartAppTask();
138 if (ret != CHIP_NO_ERROR)
140 K32W_LOG("Error during GetAppTask().StartAppTask()");
144 GetAppTask().AppTaskMain(NULL);