Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- add the ability to set the update delay for individual NTP servers #32

Closed
wants to merge 1 commit into from

Conversation

loph917
Copy link

@loph917 loph917 commented Apr 1, 2019

i hope this is useful. i made these changes 15 days ago and i'm sure the tree has changed since then.

@devyte
Copy link
Collaborator

devyte commented Apr 2, 2019

75K+ additions?? Is that correct?? I can't even see the changes in web github!

@d-a-v
Copy link
Owner

d-a-v commented Apr 2, 2019

This patch has to be redone with the correct upstream version of lwIP (which hasn't change recently, but given your diff you were probably not sync 15 days ago. Other diffs are generally less than 1KB. yours is 28MB).

cd tools/sdk/lwip2/builder/
git checkout master
git pull origin master
mv lwip2-src lwip2-src-delete-me-later
git submodule update --init   # <- restore the right lwip2-src

Now repatch it following esp8266/Arduino#5879 (comment)
(using lwip2-src-delete-me-later)

@loph917
Copy link
Author

loph917 commented Apr 2, 2019

i was afraid of that. i saw it was that big and i was confused. i followed the above instructions but i'm still getting the same results of a 28M file. when i started down this path i simply followed the instructions found somewhere here using mingw, etc.

sntp-update-delay-patches.zip

i don't know if this helps but here are the changes in simple diff format.

@d-a-v
Copy link
Owner

d-a-v commented Apr 2, 2019

the 0001 patch should not modify this file tools/sdk/lwip2/include/lwip/apps/sntp.h(esp8266-arduino repository) but src/include/lwip/apps/sntp.h (upstream lwIP repository in lwip2-src/)

esp8266-arduino repository is modified by make install.

You should rework your patches so they can be cleanly applied with

cd tools/sdk/lwip2/builder/
git checkout master
git pull origin master
# (^^ clean lwip2 submodule)

mv lwip2-src lwip2-src-delete-me-later
git submodule update --init   # <- restore the right lwip2-src
# (^^ clean sub-sub module with upstream real lwIP-v2)

cd lwip2-src
for p in ../patches/sntp-00*.patch; do patch -p1 < $p; done
# (^^ apply your patches only)

Once it works you can probably concatenate them into a single file

cat sntp-00*.patch > sntp-update-renew-delay-api.patch

and commit this file only.

@loph917
Copy link
Author

loph917 commented Apr 3, 2019

so i'm making a giant mess of this and i apologize. i have a love hate relationship with git and this particular attempt at using git is kicking my ass. i was able to push up a -v2 of something; i can't seem to make a new pull request or make this work the way it is supposed to.

the amount of change is relatively small and at this point it would take less time to simply paste in the changes!

@d-a-v
Copy link
Owner

d-a-v commented Apr 3, 2019

Here's your patch with correct paths.
I don't know why some parts of it don't apply cleanly but at least you have it nearly to what it should look like with only the necessary files.
builder/patches/sntp-update-delay.patch
Probably you'd better make another pull request from current master of this repository.
Rebuild is as simple as

tools/sdk/lwip2$ rm -rf builder/lwip2-src; make

edit: If you apply it by hand (only this one on clean lwIP sources), you can remake it with

tools/sdk/lwip2/builder/lwip2-src$ git diff < ../patches/sntp-update-delay-test.patch
diff --git a/src/include/lwip/apps/sntp.h b/src/include/lwip/apps/sntp.h
index 3c0f95f7..f4287232 100644
--- a/src/include/lwip/apps/sntp.h
+++ b/src/include/lwip/apps/sntp.h
@@ -63,6 +63,9 @@ u8_t sntp_getreachability(u8_t idx);
 #endif /* SNTP_MONITOR_SERVER_REACHABILITY */
 
 #if SNTP_SERVER_DNS
 void sntp_setservername(u8_t idx, const char *server);
+void sntp_setserver_update_delay(u8_t idx, u32_t update_delay);
+u32_t sntp_getserver_update_delay(u8_t idx);
+void sntp_setserver(u8_t idx, const ip_addr_t *addr);
 const char *sntp_getservername(u8_t idx);
 #endif /* SNTP_SERVER_DNS */
diff --git a/src/apps/sntp/sntp.c b/src/apps/sntp/sntp.c
index b7ff56ad..cee5c184 100644
--- a/src/apps/sntp/sntp.c
+++ b/src/apps/sntp/sntp.c
@@ -235,12 +235,13 @@ struct sntp_server {
   /** Reachability shift register as described in RFC 5905 */
   u8_t reachability;
 #endif /* SNTP_MONITOR_SERVER_REACHABILITY */
+  u32_t update_delay;
 };
 static struct sntp_server sntp_servers[SNTP_MAX_SERVERS];
 
-#if SNTP_GET_SERVERS_FROM_DHCP
+#if SNTP_GET_SERVERS_FROM_DHCP || SNTP_GET_SERVERS_FROM_DHCPV6
 static u8_t sntp_set_servers_from_dhcp;
-#endif /* SNTP_GET_SERVERS_FROM_DHCP */
+#endif /* SNTP_GET_SERVERS_FROM_DHCP || SNTP_GET_SERVERS_FROM_DHCPV6 */
 #if SNTP_SUPPORT_MULTIPLE_SERVERS
 /** The currently used server (initialized to 0) */
 static u8_t sntp_current_server;
@@ -516,7 +517,7 @@ sntp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr,
       /* Correct response, reset retry timeout */
       SNTP_RESET_RETRY_TIMEOUT();
 
-      sntp_update_delay = (u32_t)SNTP_UPDATE_DELAY;
+      sntp_update_delay = sntp_servers[sntp_current_server].update_delay;
       sys_timeout(sntp_update_delay, sntp_request, NULL);
       LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_recv: Scheduled next time request: %"U32_F" ms\n",
                                      sntp_update_delay));
