Skip to content

Commit

Permalink
Test on ESP8266, fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 committed Mar 3, 2021
1 parent f9ab836 commit c70f189
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
41 changes: 27 additions & 14 deletions Sming/Components/mdns/src/Finder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "include/Network/Mdns/Finder.h"
#include "include/Network/Mdns/Response.h"
#include "Packet.h"
#include <IFS/FileSystem.h>
#include <Platform/Station.h>

#define MDNS_IP 224, 0, 0, 251
#define MDNS_TARGET_PORT 5353
Expand Down Expand Up @@ -73,33 +73,46 @@ bool Finder::search(const Query& query)
pkt.write(name.c_str() + pos, wordLength);
pos = sep + 1;
} while(pos > 0);

pkt.write8('\0'); // End of name.
pkt.write8(0); // End of name.

// 2 bytes for type
pkt.write16(uint16_t(query.type));

// 2 bytes for class
unsigned int qclass = 0;
uint16_t qclass = query.klass;
if(query.isUnicastResponse) {
qclass = 0x8000;
qclass |= 0x80000000;
}
qclass += query.klass;
pkt.write16(qclass);

initialise();
listen(0);
return sendTo(IpAddress(MDNS_IP), MDNS_TARGET_PORT, reinterpret_cast<const char*>(pkt.data), pkt.pos);
out.listen(0);
return out.sendTo(IpAddress(MDNS_IP), MDNS_TARGET_PORT, reinterpret_cast<const char*>(pkt.data), pkt.pos);
}

void Finder::initialise()
bool Finder::initialise()
{
if(!initialised) {
joinMulticastGroup(IpAddress(MDNS_IP));
listen(MDNS_SOURCE_PORT);
setMulticastTtl(MDNS_TTL);
initialised = true;
if(initialised) {
return true;
}

auto localIp = WifiStation.getIP();

if(!joinMulticastGroup(localIp, IpAddress(MDNS_IP))) {
debug_w("[mDNS] joinMulticastGroup() failed");
return false;
}

if(!listen(MDNS_SOURCE_PORT)) {
debug_e("[mDNS] listen failed");
return false;
}

setMulticast(localIp);
setMulticastTtl(MDNS_TTL);

initialised = true;
return true;
}

void Finder::UdpOut::onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort)
Expand Down
2 changes: 1 addition & 1 deletion Sming/Components/mdns/src/include/Network/Mdns/Finder.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Finder : protected UdpConnection
Finder& finder;
};

void initialise();
bool initialise();

AnswerDelegate answerCallback;
UdpOut out;
Expand Down
8 changes: 6 additions & 2 deletions samples/Basic_Mdns/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ void gotIP(IpAddress ip, IpAddress netmask, IpAddress gateway)

finder.onAnswer(printResponse);

bool ok = finder.search(F("_googlecast._tcp.local"));
debug_i("search(): %s", ok ? "OK" : "FAIL");
auto timer = new Timer;
timer->initializeMs<10000>(InterruptCallback([]() {
bool ok = finder.search(F("_googlecast._tcp.local"));
debug_i("search(): %s", ok ? "OK" : "FAIL");
}));
timer->start();
}

void connectFail(const String& ssid, MacAddress bssid, WifiDisconnectReason reason)
Expand Down

0 comments on commit c70f189

Please sign in to comment.