add Board::GetJson

This commit is contained in:
Terrence
2024-11-03 05:54:15 +08:00
parent 5006e5bda1
commit 92efdc9b64
16 changed files with 131 additions and 115 deletions

View File

@@ -4,7 +4,7 @@
# CMakeLists in this exact order for cmake to work correctly # CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
set(PROJECT_VER "0.6.2") set(PROJECT_VER "0.6.3")
include($ENV{IDF_PATH}/tools/cmake/project.cmake) include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(xiaozhi) project(xiaozhi)

2
flash.sh Executable file
View File

@@ -0,0 +1,2 @@
#!/bin/sh
esptool.py -p /dev/ttyACM0 -b 2000000 write_flash 0 releases/v0.6.2_ML307/merged-binary.bin

View File

@@ -70,7 +70,6 @@ Application::~Application() {
void Application::CheckNewVersion() { void Application::CheckNewVersion() {
// Check if there is a new firmware version available // Check if there is a new firmware version available
firmware_upgrade_.SetBoardJson(Board::GetInstance().GetJson());
firmware_upgrade_.CheckVersion(); firmware_upgrade_.CheckVersion();
if (firmware_upgrade_.HasNewVersion()) { if (firmware_upgrade_.HasNewVersion()) {
// Wait for the chat state to be idle // Wait for the chat state to be idle
@@ -196,6 +195,7 @@ void Application::Start() {
}, "play_audio", 4096 * 4, this, 4, NULL); }, "play_audio", 4096 * 4, this, 4, NULL);
board.StartNetwork(); board.StartNetwork();
firmware_upgrade_.SetPostData(board.GetJson());
// Blink the LED to indicate the device is running // Blink the LED to indicate the device is running
builtin_led.SetGreen(); builtin_led.SetGreen();
builtin_led.BlinkOnce(); builtin_led.BlinkOnce();
@@ -445,6 +445,7 @@ void Application::SetChatState(ChatState state) {
BinaryProtocol3* Application::AllocateBinaryProtocol3(const uint8_t* payload, size_t payload_size) { BinaryProtocol3* Application::AllocateBinaryProtocol3(const uint8_t* payload, size_t payload_size) {
auto protocol = (BinaryProtocol3*)heap_caps_malloc(sizeof(BinaryProtocol3) + payload_size, MALLOC_CAP_SPIRAM); auto protocol = (BinaryProtocol3*)heap_caps_malloc(sizeof(BinaryProtocol3) + payload_size, MALLOC_CAP_SPIRAM);
assert(protocol != nullptr);
protocol->type = 0; protocol->type = 0;
protocol->reserved = 0; protocol->reserved = 0;
protocol->payload_size = htons(payload_size); protocol->payload_size = htons(payload_size);

View File

@@ -1,8 +1,104 @@
#include "Board.h" #include "Board.h"
#include "SystemInfo.h"
#include <esp_log.h> #include <esp_log.h>
#include <esp_ota_ops.h>
#include <esp_chip_info.h>
// static const char *TAG = "Board"; // static const char *TAG = "Board";
bool Board::GetBatteryVoltage(int &voltage, bool& charging) { bool Board::GetBatteryVoltage(int &voltage, bool& charging) {
return false; return false;
} }
std::string Board::GetJson() {
/*
{
"flash_size": 4194304,
"psram_size": 0,
"minimum_free_heap_size": 123456,
"mac_address": "00:00:00:00:00:00",
"chip_model_name": "esp32s3",
"chip_info": {
"model": 1,
"cores": 2,
"revision": 0,
"features": 0
},
"application": {
"name": "my-app",
"version": "1.0.0",
"compile_time": "2021-01-01T00:00:00Z"
"idf_version": "4.2-dev"
"elf_sha256": ""
},
"partition_table": [
"app": {
"label": "app",
"type": 1,
"subtype": 2,
"address": 0x10000,
"size": 0x100000
}
],
"ota": {
"label": "ota_0"
}
}
*/
std::string json = "{";
json += "\"flash_size\":" + std::to_string(SystemInfo::GetFlashSize()) + ",";
json += "\"minimum_free_heap_size\":" + std::to_string(SystemInfo::GetMinimumFreeHeapSize()) + ",";
json += "\"mac_address\":\"" + SystemInfo::GetMacAddress() + "\",";
json += "\"chip_model_name\":\"" + SystemInfo::GetChipModelName() + "\",";
json += "\"chip_info\":{";
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
json += "\"model\":" + std::to_string(chip_info.model) + ",";
json += "\"cores\":" + std::to_string(chip_info.cores) + ",";
json += "\"revision\":" + std::to_string(chip_info.revision) + ",";
json += "\"features\":" + std::to_string(chip_info.features);
json += "},";
json += "\"application\":{";
auto app_desc = esp_app_get_description();
json += "\"name\":\"" + std::string(app_desc->project_name) + "\",";
json += "\"version\":\"" + std::string(app_desc->version) + "\",";
json += "\"compile_time\":\"" + std::string(app_desc->date) + "T" + std::string(app_desc->time) + "Z\",";
json += "\"idf_version\":\"" + std::string(app_desc->idf_ver) + "\",";
char sha256_str[65];
for (int i = 0; i < 32; i++) {
snprintf(sha256_str + i * 2, sizeof(sha256_str) - i * 2, "%02x", app_desc->app_elf_sha256[i]);
}
json += "\"elf_sha256\":\"" + std::string(sha256_str) + "\"";
json += "},";
json += "\"partition_table\": [";
esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, NULL);
while (it) {
const esp_partition_t *partition = esp_partition_get(it);
json += "{";
json += "\"label\":\"" + std::string(partition->label) + "\",";
json += "\"type\":" + std::to_string(partition->type) + ",";
json += "\"subtype\":" + std::to_string(partition->subtype) + ",";
json += "\"address\":" + std::to_string(partition->address) + ",";
json += "\"size\":" + std::to_string(partition->size);
json += "},";
it = esp_partition_next(it);
}
json.pop_back(); // Remove the last comma
json += "],";
json += "\"ota\":{";
auto ota_partition = esp_ota_get_running_partition();
json += "\"label\":\"" + std::string(ota_partition->label) + "\"";
json += "},";
json += "\"board\":" + GetBoardJson();
// Close the JSON object
json += "}";
return json;
}

View File

@@ -27,7 +27,7 @@ public:
virtual WebSocket* CreateWebSocket() = 0; virtual WebSocket* CreateWebSocket() = 0;
virtual bool GetNetworkState(std::string& network_name, int& signal_quality, std::string& signal_quality_text) = 0; virtual bool GetNetworkState(std::string& network_name, int& signal_quality, std::string& signal_quality_text) = 0;
virtual bool GetBatteryVoltage(int &voltage, bool& charging); virtual bool GetBatteryVoltage(int &voltage, bool& charging);
virtual std::string GetJson() = 0; virtual std::string GetJson();
protected: protected:
Board() = default; Board() = default;
@@ -35,6 +35,7 @@ protected:
private: private:
Board(const Board&) = delete; // 禁用拷贝构造函数 Board(const Board&) = delete; // 禁用拷贝构造函数
Board& operator=(const Board&) = delete; // 禁用赋值操作 Board& operator=(const Board&) = delete; // 禁用赋值操作
virtual std::string GetBoardJson() = 0;
}; };
#define DECLARE_BOARD(BOARD_CLASS_NAME) \ #define DECLARE_BOARD(BOARD_CLASS_NAME) \

View File

@@ -40,7 +40,7 @@ void BoxAudioDevice::Initialize() {
.intr_priority = 0, .intr_priority = 0,
.trans_queue_depth = 0, .trans_queue_depth = 0,
.flags = { .flags = {
.enable_internal_pullup = 1, .enable_internal_pullup = 0,
}, },
}; };
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_master_handle_)); ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_cfg, &i2c_master_handle_));

