Merged with httpd - work in progress

This commit is contained in:
Sebastien
2020-03-10 17:27:06 -04:00
parent 39058213fa
commit 2ab14d62be
20 changed files with 110 additions and 216 deletions

View File

@@ -1,7 +1,8 @@
idf_component_register( SRC_DIRS .
INCLUDE_DIRS .
INCLUDE_DIRS . ${IDF_PATH}/components/esp_http_server/src ${IDF_PATH}/components/esp_http_server/src/port/esp32 ${IDF_PATH}/components/esp_http_server/src/util ${IDF_PATH}/components/esp_http_server/src/
REQUIRES squeezelite-ota json mdns
PRIV_REQUIRES tools services platform_config esp_common json newlib freertos spi_flash nvs_flash mdns pthread wpa_supplicant platform_console
PRIV_REQUIRES tools services platform_config esp_common json newlib freertos spi_flash nvs_flash mdns pthread wpa_supplicant platform_console esp_http_server
EMBED_FILES style.css code.js index.html bootstrap.min.css.gz jquery.min.js.gz popper.min.js.gz bootstrap.min.js.gz
)

View File

@@ -45,7 +45,7 @@ function to process requests, decode URLs, serve files, etc. etc.
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "config.h"
#include "platform_config.h"
#include "sys/param.h"
#include "esp_vfs.h"
#include "lwip/ip_addr.h"
@@ -663,11 +663,13 @@ esp_err_t config_post_handler(httpd_req_t *req){
cJSON_Delete(root);
if(bOTA) {
#if RECOVERY_APPLICATION
ESP_LOGW_LOC(TAG, "Starting process OTA for url %s",otaURL);
#else
ESP_LOGW_LOC(TAG, "Restarting system to process OTA for url %s",otaURL);
#endif
if(is_recovery_running){
ESP_LOGW_LOC(TAG, "Starting process OTA for url %s",otaURL);
}
else {
ESP_LOGW_LOC(TAG, "Restarting system to process OTA for url %s",otaURL);
}
wifi_manager_reboot_ota(otaURL);
free(otaURL);
}
@@ -805,82 +807,83 @@ esp_err_t recovery_post_handler(httpd_req_t *req){
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;
esp_err_t err =ESP_OK;
if(is_recovery_running){
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;
}
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;
char *head=binary_buffer;
int received;
/* Content length of the request gives
* the size of the file being uploaded */
int remaining = req->content_len;
/* Content length of the request gives
* the size of the file being uploaded */
int remaining = req->content_len;
while (remaining > 0) {
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;
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;
}
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;
/* 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;
}
/* 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;
//todo: handle this in ajax. For now, just send the root page
httpd_resp_send(req, (const char *)success, strlen(success));
}
/* 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]={};

View File

@@ -88,9 +88,7 @@ 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 messages_get_handler(httpd_req_t *req);
@@ -111,13 +109,13 @@ typedef struct rest_server_context {
* @brief RTOS task for the HTTP server. Do not start manually.
* @see void http_server_start()
*/
void CODE_RAM_LOCATION http_server(void *pvParameters);
void http_server(void *pvParameters);
/* @brief helper function that processes one HTTP request at a time */
void CODE_RAM_LOCATION http_server_netconn_serve(struct netconn *conn);
void http_server_netconn_serve(struct netconn *conn);
/* @brief create the task for the http server */
esp_err_t CODE_RAM_LOCATION http_server_start();
esp_err_t http_server_start();
/**
* @brief gets a char* pointer to the first occurence of header_name withing the complete http request request.
@@ -130,9 +128,9 @@ esp_err_t CODE_RAM_LOCATION http_server_start();
* @param len the size of the header value if found.
* @return pointer to the beginning of the header value.
*/
char* CODE_RAM_LOCATION http_server_get_header(char *request, char *header_name, int *len);
char* http_server_get_header(char *request, char *header_name, int *len);
void CODE_RAM_LOCATION strreplace(char *src, char *str, char *rep);
void strreplace(char *src, char *str, char *rep);
/* @brief lock the json config object */
bool http_server_lock_json_object(TickType_t xTicksToWait);
/* @brief unlock the json config object */

View File

@@ -1440,7 +1440,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;

View File

@@ -42,17 +42,6 @@ extern "C" {
#include "squeezelite-ota.h"
#include "cJSON.h"
#ifndef RECOVERY_APPLICATION
#error "RECOVERY_APPLICATION not defined. Defaulting to squeezelite"
#endif
#if RECOVERY_APPLICATION==1
#elif RECOVERY_APPLICATION==0
#else
#error "unknown configuration"
#endif
/**
* @brief Defines the maximum size of a SSID name. 32 is IEEE standard.

View File

@@ -31,6 +31,7 @@
#include "freertos/task.h"
#include "config.h"
#include "messaging.h"
#include "platform_esp32.h"
static const char TAG[] = "http_server";
static httpd_handle_t _server = NULL;
@@ -74,13 +75,10 @@ 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
if(is_recovery_running){
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);
}
// 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
// Known HTTP GET requests to check for Captive Portal