test html

This commit is contained in:
ok-home
2023-09-24 11:28:09 +07:00
parent 9dc7641c60
commit 5c6932902e
10 changed files with 4202 additions and 62 deletions

View File

@@ -92,7 +92,7 @@
<script>
let otaData;
let otaSetchunksSize = 0;
let otaSetChunkSize = 0;
let otaStartsegment = 0;
let otaStarted = 0;
@@ -121,19 +121,20 @@
document.getElementById("otaStartCancel").addEventListener("click", function (e) {
if (otaData.length > 0 && otaStarted == 0) {
//socket.send(JSON.stringify({ name: "otasize", msg: otaData.length }));
console.log(JSON.stringify({ name: "otasize", msg: otaData.length }));
socket.send(JSON.stringify({ name: "otaSize", value: otaData.length }));
//console.log(JSON.stringify({ name: "otaSize", value: otaData.length }));
otaStarted = 1;
this.innerHTML = "Click to Cancel";
document.getElementById("otaFileSelect").disabled = true;
document.getElementById("otaProgressVisible").style.display = "block";
document.getElementById("otaPogress").max = otaData.length;
tstReceive();
//tstReceive();
}
else {
otaStarted = 0;
receiveWsData(JSON.stringify({ name: "otaCancel", msg: "Cancel" }));
//receiveWsData(JSON.stringify({ name: "otaCancel", value: "Cancel" }));
socket.send(JSON.stringify({ name: "otaCancel", value: "Cancel" }));
}
});
@@ -143,8 +144,8 @@
window.location.href = '/';
});
document.getElementById("otaReStart").addEventListener("click", function (e) {
//socket.send(JSON.stringify({ name: "otarestartesp", msg: "restart" }));
console.log(JSON.stringify({ name: "otarestartesp", msg: "restart" }));
socket.send(JSON.stringify({ name: "otaRestartEsp", value: "restart" }));
//console.log(JSON.stringify({ name: "otaRestartEsp", value: "restart" }));
});
//
@@ -152,18 +153,18 @@
return new Promise(resolve => setTimeout(resolve, ms));
}
async function tstReceive() {
receiveWsData(JSON.stringify({ name: "otaSetchunksSize", msg: 1024 }));
while (otaStartsegment + otaSetchunksSize <= otaData.length && otaStarted == 1) {
receiveWsData(JSON.stringify({ name: "otaSetChunkSize", value: 1024 }));
while (otaStartsegment + otaSetChunkSize <= otaData.length && otaStarted == 1) {
await sleep(1000);
if(otaStarted == 1){
receiveWsData(JSON.stringify({ name: "otaGetChunk", msg: otaStartsegment }));
receiveWsData(JSON.stringify({ name: "otaGetChunk", value: otaStartsegment }));
}
otaStartsegment += otaSetchunksSize;
otaStartsegment += otaSetChunkSize;
}
//console.log(otaStartsegment + " " + otaSetchunksSize + " " + otaData.length + " " + (otaData.length - otaStartsegment));
//console.log(otaStartsegment + " " + otaSetChunkSize + " " + otaData.length + " " + (otaData.length - otaStartsegment));
if (otaStarted == 1) {
receiveWsData(JSON.stringify({ name: "otaGetChunk", msg: otaStartsegment }));
receiveWsData(JSON.stringify({ name: "otaEnd", msg: "OK" }));
receiveWsData(JSON.stringify({ name: "otaGetChunk", value: otaStartsegment }));
receiveWsData(JSON.stringify({ name: "otaEnd", value: "OK" }));
}
}
//
@@ -172,15 +173,15 @@
let obj = JSON.parse(data);
console.log(data);
switch (obj.name) {
case "otaSetchunksSize":
otaSetchunksSize = obj.msg;
case "otaSetChunkSize":
otaSetChunkSize = obj.value;
break;
case "otaGetChunk":
let otaDataSend = otaData.subarray(obj.msg, obj.msg + otaSetchunksSize);
document.getElementById("otaPogress").value = obj.msg;
document.getElementById("otaStartCancel").innerHTML = "Ota Transfer. Size = " + otaData.length + " Segment = " + obj.msg + " Click to Cancel";
console.log("sock send " + obj.msg + " " + otaDataSend.length);
//socket.send(otaDataSend);
let otaDataSend = otaData.subarray(obj.value, obj.value + otaSetChunkSize);
document.getElementById("otaPogress").value = obj.value;
document.getElementById("otaStartCancel").innerHTML = "Ota Transfer. Size = " + otaData.length + " Segment = " + obj.value + " Click to Cancel";
console.log("sock send " + obj.value + " " + otaDataSend.length);
socket.send(otaDataSend);
break;
case "otaEnd":
otaStartsegment = 0;
@@ -202,7 +203,7 @@
document.getElementById("otaPogress").value = otaData.length;
document.getElementById("otaFileSelect").disabled = false;
document.getElementById("otaReStartVisible").style.display = "block";
document.getElementById("otaReStart").innerHTML = "Transfer Cancel " + obj.msg;
document.getElementById("otaReStart").innerHTML = "Transfer Cancel " + obj.value;
document.getElementById("otaReStart").disabled = true;
}
}

