feat: boot-phase OTA, RS485 variant, async melody download, HAL guards
- bootOtaCheck() runs at top of setup() before any task/timer; streams firmware to flash on update, reboots, otherwise returns immediately - New bespoke-rs485-v1 build env + Rs485OutputManager for Waveshare 16CH Modbus RTU relay; vesper.hpp renamed vesper-std.hpp, env renamed accordingly - Player: permanent melodyDownloadTaskFn task; fast path (built-in/SD) before async download; uid accepted as alias for pid - CommunicationRouter: HAS_UART guards throughout so UART transport compiles out when Serial2 is claimed by Modbus - OTAManager: stack 8KB→16KB, pinned to Core 1, HTTPClient recreated per retry, download timeout 30s→60s - FileManager: heap guard before SSL alloc; watchdog log cleanup - ConfigManager: OTA fallback URLs http (no TLS for firmware servers) - agnus.hpp: HAS_UART 1 added - provisioning/nvs_agnus_lab_device.csv added - Version bumped to v2.0.1 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -70,15 +70,17 @@ void OTAManager::begin() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Create dedicated worker task with 8KB stack (prevents timer task overflow)
|
||||
// Create dedicated worker task with 16KB stack.
|
||||
// Update.write() + HTTPClient + TLS frames + call depth during flash requires ~12-14KB;
|
||||
// 8KB was silently overflowing during the stream-write phase.
|
||||
BaseType_t taskCreated = xTaskCreatePinnedToCore(
|
||||
otaWorkerTaskFunction,
|
||||
"OTA_Worker",
|
||||
8192, // 8KB stack - plenty for HTTP and JSON operations
|
||||
16384, // 16KB stack - required for Update.write() + HTTP during flash
|
||||
this, // Pass OTAManager instance
|
||||
2, // Priority 2 (lower than critical tasks)
|
||||
&_otaWorkerTask,
|
||||
0 // Core 0
|
||||
1 // Core 1 — TLS/HTTPS requires Core 1; lwip runs on Core 0 and deadlocks blocking TLS on the same core
|
||||
);
|
||||
|
||||
if (taskCreated != pdPASS) {
|
||||
@@ -88,7 +90,7 @@ void OTAManager::begin() {
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_INFO(TAG, "OTA worker task created with 8KB stack on Core 0");
|
||||
LOG_INFO(TAG, "OTA worker task created with 16KB stack on Core 1");
|
||||
|
||||
// Create timer for scheduled checks (checks every minute if it's 3:00 AM)
|
||||
_scheduledCheckTimer = xTimerCreate(
|
||||
@@ -215,15 +217,15 @@ void OTAManager::checkForUpdates(const String& channel) {
|
||||
LOG_WARNING(TAG, "OTA check already in progress");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// 🔥 CRITICAL: Check network connectivity before attempting HTTP requests
|
||||
if (WiFi.status() != WL_CONNECTED) { // ETHERNET DISABLED - WiFi only
|
||||
LOG_WARNING(TAG, "OTA check skipped - no network connectivity");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
setStatus(Status::CHECKING_VERSION);
|
||||
LOG_INFO(TAG, "Checking for firmware updates in %s channel for %s...",
|
||||
LOG_INFO(TAG, "Checking for firmware updates in %s channel for %s...",
|
||||
channel.c_str(), _configManager.getHardwareVariant().c_str());
|
||||
|
||||
if (checkVersion(channel)) {
|
||||
@@ -319,27 +321,37 @@ bool OTAManager::checkVersion(const String& channel) {
|
||||
|
||||
LOG_INFO(TAG, "OTA: Trying server %d/%d: %s", serverIndex + 1, servers.size(), metadataUrl.c_str());
|
||||
|
||||
HTTPClient http;
|
||||
http.setTimeout(updateConfig.timeout);
|
||||
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
|
||||
http.begin(metadataUrl.c_str());
|
||||
|
||||
int retryCount = 0;
|
||||
String jsonStr;
|
||||
int httpCode = -1;
|
||||
|
||||
while (retryCount < updateConfig.retries && httpCode != HTTP_CODE_OK) {
|
||||
for (int retryCount = 0; retryCount < updateConfig.retries; retryCount++) {
|
||||
if (retryCount > 0) {
|
||||
LOG_INFO(TAG, "OTA: Retry %d/%d for server %d", retryCount + 1, updateConfig.retries, serverIndex + 1);
|
||||
delay(1000 * retryCount); // Exponential backoff
|
||||
}
|
||||
httpCode = http.GET();
|
||||
retryCount++;
|
||||
}
|
||||
|
||||
if (httpCode == HTTP_CODE_OK) {
|
||||
String jsonStr = http.getString();
|
||||
HTTPClient http;
|
||||
http.setTimeout(updateConfig.timeout);
|
||||
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
|
||||
http.begin(metadataUrl);
|
||||
|
||||
httpCode = http.GET();
|
||||
|
||||
if (httpCode == HTTP_CODE_OK) {
|
||||
jsonStr = http.getString();
|
||||
}
|
||||
http.end();
|
||||
|
||||
if (httpCode == HTTP_CODE_OK) break;
|
||||
}
|
||||
|
||||
if (httpCode != HTTP_CODE_OK) {
|
||||
LOG_ERROR(TAG, "OTA: Server %d failed after %d retries. HTTP code: %d",
|
||||
serverIndex + 1, updateConfig.retries, httpCode);
|
||||
continue;
|
||||
}
|
||||
|
||||
{
|
||||
DynamicJsonDocument doc(1024);
|
||||
DeserializationError error = deserializeJson(doc, jsonStr);
|
||||
if (error) {
|
||||
@@ -358,19 +370,16 @@ bool OTAManager::checkVersion(const String& channel) {
|
||||
const char* minVersionStr = doc["min_fw_version"] | "";
|
||||
_minVersion = (strlen(minVersionStr) > 0) ? SemVer::parse(String(minVersionStr)) : SemVer{0, 0, 0};
|
||||
|
||||
_availableChecksum = doc["sha256"] | "";
|
||||
_expectedFileSize = doc["size"] | (size_t)0;
|
||||
_availableChecksum = doc["sha256"] | "";
|
||||
_expectedFileSize = doc["size"] | (size_t)0;
|
||||
_availableDownloadPath = doc["download_url"] | "";
|
||||
|
||||
// update_type: "optional" | "mandatory" | "emergency"
|
||||
// This is the urgency of the release — completely separate from the channel.
|
||||
// The server derives mandatory/emergency booleans from this for us.
|
||||
_isMandatory = doc["mandatory"] | false;
|
||||
_isEmergency = doc["emergency"] | false;
|
||||
_updateChannel = channel; // channel is what we requested
|
||||
_isMandatory = doc["mandatory"] | false;
|
||||
_isEmergency = doc["emergency"] | false;
|
||||
_updateChannel = channel;
|
||||
|
||||
// Validate the server actually responded for the channel we asked for.
|
||||
// The response includes a "channel" field (e.g. "stable", "beta") — use that.
|
||||
const char* serverChannel = doc["channel"] | "";
|
||||
if (strlen(serverChannel) > 0 && String(serverChannel) != channel) {
|
||||
LOG_ERROR(TAG, "OTA: Channel mismatch! Requested: %s, Got: %s", channel.c_str(), serverChannel);
|
||||
@@ -378,21 +387,16 @@ bool OTAManager::checkVersion(const String& channel) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Validate version parsed correctly
|
||||
if (_availableVersion.isZero()) {
|
||||
LOG_ERROR(TAG, "OTA: Invalid version string '%s' from server %d", versionStr, serverIndex + 1);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Validate checksum is a 64-char SHA256 hex string
|
||||
if (_availableChecksum.length() != 64) {
|
||||
LOG_ERROR(TAG, "OTA: Invalid sha256 length (%d) from server %d", _availableChecksum.length(), serverIndex + 1);
|
||||
continue;
|
||||
}
|
||||
|
||||
// min_fw_version is informational only — the server already resolved the correct
|
||||
// next hop for our current_version, so we trust the version it returned.
|
||||
// We still log it for visibility.
|
||||
SemVer currentVersion = getCurrentVersion();
|
||||
if (!_minVersion.isZero() && currentVersion < _minVersion) {
|
||||
LOG_WARNING(TAG, "OTA: Server returned version %s with min_fw_version %s — server is handling upgrade chain",
|
||||
@@ -403,11 +407,6 @@ bool OTAManager::checkVersion(const String& channel) {
|
||||
serverIndex + 1, versionStr, _expectedFileSize,
|
||||
_isMandatory ? "YES" : "NO", _isEmergency ? "YES" : "NO");
|
||||
return true;
|
||||
|
||||
} else {
|
||||
LOG_ERROR(TAG, "OTA: Server %d failed after %d retries. HTTP code: %d",
|
||||
serverIndex + 1, updateConfig.retries, httpCode);
|
||||
http.end();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -457,8 +456,8 @@ bool OTAManager::downloadDirectToFlash(const String& url, size_t expectedSize, c
|
||||
|
||||
HTTPClient http;
|
||||
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
|
||||
http.begin(url.c_str());
|
||||
http.setTimeout(30000); // 30 seconds
|
||||
http.begin(url); // Let HTTPClient manage TLS internally (same as bootOtaCheck)
|
||||
http.setTimeout(60000); // 60 seconds for a ~1.4MB binary
|
||||
|
||||
int httpCode = http.GET();
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <HTTPClient.h>
|
||||
#include <WiFiClientSecure.h>
|
||||
#include <Update.h>
|
||||
#include <WiFi.h>
|
||||
#include <SD.h>
|
||||
|
||||
Reference in New Issue
Block a user