Skip to content

Commit

Permalink
fix: add recovery page (skot#232)
Browse files Browse the repository at this point in the history
Adds a recovery web interface to enable users
to recover from a failed www.bin update.
Partial fix for Issue skot#213.
  • Loading branch information
tdb3 authored and tommy committed Jun 20, 2024
1 parent f55c6d7 commit a93c350
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 4 deletions.
33 changes: 29 additions & 4 deletions main/http_server/http_server.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "http_server.h"
#include "recovery_page.h"
#include "cJSON.h"
#include "esp_chip_info.h"
#include "esp_http_server.h"
Expand Down Expand Up @@ -119,6 +120,13 @@ static esp_err_t set_cors_headers(httpd_req_t * req)
: ESP_FAIL;
}

/* Recovery handler */
static esp_err_t rest_recovery_handler(httpd_req_t * req)
{
httpd_resp_send(req, recovery_page, HTTPD_RESP_USE_STRLEN);
return ESP_OK;
}

/* Send HTTP response with the contents of the requested file */
static esp_err_t rest_common_get_handler(httpd_req_t * req)
{
Expand Down Expand Up @@ -595,7 +603,12 @@ esp_err_t start_rest_server(void * pvParameters)
GLOBAL_STATE = (GlobalState *) pvParameters;
const char * base_path = "";

ESP_ERROR_CHECK(init_fs());
bool enter_recovery = false;
if (init_fs() != ESP_OK) {
// Unable to initialize the web app filesystem.
// Enter recovery mode
enter_recovery = true;
}

REST_CHECK(base_path, "wrong base path", err);
rest_server_context_t * rest_context = calloc(1, sizeof(rest_server_context_t));
Expand All @@ -609,6 +622,10 @@ esp_err_t start_rest_server(void * pvParameters)
ESP_LOGI(TAG, "Starting HTTP Server");
REST_CHECK(httpd_start(&server, &config) == ESP_OK, "Start server failed", err_start);

httpd_uri_t recovery_explicit_get_uri = {
.uri = "/recovery", .method = HTTP_GET, .handler = rest_recovery_handler, .user_ctx = rest_context};
httpd_register_uri_handler(server, &recovery_explicit_get_uri);

/* URI handler for fetching system info */
httpd_uri_t system_info_get_uri = {
.uri = "/api/system/info", .method = HTTP_GET, .handler = GET_system_info, .user_ctx = rest_context};
Expand Down Expand Up @@ -656,9 +673,17 @@ esp_err_t start_rest_server(void * pvParameters)
httpd_uri_t ws = {.uri = "/api/ws", .method = HTTP_GET, .handler = echo_handler, .user_ctx = NULL, .is_websocket = true};
httpd_register_uri_handler(server, &ws);

/* URI handler for getting web server files */
httpd_uri_t common_get_uri = {.uri = "/*", .method = HTTP_GET, .handler = rest_common_get_handler, .user_ctx = rest_context};
httpd_register_uri_handler(server, &common_get_uri);
if (enter_recovery) {
/* Make default route serve Recovery */
httpd_uri_t recovery_implicit_get_uri = {
.uri = "/*", .method = HTTP_GET, .handler = rest_recovery_handler, .user_ctx = rest_context};
httpd_register_uri_handler(server, &recovery_implicit_get_uri);

} else {
/* URI handler for getting web server files */
httpd_uri_t common_get_uri = {.uri = "/*", .method = HTTP_GET, .handler = rest_common_get_handler, .user_ctx = rest_context};
httpd_register_uri_handler(server, &common_get_uri);
}

httpd_register_err_handler(server, HTTPD_404_NOT_FOUND, http_404_error_handler);

Expand Down
79 changes: 79 additions & 0 deletions main/http_server/recovery_page.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#ifndef RECOVERY_PAGE
#define RECOVERY_PAGE

const char recovery_page[] = "<html>"
"<style>"
"body {"
" background-color: #000;"
" color: #00ff00;"
" font-family: courier new;"
"}"
"</style>"
"<head>"
"<title>AxeOS Recovery</title>"
"</head>"
"<body>"
"<pre>"
" ____ _____\n"
" /\\ / __ \\ / ____|\n"
" / \\ __ _____| | | | (___\n"
" / /\\ \\ \\ \\/ / _ \\ | | |\\___ \\\n"
" / ____ \\ > < __/ |__| |____) |\n"
" /_/___ \\_\\/_/\\_\\___|\\____/|_____/\n"
" | __ \\\n"
" | |__) |___ ___ _____ _____ _ __ _ _\n"
" | _ // _ \\/ __/ _ \\ \\ / / _ \\ '__| | | |\n"
" | | \\ \\ __/ (_| (_) \\ V / __/ | | |_| |\n"
" |_| \\_\\___|\\___\\___/ \\_/ \\___|_| \\__, |\n"
" __/ |\n"
" |___/\n"
"</pre>"
"<p>Please upload www.bin to recover AxeOS</p>"
"<p>After clicking upload, please wait 60 seconds<br>"
"DO NOT restart the device until response is received</p>"
"<input type=\"file\" id=\"wwwfile\" name=\"wwwfile\"><br>"
"<button id=\"upload\">Upload</button>"
"<small id=\"status\"></small>"
"<br><button id=\"restart\">Restart</button>"
"<br><br>Response:<br>"
"<small id=\"response\"></small>"
"<script>"
"document.getElementById('upload').addEventListener('click', handleUpload);"
"statusMsg = document.getElementById('status');"
"responseMsg = document.getElementById('response');"
"function handleUpload() {"
" const fileInput = document.getElementById('wwwfile');"
" const file = fileInput.files[0];"
" const uploadUrl = '/api/system/OTAWWW';"
" const upload_xhr = new XMLHttpRequest();"
" upload_xhr.open('POST', uploadUrl, true);"
" upload_xhr.setRequestHeader('Content-Type', 'application/octet-stream');"
" upload_xhr.onload = function() {"
" const responseBody = upload_xhr.responseText;"
" if (upload_xhr.status === 200) {"
" statusMsg.innerHTML = 'Upload successful!';"
" } else {"
" statusMsg.innerHTML='Error uploading!';"
" }"
" responseMsg.innerHTML = responseBody;"
" };"
" statusMsg.innerHTML = 'uploading...';"
" upload_xhr.send(file);"
"}"
"document.getElementById('restart').addEventListener('click', handleRestart);"
"function handleRestart() {"
" const restartUrl = '/api/system/restart';"
" const restart_xhr = new XMLHttpRequest();"
" restart_xhr.open('POST', restartUrl, true);"
" restart_xhr.setRequestHeader('Content-Type', '');"
" restart_xhr.onload = function() {"
" const responseBody = restart_xhr.responseText;"
" responseMsg.innerHTML = responseBody;"
" };"
" restart_xhr.send(null);"
"}"
"</script>"
"</body>"
"</html>";

#endif

0 comments on commit a93c350

Please sign in to comment.