Skip to content

Commit bad506e

Browse files
maass-hamburgkartben
authored andcommitted
net: config: init: sntp: use connection manager
Add option to use the connection manager to schedule a (re-) sync on connection and diabeling the work, when there is no connection. Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
1 parent 265eb71 commit bad506e

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

subsys/net/lib/config/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,5 +257,14 @@ config NET_CONFIG_SNTP_INIT_RESYNC_ON_FAILURE_INTERVAL
257257
If the SNTP request fails, then this is the interval to wait
258258
before trying again.
259259

260+
config NET_CONFIG_SNTP_INIT_USE_CONNECTION_MANAGER
261+
bool "Use connection manager to start and stop SNTP client"
262+
default y
263+
depends on NET_CONNECTION_MANAGER
264+
help
265+
If this option is set, then the connection manager is used to
266+
start and stop the SNTP server. This way an SNTP request is
267+
also sent everytime when the network connection is established.
268+
260269
endif # NET_CONFIG_SNTP_INIT_RESYNC
261270
endif # NET_CONFIG_CLOCK_SNTP_INIT

subsys/net/lib/config/init.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,8 @@ int net_config_init_app(const struct device *dev, const char *app_info)
546546
NET_ERR("Network initialization failed (%d)", ret);
547547
}
548548

549-
if (IS_ENABLED(CONFIG_NET_CONFIG_CLOCK_SNTP_INIT)) {
549+
if (IS_ENABLED(CONFIG_NET_CONFIG_CLOCK_SNTP_INIT) &&
550+
!IS_ENABLED(CONFIG_NET_CONFIG_SNTP_INIT_USE_CONNECTION_MANAGER)) {
550551
net_init_clock_via_sntp();
551552
}
552553

subsys/net/lib/config/init_clock_sntp.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ LOG_MODULE_DECLARE(net_config, CONFIG_NET_CONFIG_LOG_LEVEL);
1010

1111
#include <errno.h>
1212
#include <zephyr/net/net_if.h>
13+
#include <zephyr/net/net_mgmt.h>
1314
#include <zephyr/net/sntp.h>
1415
#include <zephyr/posix/time.h>
1516

@@ -86,3 +87,24 @@ static void sntp_resync_handler(struct k_work *work)
8687
LOG_DBG("Time resynced using SNTP");
8788
}
8889
#endif /* CONFIG_NET_CONFIG_SNTP_INIT_RESYNC */
90+
91+
#ifdef CONFIG_NET_CONFIG_SNTP_INIT_USE_CONNECTION_MANAGER
92+
static void l4_event_handler(uint32_t mgmt_event, struct net_if *iface, void *info,
93+
size_t info_length, void *user_data)
94+
{
95+
ARG_UNUSED(iface);
96+
ARG_UNUSED(info);
97+
ARG_UNUSED(info_length);
98+
ARG_UNUSED(user_data);
99+
100+
if (mgmt_event == NET_EVENT_L4_CONNECTED) {
101+
k_work_reschedule(&sntp_resync_work_handle, K_NO_WAIT);
102+
} else if (mgmt_event == NET_EVENT_L4_DISCONNECTED) {
103+
k_work_cancel_delayable(&sntp_resync_work_handle);
104+
}
105+
}
106+
107+
NET_MGMT_REGISTER_EVENT_HANDLER(sntp_init_event_handler,
108+
NET_EVENT_L4_CONNECTED | NET_EVENT_L4_DISCONNECTED,
109+
&l4_event_handler, NULL);
110+
#endif /* CONFIG_LOG_BACKEND_NET_USE_CONNECTION_MANAGER */

0 commit comments

Comments
 (0)