From fbed749cd316f1c9fe8dbe8a564b41f93cf0ac1f Mon Sep 17 00:00:00 2001 From: xengineering Date: Fri, 21 Mar 2025 22:15:46 +0100 Subject: fw: Use SLAAC for network configuration --- fw/prj.conf | 8 ++------ fw/src/syslog.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/fw/prj.conf b/fw/prj.conf index 9ac1b51..a046a89 100644 --- a/fw/prj.conf +++ b/fw/prj.conf @@ -8,14 +8,10 @@ CONFIG_SHELL_PROMPT_UART="[iot-contact] " CONFIG_NETWORKING=y CONFIG_NET_SHELL=y CONFIG_NET_SOCKETS=y +CONFIG_NET_CONNECTION_MANAGER=y CONFIG_LOG=y CONFIG_LOG_BACKEND_NET=y CONFIG_LOG_BACKEND_NET_SERVER="[2001:db8::2]:514" +CONFIG_LOG_BACKEND_NET_AUTOSTART=n CONFIG_LOG_MODE_DEFERRED=y - -# static IP address configuration is currently required for successful syslog -CONFIG_NET_CONFIG_SETTINGS=y -CONFIG_NET_CONFIG_NEED_IPV6=y -CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1" -CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3 diff --git a/fw/src/syslog.c b/fw/src/syslog.c index fb15ab8..3291c0e 100644 --- a/fw/src/syslog.c +++ b/fw/src/syslog.c @@ -4,20 +4,68 @@ * obtain one at https://mozilla.org/MPL/2.0/. */ +#include + #include #include #include +#include +#include +#include +#include +#include LOG_MODULE_REGISTER(syslog); -int init_syslog() +#define L4_EVENT_MASK (NET_EVENT_L4_CONNECTED | NET_EVENT_L4_DISCONNECTED) + +struct net_mgmt_event_callback l4_cb; +static K_SEM_DEFINE(network_connected, 0, 1); + +void l4_event_handler( + struct net_mgmt_event_callback *cb, + uint32_t event, + struct net_if *iface) { - for(int i=0; i<5; i++) { - LOG_INF("Informational message number %d", i); - k_msleep(1000); + switch (event) { + case NET_EVENT_L4_CONNECTED: + LOG_INF("Network connected"); + k_sem_give(&network_connected); + break; + case NET_EVENT_L4_DISCONNECTED: + LOG_INF("Network disconnected"); + break; + default: + break; } +} + +int init_network_monitoring(void) +{ + net_mgmt_init_event_callback(&l4_cb, l4_event_handler, L4_EVENT_MASK); + net_mgmt_add_event_callback(&l4_cb); return 0; } +SYS_INIT(init_network_monitoring, APPLICATION, 0); +int init_syslog(void) +{ + LOG_INF("Waiting for network ..."); + k_sem_take(&network_connected, K_FOREVER); + + LOG_INF("Enabling syslog backend"); + const struct log_backend *backend = log_backend_net_get(); + if (log_backend_is_active(backend) == false) { + /* flush log messages to ensure first syslog message is reproducible */ + while (log_process()); + log_backend_init(backend); + log_backend_enable(backend, backend->cb->ctx, CONFIG_LOG_MAX_LEVEL); + LOG_INF("Syslog backend enabled"); + } else { + LOG_INF("Syslog backend was already enabled"); + } + + return 0; +} SYS_INIT(init_syslog, APPLICATION, 50); -- cgit v1.2.3-70-g09d2