diff --git a/components/openthread/CMakeLists.txt b/components/openthread/CMakeLists.txt index 7a40455ca5..119eb17543 100644 --- a/components/openthread/CMakeLists.txt +++ b/components/openthread/CMakeLists.txt @@ -16,7 +16,8 @@ if(CONFIG_OPENTHREAD_ENABLED) "private_include") set(src_dirs - "port" + "src" + "src/port" "openthread/examples/platforms/utils" "openthread/src/core/api" "openthread/src/core/common" @@ -113,13 +114,13 @@ if(CONFIG_OPENTHREAD_ENABLED) if(CONFIG_OPENTHREAD_RADIO_NATIVE) list(APPEND exclude_srcs - "port/esp_openthread_radio_spinel.cpp" - "port/esp_spi_spinel_interface.cpp" - "port/esp_uart_spinel_interface.cpp" + "src/port/esp_openthread_radio_spinel.cpp" + "src/port/esp_spi_spinel_interface.cpp" + "src/port/esp_uart_spinel_interface.cpp" ) elseif(CONFIG_OPENTHREAD_RADIO_SPINEL_UART OR CONFIG_OPENTHREAD_RADIO_SPINEL_SPI) list(APPEND exclude_srcs - "port/esp_openthread_radio.c") + "src/port/esp_openthread_radio.c") endif() if(CONFIG_OPENTHREAD_BORDER_ROUTER) @@ -135,7 +136,7 @@ if(CONFIG_OPENTHREAD_ENABLED) if(NOT CONFIG_OPENTHREAD_DNS64_CLIENT) list(APPEND exclude_srcs - "port/esp_openthread_dns64.c") + "src/esp_openthread_dns64.c") endif() if(CONFIG_OPENTHREAD_FTD) diff --git a/components/openthread/Kconfig b/components/openthread/Kconfig index 03f154d11c..ff4ca35bb5 100644 --- a/components/openthread/Kconfig +++ b/components/openthread/Kconfig @@ -34,6 +34,40 @@ menu "OpenThread" bool "Debug logs" endchoice #OPENTHREAD_LOG_LEVEL + menu "Thread Operational Dataset" + config OPENTHREAD_NETWORK_NAME + string "OpenThread network name" + default "OpenThread-ESP" + + config OPENTHREAD_NETWORK_CHANNEL + int "OpenThread network channel" + range 11 26 + default 15 + + config OPENTHREAD_NETWORK_PANID + hex "OpenThread network pan id" + range 0 0xFFFE + default 0x1234 + + config OPENTHREAD_NETWORK_EXTPANID + string "OpenThread extended pan id" + default "dead00beef00cafe" + help + The OpenThread network extended pan id in hex string format + + config OPENTHREAD_NETWORK_MASTERKEY + string "OpenThread network key" + default "00112233445566778899aabbccddeeff" + help + The OpenThread network network key in hex string format + + config OPENTHREAD_NETWORK_PSKC + string "OpenThread pre-shared commissioner key" + default "104810e2315100afd6bc9215a6bfac53" + help + The OpenThread pre-shared commissioner key in hex string format + endmenu + config OPENTHREAD_LOG_LEVEL int depends on OPENTHREAD_ENABLED && !OPENTHREAD_LOG_LEVEL_DYNAMIC diff --git a/components/openthread/include/esp_openthread.h b/components/openthread/include/esp_openthread.h index f98f193d5c..9e5f39a59b 100644 --- a/components/openthread/include/esp_openthread.h +++ b/components/openthread/include/esp_openthread.h @@ -8,6 +8,7 @@ #include "esp_err.h" #include "esp_openthread_types.h" +#include "openthread/dataset.h" #include "openthread/error.h" #include "openthread/instance.h" #include "lwip/ip_addr.h" @@ -32,10 +33,23 @@ extern "C" { */ esp_err_t esp_openthread_init(const esp_openthread_platform_config_t *init_config); +/** + * @brief Starts the Thread protocol operation and attaches to a Thread network. + * + * @param[in] datasetTlvs The operational dataset (TLV encoded), if it's NULL, the function will generate the dataset + * based on the configurations from kconfig. + * + * @return + * - ESP_OK on success + * - ESP_FAIL on failures + * + */ +esp_err_t esp_openthread_auto_start(otOperationalDatasetTlvs *datasetTlvs); + /** * @brief Launches the OpenThread main loop. * - * @note Thie function will not return unless error happens when running the OpenThread stack. + * @note This function will not return unless error happens when running the OpenThread stack. * * @return * - ESP_OK on success diff --git a/components/openthread/port/esp_openthread.cpp b/components/openthread/port/esp_openthread.cpp deleted file mode 100644 index 5c0b34c4a8..0000000000 --- a/components/openthread/port/esp_openthread.cpp +++ /dev/null @@ -1,96 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "esp_openthread.h" -#include "esp_check.h" -#include "esp_openthread_border_router.h" -#include "esp_openthread_common_macro.h" -#include "esp_openthread_dns64.h" -#include "esp_openthread_lock.h" -#include "esp_openthread_platform.h" -#include "esp_openthread_task_queue.h" -#include "esp_openthread_types.h" -#include "freertos/FreeRTOS.h" -#include "lwip/dns.h" -#include "openthread/instance.h" -#include "openthread/netdata.h" -#include "openthread/tasklet.h" - -esp_err_t esp_openthread_init(const esp_openthread_platform_config_t *config) -{ - ESP_RETURN_ON_ERROR(esp_openthread_platform_init(config), OT_PLAT_LOG_TAG, - "Failed to initialize OpenThread platform driver"); - esp_openthread_lock_acquire(portMAX_DELAY); - ESP_RETURN_ON_FALSE(otInstanceInitSingle() != NULL, ESP_FAIL, OT_PLAT_LOG_TAG, - "Failed to initialize OpenThread instance"); -#if CONFIG_OPENTHREAD_DNS64_CLIENT - ESP_RETURN_ON_ERROR(esp_openthread_dns64_client_init(), OT_PLAT_LOG_TAG, - "Failed to initialize OpenThread dns64 client"); -#endif - esp_openthread_lock_release(); - - return ESP_OK; -} - -esp_err_t esp_openthread_launch_mainloop(void) -{ - esp_openthread_mainloop_context_t mainloop; - otInstance *instance = esp_openthread_get_instance(); - esp_err_t error = ESP_OK; - - while (true) { - FD_ZERO(&mainloop.read_fds); - FD_ZERO(&mainloop.write_fds); - FD_ZERO(&mainloop.error_fds); - - mainloop.max_fd = -1; - mainloop.timeout.tv_sec = 10; - mainloop.timeout.tv_usec = 0; - - esp_openthread_lock_acquire(portMAX_DELAY); - esp_openthread_platform_update(&mainloop); - if (otTaskletsArePending(instance)) { - mainloop.timeout.tv_sec = 0; - mainloop.timeout.tv_usec = 0; - } - esp_openthread_lock_release(); - - if (select(mainloop.max_fd + 1, &mainloop.read_fds, &mainloop.write_fds, &mainloop.error_fds, - &mainloop.timeout) >= 0) { - esp_openthread_lock_acquire(portMAX_DELAY); - error = esp_openthread_platform_process(instance, &mainloop); - while (otTaskletsArePending(instance)) { - otTaskletsProcess(instance); - } - esp_openthread_lock_release(); - if (error != ESP_OK) { - ESP_LOGE(OT_PLAT_LOG_TAG, "esp_openthread_platform_process failed"); - break; - } - } else { - error = ESP_FAIL; - ESP_LOGE(OT_PLAT_LOG_TAG, "OpenThread system polling failed"); - break; - } - } - return error; -} - -esp_err_t esp_openthread_deinit(void) -{ - otInstanceFinalize(esp_openthread_get_instance()); - return esp_openthread_platform_deinit(); -} - -static void stub_task(void *context) -{ - // this is a empty function used for ot-task signal pending -} - -void otTaskletsSignalPending(otInstance *aInstance) -{ - esp_openthread_task_queue_post(stub_task, NULL); -} diff --git a/components/openthread/src/esp_openthread.cpp b/components/openthread/src/esp_openthread.cpp new file mode 100644 index 0000000000..bcb88437b2 --- /dev/null +++ b/components/openthread/src/esp_openthread.cpp @@ -0,0 +1,194 @@ +/* + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "esp_openthread.h" +#include "esp_check.h" +#include "esp_openthread_border_router.h" +#include "esp_openthread_common_macro.h" +#include "esp_openthread_dns64.h" +#include "esp_openthread_lock.h" +#include "esp_openthread_platform.h" +#include "esp_openthread_task_queue.h" +#include "esp_openthread_types.h" +#include "freertos/FreeRTOS.h" +#include "lwip/dns.h" +#include "openthread/instance.h" +#include "openthread/netdata.h" +#include "openthread/tasklet.h" +#include "openthread/thread.h" + +static int hex_digit_to_int(char hex) +{ + if ('A' <= hex && hex <= 'F') { + return 10 + hex - 'A'; + } + if ('a' <= hex && hex <= 'f') { + return 10 + hex - 'a'; + } + if ('0' <= hex && hex <= '9') { + return hex - '0'; + } + return -1; +} + +static size_t hex_string_to_binary(const char *hex_string, uint8_t *buf, size_t buf_size) +{ + int num_char = strlen(hex_string); + + if (num_char != buf_size * 2) { + return 0; + } + for (size_t i = 0; i < num_char; i += 2) { + int digit0 = hex_digit_to_int(hex_string[i]); + int digit1 = hex_digit_to_int(hex_string[i + 1]); + + if (digit0 < 0 || digit1 < 0) { + return 0; + } + buf[i / 2] = (digit0 << 4) + digit1; + } + + return buf_size; +} + +esp_err_t esp_openthread_init(const esp_openthread_platform_config_t *config) +{ + ESP_RETURN_ON_ERROR(esp_openthread_platform_init(config), OT_PLAT_LOG_TAG, + "Failed to initialize OpenThread platform driver"); + esp_openthread_lock_acquire(portMAX_DELAY); + ESP_RETURN_ON_FALSE(otInstanceInitSingle() != NULL, ESP_FAIL, OT_PLAT_LOG_TAG, + "Failed to initialize OpenThread instance"); +#if CONFIG_OPENTHREAD_DNS64_CLIENT + ESP_RETURN_ON_ERROR(esp_openthread_dns64_client_init(), OT_PLAT_LOG_TAG, + "Failed to initialize OpenThread dns64 client"); +#endif + esp_openthread_lock_release(); + + return ESP_OK; +} + +esp_err_t esp_openthread_auto_start(otOperationalDatasetTlvs *datasetTlvs) +{ + otInstance *instance = esp_openthread_get_instance(); + + if (datasetTlvs) { + ESP_RETURN_ON_FALSE(otDatasetSetActiveTlvs(instance, datasetTlvs) == OT_ERROR_NONE, ESP_FAIL, OT_PLAT_LOG_TAG, + "Failed to set OpenThread active dataset"); + } + else { + otOperationalDataset dataset; + size_t len = 0; + + memset(&dataset, 0, sizeof(otOperationalDataset)); + + // Active timestamp + dataset.mActiveTimestamp.mSeconds = 1; + dataset.mActiveTimestamp.mTicks = 0; + dataset.mActiveTimestamp.mAuthoritative = false; + dataset.mComponents.mIsActiveTimestampPresent = true; + + // Channel, Pan ID, Network Name + dataset.mChannel = CONFIG_OPENTHREAD_NETWORK_CHANNEL; + dataset.mComponents.mIsChannelPresent = true; + dataset.mPanId = CONFIG_OPENTHREAD_NETWORK_PANID; + dataset.mComponents.mIsPanIdPresent = true; + len = strlen(CONFIG_OPENTHREAD_NETWORK_NAME); + assert(len <= OT_NETWORK_NAME_MAX_SIZE); + memcpy(dataset.mNetworkName.m8, CONFIG_OPENTHREAD_NETWORK_NAME, len); + dataset.mComponents.mIsNetworkNamePresent = true; + + // Extended Pan ID + len = hex_string_to_binary(CONFIG_OPENTHREAD_NETWORK_EXTPANID, dataset.mExtendedPanId.m8, + sizeof(dataset.mExtendedPanId.m8)); + ESP_RETURN_ON_FALSE(len == sizeof(dataset.mExtendedPanId.m8), ESP_FAIL, OT_PLAT_LOG_TAG, + "Cannot convert OpenThread extended pan id"); + dataset.mComponents.mIsExtendedPanIdPresent = true; + + // Network Key + len = hex_string_to_binary(CONFIG_OPENTHREAD_NETWORK_MASTERKEY, dataset.mNetworkKey.m8, + sizeof(dataset.mNetworkKey.m8)); + ESP_RETURN_ON_FALSE(len == sizeof(dataset.mNetworkKey.m8), ESP_FAIL, OT_PLAT_LOG_TAG, + "Cannot convert OpenThread master key"); + dataset.mComponents.mIsNetworkKeyPresent = true; + + // PSKc + len = hex_string_to_binary(CONFIG_OPENTHREAD_NETWORK_PSKC, dataset.mPskc.m8, sizeof(dataset.mPskc.m8)); + ESP_RETURN_ON_FALSE(len == sizeof(dataset.mPskc.m8), ESP_FAIL, OT_PLAT_LOG_TAG, + "Cannot convert OpenThread pre-shared commissioner key"); + dataset.mComponents.mIsPskcPresent = true; + + ESP_RETURN_ON_FALSE(otDatasetSetActive(instance, &dataset) == OT_ERROR_NONE, ESP_FAIL, OT_PLAT_LOG_TAG, + "Failed to set OpenThread active dataset"); + } + + ESP_RETURN_ON_FALSE(otIp6SetEnabled(instance, true) == OT_ERROR_NONE, ESP_FAIL, OT_PLAT_LOG_TAG, + "Failed to enable OpenThread IPv6 interface"); + + ESP_RETURN_ON_FALSE(otThreadSetEnabled(instance, true) == OT_ERROR_NONE, ESP_FAIL, OT_PLAT_LOG_TAG, + "Failed to enable OpenThread"); + + return ESP_OK; +} + +esp_err_t esp_openthread_launch_mainloop(void) +{ + esp_openthread_mainloop_context_t mainloop; + otInstance *instance = esp_openthread_get_instance(); + esp_err_t error = ESP_OK; + + while (true) { + FD_ZERO(&mainloop.read_fds); + FD_ZERO(&mainloop.write_fds); + FD_ZERO(&mainloop.error_fds); + + mainloop.max_fd = -1; + mainloop.timeout.tv_sec = 10; + mainloop.timeout.tv_usec = 0; + + esp_openthread_lock_acquire(portMAX_DELAY); + esp_openthread_platform_update(&mainloop); + if (otTaskletsArePending(instance)) { + mainloop.timeout.tv_sec = 0; + mainloop.timeout.tv_usec = 0; + } + esp_openthread_lock_release(); + + if (select(mainloop.max_fd + 1, &mainloop.read_fds, &mainloop.write_fds, &mainloop.error_fds, + &mainloop.timeout) >= 0) { + esp_openthread_lock_acquire(portMAX_DELAY); + error = esp_openthread_platform_process(instance, &mainloop); + while (otTaskletsArePending(instance)) { + otTaskletsProcess(instance); + } + esp_openthread_lock_release(); + if (error != ESP_OK) { + ESP_LOGE(OT_PLAT_LOG_TAG, "esp_openthread_platform_process failed"); + break; + } + } else { + error = ESP_FAIL; + ESP_LOGE(OT_PLAT_LOG_TAG, "OpenThread system polling failed"); + break; + } + } + return error; +} + +esp_err_t esp_openthread_deinit(void) +{ + otInstanceFinalize(esp_openthread_get_instance()); + return esp_openthread_platform_deinit(); +} + +static void stub_task(void *context) +{ + // this is a empty function used for ot-task signal pending +} + +void otTaskletsSignalPending(otInstance *aInstance) +{ + esp_openthread_task_queue_post(stub_task, NULL); +} diff --git a/components/openthread/port/esp_openthread_cli.c b/components/openthread/src/esp_openthread_cli.c similarity index 100% rename from components/openthread/port/esp_openthread_cli.c rename to components/openthread/src/esp_openthread_cli.c diff --git a/components/openthread/port/esp_openthread_dns64.c b/components/openthread/src/esp_openthread_dns64.c similarity index 100% rename from components/openthread/port/esp_openthread_dns64.c rename to components/openthread/src/esp_openthread_dns64.c diff --git a/components/openthread/port/esp_openthread_lock.c b/components/openthread/src/esp_openthread_lock.c similarity index 100% rename from components/openthread/port/esp_openthread_lock.c rename to components/openthread/src/esp_openthread_lock.c diff --git a/components/openthread/port/esp_openthread_lwip_netif.c b/components/openthread/src/esp_openthread_lwip_netif.c similarity index 100% rename from components/openthread/port/esp_openthread_lwip_netif.c rename to components/openthread/src/esp_openthread_lwip_netif.c diff --git a/components/openthread/port/esp_openthread_netif_glue.c b/components/openthread/src/esp_openthread_netif_glue.c similarity index 100% rename from components/openthread/port/esp_openthread_netif_glue.c rename to components/openthread/src/esp_openthread_netif_glue.c diff --git a/components/openthread/port/esp_openthread_platform.cpp b/components/openthread/src/esp_openthread_platform.cpp similarity index 100% rename from components/openthread/port/esp_openthread_platform.cpp rename to components/openthread/src/esp_openthread_platform.cpp diff --git a/components/openthread/port/esp_openthread_task_queue.c b/components/openthread/src/esp_openthread_task_queue.c similarity index 100% rename from components/openthread/port/esp_openthread_task_queue.c rename to components/openthread/src/esp_openthread_task_queue.c diff --git a/components/openthread/port/esp_openthread_alarm.c b/components/openthread/src/port/esp_openthread_alarm.c similarity index 100% rename from components/openthread/port/esp_openthread_alarm.c rename to components/openthread/src/port/esp_openthread_alarm.c diff --git a/components/openthread/port/esp_openthread_flash.c b/components/openthread/src/port/esp_openthread_flash.c similarity index 100% rename from components/openthread/port/esp_openthread_flash.c rename to components/openthread/src/port/esp_openthread_flash.c diff --git a/components/openthread/port/esp_openthread_logging.c b/components/openthread/src/port/esp_openthread_logging.c similarity index 100% rename from components/openthread/port/esp_openthread_logging.c rename to components/openthread/src/port/esp_openthread_logging.c diff --git a/components/openthread/port/esp_openthread_memory.c b/components/openthread/src/port/esp_openthread_memory.c similarity index 100% rename from components/openthread/port/esp_openthread_memory.c rename to components/openthread/src/port/esp_openthread_memory.c diff --git a/components/openthread/port/esp_openthread_misc.c b/components/openthread/src/port/esp_openthread_misc.c similarity index 100% rename from components/openthread/port/esp_openthread_misc.c rename to components/openthread/src/port/esp_openthread_misc.c diff --git a/components/openthread/port/esp_openthread_radio.c b/components/openthread/src/port/esp_openthread_radio.c similarity index 100% rename from components/openthread/port/esp_openthread_radio.c rename to components/openthread/src/port/esp_openthread_radio.c diff --git a/components/openthread/port/esp_openthread_radio_spinel.cpp b/components/openthread/src/port/esp_openthread_radio_spinel.cpp similarity index 100% rename from components/openthread/port/esp_openthread_radio_spinel.cpp rename to components/openthread/src/port/esp_openthread_radio_spinel.cpp diff --git a/components/openthread/port/esp_openthread_spi_slave.c b/components/openthread/src/port/esp_openthread_spi_slave.c similarity index 100% rename from components/openthread/port/esp_openthread_spi_slave.c rename to components/openthread/src/port/esp_openthread_spi_slave.c diff --git a/components/openthread/port/esp_openthread_uart.c b/components/openthread/src/port/esp_openthread_uart.c similarity index 100% rename from components/openthread/port/esp_openthread_uart.c rename to components/openthread/src/port/esp_openthread_uart.c diff --git a/components/openthread/port/esp_openthread_udp.c b/components/openthread/src/port/esp_openthread_udp.c similarity index 100% rename from components/openthread/port/esp_openthread_udp.c rename to components/openthread/src/port/esp_openthread_udp.c diff --git a/components/openthread/port/esp_spi_spinel_interface.cpp b/components/openthread/src/port/esp_spi_spinel_interface.cpp similarity index 100% rename from components/openthread/port/esp_spi_spinel_interface.cpp rename to components/openthread/src/port/esp_spi_spinel_interface.cpp diff --git a/components/openthread/port/esp_uart_spinel_interface.cpp b/components/openthread/src/port/esp_uart_spinel_interface.cpp similarity index 100% rename from components/openthread/port/esp_uart_spinel_interface.cpp rename to components/openthread/src/port/esp_uart_spinel_interface.cpp diff --git a/examples/openthread/ot_br/main/Kconfig.projbuild b/examples/openthread/ot_br/main/Kconfig.projbuild index fa8a651201..996d5e2abb 100644 --- a/examples/openthread/ot_br/main/Kconfig.projbuild +++ b/examples/openthread/ot_br/main/Kconfig.projbuild @@ -1,43 +1,5 @@ menu "OpenThread Border Router Example" - config OPENTHREAD_NETWORK_NAME - string "OpenThread network name" - default "OpenThread" - help - The OpenThread network name for example to use - - config OPENTHREAD_NETWORK_CHANNEL - int "OpenThread network channel" - range 11 26 - default 15 - help - The OpenThread network channel to use - - config OPENTHREAD_NETWORK_PANID - hex "OpenThread network pan id" - range 0 0xFFFE - default 0x1234 - help - The OpenThread network pan id to use - - config OPENTHREAD_NETWORK_EXTPANID - string "OpenThread extended pan id" - default "dead00beef00cafe" - help - The OpenThread network extended pan id in hex string format - - config OPENTHREAD_NETWORK_MASTERKEY - string "OpenThread master key" - default "00112233445566778899aabbccddeeff" - help - The OpenThread network master key in hex string format - - config OPENTHREAD_NETWORK_PSKC - string "OpenThread pre-shared commissioner key" - default "104810e2315100afd6bc9215a6bfac53" - help - The OpenThread pre-shared commissioner key in hex string format - config OPENTHREAD_BR_AUTO_START bool 'Enable the automatic start mode in Thread Border Router.' default False diff --git a/examples/openthread/ot_br/main/esp_ot_br.c b/examples/openthread/ot_br/main/esp_ot_br.c index 38d8e43dd6..249ea27654 100644 --- a/examples/openthread/ot_br/main/esp_ot_br.c +++ b/examples/openthread/ot_br/main/esp_ot_br.c @@ -20,8 +20,6 @@ #include "esp_event.h" #include "esp_log.h" #include "esp_netif.h" -#include "esp_netif_ip_addr.h" -#include "esp_netif_net_stack.h" #include "esp_openthread.h" #include "esp_openthread_border_router.h" #include "esp_openthread_cli.h" @@ -43,122 +41,12 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "hal/uart_types.h" -#include "openthread/backbone_router_ftd.h" -#include "openthread/border_router.h" -#include "openthread/cli.h" -#include "openthread/dataset.h" -#include "openthread/dataset_ftd.h" -#include "openthread/dataset_updater.h" #include "openthread/error.h" -#include "openthread/instance.h" -#include "openthread/ip6.h" #include "openthread/logging.h" #include "openthread/tasklet.h" -#include "openthread/thread_ftd.h" #define TAG "esp_ot_br" -#if CONFIG_OPENTHREAD_BR_AUTO_START -static int hex_digit_to_int(char hex) -{ - if ('A' <= hex && hex <= 'F') { - return 10 + hex - 'A'; - } - if ('a' <= hex && hex <= 'f') { - return 10 + hex - 'a'; - } - if ('0' <= hex && hex <= '9') { - return hex - '0'; - } - return -1; -} - -static size_t hex_string_to_binary(const char *hex_string, uint8_t *buf, size_t buf_size) -{ - int num_char = strlen(hex_string); - - if (num_char != buf_size * 2) { - return 0; - } - for (size_t i = 0; i < num_char; i += 2) { - int digit0 = hex_digit_to_int(hex_string[i]); - int digit1 = hex_digit_to_int(hex_string[i + 1]); - - if (digit0 < 0 || digit1 < 0) { - return 0; - } - buf[i / 2] = (digit0 << 4) + digit1; - } - - return buf_size; -} - -static void create_config_network(otInstance *instance) -{ - otOperationalDataset dataset; - - if (otDatasetGetActive(instance, &dataset) == OT_ERROR_NONE) { - ESP_LOGI(TAG, "Already has network, skip configuring OpenThread network."); - return; - } - - uint16_t network_name_len = strlen(CONFIG_OPENTHREAD_NETWORK_NAME); - - assert(network_name_len <= OT_NETWORK_NAME_MAX_SIZE); - - if (otDatasetCreateNewNetwork(instance, &dataset) != OT_ERROR_NONE) { - ESP_LOGE(TAG, "Failed to create OpenThread network dataset."); - abort(); - } - dataset.mChannel = CONFIG_OPENTHREAD_NETWORK_CHANNEL; - dataset.mComponents.mIsChannelPresent = true; - dataset.mPanId = CONFIG_OPENTHREAD_NETWORK_PANID; - dataset.mComponents.mIsPanIdPresent = true; - memcpy(dataset.mNetworkName.m8, CONFIG_OPENTHREAD_NETWORK_NAME, network_name_len); - dataset.mComponents.mIsNetworkNamePresent = true; - if (hex_string_to_binary(CONFIG_OPENTHREAD_NETWORK_EXTPANID, dataset.mExtendedPanId.m8, - sizeof(dataset.mExtendedPanId.m8)) != sizeof(dataset.mExtendedPanId.m8)) { - ESP_LOGE(TAG, "Cannot convert OpenThread extended pan id. Please double-check your config."); - abort(); - } - dataset.mComponents.mIsExtendedPanIdPresent = true; - if (hex_string_to_binary(CONFIG_OPENTHREAD_NETWORK_MASTERKEY, dataset.mNetworkKey.m8, - sizeof(dataset.mNetworkKey.m8)) != sizeof(dataset.mNetworkKey.m8)) { - ESP_LOGE(TAG, "Cannot convert OpenThread master key. Please double-check your config."); - abort(); - } - dataset.mComponents.mIsNetworkKeyPresent = true; - if (hex_string_to_binary(CONFIG_OPENTHREAD_NETWORK_PSKC, dataset.mPskc.m8, sizeof(dataset.mPskc.m8)) != - sizeof(dataset.mPskc.m8)) { - ESP_LOGE(TAG, "Cannot convert OpenThread pre-shared commissioner key. Please double-check your config."); - abort(); - } - dataset.mComponents.mIsPskcPresent = true; - if (otDatasetSetActive(instance, &dataset) != OT_ERROR_NONE) { - ESP_LOGE(TAG, "Failed to set OpenThread active dataset."); - abort(); - } - return; -} - -static void launch_openthread_network(otInstance *instance) -{ - if (otIp6SetEnabled(instance, true) != OT_ERROR_NONE) { - ESP_LOGE(TAG, "Failed to enable OpenThread IP6 link"); - abort(); - } - if (otThreadSetEnabled(instance, true) != OT_ERROR_NONE) { - ESP_LOGE(TAG, "Failed to enable OpenThread"); - abort(); - } - if (otBorderRouterRegister(instance) != OT_ERROR_NONE) { - ESP_LOGE(TAG, "Failed to register border router."); - abort(); - } - otBackboneRouterSetEnabled(instance, true); -} -#endif // CONFIG_OPENTHREAD_BR_AUTO_START - static void ot_task_worker(void *aContext) { esp_openthread_platform_config_t config = { @@ -170,8 +58,8 @@ static void ot_task_worker(void *aContext) 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 + // Initialize the OpenThread stack ESP_ERROR_CHECK(esp_openthread_init(&config)); // Initialize border routing features @@ -180,11 +68,12 @@ static void ot_task_worker(void *aContext) (void)otLoggingSetLevel(CONFIG_LOG_DEFAULT_LEVEL); esp_openthread_cli_init(); + #if CONFIG_OPENTHREAD_BR_AUTO_START ESP_ERROR_CHECK(esp_openthread_border_router_init()); - create_config_network(esp_openthread_get_instance()); - launch_openthread_network(esp_openthread_get_instance()); + ESP_ERROR_CHECK(esp_openthread_auto_start(NULL)); #endif // CONFIG_OPENTHREAD_BR_AUTO_START + esp_cli_custom_command_init(); esp_openthread_lock_release();