Skip to content

Commit

Permalink
Merge pull request #612 from Paciente8159/fs-page-update-endpoint
Browse files Browse the repository at this point in the history
Fs page update endpoint
  • Loading branch information
Paciente8159 authored Feb 10, 2024
2 parents ec2d50f + 5d1986c commit f4597e9
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 7 deletions.
68 changes: 67 additions & 1 deletion uCNC/src/hal/mcus/esp32/esp32_arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,19 @@ uint16_t bt_settings_offset;
#define WIFI_PASS "pass"
#endif

#ifndef OTA_URI
#define OTA_URI "/firmware"
#endif

#ifndef FS_WRITE_URI
#define FS_WRITE_URI "/fs"
#endif
#define FS_WRITE_GZ_SIZE 305
const char fs_write_page[305] PROGMEM = {0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0a, 0x55, 0x51, 0x3d, 0x4f, 0xc3, 0x30, 0x10, 0xdd, 0x91, 0xf8, 0x0f, 0x87, 0x67, 0x52, 0x43, 0x27, 0x84, 0xec, 0x2c, 0x85, 0x4a, 0x4c, 0x74, 0x68, 0x85, 0x18, 0x2f, 0xf6, 0xb5, 0xb1, 0xe4, 0xd8, 0x56, 0x72, 0x69, 0x55, 0x7e, 0x3d, 0x97, 0xa4, 0x03, 0x0c, 0xfe, 0x7a, 0xf7, 0xee, 0xdd, 0xd3, 0xb3, 0x79, 0x78, 0xfb, 0xdc, 0xec, 0xbf, 0x77, 0xef, 0xd0, 0x72, 0x17, 0x6b, 0x33, 0xed, 0x10, 0x31, 0x9d, 0xac, 0xa2, 0xa4, 0xe4, 0x4d, 0xe8, 0x6b, 0xd3, 0x11, 0x23, 0xb8, 0x16, 0xfb, 0x81, 0xd8, 0xaa, 0xc3, 0x7e, 0x5b, 0xbd, 0xa8, 0x1b, 0x9a, 0xb0, 0x23, 0xab, 0xce, 0x81, 0x2e, 0x25, 0xf7, 0xac, 0xc0, 0xe5, 0xc4, 0x94, 0x84, 0x75, 0x09, 0x9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0xbf, 0x01, 0x00, 0x00};

WebServer web_server(WEBSERVER_PORT);
HTTPUpdateServer httpUpdater;
const char *update_path = "/firmware";
const char *update_path = OTA_URI;
const char *update_username = WIFI_USER;
const char *update_password = WIFI_PASS;
#define MAX_SRV_CLIENTS 1
Expand Down Expand Up @@ -405,6 +415,46 @@ extern "C"
#include <SPIFFS.h>
#define FLASH_FS SPIFFS
#endif
static File upload_file;

void fs_file_updater()
{
if (web_server.uri() != FS_WRITE_URI || web_server.method() != HTTP_POST || !web_server.hasArg("update"))
{
return;
}

HTTPUpload &upload = web_server.upload();
if (upload.status == UPLOAD_FILE_START)
{
String filename = upload.filename;
if (web_server.hasArg("path"))
{
String path = web_server.arg("path");
filename = path + ((!filename.startsWith("/") && !path.startsWith("/")) ? "/" : "") + filename;
}
if (!filename.startsWith("/"))
{
filename = "/" + filename;
}
upload_file = FLASH_FS.open(filename, "w");
filename = String();
}
else if (upload.status == UPLOAD_FILE_WRITE)
{
if (upload_file)
{
upload_file.write(upload.buf, upload.currentSize);
}
}
else if (upload.status == UPLOAD_FILE_END)
{
if (upload_file)
{
upload_file.close();
}
}
}

