Browse Source

sntp: fix use of non-DHCP-provided SNTP server

sntp_setservername() doesn't seem to actually work right, so do it
"manually"...
H. Peter Anvin 2 years ago
parent
commit
9dacd3b215
4 changed files with 44 additions and 17 deletions
  1. 44 17
      esp32/max80/wifi.cpp
  2. BIN
      esp32/output/max80.ino.bin
  3. BIN
      fpga/output/v1.fw
  4. BIN
      fpga/output/v2.fw

+ 44 - 17
esp32/max80/wifi.cpp

@@ -11,7 +11,7 @@
 #include <esp_sntp.h>
 #include <esp_wifi.h>
 
-static String ssid, password, hostname, dnsserver, sntpserver;
+static String ssid, password, hostname, dnsserver;
 static TimerHandle_t sta_failure_timer;
 
 static bool sta_timeout_enabled;
@@ -61,12 +61,13 @@ static void sntp_sync_cb(struct timeval *tv)
     prev_sync_status = sync_status;
 }
 
-static void my_sntp_start()
+static void my_sntp_start(void)
 {
     setenv_bool("status.net.sntp.sync", false);
 
     if (getenv_bool("sntp.enabled")) {
 	sntp_set_time_sync_notification_cb(sntp_sync_cb);
+
 	sntp_setoperatingmode(SNTP_OPMODE_POLL);
 	sntp_servermode_dhcp(!getenv_bool("ip4.dhcp.nosntp"));
 	sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED); // Until first sync
@@ -92,6 +93,43 @@ static inline bool invalid_ip(const ip_addr_t *ip)
     return !memcmp(ip, &ip_addr_any, sizeof *ip);
 }
 
+static void sntp_server_show(void)
+{
+    const ip_addr_t *sntp_ip = sntp_getserver(0);
+
+    if (!invalid_ip(sntp_ip)) {
+	const char *sntp_server = inet_ntoa(*sntp_ip);
+	printf("[SNTP] Time server: %s\n", sntp_server);
+	setenv_cond("status.net.sntp.server", sntp_server);
+    } else {
+	unsetenv("status.net.sntp.server");
+    }
+}
+
+static void sntp_server_found(const char *name, const ip_addr_t *addr,
+			      void *arg)
+{
+    (void)name;
+    (void)arg;
+
+    if (invalid_ip(addr))
+	return;
+
+    sntp_setserver(0, addr);
+    sntp_server_show();
+}
+
+static void sntp_set_server(const char *name)
+{
+    if (!name || !*name)
+	return;
+
+    ip_addr_t addr;
+    err_t err = dns_gethostbyname(name, &addr, sntp_server_found, NULL);
+    if (err == ERR_OK)
+	sntp_server_found(name, &addr, NULL);
+}
+
 static void start_services(void)
 {
     /* Always run after (re)connect */
@@ -114,22 +152,11 @@ static void start_services(void)
 
     // If Arduino supported both of these at the same that would be
     // awesome, but it requires ESP-IDF reconfiguration...
-    if (sntp_enabled()) {
-	const ip_addr_t *sntp_ip = sntp_getserver(0);
-
-	if (invalid_ip(sntp_ip)) {
-	    if (sntpserver != "") {
-		sntp_setservername(0, sntpserver.c_str());
-		sntp_ip = sntp_getserver(0);
-	    }
-	}
-
-	if (!invalid_ip(sntp_ip)) {
-	    const char *sntp_server = inet_ntoa(*sntp_ip);
-	    printf("[SNTP] Time server: %s\n", sntp_server);
-	    setenv_cond("status.net.sntp.server", sntp_server);
+    if (getenv_bool("sntp.enabled")) {
+	if (!invalid_ip(sntp_getserver(0))) {
+	    sntp_server_show();
 	} else {
-	    unsetenv("status.net.sntp.server");
+	    sntp_set_server(getenv("sntp.server"));
 	}
     }
 

BIN
esp32/output/max80.ino.bin


BIN
fpga/output/v1.fw


BIN
fpga/output/v2.fw