@@ -648,6 +649,7 @@ void
 sntp_init(void)
 {
   /* LWIP_ASSERT_CORE_LOCKED(); is checked by udp_new() */
+  LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp_init: SNTP initialised\n"));
 
 #ifdef SNTP_SERVER_ADDRESS
 #if SNTP_SERVER_DNS
@@ -750,7 +752,7 @@ sntp_getreachability(u8_t idx)
 }
 #endif /* SNTP_MONITOR_SERVER_REACHABILITY */
 
-#if SNTP_GET_SERVERS_FROM_DHCP
+#if SNTP_GET_SERVERS_FROM_DHCP || SNTP_GET_SERVERS_FROM_DHCPV6
 /**
  * Config SNTP server handling by IP address, name, or DHCP; clear table
  * @param set_servers_from_dhcp enable or disable getting server addresses from dhcp
@@ -764,7 +766,7 @@ sntp_servermode_dhcp(int set_servers_from_dhcp)
     sntp_set_servers_from_dhcp = new_mode;
   }
 }
-#endif /* SNTP_GET_SERVERS_FROM_DHCP */
+#endif /* SNTP_GET_SERVERS_FROM_DHCP || SNTP_GET_SERVERS_FROM_DHCPV6 */
 
 /**
  * @ingroup sntp
@@ -785,6 +787,7 @@ sntp_setserver(u8_t idx, const ip_addr_t *server)
     }
 #if SNTP_SERVER_DNS
     sntp_servers[idx].name = NULL;
+	sntp_servers[idx].update_delay = SNTP_UPDATE_DELAY;
 #endif
   }
 }
@@ -816,6 +819,38 @@ dhcp_set_ntp_servers(u8_t num, const ip4_addr_t *server)
 }
 #endif /* LWIP_DHCP && SNTP_GET_SERVERS_FROM_DHCP */
 
+#if LWIP_IPV6_DHCP6 && SNTP_GET_SERVERS_FROM_DHCPV6
+/**
+ * Initialize one of the NTP servers by IP address, required by DHCPV6
+ *
+ * @param num the number of NTP server addresses to set must be < SNTP_MAX_SERVERS
+ * @param server array of IP address of the NTP servers to set
+ */
+void
+dhcp6_set_ntp_servers(u8_t num_ntp_servers, ip_addr_t* ntp_server_addrs)
+{
+  LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp: %s %u NTP server(s) via DHCPv6\n",
+                                 (sntp_set_servers_from_dhcp ? "Got" : "Rejected"),
+                                 num_ntp_servers));
+  if (sntp_set_servers_from_dhcp && num_ntp_servers) {
+    u8_t i;
+    for (i = 0; (i < num_ntp_servers) && (i < SNTP_MAX_SERVERS); i++) {
+      LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp: NTP server %u: %s\n",
+                                     i, ipaddr_ntoa(&ntp_server_addrs[i])));
+      sntp_setserver(i, &ntp_server_addrs[i]);
+    }
+    for (i = num_ntp_servers; i < SNTP_MAX_SERVERS; i++) {
+      sntp_setserver(i, NULL);
+    }
+  }
+}
+#endif /* LWIP_DHCPv6 && SNTP_GET_SERVERS_FROM_DHCPV6 */
+
+u8_t sntp_get_current_server()
+{
+	return sntp_current_server;
+}
+
 /**
  * @ingroup sntp
  * Obtain one of the currently configured by IP address (or DHCP) NTP servers
@@ -833,6 +868,27 @@ sntp_getserver(u8_t idx)
   return IP_ADDR_ANY;
 }
 
+void
+sntp_setserver_update_delay(u8_t idx, u32_t update_delay)
+{
+	LWIP_ASSERT_CORE_LOCKED();
+	if (idx < SNTP_MAX_SERVERS) {
+		if (update_delay < 15000) {
+			update_delay = SNTP_UPDATE_DELAY;
+		} else {
+			sntp_servers[idx].update_delay = update_delay;
+		}
+	}
+}
+
+u32_t sntp_getserver_update_delay(u8_t idx)
+{
+  if (idx < SNTP_MAX_SERVERS) {
+    return sntp_servers[idx].update_delay;
+  }
+  return (u32_t)NULL;
+}
+
 #if SNTP_SERVER_DNS
 /**
  * Initialize one of the NTP servers by name
@@ -846,6 +902,7 @@ sntp_setservername(u8_t idx, const char *server)
   LWIP_ASSERT_CORE_LOCKED();
   if (idx < SNTP_MAX_SERVERS) {
     sntp_servers[idx].name = server;
+	sntp_servers[idx].update_delay = SNTP_UPDATE_DELAY;
   }
 }

@d-a-v
Copy link
Owner

d-a-v commented Nov 9, 2020

I'm closing this one, SNTP API is now accessible in master.
Thanks !

@d-a-v d-a-v closed this Nov 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants