Fix Infinite Loop Bug, Melody Download crashes and dwnld skip if melody is builin.

This commit is contained in:
2026-02-04 17:00:27 +02:00
parent 9c314d88cf
commit 980de08584
4 changed files with 52 additions and 17 deletions

View File

@@ -1,5 +1,9 @@
COMMANDS:
PV26A28BC01R01
{
"cmd":"system",
"contents":

View File

@@ -1,4 +1,5 @@
#include "FileManager.hpp"
#include "../BuiltInMelodies/BuiltInMelodies.hpp"
FileManager::FileManager(ConfigManager* config) : configManager(config) {
// Constructor - store reference to ConfigManager
@@ -23,15 +24,26 @@ bool FileManager::addMelody(JsonVariant doc) {
}
const char* url = doc["download_url"];
const char* filename = doc["melodys_uid"];
const char* melodyUid = doc["melodys_uid"];
// Check if this is a built-in melody - skip download if it exists
if (BuiltInMelodies::isBuiltInMelody(melodyUid)) {
const BuiltInMelodies::MelodyInfo* builtinMelody = BuiltInMelodies::findMelodyByUID(melodyUid);
if (builtinMelody != nullptr) {
LOG_INFO("Melody '%s' is a built-in melody, skipping download", melodyUid);
return true; // Success - no download needed
}
// If starts with builtin_ but not found, log warning and try download anyway
LOG_WARNING("Melody '%s' has builtin_ prefix but not found in library, attempting download", melodyUid);
}
// Download the melody file to /melodies directory
if (downloadFile(url, "/melodies", filename)) {
LOG_INFO("Melody download successful: %s", filename);
if (downloadFile(url, "/melodies", melodyUid)) {
LOG_INFO("Melody download successful: %s", melodyUid);
return true;
}
LOG_ERROR("Melody download failed: %s", filename);
LOG_ERROR("Melody download failed: %s", melodyUid);
return false;
}
@@ -62,11 +74,13 @@ bool FileManager::downloadFile(const String& url, const String& directory, const
bool isHttps = url.startsWith("https://");
HTTPClient http;
WiFiClientSecure* secureClient = nullptr;
// Configure HTTP client based on protocol
if (isHttps) {
WiFiClientSecure* secureClient = new WiFiClientSecure();
secureClient = new WiFiClientSecure();
secureClient->setInsecure(); // Skip certificate validation for Firebase
secureClient->setTimeout(15); // 15 second timeout for TLS operations
http.begin(*secureClient, url);
LOG_DEBUG("Using HTTPS with secure client");
} else {
@@ -77,17 +91,28 @@ bool FileManager::downloadFile(const String& url, const String& directory, const
http.setTimeout(30000); // 30 second timeout for large files
http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS); // Follow redirects automatically
// Disable task watchdog for current task during blocking HTTPS operation
// The TLS handshake can take several seconds and would trigger watchdog
LOG_DEBUG("Disabling watchdog for download...");
esp_task_wdt_delete(NULL);
LOG_DEBUG("Sending HTTP GET request...");
int httpCode = http.GET();
// Re-enable task watchdog after HTTP request completes
esp_task_wdt_add(NULL);
LOG_DEBUG("Watchdog re-enabled after HTTP request");
if (httpCode != HTTP_CODE_OK && httpCode != HTTP_CODE_MOVED_PERMANENTLY && httpCode != HTTP_CODE_FOUND) {
LOG_ERROR("HTTP GET failed, code: %d, error: %s", httpCode, http.errorToString(httpCode).c_str());
http.end();
if (secureClient) delete secureClient;
return false;
}
if (!initializeSD()) {
http.end();
if (secureClient) delete secureClient;
return false;
}
@@ -95,6 +120,7 @@ bool FileManager::downloadFile(const String& url, const String& directory, const
if (!ensureDirectoryExists(directory)) {
LOG_ERROR("Failed to create directory: %s", directory.c_str());
http.end();
if (secureClient) delete secureClient;
return false;
}
@@ -107,6 +133,7 @@ bool FileManager::downloadFile(const String& url, const String& directory, const
if (!file) {
LOG_ERROR("Failed to open file for writing: %s", fullPath.c_str());
http.end();
if (secureClient) delete secureClient;
return false;
}
@@ -134,7 +161,7 @@ bool FileManager::downloadFile(const String& url, const String& directory, const
file.write(buffer, bytesRead);
totalBytes += bytesRead;
// Log progress every 5KB
// Log progress every 5 seconds
if (millis() - lastLog > 5000) {
LOG_DEBUG("Download progress: %u bytes", totalBytes);
lastLog = millis();
@@ -142,10 +169,10 @@ bool FileManager::downloadFile(const String& url, const String& directory, const
}
}
// Aggressive task yielding every 100ms to prevent watchdog timeout
if (millis() - lastYield > 100) {
// Aggressive task yielding every 50ms to prevent watchdog timeout
if (millis() - lastYield > 50) {
yield();
vTaskDelay(1 / portTICK_PERIOD_MS); // Let other tasks run
vTaskDelay(5 / portTICK_PERIOD_MS); // Let other tasks run (5ms)
lastYield = millis();
}
@@ -154,14 +181,16 @@ bool FileManager::downloadFile(const String& url, const String& directory, const
break;
}
// Small delay if no data available yet
// Yield and small delay if no data available yet
if (!availableSize) {
delay(10);
yield();
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
file.close();
http.end();
if (secureClient) delete secureClient;
LOG_INFO("Download complete, file saved to: %s (%u bytes)", fullPath.c_str(), totalBytes);
return true;
}

View File

@@ -24,6 +24,7 @@
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include <esp_task_wdt.h>
#include "../Logging/Logging.hpp"
#include "../ConfigManager/ConfigManager.hpp"

View File

@@ -64,7 +64,7 @@
* 👨‍💻 AUTHOR: BellSystems bonamin
*/
#define FW_VERSION "152"
#define FW_VERSION "153"
/*
@@ -80,6 +80,7 @@
* v140 - Changed FW Updates to Direct-to-Flash and added manual update functionality with version check
* v151 - Fixed Clock Alerts not running properly
* v152 - Fix RTC Time Reports, added sync_time_to_LCD functionality
* v153 - Fix Infinite Loop Bug and Melody Download crashes.
* ═══════════════════════════════════════════════════════════════════════════════
*/