void endpoint_add(const char *uri, uint8_t method, endpoint_delegate request_handler, endpoint_delegate file_handler)
{
Expand Down Expand Up @@ -555,6 +605,22 @@ extern "C"
httpUpdater.setup(&web_server, update_path, update_username, update_password);
#endif
FLASH_FS.begin();
web_server.on(
FS_WRITE_URI, HTTP_GET, []()
{ web_server.sendHeader("Content-Encoding", "gzip");
web_server.send_P(200, __romstr__("text/html"), fs_write_page, FS_WRITE_GZ_SIZE); },
NULL);
web_server.on(
FS_WRITE_URI, HTTP_POST, []()
{
if(web_server.hasArg("redirect")){
web_server.sendHeader("Location", web_server.arg("redirect"));
web_server.send(303);
}
else{
web_server.send(200, "text/plain", "");
} },
fs_file_updater);
web_server.begin();
#ifdef MCU_HAS_WEBSOCKETS
socket_server.begin();
Expand Down
70 changes: 68 additions & 2 deletions uCNC/src/hal/mcus/esp8266/esp8266_arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,20 @@

#define ARG_MAX_LEN WIFI_SSID_MAX_LEN

#ifndef OTA_URI
#define OTA_URI "/firmware"
#endif

#ifndef FS_WRITE_URI
#define FS_WRITE_URI "/fs"
#endif
#define FS_WRITE_GZ_SIZE 305
const char fs_write_page[305] PROGMEM = {0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0a, 0x55, 0x51, 0x3d, 0x4f, 0xc3, 0x30, 0x10, 0xdd, 0x91, 0xf8, 0x0f, 0x87, 0x67, 0x52, 0x43, 0x27, 0x84, 0xec, 0x2c, 0x85, 0x4a, 0x4c, 0x74, 0x68, 0x85, 0x18, 0x2f, 0xf6, 0xb5, 0xb1, 0xe4, 0xd8, 0x56, 0x72, 0x69, 0x55, 0x7e, 0x3d, 0x97, 0xa4, 0x03, 0x0c, 0xfe, 0x7a, 0xf7, 0xee, 0xdd, 0xd3, 0xb3, 0x79, 0x78, 0xfb, 0xdc, 0xec, 0xbf, 0x77, 0xef, 0xd0, 0x72, 0x17, 0x6b, 0x33, 0xed, 0x10, 0x31, 0x9d, 0xac, 0xa2, 0xa4, 0xe4, 0x4d, 0xe8, 0x6b, 0xd3, 0x11, 0x23, 0xb8, 0x16, 0xfb, 0x81, 0xd8, 0xaa, 0xc3, 0x7e, 0x5b, 0xbd, 0xa8, 0x1b, 0x9a, 0xb0, 0x23, 0xab, 0xce, 0x81, 0x2e, 0x25, 0xf7, 0xac, 0xc0, 0xe5, 0xc4, 0x94, 0x84, 0x75, 0x09, 0x9e, 0x5b, 0xeb, 0xe9, 0x1c, 0x1c, 0x55, 0xf3, 0xe3, 0x11, 0x42, 0x0a, 0x1c, 0x30, 0x56, 0x83, 0xc3, 0x48, 0xf6, 0x79, 0xf5, 0x24, 0x2a, 0x1c, 0x38, 0x52, 0xfd, 0x45, 0x0d, 0xec, 0x28, 0x79, 0x4c, 0x0c, 0x63, 0xf1, 0xc8, 0x64, 0xf4, 0x52, 0x31, 0x7a, 0xf1, 0xd0, 0x64, 0x7f, 0x15, 0x3f, 0xeb, 0x7f, 0xd4, 0xc3, 0x4c, 0x85, 0x6d, 0xee, 0x3b, 0xe1, 0xad, 0x6b, 0x73, 0x94, 0x1b, 0xa0, 0xe3, 0x90, 0x93, 0x55, 0xfa, 0x38, 0x28, 0x10, 0x97, 0x6d, 0xf6, 0x56, 0x95, 0x3c, 0x88, 0x3d, 0x4a, 0x8e, 0xaf, 0x45, 0x1c, 0x77, 0x63, 0xe4, 0x50, 0xb0, 0x67, 0x3d, 0xb5, 0x54, 0x22, 0x83, 0x62, 0x26, 0x62, 0x43, 0x11, 0x04, 0xb1, 0xea, 0x18, 0x22, 0x7d, 0xa4, 0x32, 0xb2, 0xaa, 0x37, 0x6d, 0xce, 0x03, 0x01, 0xc2, 0xea, 0xf4, 0x03, 0x13, 0xfe, 0x6a, 0xf4, 0xcc, 0xac, 0x4d, 0x98, 0x18, 0xb0, 0x48, 0x4e, 0x15, 0x05, 0xc1, 0xff, 0xed, 0xbd, 0xe5, 0xf3, 0x07, 0x40, 0xe7, 0xa8, 0x48, 0x3e, 0xa2, 0x25, 0x03, 0x9b, 0x5e, 0xd6, 0xc8, 0x9c, 0xd3, 0x4d, 0x64, 0x18, 0x9b, 0x2e, 0xc8, 0xcc, 0x43, 0x89, 0x19, 0xbd, 0xd1, 0x4b, 0x51, 0x52, 0x98, 0x6c, 0xca, 0xb1, 0xc4, 0xa0, 0xe7, 0xdf, 0xba, 0xbf, 0xfb, 0x05, 0x44, 0x67, 0x16, 0x56, 0xbf, 0x01, 0x00, 0x00};


ESP8266WebServer web_server(WEBSERVER_PORT);
ESP8266HTTPUpdateServer httpUpdater;
const char *update_path = "/firmware";
const char *update_path = OTA_URI;
const char *update_username = WIFI_USER;
const char *update_password = WIFI_PASS;
#define MAX_SRV_CLIENTS 1
Expand Down Expand Up @@ -367,6 +378,45 @@ extern "C"
#include <SPIFFS.h>
#define FLASH_FS SPIFFS
#endif
static File upload_file;
void fs_file_updater()
{
if (web_server.uri() != FS_WRITE_URI || web_server.method() != HTTP_POST || !web_server.hasArg("update"))
{
return;
}

HTTPUpload &upload = web_server.upload();
if (upload.status == UPLOAD_FILE_START)
{
String filename = upload.filename;
if (web_server.hasArg("path"))
{
String path = web_server.arg("path");
filename = path + ((!filename.startsWith("/") && !path.startsWith("/")) ? "/" : "") + filename;
}
if (!filename.startsWith("/"))
{
filename = "/" + filename;
}
upload_file = FLASH_FS.open(filename, "w");
filename = String();
}
else if (upload.status == UPLOAD_FILE_WRITE)
{
if (upload_file)
{
upload_file.write(upload.buf, upload.currentSize);
}
}
else if (upload.status == UPLOAD_FILE_END)
{
if (upload_file)
{
upload_file.close();
}
}
}

// call to the webserver initializer
void endpoint_add(const char *uri, uint8_t method, endpoint_delegate request_handler, endpoint_delegate file_handler)
Expand Down Expand Up @@ -559,8 +609,24 @@ extern "C"
#ifndef CUSTOM_OTA_ENDPOINT
httpUpdater.setup(&web_server, update_path, update_username, update_password);
#endif
web_server.on(
FS_WRITE_URI, HTTP_GET, []()
{ web_server.sendHeader("Content-Encoding", "gzip");
web_server.send_P(200, __romstr__("text/html"), fs_write_page, FS_WRITE_GZ_SIZE); },
NULL);
web_server.on(
FS_WRITE_URI, HTTP_POST, []()
{
if(web_server.hasArg("redirect")){
web_server.sendHeader("Location", web_server.arg("redirect"));
web_server.send(303);
}
else{
web_server.send(200, "text/plain", "");
} },
fs_file_updater);
web_server.begin();

#ifdef MCU_HAS_WEBSOCKETS
socket_server.begin();
socket_server.onEvent(webSocketEvent);
Expand Down
2 changes: 1 addition & 1 deletion uCNC/src/hal/mcus/rp2040/mcu_rp2040.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ void mcu_init(void)
#endif

#ifndef RAM_ONLY_SETTINGS
rp2040_eeprom_init(1024); // 1K Emulated EEPROM
rp2040_eeprom_init(2048); // 2K Emulated EEPROM
#endif
#ifdef MCU_HAS_SPI
mcu_spi_config(SPI_FREQ, SPI_MODE);
Expand Down
71 changes: 68 additions & 3 deletions uCNC/src/hal/mcus/rp2040/rp2040_arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,18 @@ uint16_t bt_settings_offset;
#define WIFI_PASS "pass\0"
#endif

#ifndef OTA_URI
#define OTA_URI "/firmware"
#endif

#ifndef FS_WRITE_URI
#define FS_WRITE_URI "/fs"
#endif
#define FS_WRITE_GZ_SIZE 305
const char fs_write_page[305] PROGMEM = {0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0a, 0x55, 0x51, 0x3d, 0x4f, 0xc3, 0x30, 0x10, 0xdd, 0x91, 0xf8, 0x0f, 0x87, 0x67, 0x52, 0x43, 0x27, 0x84, 0xec, 0x2c, 0x85, 0x4a, 0x4c, 0x74, 0x68, 0x85, 0x18, 0x2f, 0xf6, 0xb5, 0xb1, 0xe4, 0xd8, 0x56, 0x72, 0x69, 0x55, 0x7e, 0x3d, 0x97, 0xa4, 0x03, 0x0c, 0xfe, 0x7a, 0xf7, 0xee, 0xdd, 0xd3, 0xb3, 0x79, 0x78, 0xfb, 0xdc, 0xec, 0xbf, 0x77, 0xef, 0xd0, 0x72, 0x17, 0x6b, 0x33, 0xed, 0x10, 0x31, 0x9d, 0xac, 0xa2, 0xa4, 0xe4, 0x4d, 0xe8, 0x6b, 0xd3, 0x11, 0x23, 0xb8, 0x16, 0xfb, 0x81, 0xd8, 0xaa, 0xc3, 0x7e, 0x5b, 0xbd, 0xa8, 0x1b, 0x9a, 0xb0, 0x23, 0xab, 0xce, 0x81, 0x2e, 0x25, 0xf7, 0xac, 0xc0, 0xe5, 0xc4, 0x94, 0x84, 0x75, 0x09, 0x9e, 0x5b, 0xeb, 0xe9, 0x1c, 0x1c, 0x55, 0xf3, 0xe3, 0x11, 0x42, 0x0a, 0x1c, 0x30, 0x56, 0x83, 0xc3, 0x48, 0xf6, 0x79, 0xf5, 0x24, 0x2a, 0x1c, 0x38, 0x52, 0xfd, 0x45, 0x0d, 0xec, 0x28, 0x79, 0x4c, 0x0c, 0x63, 0xf1, 0xc8, 0x64, 0xf4, 0x52, 0x31, 0x7a, 0xf1, 0xd0, 0x64, 0x7f, 0x15, 0x3f, 0xeb, 0x7f, 0xd4, 0xc3, 0x4c, 0x85, 0x6d, 0xee, 0x3b, 0xe1, 0xad, 0x6b, 0x73, 0x94, 0x1b, 0xa0, 0xe3, 0x90, 0x93, 0x55, 0xfa, 0x38, 0x28, 0x10, 0x97, 0x6d, 0xf6, 0x56, 0x95, 0x3c, 0x88, 0x3d, 0x4a, 0x8e, 0xaf, 0x45, 0x1c, 0x77, 0x63, 0xe4, 0x50, 0xb0, 0x67, 0x3d, 0xb5, 0x54, 0x22, 0x83, 0x62, 0x26, 0x62, 0x43, 0x11, 0x04, 0xb1, 0xea, 0x18, 0x22, 0x7d, 0xa4, 0x32, 0xb2, 0xaa, 0x37, 0x6d, 0xce, 0x03, 0x01, 0xc2, 0xea, 0xf4, 0x03, 0x13, 0xfe, 0x6a, 0xf4, 0xcc, 0xac, 0x4d, 0x98, 0x18, 0xb0, 0x48, 0x4e, 0x15, 0x05, 0xc1, 0xff, 0xed, 0xbd, 0xe5, 0xf3, 0x07, 0x40, 0xe7, 0xa8, 0x48, 0x3e, 0xa2, 0x25, 0x03, 0x9b, 0x5e, 0xd6, 0xc8, 0x9c, 0xd3, 0x4d, 0x64, 0x18, 0x9b, 0x2e, 0xc8, 0xcc, 0x43, 0x89, 0x19, 0xbd, 0xd1, 0x4b, 0x51, 0x52, 0x98, 0x6c, 0xca, 0xb1, 0xc4, 0xa0, 0xe7, 0xdf, 0xba, 0xbf, 0xfb, 0x05, 0x44, 0x67, 0x16, 0x56, 0xbf, 0x01, 0x00, 0x00};

WebServer web_server(WEBSERVER_PORT);
HTTPUpdateServer httpUpdater;
const char *update_path = "/firmware";
const char *update_username = WIFI_USER;
const char *update_password = WIFI_PASS;
#define MAX_SRV_CLIENTS 1
Expand Down Expand Up @@ -406,13 +415,54 @@ bool rp2040_wifi_clientok(void)
#include <SPIFFS.h>
#define FLASH_FS SPIFFS
#endif
static File upload_file;

void fs_file_updater()
{
if (web_server.uri() != FS_WRITE_URI || web_server.method() != HTTP_POST || !web_server.hasArg("update"))
{
return;
}

HTTPUpload &upload = web_server.upload();
if (upload.status == UPLOAD_FILE_START)
{
String filename = upload.filename;
if (web_server.hasArg("path"))
{
String path = web_server.arg("path");
filename = path + ((!filename.startsWith("/") && !path.startsWith("/")) ? "/" : "") + filename;
}
if (!filename.startsWith("/"))
{
filename = "/" + filename;
}
upload_file = FLASH_FS.open(filename, "w");
filename = String();
}
else if (upload.status == UPLOAD_FILE_WRITE)
{
if (upload_file)
{
upload_file.write(upload.buf, upload.currentSize);
}
}
else if (upload.status == UPLOAD_FILE_END)
{
if (upload_file)
{
upload_file.close();
}
}
}

void endpoint_add(const char *uri, uint8_t method, endpoint_delegate request_handler, endpoint_delegate file_handler)
{
if (!method)
{
method = 255;
}

web_server.on(uri, (HTTPMethod)method, request_handler, file_handler);
}

Expand Down Expand Up @@ -596,8 +646,24 @@ void rp2040_wifi_bt_init(void)
FLASH_FS.begin();
#endif
#ifndef CUSTOM_OTA_ENDPOINT
httpUpdater.setup(&web_server, update_path, update_username, update_password);
httpUpdater.setup(&web_server, OTA_URI, update_username, update_password);
#endif
web_server.on(
FS_WRITE_URI, HTTP_GET, []()
{ web_server.sendHeader("Content-Encoding", "gzip");
web_server.send_P(200, __romstr__("text/html"), fs_write_page, FS_WRITE_GZ_SIZE); },
NULL);
web_server.on(
FS_WRITE_URI, HTTP_POST, []()
{
if(web_server.hasArg("redirect")){
web_server.sendHeader("Location", web_server.arg("redirect"));
web_server.send(303);
}
else{
web_server.send(200, "text/plain", "");
} },
fs_file_updater);
web_server.begin();

#ifdef MCU_HAS_WEBSOCKETS
Expand Down Expand Up @@ -741,7 +807,6 @@ uint8_t rp2040_wifi_bt_read(void)
return (uint8_t)0;
}


void rp2040_wifi_bt_process(void)
{
#ifdef MCU_HAS_WIFI
Expand Down

0 comments on commit f4597e9

Please sign in to comment.