2021-10-20 04:48:09 -04:00
|
|
|
/*
|
2024-01-29 04:50:48 -05:00
|
|
|
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
|
2021-10-20 04:48:09 -04:00
|
|
|
*
|
2021-12-01 22:43:44 -05:00
|
|
|
* SPDX-License-Identifier: CC0-1.0
|
2021-10-20 04:48:09 -04:00
|
|
|
*
|
|
|
|
* OpenThread Border Router Example
|
|
|
|
*
|
|
|
|
* This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, this
|
|
|
|
* software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
* CONDITIONS OF ANY KIND, either express or implied.
|
2022-05-30 03:53:11 -04:00
|
|
|
*/
|
2021-06-07 04:13:58 -04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-01-29 04:50:48 -05:00
|
|
|
#include "sdkconfig.h"
|
2021-06-07 04:13:58 -04:00
|
|
|
#include "esp_check.h"
|
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_event.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_netif.h"
|
|
|
|
#include "esp_openthread.h"
|
|
|
|
#include "esp_openthread_border_router.h"
|
2021-09-27 00:10:00 -04:00
|
|
|
#include "esp_openthread_cli.h"
|
2021-06-07 04:13:58 -04:00
|
|
|
#include "esp_openthread_lock.h"
|
|
|
|
#include "esp_openthread_netif_glue.h"
|
|
|
|
#include "esp_openthread_types.h"
|
2022-05-30 03:53:11 -04:00
|
|
|
#include "esp_ot_cli_extension.h"
|
2021-07-09 00:18:41 -04:00
|
|
|
#include "esp_ot_config.h"
|
2022-05-30 03:53:11 -04:00
|
|
|
#include "esp_ot_wifi_cmd.h"
|
|
|
|
#include "esp_vfs_dev.h"
|
2021-06-07 04:13:58 -04:00
|
|
|
#include "esp_vfs_eventfd.h"
|
|
|
|
#include "esp_wifi.h"
|
2021-06-17 03:44:19 -04:00
|
|
|
#include "mdns.h"
|
2021-06-07 04:13:58 -04:00
|
|
|
#include "nvs_flash.h"
|
|
|
|
#include "protocol_examples_common.h"
|
|
|
|
#include "driver/uart.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "hal/uart_types.h"
|
|
|
|
#include "openthread/error.h"
|
2021-09-23 05:23:38 -04:00
|
|
|
#include "openthread/logging.h"
|
2021-06-07 04:13:58 -04:00
|
|
|
#include "openthread/tasklet.h"
|
|
|
|
|
2024-01-29 04:50:48 -05:00
|
|
|
#if CONFIG_OPENTHREAD_STATE_INDICATOR_ENABLE
|
|
|
|
#include "ot_led_strip.h"
|
|
|
|
#endif
|
|
|
|
|
2021-06-07 04:13:58 -04:00
|
|
|
#define TAG "esp_ot_br"
|
|
|
|
|
2023-08-21 04:34:17 -04:00
|
|
|
#if CONFIG_EXTERNAL_COEX_ENABLE
|
|
|
|
static void ot_br_external_coexist_init(void)
|
|
|
|
{
|
|
|
|
esp_external_coex_gpio_set_t gpio_pin = ESP_OPENTHREAD_DEFAULT_EXTERNAL_COEX_CONFIG();
|
|
|
|
esp_external_coex_set_work_mode(EXTERNAL_COEX_LEADER_ROLE);
|
|
|
|
ESP_ERROR_CHECK(esp_enable_extern_coex_gpio_pin(CONFIG_EXTERNAL_COEX_WIRE_TYPE, gpio_pin));
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_EXTERNAL_COEX_ENABLE */
|
|
|
|
|
2021-06-07 04:13:58 -04:00
|
|
|
static void ot_task_worker(void *aContext)
|
|
|
|
{
|
|
|
|
esp_openthread_platform_config_t config = {
|
2021-07-09 00:18:41 -04:00
|
|
|
.radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(),
|
|
|
|
.host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(),
|
2021-06-28 07:20:51 -04:00
|
|
|
.port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(),
|
2021-06-07 04:13:58 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_OPENTHREAD();
|
2022-05-30 03:53:11 -04:00
|
|
|
esp_netif_t *openthread_netif = esp_netif_new(&cfg);
|
2021-06-07 04:13:58 -04:00
|
|
|
assert(openthread_netif != NULL);
|
2021-12-01 22:43:44 -05:00
|
|
|
|
2023-06-02 05:38:23 -04:00
|
|
|
// Initialize the OpenThread stack
|
2021-06-07 04:13:58 -04:00
|
|
|
ESP_ERROR_CHECK(esp_openthread_init(&config));
|
2021-06-28 07:20:51 -04:00
|
|
|
ESP_ERROR_CHECK(esp_netif_attach(openthread_netif, esp_openthread_netif_glue_init(&config)));
|
2024-07-08 23:12:31 -04:00
|
|
|
esp_openthread_lock_acquire(portMAX_DELAY);
|
2021-09-23 05:23:38 -04:00
|
|
|
(void)otLoggingSetLevel(CONFIG_LOG_DEFAULT_LEVEL);
|
2021-09-27 00:10:00 -04:00
|
|
|
esp_openthread_cli_init();
|
2022-07-13 05:14:29 -04:00
|
|
|
esp_cli_custom_command_init();
|
2024-07-08 23:12:31 -04:00
|
|
|
esp_openthread_cli_create_task();
|
2021-06-07 04:13:58 -04:00
|
|
|
esp_openthread_lock_release();
|
|
|
|
|
|
|
|
// Run the main loop
|
|
|
|
esp_openthread_launch_mainloop();
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
esp_openthread_netif_glue_deinit();
|
2023-12-15 05:35:34 -05:00
|
|
|
esp_netif_destroy(openthread_netif);
|
2021-06-07 04:13:58 -04:00
|
|
|
esp_vfs_eventfd_unregister();
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
2024-07-08 23:12:31 -04:00
|
|
|
void ot_br_init(void *ctx)
|
2021-06-07 04:13:58 -04:00
|
|
|
{
|
2022-09-21 04:59:08 -04:00
|
|
|
#if CONFIG_EXAMPLE_CONNECT_WIFI
|
2021-12-01 22:43:44 -05:00
|
|
|
#if CONFIG_OPENTHREAD_BR_AUTO_START
|
2021-06-07 04:13:58 -04:00
|
|
|
ESP_ERROR_CHECK(example_connect());
|
2024-07-08 23:12:31 -04:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_MAX_MODEM));
|
2023-03-09 01:48:29 -05:00
|
|
|
#if CONFIG_ESP_COEX_SW_COEXIST_ENABLE && CONFIG_OPENTHREAD_RADIO_NATIVE
|
|
|
|
ESP_ERROR_CHECK(esp_coex_wifi_i154_enable());
|
|
|
|
#else
|
2023-08-21 04:34:17 -04:00
|
|
|
|
|
|
|
#if CONFIG_EXTERNAL_COEX_ENABLE
|
|
|
|
ot_br_external_coexist_init();
|
|
|
|
#endif // CONFIG_EXTERNAL_COEX_ENABLE
|
|
|
|
|
2023-03-09 01:48:29 -05:00
|
|
|
#endif
|
2021-12-01 22:43:44 -05:00
|
|
|
esp_openthread_set_backbone_netif(get_example_netif());
|
|
|
|
#else
|
|
|
|
esp_ot_wifi_netif_init();
|
|
|
|
esp_openthread_set_backbone_netif(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"));
|
|
|
|
#endif // CONFIG_OPENTHREAD_BR_AUTO_START
|
2022-09-21 04:59:08 -04:00
|
|
|
#elif CONFIG_EXAMPLE_CONNECT_ETHERNET
|
|
|
|
ESP_ERROR_CHECK(example_connect());
|
|
|
|
esp_openthread_set_backbone_netif(get_example_netif());
|
|
|
|
#else
|
|
|
|
ESP_LOGE(TAG, "ESP-Openthread has not set backbone netif");
|
|
|
|
#endif // CONFIG_EXAMPLE_CONNECT_WIFI
|
|
|
|
|
2021-06-17 03:44:19 -04:00
|
|
|
ESP_ERROR_CHECK(mdns_init());
|
|
|
|
ESP_ERROR_CHECK(mdns_hostname_set("esp-ot-br"));
|
2024-07-08 23:12:31 -04:00
|
|
|
|
|
|
|
// Initialize border routing features
|
|
|
|
esp_openthread_lock_acquire(portMAX_DELAY);
|
|
|
|
#if CONFIG_OPENTHREAD_STATE_INDICATOR_ENABLE
|
|
|
|
ESP_ERROR_CHECK(esp_openthread_state_indicator_init(esp_openthread_get_instance()));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_OPENTHREAD_BR_AUTO_START
|
|
|
|
ESP_ERROR_CHECK(esp_openthread_border_router_init());
|
|
|
|
otOperationalDatasetTlvs dataset;
|
|
|
|
otError error = otDatasetGetActiveTlvs(esp_openthread_get_instance(), &dataset);
|
|
|
|
ESP_ERROR_CHECK(esp_openthread_auto_start((error == OT_ERROR_NONE) ? &dataset : NULL));
|
|
|
|
#endif // CONFIG_OPENTHREAD_BR_AUTO_START
|
|
|
|
|
|
|
|
esp_openthread_lock_release();
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void app_main(void)
|
|
|
|
{
|
|
|
|
// Used eventfds:
|
|
|
|
// * netif
|
|
|
|
// * task queue
|
|
|
|
// * border router
|
|
|
|
esp_vfs_eventfd_config_t eventfd_config = {
|
|
|
|
#if CONFIG_OPENTHREAD_RADIO_NATIVE || CONFIG_OPENTHREAD_RADIO_SPINEL_SPI
|
|
|
|
// * radio driver (A native radio device needs a eventfd for radio driver.)
|
|
|
|
// * SpiSpinelInterface (The Spi Spinel Interface needs a eventfd.)
|
|
|
|
// The above will not exist at the same time.
|
|
|
|
.max_fds = 4,
|
|
|
|
#else
|
|
|
|
.max_fds = 3,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
ESP_ERROR_CHECK(esp_vfs_eventfd_register(&eventfd_config));
|
|
|
|
ESP_ERROR_CHECK(nvs_flash_init());
|
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
xTaskCreate(ot_task_worker, "ot_br_main", 8192, xTaskGetCurrentTaskHandle(), 5, NULL);
|
|
|
|
xTaskCreate(ot_br_init, "ot_br_init", 6144, NULL, 4, NULL);
|
2021-06-07 04:13:58 -04:00
|
|
|
}
|