194 lines
8.9 KiB
C
194 lines
8.9 KiB
C
// app_config.h — the device's entire runtime configuration.
|
|
//
|
|
// Three layers, later ones win:
|
|
// 1. compile-time defaults (platformio.ini -D flags)
|
|
// 2. NVS, written on first boot and on every accepted config push
|
|
// 3. `apply_config` command from the endpoint, carrying a config_version
|
|
//
|
|
// The endpoint is the source of truth. The device reports the config_version it
|
|
// is running in every request; the endpoint pushes a new config whenever those
|
|
// disagree. That keeps SPEC §3.4 ("Device settings pushed via apply_config")
|
|
// working without a separate settings-fetch round trip.
|
|
#pragma once
|
|
|
|
#include <Arduino.h>
|
|
#include <ArduinoJson.h>
|
|
|
|
enum StreamMode : uint8_t {
|
|
STREAM_WATCH = 0, // relay to viewers, do not persist
|
|
STREAM_RECORD = 1, // persist on the Pi, no live viewers
|
|
STREAM_BOTH = 2,
|
|
};
|
|
|
|
enum RadarMode : uint8_t {
|
|
RADAR_OUT_PIN_ONLY = 0, // trust the module's presence pin, ignore UART
|
|
RADAR_UART_ONLY = 1, // decode reports, apply distance/energy windows
|
|
RADAR_BOTH = 2, // OUT pin arms, UART report confirms (default)
|
|
};
|
|
|
|
enum NightBehaviour : uint8_t {
|
|
NIGHT_CAPTURE = 0, // keep working, using the night camera profile
|
|
NIGHT_IDLE = 1, // stay awake and reachable, but capture nothing
|
|
NIGHT_SLEEP = 2, // deep sleep until morning, even if deep sleep is off
|
|
};
|
|
|
|
enum NightlightMode : uint8_t {
|
|
NIGHTLIGHT_OFF = 0,
|
|
NIGHTLIGHT_ON_BURST = 1, // illuminate only while a burst is being captured
|
|
NIGHTLIGHT_ALWAYS = 2, // on for the whole night; kinder to wildlife
|
|
};
|
|
|
|
enum BatteryChemistry : uint8_t {
|
|
BATT_NONE = 0, // no cell wired; report voltage only, no percentage
|
|
BATT_LIION = 1, // Li-ion / LiPo, 4.20 V full
|
|
BATT_LIFEPO4 = 2, // LiFePO4, 3.65 V full — flat curve, see battery.cpp
|
|
};
|
|
|
|
// OV2640 tuning. Held twice (day and night) so the device can switch profiles
|
|
// at dusk without a round trip to the endpoint. Ranges are the sensor's own;
|
|
// see camera_mgr.cpp for what each one is worth on a bird camera.
|
|
struct CamTuning {
|
|
int8_t brightness; // -2..2
|
|
int8_t contrast; // -2..2
|
|
int8_t saturation; // -2..2
|
|
int8_t sharpness; // -2..2 (OV2640 support is patchy)
|
|
int8_t ae_level; // -2..2 exposure bias
|
|
uint8_t special_effect; // 0 none 1 negative 2 grayscale 3 red 4 green 5 blue 6 sepia
|
|
uint8_t wb_mode; // 0 auto 1 sunny 2 cloudy 3 office 4 home
|
|
uint8_t gainceiling; // 0=2x 1=4x 2=8x 3=16x 4=32x 5=64x 6=128x
|
|
uint8_t agc_gain; // 0..30, only used when gain_ctrl = 0
|
|
uint16_t aec_value; // 0..1200, only used when exposure_ctrl = 0
|
|
bool whitebal;
|
|
bool awb_gain;
|
|
bool exposure_ctrl; // false = manual, use aec_value
|
|
bool aec2; // DSP-assisted auto exposure
|
|
bool gain_ctrl; // false = manual, use agc_gain
|
|
bool bpc; // black pixel correction
|
|
bool wpc; // white pixel correction
|
|
bool raw_gma;
|
|
bool lenc; // lens shading correction
|
|
bool dcw; // downsize/crop enable
|
|
bool colorbar; // test pattern — bring-up only
|
|
};
|
|
|
|
struct AppConfig {
|
|
// --- identity & endpoint ---------------------------------------------
|
|
char device_name[32];
|
|
char wifi_ssid[33];
|
|
char wifi_pass[64];
|
|
char api_host[64]; // Pi's IP. Use a DHCP reservation, not mDNS.
|
|
uint16_t api_port;
|
|
char api_base[24]; // "/api/v1"
|
|
char device_token[65]; // issued by the endpoint at enrollment
|
|
// The secret used to (re-)enroll. Starts as the shared bootstrap secret
|
|
// from secrets.ini and is replaced, at first enrollment, by a per-device
|
|
// one issued by the endpoint. Deliberately NOT wiped: the device must be
|
|
// able to recover from a token rotation on its own, because reaching it
|
|
// over USB is not an option once it is mounted. See api_client.h.
|
|
char enroll_secret[65];
|
|
|
|
// --- time (SPEC C7: the endpoint is the time server) -------------------
|
|
char ntp_host[64]; // empty => use api_host
|
|
char tz[48]; // POSIX TZ string
|
|
|
|
// --- camera ------------------------------------------------------------
|
|
uint8_t frame_size; // framesize_t
|
|
uint8_t jpeg_quality; // 4..40, lower is better
|
|
bool vflip;
|
|
bool hmirror;
|
|
CamTuning cam_day;
|
|
CamTuning cam_night; // applied automatically between dusk and dawn
|
|
|
|
// --- capture / trigger ---------------------------------------------------
|
|
uint8_t ring_frames; // PSRAM history depth
|
|
uint8_t pre_roll; // frames sent from before the trigger
|
|
uint8_t post_roll; // frames sent after
|
|
uint16_t post_roll_gap_ms; // spacing between post-roll frames
|
|
uint16_t cooldown_s; // minimum gap between two trigger events
|
|
uint16_t idle_period_ms; // ring refresh period when nothing is happening
|
|
|
|
// --- radar ---------------------------------------------------------------
|
|
uint8_t radar_mode; // RadarMode
|
|
bool accept_stationary; // a motionless bird still counts
|
|
uint16_t min_distance_cm;
|
|
uint16_t max_distance_cm;
|
|
uint8_t min_energy; // 0..100, per-report energy floor
|
|
uint32_t radar_baud; // 0 = autodetect (recommended)
|
|
|
|
// --- live stream (SPEC C9 — plumbing built, off by default) --------------
|
|
bool stream_enabled;
|
|
uint8_t stream_frame_size; // usually lower than stills
|
|
uint8_t stream_jpeg_quality;
|
|
uint16_t stream_duration_s; // hard cap; the device stops itself
|
|
uint8_t stream_fps;
|
|
uint8_t stream_mode; // StreamMode
|
|
|
|
// Streaming and sleeping are mutually exclusive: a sleeping device cannot
|
|
// be told to start a stream (SPEC C1). With this set, enabling streaming
|
|
// keeps the device awake and reachable so `stream_start` arrives promptly.
|
|
// Clear it to let the device sleep anyway and accept the wake latency.
|
|
bool stream_blocks_sleep;
|
|
|
|
// --- night ---------------------------------------------------------------
|
|
// The window is minutes from local midnight. The endpoint knows the site's
|
|
// latitude, so the intended use is for the Pi to push real sunset/sunrise
|
|
// daily; the fixed defaults are only a fallback.
|
|
uint16_t night_start_min; // default 21:00
|
|
uint16_t night_end_min; // default 06:00
|
|
uint8_t night_behaviour; // NightBehaviour
|
|
uint8_t nightlight_mode; // NightlightMode
|
|
uint8_t nightlight_brightness; // 0..255 PWM duty
|
|
|
|
// --- battery / supply -----------------------------------------------------
|
|
uint8_t battery_chemistry; // BatteryChemistry
|
|
uint16_t battery_divider_x100; // divider ratio x100, e.g. 200 = 2.00:1
|
|
int16_t battery_offset_mv; // calibration trim, added after scaling
|
|
uint8_t battery_min_pct_ota; // SPEC C6: refuse to flash below this
|
|
uint8_t battery_warn_pct; // surfaced to the WebUI as a low alarm
|
|
|
|
// --- ops -----------------------------------------------------------------
|
|
uint16_t heartbeat_interval_s;
|
|
uint16_t status_interval_s; // POST /status cadence; 0 = only on events
|
|
bool ota_enabled;
|
|
|
|
// --- power (see power_mgr.h for what each mode gives up) ------------------
|
|
bool deep_sleep_enabled; // the master on/off switch
|
|
uint16_t deep_sleep_s; // timer wake interval while asleep
|
|
bool sleep_wake_on_radar; // also wake on the LD2410 OUT pin
|
|
uint16_t sleep_idle_timeout_s; // stay awake this long after the last event
|
|
uint16_t sleep_max_awake_s; // hard cap: sleep even if something is stuck
|
|
|
|
uint32_t config_version;
|
|
};
|
|
|
|
extern AppConfig g_cfg;
|
|
|
|
// Load from NVS, filling anything missing from the compile-time defaults.
|
|
void configLoad();
|
|
|
|
// Persist the current g_cfg to NVS. Returns false if the write failed.
|
|
bool configSave();
|
|
|
|
// Merge a config document pushed by the endpoint into g_cfg. Only keys present
|
|
// in `doc` are touched, so the endpoint can send partial updates. Returns true
|
|
// if anything actually changed (caller decides whether a reboot is needed).
|
|
bool configApplyJson(JsonObjectConst doc);
|
|
|
|
// True when a change needs the camera re-initialised rather than just applied.
|
|
bool configNeedsCameraRestart(const AppConfig& before, const AppConfig& after);
|
|
|
|
// Stable per-device identifier derived from the eFuse MAC. Survives reflashing,
|
|
// which is what makes it usable as the enrollment key.
|
|
const char* deviceUid();
|
|
|
|
// The shared bootstrap secret compiled in from secrets.ini. Kept reachable as
|
|
// a last-resort fallback: if the per-device re-enrollment secret in NVS is
|
|
// ever rejected, the device can still get back in with this one rather than
|
|
// needing a USB cable.
|
|
const char* configBootstrapSecret();
|
|
|
|
// Base URL without trailing slash, e.g. "http://192.168.1.50:8080/api/v1".
|
|
String apiBaseUrl();
|
|
|
|
void configDump();
|