61 esp_openthread_platform_config_t config = {
64 .radio_mode = RADIO_MODE_NATIVE,
65 .radio_uart_config = {},
76 .storage_partition_name =
"nvs",
77 .netif_queue_size = 10,
78 .task_queue_size = 10,
84 ESP_ERROR_CHECK(esp_openthread_init(&config));
89 otInstance *instance = esp_openthread_get_instance();
91#if CONFIG_OPENTHREAD_STATE_INDICATOR_ENABLE
92 ESP_ERROR_CHECK(esp_openthread_state_indicator_init(instance));
95#if CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC
97 (void) otLoggingSetLevel(CONFIG_LOG_DEFAULT_LEVEL);
100#if CONFIG_OPENTHREAD_CLI
101 esp_openthread_cli_init();
104 esp_netif_t *openthread_netif;
106 openthread_netif = init_openthread_netif(&config);
107 esp_netif_set_default_netif(openthread_netif);
109#if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
110 esp_cli_custom_command_init();
113 ESP_LOGD(TAG,
"Thread Version: %" PRIu16, otThreadGetVersion());
115 otLinkModeConfig link_mode_config{};
116#if CONFIG_OPENTHREAD_FTD
117 link_mode_config.mRxOnWhenIdle =
true;
118 link_mode_config.mDeviceType =
true;
119 link_mode_config.mNetworkData =
true;
120#elif CONFIG_OPENTHREAD_MTD
122 if (otLinkSetPollPeriod(instance, this->
poll_period_) != OT_ERROR_NONE) {
123 ESP_LOGE(TAG,
"Failed to set pollperiod");
125 ESP_LOGD(TAG,
"Link Polling Period: %" PRIu32, otLinkGetPollPeriod(instance));
127 link_mode_config.mRxOnWhenIdle = this->
poll_period_ == 0;
128 link_mode_config.mDeviceType =
false;
129 link_mode_config.mNetworkData =
false;
132 if (otThreadSetLinkMode(instance, link_mode_config) != OT_ERROR_NONE) {
133 ESP_LOGE(TAG,
"Failed to set linkmode");
135#ifdef ESPHOME_LOG_HAS_DEBUG
136 link_mode_config = otThreadGetLinkMode(instance);
137 ESP_LOGD(TAG,
"Link Mode Device Type: %s, Network Data: %s, RX On When Idle: %s",
138 TRUEFALSE(link_mode_config.mDeviceType), TRUEFALSE(link_mode_config.mNetworkData),
139 TRUEFALSE(link_mode_config.mRxOnWhenIdle));
143 if (
const auto err = otPlatRadioSetTransmitPower(instance, *this->
output_power_); err != OT_ERROR_NONE) {
144 ESP_LOGE(TAG,
"Failed to set power: %s", otThreadErrorToString(err));
149#if CONFIG_OPENTHREAD_CLI
150 esp_openthread_cli_create_task();
152 ESP_LOGI(TAG,
"Activating dataset...");
153 otOperationalDatasetTlvs dataset = {};
155#ifndef USE_OPENTHREAD_FORCE_DATASET
157 otError error = otDatasetGetActiveTlvs(instance, &dataset);
158 if (error != OT_ERROR_NONE) {
162 ESP_LOGI(TAG,
"Found existing dataset, ignoring config (force_dataset: true to override)");
166#ifdef USE_OPENTHREAD_TLVS
167 if (dataset.mLength == 0) {
169 size_t len = (
sizeof(USE_OPENTHREAD_TLVS) - 1) / 2;
170 if (
len >
sizeof(dataset.mTlvs)) {
171 ESP_LOGW(TAG,
"TLV buffer too small, truncating");
172 len =
sizeof(dataset.mTlvs);
174 parse_hex(USE_OPENTHREAD_TLVS,
sizeof(USE_OPENTHREAD_TLVS) - 1, dataset.mTlvs,
len);
175 dataset.mLength =
len;
180 ESP_ERROR_CHECK(esp_openthread_auto_start(dataset.mLength > 0 ? &dataset :
nullptr));
185 esp_openthread_launch_mainloop();
189 esp_openthread_deinit();
190 esp_openthread_netif_glue_deinit();
191 esp_netif_destroy(openthread_netif);
193 esp_vfs_eventfd_unregister();