-
Notifications
You must be signed in to change notification settings - Fork 270
Add DHCP hostname option #77
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
DHCP-based hostname printer | ||
|
||
Circuit: | ||
Ethernet shield attached to pins 10, 11, 12, 13 | ||
|
||
created 10 Dec 2016 | ||
by mykh | ||
*/ | ||
|
||
#include <Ethernet.h> | ||
|
||
// MAC address | ||
byte mac[] = { | ||
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 | ||
}; | ||
// Hostname | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add the max length of hostname to comment, because most people wont get in touch with the source code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also that just acii charset is allowed |
||
const char* hostname = "myarduino"; | ||
|
||
// Initialize the Ethernet client library | ||
EthernetClient client; | ||
|
||
void setup() { | ||
// Open serial communications and wait for port to open: | ||
Serial.begin(9600); | ||
// this check is only needed on the Leonardo: | ||
while (!Serial) { | ||
; | ||
} | ||
|
||
// start the Ethernet connection: | ||
Serial.println("Setup..."); | ||
while (Ethernet.begin(mac, hostname) == 0) { | ||
Serial.println("Failed to configure Ethernet using DHCP"); | ||
delay(10000); | ||
Serial.println("Reconnecting..."); | ||
} | ||
|
||
// print your hostname: | ||
Serial.print("My Hostname: "); | ||
Serial.println(Ethernet.hostname()); | ||
} | ||
|
||
void loop() { | ||
Ethernet.maintain(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,11 @@ | |
#include "utility/w5100.h" | ||
|
||
int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) | ||
{ | ||
return beginWithDHCP(mac, NULL, timeout, responseTimeout); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of |
||
} | ||
|
||
int DhcpClass::beginWithDHCP(uint8_t *mac, const char *hostname, unsigned long timeout, unsigned long responseTimeout) | ||
{ | ||
_dhcpLeaseTime=0; | ||
_dhcpT1=0; | ||
|
@@ -18,6 +23,20 @@ int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long | |
memset(_dhcpMacAddr, 0, 6); | ||
reset_DHCP_lease(); | ||
|
||
if (NULL == hostname) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
strcpy(_dhcpHostname, HOST_NAME); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should also use safe str func |
||
int offset = strlen(HOST_NAME); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if somebody modifies constant |
||
printByte((char*)&(_dhcpHostname[offset + 0]), mac[3]); | ||
printByte((char*)&(_dhcpHostname[offset + 2]), mac[4]); | ||
printByte((char*)&(_dhcpHostname[offset + 4]), mac[5]); | ||
_dhcpHostname[offset + 6] = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be |
||
} | ||
else | ||
{ | ||
strlcpy(_dhcpHostname, hostname, MAX_HOST_NAME_LENGTH + 1); | ||
} | ||
|
||
memcpy((void*)_dhcpMacAddr, (void*)mac, 6); | ||
_dhcp_state = STATE_DHCP_START; | ||
return request_DHCP_lease(); | ||
|
@@ -188,12 +207,8 @@ 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); | ||
|
||
printByte((char*)&(buffer[24]), _dhcpMacAddr[3]); | ||
printByte((char*)&(buffer[26]), _dhcpMacAddr[4]); | ||
printByte((char*)&(buffer[28]), _dhcpMacAddr[5]); | ||
buffer[17] = strlen(_dhcpHostname); // length of hostname | ||
strcpy((char*)&(buffer[18]), _dhcpHostname); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should also use safe str func |
||
|
||
//put data in W5100 transmit buffer | ||
_dhcpUdpSocket.write(buffer, 30); | ||
|
@@ -420,6 +435,11 @@ IPAddress DhcpClass::getDnsServerIp() | |
return IPAddress(_dhcpDnsServerIp); | ||
} | ||
|
||
const char* DhcpClass::getHostname() const | ||
{ | ||
return _dhcpHostname; | ||
} | ||
|
||
void DhcpClass::printByte(char * buf, uint8_t n ) | ||
{ | ||
char *str = &buf[1]; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,11 @@ IPAddress EthernetClass::_dnsServerAddress; | |
DhcpClass* EthernetClass::_dhcp = NULL; | ||
|
||
int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) | ||
{ | ||
begin(mac, NULL, timeout, responseTimeout); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bug: missing |
||
} | ||
|
||
int EthernetClass::begin(uint8_t *mac, const char* hostname, unsigned long timeout, unsigned long responseTimeout) | ||
{ | ||
static DhcpClass s_dhcp; | ||
_dhcp = &s_dhcp; | ||
|
@@ -39,7 +44,7 @@ int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long resp | |
SPI.endTransaction(); | ||
|
||
// Now try to get our config info from a DHCP server | ||
int ret = _dhcp->beginWithDHCP(mac, timeout, responseTimeout); | ||
int ret = _dhcp->beginWithDHCP(mac, hostname, timeout, responseTimeout); | ||
if (ret == 1) { | ||
// We've successfully found a DHCP server and got our configuration | ||
// info, so set things accordingly | ||
|
@@ -230,7 +235,10 @@ void EthernetClass::setRetransmissionCount(uint8_t num) | |
SPI.endTransaction(); | ||
} | ||
|
||
|
||
const char* EthernetClass::hostname() const | ||
{ | ||
return _dhcp ? _dhcp->getHostname() : ""; | ||
} | ||
|
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ | |
|
||
#include <Arduino.h> | ||
#include "Client.h" | ||
#include "Dhcp.h" | ||
#include "Server.h" | ||
#include "Udp.h" | ||
|
||
|
@@ -80,6 +81,7 @@ class EthernetClass { | |
// gain the rest of the configuration through DHCP. | ||
// Returns 0 if the DHCP configuration failed, and 1 if it succeeded | ||
static int begin(uint8_t *mac, unsigned long timeout = 60000, unsigned long responseTimeout = 4000); | ||
static int begin(uint8_t *mac, const char *hostname, unsigned long timeout = 60000, unsigned long responseTimeout = 4000); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. overloading a function with optional arguments is a bad design pattern and should not be done. a solution without overloading is in this case not so elegant:
must be called before calling
|
||
static int maintain(); | ||
static EthernetLinkStatus linkStatus(); | ||
static EthernetHardwareStatus hardwareStatus(); | ||
|
@@ -96,6 +98,7 @@ class EthernetClass { | |
static IPAddress subnetMask(); | ||
static IPAddress gatewayIP(); | ||
static IPAddress dnsServerIP() { return _dnsServerAddress; } | ||
const char* hostname() const; | ||
|
||
void setMACAddress(const uint8_t *mac_address); | ||
void setLocalIP(const IPAddress local_ip); | ||
|
@@ -296,6 +299,7 @@ class DhcpClass { | |
unsigned long _lastCheckLeaseMillis; | ||
uint8_t _dhcp_state; | ||
EthernetUDP _dhcpUdpSocket; | ||
char _dhcpHostname[MAX_HOST_NAME_LENGTH + 1]; | ||
|
||
int request_DHCP_lease(); | ||
void reset_DHCP_lease(); | ||
|
@@ -310,8 +314,10 @@ class DhcpClass { | |
IPAddress getGatewayIp(); | ||
IPAddress getDhcpServerIp(); | ||
IPAddress getDnsServerIp(); | ||
const char* getHostname() const; | ||
|
||
int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 4000); | ||
int beginWithDHCP(uint8_t *, const char *, unsigned long timeout = 60000, unsigned long responseTimeout = 4000); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this class is just used internally(?), so in my opinion could be changed even if it breaks backwards compatibility. |
||
int checkLease(); | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
most examples use:
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
my pr to use this mac in every example is pending.