Answers "how do we emulate/lint this?" with runnable tooling for every part, and factors the pure firmware logic out so it is unit-testable on the host. What changed: - firmware/src/wav.h, firmware/src/httprange.h: pure, header-only WAV header builder and HTTP Range parser (no Arduino deps), so the tricky byte-layout and range logic can be tested on a PC. storage.cpp and api_http.cpp refactored to use them. - firmware/test/test_pure/: Unity tests for the WAV header fields and Range parsing (start-end, open-ended, clamped, unsatisfiable, non-bytes). Run: pio test -e native. - firmware/platformio.ini: add [env:native] (host tests) and [env:esp32s3_wokwi] (emulator build with default WiFi = Wokwi-GUEST so the API comes up in the sim). - firmware/wokwi.toml + firmware/diagram.json: Wokwi emulator harness (ESP32-S3 + microSD + button + LED). Note: Wokwi has no I2S mic part, so audio isn't emulated; the harness targets boot + WiFi + REST API. - firmware/src/main.cpp + config.cpp: bring up WiFi + API even if audio/SD init fails (device stays reachable, reports the fault via GET /device); compile-time default WiFi honoured when NVS is empty (used by the Wokwi build). - .forgejo/workflows/ci.yml: add native tests + cppcheck to the firmware job; new openapi job (openapi-spec-validator) and emulator job (Wokwi build, plus a full run when WOKWI_CLI_TOKEN is set). - docs/testing.md: the full emulate/lint guide for firmware, server, API, app, case. - state/: NOTES points at the guide; TODO reflects this branch. Why: - The firmware can't be flashed yet (no parts) and doesn't build on this dev host, so we need host-runnable checks. Pure-logic unit tests + OpenAPI validation run anywhere; Wokwi emulates boot/WiFi/API; CI compiles the real firmware. Verified locally: the OpenAPI spec validates (12 paths, 10 schemas). Notes: - Native tests and cppcheck run in CI (no compiler on the dev host). The Wokwi full run is skipped unless a WOKWI_CLI_TOKEN secret is present; the build is still verified. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
49 lines
1.6 KiB
C++
49 lines
1.6 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
|
// Pure, hardware-independent HTTP Range header parser. Header-only so it compiles both on
|
|
// the ESP32 and on the host for native unit tests (see firmware/test, docs/testing.md).
|
|
#pragma once
|
|
|
|
#include <stddef.h>
|
|
|
|
namespace oshttp {
|
|
|
|
struct RangeResult {
|
|
bool partial; // a "bytes=" range was requested
|
|
bool satisfiable; // start is within the file
|
|
size_t start; // inclusive
|
|
size_t end; // inclusive
|
|
};
|
|
|
|
// Parse a Range header value like "bytes=START-END" (END optional) against a known total
|
|
// size. If header is null/empty or not a bytes range, returns partial=false (send full
|
|
// body). A start past the end sets satisfiable=false (caller sends 416).
|
|
inline RangeResult parseByteRange(const char *header, size_t total) {
|
|
RangeResult r{false, true, 0, total ? total - 1 : 0};
|
|
if (!header) return r;
|
|
|
|
const char *prefix = "bytes=";
|
|
for (size_t i = 0; prefix[i]; ++i) {
|
|
if (header[i] != prefix[i]) return r; // not a bytes range -> full body
|
|
}
|
|
r.partial = true;
|
|
|
|
const char *s = header + 6;
|
|
size_t start = 0;
|
|
while (*s >= '0' && *s <= '9') { start = start * 10 + (*s - '0'); ++s; }
|
|
if (*s != '-') { r.partial = false; return r; } // malformed -> full body
|
|
++s;
|
|
|
|
size_t end = total ? total - 1 : 0;
|
|
bool anyEnd = false;
|
|
size_t ev = 0;
|
|
while (*s >= '0' && *s <= '9') { ev = ev * 10 + (*s - '0'); ++s; anyEnd = true; }
|
|
if (anyEnd) end = ev;
|
|
|
|
if (start >= total) { r.satisfiable = false; r.start = start; r.end = end; return r; }
|
|
if (end >= total) end = total ? total - 1 : 0;
|
|
r.start = start;
|
|
r.end = end;
|
|
return r;
|
|
}
|
|
|
|
} // namespace oshttp
|