board.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "board.h"
  2. #include "system_info.h"
  3. #include "settings.h"
  4. #include "display/display.h"
  5. #include "assets/lang_config.h"
  6. #include <esp_log.h>
  7. #include <esp_ota_ops.h>
  8. #include <esp_chip_info.h>
  9. #include <esp_random.h>
  10. #define TAG "Board"
  11. Board::Board() {
  12. Settings settings("board", true);
  13. uuid_ = settings.GetString("uuid");
  14. if (uuid_.empty()) {
  15. uuid_ = GenerateUuid();
  16. settings.SetString("uuid", uuid_);
  17. }
  18. ESP_LOGI(TAG, "UUID=%s SKU=%s", uuid_.c_str(), BOARD_NAME);
  19. }
  20. std::string Board::GenerateUuid() {
  21. // UUID v4 需要 16 字节的随机数据
  22. uint8_t uuid[16];
  23. // 使用 ESP32 的硬件随机数生成器
  24. esp_fill_random(uuid, sizeof(uuid));
  25. // 设置版本 (版本 4) 和变体位
  26. uuid[6] = (uuid[6] & 0x0F) | 0x40; // 版本 4
  27. uuid[8] = (uuid[8] & 0x3F) | 0x80; // 变体 1
  28. // 将字节转换为标准的 UUID 字符串格式
  29. char uuid_str[37];
  30. snprintf(uuid_str, sizeof(uuid_str),
  31. "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
  32. uuid[0], uuid[1], uuid[2], uuid[3],
  33. uuid[4], uuid[5], uuid[6], uuid[7],
  34. uuid[8], uuid[9], uuid[10], uuid[11],
  35. uuid[12], uuid[13], uuid[14], uuid[15]);
  36. return std::string(uuid_str);
  37. }
  38. bool Board::GetBatteryLevel(int &level, bool& charging, bool& discharging) {
  39. return false;
  40. }
  41. Display* Board::GetDisplay() {
  42. static NoDisplay display;
  43. return &display;
  44. }
  45. Led* Board::GetLed() {
  46. static NoLed led;
  47. return &led;
  48. }
  49. std::string Board::GetJson() {
  50. /*
  51. {
  52. "version": 2,
  53. "flash_size": 4194304,
  54. "psram_size": 0,
  55. "minimum_free_heap_size": 123456,
  56. "mac_address": "00:00:00:00:00:00",
  57. "uuid": "00000000-0000-0000-0000-000000000000",
  58. "chip_model_name": "esp32s3",
  59. "chip_info": {
  60. "model": 1,
  61. "cores": 2,
  62. "revision": 0,
  63. "features": 0
  64. },
  65. "application": {
  66. "name": "my-app",
  67. "version": "1.0.0",
  68. "compile_time": "2021-01-01T00:00:00Z"
  69. "idf_version": "4.2-dev"
  70. "elf_sha256": ""
  71. },
  72. "partition_table": [
  73. "app": {
  74. "label": "app",
  75. "type": 1,
  76. "subtype": 2,
  77. "address": 0x10000,
  78. "size": 0x100000
  79. }
  80. ],
  81. "ota": {
  82. "label": "ota_0"
  83. },
  84. "board": {
  85. ...
  86. }
  87. }
  88. */
  89. std::string json = "{";
  90. json += "\"version\":2,";
  91. json += "\"language\":\"" + std::string(Lang::CODE) + "\",";
  92. json += "\"flash_size\":" + std::to_string(SystemInfo::GetFlashSize()) + ",";
  93. json += "\"minimum_free_heap_size\":" + std::to_string(SystemInfo::GetMinimumFreeHeapSize()) + ",";
  94. json += "\"mac_address\":\"" + SystemInfo::GetMacAddress() + "\",";
  95. json += "\"uuid\":\"" + uuid_ + "\",";
  96. json += "\"chip_model_name\":\"" + SystemInfo::GetChipModelName() + "\",";
  97. json += "\"chip_info\":{";
  98. esp_chip_info_t chip_info;
  99. esp_chip_info(&chip_info);
  100. json += "\"model\":" + std::to_string(chip_info.model) + ",";
  101. json += "\"cores\":" + std::to_string(chip_info.cores) + ",";
  102. json += "\"revision\":" + std::to_string(chip_info.revision) + ",";
  103. json += "\"features\":" + std::to_string(chip_info.features);
  104. json += "},";
  105. json += "\"application\":{";
  106. auto app_desc = esp_app_get_description();
  107. json += "\"name\":\"" + std::string(app_desc->project_name) + "\",";
  108. json += "\"version\":\"" + std::string(app_desc->version) + "\",";
  109. json += "\"compile_time\":\"" + std::string(app_desc->date) + "T" + std::string(app_desc->time) + "Z\",";
  110. json += "\"idf_version\":\"" + std::string(app_desc->idf_ver) + "\",";
  111. char sha256_str[65];
  112. for (int i = 0; i < 32; i++) {
  113. snprintf(sha256_str + i * 2, sizeof(sha256_str) - i * 2, "%02x", app_desc->app_elf_sha256[i]);
  114. }
  115. json += "\"elf_sha256\":\"" + std::string(sha256_str) + "\"";
  116. json += "},";
  117. json += "\"partition_table\": [";
  118. esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, NULL);
  119. while (it) {
  120. const esp_partition_t *partition = esp_partition_get(it);
  121. json += "{";
  122. json += "\"label\":\"" + std::string(partition->label) + "\",";
  123. json += "\"type\":" + std::to_string(partition->type) + ",";
  124. json += "\"subtype\":" + std::to_string(partition->subtype) + ",";
  125. json += "\"address\":" + std::to_string(partition->address) + ",";
  126. json += "\"size\":" + std::to_string(partition->size);
  127. json += "},";
  128. it = esp_partition_next(it);
  129. }
  130. json.pop_back(); // Remove the last comma
  131. json += "],";
  132. json += "\"ota\":{";
  133. auto ota_partition = esp_ota_get_running_partition();
  134. json += "\"label\":\"" + std::string(ota_partition->label) + "\"";
  135. json += "},";
  136. json += "\"board\":" + GetBoardJson();
  137. // Close the JSON object
  138. json += "}";
  139. return json;
  140. }