From 3447721d20146ba0447ee9998838266619e4c789 Mon Sep 17 00:00:00 2001
From: xengineering <me@xengineering.eu>
Date: Fri, 21 Mar 2025 22:41:35 +0100
Subject: fw: network: Set custom hostname

---
 fw/prj.conf      |  3 +++
 fw/src/network.c | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+)

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);
-- 
cgit v1.2.3-70-g09d2