mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-07 08:07:40 +03:00
Migrate to ESPAsyncWebServer for ~3-5x faster uploads
Replace synchronous WebServer with ESPAsyncWebServer: - Fully callback-based, no polling loop needed - Handles HTTP parsing asynchronously in background - Larger chunk sizes from async TCP handling - Known to achieve 500KB-1MB/s vs 200KB/s with sync WebServer Key changes: - platformio.ini: Add ESP Async WebServer dependency - CrossPointWebServer: Rewrite for async API - CrossPointWebServerActivity: Remove handleClient loop The sync WebServer was capped at ~200KB/s due to its synchronous design and small chunk sizes.
This commit is contained in:
parent
865ee21b64
commit
c75cc5cd31
@ -52,6 +52,7 @@ lib_deps =
|
|||||||
SDCardManager=symlink://open-x4-sdk/libs/hardware/SDCardManager
|
SDCardManager=symlink://open-x4-sdk/libs/hardware/SDCardManager
|
||||||
ArduinoJson @ 7.4.2
|
ArduinoJson @ 7.4.2
|
||||||
QRCode @ 0.0.1
|
QRCode @ 0.0.1
|
||||||
|
ESP Async WebServer @ ^3.4.5
|
||||||
|
|
||||||
[env:default]
|
[env:default]
|
||||||
extends = base
|
extends = base
|
||||||
|
|||||||
@ -26,14 +26,9 @@ constexpr uint8_t AP_MAX_CONNECTIONS = 4;
|
|||||||
DNSServer* dnsServer = nullptr;
|
DNSServer* dnsServer = nullptr;
|
||||||
constexpr uint16_t DNS_PORT = 53;
|
constexpr uint16_t DNS_PORT = 53;
|
||||||
|
|
||||||
// Task configuration for high-performance uploads
|
// Task configuration for display updates
|
||||||
constexpr uint32_t WEBSERVER_TASK_STACK_SIZE = 6144; // 6KB stack for upload handling
|
constexpr uint32_t WEBSERVER_TASK_STACK_SIZE = 4096; // 4KB stack for display task
|
||||||
constexpr UBaseType_t WEBSERVER_TASK_PRIORITY = 5; // Higher priority for responsiveness
|
constexpr UBaseType_t WEBSERVER_TASK_PRIORITY = 1; // Low priority - display updates only
|
||||||
|
|
||||||
// WiFi performance: handleClient iterations per loop
|
|
||||||
// Higher values improve upload throughput by processing more data per frame
|
|
||||||
// With 200 iterations we can process ~300KB per loop at 1.5KB/chunk
|
|
||||||
constexpr int HANDLE_CLIENT_ITERATIONS = 200;
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
// Apply WiFi performance optimizations for maximum upload throughput
|
// Apply WiFi performance optimizations for maximum upload throughput
|
||||||
@ -333,25 +328,8 @@ void CrossPointWebServerActivity::loop() {
|
|||||||
dnsServer->processNextRequest();
|
dnsServer->processNextRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle web server requests - call handleClient multiple times per loop
|
// AsyncWebServer is fully callback-based - no polling needed
|
||||||
// to improve responsiveness and upload throughput
|
// The server handles requests automatically in the background
|
||||||
if (webServer && webServer->isRunning()) {
|
|
||||||
const unsigned long timeSinceLastHandleClient = millis() - lastHandleClientTime;
|
|
||||||
|
|
||||||
// Log if there's a significant gap between handleClient calls (>100ms)
|
|
||||||
if (lastHandleClientTime > 0 && timeSinceLastHandleClient > 100) {
|
|
||||||
Serial.printf("[%lu] [WEBACT] WARNING: %lu ms gap since last handleClient\n", millis(),
|
|
||||||
timeSinceLastHandleClient);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call handleClient multiple times to process pending requests faster
|
|
||||||
// This is critical for upload performance - HTTP file uploads send data
|
|
||||||
// in chunks and each handleClient() call processes incoming data
|
|
||||||
for (int i = 0; i < HANDLE_CLIENT_ITERATIONS && webServer->isRunning(); i++) {
|
|
||||||
webServer->handleClient();
|
|
||||||
}
|
|
||||||
lastHandleClientTime = millis();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle exit on Back button
|
// Handle exit on Back button
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
||||||
|
|||||||
@ -16,10 +16,6 @@ namespace {
|
|||||||
const char* HIDDEN_ITEMS[] = {"System Volume Information", "XTCache"};
|
const char* HIDDEN_ITEMS[] = {"System Volume Information", "XTCache"};
|
||||||
constexpr size_t HIDDEN_ITEMS_COUNT = sizeof(HIDDEN_ITEMS) / sizeof(HIDDEN_ITEMS[0]);
|
constexpr size_t HIDDEN_ITEMS_COUNT = sizeof(HIDDEN_ITEMS) / sizeof(HIDDEN_ITEMS[0]);
|
||||||
|
|
||||||
// CPU frequency for upload boost (240MHz for maximum performance)
|
|
||||||
constexpr uint32_t UPLOAD_CPU_FREQ_MHZ = 240;
|
|
||||||
constexpr uint32_t NORMAL_CPU_FREQ_MHZ = 160;
|
|
||||||
|
|
||||||
// Speed calculation interval
|
// Speed calculation interval
|
||||||
constexpr unsigned long SPEED_CALC_INTERVAL_MS = 500;
|
constexpr unsigned long SPEED_CALC_INTERVAL_MS = 500;
|
||||||
} // namespace
|
} // namespace
|
||||||
@ -37,25 +33,6 @@ CrossPointWebServer::~CrossPointWebServer() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CPU frequency management
|
|
||||||
void CrossPointWebServer::boostCPU() const {
|
|
||||||
if (cpuBoosted) return;
|
|
||||||
|
|
||||||
if (setCpuFrequencyMhz(UPLOAD_CPU_FREQ_MHZ)) {
|
|
||||||
cpuBoosted = true;
|
|
||||||
Serial.printf("[%lu] [WEB] [UPLOAD] CPU boosted to %dMHz\n", millis(), UPLOAD_CPU_FREQ_MHZ);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void CrossPointWebServer::restoreCPU() const {
|
|
||||||
if (!cpuBoosted) return;
|
|
||||||
|
|
||||||
if (setCpuFrequencyMhz(NORMAL_CPU_FREQ_MHZ)) {
|
|
||||||
cpuBoosted = false;
|
|
||||||
Serial.printf("[%lu] [WEB] [UPLOAD] CPU restored to %dMHz\n", millis(), NORMAL_CPU_FREQ_MHZ);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Thread-safe upload status getters
|
// Thread-safe upload status getters
|
||||||
bool CrossPointWebServer::isUploading() const {
|
bool CrossPointWebServer::isUploading() const {
|
||||||
if (!uploadMutex) return false;
|
if (!uploadMutex) return false;
|
||||||
@ -115,44 +92,48 @@ void CrossPointWebServer::begin() {
|
|||||||
Serial.printf("[%lu] [WEB] [MEM] Free heap before begin: %d bytes\n", millis(), ESP.getFreeHeap());
|
Serial.printf("[%lu] [WEB] [MEM] Free heap before begin: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||||
Serial.printf("[%lu] [WEB] Network mode: %s\n", millis(), apMode ? "AP" : "STA");
|
Serial.printf("[%lu] [WEB] Network mode: %s\n", millis(), apMode ? "AP" : "STA");
|
||||||
|
|
||||||
Serial.printf("[%lu] [WEB] Creating web server on port %d...\n", millis(), port);
|
Serial.printf("[%lu] [WEB] Creating AsyncWebServer on port %d...\n", millis(), port);
|
||||||
server.reset(new WebServer(port));
|
server.reset(new AsyncWebServer(port));
|
||||||
|
|
||||||
// Disable WiFi sleep to improve responsiveness and prevent 'unreachable' errors.
|
// Disable WiFi sleep to improve responsiveness
|
||||||
// This is critical for reliable web server operation on ESP32.
|
|
||||||
WiFi.setSleep(false);
|
WiFi.setSleep(false);
|
||||||
|
|
||||||
Serial.printf("[%lu] [WEB] [MEM] Free heap after WebServer allocation: %d bytes\n", millis(), ESP.getFreeHeap());
|
Serial.printf("[%lu] [WEB] [MEM] Free heap after AsyncWebServer allocation: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||||
|
|
||||||
if (!server) {
|
if (!server) {
|
||||||
Serial.printf("[%lu] [WEB] Failed to create WebServer!\n", millis());
|
Serial.printf("[%lu] [WEB] Failed to create AsyncWebServer!\n", millis());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup routes
|
// Setup routes
|
||||||
Serial.printf("[%lu] [WEB] Setting up routes...\n", millis());
|
Serial.printf("[%lu] [WEB] Setting up routes...\n", millis());
|
||||||
server->on("/", HTTP_GET, [this] { handleRoot(); });
|
|
||||||
server->on("/files", HTTP_GET, [this] { handleFileList(); });
|
|
||||||
|
|
||||||
server->on("/api/status", HTTP_GET, [this] { handleStatus(); });
|
server->on("/", HTTP_GET, [this](AsyncWebServerRequest* request) { handleRoot(request); });
|
||||||
server->on("/api/files", HTTP_GET, [this] { handleFileListData(); });
|
|
||||||
|
|
||||||
// Upload endpoint with special handling for multipart form data
|
server->on("/files", HTTP_GET, [this](AsyncWebServerRequest* request) { handleFileList(request); });
|
||||||
server->on("/upload", HTTP_POST, [this] { handleUploadPost(); }, [this] { handleUpload(); });
|
|
||||||
|
|
||||||
// Create folder endpoint
|
server->on("/api/status", HTTP_GET, [this](AsyncWebServerRequest* request) { handleStatus(request); });
|
||||||
server->on("/mkdir", HTTP_POST, [this] { handleCreateFolder(); });
|
|
||||||
|
|
||||||
// Delete file/folder endpoint
|
server->on("/api/files", HTTP_GET, [this](AsyncWebServerRequest* request) { handleFileListData(request); });
|
||||||
server->on("/delete", HTTP_POST, [this] { handleDelete(); });
|
|
||||||
|
// Upload endpoint - AsyncWebServer handles multipart parsing automatically
|
||||||
|
server->on(
|
||||||
|
"/upload", HTTP_POST, [this](AsyncWebServerRequest* request) { handleUploadRequest(request); },
|
||||||
|
[this](AsyncWebServerRequest* request, const String& filename, size_t index, uint8_t* data, size_t len,
|
||||||
|
bool final) { handleUpload(request, filename, index, data, len, final); });
|
||||||
|
|
||||||
|
server->on("/mkdir", HTTP_POST, [this](AsyncWebServerRequest* request) { handleCreateFolder(request); });
|
||||||
|
|
||||||
|
server->on("/delete", HTTP_POST, [this](AsyncWebServerRequest* request) { handleDelete(request); });
|
||||||
|
|
||||||
|
server->onNotFound([this](AsyncWebServerRequest* request) { handleNotFound(request); });
|
||||||
|
|
||||||
server->onNotFound([this] { handleNotFound(); });
|
|
||||||
Serial.printf("[%lu] [WEB] [MEM] Free heap after route setup: %d bytes\n", millis(), ESP.getFreeHeap());
|
Serial.printf("[%lu] [WEB] [MEM] Free heap after route setup: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||||
|
|
||||||
server->begin();
|
server->begin();
|
||||||
running = true;
|
running = true;
|
||||||
|
|
||||||
Serial.printf("[%lu] [WEB] Web server started on port %d\n", millis(), port);
|
Serial.printf("[%lu] [WEB] AsyncWebServer started on port %d\n", millis(), port);
|
||||||
// Show the correct IP based on network mode
|
// Show the correct IP based on network mode
|
||||||
const String ipAddr = apMode ? WiFi.softAPIP().toString() : WiFi.localIP().toString();
|
const String ipAddr = apMode ? WiFi.softAPIP().toString() : WiFi.localIP().toString();
|
||||||
Serial.printf("[%lu] [WEB] Access at http://%s/\n", millis(), ipAddr.c_str());
|
Serial.printf("[%lu] [WEB] Access at http://%s/\n", millis(), ipAddr.c_str());
|
||||||
@ -166,64 +147,31 @@ void CrossPointWebServer::stop() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.printf("[%lu] [WEB] STOP INITIATED - setting running=false first\n", millis());
|
Serial.printf("[%lu] [WEB] STOP INITIATED\n", millis());
|
||||||
running = false; // Set this FIRST to prevent handleClient from using server
|
running = false;
|
||||||
|
|
||||||
Serial.printf("[%lu] [WEB] [MEM] Free heap before stop: %d bytes\n", millis(), ESP.getFreeHeap());
|
Serial.printf("[%lu] [WEB] [MEM] Free heap before stop: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||||
|
|
||||||
// Add delay to allow any in-flight handleClient() calls to complete
|
server->end();
|
||||||
delay(100);
|
Serial.printf("[%lu] [WEB] [MEM] Free heap after server->end(): %d bytes\n", millis(), ESP.getFreeHeap());
|
||||||
Serial.printf("[%lu] [WEB] Waited 100ms for handleClient to finish\n", millis());
|
|
||||||
|
|
||||||
server->stop();
|
|
||||||
Serial.printf("[%lu] [WEB] [MEM] Free heap after server->stop(): %d bytes\n", millis(), ESP.getFreeHeap());
|
|
||||||
|
|
||||||
// Add another delay before deletion to ensure server->stop() completes
|
|
||||||
delay(50);
|
|
||||||
Serial.printf("[%lu] [WEB] Waited 50ms before deleting server\n", millis());
|
|
||||||
|
|
||||||
server.reset();
|
server.reset();
|
||||||
Serial.printf("[%lu] [WEB] Web server stopped and deleted\n", millis());
|
Serial.printf("[%lu] [WEB] AsyncWebServer stopped and deleted\n", millis());
|
||||||
Serial.printf("[%lu] [WEB] [MEM] Free heap after delete server: %d bytes\n", millis(), ESP.getFreeHeap());
|
|
||||||
|
|
||||||
Serial.printf("[%lu] [WEB] [MEM] Free heap final: %d bytes\n", millis(), ESP.getFreeHeap());
|
Serial.printf("[%lu] [WEB] [MEM] Free heap final: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||||
}
|
}
|
||||||
|
|
||||||
void CrossPointWebServer::handleClient() const {
|
void CrossPointWebServer::handleRoot(AsyncWebServerRequest* request) const {
|
||||||
static unsigned long lastDebugPrint = 0;
|
request->send(200, "text/html", HomePageHtml);
|
||||||
|
|
||||||
// Check running flag FIRST before accessing server
|
|
||||||
if (!running) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Double-check server pointer is valid
|
|
||||||
if (!server) {
|
|
||||||
Serial.printf("[%lu] [WEB] WARNING: handleClient called with null server!\n", millis());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print debug every 10 seconds to confirm handleClient is being called
|
|
||||||
if (millis() - lastDebugPrint > 10000) {
|
|
||||||
Serial.printf("[%lu] [WEB] handleClient active, server running on port %d\n", millis(), port);
|
|
||||||
lastDebugPrint = millis();
|
|
||||||
}
|
|
||||||
|
|
||||||
server->handleClient();
|
|
||||||
}
|
|
||||||
|
|
||||||
void CrossPointWebServer::handleRoot() const {
|
|
||||||
server->send(200, "text/html", HomePageHtml);
|
|
||||||
Serial.printf("[%lu] [WEB] Served root page\n", millis());
|
Serial.printf("[%lu] [WEB] Served root page\n", millis());
|
||||||
}
|
}
|
||||||
|
|
||||||
void CrossPointWebServer::handleNotFound() const {
|
void CrossPointWebServer::handleNotFound(AsyncWebServerRequest* request) const {
|
||||||
String message = "404 Not Found\n\n";
|
String message = "404 Not Found\n\n";
|
||||||
message += "URI: " + server->uri() + "\n";
|
message += "URI: " + request->url() + "\n";
|
||||||
server->send(404, "text/plain", message);
|
request->send(404, "text/plain", message);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CrossPointWebServer::handleStatus() const {
|
void CrossPointWebServer::handleStatus(AsyncWebServerRequest* request) const {
|
||||||
// Get correct IP based on AP vs STA mode
|
// Get correct IP based on AP vs STA mode
|
||||||
const String ipAddr = apMode ? WiFi.softAPIP().toString() : WiFi.localIP().toString();
|
const String ipAddr = apMode ? WiFi.softAPIP().toString() : WiFi.localIP().toString();
|
||||||
|
|
||||||
@ -237,7 +185,7 @@ void CrossPointWebServer::handleStatus() const {
|
|||||||
|
|
||||||
String json;
|
String json;
|
||||||
serializeJson(doc, json);
|
serializeJson(doc, json);
|
||||||
server->send(200, "application/json", json);
|
request->send(200, "application/json", json);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CrossPointWebServer::scanFiles(const char* path, const std::function<void(FileInfo)>& callback) const {
|
void CrossPointWebServer::scanFiles(const char* path, const std::function<void(FileInfo)>& callback) const {
|
||||||
@ -291,7 +239,6 @@ void CrossPointWebServer::scanFiles(const char* path, const std::function<void(F
|
|||||||
}
|
}
|
||||||
|
|
||||||
file.close();
|
file.close();
|
||||||
yield(); // Yield to allow WiFi and other tasks to process during long scans
|
|
||||||
file = root.openNextFile();
|
file = root.openNextFile();
|
||||||
}
|
}
|
||||||
root.close();
|
root.close();
|
||||||
@ -303,13 +250,15 @@ bool CrossPointWebServer::isEpubFile(const String& filename) const {
|
|||||||
return lower.endsWith(".epub");
|
return lower.endsWith(".epub");
|
||||||
}
|
}
|
||||||
|
|
||||||
void CrossPointWebServer::handleFileList() const { server->send(200, "text/html", FilesPageHtml); }
|
void CrossPointWebServer::handleFileList(AsyncWebServerRequest* request) const {
|
||||||
|
request->send(200, "text/html", FilesPageHtml);
|
||||||
|
}
|
||||||
|
|
||||||
void CrossPointWebServer::handleFileListData() const {
|
void CrossPointWebServer::handleFileListData(AsyncWebServerRequest* request) const {
|
||||||
// Get current path from query string (default to root)
|
// Get current path from query string (default to root)
|
||||||
String currentPath = "/";
|
String currentPath = "/";
|
||||||
if (server->hasArg("path")) {
|
if (request->hasParam("path")) {
|
||||||
currentPath = server->arg("path");
|
currentPath = request->getParam("path")->value();
|
||||||
// Ensure path starts with /
|
// Ensure path starts with /
|
||||||
if (!currentPath.startsWith("/")) {
|
if (!currentPath.startsWith("/")) {
|
||||||
currentPath = "/" + currentPath;
|
currentPath = "/" + currentPath;
|
||||||
@ -320,56 +269,42 @@ void CrossPointWebServer::handleFileListData() const {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
server->setContentLength(CONTENT_LENGTH_UNKNOWN);
|
// Build JSON response
|
||||||
server->send(200, "application/json", "");
|
String json = "[";
|
||||||
server->sendContent("[");
|
|
||||||
char output[512];
|
|
||||||
constexpr size_t outputSize = sizeof(output);
|
|
||||||
bool seenFirst = false;
|
bool seenFirst = false;
|
||||||
JsonDocument doc;
|
|
||||||
|
|
||||||
scanFiles(currentPath.c_str(), [this, &output, &doc, seenFirst](const FileInfo& info) mutable {
|
scanFiles(currentPath.c_str(), [&json, &seenFirst](const FileInfo& info) {
|
||||||
doc.clear();
|
JsonDocument doc;
|
||||||
doc["name"] = info.name;
|
doc["name"] = info.name;
|
||||||
doc["size"] = info.size;
|
doc["size"] = info.size;
|
||||||
doc["isDirectory"] = info.isDirectory;
|
doc["isDirectory"] = info.isDirectory;
|
||||||
doc["isEpub"] = info.isEpub;
|
doc["isEpub"] = info.isEpub;
|
||||||
|
|
||||||
const size_t written = serializeJson(doc, output, outputSize);
|
|
||||||
if (written >= outputSize) {
|
|
||||||
// JSON output truncated; skip this entry to avoid sending malformed JSON
|
|
||||||
Serial.printf("[%lu] [WEB] Skipping file entry with oversized JSON for name: %s\n", millis(), info.name.c_str());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (seenFirst) {
|
if (seenFirst) {
|
||||||
server->sendContent(",");
|
json += ",";
|
||||||
} else {
|
} else {
|
||||||
seenFirst = true;
|
seenFirst = true;
|
||||||
}
|
}
|
||||||
server->sendContent(output);
|
|
||||||
|
String entry;
|
||||||
|
serializeJson(doc, entry);
|
||||||
|
json += entry;
|
||||||
});
|
});
|
||||||
server->sendContent("]");
|
|
||||||
// End of streamed response, empty chunk to signal client
|
json += "]";
|
||||||
server->sendContent("");
|
request->send(200, "application/json", json);
|
||||||
Serial.printf("[%lu] [WEB] Served file listing page for path: %s\n", millis(), currentPath.c_str());
|
Serial.printf("[%lu] [WEB] Served file listing page for path: %s\n", millis(), currentPath.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void CrossPointWebServer::handleUpload() const {
|
void CrossPointWebServer::handleUpload(AsyncWebServerRequest* request, const String& filename, size_t index,
|
||||||
// Safety check: ensure server is still valid
|
uint8_t* data, size_t len, bool final) {
|
||||||
if (!running || !server) {
|
if (index == 0) {
|
||||||
Serial.printf("[%lu] [WEB] [UPLOAD] ERROR: handleUpload called but server not running!\n", millis());
|
// First chunk - initialize upload
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const HTTPUpload& upload = server->upload();
|
|
||||||
|
|
||||||
if (upload.status == UPLOAD_FILE_START) {
|
|
||||||
xSemaphoreTake(uploadMutex, portMAX_DELAY);
|
xSemaphoreTake(uploadMutex, portMAX_DELAY);
|
||||||
|
|
||||||
uploadFileName = upload.filename;
|
uploadFileName = filename;
|
||||||
uploadSize = 0;
|
uploadSize = 0;
|
||||||
uploadTotalExpected = upload.totalSize; // May be 0 if unknown
|
uploadTotalExpected = request->contentLength();
|
||||||
uploadSuccess = false;
|
uploadSuccess = false;
|
||||||
uploadError = "";
|
uploadError = "";
|
||||||
uploadStartTime = millis();
|
uploadStartTime = millis();
|
||||||
@ -381,8 +316,8 @@ void CrossPointWebServer::handleUpload() const {
|
|||||||
writeCount = 0;
|
writeCount = 0;
|
||||||
|
|
||||||
// Get upload path from query parameter
|
// Get upload path from query parameter
|
||||||
if (server->hasArg("path")) {
|
if (request->hasParam("path")) {
|
||||||
uploadPath = server->arg("path");
|
uploadPath = request->getParam("path")->value();
|
||||||
if (!uploadPath.startsWith("/")) {
|
if (!uploadPath.startsWith("/")) {
|
||||||
uploadPath = "/" + uploadPath;
|
uploadPath = "/" + uploadPath;
|
||||||
}
|
}
|
||||||
@ -395,11 +330,10 @@ void CrossPointWebServer::handleUpload() const {
|
|||||||
|
|
||||||
xSemaphoreGive(uploadMutex);
|
xSemaphoreGive(uploadMutex);
|
||||||
|
|
||||||
Serial.printf("[%lu] [WEB] [UPLOAD] START: %s to path: %s\n", millis(), uploadFileName.c_str(), uploadPath.c_str());
|
Serial.printf("[%lu] [WEB] [UPLOAD] START: %s to path: %s (total: %d bytes)\n", millis(), uploadFileName.c_str(),
|
||||||
|
uploadPath.c_str(), uploadTotalExpected);
|
||||||
Serial.printf("[%lu] [WEB] [UPLOAD] Free heap: %d bytes\n", millis(), ESP.getFreeHeap());
|
Serial.printf("[%lu] [WEB] [UPLOAD] Free heap: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||||
|
|
||||||
boostCPU();
|
|
||||||
|
|
||||||
// Create file path
|
// Create file path
|
||||||
String filePath = uploadPath;
|
String filePath = uploadPath;
|
||||||
if (!filePath.endsWith("/")) filePath += "/";
|
if (!filePath.endsWith("/")) filePath += "/";
|
||||||
@ -417,58 +351,57 @@ void CrossPointWebServer::handleUpload() const {
|
|||||||
uploadError = "Failed to create file on SD card";
|
uploadError = "Failed to create file on SD card";
|
||||||
uploadInProgress = false;
|
uploadInProgress = false;
|
||||||
xSemaphoreGive(uploadMutex);
|
xSemaphoreGive(uploadMutex);
|
||||||
restoreCPU();
|
|
||||||
Serial.printf("[%lu] [WEB] [UPLOAD] FAILED to create file: %s\n", millis(), filePath.c_str());
|
Serial.printf("[%lu] [WEB] [UPLOAD] FAILED to create file: %s\n", millis(), filePath.c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.printf("[%lu] [WEB] [UPLOAD] File created successfully: %s\n", millis(), filePath.c_str());
|
Serial.printf("[%lu] [WEB] [UPLOAD] File created successfully: %s\n", millis(), filePath.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
} else if (upload.status == UPLOAD_FILE_WRITE) {
|
// Write data chunk
|
||||||
if (uploadFile && uploadError.isEmpty()) {
|
if (uploadFile && uploadError.isEmpty() && len > 0) {
|
||||||
// Direct write to SD - simple and fast
|
const unsigned long writeStart = millis();
|
||||||
const unsigned long writeStart = millis();
|
const size_t written = uploadFile.write(data, len);
|
||||||
const size_t written = uploadFile.write(upload.buf, upload.currentSize);
|
const unsigned long writeTime = millis() - writeStart;
|
||||||
const unsigned long writeTime = millis() - writeStart;
|
|
||||||
|
|
||||||
totalWriteTimeMs += writeTime;
|
totalWriteTimeMs += writeTime;
|
||||||
writeCount++;
|
writeCount++;
|
||||||
|
|
||||||
if (written != upload.currentSize) {
|
if (written != len) {
|
||||||
xSemaphoreTake(uploadMutex, portMAX_DELAY);
|
xSemaphoreTake(uploadMutex, portMAX_DELAY);
|
||||||
uploadError = "Failed to write to SD card - disk may be full";
|
uploadError = "Failed to write to SD card - disk may be full";
|
||||||
xSemaphoreGive(uploadMutex);
|
xSemaphoreGive(uploadMutex);
|
||||||
uploadFile.close();
|
uploadFile.close();
|
||||||
Serial.printf("[%lu] [WEB] [UPLOAD] WRITE ERROR - expected %d, wrote %d\n", millis(), upload.currentSize,
|
Serial.printf("[%lu] [WEB] [UPLOAD] WRITE ERROR - expected %d, wrote %d\n", millis(), len, written);
|
||||||
written);
|
return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
uploadSize += written;
|
|
||||||
|
|
||||||
// Calculate speed every 500ms
|
|
||||||
const unsigned long now = millis();
|
|
||||||
if (now - lastSpeedCalcTime >= SPEED_CALC_INTERVAL_MS) {
|
|
||||||
xSemaphoreTake(uploadMutex, portMAX_DELAY);
|
|
||||||
const size_t bytesSinceLastCalc = uploadSize - lastSpeedCalcSize;
|
|
||||||
const float secondsElapsed = (now - lastSpeedCalcTime) / 1000.0f;
|
|
||||||
if (secondsElapsed > 0) {
|
|
||||||
uploadSpeedKBps = (bytesSinceLastCalc / 1024.0f) / secondsElapsed;
|
|
||||||
}
|
|
||||||
lastSpeedCalcTime = now;
|
|
||||||
lastSpeedCalcSize = uploadSize;
|
|
||||||
|
|
||||||
// Log progress with diagnostics
|
|
||||||
const float avgSpeed = (uploadSize / 1024.0f) / ((now - uploadStartTime) / 1000.0f);
|
|
||||||
const float avgWriteMs = writeCount > 0 ? (float)totalWriteTimeMs / writeCount : 0;
|
|
||||||
Serial.printf(
|
|
||||||
"[%lu] [WEB] [UPLOAD] %d bytes (%.1f KB), cur: %.1f KB/s, avg: %.1f KB/s, writes: %d, avgWrite: %.1fms\n",
|
|
||||||
millis(), uploadSize, uploadSize / 1024.0f, uploadSpeedKBps, avgSpeed, writeCount, avgWriteMs);
|
|
||||||
xSemaphoreGive(uploadMutex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (upload.status == UPLOAD_FILE_END) {
|
uploadSize += written;
|
||||||
|
|
||||||
|
// Calculate speed every 500ms
|
||||||
|
const unsigned long now = millis();
|
||||||
|
if (now - lastSpeedCalcTime >= SPEED_CALC_INTERVAL_MS) {
|
||||||
|
xSemaphoreTake(uploadMutex, portMAX_DELAY);
|
||||||
|
const size_t bytesSinceLastCalc = uploadSize - lastSpeedCalcSize;
|
||||||
|
const float secondsElapsed = (now - lastSpeedCalcTime) / 1000.0f;
|
||||||
|
if (secondsElapsed > 0) {
|
||||||
|
uploadSpeedKBps = (bytesSinceLastCalc / 1024.0f) / secondsElapsed;
|
||||||
|
}
|
||||||
|
lastSpeedCalcTime = now;
|
||||||
|
lastSpeedCalcSize = uploadSize;
|
||||||
|
|
||||||
|
// Log progress with diagnostics
|
||||||
|
const float avgSpeed = (uploadSize / 1024.0f) / ((now - uploadStartTime) / 1000.0f);
|
||||||
|
const float avgWriteMs = writeCount > 0 ? (float)totalWriteTimeMs / writeCount : 0;
|
||||||
|
Serial.printf(
|
||||||
|
"[%lu] [WEB] [UPLOAD] %d bytes (%.1f KB), cur: %.1f KB/s, avg: %.1f KB/s, writes: %d, avgWrite: %.1fms\n",
|
||||||
|
millis(), uploadSize, uploadSize / 1024.0f, uploadSpeedKBps, avgSpeed, writeCount, avgWriteMs);
|
||||||
|
xSemaphoreGive(uploadMutex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Final chunk
|
||||||
|
if (final) {
|
||||||
if (uploadFile) {
|
if (uploadFile) {
|
||||||
uploadFile.close();
|
uploadFile.close();
|
||||||
|
|
||||||
@ -476,7 +409,7 @@ void CrossPointWebServer::handleUpload() const {
|
|||||||
if (uploadError.isEmpty()) {
|
if (uploadError.isEmpty()) {
|
||||||
uploadSuccess = true;
|
uploadSuccess = true;
|
||||||
const unsigned long duration = millis() - uploadStartTime;
|
const unsigned long duration = millis() - uploadStartTime;
|
||||||
const float avgSpeed = (uploadSize / 1024.0f) / (duration / 1000.0f);
|
const float avgSpeed = duration > 0 ? (uploadSize / 1024.0f) / (duration / 1000.0f) : 0;
|
||||||
const float avgWriteMs = writeCount > 0 ? (float)totalWriteTimeMs / writeCount : 0;
|
const float avgWriteMs = writeCount > 0 ? (float)totalWriteTimeMs / writeCount : 0;
|
||||||
const float writePercent = duration > 0 ? (totalWriteTimeMs * 100.0f / duration) : 0;
|
const float writePercent = duration > 0 ? (totalWriteTimeMs * 100.0f / duration) : 0;
|
||||||
Serial.printf("[%lu] [WEB] [UPLOAD] Complete: %s (%d bytes in %lu ms, avg %.1f KB/s)\n", millis(),
|
Serial.printf("[%lu] [WEB] [UPLOAD] Complete: %s (%d bytes in %lu ms, avg %.1f KB/s)\n", millis(),
|
||||||
@ -487,56 +420,37 @@ void CrossPointWebServer::handleUpload() const {
|
|||||||
uploadInProgress = false;
|
uploadInProgress = false;
|
||||||
xSemaphoreGive(uploadMutex);
|
xSemaphoreGive(uploadMutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
restoreCPU();
|
|
||||||
|
|
||||||
} else if (upload.status == UPLOAD_FILE_ABORTED) {
|
|
||||||
if (uploadFile) {
|
|
||||||
uploadFile.close();
|
|
||||||
String filePath = uploadPath;
|
|
||||||
if (!filePath.endsWith("/")) filePath += "/";
|
|
||||||
filePath += uploadFileName;
|
|
||||||
SdMan.remove(filePath.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
xSemaphoreTake(uploadMutex, portMAX_DELAY);
|
|
||||||
uploadError = "Upload aborted";
|
|
||||||
uploadInProgress = false;
|
|
||||||
xSemaphoreGive(uploadMutex);
|
|
||||||
|
|
||||||
restoreCPU();
|
|
||||||
Serial.printf("[%lu] [WEB] [UPLOAD] Aborted\n", millis());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CrossPointWebServer::handleUploadPost() const {
|
void CrossPointWebServer::handleUploadRequest(AsyncWebServerRequest* request) {
|
||||||
if (uploadSuccess) {
|
if (uploadSuccess) {
|
||||||
server->send(200, "text/plain", "File uploaded successfully: " + uploadFileName);
|
request->send(200, "text/plain", "File uploaded successfully: " + uploadFileName);
|
||||||
} else {
|
} else {
|
||||||
const String error = uploadError.isEmpty() ? "Unknown error during upload" : uploadError;
|
const String error = uploadError.isEmpty() ? "Unknown error during upload" : uploadError;
|
||||||
server->send(400, "text/plain", error);
|
request->send(400, "text/plain", error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CrossPointWebServer::handleCreateFolder() const {
|
void CrossPointWebServer::handleCreateFolder(AsyncWebServerRequest* request) const {
|
||||||
// Get folder name from form data
|
// Get folder name from form data
|
||||||
if (!server->hasArg("name")) {
|
if (!request->hasParam("name", true)) {
|
||||||
server->send(400, "text/plain", "Missing folder name");
|
request->send(400, "text/plain", "Missing folder name");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const String folderName = server->arg("name");
|
const String folderName = request->getParam("name", true)->value();
|
||||||
|
|
||||||
// Validate folder name
|
// Validate folder name
|
||||||
if (folderName.isEmpty()) {
|
if (folderName.isEmpty()) {
|
||||||
server->send(400, "text/plain", "Folder name cannot be empty");
|
request->send(400, "text/plain", "Folder name cannot be empty");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get parent path
|
// Get parent path
|
||||||
String parentPath = "/";
|
String parentPath = "/";
|
||||||
if (server->hasArg("path")) {
|
if (request->hasParam("path", true)) {
|
||||||
parentPath = server->arg("path");
|
parentPath = request->getParam("path", true)->value();
|
||||||
if (!parentPath.startsWith("/")) {
|
if (!parentPath.startsWith("/")) {
|
||||||
parentPath = "/" + parentPath;
|
parentPath = "/" + parentPath;
|
||||||
}
|
}
|
||||||
@ -554,33 +468,33 @@ void CrossPointWebServer::handleCreateFolder() const {
|
|||||||
|
|
||||||
// Check if already exists
|
// Check if already exists
|
||||||
if (SdMan.exists(folderPath.c_str())) {
|
if (SdMan.exists(folderPath.c_str())) {
|
||||||
server->send(400, "text/plain", "Folder already exists");
|
request->send(400, "text/plain", "Folder already exists");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the folder
|
// Create the folder
|
||||||
if (SdMan.mkdir(folderPath.c_str())) {
|
if (SdMan.mkdir(folderPath.c_str())) {
|
||||||
Serial.printf("[%lu] [WEB] Folder created successfully: %s\n", millis(), folderPath.c_str());
|
Serial.printf("[%lu] [WEB] Folder created successfully: %s\n", millis(), folderPath.c_str());
|
||||||
server->send(200, "text/plain", "Folder created: " + folderName);
|
request->send(200, "text/plain", "Folder created: " + folderName);
|
||||||
} else {
|
} else {
|
||||||
Serial.printf("[%lu] [WEB] Failed to create folder: %s\n", millis(), folderPath.c_str());
|
Serial.printf("[%lu] [WEB] Failed to create folder: %s\n", millis(), folderPath.c_str());
|
||||||
server->send(500, "text/plain", "Failed to create folder");
|
request->send(500, "text/plain", "Failed to create folder");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CrossPointWebServer::handleDelete() const {
|
void CrossPointWebServer::handleDelete(AsyncWebServerRequest* request) const {
|
||||||
// Get path from form data
|
// Get path from form data
|
||||||
if (!server->hasArg("path")) {
|
if (!request->hasParam("path", true)) {
|
||||||
server->send(400, "text/plain", "Missing path");
|
request->send(400, "text/plain", "Missing path");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String itemPath = server->arg("path");
|
String itemPath = request->getParam("path", true)->value();
|
||||||
const String itemType = server->hasArg("type") ? server->arg("type") : "file";
|
const String itemType = request->hasParam("type", true) ? request->getParam("type", true)->value() : "file";
|
||||||
|
|
||||||
// Validate path
|
// Validate path
|
||||||
if (itemPath.isEmpty() || itemPath == "/") {
|
if (itemPath.isEmpty() || itemPath == "/") {
|
||||||
server->send(400, "text/plain", "Cannot delete root directory");
|
request->send(400, "text/plain", "Cannot delete root directory");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -595,7 +509,7 @@ void CrossPointWebServer::handleDelete() const {
|
|||||||
// Check if item starts with a dot (hidden/system file)
|
// Check if item starts with a dot (hidden/system file)
|
||||||
if (itemName.startsWith(".")) {
|
if (itemName.startsWith(".")) {
|
||||||
Serial.printf("[%lu] [WEB] Delete rejected - hidden/system item: %s\n", millis(), itemPath.c_str());
|
Serial.printf("[%lu] [WEB] Delete rejected - hidden/system item: %s\n", millis(), itemPath.c_str());
|
||||||
server->send(403, "text/plain", "Cannot delete system files");
|
request->send(403, "text/plain", "Cannot delete system files");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -603,7 +517,7 @@ void CrossPointWebServer::handleDelete() const {
|
|||||||
for (size_t i = 0; i < HIDDEN_ITEMS_COUNT; i++) {
|
for (size_t i = 0; i < HIDDEN_ITEMS_COUNT; i++) {
|
||||||
if (itemName.equals(HIDDEN_ITEMS[i])) {
|
if (itemName.equals(HIDDEN_ITEMS[i])) {
|
||||||
Serial.printf("[%lu] [WEB] Delete rejected - protected item: %s\n", millis(), itemPath.c_str());
|
Serial.printf("[%lu] [WEB] Delete rejected - protected item: %s\n", millis(), itemPath.c_str());
|
||||||
server->send(403, "text/plain", "Cannot delete protected items");
|
request->send(403, "text/plain", "Cannot delete protected items");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -611,7 +525,7 @@ void CrossPointWebServer::handleDelete() const {
|
|||||||
// Check if item exists
|
// Check if item exists
|
||||||
if (!SdMan.exists(itemPath.c_str())) {
|
if (!SdMan.exists(itemPath.c_str())) {
|
||||||
Serial.printf("[%lu] [WEB] Delete failed - item not found: %s\n", millis(), itemPath.c_str());
|
Serial.printf("[%lu] [WEB] Delete failed - item not found: %s\n", millis(), itemPath.c_str());
|
||||||
server->send(404, "text/plain", "Item not found");
|
request->send(404, "text/plain", "Item not found");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -630,7 +544,7 @@ void CrossPointWebServer::handleDelete() const {
|
|||||||
entry.close();
|
entry.close();
|
||||||
dir.close();
|
dir.close();
|
||||||
Serial.printf("[%lu] [WEB] Delete failed - folder not empty: %s\n", millis(), itemPath.c_str());
|
Serial.printf("[%lu] [WEB] Delete failed - folder not empty: %s\n", millis(), itemPath.c_str());
|
||||||
server->send(400, "text/plain", "Folder is not empty. Delete contents first.");
|
request->send(400, "text/plain", "Folder is not empty. Delete contents first.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
dir.close();
|
dir.close();
|
||||||
@ -643,9 +557,9 @@ void CrossPointWebServer::handleDelete() const {
|
|||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
Serial.printf("[%lu] [WEB] Successfully deleted: %s\n", millis(), itemPath.c_str());
|
Serial.printf("[%lu] [WEB] Successfully deleted: %s\n", millis(), itemPath.c_str());
|
||||||
server->send(200, "text/plain", "Deleted successfully");
|
request->send(200, "text/plain", "Deleted successfully");
|
||||||
} else {
|
} else {
|
||||||
Serial.printf("[%lu] [WEB] Failed to delete: %s\n", millis(), itemPath.c_str());
|
Serial.printf("[%lu] [WEB] Failed to delete: %s\n", millis(), itemPath.c_str());
|
||||||
server->send(500, "text/plain", "Failed to delete item");
|
request->send(500, "text/plain", "Failed to delete item");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <ESPAsyncWebServer.h>
|
||||||
#include <SDCardManager.h>
|
#include <SDCardManager.h>
|
||||||
#include <WebServer.h>
|
|
||||||
#include <freertos/FreeRTOS.h>
|
#include <freertos/FreeRTOS.h>
|
||||||
#include <freertos/semphr.h>
|
#include <freertos/semphr.h>
|
||||||
|
|
||||||
@ -26,9 +26,6 @@ class CrossPointWebServer {
|
|||||||
// Stop the web server
|
// Stop the web server
|
||||||
void stop();
|
void stop();
|
||||||
|
|
||||||
// Call this periodically to handle client requests
|
|
||||||
void handleClient() const;
|
|
||||||
|
|
||||||
// Check if server is running
|
// Check if server is running
|
||||||
bool isRunning() const { return running; }
|
bool isRunning() const { return running; }
|
||||||
|
|
||||||
@ -42,7 +39,7 @@ class CrossPointWebServer {
|
|||||||
uint8_t getUploadProgress() const; // 0-100%
|
uint8_t getUploadProgress() const; // 0-100%
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<WebServer> server = nullptr;
|
std::unique_ptr<AsyncWebServer> server = nullptr;
|
||||||
bool running = false;
|
bool running = false;
|
||||||
bool apMode = false; // true when running in AP mode, false for STA mode
|
bool apMode = false; // true when running in AP mode, false for STA mode
|
||||||
uint16_t port = 80;
|
uint16_t port = 80;
|
||||||
@ -61,29 +58,25 @@ class CrossPointWebServer {
|
|||||||
mutable unsigned long uploadStartTime = 0;
|
mutable unsigned long uploadStartTime = 0;
|
||||||
mutable unsigned long lastSpeedCalcTime = 0;
|
mutable unsigned long lastSpeedCalcTime = 0;
|
||||||
mutable size_t lastSpeedCalcSize = 0;
|
mutable size_t lastSpeedCalcSize = 0;
|
||||||
mutable bool cpuBoosted = false;
|
|
||||||
|
|
||||||
// Diagnostic counters
|
// Diagnostic counters
|
||||||
mutable unsigned long totalWriteTimeMs = 0;
|
mutable unsigned long totalWriteTimeMs = 0;
|
||||||
mutable size_t writeCount = 0;
|
mutable size_t writeCount = 0;
|
||||||
|
|
||||||
// CPU frequency management for upload performance
|
|
||||||
void boostCPU() const;
|
|
||||||
void restoreCPU() const;
|
|
||||||
|
|
||||||
// File scanning
|
// File scanning
|
||||||
void scanFiles(const char* path, const std::function<void(FileInfo)>& callback) const;
|
void scanFiles(const char* path, const std::function<void(FileInfo)>& callback) const;
|
||||||
String formatFileSize(size_t bytes) const;
|
String formatFileSize(size_t bytes) const;
|
||||||
bool isEpubFile(const String& filename) const;
|
bool isEpubFile(const String& filename) const;
|
||||||
|
|
||||||
// Request handlers
|
// Request handlers
|
||||||
void handleRoot() const;
|
void handleRoot(AsyncWebServerRequest* request) const;
|
||||||
void handleNotFound() const;
|
void handleNotFound(AsyncWebServerRequest* request) const;
|
||||||
void handleStatus() const;
|
void handleStatus(AsyncWebServerRequest* request) const;
|
||||||
void handleFileList() const;
|
void handleFileList(AsyncWebServerRequest* request) const;
|
||||||
void handleFileListData() const;
|
void handleFileListData(AsyncWebServerRequest* request) const;
|
||||||
void handleUpload() const;
|
void handleUploadRequest(AsyncWebServerRequest* request);
|
||||||
void handleUploadPost() const;
|
void handleUpload(AsyncWebServerRequest* request, const String& filename, size_t index, uint8_t* data, size_t len,
|
||||||
void handleCreateFolder() const;
|
bool final);
|
||||||
void handleDelete() const;
|
void handleCreateFolder(AsyncWebServerRequest* request) const;
|
||||||
|
void handleDelete(AsyncWebServerRequest* request) const;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user