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

Added functions for setting and getting the hostname. #24

Open
wants to merge 3 commits 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
26 changes: 19 additions & 7 deletions src/Dhcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "Dhcp.h"
#include "utility/w5100.h"

char DhcpClass::_hostName[HOST_NAME_LENGTH] = HOST_NAME;

int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout)
{
_dhcpLeaseTime=0;
Expand Down Expand Up @@ -188,15 +190,15 @@ void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed)

// OPT - host name
buffer[16] = hostName;
buffer[17] = strlen(HOST_NAME) + 6; // length of hostname + last 3 bytes of mac address
strcpy((char*)&(buffer[18]), HOST_NAME);
buffer[17] = strlen(_hostName) + 6; // length of hostname + last 3 bytes of mac address
strcpy((char*)&(buffer[18]), _hostName);

printByte((char*)&(buffer[24]), _dhcpMacAddr[3]);
printByte((char*)&(buffer[26]), _dhcpMacAddr[4]);
printByte((char*)&(buffer[28]), _dhcpMacAddr[5]);
printByte((char*)&(buffer[(18 + strlen(_hostName))]), _dhcpMacAddr[3]);
printByte((char*)&(buffer[(20 + strlen(_hostName))]), _dhcpMacAddr[4]);
printByte((char*)&(buffer[(22 + strlen(_hostName))]), _dhcpMacAddr[5]);

//put data in W5100 transmit buffer
_dhcpUdpSocket.write(buffer, 30);
//put data in W5100 transmit buffer
_dhcpUdpSocket.write(buffer, (24 + strlen(_hostName)));

if (messageType == DHCP_REQUEST) {
buffer[0] = dhcpRequestedIPaddr;
Expand Down Expand Up @@ -431,3 +433,13 @@ void DhcpClass::printByte(char * buf, uint8_t n )
*str-- = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
}

void DhcpClass::setHostName(const char * newHostName)
{
strncpy(_hostName, newHostName, HOST_NAME_LENGTH-1);
}

char * DhcpClass::getHostName()
{
return _hostName;
}
1 change: 1 addition & 0 deletions src/Dhcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#define MAGIC_COOKIE 0x63825363
#define MAX_DHCP_OPT 16

#define HOST_NAME_LENGTH 13
#define HOST_NAME "WIZnet"
#define DEFAULT_LEASE (900) //default lease time in seconds

Expand Down
11 changes: 11 additions & 0 deletions src/Ethernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ IPAddress EthernetClass::gatewayIP()
return ret;
}

void EthernetClass::setHostName(const char * newHostName)
{
_dhcp->setHostName(newHostName);
}

char * EthernetClass::getHostName()
{
return _dhcp->getHostName();
}

void EthernetClass::setMACAddress(const uint8_t *mac_address)
{
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
Expand Down Expand Up @@ -239,4 +249,5 @@ void EthernetClass::setRetransmissionCount(uint8_t num)




EthernetClass Ethernet;
14 changes: 13 additions & 1 deletion src/Ethernet.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@
// does not always seem to work in practice (maybe Wiznet bugs?)
//#define ETHERNET_LARGE_BUFFERS

// This is used to check from outside, if there is a function
// to set the hostname in the library. Inside the library it is not used.
#define SET_HOSTNAME_AVAILABLE

#include <Arduino.h>
#include "Client.h"
#include "Server.h"
#include "Udp.h"
#include "Dhcp.h"

enum EthernetLinkStatus {
Unknown,
Expand Down Expand Up @@ -108,6 +112,10 @@ class EthernetClass {
friend class EthernetClient;
friend class EthernetServer;
friend class EthernetUDP;

static void setHostName(const char * newHostName);
static char * getHostName();

private:
// Opens a socket(TCP or UDP or IP_RAW mode)
static uint8_t socketBegin(uint8_t protocol, uint16_t port);
Expand Down Expand Up @@ -297,6 +305,8 @@ class DhcpClass {
uint8_t _dhcp_state;
EthernetUDP _dhcpUdpSocket;

static char _hostName[HOST_NAME_LENGTH];

int request_DHCP_lease();
void reset_DHCP_lease();
void presend_DHCP();
Expand All @@ -313,9 +323,11 @@ class DhcpClass {

int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
int checkLease();
};

static void setHostName(const char * newHostName);
static char * getHostName();

};



Expand Down