Commit Graph

43 Commits

Author SHA1 Message Date
cbbdd25ebb docs(spec): document server-push runtime tuning in design spec
Reflects the feature shipped on this branch: backend can push per-device
CV tuning in the heartbeat response, device validates + persists to NVS.
Removes the stale line_offset row from the operator-provisioning table
(moved into CVTuning, server-managed).

Also adds .agent/, firmware/.pio/, and graphify-out/ to .gitignore so
local working dirs and build artifacts don't get accidentally tracked.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-16 22:06:59 -07:00
e5eeea2b47 fix(reporter,docs): save config before applying; correct README trust-model
Reorder reporter_heartbeat so NVS persistence commits before in-RAM apply.
If save fails, log and return without touching runtime state; RAM and NVS
stay consistent on the prior version instead of diverging until reboot.

Rewrite README "Trust model" to state reality: reporting is plain HTTP and
HMAC signs only requests, not responses. A LAN-local MITM can push any
config that passes the device range validator. Add roadmap entry for
authenticated config push (HTTPS or signed envelope).
2026-04-16 17:47:41 -07:00
bcb02b6d73 docs(readme): document server-push config + roadmap entry for gated local portal 2026-04-16 17:43:00 -07:00
b4b3a56019 fix(reporter): accept int JSON for max_move + reject malformed config fields
- max_move now accepts bare int JSON literals (server may emit 12 vs 12.0)
- Present-but-wrong-type fields reject the whole update with a log,
  preventing cfg_version from advancing on malformed payloads
- Zero-init CVTuning& out in cv_get_tuning before mutex take for safety
2026-04-16 17:41:22 -07:00
21f3bc77d1 feat(reporter): apply server-pushed CV tuning from heartbeat response
Heartbeat POST now captures the response body (up to 2048 bytes) and
looks for a "config" object. If cfg_version advances past the stored
value and all tunable fields pass range validation, the new tuning is
applied to g_cv and persisted to NVS.

- cv_tuning_validate: pure range checker (cv.cpp)
- cv_apply_tuning / cv_get_tuning: mutex-guarded helpers in main.cpp
  exposed via cv_apply.h; 500 ms timeout, drop on contention
- post_json now returns int (HTTP status) and optionally captures the
  response body; existing callers check == 200
- heartbeat: parse → cfg_version check → override present fields →
  validate → apply → save. Silent no-op when server returns no config.
- 3 new native tests (15/15 pass). timercam flash 1,423,897 bytes
  (+9,828 vs baseline).
2026-04-16 17:34:34 -07:00
94d74e425c refactor(cv): read thresholds from runtime tuning + load from NVS on boot
cv_process and helpers (frame_diff, extract_blob, find_centroids) now read
diff_thresh, min_blob_px, max_move, max_missed, and line_offset from
state.tuning instead of file-scope static const constants. The four
thresholds are promoted to file-local constexpr defaults in cv.cpp
(CV_DEFAULT_*) and are no longer part of the public cv.h API — external
code can't depend on them.

cv_process signature drops the line_pct parameter; callers use
state.tuning.line_offset instead. This eliminates the drift hazard of
having two sources of truth (DeviceConfig.line_offset vs
CVTuning.line_offset); the former is deleted.

main.cpp now calls config_load_tuning(g_cv.tuning) after cv_init on boot
so previously persisted tuning survives reboot; logs whether tuning came
from NVS or defaults.

The legacy NVS key "line_offset" is intentionally left alone — harmless
and flash_device.py may still write it during provisioning. Migration is
out of scope.

