@@ -127,7 +127,7 @@ struct SSDPTimer {
127
127
128
128
SSDPClass::SSDPClass () :
129
129
_server(0 ),
130
- _timer(new SSDPTimer ),
130
+ _timer(0 ),
131
131
_port(80 ),
132
132
_ttl(SSDP_MULTICAST_TTL),
133
133
_respondToPort(0 ),
@@ -150,27 +150,26 @@ SSDPClass::SSDPClass() :
150
150
}
151
151
152
152
SSDPClass::~SSDPClass () {
153
- delete _timer ;
153
+ end () ;
154
154
}
155
155
156
156
bool SSDPClass::begin () {
157
+ end ();
158
+
157
159
_pending = false ;
158
160
if (strcmp (_uuid," " ) == 0 ) {
159
161
uint32_t chipId = ESP.getChipId ();
160
162
sprintf (_uuid, " 38323636-4558-4dda-9188-cda0e6%02x%02x%02x" ,
161
163
(uint16_t ) ((chipId >> 16 ) & 0xff ),
162
164
(uint16_t ) ((chipId >> 8 ) & 0xff ),
163
- (uint16_t ) chipId & 0xff );
165
+ (uint16_t ) chipId & 0xff );
164
166
}
165
167
166
168
#ifdef DEBUG_SSDP
167
169
DEBUG_SSDP.printf (" SSDP UUID: %s\n " , (char *)_uuid);
168
170
#endif
169
171
170
- if (_server) {
171
- _server->unref ();
172
- _server = 0 ;
173
- }
172
+ assert (NULL == _server);
174
173
175
174
_server = new UdpContext;
176
175
_server->ref ();
@@ -199,6 +198,34 @@ bool SSDPClass::begin() {
199
198
return true ;
200
199
}
201
200
201
+ void SSDPClass::end () {
202
+ if (!_server)
203
+ return ; // object is zeroed already, nothing to do
204
+
205
+ #ifdef DEBUG_SSDP
206
+ DEBUG_SSDP.printf_P (PSTR (" SSDP end ... " ));
207
+ #endif
208
+ // undo all initializations done in begin(), in reverse order
209
+ _stopTimer ();
210
+
211
+ _server->disconnect ();
212
+
213
+ IPAddress local = WiFi.localIP ();
214
+ IPAddress mcast (SSDP_MULTICAST_ADDR);
215
+
216
+ if (igmp_leavegroup (local, mcast) != ERR_OK ) {
217
+ #ifdef DEBUG_SSDP
218
+ DEBUG_SSDP.printf_P (PSTR (" SSDP failed to leave igmp group\n " ));
219
+ #endif
220
+ }
221
+
222
+ _server->unref ();
223
+ _server = 0 ;
224
+
225
+ #ifdef DEBUG_SSDP
226
+ DEBUG_SSDP.printf_P (PSTR (" ok\n " ));
227
+ #endif
228
+ }
202
229
void SSDPClass::_send (ssdp_method_t method) {
203
230
char buffer[1460 ];
204
231
IPAddress ip = WiFi.localIP ();
@@ -461,13 +488,25 @@ void SSDPClass::_onTimerStatic(SSDPClass* self) {
461
488
}
462
489
463
490
void SSDPClass::_startTimer () {
491
+ _stopTimer ();
492
+ _timer = new SSDPTimer ();
464
493
ETSTimer* tm = &(_timer->timer );
465
494
const int interval = 1000 ;
466
495
os_timer_disarm (tm );
467
496
os_timer_setfn (tm , reinterpret_cast <ETSTimerFunc*>(&SSDPClass::_onTimerStatic), reinterpret_cast <void *>(this ));
468
497
os_timer_arm (tm , interval, 1 /* repeat */ );
469
498
}
470
499
500
+ void SSDPClass::_stopTimer () {
501
+ if (!_timer)
502
+ return ;
503
+
504
+ ETSTimer* tm = &(_timer->timer );
505
+ os_timer_disarm (tm );
506
+ delete _timer;
507
+ _timer = NULL ;
508
+ }
509
+
471
510
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SSDP)
472
511
SSDPClass SSDP;
473
512
#endif
0 commit comments