View File

@@ -64,4 +64,6 @@ idf_component_register(SRCS ${SOURCES}
) )
# 使用 target_compile_definitions 来定义 BOARD_TYPE # 使用 target_compile_definitions 来定义 BOARD_TYPE
target_compile_definitions(${COMPONENT_LIB} PRIVATE BOARD_TYPE=\"${BOARD_TYPE}\") target_compile_definitions(${COMPONENT_LIB}
PRIVATE BOARD_TYPE=\"${BOARD_TYPE}\"
)

View File

@@ -27,7 +27,7 @@ Display::Display(int sda_pin, int scl_pin) : sda_pin_(sda_pin), scl_pin_(scl_pin
.intr_priority = 0, .intr_priority = 0,
.trans_queue_depth = 0, .trans_queue_depth = 0,
.flags = { .flags = {
.enable_internal_pullup = 1, .enable_internal_pullup = 0,
}, },
}; };

View File

@@ -5,11 +5,10 @@
#include <cJSON.h> #include <cJSON.h>
#include <esp_log.h> #include <esp_log.h>
#include <esp_partition.h> #include <esp_partition.h>
#include <esp_http_client.h>
#include <esp_ota_ops.h> #include <esp_ota_ops.h>
#include <esp_app_format.h> #include <esp_app_format.h>
#include <esp_chip_info.h>
#include <cstring>
#include <vector> #include <vector>
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
@@ -31,6 +30,10 @@ void FirmwareUpgrade::SetHeader(const std::string& key, const std::string& value
headers_[key] = value; headers_[key] = value;
} }
void FirmwareUpgrade::SetPostData(const std::string& post_data) {
post_data_ = post_data;
}
void FirmwareUpgrade::CheckVersion() { void FirmwareUpgrade::CheckVersion() {
std::string current_version = esp_app_get_description()->version; std::string current_version = esp_app_get_description()->version;
ESP_LOGI(TAG, "Current version: %s", current_version.c_str()); ESP_LOGI(TAG, "Current version: %s", current_version.c_str());
@@ -46,8 +49,12 @@ void FirmwareUpgrade::CheckVersion() {
} }
http->SetHeader("Content-Type", "application/json"); http->SetHeader("Content-Type", "application/json");
http->SetContent(GetPostData()); if (post_data_.length() > 0) {
http->Open("POST", check_version_url_); http->SetContent(post_data_);
http->Open("POST", check_version_url_);
} else {
http->Open("GET", check_version_url_);
}
auto response = http->GetBody(); auto response = http->GetBody();
http->Close(); http->Close();
@@ -256,99 +263,3 @@ bool FirmwareUpgrade::IsNewVersionAvailable(const std::string& currentVersion, c
return newer.size() > current.size(); return newer.size() > current.size();
} }
void FirmwareUpgrade::SetBoardJson(const std::string& board_json) {
board_json_ = board_json;
}
std::string FirmwareUpgrade::GetPostData() {
/*
{
"flash_size": 4194304,
"psram_size": 0,
"minimum_free_heap_size": 123456,
"mac_address": "00:00:00:00:00:00",
"chip_model_name": "esp32s3",
"chip_info": {
"model": 1,
"cores": 2,
"revision": 0,
"features": 0
},
"application": {
"name": "my-app",
"version": "1.0.0",
"compile_time": "2021-01-01T00:00:00Z"
"idf_version": "4.2-dev"
"elf_sha256": ""
},
"partition_table": [
"app": {
"label": "app",
"type": 1,
"subtype": 2,
"address": 0x10000,
"size": 0x100000
}
],
"ota": {
"label": "ota_0"
}
}
*/
std::string json = "{";
json += "\"flash_size\":" + std::to_string(SystemInfo::GetFlashSize()) + ",";
json += "\"minimum_free_heap_size\":" + std::to_string(SystemInfo::GetMinimumFreeHeapSize()) + ",";
json += "\"mac_address\":\"" + SystemInfo::GetMacAddress() + "\",";
json += "\"chip_model_name\":\"" + SystemInfo::GetChipModelName() + "\",";
json += "\"chip_info\":{";
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
json += "\"model\":" + std::to_string(chip_info.model) + ",";
json += "\"cores\":" + std::to_string(chip_info.cores) + ",";
json += "\"revision\":" + std::to_string(chip_info.revision) + ",";
json += "\"features\":" + std::to_string(chip_info.features);
json += "},";
json += "\"application\":{";
auto app_desc = esp_app_get_description();
json += "\"name\":\"" + std::string(app_desc->project_name) + "\",";
json += "\"version\":\"" + std::string(app_desc->version) + "\",";
json += "\"compile_time\":\"" + std::string(app_desc->date) + "T" + std::string(app_desc->time) + "Z\",";
json += "\"idf_version\":\"" + std::string(app_desc->idf_ver) + "\",";
char sha256_str[65];
for (int i = 0; i < 32; i++) {
snprintf(sha256_str + i * 2, sizeof(sha256_str) - i * 2, "%02x", app_desc->app_elf_sha256[i]);
}
json += "\"elf_sha256\":\"" + std::string(sha256_str) + "\"";
json += "},";
json += "\"partition_table\": [";
esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, NULL);
while (it) {
const esp_partition_t *partition = esp_partition_get(it);
json += "{";
json += "\"label\":\"" + std::string(partition->label) + "\",";
json += "\"type\":" + std::to_string(partition->type) + ",";
json += "\"subtype\":" + std::to_string(partition->subtype) + ",";
json += "\"address\":" + std::to_string(partition->address) + ",";
json += "\"size\":" + std::to_string(partition->size);
json += "},";
it = esp_partition_next(it);
}
json.pop_back(); // Remove the last comma
json += "],";
json += "\"ota\":{";
auto ota_partition = esp_ota_get_running_partition();
json += "\"label\":\"" + std::string(ota_partition->label) + "\"";
json += "},";
json += "\"board\":" + board_json_;
// Close the JSON object
json += "}";
return json;
}

