Arduino DLNA Server
DLNADeviceRequestParser.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "IUDPService.h"
4 #include "Scheduler.h"
5 #include "DLNAControlPointMgr.h"
6 
7 namespace 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(DlnaDebug, "invalid request: %s", req.data.c_str());
31  } else {
32  DlnaLogger.log(DlnaWarning, "invalid request: %s", req.data.c_str());
33  }
34  return nullptr;
35  }
36 
37  // We currently
38  DlnaLogger.log(DlnaWarning, "invalid request: %s", req.data.c_str());
39 
40  return nullptr;
41  }
42 
43  protected:
45  DLNADevice* p_device = nullptr;
46 
48  assert(p_device != nullptr);
49  MSearchReplySchedule& result =
51  char tmp[200];
52  StrView tmp_str(tmp, 200);
53 
54  DlnaLogger.log(DlnaInfo, "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  }
60  result.time = millis() + random(result.mx * 1000);
61 
62  if (parse(req.data, "\nST:", tmp_str)) {
63  result.search_target = tmp;
64 
65  // determine ST if relevant for us
66  for (auto mx : mx_vector) {
67  if (result.search_target.equals(mx)) {
68  DlnaLogger.log(DlnaDebug, "MX: %s -> relevant", mx);
69  result.active = true;
70  }
71  }
72  if (!result.active) {
73  DlnaLogger.log(DlnaDebug, "MX: %s not relevant", tmp);
74  }
75 
76  } else {
77  DlnaLogger.log(DlnaError, "ST: not found");
78  }
79 
80  return result.active ? &result : nullptr;
81  }
82 
83  bool parse(Str& in, const char* tag, StrView& result) {
84  result.clearAll();
85  int start = in.indexOf(tag);
86  if (start >= 0) {
87  start += strlen(tag);
88  int end = in.indexOf("\r\n", start);
89  if (end < 0) end = in.indexOf("\n", start);
90  if (end >= 0) {
91  result.substring(in.c_str(), start, end);
92  DlnaLogger.log(DlnaDebug, "%s substring (%d,%d)->%s", tag, start, end,
93  result.c_str());
94 
95  result.trim();
96  return true;
97  }
98  }
99  return false;
100  }
101 };
102 
103 } // namespace tiny_dlna
Translates DLNA UDP Requests to Schedule so that we can schedule a reply.
Definition: DLNADeviceRequestParser.h:15
Schedule * parse(DLNADevice &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:83
Vector< const char * > mx_vector
Definition: DLNADeviceRequestParser.h:44
DLNADevice * p_device
Definition: DLNADeviceRequestParser.h:45
Schedule * processMSearch(RequestData &req)
Definition: DLNADeviceRequestParser.h:47
Device Attributes and generation of XML using urn:schemas-upnp-org:device-1-0. We could just return a...
Definition: DLNADevice.h:27
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:77
Str search_target
Definition: Schedule.h:107
int mx
Definition: Schedule.h:110
A simple wrapper to provide string functions on char*. If the underlying char* is a const we do not a...
Definition: StrView.h:25
virtual void clearAll()
Definition: StrView.h:549
virtual int indexOf(const char c, int start=0)
Definition: StrView.h:267
int toInt()
Converts the string to an int.
Definition: StrView.h:586
virtual const char * c_str()
provides the string value as const char*
Definition: StrView.h:366
virtual void trim()
remove leading and traling spaces
Definition: StrView.h:510
virtual void substring(StrView &from, int start, int end)
copies a substring into the current string
Definition: StrView.h:483
virtual bool equals(const char *str)
checks if the string equals indicated parameter string
Definition: StrView.h:171
virtual bool contains(const char *str)
checks if the string contains a substring
Definition: StrView.h:277
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:188
void push_back(T &&value)
Definition: Vector.h:167
Definition: Allocator.h:6
@ DlnaDebug
Definition: Logger.h:16
@ DlnaInfo
Definition: Logger.h:16
@ DlnaWarning
Definition: Logger.h:16
@ DlnaError
Definition: Logger.h:16
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