From 294063ec3e8fa4eee6499310bc7e20903cdd730d Mon Sep 17 00:00:00 2001 From: Juraj Andrassy Date: Sat, 7 Oct 2023 13:41:11 +0200 Subject: [PATCH] Ethernet.setHostname added (hostname to send with DHCP request) --- src/Dhcp.cpp | 13 +++++++++++++ src/Ethernet.cpp | 13 +++++++++++-- src/Ethernet.h | 3 +++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Dhcp.cpp b/src/Dhcp.cpp index 2bfd584b..58763ae7 100644 --- a/src/Dhcp.cpp +++ b/src/Dhcp.cpp @@ -188,6 +188,8 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) // OPT - host name buffer[16] = hostName; + + if (_hostname == nullptr) { buffer[17] = strlen(HOST_NAME) + 6; // length of hostname + last 3 bytes of mac address strcpy((char*)&(buffer[18]), HOST_NAME); @@ -197,6 +199,12 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed) //put data in W5100 transmit buffer _dhcpUdpSocket.write(buffer, 30); + } else { + uint8_t len = strlen(_hostname); + buffer[17] = len; + _dhcpUdpSocket.write(buffer, 18); + _dhcpUdpSocket.write(_hostname, len); + } if (messageType == DHCP_REQUEST) { buffer[0] = dhcpRequestedIPaddr; @@ -420,6 +428,11 @@ IPAddress DhcpClass::getDnsServerIp() return IPAddress(_dhcpDnsServerIp); } +void DhcpClass::setHostname(const char* hostname) +{ + _hostname = hostname; +} + void DhcpClass::printByte(char * buf, uint8_t n ) { char *str = &buf[1]; diff --git a/src/Ethernet.cpp b/src/Ethernet.cpp index 8d9ce7fd..2cc7181a 100644 --- a/src/Ethernet.cpp +++ b/src/Ethernet.cpp @@ -28,8 +28,9 @@ DhcpClass* EthernetClass::_dhcp = NULL; int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) { - static DhcpClass s_dhcp; - _dhcp = &s_dhcp; + if (_dhcp == NULL) { + _dhcp = new DhcpClass(); + } // Initialise the basic info if (W5100.init() == 0) return 0; @@ -178,6 +179,14 @@ IPAddress EthernetClass::gatewayIP() return ret; } +void EthernetClass::setHostname(const char* hostname) +{ + if (_dhcp == NULL) { + _dhcp = new DhcpClass(); + } + _dhcp->setHostname(hostname); +} + void EthernetClass::setMACAddress(const uint8_t *mac_address) { SPI.beginTransaction(SPI_ETHERNET_SETTINGS); diff --git a/src/Ethernet.h b/src/Ethernet.h index 0045de88..7375c07d 100644 --- a/src/Ethernet.h +++ b/src/Ethernet.h @@ -97,6 +97,7 @@ class EthernetClass { static IPAddress gatewayIP(); static IPAddress dnsServerIP() { return _dnsServerAddress; } + void setHostname(const char* hostname); // only the pointer is stored! void setMACAddress(const uint8_t *mac_address); void setLocalIP(const IPAddress local_ip); void setSubnetMask(const IPAddress subnet); @@ -275,6 +276,7 @@ class DhcpClass { uint32_t _dhcpInitialTransactionId; uint32_t _dhcpTransactionId; uint8_t _dhcpMacAddr[6]; + const char* _hostname = nullptr; #ifdef __arm__ uint8_t _dhcpLocalIp[4] __attribute__((aligned(4))); uint8_t _dhcpSubnetMask[4] __attribute__((aligned(4))); @@ -314,6 +316,7 @@ class DhcpClass { int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 4000); int checkLease(); + void setHostname(const char* hostname); };