Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ethernet.setHostname added (hostname to send with DHCP request) #233

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Dhcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -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];
Expand Down
13 changes: 11 additions & 2 deletions src/Ethernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/Ethernet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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);
};


Expand Down