Arduino DLNA Server
Loading...
Searching...
No Matches
DLNADeviceRequestParser.h
Go to the documentation of this file.
1#pragma once
2
3#include "IUDPService.h"
4#include "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", req.data.c_str());
31 } else {
32 DlnaLogger.log(DlnaLogLevel::Warning, "invalid request: %s", req.data.c_str());
33 }
34 return nullptr;
35 }
36
37 // We currently
38 DlnaLogger.log(DlnaLogLevel::Warning, "invalid request: %s", req.data.c_str());
39
40 return nullptr;
41 }
42
43 protected:
46
48 assert(p_device != nullptr);
49 MSearchReplySchedule& result =
51 char tmp[200];
52 StrView tmp_str(tmp, 200);
53
54 DlnaLogger.log(DlnaLogLevel::Info, "Parsing MSSearch");
55
56 // determine MX (seconds to delay response)
57 if (parse(req.data, "\nMX:", tmp_str)) {
58 result.mx = tmp_str.toInt();
59 } else {
60 result.mx = 1;
61 }
62 result.time = millis() + random(result.mx * 1000);
63
64 if (parse(req.data, "\nST:", tmp_str)) {
65 result.search_target = tmp;
66
67 // determine ST if relevant for us
68 for (auto mx : mx_vector) {
69 if (result.search_target.equals(mx)) {
70 DlnaLogger.log(DlnaLogLevel::Info, "- MX: %s -> relevant", mx);
71 result.active = true;
72 }
73 }
74 if (!result.active) {
75 DlnaLogger.log(DlnaLogLevel::Info, "-> MX: %s not relevant", tmp);
76 }
77
78 } else {
79 DlnaLogger.log(DlnaLogLevel::Error, "-> ST: not found");
80 }
81
82 return result.active ? &result : nullptr;
83 }
84
85 bool parse(Str& in, const char* tag, StrView& result) {
86 result.clearAll();
87 int start = in.indexOf(tag);
88 if (start >= 0) {
89 start += strlen(tag);
90 int end = in.indexOf("\r\n", start);
91 if (end < 0) end = in.indexOf("\n", start);
92 if (end >= 0) {
93 result.substrView(in.c_str(), start, end);
94 DlnaLogger.log(DlnaLogLevel::Debug, "%s substrView (%d,%d)->%s", tag, start, end,
95 result.c_str());
96
97 result.trim();
98 return true;
99 }
100 }
101 return false;
102 }
103};
104
105} // 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:27
Translates DLNA UDP Requests to Schedule so that we can schedule a reply.
Definition: DLNADeviceRequestParser.h:15
DLNADeviceInfo * p_device
Definition: DLNADeviceRequestParser.h:45
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:85
Schedule * processMSearch(RequestData &req)
Definition: DLNADeviceRequestParser.h:47
Vector< const char * > mx_vector
Definition: DLNADeviceRequestParser.h:44
void log(DlnaLogLevel current_level, const char *fmt...)
Print log message.
Definition: Logger.h:40
Answer from device to MSearch request by sending a reply.
Definition: Schedule.h:78
Str search_target
Definition: Schedule.h:108
int mx
Definition: Schedule.h:111
A simple wrapper to provide string functions on char*. If the underlying char* is a const we do not a...
Definition: StrView.h:19
virtual void substrView(StrView &from, int start, int end)
copies a substring into the current string
Definition: StrView.h:493
virtual void clearAll()
Definition: StrView.h:559
virtual const char * c_str()
provides the string value as const char*
Definition: StrView.h:376
virtual int indexOf(const char c, int start=0)
Definition: StrView.h:275
int toInt()
Converts the string to an int.
Definition: StrView.h:596
virtual void trim()
remove leading and traling spaces
Definition: StrView.h:520
virtual bool equals(const char *str)
checks if the string equals indicated parameter string
Definition: StrView.h:178
virtual bool contains(const char *str)
checks if the string contains a substring
Definition: StrView.h:285
String implementation which keeps the data on the heap. We grow the allocated memory only if the copy...
Definition: Str.h:22
const char * c_str()
provides the string value as const char*
Definition: Str.h:187
Vector implementation which provides the most important methods as defined by std::vector....
Definition: Vector.h:21
void push_back(T &&value)
Definition: Vector.h:171
Definition: Allocator.h:6
LoggerClass DlnaLogger
Definition: Logger.cpp:5
Provides information of the received UDP which consists of the (xml) data and the peer address and po...
Definition: IUDPService.h:23
IPAddressAndPort peer
Definition: IUDPService.h:25
Str data
Definition: IUDPService.h:24
An individual Schedule (to send out UDP messages)
Definition: Schedule.h:17
bool active
Definition: Schedule.h:26
uint64_t time
Definition: Schedule.h:20