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 5 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
29 changes: 17 additions & 12 deletions targets/ChibiOS/_Lwip/nf_lwipthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,7 @@ void lwipDefaultLinkUpCB(void *p)
}
#endif

#if SNTP_SERVER_DNS
sntp_init();
#endif
initialize_sntp();
}

void lwipDefaultLinkDownCB(void *p)
Expand All @@ -279,9 +277,7 @@ void lwipDefaultLinkDownCB(void *p)
}
#endif

#if SNTP_SERVER_DNS
sntp_stop();
#endif
}

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -396,13 +394,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 Expand Up @@ -546,6 +539,18 @@ static void do_reconfigure(void *p)
chSemSignal(&reconf->completion);
}

static void initialize_sntp()
{
sntp_stop();
sntp_setoperatingmode(SNTP_OPMODE_POLL);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to lwIP docs, this is already the default, so there's no need for this call.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, I was just copying the mechinisum used by ESP32. Also, the particular line you are commenting on was already present (just moved so it was not duplicated).

sntp_servermode_dhcp(1); // try to get the ntp server from dhcp
networkfusion marked this conversation as resolved.
Show resolved Hide resolved
#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;
Expand Down
15 changes: 8 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the change in the default MAC address? This is was using ST default for development devices.

Copy link
Member Author

@networkfusion networkfusion Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure??? all of the STM targets set the dev mac to:
image
In targetHAL_ConfigurationManager.cpp Which is what I have adjusted to...

#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 Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions targets/ChibiOS/_include/lwipopts.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----------
Expand Down