From 4756daedaefd18919e96fadaabc7f1a00b383b4c Mon Sep 17 00:00:00 2001 From: Luke Hoersten Date: Sat, 13 Jun 2026 22:08:02 -0500 Subject: M2: HTTP server + mDNS service discovery + shared state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - viewport_state.{h,c}: shared state struct (config, run-state, counters, orientation, timestamps) behind a FreeRTOS mutex. Modules update it under viewport_state_lock(); GET /state serializes a snapshot. - http_api.{h,c}: starts esp_http_server on :80, registers GET /state. Returns the full JSON shape from the spec (name, version, configured, state, uptime_ms, last_frame_ms_ago, frames_received, decode_errors, state_post_failures, resolution, ip, free_heap, free_psram). - mdns_service.{h,c}: mdns_init + advertise _scrypted-viewport._tcp.local on :80 with version/resolution/orientation/name TXT records. mdns_service_refresh() reapplies hostname + TXT after /config writes (called from M4 onward). - main/CMakeLists.txt: add esp_http_server, esp_timer, json, mdns to REQUIRES. espressif/mdns added as managed component via main/idf_component.yml. app_main calls viewport_state_init early so any module can read defaults before /config arrives. Also adds TESTING.md tracking per-milestone verification status and an integration test plan to run after M9 (races, failure modes, longevity, power cycles, negative protocol, multi-viewport). M1 promoted to "🟡 builds clean" in TESTING.md; combined HW verification of M1+M2 will be one flash session. Build verified against ESP-IDF 5.4 for target esp32p4 (binary ~550 KB). --- main/viewport_state.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 main/viewport_state.c (limited to 'main/viewport_state.c') diff --git a/main/viewport_state.c b/main/viewport_state.c new file mode 100644 index 0000000..9115ce3 --- /dev/null +++ b/main/viewport_state.c @@ -0,0 +1,46 @@ +#include "viewport_state.h" + +#include + +#include "esp_timer.h" +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" + +static viewport_state_t s_state; +static SemaphoreHandle_t s_mutex; + +void viewport_state_init(void) +{ + memset(&s_state, 0, sizeof(s_state)); + s_state.configured = false; + s_state.state = VIEWPORT_STATE_UNCONFIGURED; + s_state.brightness = 80; + s_state.idle_timeout_ms = 60000; + s_state.orientation = VIEWPORT_ORIENTATION_PORTRAIT; + s_state.boot_us = (uint64_t)esp_timer_get_time(); + s_state.last_frame_us = -1; + + s_mutex = xSemaphoreCreateMutex(); +} + +viewport_state_t *viewport_state_get(void) +{ + return &s_state; +} + +void viewport_state_lock(void) +{ + xSemaphoreTake(s_mutex, portMAX_DELAY); +} + +void viewport_state_unlock(void) +{ + xSemaphoreGive(s_mutex); +} + +const char *viewport_state_resolution_str(void) +{ + return (s_state.orientation == VIEWPORT_ORIENTATION_PORTRAIT) + ? "480x800" + : "800x480"; +} -- cgit v1.2.3