diff options
author | xengineering <me@xengineering.eu> | 2025-03-21 22:41:35 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2025-03-21 22:41:35 +0100 |
commit | 3447721d20146ba0447ee9998838266619e4c789 (patch) | |
tree | ba55f98cc4d6d3a13a20fb976a9f42b915be47ad | |
parent | 7447a3875ea64b1fd45aaae850085b0cac5c8e50 (diff) | |
download | iot-contact-3447721d20146ba0447ee9998838266619e4c789.tar iot-contact-3447721d20146ba0447ee9998838266619e4c789.tar.zst iot-contact-3447721d20146ba0447ee9998838266619e4c789.zip |
fw: network: Set custom hostname
-rw-r--r-- | fw/prj.conf | 3 | ||||
-rw-r--r-- | fw/src/network.c | 19 |
2 files changed, 22 insertions, 0 deletions
diff --git a/fw/prj.conf b/fw/prj.conf index ac120b9..3df3155 100644 --- a/fw/prj.conf +++ b/fw/prj.conf @@ -12,6 +12,9 @@ CONFIG_NET_CONNECTION_MANAGER=y CONFIG_NET_L2_ETHERNET_MGMT=y CONFIG_NET_TCP=y +CONFIG_NET_HOSTNAME_ENABLE=y +CONFIG_NET_HOSTNAME_DYNAMIC=y + CONFIG_LOG=y CONFIG_LOG_BACKEND_NET=y CONFIG_LOG_BACKEND_NET_SERVER="[2001:db8::2]:514" diff --git a/fw/src/network.c b/fw/src/network.c index 9ea4a40..6e6eb17 100644 --- a/fw/src/network.c +++ b/fw/src/network.c @@ -10,6 +10,7 @@ #include <zephyr/init.h> #include <zephyr/logging/log.h> +#include <zephyr/net/hostname.h> #include <zephyr/net/net_if.h> #include <zephyr/net/net_linkaddr.h> #include <zephyr/net/net_mgmt.h> @@ -21,6 +22,9 @@ LOG_MODULE_REGISTER(network); +#define HOSTNAME "iot-contact" + + /* will be read from an EEPROM chip in the future */ static const uint8_t mac_address[NET_ETH_ADDR_LEN] = {0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}; @@ -54,3 +58,18 @@ int init_mac_address(void) return 0; } SYS_INIT(init_mac_address, APPLICATION, 0); + +int init_hostname(void) +{ + LOG_DBG("Setting hostname"); + + int ret = net_hostname_set(HOSTNAME, sizeof(HOSTNAME)); + if (ret < 0) { + LOG_ERR("Failed to set hostname to '%s' (%d)", HOSTNAME, ret); + return ret; + } + + LOG_INF("Successfully set hostname to '%s'", HOSTNAME); + return 0; +} +SYS_INIT(init_hostname, APPLICATION, 1); |