forked from xiaozhi/xiaozhi-esp32
add uuid to board
This commit is contained in:
@@ -1,14 +1,46 @@
|
||||
#include "board.h"
|
||||
#include "system_info.h"
|
||||
#include "settings.h"
|
||||
#include "display/no_display.h"
|
||||
|
||||
#include <esp_log.h>
|
||||
#include <esp_ota_ops.h>
|
||||
#include <esp_chip_info.h>
|
||||
#include <esp_random.h>
|
||||
|
||||
#define TAG "Board"
|
||||
|
||||
Board::Board() {
|
||||
Settings settings("board", true);
|
||||
uuid_ = settings.GetString("uuid");
|
||||
if (uuid_.empty()) {
|
||||
uuid_ = GenerateUuid();
|
||||
settings.SetString("uuid", uuid_);
|
||||
}
|
||||
ESP_LOGI(TAG, "UUID: %s", uuid_.c_str());
|
||||
}
|
||||
|
||||
std::string Board::GenerateUuid() {
|
||||
// UUID v4 需要 16 字节的随机数据
|
||||
uint8_t uuid[16];
|
||||
|
||||
// 使用 ESP32 的硬件随机数生成器
|
||||
esp_fill_random(uuid, sizeof(uuid));
|
||||
|
||||
// 设置版本 (版本 4) 和变体位
|
||||
uuid[6] = (uuid[6] & 0x0F) | 0x40; // 版本 4
|
||||
uuid[8] = (uuid[8] & 0x3F) | 0x80; // 变体 1
|
||||
|
||||
// 将字节转换为标准的 UUID 字符串格式
|
||||
char uuid_str[37];
|
||||
snprintf(uuid_str, sizeof(uuid_str),
|
||||
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
||||
uuid[0], uuid[1], uuid[2], uuid[3],
|
||||
uuid[4], uuid[5], uuid[6], uuid[7],
|
||||
uuid[8], uuid[9], uuid[10], uuid[11],
|
||||
uuid[12], uuid[13], uuid[14], uuid[15]);
|
||||
|
||||
return std::string(uuid_str);
|
||||
}
|
||||
|
||||
bool Board::GetBatteryLevel(int &level, bool& charging) {
|
||||
@@ -28,10 +60,12 @@ Led* Board::GetLed() {
|
||||
std::string Board::GetJson() {
|
||||
/*
|
||||
{
|
||||
"version": 2,
|
||||
"flash_size": 4194304,
|
||||
"psram_size": 0,
|
||||
"minimum_free_heap_size": 123456,
|
||||
"mac_address": "00:00:00:00:00:00",
|
||||
"uuid": "00000000-0000-0000-0000-000000000000",
|
||||
"chip_model_name": "esp32s3",
|
||||
"chip_info": {
|
||||
"model": 1,
|
||||
@@ -57,13 +91,18 @@ std::string Board::GetJson() {
|
||||
],
|
||||
"ota": {
|
||||
"label": "ota_0"
|
||||
},
|
||||
"board": {
|
||||
...
|
||||
}
|
||||
}
|
||||
*/
|
||||
std::string json = "{";
|
||||
json += "\"version\":2,";
|
||||
json += "\"flash_size\":" + std::to_string(SystemInfo::GetFlashSize()) + ",";
|
||||
json += "\"minimum_free_heap_size\":" + std::to_string(SystemInfo::GetMinimumFreeHeapSize()) + ",";
|
||||
json += "\"mac_address\":\"" + SystemInfo::GetMacAddress() + "\",";
|
||||
json += "\"uuid\":\"" + uuid_ + "\",";
|
||||
json += "\"chip_model_name\":\"" + SystemInfo::GetChipModelName() + "\",";
|
||||
json += "\"chip_info\":{";
|
||||
|
||||
|
||||
@@ -20,6 +20,10 @@ private:
|
||||
|
||||
protected:
|
||||
Board();
|
||||
std::string GenerateUuid();
|
||||
|
||||
// 软件生成的设备唯一标识
|
||||
std::string uuid_;
|
||||
|
||||
public:
|
||||
static Board& GetInstance() {
|
||||
@@ -29,6 +33,7 @@ public:
|
||||
|
||||
virtual ~Board() = default;
|
||||
virtual std::string GetBoardType() = 0;
|
||||
virtual std::string GetUuid() { return uuid_; }
|
||||
virtual Led* GetLed();
|
||||
virtual AudioCodec* GetAudioCodec() = 0;
|
||||
virtual Display* GetDisplay();
|
||||
|
||||
Reference in New Issue
Block a user