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#if CONFIG_OPENTHREAD_STATE_INDICATOR_ENABLE
86 ESP_ERROR_CHECK(esp_openthread_state_indicator_init(esp_openthread_get_instance()));
89#if CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC
91 (void) otLoggingSetLevel(CONFIG_LOG_DEFAULT_LEVEL);
94#if CONFIG_OPENTHREAD_CLI
95 esp_openthread_cli_init();
98 esp_netif_t *openthread_netif;
100 openthread_netif = init_openthread_netif(&config);
101 esp_netif_set_default_netif(openthread_netif);
103#if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
104 esp_cli_custom_command_init();
107 otLinkModeConfig link_mode_config = {0};
108#if CONFIG_OPENTHREAD_FTD
109 link_mode_config.mRxOnWhenIdle =
true;
110 link_mode_config.mDeviceType =
true;
111 link_mode_config.mNetworkData =
true;
112#elif CONFIG_OPENTHREAD_MTD
113 if (this->poll_period > 0) {
114 if (otLinkSetPollPeriod(esp_openthread_get_instance(), this->poll_period) != OT_ERROR_NONE) {
115 ESP_LOGE(TAG,
"Failed to set OpenThread pollperiod.");
117 uint32_t link_polling_period = otLinkGetPollPeriod(esp_openthread_get_instance());
118 ESP_LOGD(TAG,
"Link Polling Period: %d", link_polling_period);
120 link_mode_config.mRxOnWhenIdle = this->poll_period == 0;
121 link_mode_config.mDeviceType =
false;
122 link_mode_config.mNetworkData =
false;
125 if (otThreadSetLinkMode(esp_openthread_get_instance(), link_mode_config) != OT_ERROR_NONE) {
126 ESP_LOGE(TAG,
"Failed to set OpenThread linkmode.");
128 link_mode_config = otThreadGetLinkMode(esp_openthread_get_instance());
129 ESP_LOGD(TAG,
"Link Mode Device Type: %s", link_mode_config.mDeviceType ?
"true" :
"false");
130 ESP_LOGD(TAG,
"Link Mode Network Data: %s", link_mode_config.mNetworkData ?
"true" :
"false");
131 ESP_LOGD(TAG,
"Link Mode RX On When Idle: %s", link_mode_config.mRxOnWhenIdle ?
"true" :
"false");
134#if CONFIG_OPENTHREAD_CLI
135 esp_openthread_cli_create_task();
137 ESP_LOGI(TAG,
"Activating dataset...");
138 otOperationalDatasetTlvs dataset = {};
140#ifndef USE_OPENTHREAD_FORCE_DATASET
142 otError error = otDatasetGetActiveTlvs(esp_openthread_get_instance(), &dataset);
143 if (error != OT_ERROR_NONE) {
147 ESP_LOGI(TAG,
"Found OpenThread-managed dataset, ignoring esphome configuration");
148 ESP_LOGI(TAG,
"(set force_dataset: true to override)");
152#ifdef USE_OPENTHREAD_TLVS
153 if (dataset.mLength == 0) {
155 size_t len = (
sizeof(USE_OPENTHREAD_TLVS) - 1) / 2;
156 if (
len >
sizeof(dataset.mTlvs)) {
157 ESP_LOGW(TAG,
"TLV buffer too small, truncating");
158 len =
sizeof(dataset.mTlvs);
160 parse_hex(USE_OPENTHREAD_TLVS,
sizeof(USE_OPENTHREAD_TLVS) - 1, dataset.mTlvs,
len);
161 dataset.mLength =
len;
166 ESP_ERROR_CHECK(esp_openthread_auto_start(dataset.mLength > 0 ? &dataset :
nullptr));
168 esp_openthread_launch_mainloop();
171 esp_openthread_deinit();
172 esp_openthread_netif_glue_deinit();
173 esp_netif_destroy(openthread_netif);
175 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.