View File

@@ -10,9 +10,9 @@ public:
FirmwareUpgrade(); FirmwareUpgrade();
~FirmwareUpgrade(); ~FirmwareUpgrade();
void SetBoardJson(const std::string& board_json);
void SetCheckVersionUrl(std::string check_version_url); void SetCheckVersionUrl(std::string check_version_url);
void SetHeader(const std::string& key, const std::string& value); void SetHeader(const std::string& key, const std::string& value);
void SetPostData(const std::string& post_data);
void CheckVersion(); void CheckVersion();
bool HasNewVersion() { return has_new_version_; } bool HasNewVersion() { return has_new_version_; }
void StartUpgrade(std::function<void(int progress, size_t speed)> callback); void StartUpgrade(std::function<void(int progress, size_t speed)> callback);
@@ -23,14 +23,13 @@ private:
bool has_new_version_ = false; bool has_new_version_ = false;
std::string firmware_version_; std::string firmware_version_;
std::string firmware_url_; std::string firmware_url_;
std::string board_json_; std::string post_data_;
std::map<std::string, std::string> headers_; std::map<std::string, std::string> headers_;
void Upgrade(const std::string& firmware_url); void Upgrade(const std::string& firmware_url);
std::function<void(int progress, size_t speed)> upgrade_callback_; std::function<void(int progress, size_t speed)> upgrade_callback_;
std::vector<int> ParseVersion(const std::string& version); std::vector<int> ParseVersion(const std::string& version);
bool IsNewVersionAvailable(const std::string& currentVersion, const std::string& newVersion); bool IsNewVersionAvailable(const std::string& currentVersion, const std::string& newVersion);
std::string GetPostData();
}; };
#endif // _FIRMWARE_UPGRADE_H #endif // _FIRMWARE_UPGRADE_H

