From 004eaf28ddd9e05cda97bb85fdf4d8eb568fc29c Mon Sep 17 00:00:00 2001 From: nhkefus Date: Thu, 17 Apr 2025 17:39:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=8F=91=E6=9C=AC=E5=9C=B0ota?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E7=94=A8=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TC1/http_server/app_httpd.c | 58 +++++++++++++++++ TC1/http_server/web/index.html | 111 ++++++++++++++++++++++----------- TC1/main.c | 4 +- 3 files changed, 133 insertions(+), 40 deletions(-) diff --git a/TC1/http_server/app_httpd.c b/TC1/http_server/app_httpd.c index 1f65fc2..96cf2e9 100644 --- a/TC1/http_server/app_httpd.c +++ b/TC1/http_server/app_httpd.c @@ -255,6 +255,64 @@ static int HttpSetButtonEvent(httpd_request_t *req) { return err; } +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; + +// mico_logic_partition_t* ota_partition = MicoFlashGetInfo(MICO_PARTITION_OTA_TEMP); +// MicoFlashErase(MICO_PARTITION_OTA_TEMP, 0x0, ota_partition->partition_length); + + CRC16_Context crc_context; + CRC16_Init(&crc_context); + + 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); + } + + uint16_t crc16; +// CRC16_Final(&crc_context, &crc16); + char response[64]; + + snprintf(response, sizeof(response), "OK, total: %ld bytes, CRC: 0x%04X", total, crc16); + send_http(response, strlen(response), exit, &err); + return 0; + + 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 (req->sock >= 0) { + close(req->sock); + req->sock = -1; + } + if (buffer) free(buffer); + tc1_log("OTA 結束,狀態: %d", err); + return err; +} + static int HttpSetDeviceName(httpd_request_t *req) { OSStatus err = kNoErr; diff --git a/TC1/http_server/web/index.html b/TC1/http_server/web/index.html index 0a50590..9dcbb63 100644 --- a/TC1/http_server/web/index.html +++ b/TC1/http_server/web/index.html @@ -40,20 +40,20 @@ TC1智能插座
- - - - - - - - - - - - - - + + + + + + + + + + + + + +