Skip to content

Commit 5635ba5

Browse files
author
Luc
committed
Update to leverage on esp8266/Arduino#5607
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
1 parent ce7e723 commit 5635ba5

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

ESP32SSDP.cpp

+32-10
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ struct SSDPTimer {
111111

112112
SSDPClass::SSDPClass() :
113113
_server(0),
114-
_timer(new SSDPTimer),
114+
_timer(0),
115115
_port(80),
116116
_ttl(SSDP_MULTICAST_TTL),
117117
_respondToPort(0),
@@ -134,30 +134,40 @@ _notify_time(0)
134134
}
135135

136136
SSDPClass::~SSDPClass(){
137-
delete _timer;
137+
end();
138+
}
139+
140+
void SSDPClass::end(){
141+
if(!_server) {
142+
return;
143+
}
144+
#ifdef DEBUG_SSDP
145+
DEBUG_SSDP.printf_P(PSTR("SSDP end ... "));
146+
#endif
147+
// undo all initializations done in begin(), in reverse order
148+
_stopTimer();
149+
_server->stop();
150+
delete (_server);
151+
_server = 0;
138152
}
139153

140154

141155
bool SSDPClass::begin(){
142156
_pending = false;
143-
157+
end();
144158
uint32_t chipId = ((uint16_t) (ESP.getEfuseMac() >> 32));
145159
sprintf(_uuid, "38323636-4558-4dda-9188-cda0e6%02x%02x%02x",
146160
(uint16_t) ((chipId >> 16) & 0xff),
147161
(uint16_t) ((chipId >> 8) & 0xff),
148162
(uint16_t) chipId & 0xff );
149-
163+
assert(nullptr == _server);
164+
_server = new WiFiUDP;
150165
#ifdef DEBUG_SSDP
151166
DEBUG_SSDP.printf("SSDP UUID: %s\n", (char *)_uuid);
152167
#endif
153168

154-
if (_server) {
155-
delete (_server);
156-
_server = 0;
157-
}
158169

159170
_server = new WiFiUDP;
160-
161171
if (!(_server->beginMulticast(IPAddress(SSDP_MULTICAST_ADDR), SSDP_PORT))) {
162172
#ifdef DEBUG_SSDP
163173
DEBUG_SSDP.println("Error begin");
@@ -234,7 +244,7 @@ void SSDPClass::schema(WiFiClient client){
234244

235245
void SSDPClass::_update(){
236246
int nbBytes =0;
237-
char * packetBuffer = NULL;
247+
char * packetBuffer = nullptr;
238248

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

432442
void SSDPClass::_startTimer() {
443+
_stopTimer();
444+
_timer= new SSDPTimer();
433445
ETSTimer* tm = &(_timer->timer);
434446
const int interval = 1000;
435447
ets_timer_disarm(tm);
436448
ets_timer_setfn(tm, reinterpret_cast<ETSTimerFunc*>(&SSDPClass::_onTimerStatic), reinterpret_cast<void*>(this));
437449
ets_timer_arm(tm, interval, 1 /* repeat */);
438450
}
439451

452+
void SSDPClass::_stopTimer() {
453+
if(!_timer){
454+
return;
455+
}
456+
ETSTimer* tm = &(_timer->timer);
457+
ets_timer_disarm(tm);
458+
delete _timer;
459+
_timer = nullptr;
460+
}
461+
440462
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SSDP)
441463
SSDPClass SSDP;
442464
#endif

ESP32SSDP.h

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class SSDPClass{
6060
~SSDPClass();
6161

6262
bool begin();
63+
void end();
6364

6465
void schema(WiFiClient client);
6566

@@ -91,6 +92,7 @@ class SSDPClass{
9192
void _send(ssdp_method_t method);
9293
void _update();
9394
void _startTimer();
95+
void _stopTimer();
9496
static void _onTimerStatic(SSDPClass* self);
9597

9698
WiFiUDP *_server;

examples/SSDP/SSDP.ino

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ void setup() {
3636
SSDP.setModelURL("http://www.meethue.com");
3737
SSDP.setManufacturer("Royal Philips Electronics");
3838
SSDP.setManufacturerURL("http://www.philips.com");
39+
SSDP.setDeviceType("upnp:rootdevice"); //to appear as root device
3940
SSDP.begin();
4041

4142
Serial.printf("Ready!\n");

0 commit comments

Comments
 (0)