Files
qtalker---/firmware/esp32p4-sensor-node/main/sensor_driver.h
Indiana 348b5fc778 feat(firmware): ESP32-P4 sensor node — Workstream I core skeleton
New firmware/esp32p4-sensor-node/ ESP-IDF (C, FreeRTOS) project skeleton
per docs/superpowers/specs/2026-07-23-esp32-sensor-node-design.md's
Workstream I:

- Wi-Fi station-mode connect with exponential-backoff reconnect
  (wifi_manager.c), credentials from a gitignored main/device_config.h
  the seeker fills in (template: device_config.h.example).
- Telemetry HTTP client (telemetry_client.c) POSTing the spec's exact
  contract shape to /api/device/telemetry with a Bearer token, via
  esp_http_client + cJSON.
- BME280 I2C driver (bme280.c) with Bosch's public double-precision
  compensation formulas, using ESP-IDF's newer driver/i2c_master.h API.
- LD2410 mmWave presence driver (ld2410.c) over UART, chosen over a
  plain PIR for its distance/motion data richness — its frame-offset
  parsing is flagged as the least-certain code in the firmware.
- sensor_driver_t registry (sensor_driver.h, sensor_registry.c) so new
  sensors are a new driver file + one array line, no main-loop changes.
- README.md: build steps, manual-config walkthrough, wiring/pinouts,
  and an explicit "what's verified vs. not" section plus a real
  hardware caveat (ESP32-P4 has no integrated Wi-Fi radio).

UNVERIFIED AGAINST REAL HARDWARE per the spec's honesty-policy note —
no ESP-IDF toolchain or physical boards available in this environment.
Syntax-checked with gcc against hand-written ESP-IDF API stubs (not
committed) as a best-effort substitute for a real idf.py build.

Workstream J (RTL-SDR experimental module) is explicitly out of scope
here; firmware/esp32p4-sensor-node/components/ is left in place for it.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-24 01:13:44 +00:00

67 lines
2.8 KiB
C

// Small internal sensor-driver interface.
//
// The whole point of this file: adding a new sensor type later should mean
// "write a new .c/.h pair implementing this interface, add one line to the
// registry array in sensor_registry.c" — never editing app_main.c's control
// flow or the telemetry POST loop. See sensor_registry.c for the array and
// README.md's "Adding a new sensor" section for a walkthrough.
//
// UNVERIFIED AGAINST REAL HARDWARE — see README.md.
#pragma once
#include <stddef.h>
#include "esp_err.h"
#include "cJSON.h"
#ifdef __cplusplus
extern "C" {
#endif
// Max length (including NUL) for a reading's sensor_type / unit strings.
// Generous for the sensor_type strings this firmware emits ("temperature",
// "humidity", "pressure", "presence", ...) with room for future ones.
#define SENSOR_READING_TYPE_MAXLEN 24
#define SENSOR_READING_UNIT_MAXLEN 16
// One entry of the backend contract's `readings` array:
// {"sensor_type": str, "value": number, "unit": str, "metadata": {}}
//
// `metadata` is optional. If non-NULL, ownership transfers to the caller
// that serializes the reading (telemetry_client.c frees it after building
// the request body) — a driver's read() must hand back a freshly-created
// cJSON object it does not touch again, never a shared/static one.
typedef struct {
char sensor_type[SENSOR_READING_TYPE_MAXLEN];
double value;
char unit[SENSOR_READING_UNIT_MAXLEN];
cJSON *metadata; // nullable; NULL serializes as {}
} sensor_reading_t;
// One registered sensor driver.
//
// name - short human-readable identifier, used only in log lines.
// init - one-time hardware bring-up (bus/peripheral init, sensor reset,
// presence/ID check). May be NULL if a driver needs no init step.
// Called once at boot, in registry array order, before Wi-Fi
// connects so a slow/hanging sensor bus can't block network
// bring-up indefinitely (each init should still apply its own
// reasonable internal timeout).
// read - sample the sensor and append up to `max_out` readings to `out`,
// writing the number actually written to `*out_count`. Called
// once per reporting cycle from the telemetry task. Must return
// ESP_OK even if it decides there is nothing new to report (set
// *out_count = 0) — returning an error is reserved for actual
// I/O failure (bus NACK, UART timeout with no valid frame, etc.),
// which the registry logs and treats as "this driver contributed
// nothing this cycle" without aborting the whole POST.
typedef struct sensor_driver {
const char *name;
esp_err_t (*init)(void);
esp_err_t (*read)(sensor_reading_t *out, size_t max_out, size_t *out_count);
} sensor_driver_t;
#ifdef __cplusplus
}
#endif