Tests: 12/12 passing (11 existing + 1 new
test_cv_process_respects_runtime_min_blob proving the runtime-read path).
Flash: 1,414,069 bytes (89.9%).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-16 15:55:35 -07:00
a992bfe391 fix(config): make cv_ver atomic commit marker in config_save_tuning 2026-04-16 15:49:41 -07:00
e28a4c1863 feat(cv): add CVTuning struct and NVS persistence scaffolding
Adds CVTuning to CVState, populates defaults from existing file-scope
constants in cv_init, and introduces config_load_tuning/config_save_tuning
backed by the doorcounter NVS namespace. No runtime behavior change yet;
CV code still reads the existing constants (Task 2 will migrate reads).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-16 15:45:53 -07:00
9d5b588231 feat: production-ready firmware with BLE memory management, device_id fixes, and docs
- Reduce debug level to 1 (errors only) for production builds
- Replace BLE pause/resume with full deinit/reinit during HTTP uploads (~25KB freed)
- Add 60s boot report delay for fast post-deploy connectivity verification
- Add device_id to BLE batch and heartbeat request bodies
- Correct API host to http:// (plain HTTP, not HTTPS)
- Add HTTP response logging and CV entry/exit serial logging
- Create root README.md with operator setup and architecture overview
- Update design spec: HMAC format, BLE memory approach, request body shapes, reporting intervals

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-16 11:13:50 -07:00
4b671843b3 fix: three stack overflows crashing firmware on TimerCamera-F
loopTask: cv_init() created a CVState{} temporary (9KB background
array) on the stack — fixed by initializing members directly.

cam task: cv_process() had uint8_t fg[CV_PIXELS] (9KB) as a local
variable — made static, matching the existing fg_copy fix.

cam task stack bumped from 4096 to 8192 for headroom.

Also: switch to 4MB OTA partition table (TimerCamera-F has 4MB flash,
not 8MB), add CONFIG_ARDUINO_LOOP_STACK_SIZE=16384 build flag,
upload_speed=115200 and --no-stub for reliable CH340 flashing.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-15 10:58:06 -07:00
265fb727ab fix: flash_device.py — correct nvs_partition_gen module name
esp-idf-nvs-partition-gen installs as esp_idf_nvs_partition_gen,
not nvs_partition_gen.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-14 19:21:08 -07:00
135eb3b46c fix: HMAC format — match server POST\npath\ntimestamp\nsha256(body) scheme
- hmac_sign now takes method+path instead of device_id; builds message as
  method\npath\ntimestamp\nhex(sha256(body)) per server verify_device_hmac
- reporter: header renamed X-HMAC-Signature → X-Signature; passes "POST"+path
- test vector regenerated against new message format; timestamp-diff test updated
- .size() → .length() throughout (Arduino String has no size())

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-14 10:47:13 -07:00
6d41529570 feat: door counter firmware — camera CV, BLE, HMAC reporting, captive portal 2026-04-14 10:35:31 -07:00
8a00665e4c fix: ArduinoOTA init, reporter mutex, BLE lock scope, NVS type
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-14 10:33:23 -07:00
883b72be77 feat: ota_push.py operator firmware update script 2026-04-14 10:28:28 -07:00
36f4becbe9 fix: camera downscale — centered crop, explicit PSRAM frame buffer 2026-04-14 09:30:20 -07:00
e19ae22915 feat: camera module — OV3660 init and 96x96 grayscale capture
Add camera.h/camera.cpp for TimerCamera-F OV3660 init and box-filter
downscale to 96x96 grayscale. Add espressif/esp32-camera to lib_deps.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-14 09:27:16 -07:00
b3c8d1c044 feat: flash_device.py operator NVS provisioning script
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-14 07:40:03 -07:00
a8f036f25f fix: CameraRecord — reject negative entries/exits via Pydantic Field(ge=0)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-14 07:02:51 -07:00
910508194a feat: camera batch endpoint implementation and tests
Self-contained server stub and pytest tests for the /api/v1/camera/events/batch
endpoint, mirroring the BLE batch pattern with idempotent INSERT on
(device_id, period_start).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-14 07:01:40 -07:00
a432813444 feat: camera_records table migration
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-14 06:59:52 -07:00
121f7a0a0a fix: main.cpp — static frame buffer, mutex for cv state, NTP init guard
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-14 06:59:09 -07:00
49da51bc05 feat: main.cpp — FreeRTOS tasks, LED indicators, factory reset
Replaces empty stub with full application: camera+CV task on core 1 at
5 fps, hourly reporter task on core 0, WiFi reconnect loop, 5-second
factory reset via BOOT button (GPIO37), LED on GPIO2 for status.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-14 06:56:59 -07:00
29737d735a feat: WiFiManager captive portal provisioning
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-14 06:44:06 -07:00
988443f207 fix: reporter — correct re-buffer on POST failure, NTP guard, TLS note
- reporter_submit_camera/ble: cap batch to REPORTER_MAX_BUFFER before
  POST and assign whole capped batch back to buffer on failure, fixing
  silent record drop when batch > buffer capacity
