From 8d546462d206f3ce749ac11bf98c3d8c0c49b2de Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sun, 17 Nov 2019 13:27:20 -0800 Subject: [PATCH 1/3] [SSDP] add `schema(Print &) const` Supercedes #2806 Make SSDP::schema(WiFiClient&) use a by-ref (reduce stack use) Add a SSDP::schema(Print&) From @Palatis' original PR: useful when using AsyncWebServer. --- libraries/ESP8266SSDP/ESP8266SSDP.cpp | 18 +++++++++++++++++- libraries/ESP8266SSDP/ESP8266SSDP.h | 3 ++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/libraries/ESP8266SSDP/ESP8266SSDP.cpp b/libraries/ESP8266SSDP/ESP8266SSDP.cpp index 900c2059fe..f7434fd9de 100644 --- a/libraries/ESP8266SSDP/ESP8266SSDP.cpp +++ b/libraries/ESP8266SSDP/ESP8266SSDP.cpp @@ -276,7 +276,7 @@ void SSDPClass::_send(ssdp_method_t method) { _server->send(remoteAddr, remotePort); } -void SSDPClass::schema(WiFiClient client) { +void SSDPClass::schema(WiFiClient &client) const { IPAddress ip = WiFi.localIP(); char buffer[strlen_P(_ssdp_schema_template) + 1]; strcpy_P(buffer, _ssdp_schema_template); @@ -295,6 +295,22 @@ void SSDPClass::schema(WiFiClient client) { ); } +void SSDPClass::schema(Print &print) const { + uint32_t ip = WiFi.localIP(); + print.printf(_ssdp_schema_template, + IP2STR(&ip), _port, + _deviceType, + _friendlyName, + _presentationURL, + _serialNumber, + _modelName, + _modelNumber, + _modelURL, + _manufacturer, + _manufacturerURL, + _uuid + ); +} void SSDPClass::_update() { if (!_pending && _server->next()) { ssdp_method_t method = NONE; diff --git a/libraries/ESP8266SSDP/ESP8266SSDP.h b/libraries/ESP8266SSDP/ESP8266SSDP.h index 5fbbf59364..91b3bdebe4 100644 --- a/libraries/ESP8266SSDP/ESP8266SSDP.h +++ b/libraries/ESP8266SSDP/ESP8266SSDP.h @@ -62,7 +62,8 @@ class SSDPClass{ ~SSDPClass(); bool begin(); void end(); - void schema(WiFiClient client); + void schema(WiFiClient &client) const; + void schema(Print &print) const; void setDeviceType(const String& deviceType) { setDeviceType(deviceType.c_str()); } void setDeviceType(const char *deviceType); From 0bbc3c5fb5ca198b5141689c422b792ae6968f2f Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sun, 17 Nov 2019 14:51:31 -0800 Subject: [PATCH 2/3] Use ip.toString, only export Print& schema interface Because WiFiClient inherits a Print interface, replace the ::schema(WiFiClient&) with ::schema(Print&) which is source compatible with existing code and allows the functionality requested in the initial PR. Use ip.toString() in the templates instead of breaking up the octets of the address. --- libraries/ESP8266SSDP/ESP8266SSDP.cpp | 26 +++++--------------------- libraries/ESP8266SSDP/ESP8266SSDP.h | 1 - 2 files changed, 5 insertions(+), 22 deletions(-) diff --git a/libraries/ESP8266SSDP/ESP8266SSDP.cpp b/libraries/ESP8266SSDP/ESP8266SSDP.cpp index f7434fd9de..efa45ef8d1 100644 --- a/libraries/ESP8266SSDP/ESP8266SSDP.cpp +++ b/libraries/ESP8266SSDP/ESP8266SSDP.cpp @@ -73,7 +73,7 @@ static const char _ssdp_packet_template[] PROGMEM = "SERVER: Arduino/1.0 UPNP/1.1 %s/%s\r\n" // _modelName, _modelNumber "USN: %s\r\n" // _uuid "%s: %s\r\n" // "NT" or "ST", _deviceType - "LOCATION: http://%u.%u.%u.%u:%u/%s\r\n" // WiFi.localIP(), _port, _schemaURL + "LOCATION: http://%s:%u/%s\r\n" // WiFi.localIP(), _port, _schemaURL "\r\n"; static const char _ssdp_schema_template[] PROGMEM = @@ -88,7 +88,7 @@ static const char _ssdp_schema_template[] PROGMEM = "1" "0" "" - "http://%u.%u.%u.%u:%u/" // WiFi.localIP(), _port + "http://%s:%u/" // WiFi.localIP(), _port "" "%s" "%s" @@ -247,7 +247,7 @@ void SSDPClass::_send(ssdp_method_t method) { _uuid, (method == NONE) ? "ST" : "NT", (_st_is_uuid) ? _uuid : _deviceType, - ip[0], ip[1], ip[2], ip[3], _port, _schemaURL + ip.toString(), _port, _schemaURL ); _server->append(buffer, len); @@ -276,12 +276,12 @@ void SSDPClass::_send(ssdp_method_t method) { _server->send(remoteAddr, remotePort); } -void SSDPClass::schema(WiFiClient &client) const { +void SSDPClass::schema(Print &client) const { IPAddress ip = WiFi.localIP(); char buffer[strlen_P(_ssdp_schema_template) + 1]; strcpy_P(buffer, _ssdp_schema_template); client.printf(buffer, - ip[0], ip[1], ip[2], ip[3], _port, + ip.toString(), _port, _deviceType, _friendlyName, _presentationURL, @@ -295,22 +295,6 @@ void SSDPClass::schema(WiFiClient &client) const { ); } -void SSDPClass::schema(Print &print) const { - uint32_t ip = WiFi.localIP(); - print.printf(_ssdp_schema_template, - IP2STR(&ip), _port, - _deviceType, - _friendlyName, - _presentationURL, - _serialNumber, - _modelName, - _modelNumber, - _modelURL, - _manufacturer, - _manufacturerURL, - _uuid - ); -} void SSDPClass::_update() { if (!_pending && _server->next()) { ssdp_method_t method = NONE; diff --git a/libraries/ESP8266SSDP/ESP8266SSDP.h b/libraries/ESP8266SSDP/ESP8266SSDP.h index 91b3bdebe4..cade42c8e4 100644 --- a/libraries/ESP8266SSDP/ESP8266SSDP.h +++ b/libraries/ESP8266SSDP/ESP8266SSDP.h @@ -62,7 +62,6 @@ class SSDPClass{ ~SSDPClass(); bool begin(); void end(); - void schema(WiFiClient &client) const; void schema(Print &print) const; void setDeviceType(const String& deviceType) { setDeviceType(deviceType.c_str()); } void setDeviceType(const char *deviceType); From 99c8204cffa3b63c3fc89e8f697381e6dcbf95b6 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sun, 17 Nov 2019 19:41:11 -0800 Subject: [PATCH 3/3] Fix compile errors and backwards compatibility --- libraries/ESP8266SSDP/ESP8266SSDP.cpp | 4 ++-- libraries/ESP8266SSDP/ESP8266SSDP.h | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/libraries/ESP8266SSDP/ESP8266SSDP.cpp b/libraries/ESP8266SSDP/ESP8266SSDP.cpp index efa45ef8d1..aed49929d5 100644 --- a/libraries/ESP8266SSDP/ESP8266SSDP.cpp +++ b/libraries/ESP8266SSDP/ESP8266SSDP.cpp @@ -247,7 +247,7 @@ void SSDPClass::_send(ssdp_method_t method) { _uuid, (method == NONE) ? "ST" : "NT", (_st_is_uuid) ? _uuid : _deviceType, - ip.toString(), _port, _schemaURL + ip.toString().c_str(), _port, _schemaURL ); _server->append(buffer, len); @@ -281,7 +281,7 @@ void SSDPClass::schema(Print &client) const { char buffer[strlen_P(_ssdp_schema_template) + 1]; strcpy_P(buffer, _ssdp_schema_template); client.printf(buffer, - ip.toString(), _port, + ip.toString().c_str(), _port, _deviceType, _friendlyName, _presentationURL, diff --git a/libraries/ESP8266SSDP/ESP8266SSDP.h b/libraries/ESP8266SSDP/ESP8266SSDP.h index cade42c8e4..ec7c86e2b5 100644 --- a/libraries/ESP8266SSDP/ESP8266SSDP.h +++ b/libraries/ESP8266SSDP/ESP8266SSDP.h @@ -62,6 +62,7 @@ class SSDPClass{ ~SSDPClass(); bool begin(); void end(); + void schema(WiFiClient client) const { schema((Print&)std::ref(client)); } void schema(Print &print) const; void setDeviceType(const String& deviceType) { setDeviceType(deviceType.c_str()); } void setDeviceType(const char *deviceType);