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>
This commit is contained in:
Fin Maaß 2025-04-30 14:15:03 +02:00 committed by Benjamin Cabé
commit bad506eb7a
3 changed files with 33 additions and 1 deletions

View file

@ -257,5 +257,14 @@ config NET_CONFIG_SNTP_INIT_RESYNC_ON_FAILURE_INTERVAL
If the SNTP request fails, then this is the interval to wait
before trying again.
config NET_CONFIG_SNTP_INIT_USE_CONNECTION_MANAGER
bool "Use connection manager to start and stop SNTP client"
default y
depends on NET_CONNECTION_MANAGER
help
If this option is set, then the connection manager is used to
start and stop the SNTP server. This way an SNTP request is
also sent everytime when the network connection is established.
endif # NET_CONFIG_SNTP_INIT_RESYNC
endif # NET_CONFIG_CLOCK_SNTP_INIT

View file

@ -546,7 +546,8 @@ int net_config_init_app(const struct device *dev, const char *app_info)
NET_ERR("Network initialization failed (%d)", ret);
}
if (IS_ENABLED(CONFIG_NET_CONFIG_CLOCK_SNTP_INIT)) {
if (IS_ENABLED(CONFIG_NET_CONFIG_CLOCK_SNTP_INIT) &&
!IS_ENABLED(CONFIG_NET_CONFIG_SNTP_INIT_USE_CONNECTION_MANAGER)) {
net_init_clock_via_sntp();
}

View file

@ -10,6 +10,7 @@ LOG_MODULE_DECLARE(net_config, CONFIG_NET_CONFIG_LOG_LEVEL);
#include <errno.h>
#include <zephyr/net/net_if.h>
#include <zephyr/net/net_mgmt.h>
#include <zephyr/net/sntp.h>
#include <zephyr/posix/time.h>
@ -86,3 +87,24 @@ static void sntp_resync_handler(struct k_work *work)
LOG_DBG("Time resynced using SNTP");
}
#endif /* CONFIG_NET_CONFIG_SNTP_INIT_RESYNC */
#ifdef CONFIG_NET_CONFIG_SNTP_INIT_USE_CONNECTION_MANAGER
static void l4_event_handler(uint32_t mgmt_event, struct net_if *iface, void *info,
size_t info_length, void *user_data)
{
ARG_UNUSED(iface);
ARG_UNUSED(info);
ARG_UNUSED(info_length);
ARG_UNUSED(user_data);
if (mgmt_event == NET_EVENT_L4_CONNECTED) {
k_work_reschedule(&sntp_resync_work_handle, K_NO_WAIT);
} else if (mgmt_event == NET_EVENT_L4_DISCONNECTED) {
k_work_cancel_delayable(&sntp_resync_work_handle);
}
}
NET_MGMT_REGISTER_EVENT_HANDLER(sntp_init_event_handler,
NET_EVENT_L4_CONNECTED | NET_EVENT_L4_DISCONNECTED,
&l4_event_handler, NULL);
#endif /* CONFIG_LOG_BACKEND_NET_USE_CONNECTION_MANAGER */