Files
board-mate/esp32-thread/open-thread/main/esp_ot_cli.c
2025-12-24 16:07:10 +01:00

45 lines
1.3 KiB
C

#include "esp_log.h"
#include "esp_err.h"
#include "nvs_flash.h"
#include "esp_openthread.h"
#include "openthread/dataset.h"
static const char *TAG = "thread_app";
void hexstr_to_bytes(const char *hex, uint8_t *out, size_t len) {
for (size_t i = 0; i < len; i++) {
sscanf(hex + 2*i, "%2hhx", &out[i]);
}
}
void app_main(void) {
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_openthread_init(NULL));
// Your hex key string
const char *hexkey = "584a84c2a1bc2ecb2ffcb2a5a0546bac";
uint8_t key[16];
hexstr_to_bytes(hexkey, key, sizeof(key));
// Build a simple TLV for Master Key (type 0x05)
uint8_t tlv_buffer[1 + 1 + 16]; // type + length + data
tlv_buffer[0] = OT_NETWORK_MASTER_KEY; // 0x05
tlv_buffer[1] = sizeof(key);
memcpy(&tlv_buffer[2], key, sizeof(key));
otOperationalDatasetTlvs datasetTlvs = {
.mTlvs = tlv_buffer,
.mLength = sizeof(tlv_buffer)
};
ESP_LOGI(TAG, "Auto starting Thread with raw key TLV");
esp_err_t rc = esp_openthread_auto_start(&datasetTlvs);
if (rc != ESP_OK) {
ESP_LOGE(TAG, "esp_openthread_auto_start failed: %d", rc);
return;
}
ESP_LOGI(TAG, "Launching OpenThread main loop");
esp_openthread_launch_mainloop();
}