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));
86#if CONFIG_OPENTHREAD_STATE_INDICATOR_ENABLE
87 ESP_ERROR_CHECK(esp_openthread_state_indicator_init(esp_openthread_get_instance()));
90#if CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC
92 (void) otLoggingSetLevel(CONFIG_LOG_DEFAULT_LEVEL);
95#if CONFIG_OPENTHREAD_CLI
96 esp_openthread_cli_init();
99 esp_netif_t *openthread_netif;
101 openthread_netif = init_openthread_netif(&config);
102 esp_netif_set_default_netif(openthread_netif);
104#if CONFIG_OPENTHREAD_CLI_ESP_EXTENSION
105 esp_cli_custom_command_init();
108 otLinkModeConfig link_mode_config = {0};
109#if CONFIG_OPENTHREAD_FTD
110 link_mode_config.mRxOnWhenIdle =
true;
111 link_mode_config.mDeviceType =
true;
112 link_mode_config.mNetworkData =
true;
113#elif CONFIG_OPENTHREAD_MTD
114 if (this->poll_period > 0) {
115 if (otLinkSetPollPeriod(esp_openthread_get_instance(), this->poll_period) != OT_ERROR_NONE) {
116 ESP_LOGE(TAG,
"Failed to set OpenThread pollperiod.");
118 uint32_t link_polling_period = otLinkGetPollPeriod(esp_openthread_get_instance());
119 ESP_LOGD(TAG,
"Link Polling Period: %d", link_polling_period);
121 link_mode_config.mRxOnWhenIdle = this->poll_period == 0;
122 link_mode_config.mDeviceType =
false;
123 link_mode_config.mNetworkData =
false;
126 if (otThreadSetLinkMode(esp_openthread_get_instance(), link_mode_config) != OT_ERROR_NONE) {
127 ESP_LOGE(TAG,
"Failed to set OpenThread linkmode.");
129 link_mode_config = otThreadGetLinkMode(esp_openthread_get_instance());
130 ESP_LOGD(TAG,
"Link Mode Device Type: %s", link_mode_config.mDeviceType ?
"true" :
"false");
131 ESP_LOGD(TAG,
"Link Mode Network Data: %s", link_mode_config.mNetworkData ?
"true" :
"false");
132 ESP_LOGD(TAG,
"Link Mode RX On When Idle: %s", link_mode_config.mRxOnWhenIdle ?
"true" :
"false");
135#if CONFIG_OPENTHREAD_CLI
136 esp_openthread_cli_create_task();
138 ESP_LOGI(TAG,
"Activating dataset...");
139 otOperationalDatasetTlvs dataset = {};
141#ifndef USE_OPENTHREAD_FORCE_DATASET
143 otError error = otDatasetGetActiveTlvs(esp_openthread_get_instance(), &dataset);
144 if (error != OT_ERROR_NONE) {
148 ESP_LOGI(TAG,
"Found OpenThread-managed dataset, ignoring esphome configuration");
149 ESP_LOGI(TAG,
"(set force_dataset: true to override)");
153#ifdef USE_OPENTHREAD_TLVS
154 if (dataset.mLength == 0) {
156 size_t len = (
sizeof(USE_OPENTHREAD_TLVS) - 1) / 2;
157 if (
len >
sizeof(dataset.mTlvs)) {
158 ESP_LOGW(TAG,
"TLV buffer too small, truncating");
159 len =
sizeof(dataset.mTlvs);
161 parse_hex(USE_OPENTHREAD_TLVS,
sizeof(USE_OPENTHREAD_TLVS) - 1, dataset.mTlvs,
len);
162 dataset.mLength =
len;
167 ESP_ERROR_CHECK(esp_openthread_auto_start(dataset.mLength > 0 ? &dataset :
nullptr));
169 esp_openthread_launch_mainloop();
172 esp_openthread_deinit();
173 esp_openthread_netif_glue_deinit();
174 esp_netif_destroy(openthread_netif);
176 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.