board.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. bool Board::GetBatteryAverageADC(uint32_t &averageADC) {
  42. return false;
  43. }
  44. Display* Board::GetDisplay() {
  45. static NoDisplay display;
  46. return &display;
  47. }
  48. Led* Board::GetLed() {
  49. static NoLed led;
  50. return &led;
  51. }
  52. std::string Board::GetJson() {
  53. /*
  54. {
  55. "version": 2,
  56. "flash_size": 4194304,
  57. "psram_size": 0,
  58. "minimum_free_heap_size": 123456,
  59. "mac_address": "00:00:00:00:00:00",
  60. "uuid": "00000000-0000-0000-0000-000000000000",
  61. "chip_model_name": "esp32s3",
  62. "chip_info": {
  63. "model": 1,
  64. "cores": 2,
  65. "revision": 0,
  66. "features": 0
  67. },
  68. "application": {
  69. "name": "my-app",
  70. "version": "1.0.0",
  71. "compile_time": "2021-01-01T00:00:00Z"
  72. "idf_version": "4.2-dev"
  73. "elf_sha256": ""
  74. },
  75. "partition_table": [
  76. "app": {
  77. "label": "app",
  78. "type": 1,
  79. "subtype": 2,
  80. "address": 0x10000,
  81. "size": 0x100000
  82. }
  83. ],
  84. "ota": {
  85. "label": "ota_0"
  86. },
  87. "board": {
  88. ...
  89. }
  90. }
  91. */
  92. std::string json = "{";
  93. json += "\"version\":2,";
  94. json += "\"language\":\"" + std::string(Lang::CODE) + "\",";
  95. json += "\"flash_size\":" + std::to_string(SystemInfo::GetFlashSize()) + ",";
  96. json += "\"minimum_free_heap_size\":" + std::to_string(SystemInfo::GetMinimumFreeHeapSize()) + ",";
  97. json += "\"mac_address\":\"" + SystemInfo::GetMacAddress() + "\",";
  98. json += "\"uuid\":\"" + uuid_ + "\",";
  99. json += "\"chip_model_name\":\"" + SystemInfo::GetChipModelName() + "\",";
  100. json += "\"chip_info\":{";
  101. esp_chip_info_t chip_info;
  102. esp_chip_info(&chip_info);
  103. json += "\"model\":" + std::to_string(chip_info.model) + ",";
  104. json += "\"cores\":" + std::to_string(chip_info.cores) + ",";
  105. json += "\"revision\":" + std::to_string(chip_info.revision) + ",";
  106. json += "\"features\":" + std::to_string(chip_info.features);
  107. json += "},";
  108. json += "\"application\":{";
  109. auto app_desc = esp_app_get_description();
  110. json += "\"name\":\"" + std::string(app_desc->project_name) + "\",";
  111. json += "\"version\":\"" + std::string(app_desc->version) + "\",";
  112. json += "\"compile_time\":\"" + std::string(app_desc->date) + "T" + std::string(app_desc->time) + "Z\",";
  113. json += "\"idf_version\":\"" + std::string(app_desc->idf_ver) + "\",";
  114. char sha256_str[65];
  115. for (int i = 0; i < 32; i++) {
  116. snprintf(sha256_str + i * 2, sizeof(sha256_str) - i * 2, "%02x", app_desc->app_elf_sha256[i]);
  117. }
  118. json += "\"elf_sha256\":\"" + std::string(sha256_str) + "\"";
  119. json += "},";
  120. json += "\"partition_table\": [";
  121. esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, NULL);
  122. while (it) {
  123. const esp_partition_t *partition = esp_partition_get(it);
  124. json += "{";
  125. json += "\"label\":\"" + std::string(partition->label) + "\",";
  126. json += "\"type\":" + std::to_string(partition->type) + ",";
  127. json += "\"subtype\":" + std::to_string(partition->subtype) + ",";
  128. json += "\"address\":" + std::to_string(partition->address) + ",";
  129. json += "\"size\":" + std::to_string(partition->size);
  130. json += "},";
  131. it = esp_partition_next(it);
  132. }
  133. json.pop_back(); // Remove the last comma
  134. json += "],";
  135. json += "\"ota\":{";
  136. auto ota_partition = esp_ota_get_running_partition();
  137. json += "\"label\":\"" + std::string(ota_partition->label) + "\"";
  138. json += "},";
  139. json += "\"board\":" + GetBoardJson();
  140. // Close the JSON object
  141. json += "}";
  142. return json;
  143. }