From 2b611de45327678fb46f178159a8127cd2e0a1e2 Mon Sep 17 00:00:00 2001 From: Tristan Seifert Date: Thu, 5 Sep 2024 11:55:32 -0700 Subject: [PATCH] API to configure syslog server address at runtime --- include/zephyr/logging/log_backend.h | 3 ++ subsys/logging/log_backend_net.c | 45 +++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/include/zephyr/logging/log_backend.h b/include/zephyr/logging/log_backend.h index 361d2cdceea9dd..4140bb49080c57 100644 --- a/include/zephyr/logging/log_backend.h +++ b/include/zephyr/logging/log_backend.h @@ -11,6 +11,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { @@ -297,6 +298,8 @@ static inline int log_backend_format_set(const struct log_backend *backend, uint return backend->api->format_set(backend, log_type); } +int log_backend_net_set_server(struct sockaddr *newAddr); + /** * @} */ diff --git a/subsys/logging/log_backend_net.c b/subsys/logging/log_backend_net.c index 7b99a05856f296..59e8593f303f3f 100644 --- a/subsys/logging/log_backend_net.c +++ b/subsys/logging/log_backend_net.c @@ -36,6 +36,9 @@ struct sockaddr server_addr; static bool panic_mode; static uint32_t log_format_current = CONFIG_LOG_BACKEND_NET_OUTPUT_DEFAULT; +// flag set when the log server has been overridden +static bool gAddressOverride = false; + const struct log_backend *log_backend_net_get(void); NET_PKT_SLAB_DEFINE(syslog_tx_pkts, CONFIG_LOG_BACKEND_NET_MAX_BUF); @@ -201,14 +204,17 @@ static void init_net(struct log_backend const *const backend) ARG_UNUSED(backend); int ret; - net_sin(&server_addr)->sin_port = htons(514); + // only apply from config if never set + if(!gAddressOverride) { + net_sin(&server_addr)->sin_port = htons(514); - ret = net_ipaddr_parse(CONFIG_LOG_BACKEND_NET_SERVER, - sizeof(CONFIG_LOG_BACKEND_NET_SERVER) - 1, - &server_addr); - if (ret == 0) { - LOG_ERR("Cannot configure syslog server address"); - return; + ret = net_ipaddr_parse(CONFIG_LOG_BACKEND_NET_SERVER, + sizeof(CONFIG_LOG_BACKEND_NET_SERVER) - 1, + &server_addr); + if (ret == 0) { + LOG_ERR("Cannot configure syslog server address"); + return; + } } log_backend_deactivate(log_backend_net_get()); @@ -236,3 +242,28 @@ const struct log_backend *log_backend_net_get(void) { return &log_backend_net; } + +/** + * @brief Set IP address of remote syslog server + * + * This must be called before the log backend is initialized. + * + * If not called, the default specified via config (CONFIG_LOG_BACKEND_NET_SERVER) is used. + */ +int log_backend_net_set_server(struct sockaddr *newAddr) { + if(!newAddr) { + return -EFAULT; + } + + // musn't be initialized yet + if(net_init_done) { + // TODO: we could probably force re-initializing the socket + return -EINVAL; + } + + // set it + server_addr = *newAddr; + gAddressOverride = true; + + return 0; +}