修复自定义名称过长可能导致系统崩溃的问题

This commit is contained in:
Your Name
2025-04-08 23:00:40 +08:00
parent fea21ab02a
commit 6e3a48cba6
4 changed files with 58 additions and 44 deletions

View File

@@ -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;
}

View File

@@ -113,7 +113,7 @@
<use xlink:href="#icon-cloud_download"/>
</svg>
</i>
<span class="lang" langKey="OTA">在线升级</span>
<span class="lang" langKey="OTA">固件升级</span>
</a>
<a class="mdl-navigation__link" href="javascript:ShowPage(7);">
<i class="mdl-color-text--blue-grey-400 material-icons" role="presentation">
@@ -671,7 +671,6 @@
<form id="otaForm" action="/ota/fileUpload" method="post" enctype="multipart/form-data">
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="file" id="ota_file" name="ota_file">
<label class="mdl-textfield__label" for="ota_file">选择 OTA 文件</label>
</div>
</form>
</div>
@@ -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');

View File

@@ -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\","

View File

@@ -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