diff options
author | xengineering <me@xengineering.eu> | 2025-03-23 17:53:24 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2025-03-26 21:18:17 +0100 |
commit | b073db4017008ceb638a9c23c8cc93e60a3a7fdb (patch) | |
tree | 3359897450d1a0d187eb1fc2c47f36a6475691fd /fw/app/src/syslog.c | |
parent | 6ebbc6cd9876744c09547bcb4ce2c39a89ce0f6c (diff) | |
download | iot-contact-b073db4017008ceb638a9c23c8cc93e60a3a7fdb.tar iot-contact-b073db4017008ceb638a9c23c8cc93e60a3a7fdb.tar.zst iot-contact-b073db4017008ceb638a9c23c8cc93e60a3a7fdb.zip |
fw: app: Move application firmware code here
This makes the structure of the `fw` folder more clear and separates
application-related code from bootloader- or rtos-related code.
Diffstat (limited to 'fw/app/src/syslog.c')
-rw-r--r-- | fw/app/src/syslog.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/fw/app/src/syslog.c b/fw/app/src/syslog.c new file mode 100644 index 0000000..b1a1077 --- /dev/null +++ b/fw/app/src/syslog.c @@ -0,0 +1,75 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * 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); + +#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) +{ + LOG_DBG("Executing L4 event handler"); + + switch (event) { + case NET_EVENT_L4_CONNECTED: + k_sem_give(&network_connected); + LOG_INF("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_DBG("Initializing syslog logging backend"); + + LOG_DBG("Waiting for network ..."); + k_sem_take(&network_connected, K_FOREVER); + + LOG_DBG("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); |