25
25
26
26
IPAddress EthernetClass::_dnsServerAddress;
27
27
DhcpClass* EthernetClass::_dhcp = NULL ;
28
+ bool EthernetClass::_manualHostName = false ;
29
+ char EthernetClass::_hostName[HOST_NAME_MAX_LEN] = " " ;
28
30
29
31
int EthernetClass::begin (uint8_t *mac, unsigned long timeout, unsigned long responseTimeout)
30
32
{
@@ -38,8 +40,13 @@ int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long resp
38
40
W5100.setIPAddress (IPAddress (0 ,0 ,0 ,0 ).raw_address ());
39
41
SPI.endTransaction ();
40
42
43
+ // Generate a default host name based on the MAC address if not already set by user
44
+ if (!_manualHostName) {
45
+ generateDefaultHostName (mac);
46
+ }
47
+
41
48
// Now try to get our config info from a DHCP server
42
- int ret = _dhcp->beginWithDHCP (mac, timeout, responseTimeout);
49
+ int ret = _dhcp->beginWithDHCP (mac, _hostName, timeout, responseTimeout);
43
50
if (ret == 1 ) {
44
51
// We've successfully found a DHCP server and got our configuration
45
52
// info, so set things accordingly
@@ -224,6 +231,32 @@ void EthernetClass::setRetransmissionCount(uint8_t num)
224
231
SPI.endTransaction ();
225
232
}
226
233
234
+ void EthernetClass::generateDefaultHostName (uint8_t *mac) {
235
+ // Copy the default host name base
236
+ strcpy (_hostName, DEFAULT_HOST_NAME);
237
+
238
+ // Append the last 3 bytes of the MAC (HEX'd)
239
+ char macAddrStr[3 ];
240
+ sprintf (macAddrStr, " %02X" , mac[3 ]);
241
+ strcat (_hostName, macAddrStr);
242
+ sprintf (macAddrStr, " %02X" , mac[4 ]);
243
+ strcat (_hostName, macAddrStr);
244
+ sprintf (macAddrStr, " %02X" , mac[5 ]);
245
+ strcat (_hostName, macAddrStr);
246
+ }
247
+
248
+ void EthernetClass::setHostName (const char *dhcpHost) {
249
+ // Copy the host name and ensure it is null terminated
250
+ strncpy (_hostName, dhcpHost, HOST_NAME_MAX_LEN);
251
+ _hostName[HOST_NAME_MAX_LEN - 1 ] = ' \0 ' ;
252
+
253
+ // Indicate that a host name has been set manually
254
+ _manualHostName = true ;
255
+ }
256
+
257
+ char * EthernetClass::getHostName () {
258
+ return _hostName;
259
+ }
227
260
228
261
229
262
0 commit comments