Arduino DLNA Server
Loading...
Searching...
No Matches
DLNADeviceRequestParser.h
Go to the documentation of this file.
1#pragma once
2
3#include "dlna/Scheduler.h"
6
7namespace tiny_dlna {
8
16 public:
17 // add ST that we consider as valid for the actual device
18 void addMSearchST(const char* accept) { mx_vector.push_back(accept); }
19
21 p_device = &device;
22 Schedule result;
23 if (req.data.contains("M-SEARCH")) {
24 return processMSearch(req);
25 }
26
27 // We ignore alive notifications
28 if (req.data.contains("NOTIFY")) {
29 if (req.data.contains("ssdp:alive")) {
30 DlnaLogger.log(DlnaLogLevel::Debug, "invalid request: %s",
31 req.data.c_str());
32 } else {
33 DlnaLogger.log(DlnaLogLevel::Warning, "invalid request: %s",
34 req.data.c_str());
35 }
36 return nullptr;
37 }
38
39 // We currently
40 DlnaLogger.log(DlnaLogLevel::Warning, "invalid request: %s",
41 req.data.c_str());
42
43 return nullptr;
44 }
45
46 protected:
49
51 assert(p_device != nullptr);
52 // allocate schedule on the heap; if not relevant we must delete it to avoid
53 // leaking the schedule and its internal buffers (e.g. Str)
54 MSearchReplySchedule* resultPtr =
56 MSearchReplySchedule& result = *resultPtr;
57 char tmp[200];
58 StrView tmp_str(tmp, 200);
59
60 DlnaLogger.log(DlnaLogLevel::Debug, "Parsing MSSearch");
61
62 // determine MX (seconds to delay response)
63 if (parse(req.data, "\nMX:", tmp_str)) {
64 result.mx = tmp_str.toInt();
65 } else {
66 result.mx = 1;
67 }
68 result.time = millis() + random(result.mx * 1000);
69
70 if (parse(req.data, "\nST:", tmp_str)) {
71 result.search_target = tmp;
72
73 // determine ST if relevant for us
74 for (auto mx : mx_vector) {
75 if (result.search_target.equals(mx)) {
76 DlnaLogger.log(DlnaLogLevel::Debug, "- MX: %s -> relevant", mx);
77 result.active = true;
78 }
79 }
80 if (!result.active) {
81 DlnaLogger.log(DlnaLogLevel::Debug, "-> MX: %s not relevant", tmp);
82 }
83
84 } else {
85 DlnaLogger.log(DlnaLogLevel::Error, "-> ST: not found");
86 }
87
88 // Check if the schedule is valid (e.g., netmask filtering)
89 if (result.active && !result.isValid()) {
90 result.active = false;
91 }
92
93 if (result.active) {
94 return resultPtr;
95 } else {
96 // not relevant -> free allocated schedule
97 delete resultPtr;
98 return nullptr;
99 }
100 }
101
102 bool parse(Str& in, const char* tag, StrView& result) {
103 result.clearAll();
104 int start = in.indexOf(tag);
105 if (start >= 0) {
106 start += strlen(tag);
107 int end = in.indexOf("\r\n", start);
108 if (end < 0) end = in.indexOf("\n", start);
109 if (end >= 0) {
110 result.substrView(in.c_str(), start, end);
111 DlnaLogger.log(DlnaLogLevel::Debug, "%s substrView (%d,%d)->%s", tag,
112 start, end, result.c_str());
113
114 result.trim();
115 return true;
116 }
117 }
118 return false;
119 }
120};
121
122} // namespace tiny_dlna
Device Attributes and generation of XML using urn:schemas-upnp-org:device-1-0. We could just return a...
Definition: DLNADeviceInfo.h:25
Translates DLNA UDP Requests to Schedule so that we can schedule a reply.
Definition: DLNADeviceRequestParser.h:15
DLNADeviceInfo * p_device
Definition: DLNADeviceRequestParser.h:48
Schedule * parse(DLNADeviceInfo &device, RequestData &req)
Definition: DLNADeviceRequestParser.h:20
void addMSearchST(const char *accept)
Definition: DLNADeviceRequestParser.h:18
bool parse(Str &in, const char *tag, StrView &result)
Definition: DLNADeviceRequestParser.h:102
Schedule * processMSearch(RequestData &req)
Definition: DLNADeviceRequestParser.h:50
Vector< const char * > mx_vector
Definition: DLNADeviceRequestParser.h:47
Answer from device to MSearch request by sending a reply.
Definition: Schedule.h:84
Str search_target
Definition: Schedule.h:135
bool isValid() override
Definition: Schedule.h:115
int mx
Definition: Schedule.h:137
A simple wrapper to provide string functions on char*. If the underlying char* is a const we do not a...
Definition: StrView.h:18
virtual void substrView(StrView &from, int start, int end)
copies a substring into the current string
Definition: StrView.h:505
virtual void clearAll()
Definition: StrView.h:571
virtual const char * c_str()
provides the string value as const char*
Definition: StrView.h:376
int toInt()
Converts the string to an int.
Definition: StrView.h:607
virtual void trim()
remove leading and traling spaces
Definition: StrView.h:532
Heap-backed string utility used throughout tiny_dlna.
Definition: Str.h:27
bool equals(const char *other) const
Exact string equality with C-string.
Definition: Str.h:167
int indexOf(const char *substr, int start=0) const
Index of substring from position (or -1)
Definition: Str.h:214
bool contains(const char *sub) const
True if substring occurs.
Definition: Str.h:171
const char * c_str() const
C-string pointer to internal buffer.
Definition: Str.h:88
Lightweight wrapper around std::vector with Arduino-friendly helpers and a pluggable allocator.
Definition: Vector.h:39
Definition: Allocator.h:13
Provides information of the received UDP which consists of the (xml) data and the peer address and po...
Definition: IUDPService.h:22
IPAddressAndPort peer
Definition: IUDPService.h:24
Str data
Definition: IUDPService.h:23
An individual Schedule (to send out UDP messages)
Definition: Schedule.h:18
bool active
Definition: Schedule.h:27
uint64_t time
Definition: Schedule.h:21