Files
Bionic_sphere/Bionic_Core/ToolsClass/ToolsClass.cpp
T
Misaki a47e20cb64 1. 优化了cpp_json的内容,使其更modern
2. 稍微优化了一下系统配置类
3. 增加了系统版本号,便于区分系统版本,方便OTA
4. 重写OTA的逻辑,完成了Cpp的OTA封装,测试通过
2025-09-24 04:01:23 +08:00

71 lines
2.0 KiB
C++

//
// Created by misaki on 2025/9/2.
//
#include "ToolsClass.h"
#include <esp_mac.h>
#include <esp_efuse.h>
#include <esp_log.h>
#include <array>
#include <esp_efuse_table.h>
#include <sstream>
#include <iomanip>
#include <algorithm>
static const char *TAG = "ToolsClass";
std::string ToolsClass::getChipSerialNumber() {
// 获取芯片序列号(17 字节长的 "Chip ID" (乐鑫官方唯一序列号))
std::array<uint8_t, 17> id{};
/* 下面这个宏在 ESP32-S3 上展开为 17 byte 序列号字段,直接可用 */
esp_err_t err = esp_efuse_read_field_blob(ESP_EFUSE_OPTIONAL_UNIQUE_ID, id.data(), id.size() * 8);
if (err != ESP_OK) {
ESP_LOGE(TAG, "read Chip ID failed: %s", esp_err_to_name(err));
return "";
}
std::stringstream ss;
for (uint8_t v : id) ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(v);
return ss.str();
}
std::string ToolsClass::getChipMAC() {
std::array<uint8_t, 6> mac{};
esp_efuse_mac_get_default(mac.data()); // 读出厂 MAC
std::stringstream ss;
for (size_t i = 0; i < mac.size(); ++i) {
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(mac[i]);
if (i + 1 != mac.size()) ss << ':';
}
return ss.str();
}
// 1. 去掉 mac 中的冒号
// 2. 拼接 chipID
// 3. 取前 24 字符并转大写
std::string ToolsClass::GenerateSN(const std::string& mac, const std::string& chipID)
{
// 1. 去掉 MAC 里的冒号
std::string plainMac;
plainMac.reserve(mac.size());
for (char ch : mac)
if (ch != ':') plainMac.push_back(ch);
// 2. 拼接
std::string raw = plainMac + chipID;
// 3. 取前 24 位并转大写
if (raw.size() > 24) raw.resize(24);
std::transform(raw.begin(), raw.end(), raw.begin(),
[](unsigned char c){ return std::toupper(c); });
return raw;
}
std::string ToolsClass::device_version = "Beta0.3";
std::string ToolsClass::getDeviceVersion() {
return device_version;
}