From ee75db5a4d6f0fb7db585c3e3ee172ef3d2c6eb7 Mon Sep 17 00:00:00 2001 From: Kenneth Barbour Date: Sat, 4 Jul 2020 08:27:37 -0400 Subject: [PATCH] NTP disabled if no host set --- src/time.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/time.cpp b/src/time.cpp index 1f04af0..1dbee3f 100644 --- a/src/time.cpp +++ b/src/time.cpp @@ -9,7 +9,7 @@ #define DEFAULT_NTP_HOST "time.nist.gov" #define NTP_PACKET_SIZE 48 -enum state { INIT, SENT, IDLE, NO_HOST} ntp_state; +enum state { INIT, SENT, IDLE, NO_HOST, DISABLED} ntp_state; byte packet_buffer[NTP_PACKET_SIZE]; // NTP timestamp is in the first 48 bytes WiFiUDP udp; unsigned long time_offset = 0; @@ -19,11 +19,7 @@ IPAddress host; const char* ntp_get_hostname() { - const char* host_in_cfg = (const char*) Config._data.ntp_host; - if (host_in_cfg[0] == '\0') { - return DEFAULT_NTP_HOST; - } - return host_in_cfg; + return (const char*) Config._data.ntp_host; } void uweather_time_init() @@ -38,6 +34,8 @@ void uweather_time_init() void uweather_time_handle() { switch (ntp_state) { + case state::DISABLED: + return; case state::INIT: { if (!WiFi.isConnected()) @@ -45,6 +43,10 @@ void uweather_time_handle() // resolve ntp hostname const char* hostname = ntp_get_hostname(); + if (hostname[0] == '\0') { + ntp_state = state::DISABLED; + return; + } if (!WiFi.hostByName(hostname, host)) { ntp_state = state::NO_HOST; Serial.printf("Unable to resolve ntp host: %s\n", hostname);