|
| 1 | +From f7740c5ec7986dab71901704936a923899041720 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Channel59 < [email protected]> |
| 3 | +Date: Tue, 9 Jul 2024 23:54:43 +0200 |
| 4 | +Subject: [PATCH 2/2] Add methods for setting hostname -- from: https://github.com/ARMmbed/mbed-os/pull/15506/commits/979a9425499565e434db708317a4fc0dc3792af8 |
| 5 | + |
| 6 | +--- |
| 7 | + .../include/netsocket/EMACInterface.h | 8 ++++++ |
| 8 | + .../include/netsocket/NetworkInterface.h | 16 ++++++++++++ |
| 9 | + .../netsocket/include/netsocket/nsapi_types.h | 10 +++++++ |
| 10 | + .../netsocket/source/EMACInterface.cpp | 26 +++++++++++++++++++ |
| 11 | + .../netsocket/source/NetworkInterface.cpp | 10 +++++++ |
| 12 | + 5 files changed, 70 insertions(+) |
| 13 | + |
| 14 | +diff --git a/connectivity/netsocket/include/netsocket/EMACInterface.h b/connectivity/netsocket/include/netsocket/EMACInterface.h |
| 15 | +index 8cf47cb703..c06aeb850e 100644 |
| 16 | +--- a/connectivity/netsocket/include/netsocket/EMACInterface.h |
| 17 | ++++ b/connectivity/netsocket/include/netsocket/EMACInterface.h |
| 18 | +@@ -83,6 +83,12 @@ public: |
| 19 | + /** @copydoc NetworkInterface::disconnect */ |
| 20 | + nsapi_error_t disconnect() override; |
| 21 | + |
| 22 | ++ /** @copydoc NetworkInterface::get_hostname */ |
| 23 | ++ const char *get_hostname() override; |
| 24 | ++ |
| 25 | ++ /** @copydoc NetworkInterface::set_hostname */ |
| 26 | ++ nsapi_error_t set_hostname(const char *hostname) override; |
| 27 | ++ |
| 28 | + /** @copydoc NetworkInterface::get_mac_address */ |
| 29 | + const char *get_mac_address() override; |
| 30 | + |
| 31 | +@@ -146,6 +152,8 @@ protected: |
| 32 | + OnboardNetworkStack::Interface *_interface = nullptr; |
| 33 | + bool _dhcp = true; |
| 34 | + bool _blocking = true; |
| 35 | ++ bool _hostname_set = false; |
| 36 | ++ char _hostname[NSAPI_HOSTNAME_SIZE]; |
| 37 | + bool _hw_mac_addr_set = false; |
| 38 | + char _mac_address[NSAPI_MAC_SIZE]; |
| 39 | + char _ip_address[NSAPI_IPv6_SIZE] {}; |
| 40 | +diff --git a/connectivity/netsocket/include/netsocket/NetworkInterface.h b/connectivity/netsocket/include/netsocket/NetworkInterface.h |
| 41 | +index 9071a1e40b..0e2aa64c9d 100644 |
| 42 | +--- a/connectivity/netsocket/include/netsocket/NetworkInterface.h |
| 43 | ++++ b/connectivity/netsocket/include/netsocket/NetworkInterface.h |
| 44 | +@@ -90,6 +90,22 @@ public: |
| 45 | + */ |
| 46 | + virtual void set_as_default(); |
| 47 | + |
| 48 | ++ /** Get hostname. |
| 49 | ++ * |
| 50 | ++ * @return Hostname if configured, null otherwise |
| 51 | ++ */ |
| 52 | ++ virtual const char *get_hostname(); |
| 53 | ++ |
| 54 | ++ /** Set hostname. |
| 55 | ++ * |
| 56 | ++ * @param hostname Hostname string |
| 57 | ++ * @retval NSAPI_ERROR_OK on success |
| 58 | ++ * @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported |
| 59 | ++ * @retval NSAPI_ERROR_PARAMETER if hostname is not valid |
| 60 | ++ * @retval NSAPI_ERROR_BUSY if hostname can't be set |
| 61 | ++ */ |
| 62 | ++ virtual nsapi_error_t set_hostname(const char *hostname); |
| 63 | ++ |
| 64 | + /** Get the local MAC address. |
| 65 | + * |
| 66 | + * Provided MAC address is intended for info or debug purposes and |
| 67 | +diff --git a/connectivity/netsocket/include/netsocket/nsapi_types.h b/connectivity/netsocket/include/netsocket/nsapi_types.h |
| 68 | +index 3b496d5087..28dbcc9a38 100644 |
| 69 | +--- a/connectivity/netsocket/include/netsocket/nsapi_types.h |
| 70 | ++++ b/connectivity/netsocket/include/netsocket/nsapi_types.h |
| 71 | +@@ -196,6 +196,16 @@ typedef enum nsapi_security { |
| 72 | + */ |
| 73 | + #define NSAPI_IP_BYTES NSAPI_IPv6_BYTES |
| 74 | + |
| 75 | ++/** Maximum size of hostname |
| 76 | ++ * |
| 77 | ++ * According to RFC 1034 [1], Section 3.1 "Name space specifications and |
| 78 | ++ * terminology", 63 is the maximum size of a hostname. +1 for the string |
| 79 | ++ * terminator. |
| 80 | ++ * |
| 81 | ++ * [1] https://www.rfc-editor.org/rfc/rfc1034 |
| 82 | ++ */ |
| 83 | ++#define NSAPI_HOSTNAME_SIZE 64 |
| 84 | ++ |
| 85 | + /** Maximum size of MAC address representation |
| 86 | + */ |
| 87 | + #define NSAPI_MAC_SIZE 18 |
| 88 | +diff --git a/connectivity/netsocket/source/EMACInterface.cpp b/connectivity/netsocket/source/EMACInterface.cpp |
| 89 | +index f48bc0a185..26b7e29856 100644 |
| 90 | +--- a/connectivity/netsocket/source/EMACInterface.cpp |
| 91 | ++++ b/connectivity/netsocket/source/EMACInterface.cpp |
| 92 | +@@ -88,6 +88,32 @@ nsapi_error_t EMACInterface::disconnect() |
| 93 | + return NSAPI_ERROR_NO_CONNECTION; |
| 94 | + } |
| 95 | + |
| 96 | ++const char *EMACInterface::get_hostname() |
| 97 | ++{ |
| 98 | ++ if (_hostname_set) { |
| 99 | ++ return _hostname; |
| 100 | ++ } |
| 101 | ++ return nullptr; |
| 102 | ++} |
| 103 | ++ |
| 104 | ++nsapi_error_t EMACInterface::set_hostname(const char *hostname) |
| 105 | ++{ |
| 106 | ++ if (!hostname || strlen(hostname) > NSAPI_HOSTNAME_SIZE-1) { |
| 107 | ++ return NSAPI_ERROR_PARAMETER; |
| 108 | ++ } |
| 109 | ++ |
| 110 | ++ if (_interface) { |
| 111 | ++ // can't set hostname once initialized |
| 112 | ++ return NSAPI_ERROR_BUSY; |
| 113 | ++ } |
| 114 | ++ |
| 115 | ++ memset(_hostname, 0, NSAPI_HOSTNAME_SIZE); |
| 116 | ++ strncpy(_hostname, hostname, NSAPI_HOSTNAME_SIZE-1); |
| 117 | ++ _hostname_set = true; |
| 118 | ++ |
| 119 | ++ return NSAPI_ERROR_OK; |
| 120 | ++} |
| 121 | ++ |
| 122 | + const char *EMACInterface::get_mac_address() |
| 123 | + { |
| 124 | + if (_interface && _interface->get_mac_address(_mac_address, sizeof(_mac_address))) { |
| 125 | +diff --git a/connectivity/netsocket/source/NetworkInterface.cpp b/connectivity/netsocket/source/NetworkInterface.cpp |
| 126 | +index 0f237f0e19..649df0f9b3 100644 |
| 127 | +--- a/connectivity/netsocket/source/NetworkInterface.cpp |
| 128 | ++++ b/connectivity/netsocket/source/NetworkInterface.cpp |
| 129 | +@@ -29,6 +29,16 @@ void NetworkInterface::set_as_default() |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | ++const char *NetworkInterface::get_hostname() |
| 134 | ++{ |
| 135 | ++ return 0; |
| 136 | ++} |
| 137 | ++ |
| 138 | ++nsapi_error_t NetworkInterface::set_hostname(const char *hostname) |
| 139 | ++{ |
| 140 | ++ return NSAPI_ERROR_UNSUPPORTED; |
| 141 | ++} |
| 142 | ++ |
| 143 | + const char *NetworkInterface::get_mac_address() |
| 144 | + { |
| 145 | + return 0; |
| 146 | +-- |
| 147 | +2.34.1 |
| 148 | + |
0 commit comments