From e671169f2d49c98e967183b8d0ccf05f282d93b8 Mon Sep 17 00:00:00 2001 From: Luc Date: Sun, 30 Jun 2019 15:56:34 +0200 Subject: [PATCH] update with latest version of SSDP --- libraries/ESP32SSDP/ESP32SSDP.cpp | 61 +++++++++++++++++----- libraries/ESP32SSDP/ESP32SSDP.h | 4 +- libraries/ESP32SSDP/examples/SSDP/SSDP.ino | 1 + 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/libraries/ESP32SSDP/ESP32SSDP.cpp b/libraries/ESP32SSDP/ESP32SSDP.cpp index 866cce57..e862b146 100644 --- a/libraries/ESP32SSDP/ESP32SSDP.cpp +++ b/libraries/ESP32SSDP/ESP32SSDP.cpp @@ -111,7 +111,7 @@ struct SSDPTimer { SSDPClass::SSDPClass() : _server(0), -_timer(new SSDPTimer), +_timer(0), _port(80), _ttl(SSDP_MULTICAST_TTL), _respondToPort(0), @@ -134,30 +134,53 @@ _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; +} + +IPAddress SSDPClass::localIP(){ + tcpip_adapter_ip_info_t ip; + if (WiFi.getMode() == WIFI_STA) { + if (tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip)) { + return IPAddress(); + } + } else if (WiFi.getMode() == WIFI_OFF) { + if (tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip)) { + return IPAddress(); + } + } + return IPAddress(ip.ip.addr); +} 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"); @@ -172,7 +195,7 @@ bool SSDPClass::begin(){ void SSDPClass::_send(ssdp_method_t method){ char buffer[1460]; - IPAddress ip = WiFi.localIP(); + IPAddress ip = localIP(); char valueBuffer[strlen_P(_ssdp_notify_template)+1]; strcpy_P(valueBuffer, (method == NONE)?_ssdp_response_template:_ssdp_notify_template); @@ -187,7 +210,7 @@ void SSDPClass::_send(ssdp_method_t method){ _deviceType, ip[0], ip[1], ip[2], ip[3], _port, _schemaURL ); - + if(len < 0) return; IPAddress remoteAddr; uint16_t remotePort; if(method == NONE) { @@ -214,7 +237,7 @@ void SSDPClass::_send(ssdp_method_t method){ } void SSDPClass::schema(WiFiClient client){ - IPAddress ip = WiFi.localIP(); + IPAddress ip = localIP(); char buffer[strlen_P(_ssdp_schema_template)+1]; strcpy_P(buffer, _ssdp_schema_template); client.printf(buffer, @@ -234,7 +257,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; @@ -430,6 +453,8 @@ void SSDPClass::_onTimerStatic(SSDPClass* self) { } void SSDPClass::_startTimer() { + _stopTimer(); + _timer= new SSDPTimer(); ETSTimer* tm = &(_timer->timer); const int interval = 1000; ets_timer_disarm(tm); @@ -437,6 +462,16 @@ void SSDPClass::_startTimer() { 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 diff --git a/libraries/ESP32SSDP/ESP32SSDP.h b/libraries/ESP32SSDP/ESP32SSDP.h index e9017baa..796c4fe1 100644 --- a/libraries/ESP32SSDP/ESP32SSDP.h +++ b/libraries/ESP32SSDP/ESP32SSDP.h @@ -60,6 +60,7 @@ class SSDPClass{ ~SSDPClass(); bool begin(); + void end(); void schema(WiFiClient client); @@ -91,8 +92,9 @@ class SSDPClass{ void _send(ssdp_method_t method); void _update(); void _startTimer(); + void _stopTimer(); static void _onTimerStatic(SSDPClass* self); - + IPAddress localIP(); WiFiUDP *_server; SSDPTimer* _timer; uint16_t _port; diff --git a/libraries/ESP32SSDP/examples/SSDP/SSDP.ino b/libraries/ESP32SSDP/examples/SSDP/SSDP.ino index 8014a3a5..46f02438 100644 --- a/libraries/ESP32SSDP/examples/SSDP/SSDP.ino +++ b/libraries/ESP32SSDP/examples/SSDP/SSDP.ino @@ -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");