|
| 1 | +From 75b34cc20a33c05cbc01b3fbdabbfc40ae034bef Mon Sep 17 00:00:00 2001 |
| 2 | +From: Guilherme Ricioli < [email protected]> |
| 3 | +Date: Mon, 15 Apr 2024 17:40:59 -0300 |
| 4 | +Subject: [PATCH] Add methods for setting hostname |
| 5 | + |
| 6 | +In the same way it is done for setting MAC address, add methods for |
| 7 | +setting hostname. The underlying network stack can then request this |
| 8 | +to the local DNS through DHCP. |
| 9 | +--- |
| 10 | + .../include/netsocket/EMACInterface.h | 8 ++++++ |
| 11 | + .../include/netsocket/NetworkInterface.h | 16 ++++++++++++ |
| 12 | + .../netsocket/include/netsocket/nsapi_types.h | 10 +++++++ |
| 13 | + .../netsocket/source/EMACInterface.cpp | 26 +++++++++++++++++++ |
| 14 | + .../netsocket/source/NetworkInterface.cpp | 10 +++++++ |
| 15 | + .../doubles/NetworkInterface_stub.cpp | 10 +++++++ |
| 16 | + .../test_NetworkInterface.cpp | 11 ++++++++ |
| 17 | + 7 files changed, 91 insertions(+) |
| 18 | + |
| 19 | +diff --git a/connectivity/netsocket/include/netsocket/EMACInterface.h b/connectivity/netsocket/include/netsocket/EMACInterface.h |
| 20 | +index 8cf47cb703..c06aeb850e 100644 |
| 21 | +--- a/connectivity/netsocket/include/netsocket/EMACInterface.h |
| 22 | ++++ b/connectivity/netsocket/include/netsocket/EMACInterface.h |
| 23 | +@@ -83,6 +83,12 @@ public: |
| 24 | + /** @copydoc NetworkInterface::disconnect */ |
| 25 | + nsapi_error_t disconnect() override; |
| 26 | + |
| 27 | ++ /** @copydoc NetworkInterface::get_hostname */ |
| 28 | ++ const char *get_hostname() override; |
| 29 | ++ |
| 30 | ++ /** @copydoc NetworkInterface::set_hostname */ |
| 31 | ++ nsapi_error_t set_hostname(const char *hostname) override; |
| 32 | ++ |
| 33 | + /** @copydoc NetworkInterface::get_mac_address */ |
| 34 | + const char *get_mac_address() override; |
| 35 | + |
| 36 | +@@ -146,6 +152,8 @@ protected: |
| 37 | + OnboardNetworkStack::Interface *_interface = nullptr; |
| 38 | + bool _dhcp = true; |
| 39 | + bool _blocking = true; |
| 40 | ++ bool _hostname_set = false; |
| 41 | ++ char _hostname[NSAPI_HOSTNAME_SIZE]; |
| 42 | + bool _hw_mac_addr_set = false; |
| 43 | + char _mac_address[NSAPI_MAC_SIZE]; |
| 44 | + char _ip_address[NSAPI_IPv6_SIZE] {}; |
| 45 | +diff --git a/connectivity/netsocket/include/netsocket/NetworkInterface.h b/connectivity/netsocket/include/netsocket/NetworkInterface.h |
| 46 | +index 9071a1e40b..81f6011950 100644 |
| 47 | +--- a/connectivity/netsocket/include/netsocket/NetworkInterface.h |
| 48 | ++++ b/connectivity/netsocket/include/netsocket/NetworkInterface.h |
| 49 | +@@ -90,6 +90,22 @@ public: |
| 50 | + */ |
| 51 | + virtual void set_as_default(); |
| 52 | + |
| 53 | ++ /** Get hostname. |
| 54 | ++ * |
| 55 | ++ * @return Hostname if configured, null otherwise |
| 56 | ++ */ |
| 57 | ++ virtual const char *get_hostname(); |
| 58 | ++ |
| 59 | ++ /** Set hostname. |
| 60 | ++ * |
| 61 | ++ * @param hostname Hostname string |
| 62 | ++ * @retval NSAPI_ERROR_OK on success |
| 63 | ++ * @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported |
| 64 | ++ * @retval NSAPI_ERROR_PARAMETER if hostname is not valid |
| 65 | ++ * @retval NSAPI_ERROR_BUSY if hostname couldn't be set |
| 66 | ++ */ |
| 67 | ++ virtual nsapi_error_t set_hostname(const char *hostname); |
| 68 | ++ |
| 69 | + /** Get the local MAC address. |
| 70 | + * |
| 71 | + * Provided MAC address is intended for info or debug purposes and |
| 72 | +diff --git a/connectivity/netsocket/include/netsocket/nsapi_types.h b/connectivity/netsocket/include/netsocket/nsapi_types.h |
| 73 | +index 3b496d5087..28dbcc9a38 100644 |
| 74 | +--- a/connectivity/netsocket/include/netsocket/nsapi_types.h |
| 75 | ++++ b/connectivity/netsocket/include/netsocket/nsapi_types.h |
| 76 | +@@ -196,6 +196,16 @@ typedef enum nsapi_security { |
| 77 | + */ |
| 78 | + #define NSAPI_IP_BYTES NSAPI_IPv6_BYTES |
| 79 | + |
| 80 | ++/** Maximum size of hostname |
| 81 | ++ * |
| 82 | ++ * According to RFC 1034 [1], Section 3.1 "Name space specifications and |
| 83 | ++ * terminology", 63 is the maximum size of a hostname. +1 for the string |
| 84 | ++ * terminator. |
| 85 | ++ * |
| 86 | ++ * [1] https://www.rfc-editor.org/rfc/rfc1034 |
| 87 | ++ */ |
| 88 | ++#define NSAPI_HOSTNAME_SIZE 64 |
| 89 | ++ |
| 90 | + /** Maximum size of MAC address representation |
| 91 | + */ |
| 92 | + #define NSAPI_MAC_SIZE 18 |
| 93 | +diff --git a/connectivity/netsocket/source/EMACInterface.cpp b/connectivity/netsocket/source/EMACInterface.cpp |
| 94 | +index f48bc0a185..de8d9753d7 100644 |
| 95 | +--- a/connectivity/netsocket/source/EMACInterface.cpp |
| 96 | ++++ b/connectivity/netsocket/source/EMACInterface.cpp |
| 97 | +@@ -88,6 +88,32 @@ nsapi_error_t EMACInterface::disconnect() |
| 98 | + return NSAPI_ERROR_NO_CONNECTION; |
| 99 | + } |
| 100 | + |
| 101 | ++const char *EMACInterface::get_hostname() |
| 102 | ++{ |
| 103 | ++ if (_hostname_set) { |
| 104 | ++ return _hostname; |
| 105 | ++ } |
| 106 | ++ return nullptr; |
| 107 | ++} |
| 108 | ++ |
| 109 | ++nsapi_error_t EMACInterface::set_hostname(const char *hostname) |
| 110 | ++{ |
| 111 | ++ if (!hostname || strlen(hostname) > NSAPI_HOSTNAME_SIZE - 1) { |
| 112 | ++ return NSAPI_ERROR_PARAMETER; |
| 113 | ++ } |
| 114 | ++ |
| 115 | ++ if (_interface) { |
| 116 | ++ // can't set hostname once initialized |
| 117 | ++ return NSAPI_ERROR_BUSY; |
| 118 | ++ } |
| 119 | ++ |
| 120 | ++ memset(_hostname, 0, NSAPI_HOSTNAME_SIZE); |
| 121 | ++ strncpy(_hostname, hostname, NSAPI_HOSTNAME_SIZE - 1); |
| 122 | ++ _hostname_set = true; |
| 123 | ++ |
| 124 | ++ return NSAPI_ERROR_OK; |
| 125 | ++} |
| 126 | ++ |
| 127 | + const char *EMACInterface::get_mac_address() |
| 128 | + { |
| 129 | + if (_interface && _interface->get_mac_address(_mac_address, sizeof(_mac_address))) { |
| 130 | +diff --git a/connectivity/netsocket/source/NetworkInterface.cpp b/connectivity/netsocket/source/NetworkInterface.cpp |
| 131 | +index 0f237f0e19..649df0f9b3 100644 |
| 132 | +--- a/connectivity/netsocket/source/NetworkInterface.cpp |
| 133 | ++++ b/connectivity/netsocket/source/NetworkInterface.cpp |
| 134 | +@@ -29,6 +29,16 @@ void NetworkInterface::set_as_default() |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | ++const char *NetworkInterface::get_hostname() |
| 139 | ++{ |
| 140 | ++ return 0; |
| 141 | ++} |
| 142 | ++ |
| 143 | ++nsapi_error_t NetworkInterface::set_hostname(const char *hostname) |
| 144 | ++{ |
| 145 | ++ return NSAPI_ERROR_UNSUPPORTED; |
| 146 | ++} |
| 147 | ++ |
| 148 | + const char *NetworkInterface::get_mac_address() |
| 149 | + { |
| 150 | + return 0; |
| 151 | +diff --git a/connectivity/netsocket/tests/UNITTESTS/doubles/NetworkInterface_stub.cpp b/connectivity/netsocket/tests/UNITTESTS/doubles/NetworkInterface_stub.cpp |
| 152 | +index 020a551ba9..c849704a35 100644 |
| 153 | +--- a/connectivity/netsocket/tests/UNITTESTS/doubles/NetworkInterface_stub.cpp |
| 154 | ++++ b/connectivity/netsocket/tests/UNITTESTS/doubles/NetworkInterface_stub.cpp |
| 155 | +@@ -21,6 +21,16 @@ |
| 156 | + |
| 157 | + |
| 158 | + // Default network-interface state |
| 159 | ++const char *NetworkInterface::get_hostname() |
| 160 | ++{ |
| 161 | ++ return 0; |
| 162 | ++} |
| 163 | ++ |
| 164 | ++nsapi_error_t NetworkInterface::set_hostname(const char *hostname) |
| 165 | ++{ |
| 166 | ++ return NSAPI_ERROR_UNSUPPORTED; |
| 167 | ++} |
| 168 | ++ |
| 169 | + const char *NetworkInterface::get_mac_address() |
| 170 | + { |
| 171 | + return 0; |
| 172 | +diff --git a/connectivity/netsocket/tests/UNITTESTS/netsocket/NetworkInterface/test_NetworkInterface.cpp b/connectivity/netsocket/tests/UNITTESTS/netsocket/NetworkInterface/test_NetworkInterface.cpp |
| 173 | +index 1a928c36ee..27433ffaa1 100644 |
| 174 | +--- a/connectivity/netsocket/tests/UNITTESTS/netsocket/NetworkInterface/test_NetworkInterface.cpp |
| 175 | ++++ b/connectivity/netsocket/tests/UNITTESTS/netsocket/NetworkInterface/test_NetworkInterface.cpp |
| 176 | +@@ -68,6 +68,17 @@ TEST_F(TestNetworkInterface, constructor) |
| 177 | + } |
| 178 | + |
| 179 | + // get_default_instance is tested along with the implementations of NetworkInterface. |
| 180 | ++TEST_F(TestNetworkInterface, get_hostname) |
| 181 | ++{ |
| 182 | ++ char *n = 0; |
| 183 | ++ EXPECT_EQ(iface->get_hostname(), n); |
| 184 | ++} |
| 185 | ++ |
| 186 | ++TEST_F(TestNetworkInterface, set_hostname) |
| 187 | ++{ |
| 188 | ++ char *hostname; |
| 189 | ++ EXPECT_EQ(iface->set_hostname(hostname), NSAPI_ERROR_UNSUPPORTED); |
| 190 | ++} |
| 191 | + |
| 192 | + TEST_F(TestNetworkInterface, get_mac_address) |
| 193 | + { |
| 194 | +-- |
| 195 | +2.45.2 |
| 196 | + |
0 commit comments