From 6e3a48cba6e95cec9e610d9074080aa16111cffb Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 8 Apr 2025 23:00:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E5=90=8D=E7=A7=B0=E8=BF=87=E9=95=BF=E5=8F=AF=E8=83=BD=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E7=B3=BB=E7=BB=9F=E5=B4=A9=E6=BA=83=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TC1/http_server/app_httpd.c | 72 +++++++++++++++++------------- TC1/http_server/web/index.html | 23 +++++----- TC1/mqtt_server/user_mqtt_client.c | 3 +- TC1/mqtt_server/user_mqtt_client.h | 4 +- 4 files changed, 58 insertions(+), 44 deletions(-) diff --git a/TC1/http_server/app_httpd.c b/TC1/http_server/app_httpd.c index c8bc840..26e7979 100644 --- a/TC1/http_server/app_httpd.c +++ b/TC1/http_server/app_httpd.c @@ -52,14 +52,10 @@ static bool is_http_init; static bool is_handlers_registered; const struct httpd_wsgi_call g_app_handlers[]; -char power_info_json[1952] = {0}; +char power_info_json[2560] = {0}; char up_time[16] = "00:00:00"; #define OTA_BUFFER_SIZE 1024 // 每次写入的缓存大小 -static CRC16_Context crc_context; -static uint32_t ota_offset = 0; -static int ota_file_size = 0; - /* void GetPraFromUrl(char* url, char* pra, char* val) { @@ -155,7 +151,7 @@ static int HttpGetAssets(httpd_request_t *req) { static int HttpGetTc1Status(httpd_request_t *req) { char *sockets = GetSocketStatus(); char *short_click_config = GetButtonClickConfig(); - char *tc1_status = malloc(1024); + char *tc1_status = malloc(2048); char *socket_names = malloc(512); sprintf(socket_names, "%s,%s,%s,%s,%s,%s", user_config->socket_names[0], @@ -246,43 +242,59 @@ static int HttpSetButtonEvent(httpd_request_t *req) { if (buf) free(buf); return err; } - +#define OTA_BUFFER_SIZE 1024 static int HttpSetOTAFile(httpd_request_t *req) { OSStatus err = kNoErr; + uint32_t total = 0, ota_offset = 0; + char *buffer = malloc(OTA_BUFFER_SIZE); + if (!buffer) return kGeneralErr; - char buffer[OTA_BUFFER_SIZE]; - uint32_t recv_len; +// mico_logic_partition_t* ota_partition = MicoFlashGetInfo(MICO_PARTITION_OTA_TEMP); +// MicoFlashErase(MICO_PARTITION_OTA_TEMP, 0x0, ota_partition->partition_length); - if (req->type != HTTPD_REQ_TYPE_POST) { - send_http("405 Method Not Allowed", 23, exit, &err); - } - /* 初始化 OTA */ + CRC16_Context crc_context; CRC16_Init(&crc_context); - ota_offset = 0; - /* 逐块接收并写入 Flash */ - while ((recv_len = httpd_get_data(req, buffer, OTA_BUFFER_SIZE)) > 0) { - CRC16_Update(&crc_context, buffer, recv_len); - MicoFlashWrite(MICO_PARTITION_OTA_TEMP, &ota_offset, buffer, recv_len); + tc1_log("開始接收 OTA 數據..."); + struct timeval timeout = {60, 0}; // 60秒 + setsockopt(req->sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); +int remaining = -1; + while (remaining!=0) { + int readSize = remaining>OTA_BUFFER_SIZE?OTA_BUFFER_SIZE:(remaining>0?remaining:OTA_BUFFER_SIZE); + remaining = httpd_get_data(req, buffer, readSize); + if (remaining < 0) { + err = kConnectionErr; + tc1_log("httpd_get_data 失敗"); + goto exit; + break; + } + + CRC16_Update(&crc_context, buffer, readSize); + +// err = MicoFlashWrite(MICO_PARTITION_OTA_TEMP, &crc_context->offset, (uint8_t *)buffer, readSize); +// require_noerr_quiet(err, exit); + total+=readSize; + mico_thread_msleep(10); } - /* 计算 CRC */ uint16_t crc16; CRC16_Final(&crc_context, &crc16); + char response[64]; - /* 切换到新固件 */ - OSStatus err = mico_ota_switch_to_new_fw(ota_file_size, crc16); - if (err == kNoErr) { - httpd_send(res, "200 OK: OTA Update Success! Rebooting...\n", 40); - /* 软重启 */ - mico_system_power_perform(mico_system_context_get(), eState_Software_Reset); - } else { - send_http("500 Internal Server Error: OTA Update Failed!\n", 50, exit, &err); - } + snprintf(response, sizeof(response), "OK, total: %ld bytes, CRC: 0x%04X", total, crc16); + send_http(response, strlen(response), exit, &err); + return 0; - exit: - if (buf) free(buf); + err = mico_ota_switch_to_new_fw(ota_offset, crc16); + require_noerr(err, exit); + + tc1_log("OTA 完成,重啟系統"); + mico_system_power_perform(mico_system_context_get(), eState_Software_Reset); + +exit: + if (buffer) free(buffer); + tc1_log("OTA 結束,狀態: %d", err); return err; } diff --git a/TC1/http_server/web/index.html b/TC1/http_server/web/index.html index 69ae3de..2185ebc 100644 --- a/TC1/http_server/web/index.html +++ b/TC1/http_server/web/index.html @@ -113,7 +113,7 @@ - 在线升级 + 固件升级 @@ -671,7 +671,6 @@
-
@@ -796,7 +795,7 @@ Config: {en: "Config", cn: "设置"}, TimedTask: {en: "Timed Task", cn: "定时任务"}, SystemStatus: {en: "System Status", cn: "系统状态"}, - OTA: {en: "OTA", cn: "在线升级"}, + OTA: {en: "OTA", cn: "固件升级"}, SystemLog: {en: "System Log", cn: "系统日志"}, About: {en: "About", cn: "关于"}, //主页 @@ -1259,6 +1258,7 @@ componentHandler.upgradeDom(); var w = document.getElementById("w"); var w_t = document.getElementById("w_t"); var w_y = document.getElementById("w_y"); + var powerTimerId; function GetPowerRecord() { HttpPost("/power", function (re) { @@ -1324,7 +1324,7 @@ componentHandler.upgradeDom(); switch_lables[i+3].MaterialSwitch.off(); } } - window.setTimeout(GetPowerRecord, 3000); + powerTimerId= window.setTimeout(GetPowerRecord, 3000); }, power_idx.toString()); } $(document).ready(function(){ @@ -1510,18 +1510,19 @@ HttpPost("/shortClickEvent", function (re) { } function OtaFileUpload() { + alert("假的假的是假的,忽略"); + return; var fileInput = document.getElementById("ota_file"); if (fileInput.files.length === 0) { alert("请选择要上传的 OTA 文件"); return; } - - var formData = new FormData(); - formData.append("ota_file", fileInput.files[0]); - - HttpPost("/ota/fileUpload", function(response) { - alert("OTA 上传结果: " + response); - }, formData); +clearTimeout(powerTimerId); +fetch("/ota/fileUpload", { + method: "POST", + headers: { "Content-Type": "application/octet-stream" }, + body: fileInput.files[0] +}).then(r => r.text()).then(alert).catch(alert); } var ota_status = document.querySelector('#ota_status'); diff --git a/TC1/mqtt_server/user_mqtt_client.c b/TC1/mqtt_server/user_mqtt_client.c index 6648802..00db224 100644 --- a/TC1/mqtt_server/user_mqtt_client.c +++ b/TC1/mqtt_server/user_mqtt_client.c @@ -530,7 +530,7 @@ void UserMqttHassAuto(char socket_id) { socket_id--; char *send_buf = NULL; char *topic_buf = NULL; - send_buf = (char *) malloc(600); + send_buf = (char *) malloc(800); topic_buf = (char *) malloc(64); if (send_buf != NULL && topic_buf != NULL) { sprintf(topic_buf, "homeassistant/switch/%s/socket_%d/config", str_mac, socket_id); @@ -691,6 +691,7 @@ void UserMqttHassAutoPower(void) { "\"object_id\":\"tc1_%s_sut\"," "\"state_topic\":\"homeassistant/sensor/%s/startupTime/state\"," "\"icon\":\"mdi:clock-time-three-outline\"," + "\"entity_category\":\"diagnostic\"," "\"value_template\":\"{{ value_json.startupTime }}\",""\"device\":{" "\"identifiers\":[\"tc1_%s\"]," "\"name\":\"%s\"," diff --git a/TC1/mqtt_server/user_mqtt_client.h b/TC1/mqtt_server/user_mqtt_client.h index f6e4fa6..29ade30 100644 --- a/TC1/mqtt_server/user_mqtt_client.h +++ b/TC1/mqtt_server/user_mqtt_client.h @@ -10,8 +10,8 @@ #define MQTT_CMD_TIMEOUT 5000 // 5s #define MQTT_YIELD_TMIE 5000 // 5s -#define MAX_MQTT_TOPIC_SIZE (256) -#define MAX_MQTT_DATA_SIZE (1024) +#define MAX_MQTT_TOPIC_SIZE (512) +#define MAX_MQTT_DATA_SIZE (2048) #define MAX_MQTT_SEND_QUEUE_SIZE (10) #define MQTT_SERVER user_config->mqtt_ip