mirror of
https://github.com/ok-home/ota_ws_update.git
synced 2025-11-13 22:03:27 +03:00
reorder to ota_ws_update
This commit is contained in:
@@ -1,10 +1,15 @@
|
|||||||
# The following lines of boilerplate have to be in your project's CMakeLists
|
idf_component_register(
|
||||||
# in this exact order for cmake to work correctly
|
SRCS
|
||||||
cmake_minimum_required(VERSION 3.16)
|
source/ota_ws_update_esp.c
|
||||||
|
source/ota_ws_update_http.c
|
||||||
# (Not part of the boilerplate)
|
INCLUDE_DIRS
|
||||||
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
|
include
|
||||||
#set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
|
PRIV_INCLUDE_DIRS
|
||||||
|
private_include
|
||||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
REQUIRES
|
||||||
project(ota_ws)
|
esp_http_server
|
||||||
|
app_update
|
||||||
|
#esp_wifi
|
||||||
|
EMBED_FILES
|
||||||
|
source/ota_ws_update.html
|
||||||
|
)
|
||||||
21
Kconfig.projbuild
Normal file
21
Kconfig.projbuild
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
menu "OTA WS UPDATE"
|
||||||
|
|
||||||
|
config OTA_DEFAULT_URI
|
||||||
|
string "OTA page URI"
|
||||||
|
default "/ota"
|
||||||
|
help
|
||||||
|
WEB page URI to OTA update.
|
||||||
|
|
||||||
|
config OTA_DEFAULT_WS_URI
|
||||||
|
string "OTA ws URI"
|
||||||
|
default "/ota/ws"
|
||||||
|
help
|
||||||
|
WEB ws URI to OTA update.
|
||||||
|
|
||||||
|
config OTA_CHUNK_SIZE
|
||||||
|
int "Ota chunk size"
|
||||||
|
default 8192
|
||||||
|
help
|
||||||
|
Ota download chunk size.
|
||||||
|
|
||||||
|
endmenu
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
dependencies:
|
|
||||||
idf:
|
|
||||||
component_hash: null
|
|
||||||
source:
|
|
||||||
type: idf
|
|
||||||
version: 5.2.0
|
|
||||||
manifest_hash: 2ac596d0d5a708817329b21531cbf3574c7c8daaf36dca979b5dd32a78fc2857
|
|
||||||
target: esp32s3
|
|
||||||
version: 1.0.0
|
|
||||||
12
example_ota_ws/CMakeLists.txt
Normal file
12
example_ota_ws/CMakeLists.txt
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
# The following lines of boilerplate have to be in your project's CMakeLists
|
||||||
|
# in this exact order for cmake to work correctly
|
||||||
|
cmake_minimum_required(VERSION 3.16)
|
||||||
|
|
||||||
|
# (Not part of the boilerplate)
|
||||||
|
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
|
||||||
|
#set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
|
||||||
|
|
||||||
|
set(EXTRA_COMPONENT_DIRS ../)
|
||||||
|
|
||||||
|
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||||
|
project(example_ota_ws_update)
|
||||||
12
example_ota_ws/main/CMakeLists.txt
Normal file
12
example_ota_ws/main/CMakeLists.txt
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
# Embed the server root certificate into the final binary
|
||||||
|
|
||||||
|
idf_component_register(
|
||||||
|
SRCS
|
||||||
|
example_ota_ws_update.c
|
||||||
|
REQUIRES
|
||||||
|
ota_ws_update
|
||||||
|
prv_Wifi_Connect
|
||||||
|
esp_http_server
|
||||||
|
mdns
|
||||||
|
esp_wifi
|
||||||
|
)
|
||||||
@@ -17,9 +17,9 @@
|
|||||||
// #include <esp_log.h>
|
// #include <esp_log.h>
|
||||||
|
|
||||||
#include "prv_wifi_connect.h"
|
#include "prv_wifi_connect.h"
|
||||||
#include "ota_ws.h"
|
#include "ota_ws_update.h"
|
||||||
|
|
||||||
static const char *TAG = "ota_ws";
|
//static const char *TAG = "ota_ws";
|
||||||
|
|
||||||
#define MDNS
|
#define MDNS
|
||||||
#ifdef MDNS
|
#ifdef MDNS
|
||||||
@@ -54,5 +54,4 @@ void app_main(void)
|
|||||||
#endif // MDNS
|
#endif // MDNS
|
||||||
|
|
||||||
prv_start_http_server(PRV_MODE_STAY_ACTIVE, ota_ws_register_uri_handler); // run server
|
prv_start_http_server(PRV_MODE_STAY_ACTIVE, ota_ws_register_uri_handler); // run server
|
||||||
// example_echo_ws_server();
|
|
||||||
}
|
}
|
||||||
@@ -7,10 +7,9 @@ extern "C"
|
|||||||
{
|
{
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @brief register provision handlers ( web page & ws handlers) on existing httpd server with ws support
|
* @brief register ota_ws httpd handlers ( web page & ws handlers) on existing httpd server with ws support
|
||||||
* uri page -> CONFIG_DEFAULT_URI
|
* uri page -> CONFIG_OTA_DEFAULT_WS_URI
|
||||||
* @param httpd_handle_t server -> existing server handle
|
* @param httpd_handle_t server -> existing server handle
|
||||||
* @return
|
* @return
|
||||||
* ESP_OK -> register OK
|
* ESP_OK -> register OK
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
# Embed the server root certificate into the final binary
|
|
||||||
|
|
||||||
idf_component_register(
|
|
||||||
SRCS
|
|
||||||
example_ota_ws.c
|
|
||||||
../source/ota_ws_esp.c
|
|
||||||
../source/ota_ws_http.c
|
|
||||||
INCLUDE_DIRS
|
|
||||||
../include
|
|
||||||
PRIV_INCLUDE_DIRS
|
|
||||||
../private_include
|
|
||||||
REQUIRES
|
|
||||||
prv_Wifi_Connect
|
|
||||||
esp_http_server
|
|
||||||
mdns
|
|
||||||
app_update
|
|
||||||
esp_wifi
|
|
||||||
EMBED_FILES
|
|
||||||
../source/ota_ws.html
|
|
||||||
)
|
|
||||||
@@ -14,7 +14,6 @@
|
|||||||
#define OTA_CHECK_ROLLBACK "otaCheckRollback"
|
#define OTA_CHECK_ROLLBACK "otaCheckRollback"
|
||||||
#define OTA_PROCESS_ROLLBACK "otaProcessRollback"
|
#define OTA_PROCESS_ROLLBACK "otaProcessRollback"
|
||||||
|
|
||||||
#define OTA_CHUNK_SIZE 4096*2
|
|
||||||
|
|
||||||
esp_err_t start_ota_ws(void);
|
esp_err_t start_ota_ws(void);
|
||||||
esp_err_t write_ota_ws(int data_read, uint8_t *ota_write_data);
|
esp_err_t write_ota_ws(int data_read, uint8_t *ota_write_data);
|
||||||
2030
sdkconfig.old
2030
sdkconfig.old
File diff suppressed because it is too large
Load Diff
@@ -3,7 +3,7 @@
|
|||||||
<html lang="ru">
|
<html lang="ru">
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<title>WiFi connect</title>
|
<title>ОTA UPDATE</title>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
|
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
|
||||||
<style>
|
<style>
|
||||||
@@ -64,22 +64,22 @@
|
|||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<div class="hdr">OTA</div>
|
<div class="hdr">OTA UPDATE</div>
|
||||||
|
|
||||||
<div class="column">
|
<div class="column">
|
||||||
<button class="btn" id="goHome">На главную</button>
|
<button class="btn" id="goHome">Home Page</button>
|
||||||
</div>
|
</div>
|
||||||
<div id="rollback" style="display:none">
|
<div id="rollback" style="display:none">
|
||||||
<div class="column" >
|
<div class="column" >
|
||||||
<button class="btn" id="otaVerifyApp">Ota APP veryfyed</button>
|
<button class="btn" id="otaVerifyApp">Click to confirm and commit OTA update</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="column">
|
<div class="column">
|
||||||
<button class="btn" id="otaRollback">Ota APP rollback and restart</button>
|
<button class="btn" id="otaRollback">Cancel OTA. Click to rollback update and restart</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div id="update" style="display:block">
|
<div id="update" style="display:block">
|
||||||
<div class="cl1" style="display:none">
|
<div class="cl1" style="display:none">
|
||||||
<label class="cl01" for="otaFile">Ota File Name</label>
|
<label class="cl01" for="otaFile">Select the new OTA firmware file</label>
|
||||||
<input class="cl02" type="file" id="otaFile" placeholder="select file" onchange="readOtaFile(this)">
|
<input class="cl02" type="file" id="otaFile" placeholder="select file" onchange="readOtaFile(this)">
|
||||||
</div>
|
</div>
|
||||||
<div class="column" style="display:block" id="otaFileSelectVisible">
|
<div class="column" style="display:block" id="otaFileSelectVisible">
|
||||||
@@ -88,10 +88,10 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="column" style="display:none" id="otaStartVisible">
|
<div class="column" style="display:none" id="otaStartVisible">
|
||||||
<button class="btn" id="otaStartCancel">Ota Start</button>
|
<button class="btn" id="otaStartCancel">Start OTA update</button>
|
||||||
</div>
|
</div>
|
||||||
<div class="column" style="display:none" id="otaReStartVisible">
|
<div class="column" style="display:none" id="otaReStartVisible">
|
||||||
<button class="btn" id="otaReStart">Ota ReStart</button>
|
<button class="btn" id="otaReStart">Reboot with new OTA firmware</button>
|
||||||
</div>
|
</div>
|
||||||
<div id="otaProgressVisible" style="display:none">
|
<div id="otaProgressVisible" style="display:none">
|
||||||
<div class="cl1">
|
<div class="cl1">
|
||||||
@@ -109,7 +109,7 @@
|
|||||||
function readOtaFile(input) {
|
function readOtaFile(input) {
|
||||||
let reader = new FileReader();
|
let reader = new FileReader();
|
||||||
let file = input.files[0];
|
let file = input.files[0];
|
||||||
document.getElementById('otaFileSelect').innerHTML = "Selected file: " + file.name;
|
document.getElementById('otaFileSelect').innerHTML = "Selected firmware file: " + file.name;
|
||||||
reader.readAsArrayBuffer(file);
|
reader.readAsArrayBuffer(file);
|
||||||
input.value = null;
|
input.value = null;
|
||||||
|
|
||||||
@@ -158,7 +158,7 @@
|
|||||||
//console.log(JSON.stringify({ name: "otaRestartEsp", value: "restart" }));
|
//console.log(JSON.stringify({ name: "otaRestartEsp", value: "restart" }));
|
||||||
});
|
});
|
||||||
|
|
||||||
//
|
/*
|
||||||
function sleep(ms) {
|
function sleep(ms) {
|
||||||
return new Promise(resolve => setTimeout(resolve, ms));
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
}
|
}
|
||||||
@@ -177,7 +177,7 @@
|
|||||||
receiveWsData(JSON.stringify({ name: "otaEnd", value: "OK" }));
|
receiveWsData(JSON.stringify({ name: "otaEnd", value: "OK" }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//
|
*/
|
||||||
function receiveWsData(data) {
|
function receiveWsData(data) {
|
||||||
try {
|
try {
|
||||||
let obj = JSON.parse(data);
|
let obj = JSON.parse(data);
|
||||||
@@ -189,7 +189,7 @@
|
|||||||
case "otaGetChunk":
|
case "otaGetChunk":
|
||||||
let otaDataSend = otaData.subarray(obj.value, obj.value + otaSetChunkSize);
|
let otaDataSend = otaData.subarray(obj.value, obj.value + otaSetChunkSize);
|
||||||
document.getElementById("otaPogress").value = obj.value;
|
document.getElementById("otaPogress").value = obj.value;
|
||||||
document.getElementById("otaStartCancel").innerHTML = "Ota Transfer. Size = " + otaData.length + " Segment = " + obj.value + " Click to Cancel";
|
document.getElementById("otaStartCancel").innerHTML = "Ota download. Size = " + otaData.length + " Segment = " + obj.value + " Click to Cancel";
|
||||||
//console.log("sock send " + obj.value + " " + otaDataSend.length);
|
//console.log("sock send " + obj.value + " " + otaDataSend.length);
|
||||||
socket.send(otaDataSend);
|
socket.send(otaDataSend);
|
||||||
break;
|
break;
|
||||||
@@ -197,11 +197,11 @@
|
|||||||
otaStartsegment = 0;
|
otaStartsegment = 0;
|
||||||
otaStarted = 0;
|
otaStarted = 0;
|
||||||
document.getElementById("otaStartVisible").style.display = "none";
|
document.getElementById("otaStartVisible").style.display = "none";
|
||||||
document.getElementById("otaStartCancel").innerHTML = "Ota Start";
|
document.getElementById("otaStartCancel").innerHTML = "Start OTA update";
|
||||||
document.getElementById("otaPogress").value = otaData.length;
|
document.getElementById("otaPogress").value = otaData.length;
|
||||||
document.getElementById("otaFileSelect").disabled = false;
|
document.getElementById("otaFileSelect").disabled = false;
|
||||||
document.getElementById("otaReStartVisible").style.display = "block";
|
document.getElementById("otaReStartVisible").style.display = "block";
|
||||||
document.getElementById("otaReStart").innerHTML = "Transfer Done Click to restart ESP";
|
document.getElementById("otaReStart").innerHTML = "The firmware is loaded. Click to reboot with new OTA firmware";
|
||||||
document.getElementById("otaReStart").disabled = false;
|
document.getElementById("otaReStart").disabled = false;
|
||||||
break;
|
break;
|
||||||
case "otaError":
|
case "otaError":
|
||||||
@@ -209,11 +209,11 @@
|
|||||||
otaStartsegment = 0;
|
otaStartsegment = 0;
|
||||||
otaStarted = 0;
|
otaStarted = 0;
|
||||||
document.getElementById("otaStartVisible").style.display = "none";
|
document.getElementById("otaStartVisible").style.display = "none";
|
||||||
document.getElementById("otaStartCancel").innerHTML = "Ota Start";
|
document.getElementById("otaStartCancel").innerHTML = "Start OTA update";
|
||||||
document.getElementById("otaPogress").value = otaData.length;
|
document.getElementById("otaPogress").value = otaData.length;
|
||||||
document.getElementById("otaFileSelect").disabled = false;
|
document.getElementById("otaFileSelect").disabled = false;
|
||||||
document.getElementById("otaReStartVisible").style.display = "block";
|
document.getElementById("otaReStartVisible").style.display = "block";
|
||||||
document.getElementById("otaReStart").innerHTML = "Transfer Cancel " + obj.value;
|
document.getElementById("otaReStart").innerHTML = "ОТА firmware download canceled " + obj.value;
|
||||||
document.getElementById("otaReStart").disabled = true;
|
document.getElementById("otaReStart").disabled = true;
|
||||||
break;
|
break;
|
||||||
case "otaCheckRollback":
|
case "otaCheckRollback":
|
||||||
@@ -262,6 +262,7 @@
|
|||||||
console.log("close");
|
console.log("close");
|
||||||
};
|
};
|
||||||
socket.onerror = function () {
|
socket.onerror = function () {
|
||||||
|
document.location.reload();
|
||||||
console.log("error");
|
console.log("error");
|
||||||
};
|
};
|
||||||
socket.onmessage = function (event) {
|
socket.onmessage = function (event) {
|
||||||
@@ -1,4 +1,10 @@
|
|||||||
|
/*
|
||||||
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, this
|
||||||
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||||
|
CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
*/
|
||||||
#include "esp_ota_ops.h"
|
#include "esp_ota_ops.h"
|
||||||
#include "esp_flash_partitions.h"
|
#include "esp_flash_partitions.h"
|
||||||
#include "esp_partition.h"
|
#include "esp_partition.h"
|
||||||
@@ -7,11 +13,9 @@
|
|||||||
#include "ota_ws_private.h"
|
#include "ota_ws_private.h"
|
||||||
|
|
||||||
static const char *TAG = "ota_ws_esp";
|
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;
|
static const esp_partition_t *update_partition = NULL;
|
||||||
static bool image_header_was_checked = false;
|
static bool image_header_was_checked = false;
|
||||||
static int binary_file_length = 0;
|
|
||||||
static esp_ota_handle_t update_handle = 0;
|
static esp_ota_handle_t update_handle = 0;
|
||||||
|
|
||||||
esp_err_t start_ota_ws(void)
|
esp_err_t start_ota_ws(void)
|
||||||
@@ -48,7 +52,6 @@ esp_err_t start_ota_ws(void)
|
|||||||
image_header_was_checked = false;
|
image_header_was_checked = false;
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t write_ota_ws(int data_read, uint8_t *ota_write_data)
|
esp_err_t write_ota_ws(int data_read, uint8_t *ota_write_data)
|
||||||
{
|
{
|
||||||
//return ESP_OK; // debug return
|
//return ESP_OK; // debug return
|
||||||
@@ -67,7 +70,7 @@ esp_err_t write_ota_ws(int data_read, uint8_t *ota_write_data)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ESP_LOGE(TAG, "received package is not fit len");
|
ESP_LOGE(TAG, "Received package is not fit len");
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -76,11 +79,8 @@ esp_err_t write_ota_ws(int data_read, uint8_t *ota_write_data)
|
|||||||
{
|
{
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
binary_file_length += data_read;
|
|
||||||
ESP_LOGD(TAG, "Written image length %d", binary_file_length);
|
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t end_ota_ws(void)
|
esp_err_t end_ota_ws(void)
|
||||||
{
|
{
|
||||||
//return ESP_OK; // debug return
|
//return ESP_OK; // debug return
|
||||||
@@ -121,7 +121,7 @@ bool check_ota_ws_rollback_enable(void)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// rollback == true - rollback
|
// rollback == true - rollback
|
||||||
// rollback == false - app valid? no rollback
|
// rollback == false - app valid? confirm update -> no rollback
|
||||||
esp_err_t rollback_ota_ws(bool rollback)
|
esp_err_t rollback_ota_ws(bool rollback)
|
||||||
{
|
{
|
||||||
#ifdef CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE
|
#ifdef CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE
|
||||||
@@ -1,3 +1,10 @@
|
|||||||
|
/*
|
||||||
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, this
|
||||||
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||||
|
CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
*/
|
||||||
#include "ota_ws_private.h"
|
#include "ota_ws_private.h"
|
||||||
#include "ota_ws.h"
|
#include "ota_ws.h"
|
||||||
|
|
||||||
@@ -5,21 +12,28 @@
|
|||||||
#include "freertos/queue.h"
|
#include "freertos/queue.h"
|
||||||
|
|
||||||
#include "jsmn.h"
|
#include "jsmn.h"
|
||||||
|
/*
|
||||||
#define OTA_DEFAULT_WS_URI "/ws"
|
#define OTA_DEFAULT_WS_URI "/ws"
|
||||||
#define OTA_DEFAULT_URI "/"
|
#define OTA_DEFAULT_URI "/"
|
||||||
|
#define OTA_CHUNK_SIZE 4096*2
|
||||||
|
*/
|
||||||
|
#define OTA_DEFAULT_WS_URI CONFIG_OTA_DEFAULT_WS_URI
|
||||||
|
#define OTA_DEFAULT_URI CONFIG_OTA_DEFAULT_URI
|
||||||
|
#define OTA_CHUNK_SIZE CONFIG_OTA_CHUNK_SIZE
|
||||||
|
|
||||||
static const char *TAG = "ota_ws";
|
|
||||||
|
|
||||||
static int ota_size;
|
static const char *TAG = "ota_ws_http";
|
||||||
static int ota_start_chunk;
|
|
||||||
static int ota_started;
|
static int ota_size; // ota firmware size
|
||||||
|
static int ota_start_chunk; // start address of http chunk
|
||||||
|
static int ota_started; // ota download started
|
||||||
|
|
||||||
static esp_err_t json_to_str_parm(char *jsonstr, char *nameStr, char *valStr);
|
static esp_err_t json_to_str_parm(char *jsonstr, char *nameStr, char *valStr);
|
||||||
static esp_err_t send_json_string(char *str, httpd_req_t *req);
|
static esp_err_t send_json_string(char *str, httpd_req_t *req);
|
||||||
static esp_err_t ota_ws_handler(httpd_req_t *req);
|
static esp_err_t ota_ws_handler(httpd_req_t *req);
|
||||||
static void ota_error(httpd_req_t *req, char *code, char *msg);
|
static void ota_error(httpd_req_t *req, char *code, char *msg);
|
||||||
|
|
||||||
|
// abort OTA, send error/cancel msg to ws
|
||||||
static void ota_error(httpd_req_t *req, char *code, char *msg)
|
static void ota_error(httpd_req_t *req, char *code, char *msg)
|
||||||
{
|
{
|
||||||
char json_str[128];
|
char json_str[128];
|
||||||
@@ -56,6 +70,7 @@ static esp_err_t json_to_str_parm(char *jsonstr, char *nameStr, char *valStr) //
|
|||||||
valStr[0] = 0;
|
valStr[0] = 0;
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
// send string to ws
|
||||||
static esp_err_t 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;
|
httpd_ws_frame_t ws_pkt;
|
||||||
@@ -65,6 +80,8 @@ static esp_err_t send_json_string(char *str, httpd_req_t *req)
|
|||||||
ws_pkt.len = strlen(str);
|
ws_pkt.len = strlen(str);
|
||||||
return httpd_ws_send_frame(req, &ws_pkt);
|
return httpd_ws_send_frame(req, &ws_pkt);
|
||||||
}
|
}
|
||||||
|
// main ws OTA handler
|
||||||
|
// Handshake and process OTA
|
||||||
static esp_err_t ota_ws_handler(httpd_req_t *req)
|
static esp_err_t ota_ws_handler(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
char json_key[64] = {0};
|
char json_key[64] = {0};
|
||||||
@@ -78,7 +95,7 @@ static esp_err_t ota_ws_handler(httpd_req_t *req)
|
|||||||
if (req->method == HTTP_GET)
|
if (req->method == HTTP_GET)
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "Handshake done, the new connection was opened");
|
ESP_LOGI(TAG, "Handshake done, the new connection was opened");
|
||||||
if(check_ota_ws_rollback_enable())
|
if(check_ota_ws_rollback_enable()) // check rollback enable, send cmd to enable rollback dialog on html
|
||||||
{
|
{
|
||||||
snprintf(json_str, sizeof(json_str), "{\"name\":\"%s\",\"value\":\"%s\" }", OTA_CHECK_ROLLBACK, "true");
|
snprintf(json_str, sizeof(json_str), "{\"name\":\"%s\",\"value\":\"%s\" }", OTA_CHECK_ROLLBACK, "true");
|
||||||
send_json_string(json_str, req);
|
send_json_string(json_str, req);
|
||||||
@@ -86,7 +103,7 @@ static esp_err_t ota_ws_handler(httpd_req_t *req)
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));
|
memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));
|
||||||
/* Set max_len = 0 to get the frame len */
|
// Set max_len = 0 to get the frame len
|
||||||
esp_err_t ret = httpd_ws_recv_frame(req, &ws_pkt, 0);
|
esp_err_t ret = httpd_ws_recv_frame(req, &ws_pkt, 0);
|
||||||
if (ret != ESP_OK)
|
if (ret != ESP_OK)
|
||||||
{
|
{
|
||||||
@@ -95,7 +112,7 @@ static esp_err_t ota_ws_handler(httpd_req_t *req)
|
|||||||
}
|
}
|
||||||
if (ws_pkt.len)
|
if (ws_pkt.len)
|
||||||
{
|
{
|
||||||
/* ws_pkt.len + 1 is for NULL termination as we are expecting a string */
|
// ws_pkt.len + 1 is for NULL termination as we are expecting a string
|
||||||
buf = calloc(1, ws_pkt.len + 1);
|
buf = calloc(1, ws_pkt.len + 1);
|
||||||
if (buf == NULL)
|
if (buf == NULL)
|
||||||
{
|
{
|
||||||
@@ -103,7 +120,7 @@ static esp_err_t ota_ws_handler(httpd_req_t *req)
|
|||||||
return ESP_ERR_NO_MEM;
|
return ESP_ERR_NO_MEM;
|
||||||
}
|
}
|
||||||
ws_pkt.payload = buf;
|
ws_pkt.payload = buf;
|
||||||
/* Set max_len = ws_pkt.len to get the frame payload */
|
// Set max_len = ws_pkt.len to get the frame payload
|
||||||
ret = httpd_ws_recv_frame(req, &ws_pkt, ws_pkt.len);
|
ret = httpd_ws_recv_frame(req, &ws_pkt, ws_pkt.len);
|
||||||
if (ret != ESP_OK)
|
if (ret != ESP_OK)
|
||||||
{
|
{
|
||||||
@@ -112,10 +129,9 @@ static esp_err_t ota_ws_handler(httpd_req_t *req)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ret = ESP_OK;
|
ret = ESP_OK;
|
||||||
if (ws_pkt.type == HTTPD_WS_TYPE_TEXT)
|
if (ws_pkt.type == HTTPD_WS_TYPE_TEXT) // process json cmd
|
||||||
{
|
{
|
||||||
//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)) // decode json to key/value parm
|
||||||
if (json_to_str_parm((char *)buf, json_key, json_value))
|
|
||||||
{
|
{
|
||||||
ota_error(req, OTA_ERROR, "Error json str");
|
ota_error(req, OTA_ERROR, "Error json str");
|
||||||
goto _recv_ret;
|
goto _recv_ret;
|
||||||
@@ -136,9 +152,9 @@ static esp_err_t ota_ws_handler(httpd_req_t *req)
|
|||||||
}
|
}
|
||||||
ota_started = 1;
|
ota_started = 1;
|
||||||
ota_start_chunk = 0;
|
ota_start_chunk = 0;
|
||||||
snprintf(json_str, sizeof(json_str), "{\"name\":\"%s\",\"value\":%d}", OTA_SET_CHUNK_SIZE, OTA_CHUNK_SIZE);
|
snprintf(json_str, sizeof(json_str), "{\"name\":\"%s\",\"value\":%d}", OTA_SET_CHUNK_SIZE, OTA_CHUNK_SIZE); // set download chunk
|
||||||
send_json_string(json_str, req);
|
send_json_string(json_str, req);
|
||||||
snprintf(json_str, sizeof(json_str), "{\"name\":\"%s\",\"value\":%d}", OTA_GET_CHUNK, ota_start_chunk);
|
snprintf(json_str, sizeof(json_str), "{\"name\":\"%s\",\"value\":%d}", OTA_GET_CHUNK, ota_start_chunk); // cmd -> send first chunk with start addresss = 0
|
||||||
send_json_string(json_str, req);
|
send_json_string(json_str, req);
|
||||||
}
|
}
|
||||||
if (strncmp(json_key, OTA_CANCEL, sizeof(OTA_CANCEL)) == 0) // cancel ota
|
if (strncmp(json_key, OTA_CANCEL, sizeof(OTA_CANCEL)) == 0) // cancel ota
|
||||||
@@ -153,12 +169,12 @@ static esp_err_t ota_ws_handler(httpd_req_t *req)
|
|||||||
ret = ESP_OK;
|
ret = ESP_OK;
|
||||||
goto _recv_ret;
|
goto _recv_ret;
|
||||||
}
|
}
|
||||||
if (strncmp(json_key, OTA_PROCESS_ROLLBACK, sizeof(OTA_PROCESS_ROLLBACK)) == 0) // process rollback ?
|
if (strncmp(json_key, OTA_PROCESS_ROLLBACK, sizeof(OTA_PROCESS_ROLLBACK)) == 0) // process rollback &
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG,"rollback command %s %s",json_key,json_value);
|
ESP_LOGI(TAG,"rollback command %s %s",json_key,json_value);
|
||||||
if(strncmp(json_value,"true",sizeof("true")) == 0)
|
if(strncmp(json_value,"true",sizeof("true")) == 0)
|
||||||
{
|
{
|
||||||
ret = rollback_ota_ws(true); // rollback
|
ret = rollback_ota_ws(true); // rollback and restart
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -166,40 +182,37 @@ static esp_err_t ota_ws_handler(httpd_req_t *req)
|
|||||||
}
|
}
|
||||||
goto _recv_ret;
|
goto _recv_ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strncmp(json_key, OTA_RESTART_ESP, sizeof(OTA_RESTART_ESP)) == 0) // cancel ota
|
if (strncmp(json_key, OTA_RESTART_ESP, sizeof(OTA_RESTART_ESP)) == 0) // cancel ota
|
||||||
{
|
{
|
||||||
esp_restart();
|
esp_restart();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (ws_pkt.type == HTTPD_WS_TYPE_BINARY && ota_started)
|
else if (ws_pkt.type == HTTPD_WS_TYPE_BINARY && ota_started) // download OTA firmware with chunked part
|
||||||
{
|
{
|
||||||
//ESP_LOGI(TAG, "Rcv bin msg start=%d len=%d", ota_start_chunk, ws_pkt.len);
|
|
||||||
|
|
||||||
if (ota_start_chunk + ws_pkt.len < ota_size)
|
if (ota_start_chunk + ws_pkt.len < ota_size) //read chuk of ota
|
||||||
{
|
{
|
||||||
ret = write_ota_ws(ws_pkt.len, buf);
|
ret = write_ota_ws(ws_pkt.len, buf); // write chunk of ota
|
||||||
if (ret)
|
if (ret)
|
||||||
{
|
{
|
||||||
ota_error(req, OTA_ERROR, "Error write ota");
|
ota_error(req, OTA_ERROR, "Error write ota");
|
||||||
goto _recv_ret;
|
goto _recv_ret;
|
||||||
}
|
}
|
||||||
ota_start_chunk += ws_pkt.len;
|
ota_start_chunk += ws_pkt.len;
|
||||||
snprintf(json_str, sizeof(json_str), "{\"name\":\"%s\",\"value\": %d }", OTA_GET_CHUNK, ota_start_chunk);
|
snprintf(json_str, sizeof(json_str), "{\"name\":\"%s\",\"value\": %d }", OTA_GET_CHUNK, ota_start_chunk); // cmd -> next chunk
|
||||||
send_json_string(json_str, req);
|
send_json_string(json_str, req);
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else // last chunk and end ota
|
||||||
{
|
{
|
||||||
// last chunk and end ota
|
ret = write_ota_ws(ws_pkt.len, buf); // write last chunk of ota
|
||||||
ret = write_ota_ws(ws_pkt.len, buf);
|
|
||||||
if (ret)
|
if (ret)
|
||||||
{
|
{
|
||||||
ota_error(req, OTA_ERROR, "Error write ota");
|
ota_error(req, OTA_ERROR, "Error write ota");
|
||||||
goto _recv_ret;
|
goto _recv_ret;
|
||||||
}
|
}
|
||||||
ret = end_ota_ws();
|
ret = end_ota_ws(); // end ota
|
||||||
if (ret)
|
if (ret)
|
||||||
{
|
{
|
||||||
ota_error(req, OTA_ERROR, "Error end ota");
|
ota_error(req, OTA_ERROR, "Error end ota");
|
||||||
@@ -209,7 +222,7 @@ static esp_err_t ota_ws_handler(httpd_req_t *req)
|
|||||||
ota_start_chunk = 0;
|
ota_start_chunk = 0;
|
||||||
ota_started = 0;
|
ota_started = 0;
|
||||||
ESP_LOGI(TAG,"OTA END OK");
|
ESP_LOGI(TAG,"OTA END OK");
|
||||||
snprintf(json_str, sizeof(json_str), "{\"name\":\"%s\",\"value\":\"%s\" }", OTA_END, "OK");
|
snprintf(json_str, sizeof(json_str), "{\"name\":\"%s\",\"value\":\"%s\" }", OTA_END, "OK"); // send ota end cmd ( ota ok )
|
||||||
send_json_string(json_str, req);
|
send_json_string(json_str, req);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -217,6 +230,8 @@ _recv_ret:
|
|||||||
free(buf);
|
free(buf);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
// main http get handler
|
||||||
|
// send http initial page and js code
|
||||||
static esp_err_t ota_get_handler(httpd_req_t *req)
|
static esp_err_t ota_get_handler(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
extern const unsigned char ota_ws_html_start[] asm("_binary_ota_ws_html_start");
|
extern const unsigned char ota_ws_html_start[] asm("_binary_ota_ws_html_start");
|
||||||
@@ -238,6 +253,8 @@ static const httpd_uri_t ws = {
|
|||||||
.handler = ota_ws_handler,
|
.handler = ota_ws_handler,
|
||||||
.user_ctx = NULL,
|
.user_ctx = NULL,
|
||||||
.is_websocket = true};
|
.is_websocket = true};
|
||||||
|
|
||||||
|
// register all ota uri handler
|
||||||
esp_err_t ota_ws_register_uri_handler(httpd_handle_t server)
|
esp_err_t ota_ws_register_uri_handler(httpd_handle_t server)
|
||||||
{
|
{
|
||||||
esp_err_t ret = ESP_OK;
|
esp_err_t ret = ESP_OK;
|
||||||
Reference in New Issue
Block a user