From 16c3b47f621c1c63bd2a998be1b681c141858c26 Mon Sep 17 00:00:00 2001 From: Cassiano Martin Date: Sat, 24 Oct 2026 16:39:08 -0300 Subject: [PATCH] Add Orca Slicer SD upload and print compatibility --- .../modules/http/handlers/handle-command.cpp | 62 ++++++++++++++++++- .../modules/http/handlers/upload-SD-files.cpp | 45 ++++++++++++++ esp3d/src/modules/http/http_server.cpp | 7 +++ esp3d/src/modules/http/http_server.h | 8 +++ 4 files changed, 120 insertions(+), 2 deletions(-) diff --git a/esp3d/src/modules/http/handlers/handle-command.cpp b/esp3d/src/modules/http/handlers/handle-command.cpp index 580694b39..d581e52bf 100644 --- a/esp3d/src/modules/http/handlers/handle-command.cpp +++ b/esp3d/src/modules/http/handlers/handle-command.cpp @@ -31,6 +31,10 @@ #include "../../../core/esp3d_settings.h" #include "../../../core/esp3d_string.h" #include "../../authentication/authentication_service.h" +#if defined(SD_DEVICE) && defined(GCODE_HOST_FEATURE) +#include "../../filesystem/esp_sd.h" +#include "../../gcode_host/gcode_host.h" +#endif // Handle web command query and send answer////////////////////////////// @@ -44,8 +48,62 @@ void HTTP_Server::handle_web_command() { // esp3d_log("Authentication = %d", auth_level); String cmd = ""; bool isRealTimeCommand = false; - if (_webserver->hasArg("cmd")) { - cmd = _webserver->arg("cmd"); +#if defined(SD_DEVICE) && defined(GCODE_HOST_FEATURE) + if (_webserver->hasArg("plain")) { + // Orca's ESP3D host uses "plain" for its test and post-upload commands. + // Its M23/M24 refer to the file just uploaded to ESP3D's own SD card, + // not to a file on the printer firmware's SD card. + cmd = _webserver->arg("plain"); + cmd.trim(); + if (cmd.startsWith("M23 ")) { + String name = cmd.substring(4); + name.trim(); + if (name.length() == 0 || name != _orca_uploaded_name) { + _webserver->send(409, "text/plain", "No matching SD upload"); + return; + } + _orca_selected_name = name; + _webserver->send(200, "text/plain", "File selected"); + return; + } + if (cmd == "M24") { + if (_orca_selected_name.length() == 0) { + _webserver->send(409, "text/plain", "No selected SD upload"); + return; + } + if (esp3d_gcode_host.getStatus() != HOST_NO_STREAM) { + _webserver->send(409, "text/plain", "A stream is already active"); + return; + } + String path = "/" + _orca_selected_name; + if (!ESP_SD::accessFS()) { + _webserver->send(503, "text/plain", "SD card unavailable"); + return; + } + bool exists = ESP_SD::exists(path.c_str()); + ESP_SD::releaseFS(); + if (!exists) { + _webserver->send(404, "text/plain", "Uploaded file not found"); + return; + } + String stream_path = "/SD" + path; + if (!esp3d_gcode_host.processFile(stream_path.c_str(), auth_level)) { + _webserver->send(500, "text/plain", "Cannot start SD stream"); + return; + } + _orca_uploaded_name = ""; + _orca_selected_name = ""; + _webserver->send(200, "text/plain", "ESP3D SD stream started"); + return; + } + if (cmd != "M105") { + _webserver->send(400, "text/plain", "Unsupported plain command"); + return; + } + } +#endif // SD_DEVICE && GCODE_HOST_FEATURE + if (_webserver->hasArg("cmd") || cmd.length() > 0) { + if (cmd.length() == 0) cmd = _webserver->arg("cmd"); esp3d_log("Command is %s", cmd.c_str()); if (!cmd.endsWith("\n")) { esp3d_log("Command is not ending with \\n"); diff --git a/esp3d/src/modules/http/handlers/upload-SD-files.cpp b/esp3d/src/modules/http/handlers/upload-SD-files.cpp index affbfd119..3ae5ff877 100644 --- a/esp3d/src/modules/http/handlers/upload-SD-files.cpp +++ b/esp3d/src/modules/http/handlers/upload-SD-files.cpp @@ -93,6 +93,7 @@ void HTTP_Server::SDFileupload() { ESP_SD::remove(filename.c_str()); } String path = _webserver->arg("path"); + if (path.length() == 0) path = "/"; if (path[0] != '/') { path = "/" + path; } @@ -245,4 +246,48 @@ void HTTP_Server::SDFileupload() { } ESP3DHal::wait(5); } +#ifdef GCODE_HOST_FEATURE +// Orca sends a single multipart field named "file" with a short filename. +// Reject paths: this endpoint always writes to the root of ESP3D's SD card. +static bool isOrcaFilenameValid(const String& name) { + if (name.length() == 0 || name.length() > 64 || name[0] == '.') return false; + for (size_t i = 0; i < name.length(); ++i) { + char c = name[i]; + if (c == '/' || c == '\\' || static_cast(c) < 0x20) return false; + } + return true; +} + +void HTTP_Server::OrcaSDFileupload() { + HTTPUpload& upload = _webserver->upload(); + if (upload.status == UPLOAD_FILE_START) { + _orca_uploaded_name = ""; + _orca_selected_name = ""; + _upload_status = UPLOAD_STATUS_NONE; + } + if (!isOrcaFilenameValid(upload.filename)) { + _upload_status = UPLOAD_STATUS_FAILED; + return; + } + SDFileupload(); + if (upload.status == UPLOAD_FILE_END && + _upload_status == UPLOAD_STATUS_SUCCESSFUL) { + _orca_uploaded_name = upload.filename; + } +} + +void HTTP_Server::handleOrcaUpload() { + if (AuthenticationService::getAuthenticatedLevel() == + ESP3DAuthenticationLevel::guest) { + _webserver->send(401, "text/plain", "Wrong authentication!"); + } else if (_upload_status == UPLOAD_STATUS_SUCCESSFUL && + _orca_uploaded_name.length() > 0) { + _webserver->send(200, "text/plain", "ok"); + } else { + _orca_uploaded_name = ""; + _webserver->send(500, "text/plain", "SD upload failed"); + } + _upload_status = UPLOAD_STATUS_NONE; +} +#endif // GCODE_HOST_FEATURE #endif // HTTP_FEATURE && SD_DEVICE diff --git a/esp3d/src/modules/http/http_server.cpp b/esp3d/src/modules/http/http_server.cpp index 46e794e89..b06b0b7e8 100644 --- a/esp3d/src/modules/http/http_server.cpp +++ b/esp3d/src/modules/http/http_server.cpp @@ -53,6 +53,10 @@ bool HTTP_Server::_started = false; uint16_t HTTP_Server::_port = 0; WEBSERVER* HTTP_Server::_webserver = nullptr; uint8_t HTTP_Server::_upload_status = UPLOAD_STATUS_NONE; +#if defined(SD_DEVICE) && defined(GCODE_HOST_FEATURE) +String HTTP_Server::_orca_uploaded_name; +String HTTP_Server::_orca_selected_name; +#endif void HTTP_Server::init_handlers() { _webserver->on("/", HTTP_ANY, handle_root); @@ -74,6 +78,9 @@ void HTTP_Server::init_handlers() { #ifdef SD_DEVICE // SD _webserver->on("/sdfiles", HTTP_ANY, handleSDFileList, SDFileupload); +#ifdef GCODE_HOST_FEATURE + _webserver->on("/upload_serial", HTTP_POST, handleOrcaUpload, OrcaSDFileupload); +#endif #endif // SD_DEVICE #ifdef WEB_UPDATE_FEATURE // web update diff --git a/esp3d/src/modules/http/http_server.h b/esp3d/src/modules/http/http_server.h index c0115ccdc..a76205827 100644 --- a/esp3d/src/modules/http/http_server.h +++ b/esp3d/src/modules/http/http_server.h @@ -60,6 +60,10 @@ class HTTP_Server { static WEBSERVER* _webserver; static uint16_t _port; static uint8_t _upload_status; +#if defined(SD_DEVICE) && defined(GCODE_HOST_FEATURE) + static String _orca_uploaded_name; + static String _orca_selected_name; +#endif static const char* get_Splited_Value(String data, char separator, int index); #ifdef SSDP_FEATURE static void handle_SSDP(); @@ -89,6 +93,10 @@ class HTTP_Server { #ifdef SD_DEVICE static void SDFileupload(); static void handleSDFileList(); +#ifdef GCODE_HOST_FEATURE + static void OrcaSDFileupload(); + static void handleOrcaUpload(); +#endif static bool StreamSDFile(const char* filename, const char* contentType); #endif // SD_DEVICE #if COMMUNICATION_PROTOCOL == MKS_SERIAL