修复在不配置MQTT信息的情况下,OTA将大概率无法进行,且后台可能经常卡死刷不出页面的问题

This commit is contained in:
Your Name
2025-04-16 23:16:01 +08:00
parent 5623b632c7
commit 026e058934
5 changed files with 49 additions and 17 deletions

View File

@@ -54,6 +54,7 @@ static bool is_handlers_registered;
const struct httpd_wsgi_call g_app_handlers[];
char power_info_json[2560] = {0};
char up_time[16] = "00:00:00";
#define CHUNK_SIZE 512 // 每次发送 512 字节,避免 buffer 太大
/*
void GetPraFromUrl(char* url, char* pra, char* val)
@@ -90,16 +91,30 @@ void GetPraFromUrl(char* url, char* pra, char* val)
}
*/
static OSStatus send_in_chunks(int sock, const uint8_t *data, int total_len) {
OSStatus err = kNoErr;
for (int offset = 0; offset < total_len; offset += CHUNK_SIZE) {
int chunk_len = (total_len - offset > CHUNK_SIZE) ? CHUNK_SIZE : (total_len - offset);
err = httpd_send_body(sock, data + offset, chunk_len);
require_noerr_action(err, exit, http_log("ERROR: Send chunk failed at offset %d", offset));
}
exit:
return err;
}
static int HttpGetIndexPage(httpd_request_t *req) {
OSStatus err = kNoErr;
int total_sz = sizeof(web_index_html);
err = httpd_send_all_header(req, HTTP_RES_200, sizeof(web_index_html), HTTP_CONTENT_HTML_ZIP);
require_noerr_action(err, exit, http_log("ERROR: Unable to send http index headers."));
err = httpd_send_body(req->sock, web_index_html, sizeof(web_index_html));
require_noerr_action(err, exit, http_log("ERROR: Unable to send http index body."));
err = httpd_send_all_header(req, HTTP_RES_200, total_sz, HTTP_CONTENT_HTML_ZIP);
require_noerr_action(err, exit, http_log("ERROR: Unable to send index headers."));
exit:
err = send_in_chunks(req->sock, web_index_html, total_sz);
require_noerr_action(err, exit, http_log("ERROR: Unable to send index body."));
exit:
return err;
}
@@ -107,14 +122,15 @@ static int HttpGetAssets(httpd_request_t *req) {
OSStatus err = kNoErr;
char *file_name = strstr(req->filename, "/assets/");
if (!file_name) { http_log("HttpGetAssets url[%s] err", req->filename);
if (!file_name) {
http_log("HttpGetAssets url[%s] err", req->filename);
return err;
}
//http_log("HttpGetAssets url[%s] file_name[%s]", req->filename, file_name);
int total_sz = 0;
const unsigned char *file_data = NULL;
const char *content_type = HTTP_CONTENT_JS_ZIP;
if (strcmp(file_name + 8, "js_pack.js") == 0) {
total_sz = sizeof(js_pack);
file_data = js_pack;
@@ -122,17 +138,25 @@ static int HttpGetAssets(httpd_request_t *req) {
total_sz = sizeof(css_pack);
file_data = css_pack;
content_type = HTTP_CONTENT_CSS_ZIP;
} else if (strcmp(file_name + 8, "index.html") == 0) {
total_sz = sizeof(web_index_html);
file_data = web_index_html;
content_type = HTTP_CONTENT_HTML_ZIP;
}
if (total_sz == 0 || file_data == NULL) {
http_log("File not found: %s", req->filename);
return err;
}
if (total_sz == 0) return err;
err = httpd_send_all_header(req, HTTP_RES_200, total_sz, content_type);
require_noerr_action(err, exit, http_log("ERROR: Unable to send http assets headers."));
require_noerr_action(err, exit, http_log("ERROR: Unable to send asset headers."));
err = httpd_send_body(req->sock, file_data, total_sz);
require_noerr_action(err, exit, http_log("ERROR: Unable to send http assets body."));
err = send_in_chunks(req->sock, file_data, total_sz);
require_noerr_action(err, exit, http_log("ERROR: Unable to send asset body."));
exit:
exit:
return err;
}
@@ -392,7 +416,10 @@ static int HttpSetMqttConfig(httpd_request_t *req) {
sscanf(buf, "%s %d %s %s", MQTT_SERVER, &MQTT_SERVER_PORT, MQTT_SERVER_USR, MQTT_SERVER_PWD);
mico_system_context_update(sys_config);
if (!(MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1)){
err = UserMqttInit();
require_noerr(err, exit);
}
send_http("OK", 2, exit, &err);
exit: