3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2019 Google LLC.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
29 #include <mbedtls/threading.h>
31 #include <platform/CHIPDeviceLayer.h>
32 #include <platform/KeyValueStoreManager.h>
33 #include <support/CHIPMem.h>
34 #include <support/CHIPPlatformMemory.h>
38 #include "AppConfig.h"
39 #include "DataModelHandler.h"
41 #include "init_efrPlatform.h"
47 #if CHIP_ENABLE_OPENTHREAD
48 #include <mbedtls/platform.h>
49 #include <openthread/cli.h>
50 #include <openthread/dataset.h>
51 #include <openthread/error.h>
52 #include <openthread/heap.h>
53 #include <openthread/icmp6.h>
54 #include <openthread/instance.h>
55 #include <openthread/link.h>
56 #include <openthread/platform/openthread-system.h>
57 #include <openthread/tasklet.h>
58 #include <openthread/thread.h>
59 #endif // CHIP_ENABLE_OPENTHREAD
61 using namespace ::chip;
62 using namespace ::chip::Inet;
63 using namespace ::chip::DeviceLayer;
65 #define UNUSED_PARAMETER(a) (a = a)
67 volatile int apperror_cnt;
68 // ================================================================================
70 //=================================================================================
71 void appError(int err)
73 EFR32_LOG("!!!!!!!!!!!! App Critical Error: %d !!!!!!!!!!!", err);
74 portDISABLE_INTERRUPTS();
79 // ================================================================================
81 // ================================================================================
82 extern "C" void vApplicationIdleHook(void)
84 // FreeRTOS Idle callback
86 // Check CHIP Config nvm3 and repack flash if necessary.
87 Internal::EFR32Config::RepackNvm3Flash();
90 // ================================================================================
92 // ================================================================================
95 int ret = CHIP_ERROR_MAX;
98 mbedtls_platform_set_calloc_free(CHIPPlatformMemoryCalloc, CHIPPlatformMemoryFree);
100 // Initialize mbedtls threading support on EFR32
103 EFR32_LOG("==================================================");
104 EFR32_LOG("chip-efr32-lock-example starting");
105 EFR32_LOG("==================================================");
107 EFR32_LOG("Init CHIP Stack");
109 // Init Chip memory management before the stack
110 chip::Platform::MemoryInit();
111 chip::DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().Init();
113 ret = PlatformMgr().InitChipStack();
114 if (ret != CHIP_NO_ERROR)
116 EFR32_LOG("PlatformMgr().InitChipStack() failed");
119 chip::DeviceLayer::ConnectivityMgr().SetBLEDeviceName("EFR32_LOCK");
120 #if CHIP_ENABLE_OPENTHREAD
121 EFR32_LOG("Initializing OpenThread stack");
122 ret = ThreadStackMgr().InitThreadStack();
123 if (ret != CHIP_NO_ERROR)
125 EFR32_LOG("ThreadStackMgr().InitThreadStack() failed");
129 ret = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_Router);
130 if (ret != CHIP_NO_ERROR)
132 EFR32_LOG("ConnectivityMgr().SetThreadDeviceType() failed");
135 #endif // CHIP_ENABLE_OPENTHREAD
137 EFR32_LOG("Starting Platform Manager Event Loop");
138 ret = PlatformMgr().StartEventLoopTask();
139 if (ret != CHIP_NO_ERROR)
141 EFR32_LOG("PlatformMgr().StartEventLoopTask() failed");
145 #if CHIP_ENABLE_OPENTHREAD
146 EFR32_LOG("Starting OpenThread task");
148 // Start OpenThread task
149 ret = ThreadStackMgrImpl().StartThreadTask();
150 if (ret != CHIP_NO_ERROR)
152 EFR32_LOG("ThreadStackMgr().StartThreadTask() failed");
155 #endif // CHIP_ENABLE_OPENTHREAD
157 EFR32_LOG("Starting App Task");
158 ret = GetAppTask().StartAppTask();
159 if (ret != CHIP_NO_ERROR)
161 EFR32_LOG("GetAppTask().Init() failed");
165 EFR32_LOG("Starting FreeRTOS scheduler");
166 vTaskStartScheduler();
168 chip::Platform::MemoryShutdown();
170 // Should never get here.
171 EFR32_LOG("vTaskStartScheduler() failed");