60 esp_openthread_platform_config_t config = {
63 .radio_mode = RADIO_MODE_NATIVE,
64 .radio_uart_config = {},
75 .storage_partition_name =
"nvs",
76 .netif_queue_size = 10,
77 .task_queue_size = 10,
83 ESP_ERROR_CHECK(esp_openthread_init(&config));
88 otInstance *instance = esp_openthread_get_instance();
90#if CONFIG_OPENTHREAD_STATE_INDICATOR_ENABLE
91 ESP_ERROR_CHECK(esp_openthread_state_indicator_init(instance));
94#if CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC
96 (void) otLoggingSetLevel(CONFIG_LOG_DEFAULT_LEVEL);
99#if CONFIG_OPENTHREAD_CLI
100 esp_openthread_cli_init();
103 esp_netif_t *openthread_netif;
105 openthread_netif = init_openthread_netif(&config);
106 esp_netif_set_default_netif(openthread_netif);
108#if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
109 esp_cli_custom_command_init();
112 ESP_LOGD(TAG,
"Thread Version: %" PRIu16, otThreadGetVersion());
117 if (
const auto err = otPlatRadioSetTransmitPower(instance, *this->
output_power_); err != OT_ERROR_NONE) {
118 ESP_LOGE(TAG,
"Failed to set power: %s", otThreadErrorToString(err));
123#if CONFIG_OPENTHREAD_CLI
124 esp_openthread_cli_create_task();
126 ESP_LOGI(TAG,
"Activating dataset...");
127 otOperationalDatasetTlvs dataset = {};
129#ifndef USE_OPENTHREAD_FORCE_DATASET
131 otError error = otDatasetGetActiveTlvs(instance, &dataset);
132 if (error != OT_ERROR_NONE) {
136 ESP_LOGI(TAG,
"Found existing dataset, ignoring config (force_dataset: true to override)");
140#ifdef USE_OPENTHREAD_TLVS
141 if (dataset.mLength == 0) {
143 size_t len = (
sizeof(USE_OPENTHREAD_TLVS) - 1) / 2;
144 if (
len >
sizeof(dataset.mTlvs)) {
145 ESP_LOGW(TAG,
"TLV buffer too small, truncating");
146 len =
sizeof(dataset.mTlvs);
148 parse_hex(USE_OPENTHREAD_TLVS,
sizeof(USE_OPENTHREAD_TLVS) - 1, dataset.mTlvs,
len);
149 dataset.mLength =
len;
154 ESP_ERROR_CHECK(esp_openthread_auto_start(dataset.mLength > 0 ? &dataset :
nullptr));
158 if (ot_err != OT_ERROR_NONE) {
159 ESP_LOGW(TAG,
"Failed to register state change callback: %d", ot_err);
162 esp_openthread_launch_mainloop();
166 esp_openthread_deinit();
167 esp_openthread_netif_glue_deinit();
168 esp_netif_destroy(openthread_netif);
170 esp_vfs_eventfd_unregister();
size_t parse_hex(const char *str, size_t length, uint8_t *data, size_t count)
Parse bytes from a hex-encoded string into a byte array.