forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP8266SSDP.cpp
531 lines (454 loc) · 13.8 KB
/
ESP8266SSDP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
ESP8266 Simple Service Discovery
Copyright (c) 2015 Hristo Gochkov
Original (Arduino) version by Filippo Sallemi, July 23, 2014.
Can be found at: https://github.com/nomadnt/uSSDP
License (MIT license):
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef LWIP_OPEN_SRC
#define LWIP_OPEN_SRC
#endif
#include <functional>
#include "ESP8266SSDP.h"
#include "WiFiUdp.h"
#include "debug.h"
extern "C" {
#include "osapi.h"
#include "ets_sys.h"
#include "user_interface.h"
}
#include "lwip/opt.h"
#include "lwip/udp.h"
#include "lwip/inet.h"
#include "lwip/igmp.h"
#include "lwip/mem.h"
#include "include/UdpContext.h"
//#define DEBUG_SSDP Serial
#define SSDP_INTERVAL 1200
#define SSDP_PORT 1900
#define SSDP_METHOD_SIZE 10
#define SSDP_URI_SIZE 2
#define SSDP_BUFFER_SIZE 64
#define SSDP_MULTICAST_TTL 2
// ssdp ipv6 is FF05::C
// lwip-v2's igmp_joingroup only supports IPv4
#define SSDP_MULTICAST_ADDR 239, 255, 255, 250
static const char _ssdp_response_template[] PROGMEM =
"HTTP/1.1 200 OK\r\n"
"EXT:\r\n";
static const char _ssdp_notify_template[] PROGMEM =
"NOTIFY * HTTP/1.1\r\n"
"HOST: 239.255.255.250:1900\r\n"
"NTS: ssdp:alive\r\n";
static const char _ssdp_packet_template[] PROGMEM =
"%s" // _ssdp_response_template / _ssdp_notify_template
"CACHE-CONTROL: max-age=%u\r\n" // _interval
"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://%s:%u/%s\r\n" // WiFi.localIP(), _port, _schemaURL
"\r\n";
static const char _ssdp_schema_template[] PROGMEM =
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/xml\r\n"
"Connection: close\r\n"
"Access-Control-Allow-Origin: *\r\n"
"\r\n"
"<?xml version=\"1.0\"?>"
"<root xmlns=\"urn:schemas-upnp-org:device-1-0\">"
"<specVersion>"
"<major>1</major>"
"<minor>0</minor>"
"</specVersion>"
"<URLBase>http://%s:%u/</URLBase>" // WiFi.localIP(), _port
"<device>"
"<deviceType>%s</deviceType>"
"<friendlyName>%s</friendlyName>"
"<presentationURL>%s</presentationURL>"
"<serialNumber>%s</serialNumber>"
"<modelName>%s</modelName>"
"<modelNumber>%s</modelNumber>"
"<modelURL>%s</modelURL>"
"<manufacturer>%s</manufacturer>"
"<manufacturerURL>%s</manufacturerURL>"
"<UDN>%s</UDN>"
"</device>"
//"<iconList>"
//"<icon>"
//"<mimetype>image/png</mimetype>"
//"<height>48</height>"
//"<width>48</width>"
//"<depth>24</depth>"
//"<url>icon48.png</url>"
//"</icon>"
//"<icon>"
//"<mimetype>image/png</mimetype>"
//"<height>120</height>"
//"<width>120</width>"
//"<depth>24</depth>"
//"<url>icon120.png</url>"
//"</icon>"
//"</iconList>"
"</root>\r\n"
"\r\n";
struct SSDPTimer {
ETSTimer timer;
};
SSDPClass::SSDPClass() :
_server(0),
_timer(0),
_port(80),
_ttl(SSDP_MULTICAST_TTL),
_interval(SSDP_INTERVAL),
_respondToAddr(0,0,0,0),
_respondToPort(0),
_pending(false),
_st_is_uuid(false),
_delay(0),
_process_time(0),
_notify_time(0)
{
_uuid[0] = '\0';
_modelNumber[0] = '\0';
sprintf(_deviceType, "urn:schemas-upnp-org:device:Basic:1");
_friendlyName[0] = '\0';
_presentationURL[0] = '\0';
_serialNumber[0] = '\0';
_modelName[0] = '\0';
_modelURL[0] = '\0';
_manufacturer[0] = '\0';
_manufacturerURL[0] = '\0';
sprintf(_schemaURL, "ssdp/schema.xml");
}
SSDPClass::~SSDPClass() {
end();
}
bool SSDPClass::begin() {
end();
_pending = false;
_st_is_uuid = false;
if (strcmp(_uuid,"") == 0) {
uint32_t chipId = ESP.getChipId();
sprintf_P(_uuid, PSTR("uuid:38323636-4558-4dda-9188-cda0e6%02x%02x%02x"),
(uint16_t) ((chipId >> 16) & 0xff),
(uint16_t) ((chipId >> 8) & 0xff),
(uint16_t) chipId & 0xff);
}
#ifdef DEBUG_SSDP
DEBUG_SSDP.printf("SSDP UUID: %s\n", (char *)_uuid);
#endif
assert(NULL == _server);
_server = new UdpContext;
_server->ref();
IPAddress local = WiFi.localIP();
IPAddress mcast(SSDP_MULTICAST_ADDR);
if (igmp_joingroup(local, mcast) != ERR_OK ) {
#ifdef DEBUG_SSDP
DEBUG_SSDP.printf_P(PSTR("SSDP failed to join igmp group\n"));
#endif
return false;
}
if (!_server->listen(IP_ADDR_ANY, SSDP_PORT)) {
return false;
}
_server->setMulticastInterface(local);
_server->setMulticastTTL(_ttl);
_server->onRx(std::bind(&SSDPClass::_update, this));
if (!_server->connect(mcast, SSDP_PORT)) {
return false;
}
_startTimer();
return true;
}
void SSDPClass::end() {
if(!_server)
return; // object is zeroed already, nothing to do
#ifdef DEBUG_SSDP
DEBUG_SSDP.printf_P(PSTR("SSDP end ... "));
#endif
// undo all initializations done in begin(), in reverse order
_stopTimer();
_server->disconnect();
IPAddress local = WiFi.localIP();
IPAddress mcast(SSDP_MULTICAST_ADDR);
if (igmp_leavegroup(local, mcast) != ERR_OK ) {
#ifdef DEBUG_SSDP
DEBUG_SSDP.printf_P(PSTR("SSDP failed to leave igmp group\n"));
#endif
}
_server->unref();
_server = 0;
#ifdef DEBUG_SSDP
DEBUG_SSDP.printf_P(PSTR("ok\n"));
#endif
}
void SSDPClass::_send(ssdp_method_t method) {
char buffer[1460];
IPAddress ip = WiFi.localIP();
char valueBuffer[strlen_P(_ssdp_notify_template) + 1];
strcpy_P(valueBuffer, (method == NONE) ? _ssdp_response_template : _ssdp_notify_template);
int len = snprintf_P(buffer, sizeof(buffer),
_ssdp_packet_template,
valueBuffer,
_interval,
_modelName,
_modelNumber,
_uuid,
(method == NONE) ? "ST" : "NT",
(_st_is_uuid) ? _uuid : _deviceType,
ip.toString().c_str(), _port, _schemaURL
);
_server->append(buffer, len);
IPAddress remoteAddr;
uint16_t remotePort;
if (method == NONE) {
remoteAddr = _respondToAddr;
remotePort = _respondToPort;
#ifdef DEBUG_SSDP
DEBUG_SSDP.print("Sending Response to ");
#endif
} else {
remoteAddr = IPAddress(SSDP_MULTICAST_ADDR);
remotePort = SSDP_PORT;
#ifdef DEBUG_SSDP
DEBUG_SSDP.println("Sending Notify to ");
#endif
}
#ifdef DEBUG_SSDP
DEBUG_SSDP.print(IPAddress(remoteAddr));
DEBUG_SSDP.print(":");
DEBUG_SSDP.println(remotePort);
#endif
_server->send(remoteAddr, remotePort);
}
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.toString().c_str(), _port,
_deviceType,
_friendlyName,
_presentationURL,
_serialNumber,
_modelName,
_modelNumber,
_modelURL,
_manufacturer,
_manufacturerURL,
_uuid
);
}
void SSDPClass::_update() {
if (!_pending && _server->next()) {
ssdp_method_t method = NONE;
_respondToAddr = _server->getRemoteAddress();
_respondToPort = _server->getRemotePort();
typedef enum {METHOD, URI, PROTO, KEY, VALUE, ABORT} states;
states state = METHOD;
typedef enum {START, MAN, ST, MX} headers;
headers header = START;
uint8_t cursor = 0;
uint8_t cr = 0;
char buffer[SSDP_BUFFER_SIZE] = {0};
while (_server->getSize() > 0) {
char c = _server->read();
(c == '\r' || c == '\n') ? cr++ : cr = 0;
switch (state) {
case METHOD:
if (c == ' ') {
if (strcmp(buffer, "M-SEARCH") == 0) method = SEARCH;
if (method == NONE) state = ABORT;
else state = URI;
cursor = 0;
} else if (cursor < SSDP_METHOD_SIZE - 1) {
buffer[cursor++] = c;
buffer[cursor] = '\0';
}
break;
case URI:
if (c == ' ') {
if (strcmp(buffer, "*")) state = ABORT;
else state = PROTO;
cursor = 0;
} else if (cursor < SSDP_URI_SIZE - 1) {
buffer[cursor++] = c;
buffer[cursor] = '\0';
}
break;
case PROTO:
if (cr == 2) {
state = KEY;
cursor = 0;
}
break;
case KEY:
if (cr == 4) {
_pending = true;
_process_time = millis();
}
else if (c == ' ') {
cursor = 0;
state = VALUE;
}
else if (c != '\r' && c != '\n' && c != ':' && cursor < SSDP_BUFFER_SIZE - 1) {
buffer[cursor++] = c;
buffer[cursor] = '\0';
}
break;
case VALUE:
if (cr == 2) {
switch (header) {
case START:
break;
case MAN:
#ifdef DEBUG_SSDP
DEBUG_SSDP.printf("MAN: %s\n", (char *)buffer);
#endif
break;
case ST:
if (strcmp(buffer, "ssdp:all")) {
state = ABORT;
#ifdef DEBUG_SSDP
DEBUG_SSDP.printf("REJECT: %s\n", (char *)buffer);
#endif
}else{
_st_is_uuid = false;
}
// if the search type matches our type, we should respond instead of ABORT
if (strcasecmp(buffer, _deviceType) == 0) {
_pending = true;
_st_is_uuid = false;
_process_time = millis();
state = KEY;
}
if (strcasecmp(buffer, _uuid) == 0) {
_pending = true;
_st_is_uuid = true;
_process_time = millis();
state = KEY;
}
break;
case MX:
_delay = random(0, atoi(buffer)) * 1000L;
break;
}
if (state != ABORT) {
state = KEY;
header = START;
cursor = 0;
}
} else if (c != '\r' && c != '\n') {
if (header == START) {
if (strncmp(buffer, "MA", 2) == 0) header = MAN;
else if (strcmp(buffer, "ST") == 0) header = ST;
else if (strcmp(buffer, "MX") == 0) header = MX;
}
if (cursor < SSDP_BUFFER_SIZE - 1) {
buffer[cursor++] = c;
buffer[cursor] = '\0';
}
}
break;
case ABORT:
_pending = false; _delay = 0;
break;
}
}
}
if (_pending && (millis() - _process_time) > _delay) {
_pending = false; _delay = 0;
_send(NONE);
} else if(_notify_time == 0 || (millis() - _notify_time) > (_interval * 1000L)){
_notify_time = millis();
_st_is_uuid = false;
_send(NOTIFY);
}
if (_pending) {
while (_server->next())
_server->flush();
}
}
void SSDPClass::setSchemaURL(const char *url) {
strlcpy(_schemaURL, url, sizeof(_schemaURL));
}
void SSDPClass::setHTTPPort(uint16_t port) {
_port = port;
}
void SSDPClass::setDeviceType(const char *deviceType) {
strlcpy(_deviceType, deviceType, sizeof(_deviceType));
}
void SSDPClass::setUUID(const char *uuid) {
snprintf_P(_uuid, sizeof(_uuid), PSTR("uuid:%s"), uuid);
}
void SSDPClass::setName(const char *name) {
strlcpy(_friendlyName, name, sizeof(_friendlyName));
}
void SSDPClass::setURL(const char *url) {
strlcpy(_presentationURL, url, sizeof(_presentationURL));
}
void SSDPClass::setSerialNumber(const char *serialNumber) {
strlcpy(_serialNumber, serialNumber, sizeof(_serialNumber));
}
void SSDPClass::setSerialNumber(const uint32_t serialNumber) {
snprintf(_serialNumber, sizeof(uint32_t) * 2 + 1, "%08X", serialNumber);
}
void SSDPClass::setModelName(const char *name) {
strlcpy(_modelName, name, sizeof(_modelName));
}
void SSDPClass::setModelNumber(const char *num) {
strlcpy(_modelNumber, num, sizeof(_modelNumber));
}
void SSDPClass::setModelURL(const char *url) {
strlcpy(_modelURL, url, sizeof(_modelURL));
}
void SSDPClass::setManufacturer(const char *name) {
strlcpy(_manufacturer, name, sizeof(_manufacturer));
}
void SSDPClass::setManufacturerURL(const char *url) {
strlcpy(_manufacturerURL, url, sizeof(_manufacturerURL));
}
void SSDPClass::setTTL(const uint8_t ttl) {
_ttl = ttl;
}
void SSDPClass::setInterval(uint32_t interval) {
_interval = interval;
}
void SSDPClass::_onTimerStatic(SSDPClass* self) {
self->_update();
}
void SSDPClass::_startTimer() {
_stopTimer();
_timer = new SSDPTimer();
ETSTimer* tm = &(_timer->timer);
const int interval = 1000;
os_timer_disarm(tm);
os_timer_setfn(tm, reinterpret_cast<ETSTimerFunc*>(&SSDPClass::_onTimerStatic), reinterpret_cast<void*>(this));
os_timer_arm(tm, interval, 1 /* repeat */);
}
void SSDPClass::_stopTimer() {
if(!_timer)
return;
ETSTimer* tm = &(_timer->timer);
os_timer_disarm(tm);
delete _timer;
_timer = NULL;
}
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SSDP)
SSDPClass SSDP;
#endif