Skip to content

Commit

Permalink
Add support for searching for other MDNS endpoints
Browse files Browse the repository at this point in the history
This allows for beginning a SEARCH and handling SEARCH responses.
  • Loading branch information
opticron authored and Kinsey Moore committed Apr 29, 2018
1 parent 41c1d85 commit 3ee1200
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
25 changes: 24 additions & 1 deletion ESP8266HueEmulator/SSDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ bool SSDPClass::begin(){
return true;
}

void SSDPClass::beginSearch() {
_send(SEARCH);
}

void SSDPClass::_send(ssdp_method_t method){
char buffer[1460];
uint32_t ip = WiFi.localIP();
Expand All @@ -218,6 +222,11 @@ void SSDPClass::_send(ssdp_method_t method){
);
}

if (method == SEARCH) {
_respondToAddr = SSDP_MULTICAST_ADDR;
_respondToPort = SSDP_PORT;
}

_server->append(buffer, len);

ip_addr_t remoteAddr;
Expand Down Expand Up @@ -341,6 +350,7 @@ void SSDPClass::_parseIncoming() {
typedef enum {START, MAN, ST, MX, UNKNOWN} headers;
headers header = START;
bool notify = false;
bool response = false;

String token;
// get message type
Expand All @@ -353,6 +363,8 @@ void SSDPClass::_parseIncoming() {
if (token == "M-SEARCH") {
} else if (token == "NOTIFY") {
notify = true;
} else if (token == "HTTP/1.1") {
response = true;
} else {
_bailRead();
return;
Expand All @@ -364,7 +376,7 @@ void SSDPClass::_parseIncoming() {
_bailRead();
return;
}
if (token != "*") {
if (token != "*" && token != "200") {
_bailRead();
return;
}
Expand All @@ -387,6 +399,17 @@ void SSDPClass::_parseIncoming() {
return;
}

if (response) {
if (_responseCallback) {
_inCallback = true;
_responseCallback(this, _respondToAddr, _respondToPort);
_inCallback = false;
}
// even if the callback was called, exhaust the rest of the data if any exists
_bailRead();
return;
}

while(_server->getSize() > 0){
res = _getNextToken(&token, header == START, header == START);
if (res < 0 && header == START) {
Expand Down
3 changes: 3 additions & 0 deletions ESP8266HueEmulator/SSDP.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class SSDPClass{
// this callback gets called when a notify comes in
typedef std::function<void(SSDPClass *ssdp, IPAddress addr, uint16_t port)> IncomingFunction;
void setNotifyCallback(IncomingFunction callback) { _notifyCallback = callback; }
void setResponseCallback(IncomingFunction callback) { _responseCallback = callback; }
int readIncomingLine(String *key, String *value);

void schema(WiFiClient client);
Expand Down Expand Up @@ -98,6 +99,7 @@ class SSDPClass{
void setManufacturerURL(const char *url);
void setHTTPPort(uint16_t port);
void setTTL(uint8_t ttl);
void beginSearch();

protected:
void _send(ssdp_method_t method);
Expand Down Expand Up @@ -135,6 +137,7 @@ class SSDPClass{
char _modelNumber[SSDP_MODEL_VERSION_SIZE];
MsgFormatFunction _messageFormatCallback = nullptr;
IncomingFunction _notifyCallback = nullptr;
IncomingFunction _responseCallback = nullptr;
};

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SSDP)
Expand Down

0 comments on commit 3ee1200

Please sign in to comment.