net: hostname: trigger an event when the hostname changes

This commit introduces new network event that is triggered on every
change of the hostname.

Signed-off-by: Konrad Derda <konrad.derda@nordicsemi.no>
This commit is contained in:
Konrad Derda 2023-10-18 17:46:35 +02:00 committed by Maureen Helm
parent 2fa6a440ae
commit 3c39f7efd9
4 changed files with 41 additions and 3 deletions

View file

@ -27,6 +27,12 @@ extern "C" {
(IS_ENABLED(CONFIG_NET_HOSTNAME_UNIQUE) ? \
sizeof("0011223344556677") - 1 : 0))
#if defined(CONFIG_NET_HOSTNAME_ENABLE)
#define NET_HOSTNAME_SIZE NET_HOSTNAME_MAX_LEN + 1
#else
#define NET_HOSTNAME_SIZE 1
#endif
/**
* @brief Get the device hostname
*

View file

@ -13,6 +13,7 @@
#define ZEPHYR_INCLUDE_NET_NET_EVENT_H_
#include <zephyr/net/net_ip.h>
#include <zephyr/net/hostname.h>
#ifdef __cplusplus
extern "C" {
@ -38,7 +39,6 @@ enum net_event_if_cmd {
NET_EVENT_IF_CMD_UP,
NET_EVENT_IF_CMD_ADMIN_DOWN,
NET_EVENT_IF_CMD_ADMIN_UP,
};
#define NET_EVENT_IF_DOWN \
@ -53,7 +53,6 @@ enum net_event_if_cmd {
#define NET_EVENT_IF_ADMIN_UP \
(_NET_EVENT_IF_BASE | NET_EVENT_IF_CMD_ADMIN_UP)
/* IPv6 Events */
#define _NET_IPV6_LAYER NET_MGMT_LAYER_L3
#define _NET_IPV6_CORE_CODE 0x060
@ -210,6 +209,7 @@ enum net_event_l4_cmd {
NET_EVENT_L4_CMD_DISCONNECTED,
NET_EVENT_L4_CMD_DNS_SERVER_ADD,
NET_EVENT_L4_CMD_DNS_SERVER_DEL,
NET_EVENT_L4_CMD_HOSTNAME_CHANGED,
};
#define NET_EVENT_L4_CONNECTED \
@ -224,6 +224,9 @@ enum net_event_l4_cmd {
#define NET_EVENT_DNS_SERVER_DEL \
(_NET_EVENT_L4_BASE | NET_EVENT_L4_CMD_DNS_SERVER_DEL)
#define NET_EVENT_HOSTNAME_CHANGED \
(_NET_EVENT_L4_BASE | NET_EVENT_L4_CMD_HOSTNAME_CHANGED)
/** @endcond */
/**
@ -282,6 +285,16 @@ struct net_event_ipv6_prefix {
uint32_t lifetime;
};
/**
* @brief Network Management event information structure
* Used to pass information on NET_EVENT_HOSTNAME_CHANGED event when
* CONFIG_NET_MGMT_EVENT_INFO is enabled and event generator pass the
* information.
*/
struct net_event_l4_hostname {
char hostname[NET_HOSTNAME_SIZE];
};
#ifdef __cplusplus
}
#endif

View file

@ -15,8 +15,22 @@ 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>
static char hostname[NET_HOSTNAME_MAX_LEN + 1];
static char hostname[NET_HOSTNAME_SIZE];
static void trigger_net_event(void)
{
if (IS_ENABLED(CONFIG_NET_MGMT_EVENT_INFO)) {
struct net_event_l4_hostname info;
memcpy(info.hostname, hostname, sizeof(hostname));
net_mgmt_event_notify_with_info(NET_EVENT_HOSTNAME_CHANGED, NULL,
&info, sizeof(info));
} else {
net_mgmt_event_notify(NET_EVENT_HOSTNAME_CHANGED, NULL);
}
}
const char *net_hostname_get(void)
{
@ -57,6 +71,7 @@ int net_hostname_set_postfix(const uint8_t *hostname_postfix,
#if !defined(CONFIG_NET_HOSTNAME_UNIQUE_UPDATE)
postfix_set = true;
#endif
trigger_net_event();
return 0;
}
@ -67,4 +82,5 @@ void net_hostname_init(void)
memcpy(hostname, CONFIG_NET_HOSTNAME, sizeof(CONFIG_NET_HOSTNAME) - 1);
NET_DBG("Hostname set to %s", CONFIG_NET_HOSTNAME);
trigger_net_event();
}

View file

@ -43,6 +43,9 @@ union net_mgmt_events {
struct net_event_ipv6_route ipv6_route;
#endif /* CONFIG_NET_IPV6_MLD */
#endif /* CONFIG_NET_IPV6 */
#if defined(CONFIG_NET_HOSTNAME_ENABLE)
struct net_event_l4_hostname hostname;
#endif /* CONFIG_NET_HOSTNAME_ENABLE */
char default_event[DEFAULT_NET_EVENT_INFO_SIZE];
};