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 SNTP from DHCP #2798

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/DeviceInterfaces/Networking.Sntp/nf_lwipopts_sntp.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ extern "C"
#endif

// SNTP servers can be IP or full address and are resolved at
#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
Expand Down
30 changes: 20 additions & 10 deletions targets/ChibiOS/_lwIP/nf_lwipthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -260,7 +275,7 @@ void lwipDefaultLinkUpCB(void *p)
// otherwise SNTP will be started when DHCP request succeeds
if (addressMode != NET_ADDRESS_DHCP)
{
sntp_init();
initialize_sntp();
}
#endif
}
Expand All @@ -284,9 +299,7 @@ void lwipDefaultLinkDownCB(void *p)
}
#endif

#if SNTP_SERVER_DNS
sntp_stop();
#endif
}

/**
Expand Down Expand Up @@ -337,6 +350,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;
Expand Down Expand Up @@ -398,13 +413,8 @@ static THD_FUNCTION(lwip_thread, p)
chThdResume(&lwip_trp, MSG_OK);
chThdSetPriority(LWIP_THREAD_PRIORITY);

// setup SNTP
#if SNTP_SERVER_DNS
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, SNTP_SERVER0_DEFAULT_ADDRESS);
sntp_setservername(1, SNTP_SERVER1_DEFAULT_ADDRESS);
sntp_init();
#endif
// setup SNTP
initialize_sntp();

while (true)
{
Expand Down
14 changes: 7 additions & 7 deletions targets/ChibiOS/_lwIP/nf_lwipthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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

/**
Expand Down
6 changes: 6 additions & 0 deletions targets/ESP32/_common/targetHAL_Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down