update with latest version of SSDP

This commit is contained in:
Luc 2019-06-30 15:56:34 +02:00
parent 36ff302558
commit e671169f2d
3 changed files with 52 additions and 14 deletions

View File

@ -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

View File

@ -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;

View File

@ -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");