Skip to content

Commit 8d9f174

Browse files
author
Eric Wilkison
committed
Add human readable instance name property
1 parent 971bd9b commit 8d9f174

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

libraries/ESP8266mDNS/ESP8266mDNS.cpp

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ extern "C" {
5555

5656

5757

58-
//#define MDNS_DEBUG_ERR
59-
//#define MDNS_DEBUG_TX
60-
//#define MDNS_DEBUG_RX
58+
#define MDNS_DEBUG_ERR
59+
#define MDNS_DEBUG_TX
60+
#define MDNS_DEBUG_RX
6161

6262
#define MDNS_NAME_REF 0xC000
6363

@@ -88,19 +88,22 @@ static const int MDNS_PORT = 5353;
8888
MDNSResponder::MDNSResponder() : _conn(0) { _services = 0; }
8989
MDNSResponder::~MDNSResponder() {}
9090

91-
bool MDNSResponder::begin(const char* domain){
91+
bool MDNSResponder::begin(const char* hostname){
9292
// Open the MDNS socket if it isn't already open.
9393

94-
size_t n = strlen(domain);
95-
if (n > 255) { // Can only handle domains that are 255 chars in length.
94+
size_t n = strlen(hostname);
95+
if (n > 63) { // max size for a single label.
9696
return false;
9797
}
9898

99-
// Copy in domain characters as lowercase
99+
// Copy in hostname characters as lowercase
100100
for (size_t i = 0; i < n; ++i)
101-
_hostName[i] = tolower(domain[i]);
101+
_hostName[i] = tolower(hostname[i]);
102102
_hostName[n] = '\0';
103103

104+
// Copy hostname to default instance name
105+
os_strcpy(_instanceName,hostname);
106+
104107
// Open the MDNS socket if it isn't already open.
105108
if (!_conn) {
106109
uint32_t ourIp = _getOurIp();
@@ -139,10 +142,10 @@ void MDNSResponder::update() {
139142
}
140143

141144

142-
143-
144-
145-
145+
void MDNSResponder::setInstanceName(char * name){
146+
if (os_strlen(name) > 63) return;
147+
else os_strcpy(_instanceName,name);
148+
}
146149

147150

148151
bool MDNSResponder::addServiceTxt(char *name, char *proto, char *key, char *value){
@@ -317,9 +320,11 @@ void MDNSResponder::_parsePacket(){
317320
hostNameLen = 0;
318321
}
319322

320-
if(hostNameLen > 0 && strcmp(_hostName, hostName) != 0){
323+
if(hostNameLen > 0 && strcmp(_hostName, hostName) != 0 && strcmp(_instanceName, hostName) != 0 ){
321324
#ifdef MDNS_DEBUG_ERR
322325
Serial.printf("ERR_NO_HOST: %s\n", hostName);
326+
Serial.printf("hostname: %s\n", hostName);
327+
Serial.printf("instance: %s\n", _instanceName);
323328
#endif
324329
_conn->flush();
325330
return;
@@ -495,6 +500,9 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
495500
Serial.printf("TX: mask:%01X, service:%s, proto:%s, port:%u\n", replyMask, service, proto, port);
496501
#endif
497502

503+
char * instanceName = _instanceName;
504+
size_t instanceNameLen = os_strlen(instanceName);
505+
498506
char * hostName = _hostName;
499507
size_t hostNameLen = os_strlen(hostName);
500508

@@ -549,7 +557,7 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
549557
_conn->append(reinterpret_cast<const char*>(&terminator), 1); // terminator
550558

551559
//Send the type, class, ttl and rdata length
552-
uint8_t ptrDataLen = hostNameLen + serviceNameLen + protoNameLen + localNameLen + 5; // 5 is four label sizes and the terminator
560+
uint8_t ptrDataLen = instanceNameLen + serviceNameLen + protoNameLen + localNameLen + 5; // 5 is four label sizes and the terminator
553561
uint8_t ptrAttrs[10] = {
554562
0x00, 0x0c, //PTR record query
555563
0x00, 0x01, //Class IN
@@ -558,9 +566,9 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
558566
};
559567
_conn->append(reinterpret_cast<const char*>(ptrAttrs), 10);
560568

561-
//Send the RData (ie. "esp8266._http._tcp.local")
562-
_conn->append(reinterpret_cast<const char*>(&hostNameLen), 1); // lenght of "esp8266"
563-
_conn->append(reinterpret_cast<const char*>(hostName), hostNameLen); // "esp8266"
569+
//Send the RData (ie. "My IOT device._http._tcp.local")
570+
_conn->append(reinterpret_cast<const char*>(&instanceNameLen), 1); // lenght of "My IOT device"
571+
_conn->append(reinterpret_cast<const char*>(instanceName), instanceNameLen);// "My IOT device"
564572
_conn->append(reinterpret_cast<const char*>(&serviceNameLen), 1); // lenght of "_http"
565573
_conn->append(reinterpret_cast<const char*>(serviceName), serviceNameLen); // "_http"
566574
_conn->append(reinterpret_cast<const char*>(&protoNameLen), 1); // lenght of "_tcp"
@@ -572,9 +580,9 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
572580

573581
//TXT Responce
574582
if(replyMask & 0x4){
575-
//Send the name field (ie. "esp8266._http._tcp.local")
576-
_conn->append(reinterpret_cast<const char*>(&hostNameLen), 1); // lenght of "esp8266"
577-
_conn->append(reinterpret_cast<const char*>(hostName), hostNameLen); // "esp8266"
583+
//Send the name field (ie. "My IOT device._http._tcp.local")
584+
_conn->append(reinterpret_cast<const char*>(&instanceNameLen), 1); // lenght of "My IOT device"
585+
_conn->append(reinterpret_cast<const char*>(instanceName), instanceNameLen);// "My IOT device"
578586
_conn->append(reinterpret_cast<const char*>(&serviceNameLen), 1); // lenght of "_http"
579587
_conn->append(reinterpret_cast<const char*>(serviceName), serviceNameLen); // "_http"
580588
_conn->append(reinterpret_cast<const char*>(&protoNameLen), 1); // lenght of "_tcp"
@@ -608,9 +616,9 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
608616

609617
//SRV Responce
610618
if(replyMask & 0x2){
611-
//Send the name field (ie. "esp8266._http._tcp.local")
612-
_conn->append(reinterpret_cast<const char*>(&hostNameLen), 1); // lenght of "esp8266"
613-
_conn->append(reinterpret_cast<const char*>(hostName), hostNameLen); // "esp8266"
619+
//Send the name field (ie. "My IOT device._http._tcp.local")
620+
_conn->append(reinterpret_cast<const char*>(&instanceNameLen), 1); // lenght of "My IOT device"
621+
_conn->append(reinterpret_cast<const char*>(instanceName), instanceNameLen);// "My IOT device"
614622
_conn->append(reinterpret_cast<const char*>(&serviceNameLen), 1); // lenght of "_http"
615623
_conn->append(reinterpret_cast<const char*>(serviceName), serviceNameLen); // "_http"
616624
_conn->append(reinterpret_cast<const char*>(&protoNameLen), 1); // lenght of "_tcp"

libraries/ESP8266mDNS/ESP8266mDNS.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,21 @@ class MDNSResponder {
9393
addServiceTxt(name.c_str(), proto.c_str(), key.c_str(), value.c_str());
9494
}
9595

96-
void enableArduino(uint16_t port, bool auth=false);
96+
void enableArduino(uint16_t port, bool auth=false);
97+
98+
void setInstanceName(char * name);
99+
void setInstanceName(const char * name){
100+
setInstanceName((char*) name);
101+
}
102+
void setInstanceName(String name){
103+
setInstanceName(name.c_str());
104+
}
97105

98106
private:
99107
struct MDNSService * _services;
100108
UdpContext* _conn;
101-
char _hostName[128];
109+
char _hostName[63];
110+
char _instanceName[63];
102111

103112
uint32_t _getOurIp();
104113
uint16_t _getServicePort(char *service, char *proto);

0 commit comments

Comments
 (0)