NetworkService: use NTP server from DHCP Option 42 when available
Call sntp_servermode_dhcp(1) before WiFi.begin() so that lwIP's DHCP client passes any NTP server address received in DHCP Option 42 to the SNTP client (slot 0). Replace configTzTime() with explicit setenv/tzset + sntp_setservername + sntp_init() to avoid overwriting the DHCP-provided server. Static fallback servers (0.ru.pool.ntp.org, ntp.ix.ru, ntp21.vniiftri.ru) are placed in slots 1-2 when DHCP provides a server, or slots 0-2 otherwise. This relies on CONFIG_LWIP_DHCP_GET_NTP_SRV=y, which is already enabled in the pre-built Arduino ESP32 core (framework-arduinoespressif32). Add debug logging of the initialised NTP server list, guarded by WIFI_DEBUG_LOGGING.
This commit is contained in:
@@ -282,6 +282,7 @@ void NetworkService::ensureWifi(bool network_required) {
|
||||
if (!_wifi_started) {
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.setSleep(toEspPowerSave(_prefs.wifi_powersave));
|
||||
sntp_servermode_dhcp(1);
|
||||
_wifi_started = true;
|
||||
}
|
||||
|
||||
@@ -297,7 +298,34 @@ void NetworkService::updateTimeSync() {
|
||||
}
|
||||
|
||||
if (!_sntp_started) {
|
||||
configTzTime("UTC0", "0.ru.pool.ntp.org", "ntp.ix.ru", "ntp21.vniiftri.ru");
|
||||
setenv("TZ", "UTC0", 1);
|
||||
tzset();
|
||||
|
||||
const ip_addr_t* dhcp_srv = sntp_getserver(0);
|
||||
bool have_dhcp_ntp = dhcp_srv && !ip_addr_isany(dhcp_srv);
|
||||
|
||||
if (have_dhcp_ntp) {
|
||||
sntp_setservername(1, "0.ru.pool.ntp.org");
|
||||
sntp_setservername(2, "ntp.ix.ru");
|
||||
} else {
|
||||
sntp_setservername(0, "0.ru.pool.ntp.org");
|
||||
sntp_setservername(1, "ntp.ix.ru");
|
||||
sntp_setservername(2, "ntp21.vniiftri.ru");
|
||||
}
|
||||
|
||||
sntp_init();
|
||||
|
||||
#if WIFI_DEBUG_LOGGING && ARDUINO
|
||||
for (int i = 0; i < SNTP_MAX_SERVERS; i++) {
|
||||
const ip_addr_t* srv = sntp_getserver(i);
|
||||
const char* name = sntp_getservername(i);
|
||||
if (name)
|
||||
Serial.printf("[NTP] server[%d] = %s\n", i, name);
|
||||
else if (srv && !ip_addr_isany(srv))
|
||||
Serial.printf("[NTP] server[%d] = %s (DHCP)\n", i, ipaddr_ntoa(srv));
|
||||
}
|
||||
#endif
|
||||
|
||||
_sntp_started = true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user