3 * Copyright (c) 2020 Project CHIP Authors
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "esp_event.h"
19 #include "esp_heap_caps_init.h"
21 #include "esp_netif.h"
22 #include "esp_spi_flash.h"
23 #include "esp_system.h"
25 #include "freertos/FreeRTOS.h"
26 #include "freertos/task.h"
27 #include "nvs_flash.h"
31 #include <crypto/CHIPCryptoPAL.h>
32 #include <platform/CHIPDeviceLayer.h>
33 #include <support/ErrorStr.h>
34 #include <support/UnitTestRegistration.h>
36 using namespace ::chip;
37 using namespace ::chip::DeviceLayer;
39 const char * TAG = "CHIP-tests";
41 static int app_entropy_source(void * data, unsigned char * output, size_t len, size_t * olen)
43 esp_fill_random(output, len);
48 static void tester_task(void * pvParameters)
50 ESP_LOGI(TAG, "Starting CHIP tests!");
51 int status = RunRegisteredUnitTests();
52 ESP_LOGI(TAG, "CHIP test status: %d", status);
56 extern "C" void app_main()
58 esp_chip_info_t chip_info;
59 esp_chip_info(&chip_info);
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" : "");
64 ESP_LOGI(TAG, "silicon revision %d, ", chip_info.revision);
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");
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
76 // Initialize the ESP NVS layer.
77 err = nvs_flash_init();
78 if (err != CHIP_NO_ERROR)
80 ESP_LOGE(TAG, "nvs_flash_init() failed: %s", ErrorStr(err));
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)
89 ESP_LOGE(TAG, "PlatformMgr().InitLocks() failed: %s", ErrorStr(err));
93 err = esp_netif_init();
94 if (err != CHIP_NO_ERROR)
96 ESP_LOGE(TAG, "esp_netif_init() failed: %s", ErrorStr(err));
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)
104 ESP_LOGE(TAG, "esp_event_loop_create_default() failed: %s", ErrorStr(err));
107 esp_netif_create_default_wifi_ap();
108 esp_netif_create_default_wifi_sta();
110 err = esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent, NULL);
111 if (err != CHIP_NO_ERROR)
113 ESP_LOGE(TAG, "esp_event_handler_register() failed for WIFI_EVENT: %s", ErrorStr(err));
116 err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent, NULL);
117 if (err != CHIP_NO_ERROR)
119 ESP_LOGE(TAG, "esp_event_handler_register() failed for IP_EVENT: %s", ErrorStr(err));
123 err = Crypto::add_entropy_source(app_entropy_source, NULL, 16);
124 if (err != CHIP_NO_ERROR)
126 ESP_LOGE(TAG, "add_entropy_source() failed: %s", ErrorStr(err));
130 xTaskCreate(tester_task, "tester", 12288, (void *) NULL, tskIDLE_PRIORITY + 10, NULL);
134 vTaskDelay(50 / portTICK_PERIOD_MS);