- post_json: reject sends when ts < 1700000000 (clock not NTP-synced)
- post_json: add comment documenting intentional no-cert-validation

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-14 06:33:00 -07:00
244426ec8b feat: reporter — HMAC-signed hourly POST with 24-record offline buffer
Fix Arduino String .size() → .length() in hmac.cpp (pre-existing bug surfaced by compilation).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-14 06:28:24 -07:00
6422e052df fix: ble_scanner sha256_prefix — guard mbedTLS null info and setup failure 2026-04-14 06:26:20 -07:00
ccbbf689cf feat: BLE passive scanner with RSSI bucketing and MAC hashing
Add passive BLE scan module using NimBLE for WiFi coexistence. Tracks
unique devices per hour with SHA256-hashed MACs, RSSI bucketing
(near/mid/far), max concurrent count, and thread-safe collect/reset.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-14 06:24:49 -07:00
29808e07a6 fix: camera — null-check sensor handle before set_vflip/set_hmirror
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-13 17:34:38 -07:00
99756bdbaf feat: camera module — OV3660 init and 96x96 grayscale capture
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-13 17:33:10 -07:00
0a6470a096 feat: CV line-crossing entry/exit detection with tests
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-13 15:10:01 -07:00
655abc914b fix: CV find_centroids — static fg_copy to prevent 9KB stack allocation
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-13 14:41:11 -07:00
b664753596 feat: CV blob detection and centroid tracking
Add BFS flood-fill blob extraction, centroid finding, and nearest-neighbour track matching/spawning inside cv_process. Add test verifying a new blob spawns a track.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-13 14:39:23 -07:00
136b22bc1b fix: cv_init — replace memset with value-init to avoid UB on std::vector
Also fix stale path comment in test/test_cv/test_cv.cpp.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-13 14:33:12 -07:00
e6843584cf feat: CV module — frame diff + threshold (blob tracking TODO)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-13 14:26:34 -07:00
7662fc4c25 fix: HMAC module — mbedTLS error handling, hex guard, test docs
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-13 14:24:48 -07:00
47f3f6afef feat: HMAC-SHA256 signing module with native tests
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-13 14:20:24 -07:00
74bff0912b fix: config_save_wifi — always write both credentials
Replace short-circuit boolean evaluation of putString return values with
separate size_t variables so both writes always execute regardless of
whether the first succeeds.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-13 14:05:26 -07:00
d5afd0bd87 feat: config module — NVS read/write via Preferences
Add config.h/config.cpp for DeviceConfig NVS persistence using Arduino
Preferences library. Add minimal main.cpp stub. Fix partition table
overlap (nvs 0x6000→0x5000, otadata 0xf000→0xe000) so firmware builds.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-13 14:02:28 -07:00
f4d9e1b2a5 fix: update platformio.ini — OTA partitions, NimBLE, PSRAM flags
- Switch board to m5stack-timer-cam (confirmed in pio boards)
- Pin platform to espressif32@6.6.0
- Replace huge_app.csv with custom partitions_8mb_ota.csv (8MB + OTA)
- Add -DCONFIG_BT_NIMBLE_ENABLED=1 and -DCONFIG_SPIRAM_USE_MALLOC=1
- Add h2zero/NimBLE-Arduino@^1.4.2 to lib_deps
- Raise CORE_DEBUG_LEVEL from 1 → 3

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-13 13:52:33 -07:00
6c46ea26ab chore: init PlatformIO project for TimerCamera-F 2026-04-13 13:31:38 -07:00
46c0908798 chore: ignore .worktrees directory 2026-04-13 13:05:21 -07:00
95d9c7ef4c chore: initial commit — spec and implementation plan 2026-04-13 13:04:55 -07:00