尝试增加https下载ota文件的功能,增加后台直接上传ota bin文件来升级的功能

This commit is contained in:
nhkefus
2025-04-03 16:05:55 +08:00
parent e6d430c37c
commit 49847dc77c
3 changed files with 81 additions and 1 deletions

View File

@@ -54,6 +54,11 @@ static bool is_handlers_registered;
const struct httpd_wsgi_call g_app_handlers[]; const struct httpd_wsgi_call g_app_handlers[];
char power_info_json[1952] = {0}; char power_info_json[1952] = {0};
char up_time[16] = "00:00:00"; 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) void GetPraFromUrl(char* url, char* pra, char* val)
@@ -242,6 +247,45 @@ static int HttpSetButtonEvent(httpd_request_t *req) {
return err; return err;
} }
static int HttpSetOTAFile(httpd_request_t *req) {
OSStatus err = kNoErr;
char buffer[OTA_BUFFER_SIZE];
uint32_t recv_len;
if (req->type != HTTPD_REQ_TYPE_POST) {
send_http("405 Method Not Allowed", 23, exit, &err);
}
/* 初始化 OTA */
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);
}
/* 计算 CRC */
uint16_t crc16;
CRC16_Final(&crc_context, &crc16);
/* 切换到新固件 */
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);
}
exit:
if (buf) free(buf);
return err;
}
static int HttpSetDeviceName(httpd_request_t *req) { static int HttpSetDeviceName(httpd_request_t *req) {
OSStatus err = kNoErr; OSStatus err = kNoErr;
@@ -628,6 +672,7 @@ const struct httpd_wsgi_call g_app_handlers[] = {
{"/childLock", HTTPD_HDR_DEFORT, 0, NULL, HttpSetChildLock, NULL, NULL}, {"/childLock", HTTPD_HDR_DEFORT, 0, NULL, HttpSetChildLock, NULL, NULL},
{"/deviceName", HTTPD_HDR_DEFORT, 0, NULL, HttpSetDeviceName, NULL, NULL}, {"/deviceName", HTTPD_HDR_DEFORT, 0, NULL, HttpSetDeviceName, NULL, NULL},
{"/buttonEvents", HTTPD_HDR_DEFORT, 0, HttpGetButtonEvents, HttpSetButtonEvent, NULL, NULL}, {"/buttonEvents", HTTPD_HDR_DEFORT, 0, HttpGetButtonEvents, HttpSetButtonEvent, NULL, NULL},
{"/ota/fileUpload", HTTPD_HDR_DEFORT, 0, NULL, HttpSetOTAFile, NULL, NULL},
}; };
static int g_app_handlers_no = sizeof(g_app_handlers) / sizeof(struct httpd_wsgi_call); static int g_app_handlers_no = sizeof(g_app_handlers) / sizeof(struct httpd_wsgi_call);

View File

@@ -663,6 +663,25 @@
<div id="ota_status" class="mdl-progress mdl-js-progress"></div> <div id="ota_status" class="mdl-progress mdl-js-progress"></div>
</div> </div>
<div class="page page6 mdl-cell mdl-cell--12-col demo-card-square mdl-card mdl-shadow--2dp">
<div class="mdl-card__title mdl-card--expand">
<h2 class="mdl-card__title-text">本地升级</h2>
</div>
<div class="mdl-card__supporting-text">
<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>
<div class="mdl-card__actions mdl-card--border">
<a href="javascript:OtaFileUpload();" class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect">
上传 OTA 文件
</a>
</div>
</div>
<div class="page page7 mdl-cell mdl-cell--12-col demo-card-square mdl-card mdl-shadow--2dp"> <div class="page page7 mdl-cell mdl-cell--12-col demo-card-square mdl-card mdl-shadow--2dp">
<div class="mdl-card__title mdl-card--expand"> <div class="mdl-card__title mdl-card--expand">
<h2 class="mdl-card__title-text">系统日志</h2> <h2 class="mdl-card__title-text">系统日志</h2>
@@ -1489,6 +1508,22 @@ HttpPost("/shortClickEvent", function (re) {
OtaStatus(); OtaStatus();
}, ota_url); }, ota_url);
} }
function OtaFileUpload() {
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);
}
var ota_status = document.querySelector('#ota_status'); var ota_status = document.querySelector('#ota_status');
ota_status.addEventListener('mdl-componentupgraded', function() { ota_status.addEventListener('mdl-componentupgraded', function() {
this.MaterialProgress.setProgress(0); this.MaterialProgress.setProgress(0);

View File

@@ -32,7 +32,7 @@
#define __ota_server_H #define __ota_server_H
#define OTA_DEBUG (1) #define OTA_DEBUG (1)
#define OTA_USE_HTTPS (0) #define OTA_USE_HTTPS (1)
#define OTA_MD5_LENTH 32 #define OTA_MD5_LENTH 32
#define OTA_SEND_HEAD_SIZE 256 #define OTA_SEND_HEAD_SIZE 256