View File

@@ -94,7 +94,7 @@ bool Ml307Board::GetNetworkState(std::string& network_name, int& signal_quality,
return signal_quality != -1; return signal_quality != -1;
} }
std::string Ml307Board::GetJson() { std::string Ml307Board::GetBoardJson() {
// Set the board type for OTA // Set the board type for OTA
std::string board_type = BOARD_TYPE; std::string board_type = BOARD_TYPE;
std::string module_name = modem_.GetModuleName(); std::string module_name = modem_.GetModuleName();

View File

@@ -8,6 +8,7 @@ class Ml307Board : public Board {
protected: protected:
Ml307AtModem modem_; Ml307AtModem modem_;
virtual std::string GetBoardJson() override;
void StartModem(); void StartModem();
public: public:
@@ -18,7 +19,6 @@ public:
virtual Http* CreateHttp() override; virtual Http* CreateHttp() override;
virtual WebSocket* CreateWebSocket() override; virtual WebSocket* CreateWebSocket() override;
virtual bool GetNetworkState(std::string& network_name, int& signal_quality, std::string& signal_quality_text) override; virtual bool GetNetworkState(std::string& network_name, int& signal_quality, std::string& signal_quality_text) override;
virtual std::string GetJson() override;
}; };
#endif // ML307_BOARD_H #endif // ML307_BOARD_H

View File

@@ -87,7 +87,7 @@ bool WifiBoard::GetNetworkState(std::string& network_name, int& signal_quality,
return signal_quality != -1; return signal_quality != -1;
} }
std::string WifiBoard::GetJson() { std::string WifiBoard::GetBoardJson() {
// Set the board type for OTA // Set the board type for OTA
auto& wifi_station = WifiStation::GetInstance(); auto& wifi_station = WifiStation::GetInstance();
std::string board_type = BOARD_TYPE; std::string board_type = BOARD_TYPE;

View File

@@ -7,13 +7,14 @@ class WifiBoard : public Board {
protected: protected:
bool wifi_config_mode_ = false; bool wifi_config_mode_ = false;
virtual std::string GetBoardJson() override;
public: public:
virtual void Initialize() override; virtual void Initialize() override;
virtual void StartNetwork() override; virtual void StartNetwork() override;
virtual Http* CreateHttp() override; virtual Http* CreateHttp() override;
virtual WebSocket* CreateWebSocket() override; virtual WebSocket* CreateWebSocket() override;
virtual bool GetNetworkState(std::string& network_name, int& signal_quality, std::string& signal_quality_text) override; virtual bool GetNetworkState(std::string& network_name, int& signal_quality, std::string& signal_quality_text) override;
virtual std::string GetJson() override;
}; };
#endif // WIFI_BOARD_H #endif // WIFI_BOARD_H

View File

@@ -29,7 +29,7 @@ private:
void Enable4GModule() { void Enable4GModule() {
// Make GPIO15 HIGH to enable the 4G module // Make GPIO15 HIGH to enable the 4G module
gpio_config_t ml307_enable_config = { gpio_config_t ml307_enable_config = {
.pin_bit_mask = (1ULL << 15), .pin_bit_mask = (1ULL << 15) | (1ULL << 18),
.mode = GPIO_MODE_OUTPUT, .mode = GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_DISABLE, .pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE, .pull_down_en = GPIO_PULLDOWN_DISABLE,
@@ -37,6 +37,7 @@ private:
}; };
gpio_config(&ml307_enable_config); gpio_config(&ml307_enable_config);
gpio_set_level(GPIO_NUM_15, 1); gpio_set_level(GPIO_NUM_15, 1);
gpio_set_level(GPIO_NUM_18, 1);
} }
virtual void InitializeADC() { virtual void InitializeADC() {

View File

@@ -60,8 +60,10 @@ def get_board_name(folder):
if basename.startswith("v0.3") or basename.startswith("v0.4") or basename.startswith("v0.5") or basename.startswith("v0.6"): if basename.startswith("v0.3") or basename.startswith("v0.4") or basename.startswith("v0.5") or basename.startswith("v0.6"):
if "ML307" in basename: if "ML307" in basename:
return "bread-compact-ml307" return "bread-compact-ml307"
else: elif "WiFi" in basename:
return "bread-compact-wifi" return "bread-compact-wifi"
elif "KevinBox1" in basename:
return "kevin-box-1"
raise Exception(f"Unknown board name: {basename}") raise Exception(f"Unknown board name: {basename}")
def read_binary(dir_path): def read_binary(dir_path):