Arduino DLNA Server
DLNADevice.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <functional> // std::bind
5 
6 #include "DLNAServiceInfo.h"
7 #include "StringRegistry.h"
8 #include "basic/Icon.h"
9 #include "basic/Vector.h"
10 #include "service/Action.h"
11 #include "vector"
12 #include "xml/XMLPrinter.h"
13 
14 namespace tiny_dlna {
15 
27 class DLNADevice {
28  friend class XMLDeviceParser;
29  friend class DLNAControlPointMgr;
30 
31  public:
32  DLNADevice(bool ok = true) { is_active = true; }
34  void print(Print& out) {
35  xml.setOutput(out);
37  auto printRootCb = std::bind(&DLNADevice::printRoot, this);
38  xml.printNode("root", printRootCb, ns);
39  }
40 
41  // sets the device type (ST or NT)
42  void setDeviceType(const char* st) { device_type = st; }
43 
44  const char* getDeviceType() { return device_type; }
45 
47  void setUDN(const char* id) { udn = id; }
48 
50  const char* getUDN() { return udn; }
51 
53  void setBaseURL(Url url) { base_url = url; }
54 
57  // replace localhost url
58  if (StrView(base_url.host()).contains("localhost")) {
59  String url_str;
60  url_str = base_url.url();
61  url_str.replace("localhost", getIPStr());
62  Url new_url{url_str.c_str()};
63  base_url = new_url;
64  }
65  return base_url;
66  }
67 
70  if (!device_url) {
71  Str str = getBaseURL().url();
72  if (!str.endsWith("/")) str += "/";
73  str += "device.xml";
74  Url new_url(str.c_str());
75  device_url = new_url;
76  }
77  return device_url;
78  }
79 
80  void setIPAddress(IPAddress address) { localhost = address; }
81 
82  IPAddress getIPAddress() { return localhost; }
83 
85  const char* getIPStr() {
86  static char result[80] = {0};
87  snprintf(result, 80, "%d.%d.%d.%d", localhost[0], localhost[1],
88  localhost[2], localhost[3]);
89  return result;
90  }
91 
92  void setNS(const char* ns) { this->ns = ns; }
93  const char* getNS() { return ns; }
94  void setFriendlyName(const char* name) { friendly_name = name; }
95  const char* getFriendlyName() { return friendly_name; }
96  void setManufacturer(const char* man) { manufacturer = man; }
97  const char* getManufacturer() { return manufacturer; }
98  void setManufacturerURL(const char* url) { manufacturer_url = url; }
99  const char* getManufacturerURL() { return manufacturer_url; }
100  void setModelDescription(const char* descr) { model_description = descr; }
101  const char* getModelDescription() { return model_description; }
102  void setModelName(const char* name) { model_name = name; }
103  const char* getModelName() { return model_name; }
104  void setModelNumber(const char* number) { model_number = number; }
105  const char* getModelNumber() { return model_number; }
106  void setSerialNumber(const char* sn) { serial_number = sn; }
107  const char* getSerialNumber() { return serial_number; }
108  void setUniveralProductCode(const char* upc) { universal_product_code = upc; }
110 
112  void addService(DLNAServiceInfo s) { services.push_back(s); }
113 
115  DLNAServiceInfo& getService(const char* id) {
116  static DLNAServiceInfo result{false};
117  for (auto& service : services) {
118  if (StrView(service.service_id).contains(id)) {
119  return service;
120  }
121  }
122  return result;
123  }
124 
126 
127  void clear() {
128  services.clear();
129  strings.clear();
130  udn = nullptr;
131  ns = nullptr;
132  device_type = nullptr;
133  friendly_name = nullptr;
134  manufacturer = nullptr;
135  manufacturer_url = nullptr;
136  model_description = nullptr;
137  model_name = nullptr;
138  model_number = nullptr;
139  serial_number = nullptr;
140  universal_product_code = nullptr;
141  }
142 
144  void clearIcons() { icons.clear(); }
145  void addIcon(Icon icon) { icons.push_back(icon); }
146  Icon getIcon(int idx = 0) { return icons[idx]; }
147 
148  operator bool() { return is_active; }
149 
151  const char* addString(char* string) { return strings.add(string); }
152 
154  void updateTimestamp() { timestamp = millis(); }
155 
157  uint32_t getTimestamp() { return timestamp; }
158 
159  void setActive(bool flag) { is_active = flag; }
160 
162 
163  protected:
164  uint64_t timestamp = 0;
165  bool is_active = true;
167  Url base_url{"http://localhost:9876/dlna"};
169  IPAddress localhost;
170  int version_major = 1;
171  int version_minor = 0;
172  const char* udn = "uuid:09349455-2941-4cf7-9847-0dd5ab210e97";
173  const char* ns = "xmlns=\"urn:schemas-upnp-org:device-1-0\"";
174  const char* device_type = nullptr;
175  const char* friendly_name = nullptr;
176  const char* manufacturer = nullptr;
177  const char* manufacturer_url = nullptr;
178  const char* model_description = nullptr;
179  const char* model_name = nullptr;
180  const char* model_url = nullptr;
181  const char* model_number = nullptr;
182  const char* serial_number = nullptr;
183  const char* universal_product_code = nullptr;
188 
189  size_t printRoot() {
190  size_t result = 0;
191  auto printSpecVersionB = std::bind(&DLNADevice::printSpecVersion, this);
192  result += xml.printNode("specVersion", printSpecVersionB);
193  result += xml.printNode("URLBase", base_url.url());
194  auto printDeviceB = std::bind(&DLNADevice::printDevice, this);
195  result += xml.printNode("device", printDeviceB);
196  return result;
197  }
198 
199  size_t printDevice() {
200  size_t result = 0;
201  result += xml.printNode("deviceType", getDeviceType());
202  result += xml.printNode("friendlyName", friendly_name);
203  result += xml.printNode("manufacturer", manufacturer);
204  result += xml.printNode("manufacturerURL", manufacturer_url);
205  result += xml.printNode("modelDescription", model_description);
206  result += xml.printNode("modelName", model_name);
207  result += xml.printNode("modelNumber", model_number);
208  result += xml.printNode("modelURL", model_url);
209  result += xml.printNode("serialNumber", serial_number);
210  result += xml.printNode("UDN", getUDN());
211  result += xml.printNode("UPC", universal_product_code);
212  auto printIconListCb = std::bind(&DLNADevice::printIconList, this);
213  result += xml.printNode("iconList", printIconListCb);
214  auto printServiceListCb = std::bind(&DLNADevice::printServiceList, this);
215  result += xml.printNode("serviceList", printServiceListCb);
216  return result;
217  }
218 
219  size_t printSpecVersion() {
220  char major[5], minor[5];
221  sprintf(major, "%d", this->version_major);
222  sprintf(minor, "%d", this->version_minor);
223  return xml.printNode("major", major) + xml.printNode("minor", minor);
224  }
225 
226  size_t printServiceList() {
227  size_t result = 0;
228  for (auto& service : services) {
229  auto printServiceCb =
230  std::bind(&DLNADevice::printService, this, &service);
231  result += xml.printNode("service", printServiceCb);
232  }
233  return result;
234  }
235 
236  size_t printService(void* srv) {
237  size_t result = 0;
238  char buffer[DLNA_MAX_URL_LEN] = {0};
239  StrView url(buffer, DLNA_MAX_URL_LEN);
240  DLNAServiceInfo* service = (DLNAServiceInfo*)srv;
241  result += xml.printNode("serviceType", service->service_type);
242  result += xml.printNode("serviceId", service->service_id);
243  result += xml.printNode("SCPDURL",
244  url.buildPath(base_url.path(), service->scpd_url));
245  result += xml.printNode(
246  "controlURL", url.buildPath(base_url.path(), service->control_url));
247  result += xml.printNode(
248  "eventSubURL", url.buildPath(base_url.path(), service->event_sub_url));
249  return result;
250  }
251 
252  size_t printIconList() {
253  // make sure we have at least the default icon
254  Icon icon;
255  if (icons.empty()) {
256  icons.push_back(icon);
257  }
258  int result = 0;
259 
260  // print all icons
261  for (auto icon : icons) {
262  auto printIconDlnaInfoCb =
263  std::bind(&DLNADevice::printIconDlnaInfo, this, icon);
264  result += xml.printNode("icon", printIconDlnaInfoCb);
265  }
266  return result;
267  }
268 
270  size_t result = 0;
271  if (!StrView(icon.icon_url).isEmpty()) {
272  char buffer[DLNA_MAX_URL_LEN] = {0};
273  StrView url(buffer, DLNA_MAX_URL_LEN);
274  result += xml.printNode("mimetype", "image/png");
275  result += xml.printNode("width", icon.width);
276  result += xml.printNode("height", icon.height);
277  result += xml.printNode("depth", icon.depth);
278  result +=
279  xml.printNode("url", url.buildPath(base_url.path(), icon.icon_url));
280  }
281  return result;
282  }
283 };
284 
285 } // namespace tiny_dlna
#define DLNA_MAX_URL_LEN
Definition: DLNAServiceInfo.h:5
Setup of a Basic DLNA Control Point. The control point.
Definition: DLNAControlPointMgr.h:32
Device Attributes and generation of XML using urn:schemas-upnp-org:device-1-0. We could just return a...
Definition: DLNADevice.h:27
Vector< DLNAServiceInfo > services
Definition: DLNADevice.h:185
const char * getSerialNumber()
Definition: DLNADevice.h:107
const char * model_number
Definition: DLNADevice.h:181
void setManufacturerURL(const char *url)
Definition: DLNADevice.h:98
size_t printService(void *srv)
Definition: DLNADevice.h:236
void setDeviceType(const char *st)
Definition: DLNADevice.h:42
void setSerialNumber(const char *sn)
Definition: DLNADevice.h:106
StringRegistry & getStringRegistry()
Definition: DLNADevice.h:161
void setActive(bool flag)
Definition: DLNADevice.h:159
const char * getModelDescription()
Definition: DLNADevice.h:101
const char * model_description
Definition: DLNADevice.h:178
const char * friendly_name
Definition: DLNADevice.h:175
uint32_t getTimestamp()
Returns the time when this object has been updated.
Definition: DLNADevice.h:157
const char * getModelNumber()
Definition: DLNADevice.h:105
void setModelDescription(const char *descr)
Definition: DLNADevice.h:100
const char * device_type
Definition: DLNADevice.h:174
Vector< DLNAServiceInfo > & getServices()
Definition: DLNADevice.h:125
const char * getModelName()
Definition: DLNADevice.h:103
Icon icon
Definition: DLNADevice.h:184
const char * manufacturer_url
Definition: DLNADevice.h:177
const char * addString(char *string)
Adds a string to the string repository.
Definition: DLNADevice.h:151
void setFriendlyName(const char *name)
Definition: DLNADevice.h:94
const char * universal_product_code
Definition: DLNADevice.h:183
size_t printRoot()
Definition: DLNADevice.h:189
void clear()
Definition: DLNADevice.h:127
bool is_active
Definition: DLNADevice.h:165
const char * model_url
Definition: DLNADevice.h:180
IPAddress localhost
Definition: DLNADevice.h:169
const char * model_name
Definition: DLNADevice.h:179
const char * getManufacturer()
Definition: DLNADevice.h:97
const char * manufacturer
Definition: DLNADevice.h:176
DLNAServiceInfo & getService(const char *id)
Finds a service definition by name.
Definition: DLNADevice.h:115
void setBaseURL(Url url)
Defines the base url.
Definition: DLNADevice.h:53
size_t printIconList()
Definition: DLNADevice.h:252
IPAddress getIPAddress()
Definition: DLNADevice.h:82
size_t printDevice()
Definition: DLNADevice.h:199
Url & getDeviceURL()
This method returns base url/device.xml.
Definition: DLNADevice.h:69
size_t printSpecVersion()
Definition: DLNADevice.h:219
Url base_url
Definition: DLNADevice.h:167
void setManufacturer(const char *man)
Definition: DLNADevice.h:96
void print(Print &out)
renderes the device xml
Definition: DLNADevice.h:34
const char * getManufacturerURL()
Definition: DLNADevice.h:99
const char * getFriendlyName()
Definition: DLNADevice.h:95
void setModelNumber(const char *number)
Definition: DLNADevice.h:104
size_t printServiceList()
Definition: DLNADevice.h:226
void addService(DLNAServiceInfo s)
Adds a service defintion.
Definition: DLNADevice.h:112
Url device_url
Definition: DLNADevice.h:168
const char * serial_number
Definition: DLNADevice.h:182
Url & getBaseURL()
Provides the base url.
Definition: DLNADevice.h:56
void setUniveralProductCode(const char *upc)
Definition: DLNADevice.h:108
void setIPAddress(IPAddress address)
Definition: DLNADevice.h:80
size_t printIconDlnaInfo(Icon &icon)
Definition: DLNADevice.h:269
void setNS(const char *ns)
Definition: DLNADevice.h:92
int version_minor
Definition: DLNADevice.h:171
void setUDN(const char *id)
Define the udn uuid.
Definition: DLNADevice.h:47
const char * ns
Definition: DLNADevice.h:173
const char * getDeviceType()
Definition: DLNADevice.h:44
void clearIcons()
Overwrite the default icon.
Definition: DLNADevice.h:144
Icon getIcon(int idx=0)
Definition: DLNADevice.h:146
const char * getUDN()
Provide the udn uuid.
Definition: DLNADevice.h:50
uint64_t timestamp
Definition: DLNADevice.h:164
const char * getIPStr()
Provides the local address as string.
Definition: DLNADevice.h:85
void setModelName(const char *name)
Definition: DLNADevice.h:102
const char * getNS()
Definition: DLNADevice.h:93
int version_major
Definition: DLNADevice.h:170
const char * udn
Definition: DLNADevice.h:172
void addIcon(Icon icon)
Definition: DLNADevice.h:145
StringRegistry strings
Definition: DLNADevice.h:187
void updateTimestamp()
Update the timestamp.
Definition: DLNADevice.h:154
XMLPrinter xml
Definition: DLNADevice.h:166
const char * getUniveralProductCode()
Definition: DLNADevice.h:109
Vector< Icon > icons
Definition: DLNADevice.h:186
DLNADevice(bool ok=true)
Definition: DLNADevice.h:32
Attributes needed for the DLNA Service Definition.
Definition: DLNAServiceInfo.h:16
const char * scpd_url
Definition: DLNAServiceInfo.h:35
const char * service_type
Definition: DLNAServiceInfo.h:33
const char * service_id
Definition: DLNAServiceInfo.h:34
const char * control_url
Definition: DLNAServiceInfo.h:36
const char * event_sub_url
Definition: DLNAServiceInfo.h:37
Information about the icon.
Definition: Icon.h:10
const char * icon_url
Definition: Icon.h:16
int width
Definition: Icon.h:13
int height
Definition: Icon.h:14
int depth
Definition: Icon.h:15
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 bool isEmpty()
checks if the string is empty
Definition: StrView.h:373
const char * buildPath(const char *start, const char *p1=nullptr, const char *p2=nullptr)
Definition: StrView.h:736
virtual bool endsWith(const char *str)
checks if the string ends with the indicated substring
Definition: StrView.h:185
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
Definition: StringRegistry.h:9
void clear()
Definition: StringRegistry.h:22
const char * add(char *in)
adds a string
Definition: StringRegistry.h:12
URL parser which breaks a full url string up into its individual parts.
Definition: Url.h:18
const char * path()
Definition: Url.h:47
const char * url()
Definition: Url.h:46
const char * host()
Definition: Url.h:48
Vector implementation which provides the most important methods as defined by std::vector....
Definition: Vector.h:21
Parses an DLNA device xml string to fill the DLNADevice data structure.
Definition: XMLDeviceParser.h:17
Definition: Allocator.h:6
Functions to efficiently output XML. XML data contains a lot of redundancy so it is more memory effic...
Definition: XMLPrinter.h:31
size_t printNode(XMLNode node)
Definition: XMLPrinter.h:43
void setOutput(Print &output)
Defines the output.
Definition: XMLPrinter.h:36
size_t printXMLHeader()
Definition: XMLPrinter.h:38