logging: net: update hostname

This commit adds a function that updates the hostname displayed by the
net backend. It is called by the network stack when the hostname is
updated.

Signed-off-by: Gerhard Jörges <joerges@metratec.com>
This commit is contained in:
Gerhard Jörges 2024-01-12 14:49:54 +01:00 committed by Carles Cufí
parent 13740696c7
commit 2a0e5e93f3
3 changed files with 32 additions and 0 deletions

View file

@ -27,6 +27,25 @@ extern "C" {
*/
bool log_backend_net_set_addr(const char *addr);
/**
* @brief update the hostname
*
* @details This function allows to update the hostname displayed by the logging backend. It will be
* called by the network stack if the hostname is set with net_hostname_set().
*
* @param hostname new hostname as char array.
* @param len Length of the hostname array.
*/
#if defined(CONFIG_NET_HOSTNAME_ENABLE)
void log_backend_net_hostname_set(char *hostname, size_t len);
#else
static inline void log_backend_net_hostname_set(const char *hostname, size_t len)
{
ARG_UNUSED(hostname);
ARG_UNUSED(len);
}
#endif
#ifdef __cplusplus
}
#endif

View file

@ -235,6 +235,14 @@ bool log_backend_net_set_addr(const char *addr)
return ret;
}
#if defined(CONFIG_NET_HOSTNAME_ENABLE)
void log_backend_net_hostname_set(char *hostname, size_t len)
{
(void)strncpy(dev_hostname, hostname, MIN(len, MAX_HOSTNAME_LEN));
log_output_hostname_set(&log_output_net, dev_hostname);
}
#endif
static void init_net(struct log_backend const *const backend)
{
ARG_UNUSED(backend);

View file

@ -16,6 +16,7 @@ LOG_MODULE_REGISTER(net_hostname, CONFIG_NET_HOSTNAME_LOG_LEVEL);
#include <zephyr/net/hostname.h>
#include <zephyr/net/net_core.h>
#include <zephyr/net/net_mgmt.h>
#include <zephyr/logging/log_backend_net.h>
static char hostname[NET_HOSTNAME_SIZE];
@ -30,6 +31,10 @@ static void trigger_net_event(void)
} else {
net_mgmt_event_notify(NET_EVENT_HOSTNAME_CHANGED, NULL);
}
if (IS_ENABLED(CONFIG_LOG_BACKEND_NET)) {
log_backend_net_hostname_set(hostname, sizeof(hostname));
}
}
const char *net_hostname_get(void)