board.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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::GetTemperature(float& esp32temp){
  42. return false;
  43. }
  44. Display* Board::GetDisplay() {
  45. static NoDisplay display;
  46. return &display;
  47. }
  48. Camera* Board::GetCamera() {
  49. return nullptr;
  50. }
  51. Led* Board::GetLed() {
  52. static NoLed led;
  53. return &led;
  54. }
  55. std::string Board::GetJson() {
  56. /*
  57. {
  58. "version": 2,
  59. "flash_size": 4194304,
  60. "psram_size": 0,
  61. "minimum_free_heap_size": 123456,
  62. "mac_address": "00:00:00:00:00:00",
  63. "uuid": "00000000-0000-0000-0000-000000000000",
  64. "chip_model_name": "esp32s3",
  65. "chip_info": {
  66. "model": 1,
  67. "cores": 2,
  68. "revision": 0,
  69. "features": 0
  70. },
  71. "application": {
  72. "name": "my-app",
  73. "version": "1.0.0",
  74. "compile_time": "2021-01-01T00:00:00Z"
  75. "idf_version": "4.2-dev"
  76. "elf_sha256": ""
  77. },
  78. "partition_table": [
  79. "app": {
  80. "label": "app",
  81. "type": 1,
  82. "subtype": 2,
  83. "address": 0x10000,
  84. "size": 0x100000
  85. }
  86. ],
  87. "ota": {
  88. "label": "ota_0"
  89. },
  90. "board": {
  91. ...
  92. }
  93. }
  94. */
  95. std::string json = R"({"version":2,"language":")" + std::string(Lang::CODE) + R"(",)";
  96. json += R"("flash_size":)" + std::to_string(SystemInfo::GetFlashSize()) + R"(,)";
  97. json += R"("minimum_free_heap_size":")" + std::to_string(SystemInfo::GetMinimumFreeHeapSize()) + R"(",)";
  98. json += R"("mac_address":")" + SystemInfo::GetMacAddress() + R"(",)";
  99. json += R"("uuid":")" + uuid_ + R"(",)";
  100. json += R"("chip_model_name":")" + SystemInfo::GetChipModelName() + R"(",)";
  101. esp_chip_info_t chip_info;
  102. esp_chip_info(&chip_info);
  103. json += R"("chip_info":{)";
  104. json += R"("model":)" + std::to_string(chip_info.model) + R"(,)";
  105. json += R"("cores":)" + std::to_string(chip_info.cores) + R"(,)";
  106. json += R"("revision":)" + std::to_string(chip_info.revision) + R"(,)";
  107. json += R"("features":)" + std::to_string(chip_info.features) + R"(},)";
  108. auto app_desc = esp_app_get_description();
  109. json += R"("application":{)";
  110. json += R"("name":")" + std::string(app_desc->project_name) + R"(",)";
  111. json += R"("version":")" + std::string(app_desc->version) + R"(",)";
  112. json += R"("compile_time":")" + std::string(app_desc->date) + R"(T)" + std::string(app_desc->time) + R"(Z",)";
  113. json += R"("idf_version":")" + std::string(app_desc->idf_ver) + R"(",)";
  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 += R"("elf_sha256":")" + std::string(sha256_str) + R"(")";
  119. json += R"(},)";
  120. json += R"("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 += R"({)";
  125. json += R"("label":")" + std::string(partition->label) + R"(",)";
  126. json += R"("type":)" + std::to_string(partition->type) + R"(,)";
  127. json += R"("subtype":)" + std::to_string(partition->subtype) + R"(,)";
  128. json += R"("address":)" + std::to_string(partition->address) + R"(,)";
  129. json += R"("size":)" + std::to_string(partition->size) + R"(},)";;
  130. it = esp_partition_next(it);
  131. }
  132. json.pop_back(); // Remove the last comma
  133. json += R"(],)";
  134. json += R"("ota":{)";
  135. auto ota_partition = esp_ota_get_running_partition();
  136. json += R"("label":")" + std::string(ota_partition->label) + R"(")";
  137. json += R"(},)";
  138. json += R"("board":)" + GetBoardJson();
  139. // Close the JSON object
  140. json += R"(})";
  141. return json;
  142. }
  143. void Board::postAlive() {
  144. // 创建HTTP客户端
  145. auto network = Board::GetInstance().GetNetwork();
  146. if (!network) {
  147. ESP_LOGE(TAG, "Failed to get network instance");
  148. return;
  149. }
  150. // 创建 HTTP 客户端
  151. auto http = network->CreateHttp(2); // 使用正确的创建方式
  152. if (!http) {
  153. ESP_LOGE(TAG, "Failed to create HTTP client");
  154. return;
  155. }
  156. std::string json = "{}";
  157. // 获取MAC地址并构建JSON
  158. std::string mac = SystemInfo::GetMacAddress();
  159. ESP_LOGI(TAG, "Device MAC: %s", mac.c_str());
  160. http->SetHeader("macAddress", SystemInfo::GetMacAddress().c_str());
  161. http->SetHeader("Content-Type", "application/json");
  162. http->SetContent(std::move(json));
  163. std::string url ="http://36.134.23.41:8004/xiaozhi-app/app/device/getDeviceEnable";
  164. // 发送POST请求
  165. if (!http->Open("POST",url.c_str())) {
  166. ESP_LOGE(TAG, "HTTP POST failed to open connection");
  167. return;
  168. }
  169. // 获取响应状态码
  170. int status = http->GetStatusCode();
  171. ESP_LOGI(TAG, "HTTP response status: %d", status);
  172. if (status == 200) {
  173. ESP_LOGI(TAG, "POST success");
  174. } else {
  175. ESP_LOGW(TAG, "POST received non-200 status: %d", status);
  176. }
  177. http->Close();
  178. ESP_LOGD(TAG, "postAlive completed");
  179. }