From b3ad8e9eb88d003f0a981a1cd7af38c64c611c49 Mon Sep 17 00:00:00 2001
From: Juraj Andrassy <juraj.andrassy@gmail.com>
Date: Fri, 1 Nov 2024 13:04:21 +0100
Subject: [PATCH] networking - move setHostame to base class SocketWrapper

---
 libraries/Ethernet/src/Ethernet.cpp           | 5 -----
 libraries/Ethernet/src/Ethernet.h             | 3 ---
 libraries/SocketWrapper/src/SocketHelpers.cpp | 6 ++++++
 libraries/SocketWrapper/src/SocketHelpers.h   | 3 +++
 libraries/WiFi/src/WiFi.cpp                   | 5 -----
 libraries/WiFi/src/WiFi.h                     | 3 ---
 6 files changed, 9 insertions(+), 16 deletions(-)

diff --git a/libraries/Ethernet/src/Ethernet.cpp b/libraries/Ethernet/src/Ethernet.cpp
index 05d4061fb..ab720fcaf 100644
--- a/libraries/Ethernet/src/Ethernet.cpp
+++ b/libraries/Ethernet/src/Ethernet.cpp
@@ -24,11 +24,6 @@ int arduino::EthernetClass::_begin(uint8_t *mac, unsigned long timeout, unsigned
   return (linkStatus() == LinkON ? 1 : 0);
 }
 
-int arduino::EthernetClass::setHostname(const char* hostname) {
-  eth_if->set_hostname(hostname);
-  return 1;
-}
-
 int arduino::EthernetClass::begin(uint8_t *mac, IPAddress ip) {
   IPAddress dns = ip;
   dns[3] = 1;
diff --git a/libraries/Ethernet/src/Ethernet.h b/libraries/Ethernet/src/Ethernet.h
index b554b3ef2..8783d651c 100644
--- a/libraries/Ethernet/src/Ethernet.h
+++ b/libraries/Ethernet/src/Ethernet.h
@@ -56,9 +56,6 @@ class EthernetClass : public MbedSocketClass {
   EthernetClass(EthernetInterface *_if)
     : eth_if(_if){};
 
-  // When using DHCP the hostname provided will be used.
-  int setHostname(const char* hostname);
-
   // Initialise the Ethernet shield to use the provided MAC address and
   // gain the rest of the configuration through DHCP.
   // Returns 0 if the DHCP configuration failed, and 1 if it succeeded
diff --git a/libraries/SocketWrapper/src/SocketHelpers.cpp b/libraries/SocketWrapper/src/SocketHelpers.cpp
index ada31434f..fdfa89000 100644
--- a/libraries/SocketWrapper/src/SocketHelpers.cpp
+++ b/libraries/SocketWrapper/src/SocketHelpers.cpp
@@ -15,6 +15,12 @@ String arduino::MbedSocketClass::macAddress() {
   return String(getNetwork()->get_mac_address());
 }
 
+int arduino::MbedSocketClass::setHostname(const char* hostname) {
+  NetworkInterface* interface = getNetwork();
+  interface->set_hostname(hostname);
+  return 1;
+}
+
 int arduino::MbedSocketClass::hostByName(const char* aHostname, IPAddress& aResult) {
   SocketAddress socketAddress = SocketAddress();
   nsapi_error_t returnCode = gethostbyname(getNetwork(), aHostname, &socketAddress);
diff --git a/libraries/SocketWrapper/src/SocketHelpers.h b/libraries/SocketWrapper/src/SocketHelpers.h
index c31988fde..f3400b59d 100644
--- a/libraries/SocketWrapper/src/SocketHelpers.h
+++ b/libraries/SocketWrapper/src/SocketHelpers.h
@@ -57,6 +57,9 @@ class MbedSocketClass {
         */
   void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);
 
+  // When using DHCP the hostname provided will be used.
+  int setHostname(const char* hostname);
+
   /* Change DNS Ip configuration
      *
      * param dns_server1: ip configuration for DNS server 1
diff --git a/libraries/WiFi/src/WiFi.cpp b/libraries/WiFi/src/WiFi.cpp
index 957a2966b..6ad89d619 100644
--- a/libraries/WiFi/src/WiFi.cpp
+++ b/libraries/WiFi/src/WiFi.cpp
@@ -96,11 +96,6 @@ int arduino::WiFiClass::begin(const char* ssid) {
   return begin(ssid, NULL, ENC_TYPE_NONE);
 }
 
-int arduino::WiFiClass::setHostname(const char* hostname) {
-  wifi_if->set_hostname(hostname);
-  return 1;
-}
-
 //Config Wifi to set Static IP && Disable DHCP
 void arduino::WiFiClass::config(const char* localip, const char* netmask, const char* gateway){
   SocketHelpers::config(IPAddress(localip), dnsIP(0), IPAddress(gateway), IPAddress(netmask));
diff --git a/libraries/WiFi/src/WiFi.h b/libraries/WiFi/src/WiFi.h
index dd01192a2..2a15528af 100644
--- a/libraries/WiFi/src/WiFi.h
+++ b/libraries/WiFi/src/WiFi.h
@@ -76,9 +76,6 @@ class WiFiClass : public MbedSocketClass {
      */
   int begin(const char* ssid, const char* passphrase, wl_enc_type security = ENC_TYPE_UNKNOWN);
 
-  // When using DHCP the hostname provided will be used.
-  int setHostname(const char* hostname);
-
   // Inherit config methods from the parent class
   using MbedSocketClass::config;