feat(openthread): add common components for openthread examples

This commit is contained in:
Xu Si Yu
2025-09-16 14:07:24 +08:00
parent c28ab21bf1
commit ce9a105b11
11 changed files with 377 additions and 0 deletions
@@ -0,0 +1,9 @@
set(srcs "")
if(CONFIG_OPENTHREAD_BORDER_ROUTER_AUTO_START)
list(APPEND srcs "ot_examples_br.c")
endif()
idf_component_register(SRCS "${srcs}"
INCLUDE_DIRS "include"
PRIV_REQUIRES openthread protocol_examples_common)
@@ -0,0 +1,12 @@
menu "OpenThread Border Router Config"
depends on OPENTHREAD_BORDER_ROUTER
config OPENTHREAD_BORDER_ROUTER_AUTO_START
depends on OPENTHREAD_BORDER_ROUTER
bool 'Enable the border router auto start'
default n
help
If enabled, the program will automatically connect to the backbone network and
initialize the border router at startup.
endmenu
@@ -0,0 +1,37 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
* OpenThread Command Line 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.
*/
#include "esp_err.h"
#include "esp_openthread.h"
/**
* @brief Start the border router features of OpenThread.
*
* @note Calling this function will make the device connect to the Wi-Fi or Ethernet,
* and initialize the border router feature.
*
* @return
* - ESP_OK on success.
* - ESP_FAIL on failure.
*
*/
esp_err_t esp_openthread_border_router_start(void);
/**
* @brief Stop the border router features of OpenThread.
*
* @note Calling this function will make the device deinitialize the border router feature.
*
*/
void esp_openthread_border_router_stop(void);
@@ -0,0 +1,55 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
* OpenThread Command Line 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.
*/
#include "ot_examples_br.h"
#include "esp_check.h"
#include "esp_err.h"
#include "esp_openthread.h"
#include "esp_openthread_lock.h"
#include "esp_openthread_border_router.h"
#include "protocol_examples_common.h"
#define TAG "ot_examples_br"
#if CONFIG_OPENTHREAD_CLI_WIFI
#error "CONFIG_OPENTHREAD_CLI_WIFI conflicts with the border router auto-initialization feature"
#endif
static bool s_border_router_started = false;
static void ot_br_init(void *ctx)
{
ESP_ERROR_CHECK(example_connect());
esp_openthread_lock_acquire(portMAX_DELAY);
esp_openthread_set_backbone_netif(get_example_netif());
ESP_ERROR_CHECK(esp_openthread_border_router_init());
esp_openthread_lock_release();
s_border_router_started = true;
vTaskDelete(NULL);
}
esp_err_t esp_openthread_border_router_start(void)
{
return (xTaskCreate(ot_br_init, "ot_br_init", 6144, NULL, 4, NULL) == pdPASS) ? ESP_OK : ESP_FAIL;
}
void esp_openthread_border_router_stop(void)
{
if (s_border_router_started) {
esp_openthread_lock_acquire(portMAX_DELAY);
ESP_ERROR_CHECK(esp_openthread_border_router_deinit());
esp_openthread_lock_release();
s_border_router_started =false;
}
}
@@ -0,0 +1,19 @@
set(srcs "")
if(CONFIG_OPENTHREAD_FTD OR CONFIG_OPENTHREAD_MTD)
list(APPEND srcs "ot_network.c")
endif()
if(CONFIG_OPENTHREAD_CLI)
list(APPEND srcs "ot_console.c")
endif()
if(CONFIG_ESP_COEX_EXTERNAL_COEXIST_ENABLE)
list(APPEND srcs "ot_external_coexist.c")
endif()
idf_component_register(
SRCS "${srcs}"
INCLUDE_DIRS "include"
PRIV_REQUIRES console cmd_system esp_coex openthread
)
@@ -0,0 +1,67 @@
menu "Config for OpenThread Examples"
depends on OPENTHREAD_ENABLED
config OPENTHREAD_NETWORK_AUTO_START
bool 'Enable the automatic start mode of Thread network.'
depends on OPENTHREAD_FTD || OPENTHREAD_MTD
default n
help
If enabled, the Openthread Device will create or connect to Thread network with pre-configured
network parameters automatically. Otherwise, user need to configure Thread via CLI command manually.
menu "External coexist wire type and pin config"
depends on ESP_COEX_EXTERNAL_COEXIST_ENABLE
choice EXTERNAL_COEX_WORK_MODE
prompt "The work mode of external coexist"
default EXTERNAL_COEX_WORK_MODE_FOLLOWER if SOC_IEEE802154_SUPPORTED
default EXTERNAL_COEX_WORK_MODE_LEADER if !SOC_IEEE802154_SUPPORTED && SOC_WIFI_SUPPORTED
help
Select work mode for external coexist, the work mode defined in esp_extern_coex_work_mode_t.
config EXTERNAL_COEX_WORK_MODE_LEADER
bool "Leader mode"
help
Select this to set the external coexistence work mode to leader mode.
config EXTERNAL_COEX_WORK_MODE_FOLLOWER
bool "Follower mode"
help
Select this to set the external coexistence work mode to follower mode.
config EXTERNAL_COEX_WORK_MODE_UNKNOWN
bool "Unknown mode"
help
Select this to set the external coexistence work mode to unknown mode.
endchoice
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
@@ -0,0 +1,4 @@
## IDF Component Manager Manifest File
dependencies:
cmd_system:
path: ${IDF_PATH}/examples/system/console/advanced/components/cmd_system
@@ -0,0 +1,63 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
* OpenThread Command Line 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.
*/
#include "sdkconfig.h"
#if CONFIG_ESP_COEX_EXTERNAL_COEXIST_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_ESP_COEX_EXTERNAL_COEXIST_ENABLE
/**
* @brief Initializes the external coexistence.
*
*/
void ot_external_coexist_init(void);
/**
* @brief Initializes the console.
*
*/
void ot_console_start(void);
/**
* @brief Form or join the Thread network automatically.
*
*/
void ot_network_auto_start(void);
@@ -0,0 +1,48 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
* OpenThread Command Line 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.
*/
#include "ot_examples_common.h"
#include "esp_check.h"
#include "esp_err.h"
#include "esp_console.h"
#include "cmd_system.h"
void ot_console_start(void)
{
esp_console_repl_t *repl = NULL;
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
/* Prompt to be printed before each line.
* This can be customized, made dynamic, etc.
*/
repl_config.prompt = CONFIG_IDF_TARGET ">";
repl_config.max_cmdline_length = 256;
repl_config.max_history_len = 10;
#if defined(CONFIG_ESP_CONSOLE_UART_DEFAULT) || defined(CONFIG_ESP_CONSOLE_UART_CUSTOM)
esp_console_dev_uart_config_t hw_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_console_new_repl_uart(&hw_config, &repl_config, &repl));
#elif defined(CONFIG_ESP_CONSOLE_USB_CDC)
esp_console_dev_usb_cdc_config_t hw_config = ESP_CONSOLE_DEV_CDC_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_console_new_repl_usb_cdc(&hw_config, &repl_config, &repl));
#elif defined(CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG)
esp_console_dev_usb_serial_jtag_config_t hw_config = ESP_CONSOLE_DEV_USB_SERIAL_JTAG_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_console_new_repl_usb_serial_jtag(&hw_config, &repl_config, &repl));
#else
#error Unsupported console type
#endif
ESP_ERROR_CHECK(esp_console_start_repl(repl));
register_system();
}
@@ -0,0 +1,35 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
* OpenThread Command Line 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.
*/
#include "sdkconfig.h"
#include "esp_check.h"
#include "esp_err.h"
#include "esp_coexist.h"
#include "ot_examples_common.h"
void ot_external_coexist_init(void)
{
esp_extern_coex_work_mode_t mode =
#if CONFIG_EXTERNAL_COEX_WORK_MODE_LEADER
EXTERNAL_COEX_LEADER_ROLE;
#elif CONFIG_EXTERNAL_COEX_WORK_MODE_FOLLOWER
EXTERNAL_COEX_FOLLOWER_ROLE;
#else
EXTERNAL_COEX_UNKNOWN_ROLE;
#endif
esp_external_coex_gpio_set_t gpio_pin = ESP_OPENTHREAD_DEFAULT_EXTERNAL_COEX_CONFIG();
ESP_ERROR_CHECK(esp_external_coex_set_work_mode(mode));
ESP_ERROR_CHECK(esp_enable_extern_coex_gpio_pin(CONFIG_EXTERNAL_COEX_WIRE_TYPE, gpio_pin));
}
@@ -0,0 +1,28 @@
/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*
* OpenThread Command Line 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.
*/
#include "ot_examples_common.h"
#include "esp_check.h"
#include "esp_err.h"
#include "esp_openthread.h"
#include "esp_openthread_lock.h"
void ot_network_auto_start(void)
{
otOperationalDatasetTlvs dataset;
esp_openthread_lock_acquire(portMAX_DELAY);
otError error = otDatasetGetActiveTlvs(esp_openthread_get_instance(), &dataset);
ESP_ERROR_CHECK(esp_openthread_auto_start((error == OT_ERROR_NONE) ? &dataset : NULL));
esp_openthread_lock_release();
}