mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-09 13:07:03 +03:00
added ability to upload new firmware from the browser.
This commit is contained in:
@@ -367,7 +367,31 @@ $(document).ready(function(){
|
||||
console.log('sent config JSON with headers:', JSON.stringify(headers));
|
||||
console.log('sent config JSON with data:', JSON.stringify(data));
|
||||
});
|
||||
|
||||
$("#fwUpload").on("click", function() {
|
||||
|
||||
var upload_path = "/flash.json";
|
||||
var fileInput = document.getElementById("flashfilename").files;
|
||||
if (fileInput.length == 0) {
|
||||
alert("No file selected!");
|
||||
} else {
|
||||
var file = fileInput[0];
|
||||
var xhttp = new XMLHttpRequest();
|
||||
xhttp.onreadystatechange = function() {
|
||||
if (xhttp.readyState == 4) {
|
||||
if (xhttp.status == 200) {
|
||||
showMessage(xhttp.responseText, 'INFO')
|
||||
} else if (xhttp.status == 0) {
|
||||
showMessage("Upload connection was closed abruptly!", 'ERROR');
|
||||
} else {
|
||||
showMessage(xhttp.status + " Error!\n" + xhttp.responseText, 'ERROR');
|
||||
}
|
||||
}
|
||||
};
|
||||
xhttp.open("POST", upload_path, true);
|
||||
xhttp.send(file);
|
||||
}
|
||||
enableStatusTimer = true;
|
||||
});
|
||||
$("#flash").on("click", function() {
|
||||
var data = { 'timestamp': Date.now() };
|
||||
if (blockFlashButton) return;
|
||||
@@ -732,6 +756,8 @@ function checkStatus(){
|
||||
$("footer.footer").addClass('sl');
|
||||
$("#boot-button").html('Recovery');
|
||||
$("#boot-form").attr('action', '/recovery.json');
|
||||
$('#uploaddiv"]').hide();
|
||||
|
||||
enableStatusTimer = false;
|
||||
}
|
||||
}
|
||||
@@ -874,3 +900,6 @@ function showMessage(message, severity) {
|
||||
function inRange(x, min, max) {
|
||||
return ((x-min)*(x-max) <= 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -41,6 +41,7 @@ function to process requests, decode URLs, serve files, etc. etc.
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "cJSON.h"
|
||||
#include "config.h"
|
||||
#include "esp_system.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
@@ -323,7 +324,10 @@ static const char* get_path_from_uri(char *dest, const char *uri, size_t destsiz
|
||||
/* Set HTTP response content type according to file extension */
|
||||
static esp_err_t set_content_type_from_file(httpd_req_t *req, const char *filename)
|
||||
{
|
||||
if (IS_FILE_EXT(filename, ".pdf")) {
|
||||
if(strlen(filename) ==0){
|
||||
// for root page, etc.
|
||||
return httpd_resp_set_type(req, HTTPD_TYPE_TEXT);
|
||||
} else if (IS_FILE_EXT(filename, ".pdf")) {
|
||||
return httpd_resp_set_type(req, "application/pdf");
|
||||
} else if (IS_FILE_EXT(filename, ".html")) {
|
||||
return httpd_resp_set_type(req, HTTPD_TYPE_TEXT);
|
||||
@@ -340,6 +344,7 @@ static esp_err_t set_content_type_from_file(httpd_req_t *req, const char *filena
|
||||
} else if (IS_FILE_EXT(filename, ".json")) {
|
||||
return httpd_resp_set_type(req, HTTPD_TYPE_JSON);
|
||||
}
|
||||
|
||||
/* This is a limited set only */
|
||||
/* For any other type always set as plain text */
|
||||
return httpd_resp_set_type(req, "text/plain");
|
||||
@@ -360,6 +365,7 @@ static esp_err_t set_content_type_from_req(httpd_req_t *req)
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Browsing files forbidden.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
set_content_type_from_file(req, filename);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -548,6 +554,11 @@ esp_err_t config_post_handler(httpd_req_t *req){
|
||||
// todo: redirect to login page
|
||||
// return ESP_OK;
|
||||
}
|
||||
err = set_content_type_from_req(req);
|
||||
if(err != ESP_OK){
|
||||
return err;
|
||||
}
|
||||
|
||||
char *buf = ((rest_server_context_t *)(req->user_ctx))->scratch;
|
||||
cJSON *root = cJSON_Parse(buf);
|
||||
if(root == NULL){
|
||||
@@ -637,7 +648,6 @@ esp_err_t config_post_handler(httpd_req_t *req){
|
||||
|
||||
|
||||
if(err==ESP_OK){
|
||||
set_content_type_from_req(req);
|
||||
httpd_resp_sendstr(req, "{ \"result\" : \"OK\" }");
|
||||
}
|
||||
cJSON_Delete(root);
|
||||
@@ -660,11 +670,16 @@ esp_err_t connect_post_handler(httpd_req_t *req){
|
||||
char * ssid=NULL;
|
||||
char * password=NULL;
|
||||
char * host_name=NULL;
|
||||
set_content_type_from_req(req);
|
||||
|
||||
esp_err_t err = post_handler_buff_receive(req);
|
||||
if(err!=ESP_OK){
|
||||
return err;
|
||||
}
|
||||
err = set_content_type_from_req(req);
|
||||
if(err != ESP_OK){
|
||||
return err;
|
||||
}
|
||||
|
||||
char *buf = ((rest_server_context_t *)(req->user_ctx))->scratch;
|
||||
if(!is_user_authenticated(req)){
|
||||
// todo: redirect to login page
|
||||
@@ -724,7 +739,10 @@ esp_err_t connect_delete_handler(httpd_req_t *req){
|
||||
// todo: redirect to login page
|
||||
// return ESP_OK;
|
||||
}
|
||||
set_content_type_from_req(req);
|
||||
esp_err_t err = set_content_type_from_req(req);
|
||||
if(err != ESP_OK){
|
||||
return err;
|
||||
}
|
||||
httpd_resp_send(req, (const char *)success, strlen(success));
|
||||
wifi_manager_disconnect_async();
|
||||
|
||||
@@ -737,7 +755,11 @@ esp_err_t reboot_ota_post_handler(httpd_req_t *req){
|
||||
// todo: redirect to login page
|
||||
// return ESP_OK;
|
||||
}
|
||||
set_content_type_from_req(req);
|
||||
esp_err_t err = set_content_type_from_req(req);
|
||||
if(err != ESP_OK){
|
||||
return err;
|
||||
}
|
||||
|
||||
httpd_resp_send(req, (const char *)success, strlen(success));
|
||||
wifi_manager_reboot(OTA);
|
||||
return ESP_OK;
|
||||
@@ -749,7 +771,10 @@ esp_err_t reboot_post_handler(httpd_req_t *req){
|
||||
// todo: redirect to login page
|
||||
// return ESP_OK;
|
||||
}
|
||||
set_content_type_from_req(req);
|
||||
esp_err_t err = set_content_type_from_req(req);
|
||||
if(err != ESP_OK){
|
||||
return err;
|
||||
}
|
||||
httpd_resp_send(req, (const char *)success, strlen(success));
|
||||
wifi_manager_reboot(RESTART);
|
||||
return ESP_OK;
|
||||
@@ -761,12 +786,91 @@ esp_err_t recovery_post_handler(httpd_req_t *req){
|
||||
// todo: redirect to login page
|
||||
// return ESP_OK;
|
||||
}
|
||||
set_content_type_from_req(req);
|
||||
esp_err_t err = set_content_type_from_req(req);
|
||||
if(err != ESP_OK){
|
||||
return err;
|
||||
}
|
||||
httpd_resp_send(req, (const char *)success, strlen(success));
|
||||
wifi_manager_reboot(RECOVERY);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
#if RECOVERY_APPLICATION
|
||||
esp_err_t flash_post_handler(httpd_req_t *req){
|
||||
ESP_LOGD_LOC(TAG, "serving [%s]", req->uri);
|
||||
char success[]="File uploaded. Flashing started.";
|
||||
if(!is_user_authenticated(req)){
|
||||
// todo: redirect to login page
|
||||
// return ESP_OK;
|
||||
}
|
||||
esp_err_t err = httpd_resp_set_type(req, HTTPD_TYPE_TEXT);
|
||||
if(err != ESP_OK){
|
||||
return err;
|
||||
}
|
||||
char * binary_buffer = malloc(req->content_len);
|
||||
if(binary_buffer == NULL){
|
||||
ESP_LOGE(TAG, "File too large : %d bytes", req->content_len);
|
||||
/* Respond with 400 Bad Request */
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST,
|
||||
"Binary file too large. Unable to allocate memory!");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
ESP_LOGI(TAG, "Receiving ota binary file");
|
||||
/* Retrieve the pointer to scratch buffer for temporary storage */
|
||||
char *buf = ((rest_server_context_t *)(req->user_ctx))->scratch;
|
||||
|
||||
char *head=binary_buffer;
|
||||
int received;
|
||||
|
||||
/* Content length of the request gives
|
||||
* the size of the file being uploaded */
|
||||
int remaining = req->content_len;
|
||||
|
||||
while (remaining > 0) {
|
||||
|
||||
ESP_LOGI(TAG, "Remaining size : %d", remaining);
|
||||
/* Receive the file part by part into a buffer */
|
||||
if ((received = httpd_req_recv(req, buf, MIN(remaining, SCRATCH_BUFSIZE))) <= 0) {
|
||||
if (received == HTTPD_SOCK_ERR_TIMEOUT) {
|
||||
/* Retry if timeout occurred */
|
||||
continue;
|
||||
}
|
||||
FREE_RESET(binary_buffer);
|
||||
ESP_LOGE(TAG, "File reception failed!");
|
||||
/* Respond with 500 Internal Server Error */
|
||||
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to receive file");
|
||||
err = ESP_FAIL;
|
||||
goto bail_out;
|
||||
}
|
||||
|
||||
/* Write buffer content to file on storage */
|
||||
if (received ) {
|
||||
memcpy(head,buf,received );
|
||||
head+=received;
|
||||
}
|
||||
|
||||
/* Keep track of remaining size of
|
||||
* the file left to be uploaded */
|
||||
remaining -= received;
|
||||
}
|
||||
|
||||
/* Close file upon upload completion */
|
||||
ESP_LOGI(TAG, "File reception complete. Invoking OTA process.");
|
||||
err = start_ota(NULL, binary_buffer, req->content_len);
|
||||
if(err!=ESP_OK){
|
||||
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "OTA processing failed");
|
||||
goto bail_out;
|
||||
}
|
||||
|
||||
//todo: handle this in ajax. For now, just send the root page
|
||||
httpd_resp_send(req, (const char *)success, strlen(success));
|
||||
|
||||
bail_out:
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
char * get_ap_ip_address(){
|
||||
static char ap_ip_address[IP4ADDR_STRLEN_MAX]={};
|
||||
|
||||
@@ -869,7 +973,7 @@ esp_err_t redirect_processor(httpd_req_t *req, httpd_err_code_t error){
|
||||
char *req_host = alloc_get_http_header(req, "Host");
|
||||
|
||||
user_agent = alloc_get_http_header(req,"User-Agent");
|
||||
if((useragentiscaptivenetwork = strcasestr(user_agent,"CaptiveNetworkSupport"))==true){
|
||||
if((useragentiscaptivenetwork = (user_agent!=NULL && strcasestr(user_agent,"CaptiveNetworkSupport"))==true)){
|
||||
ESP_LOGW_LOC(TAG,"Found user agent that supports captive networks! [%s]",user_agent);
|
||||
}
|
||||
|
||||
@@ -941,7 +1045,10 @@ esp_err_t status_get_handler(httpd_req_t *req){
|
||||
// todo: redirect to login page
|
||||
// return ESP_OK;
|
||||
}
|
||||
set_content_type_from_req(req);
|
||||
esp_err_t err = set_content_type_from_req(req);
|
||||
if(err != ESP_OK){
|
||||
return err;
|
||||
}
|
||||
|
||||
if(wifi_manager_lock_json_buffer(( TickType_t ) 10)) {
|
||||
char *buff = wifi_manager_alloc_get_ip_info_json();
|
||||
|
||||
@@ -88,6 +88,9 @@ esp_err_t connect_delete_handler(httpd_req_t *req);
|
||||
esp_err_t reboot_ota_post_handler(httpd_req_t *req);
|
||||
esp_err_t reboot_post_handler(httpd_req_t *req);
|
||||
esp_err_t recovery_post_handler(httpd_req_t *req);
|
||||
#if RECOVERY_APPLICATION
|
||||
esp_err_t flash_post_handler(httpd_req_t *req);
|
||||
#endif
|
||||
esp_err_t status_get_handler(httpd_req_t *req);
|
||||
esp_err_t ap_scan_handler(httpd_req_t *req);
|
||||
esp_err_t redirect_ev_handler(httpd_req_t *req);
|
||||
|
||||
@@ -345,21 +345,20 @@
|
||||
</table>
|
||||
<h2>Firmware URL:</h2>
|
||||
<textarea id="fwurl" maxlength="350"></textarea>
|
||||
<!--
|
||||
<br />OR<br />
|
||||
<div class="input-group mb-3" id="upload">
|
||||
<div class="buttons">
|
||||
<input type="button" id="flash" class="btn btn-danger" value="Flash!" /><span id="flash-status"></span>
|
||||
</div>
|
||||
<br />OR<br />
|
||||
<div class="input-group mb-3" id="uploaddiv">
|
||||
<div class="custom-file">
|
||||
<input type="file" class="custom-file-input" id="inputGroupFile01">
|
||||
<label class="custom-file-label" for="inputGroupFile01"></label>
|
||||
<input type="file" class="custom-file-input" id="flashfilename">
|
||||
<label class="custom-file-label" for="flashfilename"></label>
|
||||
</div>
|
||||
<div class="input-group-append">
|
||||
<span class="input-group-text" id="fwUpload">Upload</span>
|
||||
</div>
|
||||
</div>
|
||||
-->
|
||||
<div class="buttons">
|
||||
<input type="button" id="flash" class="btn btn-danger" value="Flash!" /><span id="flash-status"></span>
|
||||
</div>
|
||||
|
||||
<div id="otadiv">
|
||||
<div class="progress" id="progress">
|
||||
<div class="progress-bar" role="progressbar" aria-valuemin="0" aria-valuemax="100" style="width:0%">
|
||||
|
||||
@@ -66,6 +66,7 @@ Contains the freeRTOS task and all necessary support
|
||||
#include "globdefs.h"
|
||||
|
||||
#ifndef RECOVERY_APPLICATION
|
||||
#pragma warning "Defaulting to squeezelite build"
|
||||
#define RECOVERY_APPLICATION 0
|
||||
#endif
|
||||
|
||||
@@ -1470,7 +1471,7 @@ void wifi_manager( void * pvParameters ){
|
||||
break;
|
||||
case ORDER_RESTART_OTA_URL:
|
||||
ESP_LOGD(TAG, "Calling start_ota.");
|
||||
start_ota(msg.param);
|
||||
start_ota(msg.param, NULL, 0);
|
||||
free(msg.param);
|
||||
break;
|
||||
|
||||
|
||||
@@ -48,7 +48,6 @@ extern "C" {
|
||||
|
||||
#if RECOVERY_APPLICATION==1
|
||||
#elif RECOVERY_APPLICATION==0
|
||||
#pragma message "compiling for squeezelite."
|
||||
#else
|
||||
#error "unknown configuration"
|
||||
#endif
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include "http_server_handlers.h"
|
||||
#include "http_server_handlers.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_http_server.h"
|
||||
#include <inttypes.h>
|
||||
@@ -70,6 +70,12 @@ void register_regular_handlers(httpd_handle_t server){
|
||||
httpd_uri_t connect_delete = { .uri = "/connect.json", .method = HTTP_DELETE, .handler = connect_delete_handler, .user_ctx = rest_context };
|
||||
httpd_register_uri_handler(server, &connect_delete);
|
||||
|
||||
#if RECOVERY_APPLICATION
|
||||
|
||||
httpd_uri_t flash_post = { .uri = "/flash.json", .method = HTTP_POST, .handler = flash_post_handler, .user_ctx = rest_context };
|
||||
httpd_register_uri_handler(server, &flash_post);
|
||||
#endif
|
||||
|
||||
|
||||
// from https://github.com/tripflex/wifi-captive-portal/blob/master/src/mgos_wifi_captive_portal.c
|
||||
// https://unix.stackexchange.com/questions/432190/why-isnt-androids-captive-portal-detection-triggering-a-browser-window
|
||||
@@ -97,6 +103,7 @@ void register_regular_handlers(httpd_handle_t server){
|
||||
httpd_register_uri_handler(server, &connect_redirect_8);
|
||||
|
||||
|
||||
|
||||
ESP_LOGD(TAG,"Registering default error handler for 404");
|
||||
httpd_register_err_handler(server, HTTPD_404_NOT_FOUND,&err_handler);
|
||||
|
||||
@@ -150,223 +157,3 @@ void stop_webserver(httpd_handle_t server)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#if 0
|
||||
|
||||
if(strstr(line, "GET / ")) {
|
||||
netconn_write(conn, http_html_hdr, sizeof(http_html_hdr) - 1, NETCONN_NOCOPY);
|
||||
netconn_write(conn, index_html_start, index_html_end- index_html_start, NETCONN_NOCOPY);
|
||||
}
|
||||
else if(strstr(line, "GET /code.js ")) {
|
||||
netconn_write(conn, http_js_hdr, sizeof(http_js_hdr) - 1, NETCONN_NOCOPY);
|
||||
netconn_write(conn, code_js_start, code_js_end - code_js_start, NETCONN_NOCOPY);
|
||||
}
|
||||
else if(strstr(line, "GET /style.css ")) {
|
||||
netconn_write(conn, http_css_hdr, sizeof(http_css_hdr) - 1, NETCONN_NOCOPY);
|
||||
netconn_write(conn, style_css_start, style_css_end - style_css_start, NETCONN_NOCOPY);
|
||||
}
|
||||
else if(strstr(line, "GET /jquery.js ")) {
|
||||
http_server_send_resource_file(conn,jquery_gz_start, jquery_gz_end, "text/javascript", "gzip" );
|
||||
}
|
||||
else if(strstr(line, "GET /popper.js ")) {
|
||||
http_server_send_resource_file(conn,popper_gz_start, popper_gz_end, "text/javascript", "gzip" );
|
||||
}
|
||||
else if(strstr(line, "GET /bootstrap.js ")) {
|
||||
http_server_send_resource_file(conn,bootstrap_js_gz_start, bootstrap_js_gz_end, "text/javascript", "gzip" );
|
||||
}
|
||||
else if(strstr(line, "GET /bootstrap.css ")) {
|
||||
http_server_send_resource_file(conn,bootstrap_css_gz_start, bootstrap_css_gz_end, "text/css", "gzip" );
|
||||
}
|
||||
|
||||
//dynamic stuff
|
||||
else if(strstr(line, "GET /scan.json ")) {
|
||||
ESP_LOGI(TAG, "Starting wifi scan");
|
||||
wifi_manager_scan_async();
|
||||
}
|
||||
else if(strstr(line, "GET /ap.json ")) {
|
||||
/* if we can get the mutex, write the last version of the AP list */
|
||||
ESP_LOGI(TAG, "Processing ap.json request");
|
||||
if(wifi_manager_lock_json_buffer(( TickType_t ) 10)) {
|
||||
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
|
||||
char *buff = wifi_manager_alloc_get_ap_list_json();
|
||||
wifi_manager_unlock_json_buffer();
|
||||
if(buff!=NULL){
|
||||
netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
|
||||
free(buff);
|
||||
}
|
||||
else {
|
||||
ESP_LOGD(TAG, "Error retrieving ap list json string. ");
|
||||
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
|
||||
}
|
||||
}
|
||||
else {
|
||||
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
|
||||
ESP_LOGE(TAG, "http_server_netconn_serve: GET /ap.json failed to obtain mutex");
|
||||
}
|
||||
/* request a wifi scan */
|
||||
ESP_LOGI(TAG, "Starting wifi scan");
|
||||
wifi_manager_scan_async();
|
||||
ESP_LOGI(TAG, "Done serving ap.json");
|
||||
}
|
||||
else if(strstr(line, "GET /config.json ")) {
|
||||
ESP_LOGI(TAG, "Serving config.json");
|
||||
ESP_LOGI(TAG, "About to get config from flash");
|
||||
http_server_send_config_json(conn);
|
||||
ESP_LOGD(TAG, "Done serving config.json");
|
||||
}
|
||||
else if(strstr(line, "POST /config.json ")) {
|
||||
ESP_LOGI(TAG, "Serving POST config.json");
|
||||
int lenA=0;
|
||||
char * last_parm=save_ptr;
|
||||
char * next_parm=save_ptr;
|
||||
char * last_parm_name=NULL;
|
||||
bool bErrorFound=false;
|
||||
bool bOTA=false;
|
||||
char * otaURL=NULL;
|
||||
// todo: implement json body parsing
|
||||
//http_server_process_config(conn,save_ptr);
|
||||
|
||||
while(last_parm!=NULL) {
|
||||
// Search will return
|
||||
ESP_LOGD(TAG, "Getting parameters from X-Custom headers");
|
||||
last_parm = http_server_search_header(next_parm, "X-Custom-", &lenA, &last_parm_name,&next_parm,buf+buflen);
|
||||
if(last_parm!=NULL && last_parm_name!=NULL) {
|
||||
ESP_LOGI(TAG, "http_server_netconn_serve: POST config.json, config %s=%s", last_parm_name, last_parm);
|
||||
if(strcmp(last_parm_name, "fwurl")==0) {
|
||||
// we're getting a request to do an OTA from that URL
|
||||
ESP_LOGW(TAG, "Found OTA request!");
|
||||
otaURL=strdup(last_parm);
|
||||
bOTA=true;
|
||||
}
|
||||
else {
|
||||
ESP_LOGV(TAG, "http_server_netconn_serve: POST config.json Storing parameter");
|
||||
if(config_set_value(NVS_TYPE_STR, last_parm_name , last_parm) != ESP_OK){
|
||||
ESP_LOGE(TAG, "Unable to save nvs value.");
|
||||
}
|
||||
}
|
||||
}
|
||||
if(last_parm_name!=NULL) {
|
||||
free(last_parm_name);
|
||||
last_parm_name=NULL;
|
||||
}
|
||||
}
|
||||
if(bErrorFound) {
|
||||
netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY); //400 invalid request
|
||||
}
|
||||
else {
|
||||
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); //200ok
|
||||
if(bOTA) {
|
||||
|
||||
#if RECOVERY_APPLICATION
|
||||
ESP_LOGW(TAG, "Starting process OTA for url %s",otaURL);
|
||||
#else
|
||||
ESP_LOGW(TAG, "Restarting system to process OTA for url %s",otaURL);
|
||||
#endif
|
||||
wifi_manager_reboot_ota(otaURL);
|
||||
free(otaURL);
|
||||
}
|
||||
}
|
||||
ESP_LOGI(TAG, "Done Serving POST config.json");
|
||||
}
|
||||
else if(strstr(line, "POST /connect.json ")) {
|
||||
ESP_LOGI(TAG, "http_server_netconn_serve: POST /connect.json");
|
||||
bool found = false;
|
||||
int lenS = 0, lenP = 0, lenN = 0;
|
||||
char *ssid = NULL, *password = NULL;
|
||||
ssid = http_server_get_header(save_ptr, "X-Custom-ssid: ", &lenS);
|
||||
password = http_server_get_header(save_ptr, "X-Custom-pwd: ", &lenP);
|
||||
char * new_host_name_b = http_server_get_header(save_ptr, "X-Custom-host_name: ", &lenN);
|
||||
if(lenN > 0){
|
||||
lenN++;
|
||||
char * new_host_name = malloc(lenN);
|
||||
strlcpy(new_host_name, new_host_name_b, lenN);
|
||||
if(config_set_value(NVS_TYPE_STR, "host_name", new_host_name) != ESP_OK){
|
||||
ESP_LOGE(TAG, "Unable to save host name configuration");
|
||||
}
|
||||
free(new_host_name);
|
||||
}
|
||||
|
||||
if(ssid && lenS <= MAX_SSID_SIZE && password && lenP <= MAX_PASSWORD_SIZE) {
|
||||
wifi_config_t* config = wifi_manager_get_wifi_sta_config();
|
||||
memset(config, 0x00, sizeof(wifi_config_t));
|
||||
memcpy(config->sta.ssid, ssid, lenS);
|
||||
memcpy(config->sta.password, password, lenP);
|
||||
ESP_LOGD(TAG, "http_server_netconn_serve: wifi_manager_connect_async() call, with ssid: %s, password: %s", config->sta.ssid, config->sta.password);
|
||||
wifi_manager_connect_async();
|
||||
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); //200ok
|
||||
found = true;
|
||||
}
|
||||
else{
|
||||
ESP_LOGE(TAG, "SSID or Password invalid");
|
||||
}
|
||||
|
||||
|
||||
if(!found) {
|
||||
/* bad request the authentification header is not complete/not the correct format */
|
||||
netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY);
|
||||
ESP_LOGE(TAG, "bad request the authentification header is not complete/not the correct format");
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "http_server_netconn_serve: done serving connect.json");
|
||||
}
|
||||
else if(strstr(line, "DELETE /connect.json ")) {
|
||||
ESP_LOGI(TAG, "http_server_netconn_serve: DELETE /connect.json");
|
||||
/* request a disconnection from wifi and forget about it */
|
||||
wifi_manager_disconnect_async();
|
||||
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
|
||||
ESP_LOGI(TAG, "http_server_netconn_serve: done serving DELETE /connect.json");
|
||||
}
|
||||
else if(strstr(line, "POST /reboot_ota.json ")) {
|
||||
ESP_LOGI(TAG, "http_server_netconn_serve: POST reboot_ota.json");
|
||||
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
|
||||
wifi_manager_reboot(OTA);
|
||||
ESP_LOGI(TAG, "http_server_netconn_serve: done serving POST reboot_ota.json");
|
||||
}
|
||||
else if(strstr(line, "POST /reboot.json ")) {
|
||||
ESP_LOGI(TAG, "http_server_netconn_serve: POST reboot.json");
|
||||
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
|
||||
wifi_manager_reboot(RESTART);
|
||||
ESP_LOGI(TAG, "http_server_netconn_serve: done serving POST reboot.json");
|
||||
}
|
||||
else if(strstr(line, "POST /recovery.json ")) {
|
||||
ESP_LOGI(TAG, "http_server_netconn_serve: POST recovery.json");
|
||||
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY); /* 200 ok */
|
||||
wifi_manager_reboot(RECOVERY);
|
||||
ESP_LOGI(TAG, "http_server_netconn_serve: done serving POST recovery.json");
|
||||
}
|
||||
else if(strstr(line, "GET /status.json ")) {
|
||||
ESP_LOGI(TAG, "Serving status.json");
|
||||
if(wifi_manager_lock_json_buffer(( TickType_t ) 10)) {
|
||||
char *buff = wifi_manager_alloc_get_ip_info_json();
|
||||
wifi_manager_unlock_json_buffer();
|
||||
if(buff) {
|
||||
netconn_write(conn, http_ok_json_no_cache_hdr, sizeof(http_ok_json_no_cache_hdr) - 1, NETCONN_NOCOPY);
|
||||
netconn_write(conn, buff, strlen(buff), NETCONN_NOCOPY);
|
||||
free(buff);
|
||||
}
|
||||
else {
|
||||
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
netconn_write(conn, http_503_hdr, sizeof(http_503_hdr) - 1, NETCONN_NOCOPY);
|
||||
ESP_LOGE(TAG, "http_server_netconn_serve: GET /status failed to obtain mutex");
|
||||
}
|
||||
ESP_LOGI(TAG, "Done Serving status.json");
|
||||
}
|
||||
else {
|
||||
netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY);
|
||||
ESP_LOGE(TAG, "bad request from host: %s, request %s",remote_address, line);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
ESP_LOGE(TAG, "URL not found processing for remote host : %s",remote_address);
|
||||
netconn_write(conn, http_404_hdr, sizeof(http_404_hdr) - 1, NETCONN_NOCOPY);
|
||||
}
|
||||
free(host);
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user