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));
85 otInstance *instance = esp_openthread_get_instance();
87#if CONFIG_OPENTHREAD_STATE_INDICATOR_ENABLE
88 ESP_ERROR_CHECK(esp_openthread_state_indicator_init(instance));
91#if CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC
93 (void) otLoggingSetLevel(CONFIG_LOG_DEFAULT_LEVEL);
96#if CONFIG_OPENTHREAD_CLI
97 esp_openthread_cli_init();
100 esp_netif_t *openthread_netif;
102 openthread_netif = init_openthread_netif(&config);
103 esp_netif_set_default_netif(openthread_netif);
105#if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
106 esp_cli_custom_command_init();
109 ESP_LOGD(TAG,
"Thread Version: %" PRIu16, otThreadGetVersion());
111 otLinkModeConfig link_mode_config{};
112#if CONFIG_OPENTHREAD_FTD
113 link_mode_config.mRxOnWhenIdle =
true;
114 link_mode_config.mDeviceType =
true;
115 link_mode_config.mNetworkData =
true;
116#elif CONFIG_OPENTHREAD_MTD
118 if (otLinkSetPollPeriod(instance, this->
poll_period_) != OT_ERROR_NONE) {
119 ESP_LOGE(TAG,
"Failed to set pollperiod");
121 ESP_LOGD(TAG,
"Link Polling Period: %" PRIu32, otLinkGetPollPeriod(instance));
123 link_mode_config.mRxOnWhenIdle = this->
poll_period_ == 0;
124 link_mode_config.mDeviceType =
false;
125 link_mode_config.mNetworkData =
false;
128 if (otThreadSetLinkMode(instance, link_mode_config) != OT_ERROR_NONE) {
129 ESP_LOGE(TAG,
"Failed to set linkmode");
131#ifdef ESPHOME_LOG_HAS_DEBUG
132 link_mode_config = otThreadGetLinkMode(instance);
133 ESP_LOGD(TAG,
"Link Mode Device Type: %s, Network Data: %s, RX On When Idle: %s",
134 TRUEFALSE(link_mode_config.mDeviceType), TRUEFALSE(link_mode_config.mNetworkData),
135 TRUEFALSE(link_mode_config.mRxOnWhenIdle));
139#if CONFIG_OPENTHREAD_CLI
140 esp_openthread_cli_create_task();
142 ESP_LOGI(TAG,
"Activating dataset...");
143 otOperationalDatasetTlvs dataset = {};
145#ifndef USE_OPENTHREAD_FORCE_DATASET
147 otError error = otDatasetGetActiveTlvs(instance, &dataset);
148 if (error != OT_ERROR_NONE) {
152 ESP_LOGI(TAG,
"Found existing dataset, ignoring config (force_dataset: true to override)");
156#ifdef USE_OPENTHREAD_TLVS
157 if (dataset.mLength == 0) {
159 size_t len = (
sizeof(USE_OPENTHREAD_TLVS) - 1) / 2;
160 if (
len >
sizeof(dataset.mTlvs)) {
161 ESP_LOGW(TAG,
"TLV buffer too small, truncating");
162 len =
sizeof(dataset.mTlvs);
164 parse_hex(USE_OPENTHREAD_TLVS,
sizeof(USE_OPENTHREAD_TLVS) - 1, dataset.mTlvs,
len);
165 dataset.mLength =
len;
170 ESP_ERROR_CHECK(esp_openthread_auto_start(dataset.mLength > 0 ? &dataset :
nullptr));
172 esp_openthread_launch_mainloop();
175 esp_openthread_deinit();
176 esp_openthread_netif_glue_deinit();
177 esp_netif_destroy(openthread_netif);
179 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.