View File

@@ -2,8 +2,11 @@
#include "esp_ota_ops.h"
#include "esp_flash_partitions.h"
#include "esp_partition.h"
#include "esp_image_format.h"
static const char *TAG = "ota_esp";
#include "ota_ws_private.h"
static const char *TAG = "ota_ws_esp";
/*an ota data write buffer ready to write to the flash*/
//static char ota_write_data[BUFFSIZE + 1] = {0};
static const esp_partition_t *update_partition = NULL;
@@ -11,9 +14,9 @@ static bool image_header_was_checked = false;
static int binary_file_length = 0;
static esp_ota_handle_t update_handle = 0;
esp_err_t start_ota(void)
esp_err_t start_ota_ws(void)
{
// return ESP_OK; // debug return
return ESP_OK; // debug return
esp_err_t err;
ESP_LOGI(TAG, "Starting OTA");
@@ -23,22 +26,22 @@ esp_err_t start_ota(void)
if (configured != running)
{
ESP_LOGW(TAG, "Configured OTA boot partition at offset 0x%08x, but running from offset 0x%08x",
ESP_LOGW(TAG, "Configured OTA boot partition at offset 0x%08lx, but running from offset 0x%08lx",
configured->address, running->address);
ESP_LOGW(TAG, "(This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)");
}
ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)",
ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08lx)",
running->type, running->subtype, running->address);
update_partition = esp_ota_get_next_update_partition(NULL);
ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x",
ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%lx",
update_partition->subtype, update_partition->address);
err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle);
if (err != ESP_OK)
{
ESP_LOGI(TAG, "esp_ota_begin failed ");
return -1;
ESP_LOGE(TAG, "esp_ota_begin failed ");
return ESP_FAIL;
}
ESP_LOGI(TAG, "esp_ota_begin succeeded");
@@ -46,9 +49,10 @@ esp_err_t start_ota(void)
return ESP_OK;
}
esp_err_t write_ota(int data_read, uint8_t *ota_write_data)
esp_err_t write_ota_ws(int data_read, uint8_t *ota_write_data)
{
// return data_read; // debug return
return ESP_OK; // debug return
if (image_header_was_checked == false) // first segment
{
@@ -64,33 +68,35 @@ esp_err_t write_ota(int data_read, uint8_t *ota_write_data)
else
{
ESP_LOGE(TAG, "received package is not fit len");
return -1;
return ESP_FAIL;
}
}
esp_err_t err = esp_ota_write(update_handle, (const void *)ota_write_data, data_read);
if (err != ESP_OK)
{
return -1;
return ESP_FAIL;
}
binary_file_length += data_read;
ESP_LOGD(TAG, "Written image length %d", binary_file_length);
return ESP_OK;
}
esp_err_t end_ota(void)
esp_err_t end_ota_ws(void)
{
return ESP_OK; // debug return
esp_err_t err = esp_ota_end(update_handle);
if (err != ESP_OK) {
if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
ESP_LOGE(TAG, "Image validation failed, image is corrupted");
}
ESP_LOGE(TAG, "esp_ota_end failed (%s)!", esp_err_to_name(err));
return -1;
return ESP_FAIL;
}
err = esp_ota_set_boot_partition(update_partition);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err));
return -1;
return ESP_FAIL;
}
return ESP_OK;
}

