From 72a51c7ec45b1b02c1fc13c3a655792e898afa27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerhard=20J=C3=B6rges?= Date: Wed, 10 Jan 2024 14:20:07 +0100 Subject: [PATCH] net: add set hostname function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit adds a function to set the hostname on runtime. Signed-off-by: Gerhard Jörges --- include/zephyr/net/hostname.h | 31 +++++++++++++++++++++++++++---- subsys/net/Kconfig.hostname | 17 ++++++++++++++++- subsys/net/hostname.c | 21 +++++++++++++++++++-- 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/include/zephyr/net/hostname.h b/include/zephyr/net/hostname.h index 292c4d62d72..196e9ab18e7 100644 --- a/include/zephyr/net/hostname.h +++ b/include/zephyr/net/hostname.h @@ -22,10 +22,16 @@ extern "C" { * @{ */ -#define NET_HOSTNAME_MAX_LEN \ - (sizeof(CONFIG_NET_HOSTNAME) - 1 + \ - (IS_ENABLED(CONFIG_NET_HOSTNAME_UNIQUE) ? \ - sizeof("0011223344556677") - 1 : 0)) +#if defined(CONFIG_NET_HOSTNAME_MAX_LEN) +#define NET_HOSTNAME_MAX_LEN \ + MAX(CONFIG_NET_HOSTNAME_MAX_LEN, \ + (sizeof(CONFIG_NET_HOSTNAME) - 1 + \ + (IS_ENABLED(CONFIG_NET_HOSTNAME_UNIQUE) ? sizeof("0011223344556677") - 1 : 0))) +#else +#define NET_HOSTNAME_MAX_LEN \ + (sizeof(CONFIG_NET_HOSTNAME) - 1 + \ + (IS_ENABLED(CONFIG_NET_HOSTNAME_UNIQUE) ? sizeof("0011223344556677") - 1 : 0)) +#endif #if defined(CONFIG_NET_HOSTNAME_ENABLE) #define NET_HOSTNAME_SIZE NET_HOSTNAME_MAX_LEN + 1 @@ -49,6 +55,23 @@ static inline const char *net_hostname_get(void) } #endif /* CONFIG_NET_HOSTNAME_ENABLE */ +/** + * @brief Set the device hostname + * + * @param host new hostname as char array. + * @param len Length of the hostname array. + * + * @return 0 if ok, <0 on error + */ +#if defined(CONFIG_NET_HOSTNAME_DYNAMIC) +int net_hostname_set(char *host, size_t len); +#else +static inline int net_hostname_set(char *host, size_t len) +{ + return -ENOTSUP; +} +#endif + /** * @brief Initialize and set the device hostname. * diff --git a/subsys/net/Kconfig.hostname b/subsys/net/Kconfig.hostname index eb5f32756a2..eb082d93476 100644 --- a/subsys/net/Kconfig.hostname +++ b/subsys/net/Kconfig.hostname @@ -17,10 +17,25 @@ config NET_HOSTNAME help The string should be a valid hostname. +config NET_HOSTNAME_DYNAMIC + bool "Allow the hostname to be set by the application" + depends on !NET_HOSTNAME_UNIQUE_UPDATE + help + This will enable the net_hostname_set() function. NET_HOSTNAME + will be used as default hostname. + +config NET_HOSTNAME_MAX_LEN + int "The maximum allowed hostname length" + depends on NET_HOSTNAME_DYNAMIC + range 1 63 + default 63 + help + This will set the number of bytes allocateed for the hostname. + config NET_HOSTNAME_UNIQUE bool "Make hostname unique" help - This will append link address to hostname to create a unique + This will append link address to NET_HOSTNAME to create a unique hostname. For example, zephyr00005e005357 could be the hostname if this setting is enabled. diff --git a/subsys/net/hostname.c b/subsys/net/hostname.c index 073756eb077..fa22a11e598 100644 --- a/subsys/net/hostname.c +++ b/subsys/net/hostname.c @@ -37,6 +37,23 @@ const char *net_hostname_get(void) return hostname; } +#if defined(CONFIG_NET_HOSTNAME_DYNAMIC) +int net_hostname_set(char *host, size_t len) +{ + if (len > NET_HOSTNAME_MAX_LEN) { + return -ENOMEM; + } + + memcpy(hostname, host, len); + hostname[len] = 0; + + NET_DBG("New hostname %s", hostname); + trigger_net_event(); + + return 0; +} +#endif + #if defined(CONFIG_NET_HOSTNAME_UNIQUE) int net_hostname_set_postfix(const uint8_t *hostname_postfix, int postfix_len) @@ -62,8 +79,8 @@ int net_hostname_set_postfix(const uint8_t *hostname_postfix, } for (i = 0; i < postfix_len; i++, pos += 2) { - snprintk(&hostname[sizeof(CONFIG_NET_HOSTNAME) - 1 + pos], - 2 + 1, "%02x", hostname_postfix[i]); + snprintk(&hostname[sizeof(CONFIG_NET_HOSTNAME) - 1 + pos], 2 + 1, "%02x", + hostname_postfix[i]); } NET_DBG("New hostname %s", hostname);