diff --git a/include/zephyr/net/hostname.h b/include/zephyr/net/hostname.h index 292c4d62d7..196e9ab18e 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 eb5f32756a..eb082d9347 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 073756eb07..fa22a11e59 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);