mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-20 10:24:18 +03:00
Add human readable instance name property
This commit is contained in:
@ -55,9 +55,9 @@ extern "C" {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
//#define MDNS_DEBUG_ERR
|
#define MDNS_DEBUG_ERR
|
||||||
//#define MDNS_DEBUG_TX
|
#define MDNS_DEBUG_TX
|
||||||
//#define MDNS_DEBUG_RX
|
#define MDNS_DEBUG_RX
|
||||||
|
|
||||||
#define MDNS_NAME_REF 0xC000
|
#define MDNS_NAME_REF 0xC000
|
||||||
|
|
||||||
@ -88,19 +88,22 @@ static const int MDNS_PORT = 5353;
|
|||||||
MDNSResponder::MDNSResponder() : _conn(0) { _services = 0; }
|
MDNSResponder::MDNSResponder() : _conn(0) { _services = 0; }
|
||||||
MDNSResponder::~MDNSResponder() {}
|
MDNSResponder::~MDNSResponder() {}
|
||||||
|
|
||||||
bool MDNSResponder::begin(const char* domain){
|
bool MDNSResponder::begin(const char* hostname){
|
||||||
// Open the MDNS socket if it isn't already open.
|
// Open the MDNS socket if it isn't already open.
|
||||||
|
|
||||||
size_t n = strlen(domain);
|
size_t n = strlen(hostname);
|
||||||
if (n > 255) { // Can only handle domains that are 255 chars in length.
|
if (n > 63) { // max size for a single label.
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy in domain characters as lowercase
|
// Copy in hostname characters as lowercase
|
||||||
for (size_t i = 0; i < n; ++i)
|
for (size_t i = 0; i < n; ++i)
|
||||||
_hostName[i] = tolower(domain[i]);
|
_hostName[i] = tolower(hostname[i]);
|
||||||
_hostName[n] = '\0';
|
_hostName[n] = '\0';
|
||||||
|
|
||||||
|
// Copy hostname to default instance name
|
||||||
|
os_strcpy(_instanceName,hostname);
|
||||||
|
|
||||||
// Open the MDNS socket if it isn't already open.
|
// Open the MDNS socket if it isn't already open.
|
||||||
if (!_conn) {
|
if (!_conn) {
|
||||||
uint32_t ourIp = _getOurIp();
|
uint32_t ourIp = _getOurIp();
|
||||||
@ -139,10 +142,10 @@ void MDNSResponder::update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void MDNSResponder::setInstanceName(char * name){
|
||||||
|
if (os_strlen(name) > 63) return;
|
||||||
|
else os_strcpy(_instanceName,name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool MDNSResponder::addServiceTxt(char *name, char *proto, char *key, char *value){
|
bool MDNSResponder::addServiceTxt(char *name, char *proto, char *key, char *value){
|
||||||
@ -317,9 +320,11 @@ void MDNSResponder::_parsePacket(){
|
|||||||
hostNameLen = 0;
|
hostNameLen = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(hostNameLen > 0 && strcmp(_hostName, hostName) != 0){
|
if(hostNameLen > 0 && strcmp(_hostName, hostName) != 0 && strcmp(_instanceName, hostName) != 0 ){
|
||||||
#ifdef MDNS_DEBUG_ERR
|
#ifdef MDNS_DEBUG_ERR
|
||||||
Serial.printf("ERR_NO_HOST: %s\n", hostName);
|
Serial.printf("ERR_NO_HOST: %s\n", hostName);
|
||||||
|
Serial.printf("hostname: %s\n", hostName);
|
||||||
|
Serial.printf("instance: %s\n", _instanceName);
|
||||||
#endif
|
#endif
|
||||||
_conn->flush();
|
_conn->flush();
|
||||||
return;
|
return;
|
||||||
@ -495,6 +500,9 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
|
|||||||
Serial.printf("TX: mask:%01X, service:%s, proto:%s, port:%u\n", replyMask, service, proto, port);
|
Serial.printf("TX: mask:%01X, service:%s, proto:%s, port:%u\n", replyMask, service, proto, port);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
char * instanceName = _instanceName;
|
||||||
|
size_t instanceNameLen = os_strlen(instanceName);
|
||||||
|
|
||||||
char * hostName = _hostName;
|
char * hostName = _hostName;
|
||||||
size_t hostNameLen = os_strlen(hostName);
|
size_t hostNameLen = os_strlen(hostName);
|
||||||
|
|
||||||
@ -549,7 +557,7 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
|
|||||||
_conn->append(reinterpret_cast<const char*>(&terminator), 1); // terminator
|
_conn->append(reinterpret_cast<const char*>(&terminator), 1); // terminator
|
||||||
|
|
||||||
//Send the type, class, ttl and rdata length
|
//Send the type, class, ttl and rdata length
|
||||||
uint8_t ptrDataLen = hostNameLen + serviceNameLen + protoNameLen + localNameLen + 5; // 5 is four label sizes and the terminator
|
uint8_t ptrDataLen = instanceNameLen + serviceNameLen + protoNameLen + localNameLen + 5; // 5 is four label sizes and the terminator
|
||||||
uint8_t ptrAttrs[10] = {
|
uint8_t ptrAttrs[10] = {
|
||||||
0x00, 0x0c, //PTR record query
|
0x00, 0x0c, //PTR record query
|
||||||
0x00, 0x01, //Class IN
|
0x00, 0x01, //Class IN
|
||||||
@ -558,9 +566,9 @@ void MDNSResponder::_reply(uint8_t replyMask, char * service, char *proto, uint1
|
|||||||
};
|
};
|
||||||
_conn->append(reinterpret_cast<const char*>(ptrAttrs), 10);
|
_conn->append(reinterpret_cast<const char*>(ptrAttrs), 10);
|
||||||
|
|
||||||
//Send the RData (ie. "esp8266._http._tcp.local")
|
//Send the RData (ie. "My IOT device._http._tcp.local")
|
||||||
_conn->append(reinterpret_cast<const char*>(&hostNameLen), 1); // lenght of "esp8266"
|
_conn->append(reinterpret_cast<const char*>(&instanceNameLen), 1); // lenght of "My IOT device"
|
||||||
_conn->append(reinterpret_cast<const char*>(hostName), hostNameLen); // "esp8266"
|
_conn->append(reinterpret_cast<const char*>(instanceName), instanceNameLen);// "My IOT device"
|
||||||
_conn->append(reinterpret_cast<const char*>(&serviceNameLen), 1); // lenght of "_http"
|
_conn->append(reinterpret_cast<const char*>(&serviceNameLen), 1); // lenght of "_http"
|
||||||
_conn->append(reinterpret_cast<const char*>(serviceName), serviceNameLen); // "_http"
|
_conn->append(reinterpret_cast<const char*>(serviceName), serviceNameLen); // "_http"
|
||||||
_conn->append(reinterpret_cast<const char*>(&protoNameLen), 1); // lenght of "_tcp"
|
_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
|
|||||||
|
|
||||||
//TXT Responce
|
//TXT Responce
|
||||||
if(replyMask & 0x4){
|
if(replyMask & 0x4){
|
||||||
//Send the name field (ie. "esp8266._http._tcp.local")
|
//Send the name field (ie. "My IOT device._http._tcp.local")
|
||||||
_conn->append(reinterpret_cast<const char*>(&hostNameLen), 1); // lenght of "esp8266"
|
_conn->append(reinterpret_cast<const char*>(&instanceNameLen), 1); // lenght of "My IOT device"
|
||||||
_conn->append(reinterpret_cast<const char*>(hostName), hostNameLen); // "esp8266"
|
_conn->append(reinterpret_cast<const char*>(instanceName), instanceNameLen);// "My IOT device"
|
||||||
_conn->append(reinterpret_cast<const char*>(&serviceNameLen), 1); // lenght of "_http"
|
_conn->append(reinterpret_cast<const char*>(&serviceNameLen), 1); // lenght of "_http"
|
||||||
_conn->append(reinterpret_cast<const char*>(serviceName), serviceNameLen); // "_http"
|
_conn->append(reinterpret_cast<const char*>(serviceName), serviceNameLen); // "_http"
|
||||||
_conn->append(reinterpret_cast<const char*>(&protoNameLen), 1); // lenght of "_tcp"
|
_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
|
|||||||
|
|
||||||
//SRV Responce
|
//SRV Responce
|
||||||
if(replyMask & 0x2){
|
if(replyMask & 0x2){
|
||||||
//Send the name field (ie. "esp8266._http._tcp.local")
|
//Send the name field (ie. "My IOT device._http._tcp.local")
|
||||||
_conn->append(reinterpret_cast<const char*>(&hostNameLen), 1); // lenght of "esp8266"
|
_conn->append(reinterpret_cast<const char*>(&instanceNameLen), 1); // lenght of "My IOT device"
|
||||||
_conn->append(reinterpret_cast<const char*>(hostName), hostNameLen); // "esp8266"
|
_conn->append(reinterpret_cast<const char*>(instanceName), instanceNameLen);// "My IOT device"
|
||||||
_conn->append(reinterpret_cast<const char*>(&serviceNameLen), 1); // lenght of "_http"
|
_conn->append(reinterpret_cast<const char*>(&serviceNameLen), 1); // lenght of "_http"
|
||||||
_conn->append(reinterpret_cast<const char*>(serviceName), serviceNameLen); // "_http"
|
_conn->append(reinterpret_cast<const char*>(serviceName), serviceNameLen); // "_http"
|
||||||
_conn->append(reinterpret_cast<const char*>(&protoNameLen), 1); // lenght of "_tcp"
|
_conn->append(reinterpret_cast<const char*>(&protoNameLen), 1); // lenght of "_tcp"
|
||||||
|
@ -93,12 +93,21 @@ public:
|
|||||||
addServiceTxt(name.c_str(), proto.c_str(), key.c_str(), value.c_str());
|
addServiceTxt(name.c_str(), proto.c_str(), key.c_str(), value.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void enableArduino(uint16_t port, bool auth=false);
|
void enableArduino(uint16_t port, bool auth=false);
|
||||||
|
|
||||||
|
void setInstanceName(char * name);
|
||||||
|
void setInstanceName(const char * name){
|
||||||
|
setInstanceName((char*) name);
|
||||||
|
}
|
||||||
|
void setInstanceName(String name){
|
||||||
|
setInstanceName(name.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct MDNSService * _services;
|
struct MDNSService * _services;
|
||||||
UdpContext* _conn;
|
UdpContext* _conn;
|
||||||
char _hostName[128];
|
char _hostName[63];
|
||||||
|
char _instanceName[63];
|
||||||
|
|
||||||
uint32_t _getOurIp();
|
uint32_t _getOurIp();
|
||||||
uint16_t _getServicePort(char *service, char *proto);
|
uint16_t _getServicePort(char *service, char *proto);
|
||||||
|
Reference in New Issue
Block a user