Arduino DLNA Server
Loading...
Searching...
No Matches
Schedule.h
Go to the documentation of this file.
1#pragma once
2
6
7#define MAX_TMP_SIZE 400
8#define ALIVE_MS 0
9#define MAX_AGE (60 * 60 * 24)
10#define MULTI_MSG_DELAY_MS 80
11
12namespace tiny_dlna {
13
18struct Schedule {
19 // int id = 0;
20 // scheduled next execution time
21 uint64_t time = 0;
22 // repeat every n ms
23 uint32_t repeat_ms = 0;
24 // repeat until;
25 uint64_t end_time = 0;
26 // schedle is active
27 bool active = false;
28 // optional address associated with the schedule (e.g. requester)
30 // enables logging the address when the schedule is queued
31 bool report_ip = false;
32
33 virtual bool process(IUDPService& udp) { return false; }
34
35 virtual const char* name() { return "n/a"; };
36
37 virtual bool isValid() { return true; }
38
39 operator bool() { return active; }
40};
41
46class MSearchSchedule : public Schedule {
47 public:
48 MSearchSchedule(IPAddressAndPort addr, const char* searchTarget, int mx = 3) {
49 this->address = addr;
50 search_target = searchTarget;
51 max_age = mx;
52 }
53 const char* name() override { return "MSearch"; }
54
55 bool process(IUDPService& udp) override {
56 // we keep the data on the stack
57 DlnaLogger.log(DlnaLogLevel::Debug, "Sending %s for %s to %s", name(),
59
60 char buffer[MAX_TMP_SIZE] = {0};
61 const char* tmp =
62 "M-SEARCH * HTTP/1.1\r\n"
63 "HOST: %s\r\n"
64 "MAN: \"ssdp:discover\"\r\n"
65 "MX: %d\r\n"
66 "ST: %s\r\n\r\n";
67 int n = snprintf(buffer, MAX_TMP_SIZE, tmp, address.toString(), max_age,
69 assert(n < MAX_TMP_SIZE);
70 DlnaLogger.log(DlnaLogLevel::Debug, "sending: %s", buffer);
71 udp.send(address, (uint8_t*)buffer, n);
72 return true;
73 }
74
75 protected:
76 int max_age = 3;
77 const char* search_target = nullptr;
78};
79
85 public:
87 this->address = addr;
88 this->report_ip = true;
89 p_device = &device;
90 }
91 const char* name() override { return "MSearchReply"; }
92
93 bool process(IUDPService& udp) override {
94 // we keep the data on the stack
95 DlnaLogger.log(DlnaLogLevel::Info, "Sending %s for %s to %s", name(),
97
98 DLNADeviceInfo& device = *p_device;
99 const char* udn = device.getUDN();
100 int delay_ms = MULTI_MSG_DELAY_MS;
101
102 sendReply(udp, device, "upnp:rootdevice", udn);
103 delay(delay_ms);
104 sendReply(udp, device, p_device->getDeviceType(), udn);
105 // announce with service udn
106 for (auto& service : device.getServices()) {
107 delay(delay_ms);
108 sendReply(udp, device, service.service_type, udn);
109 }
110 return true;
111 }
112
113 bool isValid() override {
114 // check if the search target is valid for this device
116 return false;
117 }
118 return isValidIP();
119 }
120
123 int mx = 0;
124
125 protected:
127
129 bool isValidIP() {
130 IPAddress netmask = DLNA_DISCOVERY_NETMASK;
131 IPAddress localIP = p_device->getIPAddress();
132 IPAddress peerIP = address.address;
133 Str netmask_str = netmask.toString().c_str();
134 Str localIP_str = localIP.toString().c_str();
135
136 // Apply netmask to both local and peer IP addresses
137 for (int i = 0; i < 4; i++) {
138 if ((localIP[i] & netmask[i]) != (peerIP[i] & netmask[i])) {
139 DlnaLogger.log(DlnaLogLevel::Info,
140 "Discovery request from %s filtered (not in same subnet "
141 "as %s with mask %s)",
142 address.toString(), localIP_str.c_str(),
143 netmask_str.c_str());
144 return false;
145 }
146 }
147 return true;
148 }
149
150 // check requested search target
152 if (StrView(search_target).equals("ssdp:all")) {
153 return true;
154 }
155 if (StrView(search_target).equals("upnp:rootdevice")) {
156 return true;
157 }
158 if (StrView(search_target).equals(device.getDeviceType())) {
159 return true;
160 }
161 // check services
162 for (auto& service : device.getServices()) {
163 if (StrView(search_target).equals(service.service_type)) {
164 return true;
165 }
166 }
167 DlnaLogger.log(DlnaLogLevel::Info, "Ignoring M-SEARCH for %s",
169 return false;
170 }
171
172 bool sendReply(IUDPService& udp, DLNADeviceInfo& device, const char* target,
173 const char* udn) {
174 // construct usn
175 char usn_str[200];
176 snprintf(usn_str, 200, "%s::%s", device.getUDN(), target);
177 // construct message
178 char buffer[MAX_TMP_SIZE] = {0};
179 const char* tmp =
180 "HTTP/1.1 200 OK\r\n"
181 "CACHE-CONTROL: max-age=%d\r\n"
182 "EXT:\r\n"
183 "LOCATION: %s\r\n"
184 "SERVER: Arduino-DLNA/1.0 UPnP/1.1 DLNA/1.5\r\n"
185 "ST: %s\r\n"
186 "USN: %s\r\n"
187 "CONTENT-LENGTH: 0\r\n\r\n";
188 int n = snprintf(buffer, MAX_TMP_SIZE, tmp, max_age,
189 device.getDeviceURL().url(), target, usn_str);
190 assert(n < MAX_TMP_SIZE);
191 DlnaLogger.log(DlnaLogLevel::Info, "- %s: %s", name(), target);
192 DlnaLogger.log(DlnaLogLevel::Debug, "%s", buffer);
193 if (!udp.send(address, (uint8_t*)buffer, n)) {
194 DlnaLogger.log(DlnaLogLevel::Warning, "Failed to send MSearchReply to %s",
195 address.toString());
196 return false;
197 }
198 return true;
199 }
200};
201
207class MSearchReplyCP : public Schedule {
208 public:
209 const char* name() override { return "MSearchReplyCP"; }
213
214 bool process(IUDPService& udp) override {
215 DlnaLogger.log(DlnaLogLevel::Debug, "-> %s not processed",
217 return true;
218 }
219};
220
249 public:
250 const char* name() override { return "NotifyReplyCP"; }
251 Str nts{80};
257
258 // callback invoked by the scheduler when processing this notification.
259 // Return true if the notification was handled; false otherwise.
260 std::function<bool(NotifyReplyCP& ref)> callback;
261
262 bool process(IUDPService& udp) override {
263 if (callback(*this)) {
264 DlnaLogger.log(DlnaLogLevel::Debug, "%s -> %s", name(), nts.c_str());
265 return true;
266 }
267
268 DlnaLogger.log(DlnaLogLevel::Debug, "-> %s not processed", nts.c_str());
269 return true;
270 }
271};
272
278 public:
279 PostAliveSchedule(DLNADeviceInfo& device, uint32_t repeatMs) {
280 p_device = &device;
281 this->repeat_ms = repeatMs;
282 }
283 const char* name() override { return "PostAlive"; }
284
285 void setRepeatMs(uint32_t ms) { this->repeat_ms = ms; }
286
287 bool process(IUDPService& udp) override {
288 DlnaLogger.log(DlnaLogLevel::Debug, "Sending %s to %s", name(),
289 DLNABroadcastAddress.toString());
290 DLNADeviceInfo& device = *p_device;
291 char nt[100];
292
293 const char* device_url = device.getDeviceURL().url();
294 const char* udn = device.getUDN();
295 int max_age = repeat_ms / 1000 + 10;
296 int delay_ms = MULTI_MSG_DELAY_MS;
297
298 // announce with udn
299 sendData(udn, udn, device_url, max_age, udp);
300 // NT: upnp:rootdevice\r\n
301 sendData("upnp:rootdevice", udn, device_url, max_age, udp);
302 delay(delay_ms);
303 // NT: urn:schemas-upnp-org:device:MediaRenderer:1\r\n
304 sendData(device.getDeviceType(), udn, device_url, max_age, udp);
305
306 // announce with service udn
307 for (auto& service : device.getServices()) {
308 delay(delay_ms);
309 sendData(service.service_type, udn, device_url, max_age, udp);
310 }
311 return true;
312 }
313
314 protected:
316
317 bool sendData(const char* nt, const char* udn, const char* device_url,
318 int max_age, IUDPService& udp) {
319 char usn[200];
320 if (StrView(nt).equals(udn)) {
321 strcpy(usn, nt);
322 } else {
323 snprintf(usn, 200, "%s::%s", udn, nt);
324 }
325
326 // we keep the data on the stack
327 char buffer[MAX_TMP_SIZE] = {0};
328 const char* tmp =
329 "NOTIFY * HTTP/1.1\r\n"
330 "HOST:%s\r\n"
331 "CACHE-CONTROL: max-age=%d\r\n"
332 "LOCATION: %s\r\n"
333 "NT: %s\r\n"
334 "NTS: ssdp:alive\r\n"
335 "USN: %s\r\n\r\n";
336 int n = snprintf(buffer, MAX_TMP_SIZE, tmp, DLNABroadcastAddress.toString(),
337 max_age, device_url, nt, usn);
338
339 assert(n < MAX_TMP_SIZE);
340 DlnaLogger.log(DlnaLogLevel::Info, "sending: ssdp:alive %s", nt);
341 DlnaLogger.log(DlnaLogLevel::Debug, "%s", buffer);
342 udp.send(DLNABroadcastAddress, (uint8_t*)buffer, n);
343 return true;
344 }
345};
346
351class PostByeSchedule : public Schedule {
352 public:
354 const char* name() override { return "ByeBye"; }
355 bool process(IUDPService& udp) override {
356 DlnaLogger.log(DlnaLogLevel::Debug, "Sending %s to %s", name(),
357 DLNABroadcastAddress.toString());
358 // we keep the data on the stack
359 char buffer[MAX_TMP_SIZE] = {0};
360 const char* tmp =
361 "NOTIFY * HTTP/1.1\r\n"
362 "HOST: %s\r\n"
363 "CACHE-CONTROL: max-age = %d\r\n"
364 "LOCATION: *\r\n"
365 "NT: %s\r\n"
366 "NTS: ssdp:byebye\r\n"
367 "USN: %s\r\n\r\n";
368 int n = snprintf(buffer, MAX_TMP_SIZE, tmp, DLNABroadcastAddress.toString(),
370
371 DlnaLogger.log(DlnaLogLevel::Debug, "sending: %s", buffer);
372 udp.send(DLNABroadcastAddress, (uint8_t*)buffer, n);
373 return true;
374 }
375
376 protected:
377 int max_age = 1800;
379};
380
385class PostSubscribe : public Schedule {
386 public:
387 PostSubscribe(IPAddressAndPort addr, const char* path, uint32_t sec) {
388 setDestination(addr, path);
389 setDuration(sec);
390 }
391 const char* name() override { return "Subscribe"; }
392
393 bool process(IUDPService& udp) override {
395 DlnaLogger.log(DlnaLogLevel::Debug, "Sending Subscribe to %s",
396 address.toString());
397
398 char buffer[MAX_TMP_SIZE] = {0};
399 const char* tmp =
400 "SUBSCRIBE %s HTTP/1.1\r\n"
401 "HOST: %s\r\n"
402 "CALLBACK: %s"
403 "NT: upnp-event\r\n"
404 "TIMEOUT: Second-%d\r\n\r\n";
405 int n = snprintf(buffer, MAX_TMP_SIZE, tmp, path, address.toString(),
407 assert(n < MAX_TMP_SIZE);
408 DlnaLogger.log(DlnaLogLevel::Debug, "sending: %s", buffer);
409 udp.send(address, (uint8_t*)buffer, n);
410 return true;
411 }
412
413 protected:
414 const char* path;
415 int durationSec = 0;
416
417 void setDestination(IPAddressAndPort addr, const char* path) {
418 this->address = addr;
419 this->path = path;
420 }
421
422 void setDuration(uint32_t sec) { durationSec = sec; }
423};
424
430 public:
431 CallbackSchedule(std::function<bool(void* ref)> cb, void* ref) {
432 callback = cb;
433 reference = ref;
434 }
435 const char* name() override { return "Callback"; }
436 void* reference = nullptr;
437
438 bool process(IUDPService& udp) override {
439 if (callback) {
440 return callback(reference);
441 }
442 return false;
443 }
444
445 protected:
446 std::function<bool(void* ref)> callback;
447};
448
449} // namespace tiny_dlna
#define MAX_TMP_SIZE
Definition: Schedule.h:7
#define MAX_AGE
Definition: Schedule.h:9
#define MULTI_MSG_DELAY_MS
Definition: Schedule.h:10
Generic Schedule that invokes a callback function.
Definition: Schedule.h:429
CallbackSchedule(std::function< bool(void *ref)> cb, void *ref)
Definition: Schedule.h:431
void * reference
Definition: Schedule.h:436
std::function< bool(void *ref)> callback
Definition: Schedule.h:446
const char * name() override
Definition: Schedule.h:435
bool process(IUDPService &udp) override
Definition: Schedule.h:438
Device Attributes and generation of XML using urn:schemas-upnp-org:device-1-0. We could just return a...
Definition: DLNADeviceInfo.h:28
Vector< DLNAServiceInfo > & getServices()
Provides all service definitions.
Definition: DLNADeviceInfo.h:205
const char * getUDN()
Provide the udn uuid.
Definition: DLNADeviceInfo.h:90
Url & getDeviceURL()
This method returns base url/device.xml.
Definition: DLNADeviceInfo.h:131
IPAddress getIPAddress()
Provides the local IP address.
Definition: DLNADeviceInfo.h:146
const char * getDeviceType()
Definition: DLNADeviceInfo.h:84
Abstract Interface for UDP API.
Definition: IUDPService.h:33
virtual bool send(uint8_t *data, int len)=0
Send data to the default destination.
Processing at control point to handle a MSearchReply from the device.
Definition: Schedule.h:207
Str usn
Definition: Schedule.h:211
const char * name() override
Definition: Schedule.h:209
Str location
Definition: Schedule.h:210
Str search_target
Definition: Schedule.h:212
bool process(IUDPService &udp) override
Definition: Schedule.h:214
Answer from device to MSearch request by sending the related replies.
Definition: Schedule.h:84
Str search_target
Definition: Schedule.h:121
bool isValid() override
Definition: Schedule.h:113
int mx
Definition: Schedule.h:123
bool process(IUDPService &udp) override
Definition: Schedule.h:93
bool isValidIP()
check netmask
Definition: Schedule.h:129
MSearchReplySchedule(DLNADeviceInfo &device, IPAddressAndPort addr)
Definition: Schedule.h:86
DLNADeviceInfo * p_device
Definition: Schedule.h:122
const char * name() override
Definition: Schedule.h:91
bool isValidSearchTarget(DLNADeviceInfo &device, const char *search_target)
Definition: Schedule.h:151
bool sendReply(IUDPService &udp, DLNADeviceInfo &device, const char *target, const char *udn)
Definition: Schedule.h:172
int max_age
Definition: Schedule.h:126
Send MSearch request.
Definition: Schedule.h:46
const char * search_target
Definition: Schedule.h:77
bool process(IUDPService &udp) override
Definition: Schedule.h:55
const char * name() override
Definition: Schedule.h:53
MSearchSchedule(IPAddressAndPort addr, const char *searchTarget, int mx=3)
Definition: Schedule.h:48
int max_age
Definition: Schedule.h:76
Represents a notification/notify reply scheduled for control-point processing.
Definition: Schedule.h:248
std::function< bool(NotifyReplyCP &ref)> callback
Definition: Schedule.h:260
Str delivery_host_and_port
Definition: Schedule.h:252
Str xml
Definition: Schedule.h:256
Str event_key
Definition: Schedule.h:255
Str subscription_id
Definition: Schedule.h:254
Str delivery_path
Definition: Schedule.h:253
const char * name() override
Definition: Schedule.h:250
bool process(IUDPService &udp) override
Definition: Schedule.h:262
Str nts
Definition: Schedule.h:251
Send out PostAlive messages: Repeated every 5 seconds.
Definition: Schedule.h:277
bool sendData(const char *nt, const char *udn, const char *device_url, int max_age, IUDPService &udp)
Definition: Schedule.h:317
DLNADeviceInfo * p_device
Definition: Schedule.h:315
PostAliveSchedule(DLNADeviceInfo &device, uint32_t repeatMs)
Definition: Schedule.h:279
const char * name() override
Definition: Schedule.h:283
bool process(IUDPService &udp) override
Definition: Schedule.h:287
void setRepeatMs(uint32_t ms)
Definition: Schedule.h:285
Send out ByeBye message.
Definition: Schedule.h:351
DLNADeviceInfo * p_device
Definition: Schedule.h:378
PostByeSchedule(DLNADeviceInfo &device)
Definition: Schedule.h:353
bool process(IUDPService &udp) override
Definition: Schedule.h:355
int max_age
Definition: Schedule.h:377
const char * name() override
Definition: Schedule.h:354
Send SUBSCRIBE message via UDP unicast.
Definition: Schedule.h:385
const char * name() override
Definition: Schedule.h:391
void setDuration(uint32_t sec)
Definition: Schedule.h:422
void setDestination(IPAddressAndPort addr, const char *path)
Definition: Schedule.h:417
PostSubscribe(IPAddressAndPort addr, const char *path, uint32_t sec)
Definition: Schedule.h:387
const char * path
Definition: Schedule.h:414
int durationSec
Definition: Schedule.h:415
bool process(IUDPService &udp) override
Definition: Schedule.h:393
A simple wrapper to provide string functions on char*. If the underlying char* is a const we do not a...
Definition: StrView.h:18
Heap-backed string utility used throughout tiny_dlna.
Definition: Str.h:27
const char * c_str() const
C-string pointer to internal buffer.
Definition: Str.h:88
const char * url()
Definition: Url.h:39
#define DLNA_DISCOVERY_NETMASK
Define the netmask for discovery filtering.
Definition: dlna_config.h:197
Definition: Allocator.h:13
IP Adress including Port information.
Definition: IPAddressAndPort.h:20
const char * toString()
Definition: IPAddressAndPort.h:26
IPAddress address
Definition: IPAddressAndPort.h:23
An individual Schedule (to send out UDP messages)
Definition: Schedule.h:18
uint64_t end_time
Definition: Schedule.h:25
IPAddressAndPort address
Definition: Schedule.h:29
bool active
Definition: Schedule.h:27
bool report_ip
Definition: Schedule.h:31
virtual const char * name()
Definition: Schedule.h:35
virtual bool isValid()
Definition: Schedule.h:37
virtual bool process(IUDPService &udp)
Definition: Schedule.h:33
uint64_t time
Definition: Schedule.h:21
uint32_t repeat_ms
Definition: Schedule.h:23