View File

@@ -37,24 +37,29 @@ static esp_err_t json_to_str_parm(char *jsonstr, char *nameStr, char *valStr) //
valStr[0] = 0;
return ESP_OK;
}
static void send_json_string(char *str, httpd_req_t *req)
static esp_err_t send_json_string(char *str, httpd_req_t *req)
{
httpd_ws_frame_t ws_pkt;
memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));
ws_pkt.type = HTTPD_WS_TYPE_TEXT;
ws_pkt.payload = (uint8_t *)str;
ws_pkt.len = strlen(str);
httpd_ws_send_frame(req, &ws_pkt);
return httpd_ws_send_frame(req, &ws_pkt);
}
// write wifi data from ws to nvs
static esp_err_t ota_ws_handler(httpd_req_t *req)
{
static int ota_size;
static int ota_start_chunk;
if (req->method == HTTP_GET)
{
ESP_LOGI(TAG, "Handshake done, the new connection was opened");
send_nvs_data(req); // read & send initial wifi data from nvs
return ESP_OK;
}
char json_key[64]={0};
char json_value[64]={0};
char json_str[64] = {0};
httpd_ws_frame_t ws_pkt;
uint8_t *buf = NULL;
memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));
@@ -87,11 +92,41 @@ static esp_err_t ota_ws_handler(httpd_req_t *req)
ret = ESP_OK;
if (ws_pkt.type == HTTPD_WS_TYPE_TEXT)
{
// json data
ESP_LOGI(TAG, "Rcv txt msg len=%d data=%s", ws_pkt.len, buf);
if (json_to_str_parm((char*)buf,json_key,json_value))
{
ESP_LOGE(TAG,"error json str: %s",buf);
goto _recv_ret;
}
if(strcmp(json_key,OTA_SIZE_START)==0)// start ota
{
ota_size = atoi(json_value);
ota_start_chunk = 0;
snprintf(json_str, sizeof(json_str), "{\"name\":\"%s\",\"value\":%d}", OTA_SET_CHUNK_SIZE, OTA_CHUNK_SIZE);
send_json_string(json_str,req);
snprintf(json_str, sizeof(json_str), "{\"name\":\"%s\",\"value\":%d}", OTA_GET_CHUNK, ota_start_chunk);
send_json_string(json_str,req);
}
}
else if (ws_pkt.type == HTTPD_WS_TYPE_BIN)
else if (ws_pkt.type == HTTPD_WS_TYPE_BINARY)
{
// ota data
ESP_LOGI(TAG, "Rcv bin msg len=%d", ws_pkt.len);
ota_start_chunk += ws_pkt.len;
if(ota_start_chunk < ota_size)
{
snprintf(json_str, sizeof(json_str), "{\"name\":\"%s\",\"value\": %d }", OTA_GET_CHUNK, ota_start_chunk);
send_json_string(json_str,req);
}
else{
// end ota
ota_size = 0;
ota_start_chunk = 0;
snprintf(json_str, sizeof(json_str), "{\"name\":\"%s\",\"value\":\"%s\" }", OTA_END, "OK");
send_json_string(json_str,req);
}
}
else
{