#include "boomcast_uploader.h" #include "boomcast_config.h" #include "boomcast_network.h" #include "boomcast_state.h" #include "boomcast_monitor.h" #include #include #include #include #include "esp_sntp.h" #include "esp_heap_caps.h" BoomUploader boomUploader; static String eventIdFromFilename(String name) { if (name.endsWith(".wav")) name.remove(name.length() - 4); return name; } static String isoUtc(uint64_t epochMs) { if (!epochMs) return ""; time_t seconds = epochMs / 1000; struct tm utc; if (!gmtime_r(&seconds, &utc)) return ""; char text[30]; snprintf(text, sizeof(text), "%04d-%02d-%02dT%02d:%02d:%02d.%03uZ", utc.tm_year + 1900, utc.tm_mon + 1, utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec, unsigned(epochMs % 1000)); return String(text); } static bool writeAll(WiFiClientSecure& client, const uint8_t* bytes, size_t size) { size_t done = 0; const uint32_t started = millis(); while (done < size && client.connected()) { boomMonitor.tick(); if (millis() - started > 15000) return false; size_t n = client.write(bytes + done, size - done); if (!n) return false; done += n; } return done == size; } static bool writeAll(WiFiClientSecure& client, const String& value) { return writeAll(client, reinterpret_cast(value.c_str()), value.length()); } static void addPart(String& body, const String& boundary, const char* name, const String& value) { body += "--" + boundary + "\r\nContent-Disposition: form-data; name=\"" + name + "\"\r\n\r\n" + value + "\r\n"; } static bool waitForNtpTime() { // SNTP reports COMPLETED once per synchronization. Retain that result for // later uploads; a reboot clears it and requires a new NTP sync. static bool synchronized = false; const uint32_t started = millis(); while (!synchronized && boomNetwork.connected() && millis() - started < 20000) { if (sntp_get_sync_status() == SNTP_SYNC_STATUS_COMPLETED) synchronized = true; boomMonitor.tick(); if (!synchronized) delay(100); } time_t now = time(nullptr); return synchronized && now >= 1704067200; // Reject an unset or implausible clock. } bool BoomUploader::submitOne(const String& filename) { if (!queue_ || !queue_->ready() || !boomNetwork.connected() || !queue_->exists(filename)) return false; boomState.setSyncing(true, "Uploading " + filename); bool ok = doPost(filename, queue_->absolutePath(filename)); if (ok && !queue_->remove(filename)) { boomState.setSyncing(false, "Receipt received; local cleanup failed"); return false; } boomState.setSyncing(false, ok ? "Submitted " + filename : "Upload failed; queued for retry"); return ok; } int BoomUploader::submitAll() { if (!queue_ || !boomNetwork.connected()) return 0; int sent = 0; const uint32_t started = millis(); while (sent < 500 && millis() - started < 30000) { boomMonitor.tick(); String first[1]; if (!queue_->listNames(first, 1) || !submitOne(first[0])) break; ++sent; } return sent; } void BoomUploader::tick() { if (!queue_ || !queue_->ready() || !boomNetwork.connected() || !queue_->count()) return; if ((int32_t)(millis() - nextAttemptMs_) < 0) return; String first[1]; if (!queue_->listNames(first, 1)) return; if (submitOne(first[0])) { failures_ = 0; permanentFailure_ = false; nextAttemptMs_ = millis() + 1000; } else { failures_ = min(uint8_t(failures_ + 1), uint8_t(8)); uint32_t base = permanentFailure_ ? 3600000UL : min(900000UL, 5000UL << failures_); nextAttemptMs_ = millis() + base + (esp_random() % 5000); } } bool BoomUploader::doPost(const String& filename, const String& path) { permanentFailure_ = false; BoomEventMeta meta; if (!queue_->readMeta(filename, meta)) return false; String token = boomState.prefs().getString("token", SENSOR_TOKEN); if (!token.length() || !SENSOR_ID) { Serial.println("[Upload] sensor ID or token missing"); permanentFailure_ = true; return false; } String origin(SERVER_BASE_URL); if (!origin.startsWith("https://")) { Serial.println("[Upload] HTTPS required"); permanentFailure_ = true; return false; } String host = origin.substring(8); int slash = host.indexOf('/'); if (slash >= 0) host = host.substring(0, slash); uint16_t port = 443; int colon = host.lastIndexOf(':'); if (colon >= 0) { port = host.substring(colon + 1).toInt(); host = host.substring(0, colon); } if (!host.length() || !port) return false; if (!waitForNtpTime()) { Serial.println("[Upload] waiting for NTP time before TLS validation"); return false; } FILE* file = fopen(path.c_str(), "rb"); if (!file) return false; const size_t fileBytes = queue_->fileSize(filename); const String boundary = "BoomCast" + String(esp_random(), HEX); const String eventId = eventIdFromFilename(filename); String preamble; preamble.reserve(700); addPart(preamble, boundary, "sensor_id", String(SENSOR_ID)); addPart(preamble, boundary, "event_id", eventId); if (meta.captureStartedMs) { addPart(preamble, boundary, "captured_at", isoUtc(meta.captureStartedMs)); addPart(preamble, boundary, "capture_started_at", isoUtc(meta.captureStartedMs)); } else addPart(preamble, boundary, "time_state", "unknown"); if (meta.triggeredMs) addPart(preamble, boundary, "triggered_at", isoUtc(meta.triggeredMs)); if (meta.queuedMs) addPart(preamble, boundary, "queued_at", isoUtc(meta.queuedMs)); addPart(preamble, boundary, "duration_seconds", String(meta.sampleCount / float(AUDIO_SAMPLE_RATE), 4)); addPart(preamble, boundary, "firmware_version", BOOMCAST_FIRMWARE_VERSION); addPart(preamble, boundary, "trigger_type", filename.indexOf("_manual_") >= 0 ? "manual" : "audio_spike"); preamble += "--" + boundary + "\r\nContent-Disposition: form-data; name=\"file\"; filename=\"" + filename + "\"\r\nContent-Type: audio/wav\r\n\r\n"; const String trailer = "\r\n--" + boundary + "--\r\n"; const size_t length = preamble.length() + fileBytes + trailer.length(); WiFiClientSecure client; client.useBuiltinCACertBundle(); client.setTimeout(5); client.setHandshakeTimeout(15); Serial.printf("[Upload] free heap=%u PSRAM=%u largest PSRAM=%u\n", unsigned(ESP.getFreeHeap()), unsigned(ESP.getFreePsram()), unsigned(heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM))); if (!client.connect(host.c_str(), port, 5000)) { fclose(file); return false; } String header = "POST " + String(SUBMIT_PATH) + " HTTP/1.1\r\nHost: " + host + "\r\n"; header += "Authorization: Bearer " + token + "\r\n"; header += "Content-Type: multipart/form-data; boundary=" + boundary + "\r\n"; header += "Content-Length: " + String(length) + "\r\nAccept: application/json\r\nConnection: close\r\n\r\n"; bool sent = writeAll(client, header) && writeAll(client, preamble); uint8_t chunk[1024]; size_t fileSent = 0; const uint32_t uploadStarted = millis(); while (sent && fileSent < fileBytes && millis() - uploadStarted < 30000) { size_t n = fread(chunk, 1, min(sizeof(chunk), fileBytes - fileSent), file); if (n == 0) { sent = false; break; } sent = writeAll(client, chunk, n); fileSent += n; } if (ferror(file) || fileSent != fileBytes) sent = false; fclose(file); if (sent) sent = writeAll(client, trailer); if (!sent) { client.stop(); return false; } const uint32_t responseStarted = millis(); auto readLine = [&client, responseStarted](String& line) { line = ""; while (millis() - responseStarted < 60000 && line.length() < 1024) { boomMonitor.tick(); if (client.available()) { char c = char(client.read()); if (c == '\n') return true; line += c; } else { if (!client.connected()) return false; delay(5); } } return false; }; String statusLine; if (!readLine(statusLine)) { client.stop(); return false; } int status = 0; if (statusLine.startsWith("HTTP/1.")) { int firstSpace = statusLine.indexOf(' '); if (firstSpace > 0) status = statusLine.substring(firstSpace + 1).toInt(); } // Django returns an ordinary Content-Length response; do not accept redirects. int contentLength = -1; size_t headerBytes = 0; while (true) { String line; if (!readLine(line) || (headerBytes += line.length()) > 8192) { client.stop(); return false; } line.trim(); if (!line.length()) break; if (line.startsWith("Content-Length:") || line.startsWith("content-length:")) contentLength = line.substring(line.indexOf(':') + 1).toInt(); } if (contentLength < 0 || contentLength > 8192) { client.stop(); return false; } String body; body.reserve(contentLength); while (body.length() < size_t(contentLength) && millis() - responseStarted < 60000) { boomMonitor.tick(); if (!client.connected() && !client.available()) break; while (client.available() && body.length() < size_t(contentLength)) body += char(client.read()); delay(1); } client.stop(); Serial.printf("[Upload] HTTP %d, response bytes=%u\n", status, unsigned(body.length())); if (body.length() != size_t(contentLength)) return false; if (status != 200 && status != 201) { permanentFailure_ = status >= 400 && status < 500 && status != 408 && status != 429; return false; } JsonDocument receipt; if (deserializeJson(receipt, body)) return false; const char* returnedEvent = receipt["event_id"] | ""; const char* submission = receipt["submission_id"] | ""; const char* state = receipt["status"] | ""; if (eventId != returnedEvent || strlen(submission) != 36 || (strcmp(state, "pending") && strcmp(state, "reviewed")) || (status == 200 && !receipt["duplicate"].as())) return false; Serial.printf("[Upload] accepted event %s; free heap=%u PSRAM=%u largest PSRAM=%u\n", eventId.c_str(), unsigned(ESP.getFreeHeap()), unsigned(ESP.getFreePsram()), unsigned(heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM))); return true; }