Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / test_driver / esp32 / main / main_app.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
5  *    Licensed under the Apache License, Version 2.0 (the "License");
6  *    you may not use this file except in compliance with the License.
7  *    You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *    Unless required by applicable law or agreed to in writing, software
12  *    distributed under the License is distributed on an "AS IS" BASIS,
13  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *    See the License for the specific language governing permissions and
15  *    limitations under the License.
16  */
17
18 #include "esp_event.h"
19 #include "esp_heap_caps_init.h"
20 #include "esp_log.h"
21 #include "esp_netif.h"
22 #include "esp_spi_flash.h"
23 #include "esp_system.h"
24 #include "esp_wifi.h"
25 #include "freertos/FreeRTOS.h"
26 #include "freertos/task.h"
27 #include "nvs_flash.h"
28
29 #include <stdio.h>
30
31 #include <crypto/CHIPCryptoPAL.h>
32 #include <platform/CHIPDeviceLayer.h>
33 #include <support/ErrorStr.h>
34 #include <support/UnitTestRegistration.h>
35
36 using namespace ::chip;
37 using namespace ::chip::DeviceLayer;
38
39 const char * TAG = "CHIP-tests";
40
41 static int app_entropy_source(void * data, unsigned char * output, size_t len, size_t * olen)
42 {
43     esp_fill_random(output, len);
44     *olen = len;
45     return 0;
46 }
47
48 static void tester_task(void * pvParameters)
49 {
50     ESP_LOGI(TAG, "Starting CHIP tests!");
51     int status = RunRegisteredUnitTests();
52     ESP_LOGI(TAG, "CHIP test status: %d", status);
53     exit(status);
54 }
55
56 extern "C" void app_main()
57 {
58     esp_chip_info_t chip_info;
59     esp_chip_info(&chip_info);
60
61     ESP_LOGI(TAG, "This is ESP32 chip with %d CPU cores, WiFi%s%s, ", chip_info.cores,
62              (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "", (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");
63
64     ESP_LOGI(TAG, "silicon revision %d, ", chip_info.revision);
65
66     ESP_LOGI(TAG, "%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
67              (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
68
69     CHIP_ERROR err; // A quick note about errors: CHIP adopts the error type and numbering
70                     // convention of the environment into which it is ported.  Thus esp_err_t
71                     // and CHIP_ERROR are in fact the same type, and both ESP-IDF errors
72                     // and CHIO-specific errors can be stored in the same value without
73                     // ambiguity.  For convenience, ESP_OK and CHIP_NO_ERROR are mapped
74                     // to the same value.
75
76     // Initialize the ESP NVS layer.
77     err = nvs_flash_init();
78     if (err != CHIP_NO_ERROR)
79     {
80         ESP_LOGE(TAG, "nvs_flash_init() failed: %s", ErrorStr(err));
81         exit(err);
82     }
83
84     // Initialize the LwIP core lock.  This must be done before the ESP
85     // tcpip_adapter layer is initialized.
86     err = PlatformMgrImpl().InitLwIPCoreLock();
87     if (err != CHIP_NO_ERROR)
88     {
89         ESP_LOGE(TAG, "PlatformMgr().InitLocks() failed: %s", ErrorStr(err));
90         exit(err);
91     }
92
93     err = esp_netif_init();
94     if (err != CHIP_NO_ERROR)
95     {
96         ESP_LOGE(TAG, "esp_netif_init() failed: %s", ErrorStr(err));
97         exit(err);
98     }
99
100     // Arrange for the ESP event loop to deliver events into the CHIP Device layer.
101     err = esp_event_loop_create_default();
102     if (err != CHIP_NO_ERROR)
103     {
104         ESP_LOGE(TAG, "esp_event_loop_create_default() failed: %s", ErrorStr(err));
105         exit(err);
106     }
107     esp_netif_create_default_wifi_ap();
108     esp_netif_create_default_wifi_sta();
109
110     err = esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent, NULL);
111     if (err != CHIP_NO_ERROR)
112     {
113         ESP_LOGE(TAG, "esp_event_handler_register() failed for WIFI_EVENT: %s", ErrorStr(err));
114         exit(err);
115     }
116     err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent, NULL);
117     if (err != CHIP_NO_ERROR)
118     {
119         ESP_LOGE(TAG, "esp_event_handler_register() failed for IP_EVENT: %s", ErrorStr(err));
120         exit(err);
121     }
122
123     err = Crypto::add_entropy_source(app_entropy_source, NULL, 16);
124     if (err != CHIP_NO_ERROR)
125     {
126         ESP_LOGE(TAG, "add_entropy_source() failed: %s", ErrorStr(err));
127         exit(err);
128     }
129
130     xTaskCreate(tester_task, "tester", 12288, (void *) NULL, tskIDLE_PRIORITY + 10, NULL);
131
132     while (1)
133     {
134         vTaskDelay(50 / portTICK_PERIOD_MS);
135     }
136 }