Skip to content

Make ESPmDNS compatible to ESP8266mDNS #1085

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

Merged
merged 1 commit into from
Mar 4, 2018
Merged
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
43 changes: 39 additions & 4 deletions libraries/ESPmDNS/src/ESPmDNS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ bool MDNSResponder::begin(const char* hostName){
}
WiFi.onEvent(_on_sys_event);
_hostname = hostName;
_hostname.toLowerCase();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be merged with the line above as:
_hostname = hostName.toLowerCase();

Copy link
Contributor

@baggior baggior Feb 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think
String.toLowerCase() make side effect on the object itself and doesn't returns anything
https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/tolowercase/

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I was looking at it and it looked odd. Normally a method like this would return the modification rather than do the modification on the object itself. It appears that https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/WString.h#L255 is just doing a direct modification on the object in place.

if(mdns_hostname_set(hostName)) {
log_e("Failed setting MDNS hostname");
return false;
Expand Down Expand Up @@ -122,13 +123,39 @@ void MDNSResponder::disableWorkstation(){
}

void MDNSResponder::addService(char *name, char *proto, uint16_t port){
if(mdns_service_add(NULL, name, proto, port, NULL, 0)) {
char _name[strlen(name)+2];
char _proto[strlen(proto)+2];
if (name[0] == '_') {
sprintf(_name, "%s", name);
} else {
sprintf(_name, "_%s", name);
}
if (proto[0] == '_') {
sprintf(_proto, "%s", proto);
} else {
sprintf(_proto, "_%s", proto);
}

if(mdns_service_add(NULL, _name, _proto, port, NULL, 0)) {
log_e("Failed adding service %s.%s.\n", name, proto);
}
}

bool MDNSResponder::addServiceTxt(char *name, char *proto, char *key, char *value){
if(mdns_service_txt_item_set(name, proto, key, value)) {
char _name[strlen(name)+2];
char _proto[strlen(proto)+2];
if (name[0] == '_') {
sprintf(_name, "%s", name);
} else {
sprintf(_name, "_%s", name);
}
if (proto[0] == '_') {
sprintf(_proto, "%s", proto);
} else {
sprintf(_proto, "_%s", proto);
}

if(mdns_service_txt_item_set(_name, _proto, key, value)) {
log_e("Failed setting service TXT");
return false;
}
Expand Down Expand Up @@ -165,8 +192,16 @@ int MDNSResponder::queryService(char *service, char *proto) {

char srv[strlen(service)+2];
char prt[strlen(proto)+2];
sprintf(srv, "_%s", service);
sprintf(prt, "_%s", proto);
if (service[0] == '_') {
sprintf(srv, "%s", service);
} else {
sprintf(srv, "_%s", service);
}
if (proto[0] == '_') {
sprintf(prt, "%s", proto);
} else {
sprintf(prt, "_%s", proto);
}

esp_err_t err = mdns_query_ptr(srv, prt, 3000, 20, &results);
if(err){
Expand Down