Files
poorBird/firmware/include/net_mgr.h
T

41 lines
1.6 KiB
C

// net_mgr.h — WiFi association and clock.
//
// TIME (SPEC C7, and "timeserver is on endpoint")
// Two sources, in order of preference:
// 1. SNTP against the Pi. Cheap, standard, works before any HTTP call.
// 2. The `server_time` field every ingest/heartbeat response carries. This
// is the fallback that matters: if SNTP is blocked or the Pi is not
// running an NTP daemon after all, the device still converges on the
// endpoint's clock without extra configuration.
// Either way `received_at` on the Pi stays authoritative for ordering. The
// device timestamp is a hint, and it is explicitly 0 when unsynced rather than
// silently 1970.
#pragma once
#include <Arduino.h>
bool netBegin();
// Associate, with bounded retries and backoff. SPEC §4.3: never loop forever
// on a WiFi outage. Returns false after `max_attempts` failures; the caller
// decides whether that means sleep or retry later.
bool netConnect(uint8_t max_attempts = 3);
bool netIsConnected();
void netDisconnect();
// Start SNTP against ntp_host (or api_host if unset) and wait up to
// `timeout_ms` for the first sync.
bool netSyncTime(uint32_t timeout_ms = 5000);
// Accept the endpoint's clock from a response envelope. Only steps the RTC if
// we are unsynced or off by more than 2 s, so normal jitter does not cause a
// timestamp discontinuity mid-burst.
void netAdoptServerTime(int64_t server_ms);
bool netTimeValid();
int64_t netEpochMs(); // 0 when not yet synced — never a fake 1970 value
// "2026-09-20T14:31:07Z" into `out`. Empty string when unsynced.
void netIsoTime(char* out, size_t cap);