Skip to content

Commit

Permalink
Update to leverage on esp8266/Arduino#5607
Browse files Browse the repository at this point in the history
Add proper end() function
Add `SSDP.setDeviceType("upnp:rootdevice"); ` in sample as it may not be obvious that it is an option to appear in Windows Network Panel
  • Loading branch information
Luc committed Jan 14, 2019
1 parent ce7e723 commit 5635ba5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
42 changes: 32 additions & 10 deletions ESP32SSDP.cpp
Expand Up @@ -111,7 +111,7 @@ struct SSDPTimer {

SSDPClass::SSDPClass() :
_server(0),
_timer(new SSDPTimer),
_timer(0),
_port(80),
_ttl(SSDP_MULTICAST_TTL),
_respondToPort(0),
Expand All @@ -134,30 +134,40 @@ _notify_time(0)
}

SSDPClass::~SSDPClass(){
delete _timer;
end();
}

void SSDPClass::end(){
if(!_server) {
return;
}
#ifdef DEBUG_SSDP
DEBUG_SSDP.printf_P(PSTR("SSDP end ... "));
#endif
// undo all initializations done in begin(), in reverse order
_stopTimer();
_server->stop();
delete (_server);
_server = 0;
}


bool SSDPClass::begin(){
_pending = false;

end();
uint32_t chipId = ((uint16_t) (ESP.getEfuseMac() >> 32));
sprintf(_uuid, "38323636-4558-4dda-9188-cda0e6%02x%02x%02x",
(uint16_t) ((chipId >> 16) & 0xff),
(uint16_t) ((chipId >> 8) & 0xff),
(uint16_t) chipId & 0xff );

assert(nullptr == _server);
_server = new WiFiUDP;
#ifdef DEBUG_SSDP
DEBUG_SSDP.printf("SSDP UUID: %s\n", (char *)_uuid);
#endif

if (_server) {
delete (_server);
_server = 0;
}

_server = new WiFiUDP;

if (!(_server->beginMulticast(IPAddress(SSDP_MULTICAST_ADDR), SSDP_PORT))) {
#ifdef DEBUG_SSDP
DEBUG_SSDP.println("Error begin");
Expand Down Expand Up @@ -234,7 +244,7 @@ void SSDPClass::schema(WiFiClient client){

void SSDPClass::_update(){
int nbBytes =0;
char * packetBuffer = NULL;
char * packetBuffer = nullptr;

if(!_pending && _server) {
ssdp_method_t method = NONE;
Expand Down Expand Up @@ -430,13 +440,25 @@ void SSDPClass::_onTimerStatic(SSDPClass* self) {
}

void SSDPClass::_startTimer() {
_stopTimer();
_timer= new SSDPTimer();
ETSTimer* tm = &(_timer->timer);
const int interval = 1000;
ets_timer_disarm(tm);
ets_timer_setfn(tm, reinterpret_cast<ETSTimerFunc*>(&SSDPClass::_onTimerStatic), reinterpret_cast<void*>(this));
ets_timer_arm(tm, interval, 1 /* repeat */);
}

void SSDPClass::_stopTimer() {
if(!_timer){
return;
}
ETSTimer* tm = &(_timer->timer);
ets_timer_disarm(tm);
delete _timer;
_timer = nullptr;
}

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SSDP)
SSDPClass SSDP;
#endif
2 changes: 2 additions & 0 deletions ESP32SSDP.h
Expand Up @@ -60,6 +60,7 @@ class SSDPClass{
~SSDPClass();

bool begin();
void end();

void schema(WiFiClient client);

Expand Down Expand Up @@ -91,6 +92,7 @@ class SSDPClass{
void _send(ssdp_method_t method);
void _update();
void _startTimer();
void _stopTimer();
static void _onTimerStatic(SSDPClass* self);

WiFiUDP *_server;
Expand Down
1 change: 1 addition & 0 deletions examples/SSDP/SSDP.ino
Expand Up @@ -36,6 +36,7 @@ void setup() {
SSDP.setModelURL("http://www.meethue.com");
SSDP.setManufacturer("Royal Philips Electronics");
SSDP.setManufacturerURL("http://www.philips.com");
SSDP.setDeviceType("upnp:rootdevice"); //to appear as root device
SSDP.begin();

Serial.printf("Ready!\n");
Expand Down

0 comments on commit 5635ba5

Please sign in to comment.