#include "net_mgr.h" #include #include #include #include #include "app_config.h" static bool s_time_valid = false; // Anything before this is obviously not a real clock reading. static const int64_t kSaneEpochMs = 1735689600000LL; // 2025-01-01 bool netBegin() { WiFi.persistent(false); WiFi.mode(WIFI_STA); WiFi.setSleep(true); // DTIM-based modem sleep; harmless on mains WiFi.setAutoReconnect(true); WiFi.setHostname(g_cfg.device_name); setenv("TZ", g_cfg.tz, 1); // SPEC C7: set TZ explicitly, everywhere tzset(); return true; } bool netConnect(uint8_t max_attempts) { if (WiFi.status() == WL_CONNECTED) return true; if (!g_cfg.wifi_ssid[0]) { log_e("no WiFi SSID configured — set CFG_WIFI_SSID in secrets.ini"); return false; } for (uint8_t attempt = 1; attempt <= max_attempts; attempt++) { log_i("wifi: connecting to '%s' (attempt %u/%u)", g_cfg.wifi_ssid, attempt, max_attempts); WiFi.begin(g_cfg.wifi_ssid, g_cfg.wifi_pass); const uint32_t deadline = millis() + 10000; while (WiFi.status() != WL_CONNECTED && (int32_t)(deadline - millis()) > 0) { delay(100); } if (WiFi.status() == WL_CONNECTED) { log_i("wifi: %s rssi %d dBm", WiFi.localIP().toString().c_str(), WiFi.RSSI()); if (WiFi.RSSI() < -75) { // Worth surfacing: weak signal lengthens every transmit, which // on battery is a direct runtime cost (SPEC C5). log_w("wifi: RSSI %d dBm is marginal at this mounting point", WiFi.RSSI()); } return true; } WiFi.disconnect(true, false); // Exponential backoff, bounded. Never a tight retry loop (SPEC §4.3). if (attempt < max_attempts) delay(1000UL << attempt); } log_w("wifi: giving up after %u attempts", max_attempts); return false; } bool netIsConnected() { return WiFi.status() == WL_CONNECTED; } void netDisconnect() { WiFi.disconnect(true, false); WiFi.mode(WIFI_OFF); } bool netSyncTime(uint32_t timeout_ms) { const char* host = g_cfg.ntp_host[0] ? g_cfg.ntp_host : g_cfg.api_host; log_i("time: SNTP against %s", host); esp_sntp_stop(); esp_sntp_setoperatingmode(SNTP_OPMODE_POLL); esp_sntp_setservername(0, host); esp_sntp_init(); const uint32_t deadline = millis() + timeout_ms; while ((int32_t)(deadline - millis()) > 0) { if (esp_sntp_get_sync_status() == SNTP_SYNC_STATUS_COMPLETED || netEpochMs() > kSaneEpochMs) { s_time_valid = true; char iso[32]; netIsoTime(iso, sizeof(iso)); log_i("time: synced, %s", iso); return true; } delay(100); } // Not fatal. The next ingest/heartbeat response will carry server_time and // netAdoptServerTime() picks it up from there. log_w("time: SNTP did not complete, falling back to the API response clock"); return false; } void netAdoptServerTime(int64_t server_ms) { if (server_ms < kSaneEpochMs) return; const int64_t now = netEpochMs(); const int64_t drift = (now > server_ms) ? (now - server_ms) : (server_ms - now); if (s_time_valid && drift < 2000) return; // within tolerance, leave it struct timeval tv = { .tv_sec = (time_t)(server_ms / 1000), .tv_usec = (suseconds_t)((server_ms % 1000) * 1000), }; settimeofday(&tv, nullptr); s_time_valid = true; char iso[32]; netIsoTime(iso, sizeof(iso)); log_i("time: adopted endpoint clock (%s, drift was %lld ms)", iso, (long long)drift); } int64_t netEpochMs() { struct timeval tv; if (gettimeofday(&tv, nullptr) != 0) return 0; int64_t ms = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; return (ms > kSaneEpochMs) ? ms : 0; } bool netTimeValid() { return s_time_valid && netEpochMs() > 0; } void netIsoTime(char* out, size_t cap) { if (!cap) return; out[0] = '\0'; int64_t ms = netEpochMs(); if (ms == 0) return; time_t t = (time_t)(ms / 1000); struct tm gm; gmtime_r(&t, &gm); strftime(out, cap, "%Y-%m-%dT%H:%M:%SZ", &gm); }