diff options
Diffstat (limited to 'fw/src')
-rw-r--r-- | fw/src/syslog.c | 56 |
1 files changed, 52 insertions, 4 deletions
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 <stdbool.h> + #include <zephyr/init.h> #include <zephyr/kernel.h> #include <zephyr/logging/log.h> +#include <zephyr/logging/log_backend.h> +#include <zephyr/logging/log_backend_net.h> +#include <zephyr/logging/log_ctrl.h> +#include <zephyr/logging/log_core.h> +#include <zephyr/net/conn_mgr_connectivity.h> 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); |