feat(openthread): examples adaptation of new APIs

This commit is contained in:
Xu Si Yu
2025-10-21 19:55:14 +08:00
parent 8436324680
commit 25fbc00c0e
38 changed files with 270 additions and 803 deletions
+17 -17
View File
@@ -56,11 +56,11 @@ In order to run the example on single SoC which supports both Wi-Fi and Thread,
Two ways are provided to setup the Thread Border Router in this example:
- Auto Start
Enable `OPENTHREAD_BR_AUTO_START`, configure the `CONFIG_EXAMPLE_WIFI_SSID` and `CONFIG_EXAMPLE_WIFI_PASSWORD` with your access point's ssid and psk.
Enable `OPENTHREAD_NETWORK_AUTO_START`, configure the `CONFIG_EXAMPLE_WIFI_SSID` and `CONFIG_EXAMPLE_WIFI_PASSWORD` with your access point's ssid and psk.
The device will connect to Wi-Fi and form a Thread network automatically after boot up.
- Manual mode
Disable `OPENTHREAD_BR_AUTO_START` and enable `OPENTHREAD_CLI_ESP_EXTENSION`. `wifi` command will be added for connecting the device to the Wi-Fi network.
Disable `OPENTHREAD_NETWORK_AUTO_START` and enable `OPENTHREAD_CLI_ESP_EXTENSION`. `wifi` command will be added for connecting the device to the Wi-Fi network.
If the `CONFIG_EXAMPLE_CONNECT_ETHERNET` option is enabled, the device will connect to `Ethernet`, form a Thread network and act as a Ethernet based Thread Border Router.
@@ -71,14 +71,14 @@ Build the project and flash it to the board, then run monitor tool to view seria
```
idf.py -p PORT build flash monitor
```
If the `OPENTHREAD_BR_AUTO_START` option is enabled, The device will be connected to the configured Wi-Fi and Thread network automatically then act as the border router.
If the `OPENTHREAD_NETWORK_AUTO_START` option is enabled, The device will be connected to the configured Wi-Fi and Thread network automatically then act as the border router.
Otherwise, you need to manually configure the networks with CLI commands.
`wifi` command can be used to configure the Wi-Fi network.
```bash
> wifi
esp32s3> ot wifi
--wifi parameter---
connect
-s : wifi ssid
@@ -96,7 +96,7 @@ Done
To join a Wi-Fi network, please use the `wifi connect` command:
```bash
> wifi connect -s threadcertAP -p threadcertAP
esp32s3> ot wifi connect -s threadcertAP -p threadcertAP
ssid: threadcertAP
psk: threadcertAP
I (11331) wifi:wifi driver task: 3ffd06e4, prio:23, stack:6656, core=0
@@ -117,7 +117,7 @@ Done
To get the state of the Wi-Fi network:
```bash
> wifi state
esp32s3> ot wifi state
connected
Done
```
@@ -165,7 +165,7 @@ For mobile devices, the route table rules will be automatically configured after
Now in the Thread end device, check the IP addresses:
```
> ipaddr
esp32h2> ot ipaddr
fde6:75ff:def4:3bc3:9e9e:3ef:4245:28b5
fdde:ad00:beef:0:0:ff:fe00:c402
fdde:ad00:beef:0:ad4a:9a9a:3cd6:e423
@@ -192,13 +192,13 @@ The newly introduced service registration protocol([SRP](https://datatracker.iet
Now we'll publish the service `my-service._test._udp` with hostname `test0` and port 12345
```
> srp client host name test0
esp32h2> ot srp client host name test0
Done
> srp client host address fde6:75ff:def4:3bc3:9e9e:3ef:4245:28b5
esp32h2> ot srp client host address fde6:75ff:def4:3bc3:9e9e:3ef:4245:28b5
Done
> srp client service add my-service _test._udp 12345
esp32h2> ot srp client service add my-service _test._udp 12345
Done
> srp client autostart enable
esp32h2> ot srp client autostart enable
Done
```
@@ -233,7 +233,7 @@ Then get the border router's OMR prefix global unicast address(or ML-EID), and c
On the border router:
```
> ipaddr
esp32s3> ot ipaddr
fdde:ad00:beef:0:0:ff:fe00:fc10
fd9b:347f:93f7:1:1003:8f00:bcc1:3038
fdde:ad00:beef:0:0:ff:fe00:fc00
@@ -245,19 +245,19 @@ Done
On the Thread end device:
```
> dns config fd9b:347f:93f7:1:1003:8f00:bcc1:3038
esp32h2> ot dns config fd9b:347f:93f7:1:1003:8f00:bcc1:3038
(or
> dns config fdde:ad00:beef:0:f891:287:866:776)
esp32h2> ot dns config fdde:ad00:beef:0:f891:287:866:776)
Done
```
Now the service published on the Host can be discovered on the Thread end device.
```
> dns resolve FA001208.default.service.arpa.
esp32h2> ot dns resolve FA001208.default.service.arpa.
DNS response for FA001208.default.service.arpa. - fdde:ad00:beef:cafe:b939:26be:7516:b87e TTL:120
Done
> dns browse _test._udp.default.service.arpa.
esp32h2> ot dns browse _test._udp.default.service.arpa.
DNS browse response for _test._udp.default.service.arpa.
testhost
Port:5683, Priority:0, Weight:0, TTL:120
@@ -266,7 +266,7 @@ testhost
TXT:[test=31, dn=616162626262] TTL:120
Done
> dns service testhost _test._udp.default.service.arpa.
esp32h2> ot dns service testhost _test._udp.default.service.arpa.
DNS service resolution response for testhost for service _test._udp.default.service.arpa.
Port:5683, Priority:0, Weight:0, TTL:120
Host:FA001208.default.service.arpa.
@@ -1,13 +1,5 @@
menu "OpenThread Border Router Example"
config OPENTHREAD_BR_AUTO_START
bool 'Enable the automatic start mode in Thread Border Router.'
default n
help
If enabled, The Thread Border Router will connect to Wi-Fi with pre-configured
SSID and PSK, and then form a Thread network automatically. Otherwise, user need
to configure Wi-Fi and Thread manually.
config OPENTHREAD_SUPPORT_HW_RESET_RCP
bool 'Enable hardware RCP resetting'
default n
@@ -20,33 +12,4 @@ menu "OpenThread Border Router Example"
depends on OPENTHREAD_SUPPORT_HW_RESET_RCP
default 7
menu "External coexist wire type and pin config"
config EXTERNAL_COEX_WIRE_TYPE
int "The wire_type of external coexist"
depends on ESP_COEX_EXTERNAL_COEXIST_ENABLE
default 3
range 0 3
help
Select wire_type for external coexist, the wire_type define in external_coex_wire_t.
config EXTERNAL_COEX_REQUEST_PIN
int "The number of external coexist request pin"
depends on ESP_COEX_EXTERNAL_COEXIST_ENABLE && (EXTERNAL_COEX_WIRE_TYPE >= 0)
default 0
config EXTERNAL_COEX_GRANT_PIN
int "The number of external coexist grant pin"
depends on ESP_COEX_EXTERNAL_COEXIST_ENABLE && (EXTERNAL_COEX_WIRE_TYPE >= 1)
default 1
config EXTERNAL_COEX_PRIORITY_PIN
int "The number of external coexist priority pin"
depends on ESP_COEX_EXTERNAL_COEXIST_ENABLE && (EXTERNAL_COEX_WIRE_TYPE >= 2)
default 2
config EXTERNAL_COEX_TX_LINE_PIN
int "The number of external coexist tx_line pin"
depends on ESP_COEX_EXTERNAL_COEXIST_ENABLE && (EXTERNAL_COEX_WIRE_TYPE = 3)
default 3
endmenu # External coexist wire type and pin config
endmenu
+35 -135
View File
@@ -17,50 +17,31 @@
#include "sdkconfig.h"
#include "esp_check.h"
#include "esp_coexist.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"
#include "esp_openthread_cli.h"
#include "esp_openthread_lock.h"
#include "esp_openthread_netif_glue.h"
#include "esp_openthread_spinel.h"
#include "esp_openthread_types.h"
#if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
#include "esp_ot_cli_extension.h"
#endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
#include "esp_ot_config.h"
#include "esp_ot_wifi_cmd.h"
#include "esp_vfs_dev.h"
#include "esp_vfs_eventfd.h"
#include "esp_wifi.h"
#include "mdns.h"
#include "nvs_flash.h"
#include "protocol_examples_common.h"
#include "driver/gpio.h"
#include "driver/uart.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "hal/uart_types.h"
#include "openthread/error.h"
#include "openthread/logging.h"
#include "openthread/tasklet.h"
#include "ot_examples_br.h"
#include "ot_examples_common.h"
#if CONFIG_OPENTHREAD_STATE_INDICATOR_ENABLE
#include "ot_led_strip.h"
#endif
#if CONFIG_OPENTHREAD_BR_AUTO_START
#include "example_common_private.h"
#include "protocol_examples_common.h"
#endif
#if !CONFIG_OPENTHREAD_BR_AUTO_START && CONFIG_EXAMPLE_CONNECT_ETHERNET
// TZ-1109: Add a menchanism for connecting ETH manually.
#error Currently we do not support a manual way to connect ETH, if you want to use ETH, please enable OPENTHREAD_BR_AUTO_START.
#endif
#define TAG "esp_ot_br"
#if CONFIG_OPENTHREAD_SUPPORT_HW_RESET_RCP
@@ -83,116 +64,6 @@ static void rcp_failure_hardware_reset_handler(void)
}
#endif
#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 */
static void ot_task_worker(void *aContext)
{
esp_openthread_platform_config_t config = {
.radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(),
.host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(),
.port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(),
};
esp_netif_config_t cfg = ESP_NETIF_DEFAULT_OPENTHREAD();
esp_netif_t *openthread_netif = esp_netif_new(&cfg);
assert(openthread_netif != NULL);
// Initialize the OpenThread stack
ESP_ERROR_CHECK(esp_openthread_init(&config));
ESP_ERROR_CHECK(esp_netif_attach(openthread_netif, esp_openthread_netif_glue_init(&config)));
esp_openthread_lock_acquire(portMAX_DELAY);
#if CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC
// The OpenThread log level directly matches ESP log level
(void)otLoggingSetLevel(CONFIG_LOG_DEFAULT_LEVEL);
#endif // CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC
#if CONFIG_OPENTHREAD_CLI
esp_openthread_cli_init();
#if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
esp_cli_custom_command_init();
#endif // CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
esp_openthread_cli_create_task();
#endif // CONFIG_OPENTHREAD_CLI
esp_openthread_lock_release();
// Run the main loop
esp_openthread_launch_mainloop();
// Clean up
esp_openthread_netif_glue_deinit();
esp_netif_destroy(openthread_netif);
esp_vfs_eventfd_unregister();
vTaskDelete(NULL);
}
void ot_br_init(void *ctx)
{
#if CONFIG_OPENTHREAD_CLI_WIFI
ESP_ERROR_CHECK(esp_ot_wifi_config_init());
#endif
#if CONFIG_OPENTHREAD_BR_AUTO_START
#if CONFIG_EXAMPLE_CONNECT_WIFI || CONFIG_EXAMPLE_CONNECT_ETHERNET
bool wifi_or_ethernet_connected = false;
#else
#error No backbone netif!
#endif
#if CONFIG_EXAMPLE_CONNECT_WIFI
char wifi_ssid[32] = "";
char wifi_password[64] = "";
if (esp_ot_wifi_config_get_ssid(wifi_ssid) == ESP_OK) {
ESP_LOGI(TAG, "use the Wi-Fi config from NVS");
esp_ot_wifi_config_get_password(wifi_password);
} else {
ESP_LOGI(TAG, "use the Wi-Fi config from Kconfig");
strcpy(wifi_ssid, CONFIG_EXAMPLE_WIFI_SSID);
strcpy(wifi_password, CONFIG_EXAMPLE_WIFI_PASSWORD);
}
if (esp_ot_wifi_connect(wifi_ssid, wifi_password) == ESP_OK) {
wifi_or_ethernet_connected = true;
} else {
ESP_LOGE(TAG, "Fail to connect to Wi-Fi, please try again manually");
}
#endif
#if CONFIG_EXAMPLE_CONNECT_ETHERNET
ESP_ERROR_CHECK(example_ethernet_connect());
wifi_or_ethernet_connected = true;
#endif
#endif // CONFIG_OPENTHREAD_BR_AUTO_START
#if CONFIG_EXTERNAL_COEX_ENABLE
ot_br_external_coexist_init();
#endif // CONFIG_EXTERNAL_COEX_ENABLE
ESP_ERROR_CHECK(mdns_init());
ESP_ERROR_CHECK(mdns_hostname_set("esp-ot-br"));
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
if (wifi_or_ethernet_connected) {
esp_openthread_set_backbone_netif(get_example_netif());
ESP_ERROR_CHECK(esp_openthread_border_router_init());
#if CONFIG_EXAMPLE_CONNECT_WIFI
esp_ot_wifi_border_router_init_flag_set(true);
#endif
otOperationalDatasetTlvs dataset;
otError error = otDatasetGetActiveTlvs(esp_openthread_get_instance(), &dataset);
ESP_ERROR_CHECK(esp_openthread_auto_start((error == OT_ERROR_NONE) ? &dataset : NULL));
} else {
ESP_LOGE(TAG, "Auto-start mode failed, please try to start manually");
}
#endif // CONFIG_OPENTHREAD_BR_AUTO_START
esp_openthread_lock_release();
vTaskDelete(NULL);
}
void app_main(void)
{
// Used eventfds:
@@ -218,9 +89,38 @@ void app_main(void)
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
ESP_ERROR_CHECK(mdns_init());
ESP_ERROR_CHECK(mdns_hostname_set("esp-ot-br"));
#if CONFIG_OPENTHREAD_SUPPORT_HW_RESET_RCP
esp_openthread_register_rcp_failure_handler(rcp_failure_hardware_reset_handler);
#endif
xTaskCreate(ot_task_worker, "ot_br_main", 8192, xTaskGetCurrentTaskHandle(), 5, NULL);
xTaskCreate(ot_br_init, "ot_br_init", 6144, NULL, 4, NULL);
#if CONFIG_OPENTHREAD_CLI
ot_console_start();
#endif
#if CONFIG_ESP_COEX_EXTERNAL_COEXIST_ENABLE
ot_external_coexist_init();
#endif
static esp_openthread_platform_config_t config = {
.radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(),
.host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(),
.port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(),
};
ESP_ERROR_CHECK(esp_openthread_start(&config));
esp_netif_set_default_netif(esp_openthread_get_netif());
#if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
esp_cli_custom_command_init();
#endif
#if CONFIG_OPENTHREAD_BORDER_ROUTER_AUTO_START
ESP_ERROR_CHECK(esp_openthread_border_router_start());
#if CONFIG_ESP_COEX_SW_COEXIST_ENABLE && CONFIG_SOC_IEEE802154_SUPPORTED
ESP_ERROR_CHECK(esp_coex_wifi_i154_enable());
#endif
#endif
#if CONFIG_OPENTHREAD_NETWORK_AUTO_START
ot_network_auto_start();
#endif
}
+2 -61
View File
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
@@ -68,39 +68,10 @@
}
#endif // CONFIG_OPENTHREAD_RADIO_SPINEL_UART OR CONFIG_OPENTHREAD_RADIO_SPINEL_SPI
#if CONFIG_IDF_TARGET_ESP32C2 && CONFIG_XTAL_FREQ_26
#define HOST_BAUD_RATE 74880
#else
#define HOST_BAUD_RATE 115200
#endif
#if CONFIG_OPENTHREAD_CONSOLE_TYPE_UART
#define ESP_OPENTHREAD_DEFAULT_HOST_CONFIG() \
{ \
.host_connection_mode = HOST_CONNECTION_MODE_CLI_UART, \
.host_uart_config = { \
.port = 0, \
.uart_config = \
{ \
.baud_rate = HOST_BAUD_RATE, \
.data_bits = UART_DATA_8_BITS, \
.parity = UART_PARITY_DISABLE, \
.stop_bits = UART_STOP_BITS_1, \
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE, \
.rx_flow_ctrl_thresh = 0, \
.source_clk = UART_SCLK_DEFAULT, \
}, \
.rx_pin = UART_PIN_NO_CHANGE, \
.tx_pin = UART_PIN_NO_CHANGE, \
}, \
}
#elif CONFIG_OPENTHREAD_CONSOLE_TYPE_USB_SERIAL_JTAG
#define ESP_OPENTHREAD_DEFAULT_HOST_CONFIG() \
{ \
.host_connection_mode = HOST_CONNECTION_MODE_CLI_USB, \
.host_usb_config = USB_SERIAL_JTAG_DRIVER_CONFIG_DEFAULT(), \
.host_connection_mode = HOST_CONNECTION_MODE_NONE, \
}
#endif
#define ESP_OPENTHREAD_DEFAULT_PORT_CONFIG() \
{ \
@@ -108,33 +79,3 @@
.netif_queue_size = 10, \
.task_queue_size = 10, \
}
#if CONFIG_EXTERNAL_COEX_ENABLE
#if CONFIG_EXTERNAL_COEX_WIRE_TYPE == EXTERNAL_COEXIST_WIRE_1
#define ESP_OPENTHREAD_DEFAULT_EXTERNAL_COEX_CONFIG() \
{ \
.request = CONFIG_EXTERNAL_COEX_REQUEST_PIN, \
}
#elif CONFIG_EXTERNAL_COEX_WIRE_TYPE == EXTERNAL_COEXIST_WIRE_2
#define ESP_OPENTHREAD_DEFAULT_EXTERNAL_COEX_CONFIG() \
{ \
.request = CONFIG_EXTERNAL_COEX_REQUEST_PIN, \
.grant = CONFIG_EXTERNAL_COEX_GRANT_PIN, \
}
#elif CONFIG_EXTERNAL_COEX_WIRE_TYPE == EXTERNAL_COEXIST_WIRE_3
#define ESP_OPENTHREAD_DEFAULT_EXTERNAL_COEX_CONFIG() \
{ \
.request = CONFIG_EXTERNAL_COEX_REQUEST_PIN, \
.priority = CONFIG_EXTERNAL_COEX_PRIORITY_PIN, \
.grant = CONFIG_EXTERNAL_COEX_GRANT_PIN, \
}
#elif CONFIG_EXTERNAL_COEX_WIRE_TYPE == EXTERNAL_COEXIST_WIRE_4
#define ESP_OPENTHREAD_DEFAULT_EXTERNAL_COEX_CONFIG() \
{ \
.request = CONFIG_EXTERNAL_COEX_REQUEST_PIN, \
.priority = CONFIG_EXTERNAL_COEX_PRIORITY_PIN, \
.grant = CONFIG_EXTERNAL_COEX_GRANT_PIN, \
.tx_line = CONFIG_EXTERNAL_COEX_TX_LINE_PIN, \
}
#endif
#endif // CONFIG_EXTERNAL_COEX_ENABLE
@@ -1,7 +1,7 @@
## IDF Component Manager Manifest File
dependencies:
espressif/esp_ot_cli_extension:
version: "~1.3.0"
version: "~1.4.0"
espressif/mdns: "^1.0.3"
## Required IDF version
idf:
@@ -10,3 +10,7 @@ dependencies:
path: ${IDF_PATH}/examples/common_components/protocol_examples_common
ot_led:
path: ${IDF_PATH}/examples/openthread/ot_common_components/ot_led
ot_examples_br:
path: ${IDF_PATH}/examples/openthread/ot_common_components/ot_examples_br
ot_examples_common:
path: ${IDF_PATH}/examples/openthread/ot_common_components/ot_examples_common
@@ -1,2 +1,2 @@
CONFIG_EXTERNAL_COEX_ENABLE=y
CONFIG_ESP_COEX_EXTERNAL_COEXIST_ENABLE=y
CONFIG_ESP_COEX_SW_COEXIST_ENABLE=n
@@ -1,2 +1,2 @@
CONFIG_OPENTHREAD_RADIO_TREL=y
CONFIG_EXTERNAL_COEX_ENABLE=y
CONFIG_ESP_COEX_EXTERNAL_COEXIST_ENABLE=y
@@ -25,6 +25,8 @@ CONFIG_MBEDTLS_ECJPAKE_C=y
CONFIG_OPENTHREAD_ENABLED=y
CONFIG_OPENTHREAD_BORDER_ROUTER=y
CONFIG_OPENTHREAD_RADIO_SPINEL_UART=y
CONFIG_OPENTHREAD_TASK_SIZE=8192
CONFIG_OPENTHREAD_CONSOLE_ENABLE=n
# end of OpenThread
#
@@ -54,4 +56,5 @@ CONFIG_EXAMPLE_CONNECT_THREAD=n
# ESP System Settings
#
CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=3584
CONFIG_ESP_MAIN_TASK_STACK_SIZE=6144
# end of ESP System Settings
@@ -1,5 +1,6 @@
# Enable BR auto start for Ethernet builds
CONFIG_OPENTHREAD_BR_AUTO_START=y
CONFIG_OPENTHREAD_NETWORK_AUTO_START=y
CONFIG_OPENTHREAD_BORDER_ROUTER_AUTO_START=y
# Enable PPP support as a workaround to ensure LWIP thread-lib compatibility for Ethernet builds
CONFIG_LWIP_PPP_SUPPORT=y