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());
114 otLinkModeConfig link_mode_config{};
115#if CONFIG_OPENTHREAD_FTD
116 link_mode_config.mRxOnWhenIdle =
true;
117 link_mode_config.mDeviceType =
true;
118 link_mode_config.mNetworkData =
true;
119#elif CONFIG_OPENTHREAD_MTD
121 if (otLinkSetPollPeriod(instance, this->
poll_period_) != OT_ERROR_NONE) {
122 ESP_LOGE(TAG,
"Failed to set pollperiod");
124 ESP_LOGD(TAG,
"Link Polling Period: %" PRIu32, otLinkGetPollPeriod(instance));
126 link_mode_config.mRxOnWhenIdle = this->
poll_period_ == 0;
127 link_mode_config.mDeviceType =
false;
128 link_mode_config.mNetworkData =
false;
131 if (otThreadSetLinkMode(instance, link_mode_config) != OT_ERROR_NONE) {
132 ESP_LOGE(TAG,
"Failed to set linkmode");
134#ifdef ESPHOME_LOG_HAS_DEBUG
135 link_mode_config = otThreadGetLinkMode(instance);
136 ESP_LOGD(TAG,
"Link Mode Device Type: %s, Network Data: %s, RX On When Idle: %s",
137 TRUEFALSE(link_mode_config.mDeviceType), TRUEFALSE(link_mode_config.mNetworkData),
138 TRUEFALSE(link_mode_config.mRxOnWhenIdle));
142 if (
const auto err = otPlatRadioSetTransmitPower(instance, *this->
output_power_); err != OT_ERROR_NONE) {
143 ESP_LOGE(TAG,
"Failed to set power: %s", otThreadErrorToString(err));
148#if CONFIG_OPENTHREAD_CLI
149 esp_openthread_cli_create_task();
151 ESP_LOGI(TAG,
"Activating dataset...");
152 otOperationalDatasetTlvs dataset = {};
154#ifndef USE_OPENTHREAD_FORCE_DATASET
156 otError error = otDatasetGetActiveTlvs(instance, &dataset);
157 if (error != OT_ERROR_NONE) {
161 ESP_LOGI(TAG,
"Found existing dataset, ignoring config (force_dataset: true to override)");
165#ifdef USE_OPENTHREAD_TLVS
166 if (dataset.mLength == 0) {
168 size_t len = (
sizeof(USE_OPENTHREAD_TLVS) - 1) / 2;
169 if (
len >
sizeof(dataset.mTlvs)) {
170 ESP_LOGW(TAG,
"TLV buffer too small, truncating");
171 len =
sizeof(dataset.mTlvs);
173 parse_hex(USE_OPENTHREAD_TLVS,
sizeof(USE_OPENTHREAD_TLVS) - 1, dataset.mTlvs,
len);
174 dataset.mLength =
len;
179 ESP_ERROR_CHECK(esp_openthread_auto_start(dataset.mLength > 0 ? &dataset :
nullptr));
183 if (ot_err != OT_ERROR_NONE) {
184 ESP_LOGW(TAG,
"Failed to register state change callback: %d", ot_err);
187 esp_openthread_launch_mainloop();
191 esp_openthread_deinit();
192 esp_openthread_netif_glue_deinit();
193 esp_netif_destroy(openthread_netif);
195 esp_vfs_eventfd_unregister();