Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / lock-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 #if DISPLAY_ENABLED
44 #include "lcd.h"
45 #endif
46
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
60
61 using namespace ::chip;
62 using namespace ::chip::Inet;
63 using namespace ::chip::DeviceLayer;
64
65 #define UNUSED_PARAMETER(a) (a = a)
66
67 volatile int apperror_cnt;
68 // ================================================================================
69 // App Error
70 //=================================================================================
71 void appError(int err)
72 {
73     EFR32_LOG("!!!!!!!!!!!! App Critical Error: %d !!!!!!!!!!!", err);
74     portDISABLE_INTERRUPTS();
75     while (1)
76         ;
77 }
78
79 // ================================================================================
80 // FreeRTOS Callbacks
81 // ================================================================================
82 extern "C" void vApplicationIdleHook(void)
83 {
84     // FreeRTOS Idle callback
85
86     // Check CHIP Config nvm3 and repack flash if necessary.
87     Internal::EFR32Config::RepackNvm3Flash();
88 }
89
90 // ================================================================================
91 // Main Code
92 // ================================================================================
93 int main(void)
94 {
95     int ret = CHIP_ERROR_MAX;
96
97     init_efrPlatform();
98     mbedtls_platform_set_calloc_free(CHIPPlatformMemoryCalloc, CHIPPlatformMemoryFree);
99
100     // Initialize mbedtls threading support on EFR32
101     THREADING_setup();
102
103     EFR32_LOG("==================================================");
104     EFR32_LOG("chip-efr32-lock-example starting");
105     EFR32_LOG("==================================================");
106
107     EFR32_LOG("Init CHIP Stack");
108
109     // Init Chip memory management before the stack
110     chip::Platform::MemoryInit();
111     chip::DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().Init();
112
113     ret = PlatformMgr().InitChipStack();
114     if (ret != CHIP_NO_ERROR)
115     {
116         EFR32_LOG("PlatformMgr().InitChipStack() failed");
117         appError(ret);
118     }
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)
124     {
125         EFR32_LOG("ThreadStackMgr().InitThreadStack() failed");
126         appError(ret);
127     }
128
129     ret = ConnectivityMgr().SetThreadDeviceType(ConnectivityManager::kThreadDeviceType_Router);
130     if (ret != CHIP_NO_ERROR)
131     {
132         EFR32_LOG("ConnectivityMgr().SetThreadDeviceType() failed");
133         appError(ret);
134     }
135 #endif // CHIP_ENABLE_OPENTHREAD
136
137     EFR32_LOG("Starting Platform Manager Event Loop");
138     ret = PlatformMgr().StartEventLoopTask();
139     if (ret != CHIP_NO_ERROR)
140     {
141         EFR32_LOG("PlatformMgr().StartEventLoopTask() failed");
142         appError(ret);
143     }
144
145 #if CHIP_ENABLE_OPENTHREAD
146     EFR32_LOG("Starting OpenThread task");
147
148     // Start OpenThread task
149     ret = ThreadStackMgrImpl().StartThreadTask();
150     if (ret != CHIP_NO_ERROR)
151     {
152         EFR32_LOG("ThreadStackMgr().StartThreadTask() failed");
153         appError(ret);
154     }
155 #endif // CHIP_ENABLE_OPENTHREAD
156
157     EFR32_LOG("Starting App Task");
158     ret = GetAppTask().StartAppTask();
159     if (ret != CHIP_NO_ERROR)
160     {
161         EFR32_LOG("GetAppTask().Init() failed");
162         appError(ret);
163     }
164
165     EFR32_LOG("Starting FreeRTOS scheduler");
166     vTaskStartScheduler();
167
168     chip::Platform::MemoryShutdown();
169
170     // Should never get here.
171     EFR32_LOG("vTaskStartScheduler() failed");
172     appError(ret);
173 }