Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / lighting-app / efr32 / src / main.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2019 Google LLC.
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 #include <bsp.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include <stdarg.h>
25 #include <stdbool.h>
26 #include <stdint.h>
27
28 #include <FreeRTOS.h>
29 #include <mbedtls/threading.h>
30
31 #include <platform/CHIPDeviceLayer.h>
32 #include <platform/KeyValueStoreManager.h>
33 #include <support/CHIPMem.h>
34 #include <support/CHIPPlatformMemory.h>
35
36 #include <AppTask.h>
37
38 #include "AppConfig.h"
39 #include "DataModelHandler.h"
40 #include "Server.h"
41 #include "init_efrPlatform.h"
42
43 #ifdef HEAP_MONITORING
44 #include "MemMonitoring.h"
45 #endif
46
47 #if DISPLAY_ENABLED
48 #include "lcd.h"
49 #endif
50
51 #if CHIP_ENABLE_OPENTHREAD
52 #include <mbedtls/platform.h>
53 #include <openthread/cli.h>
54 #include <openthread/dataset.h>
55 #include <openthread/error.h>
56 #include <openthread/heap.h>
57 #include <openthread/icmp6.h>
58 #include <openthread/instance.h>
59 #include <openthread/link.h>
60 #include <openthread/platform/openthread-system.h>
61 #include <openthread/tasklet.h>
62 #include <openthread/thread.h>
63 #endif // CHIP_ENABLE_OPENTHREAD
64
65 #if PW_RPC_ENABLED
66 #include "Rpc.h"
67 #endif
68
69 using namespace ::chip;
70 using namespace ::chip::Inet;
71 using namespace ::chip::DeviceLayer;
72
73 #define UNUSED_PARAMETER(a) (a = a)
74
75 volatile int apperror_cnt;
76 // ================================================================================
77 // App Error
78 //=================================================================================
79 void appError(int err)
80 {
81     EFR32_LOG("!!!!!!!!!!!! App Critical Error: %d !!!!!!!!!!!", err);
82     portDISABLE_INTERRUPTS();
83     while (1)
84         ;
85 }
86
87 // ================================================================================
88 // FreeRTOS Callbacks
89 // ================================================================================
90 extern "C" void vApplicationIdleHook(void)
91 {
92     // FreeRTOS Idle callback
93
94     // Check CHIP Config nvm3 and repack flash if necessary.
95     Internal::EFR32Config::RepackNvm3Flash();
96 }
97
98 // ================================================================================
99 // Main Code
100 // ================================================================================
101 int main(void)
102 {
103     int ret = CHIP_ERROR_MAX;
104
105     init_efrPlatform();
106     mbedtls_platform_set_calloc_free(CHIPPlatformMemoryCalloc, CHIPPlatformMemoryFree);
107
108 #if PW_RPC_ENABLED
109     chip::rpc::Init();
110 #endif
111
112 #ifdef HEAP_MONITORING
113     MemMonitoring::startHeapMonitoring();
114 #endif
115
116     // Initialize mbedtls threading support on EFR32
117     THREADING_setup();
118
119     EFR32_LOG("==================================================");
120     EFR32_LOG("chip-efr32-lighting-example starting");
121     EFR32_LOG("==================================================");
122
123     EFR32_LOG("Init CHIP Stack");
124
125     // Init Chip memory management before the stack
126     chip::Platform::MemoryInit();
127     chip::DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().Init();
128
129     ret = PlatformMgr().InitChipStack();
130     if (ret != CHIP_NO_ERROR)
131     {
132         EFR32_LOG("PlatformMgr().InitChipStack() failed");
133         appError(ret);
134     }
135     chip::DeviceLayer::ConnectivityMgr().SetBLEDeviceName("EFR32_LIGHT");
136 #if CHIP_ENABLE_OPENTHREAD
137     EFR32_LOG("Initializing OpenThread stack");
138     ret = ThreadStackMgr().InitThreadStack();
139     if (ret != CHIP_NO_ERROR)
140     {
141         EFR32_LOG("ThreadStackMgr().InitThreadStack() failed");
142         appError(ret);
143     }
144
145     ret = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_Router);
146     if (ret != CHIP_NO_ERROR)
147     {
148         EFR32_LOG("ConnectivityMgr().SetThreadDeviceType() failed");
149         appError(ret);
150     }
151 #endif // CHIP_ENABLE_OPENTHREAD
152
153     EFR32_LOG("Starting Platform Manager Event Loop");
154     ret = PlatformMgr().StartEventLoopTask();
155     if (ret != CHIP_NO_ERROR)
156     {
157         EFR32_LOG("PlatformMgr().StartEventLoopTask() failed");
158         appError(ret);
159     }
160
161 #if CHIP_ENABLE_OPENTHREAD
162     EFR32_LOG("Starting OpenThread task");
163
164     // Start OpenThread task
165     ret = ThreadStackMgrImpl().StartThreadTask();
166     if (ret != CHIP_NO_ERROR)
167     {
168         EFR32_LOG("ThreadStackMgr().StartThreadTask() failed");
169         appError(ret);
170     }
171 #endif // CHIP_ENABLE_OPENTHREAD
172     EFR32_LOG("Starting App Task");
173     ret = GetAppTask().StartAppTask();
174     if (ret != CHIP_NO_ERROR)
175     {
176         EFR32_LOG("GetAppTask().Init() failed");
177         appError(ret);
178     }
179
180     EFR32_LOG("Starting FreeRTOS scheduler");
181     vTaskStartScheduler();
182
183     chip::Platform::MemoryShutdown();
184
185     // Should never get here.
186     EFR32_LOG("vTaskStartScheduler() failed");
187     appError(ret);
188 }