From 30400c080e9e678fa4f5afe47979e1941e03a263 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Fri, 20 Oct 2023 17:54:05 +0100 Subject: [PATCH 1/9] Add sntp from dhcp Ensure the SNTP service is stopped before configure. Change default servers to seperate concerns. --- .../Networking.Sntp/lwip_sntp_default_options.h | 4 ++-- targets/ChibiOS/_Lwip/nf_lwipthread.c | 2 ++ targets/ChibiOS/_include/lwipopts.h | 4 ++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/DeviceInterfaces/Networking.Sntp/lwip_sntp_default_options.h b/src/DeviceInterfaces/Networking.Sntp/lwip_sntp_default_options.h index a55419f165..7a049193cd 100644 --- a/src/DeviceInterfaces/Networking.Sntp/lwip_sntp_default_options.h +++ b/src/DeviceInterfaces/Networking.Sntp/lwip_sntp_default_options.h @@ -3,8 +3,8 @@ // See LICENSE file in the project root for full license information. // -#define SNTP_SERVER0_DEFAULT_ADDRESS "0.pool.ntp.org" -#define SNTP_SERVER1_DEFAULT_ADDRESS "1.pool.ntp.org" +#define SNTP_SERVER0_DEFAULT_ADDRESS "pool.ntp.org" +#define SNTP_SERVER1_DEFAULT_ADDRESS "time.cloudflare.com" // update delay (default 1 hour) // (value in milliseconds) diff --git a/targets/ChibiOS/_Lwip/nf_lwipthread.c b/targets/ChibiOS/_Lwip/nf_lwipthread.c index da66ac9978..eeed6da56b 100644 --- a/targets/ChibiOS/_Lwip/nf_lwipthread.c +++ b/targets/ChibiOS/_Lwip/nf_lwipthread.c @@ -398,7 +398,9 @@ static THD_FUNCTION(lwip_thread, p) // setup SNTP #if SNTP_SERVER_DNS + sntp_stop(); sntp_setoperatingmode(SNTP_OPMODE_POLL); + sntp_servermode_dhcp(1); //try to get the ntp server from dhcp sntp_setservername(0, SNTP_SERVER0_DEFAULT_ADDRESS); sntp_setservername(1, SNTP_SERVER1_DEFAULT_ADDRESS); sntp_init(); diff --git a/targets/ChibiOS/_include/lwipopts.h b/targets/ChibiOS/_include/lwipopts.h index 113ec07305..e92e4439c8 100644 --- a/targets/ChibiOS/_include/lwipopts.h +++ b/targets/ChibiOS/_include/lwipopts.h @@ -435,6 +435,10 @@ #define LWIP_DHCP 1 #endif +#if !defined SNTP_GET_SERVERS_FROM_DHCP +#define SNTP_GET_SERVERS_FROM_DHCP 1 +#endif + /* ------------------------------------ ---------- AUTOIP options ---------- From 026c57b05f4be6a09808be98e6069484cc08ca32 Mon Sep 17 00:00:00 2001 From: nfbot Date: Fri, 20 Oct 2023 16:59:40 +0000 Subject: [PATCH 2/9] Code style fixes Automated fixes for code style. --- targets/ChibiOS/_Lwip/nf_lwipthread.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/targets/ChibiOS/_Lwip/nf_lwipthread.c b/targets/ChibiOS/_Lwip/nf_lwipthread.c index eeed6da56b..058c67e85e 100644 --- a/targets/ChibiOS/_Lwip/nf_lwipthread.c +++ b/targets/ChibiOS/_Lwip/nf_lwipthread.c @@ -400,7 +400,7 @@ static THD_FUNCTION(lwip_thread, p) #if SNTP_SERVER_DNS sntp_stop(); sntp_setoperatingmode(SNTP_OPMODE_POLL); - sntp_servermode_dhcp(1); //try to get the ntp server from dhcp + sntp_servermode_dhcp(1); // try to get the ntp server from dhcp sntp_setservername(0, SNTP_SERVER0_DEFAULT_ADDRESS); sntp_setservername(1, SNTP_SERVER1_DEFAULT_ADDRESS); sntp_init(); From 89b5f53ce6aba492502d69f17b14ceac557090ec Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Fri, 20 Oct 2023 18:52:26 +0100 Subject: [PATCH 3/9] Use a function for SNTP initialization Improve default MAC (set to developer) --- targets/ChibiOS/_Lwip/nf_lwipthread.c | 31 +++++++++++++++------------ targets/ChibiOS/_Lwip/nf_lwipthread.h | 15 +++++++------ 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/targets/ChibiOS/_Lwip/nf_lwipthread.c b/targets/ChibiOS/_Lwip/nf_lwipthread.c index eeed6da56b..44aa685d89 100644 --- a/targets/ChibiOS/_Lwip/nf_lwipthread.c +++ b/targets/ChibiOS/_Lwip/nf_lwipthread.c @@ -255,9 +255,7 @@ void lwipDefaultLinkUpCB(void *p) } #endif -#if SNTP_SERVER_DNS - sntp_init(); -#endif + initialize_sntp(); } void lwipDefaultLinkDownCB(void *p) @@ -279,9 +277,7 @@ void lwipDefaultLinkDownCB(void *p) } #endif -#if SNTP_SERVER_DNS sntp_stop(); -#endif } /** @@ -335,6 +331,8 @@ static THD_FUNCTION(lwip_thread, p) } else { + // FIXME: this is set to the default lwIP address which might not be the same as the opts. + // It currently defaults to the STM32 developer MAC. thisif.hwaddr[0] = LWIP_ETHADDR_0; thisif.hwaddr[1] = LWIP_ETHADDR_1; thisif.hwaddr[2] = LWIP_ETHADDR_2; @@ -396,15 +394,8 @@ static THD_FUNCTION(lwip_thread, p) chThdResume(&lwip_trp, MSG_OK); chThdSetPriority(LWIP_THREAD_PRIORITY); -// setup SNTP -#if SNTP_SERVER_DNS - sntp_stop(); - sntp_setoperatingmode(SNTP_OPMODE_POLL); - sntp_servermode_dhcp(1); //try to get the ntp server from dhcp - sntp_setservername(0, SNTP_SERVER0_DEFAULT_ADDRESS); - sntp_setservername(1, SNTP_SERVER1_DEFAULT_ADDRESS); - sntp_init(); -#endif + // setup SNTP + initialize_sntp(); while (true) { @@ -548,6 +539,18 @@ static void do_reconfigure(void *p) chSemSignal(&reconf->completion); } +static void initialize_sntp() +{ + sntp_stop(); + sntp_setoperatingmode(SNTP_OPMODE_POLL); + sntp_servermode_dhcp(1); //try to get the ntp server from dhcp +#if SNTP_SERVER_DNS + sntp_setservername(0, SNTP_SERVER0_DEFAULT_ADDRESS); + sntp_setservername(1, SNTP_SERVER1_DEFAULT_ADDRESS); +#endif + sntp_init(); +} + void lwipReconfigure(const lwipreconf_opts_t *opts) { lwip_reconf_params_t params; diff --git a/targets/ChibiOS/_Lwip/nf_lwipthread.h b/targets/ChibiOS/_Lwip/nf_lwipthread.h index 020b758102..ee7cd962c6 100644 --- a/targets/ChibiOS/_Lwip/nf_lwipthread.h +++ b/targets/ChibiOS/_Lwip/nf_lwipthread.h @@ -21,7 +21,7 @@ * @brief Default network interface hostname. */ #if !defined(LWIP_NETIF_HOSTNAME_STRING) || defined(__DOXYGEN__) -#define LWIP_NETIF_HOSTNAME_STRING "lwip" +#define LWIP_NETIF_HOSTNAME_STRING "nf-chibios-lwip" #endif /** @@ -91,42 +91,42 @@ * @brief MAC Address byte 0. */ #if !defined(LWIP_ETHADDR_0) || defined(__DOXYGEN__) -#define LWIP_ETHADDR_0 0xC2 +#define LWIP_ETHADDR_0 0x00 #endif /** * @brief MAC Address byte 1. */ #if !defined(LWIP_ETHADDR_1) || defined(__DOXYGEN__) -#define LWIP_ETHADDR_1 0xAF +#define LWIP_ETHADDR_1 0x80 #endif /** * @brief MAC Address byte 2. */ #if !defined(LWIP_ETHADDR_2) || defined(__DOXYGEN__) -#define LWIP_ETHADDR_2 0x51 +#define LWIP_ETHADDR_2 0xE1 #endif /** * @brief MAC Address byte 3. */ #if !defined(LWIP_ETHADDR_3) || defined(__DOXYGEN__) -#define LWIP_ETHADDR_3 0x03 +#define LWIP_ETHADDR_3 0x01 #endif /** * @brief MAC Address byte 4. */ #if !defined(LWIP_ETHADDR_4) || defined(__DOXYGEN__) -#define LWIP_ETHADDR_4 0xCF +#define LWIP_ETHADDR_4 0x35 #endif /** * @brief MAC Address byte 5. */ #if !defined(LWIP_ETHADDR_5) || defined(__DOXYGEN__) -#define LWIP_ETHADDR_5 0x46 +#define LWIP_ETHADDR_5 0xD1 #endif /** @@ -248,6 +248,7 @@ extern "C" void lwipDefaultLinkDownCB(void *p); void lwipInit(const lwipthread_opts_t *opts); void lwipReconfigure(const lwipreconf_opts_t *opts); + static void initialize_sntp(void); #ifdef __cplusplus } #endif From db7cb5d27d842889ece5425b9033c83f218d4054 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Mon, 23 Oct 2023 10:59:06 +0100 Subject: [PATCH 4/9] Update targets/ChibiOS/_Lwip/nf_lwipthread.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: José Simões --- targets/ChibiOS/_Lwip/nf_lwipthread.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/targets/ChibiOS/_Lwip/nf_lwipthread.c b/targets/ChibiOS/_Lwip/nf_lwipthread.c index 47ad37999f..f27b321beb 100644 --- a/targets/ChibiOS/_Lwip/nf_lwipthread.c +++ b/targets/ChibiOS/_Lwip/nf_lwipthread.c @@ -543,7 +543,8 @@ static void initialize_sntp() { sntp_stop(); sntp_setoperatingmode(SNTP_OPMODE_POLL); - sntp_servermode_dhcp(1); // try to get the ntp server from dhcp + // try to get the ntp server from dhcp + sntp_servermode_dhcp(1); #if SNTP_SERVER_DNS sntp_setservername(0, SNTP_SERVER0_DEFAULT_ADDRESS); sntp_setservername(1, SNTP_SERVER1_DEFAULT_ADDRESS); From 2bc94a1142f387be3367801ef82f4ec6cc4e550f Mon Sep 17 00:00:00 2001 From: nfbot Date: Mon, 23 Oct 2023 10:01:29 +0000 Subject: [PATCH 5/9] Code style fixes Automated fixes for code style. --- targets/ChibiOS/_Lwip/nf_lwipthread.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/targets/ChibiOS/_Lwip/nf_lwipthread.c b/targets/ChibiOS/_Lwip/nf_lwipthread.c index f27b321beb..60203fab71 100644 --- a/targets/ChibiOS/_Lwip/nf_lwipthread.c +++ b/targets/ChibiOS/_Lwip/nf_lwipthread.c @@ -543,7 +543,7 @@ static void initialize_sntp() { sntp_stop(); sntp_setoperatingmode(SNTP_OPMODE_POLL); - // try to get the ntp server from dhcp + // try to get the ntp server from dhcp sntp_servermode_dhcp(1); #if SNTP_SERVER_DNS sntp_setservername(0, SNTP_SERVER0_DEFAULT_ADDRESS); From cfcdc0fb486b337144f6ae8d5ef0dbd85b29a1e4 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Mon, 23 Oct 2023 16:25:06 +0100 Subject: [PATCH 6/9] Move SNTP_GET_SERVERS_FROM_DHCP to sntp options Add to other platforms --- .../Networking.Sntp/nf_lwipopts_sntp.h | 10 +++++++++- targets/ChibiOS/_Lwip/nf_lwipthread.c | 2 ++ targets/ChibiOS/_include/lwipopts.h | 4 ---- targets/ESP32/_common/targetHAL_Network.cpp | 6 ++++++ .../_nanoCLR/targetSimpleLinkCC32xx_Threads.c | 8 ++++++++ 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/DeviceInterfaces/Networking.Sntp/nf_lwipopts_sntp.h b/src/DeviceInterfaces/Networking.Sntp/nf_lwipopts_sntp.h index 939e48e89d..424f03a011 100644 --- a/src/DeviceInterfaces/Networking.Sntp/nf_lwipopts_sntp.h +++ b/src/DeviceInterfaces/Networking.Sntp/nf_lwipopts_sntp.h @@ -25,7 +25,15 @@ extern "C" #endif // SNTP servers can be IP or full address and are resolved at -#define SNTP_SERVER_DNS 1 +#if !defined SNTP_SERVER_DNS +#define SNTP_SERVER_DNS 1 +#endif + +// SNTP servers can be discovered from DHCP +#if !defined SNTP_GET_SERVERS_FROM_DHCP +#define SNTP_GET_SERVERS_FROM_DHCP 1 +#endif + // use two servers #define SNTP_MAX_SERVERS 2 diff --git a/targets/ChibiOS/_Lwip/nf_lwipthread.c b/targets/ChibiOS/_Lwip/nf_lwipthread.c index 60203fab71..6041ed0982 100644 --- a/targets/ChibiOS/_Lwip/nf_lwipthread.c +++ b/targets/ChibiOS/_Lwip/nf_lwipthread.c @@ -543,8 +543,10 @@ static void initialize_sntp() { sntp_stop(); sntp_setoperatingmode(SNTP_OPMODE_POLL); +#if SNTP_GET_SERVERS_FROM_DHCP // try to get the ntp server from dhcp sntp_servermode_dhcp(1); +#endif #if SNTP_SERVER_DNS sntp_setservername(0, SNTP_SERVER0_DEFAULT_ADDRESS); sntp_setservername(1, SNTP_SERVER1_DEFAULT_ADDRESS); diff --git a/targets/ChibiOS/_include/lwipopts.h b/targets/ChibiOS/_include/lwipopts.h index e92e4439c8..113ec07305 100644 --- a/targets/ChibiOS/_include/lwipopts.h +++ b/targets/ChibiOS/_include/lwipopts.h @@ -435,10 +435,6 @@ #define LWIP_DHCP 1 #endif -#if !defined SNTP_GET_SERVERS_FROM_DHCP -#define SNTP_GET_SERVERS_FROM_DHCP 1 -#endif - /* ------------------------------------ ---------- AUTOIP options ---------- diff --git a/targets/ESP32/_common/targetHAL_Network.cpp b/targets/ESP32/_common/targetHAL_Network.cpp index c9d1ca6408..8c1b105014 100644 --- a/targets/ESP32/_common/targetHAL_Network.cpp +++ b/targets/ESP32/_common/targetHAL_Network.cpp @@ -86,8 +86,14 @@ static void initialize_sntp() { sntp_stop(); sntp_setoperatingmode(SNTP_OPMODE_POLL); +#if SNTP_GET_SERVERS_FROM_DHCP + // try to get the ntp server from dhcp + sntp_servermode_dhcp(1); +#endif +#if SNTP_SERVER_DNS sntp_setservername(0, SNTP_SERVER0_DEFAULT_ADDRESS); sntp_setservername(1, SNTP_SERVER1_DEFAULT_ADDRESS); +#endif sntp_init(); } diff --git a/targets/TI_SimpleLink/_nanoCLR/targetSimpleLinkCC32xx_Threads.c b/targets/TI_SimpleLink/_nanoCLR/targetSimpleLinkCC32xx_Threads.c index e5995bb76b..1bc7fc4dc7 100644 --- a/targets/TI_SimpleLink/_nanoCLR/targetSimpleLinkCC32xx_Threads.c +++ b/targets/TI_SimpleLink/_nanoCLR/targetSimpleLinkCC32xx_Threads.c @@ -833,8 +833,16 @@ void *mainThread(void *arg) } #ifdef SL_APP_SNTP + sntp_stop(); + sntp_setoperatingmode(SNTP_OPMODE_POLL); +#if SNTP_GET_SERVERS_FROM_DHCP + // try to get the ntp server from dhcp + sntp_servermode_dhcp(1); +#endif +#if SNTP_SERVER_DNS sntp_setservername(0, SNTP_SERVER0_DEFAULT_ADDRESS); sntp_setservername(1, SNTP_SERVER1_DEFAULT_ADDRESS); +#endif sntp_init(); #endif From 025c2704b95ef92dc9c4d614d9262a01d8c80cf6 Mon Sep 17 00:00:00 2001 From: nfbot Date: Mon, 23 Oct 2023 15:27:23 +0000 Subject: [PATCH 7/9] Code style fixes Automated fixes for code style. --- src/DeviceInterfaces/Networking.Sntp/nf_lwipopts_sntp.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/DeviceInterfaces/Networking.Sntp/nf_lwipopts_sntp.h b/src/DeviceInterfaces/Networking.Sntp/nf_lwipopts_sntp.h index 424f03a011..e6a2062185 100644 --- a/src/DeviceInterfaces/Networking.Sntp/nf_lwipopts_sntp.h +++ b/src/DeviceInterfaces/Networking.Sntp/nf_lwipopts_sntp.h @@ -26,7 +26,7 @@ extern "C" // SNTP servers can be IP or full address and are resolved at #if !defined SNTP_SERVER_DNS -#define SNTP_SERVER_DNS 1 +#define SNTP_SERVER_DNS 1 #endif // SNTP servers can be discovered from DHCP @@ -34,7 +34,6 @@ extern "C" #define SNTP_GET_SERVERS_FROM_DHCP 1 #endif - // use two servers #define SNTP_MAX_SERVERS 2 From 20bfca705caca6eb560e150ce7fac225df0842c2 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Thu, 16 Nov 2023 18:36:36 +0000 Subject: [PATCH 8/9] make function non static. --- targets/ChibiOS/_lwIP/nf_lwipthread.c | 2 +- targets/ChibiOS/_lwIP/nf_lwipthread.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/targets/ChibiOS/_lwIP/nf_lwipthread.c b/targets/ChibiOS/_lwIP/nf_lwipthread.c index 3d63a88bf6..2053523d4e 100644 --- a/targets/ChibiOS/_lwIP/nf_lwipthread.c +++ b/targets/ChibiOS/_lwIP/nf_lwipthread.c @@ -539,7 +539,7 @@ static void do_reconfigure(void *p) chSemSignal(&reconf->completion); } -static void initialize_sntp() +void initialize_sntp() { sntp_stop(); sntp_setoperatingmode(SNTP_OPMODE_POLL); diff --git a/targets/ChibiOS/_lwIP/nf_lwipthread.h b/targets/ChibiOS/_lwIP/nf_lwipthread.h index ee7cd962c6..d890c2d830 100644 --- a/targets/ChibiOS/_lwIP/nf_lwipthread.h +++ b/targets/ChibiOS/_lwIP/nf_lwipthread.h @@ -248,7 +248,7 @@ extern "C" void lwipDefaultLinkDownCB(void *p); void lwipInit(const lwipthread_opts_t *opts); void lwipReconfigure(const lwipreconf_opts_t *opts); - static void initialize_sntp(void); + void initialize_sntp(void); #ifdef __cplusplus } #endif From 9558d4c03ae14884e9b1810e6dde1d2b4c302605 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Thu, 16 Nov 2023 19:55:13 +0000 Subject: [PATCH 9/9] move initialize_sntp() make static again. move above all functions that use it. --- targets/ChibiOS/_lwIP/nf_lwipthread.c | 30 +++++++++++++-------------- targets/ChibiOS/_lwIP/nf_lwipthread.h | 1 - 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/targets/ChibiOS/_lwIP/nf_lwipthread.c b/targets/ChibiOS/_lwIP/nf_lwipthread.c index 2053523d4e..6613797a55 100644 --- a/targets/ChibiOS/_lwIP/nf_lwipthread.c +++ b/targets/ChibiOS/_lwIP/nf_lwipthread.c @@ -236,6 +236,21 @@ struct netif *nf_getNetif() return &thisif; } +static void initialize_sntp() +{ + sntp_stop(); + sntp_setoperatingmode(SNTP_OPMODE_POLL); +#if SNTP_GET_SERVERS_FROM_DHCP + // try to get the ntp server from dhcp + sntp_servermode_dhcp(1); +#endif +#if SNTP_SERVER_DNS + sntp_setservername(0, SNTP_SERVER0_DEFAULT_ADDRESS); + sntp_setservername(1, SNTP_SERVER1_DEFAULT_ADDRESS); +#endif + sntp_init(); +} + void lwipDefaultLinkUpCB(void *p) { struct netif *ifc = (struct netif *)p; @@ -539,21 +554,6 @@ static void do_reconfigure(void *p) chSemSignal(&reconf->completion); } -void initialize_sntp() -{ - sntp_stop(); - sntp_setoperatingmode(SNTP_OPMODE_POLL); -#if SNTP_GET_SERVERS_FROM_DHCP - // try to get the ntp server from dhcp - sntp_servermode_dhcp(1); -#endif -#if SNTP_SERVER_DNS - sntp_setservername(0, SNTP_SERVER0_DEFAULT_ADDRESS); - sntp_setservername(1, SNTP_SERVER1_DEFAULT_ADDRESS); -#endif - sntp_init(); -} - void lwipReconfigure(const lwipreconf_opts_t *opts) { lwip_reconf_params_t params; diff --git a/targets/ChibiOS/_lwIP/nf_lwipthread.h b/targets/ChibiOS/_lwIP/nf_lwipthread.h index d890c2d830..9c8ba0075a 100644 --- a/targets/ChibiOS/_lwIP/nf_lwipthread.h +++ b/targets/ChibiOS/_lwIP/nf_lwipthread.h @@ -248,7 +248,6 @@ extern "C" void lwipDefaultLinkDownCB(void *p); void lwipInit(const lwipthread_opts_t *opts); void lwipReconfigure(const lwipreconf_opts_t *opts); - void initialize_sntp(void); #ifdef __cplusplus } #endif