Arduino DLNA Server
Loading...
Searching...
No Matches
DLNADeviceInfo.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
14namespace tiny_dlna {
15
28 friend class XMLDeviceParser;
29 friend class DLNAControlPointMgr;
30 friend class DLNADevice;
31
32 public:
33 DLNADeviceInfo(bool ok = true) { is_active = ok; }
35
37 virtual bool begin() { return true; }
38
40 void print(Print& out) {
41 xml.setOutput(out);
43 auto printRootCb = std::bind(&DLNADeviceInfo::printRoot, this);
44 xml.printNode("root", printRootCb, ns);
45 }
46
47 // sets the device type (ST or NT)
48 void setDeviceType(const char* st) { device_type = st; }
49
50 const char* getDeviceType() { return device_type; }
51
53 void setUDN(const char* id) { udn = id; }
54
56 const char* getUDN() { return udn; }
57
59 void setBaseURL(const char* url) { base_url = url; }
61 void setBaseURL(IPAddress ip, int port, const char* path="") {
62 localhost = ip;
63 static Str str = "http://";
64 str += ip[0];
65 str += ".";
66 str += ip[1];
67 str += ".";
68 str += ip[2];
69 str += ".";
70 str += ip[3];
71 str += ":";
72 str += port;
73 if (!StrView(path).startsWith("/")){
74 str += "/";
75 }
76 str += path;
77 setBaseURL(str.c_str());
78 }
79
81 const char* getBaseURL() {
82 // replace localhost url
83 if (StrView(base_url).contains("localhost")) {
85 url_str.replace("localhost", getIPStr());
87 }
88 return base_url;
89 }
90
93 if (!device_url) {
94 Str str = getBaseURL();
95 if (!str.endsWith("/")) str += "/";
96 str += "device.xml";
97 Url new_url(str.c_str());
98 device_url = new_url;
99 }
100 return device_url;
101 }
102
104 void setIPAddress(IPAddress address) { localhost = address; }
105
107 IPAddress getIPAddress() { return localhost; }
108
110 const char* getIPStr() {
111 static char result[80] = {0};
112 snprintf(result, 80, "%d.%d.%d.%d", localhost[0], localhost[1],
113 localhost[2], localhost[3]);
114 return result;
115 }
116
117 void setNS(const char* ns) { this->ns = ns; }
118 const char* getNS() { return ns; }
119 void setFriendlyName(const char* name) { friendly_name = name; }
120 const char* getFriendlyName() { return friendly_name; }
121 void setManufacturer(const char* man) { manufacturer = man; }
122 const char* getManufacturer() { return manufacturer; }
123 void setManufacturerURL(const char* url) { manufacturer_url = url; }
124 const char* getManufacturerURL() { return manufacturer_url; }
125 void setModelDescription(const char* descr) { model_description = descr; }
126 const char* getModelDescription() { return model_description; }
127 void setModelName(const char* name) { model_name = name; }
128 const char* getModelName() { return model_name; }
129 void setModelNumber(const char* number) { model_number = number; }
130 const char* getModelNumber() { return model_number; }
131 void setSerialNumber(const char* sn) { serial_number = sn; }
132 const char* getSerialNumber() { return serial_number; }
133 void setUniveralProductCode(const char* upc) { universal_product_code = upc; }
135
137 void addService(DLNAServiceInfo s) { services.push_back(s); }
138
140 DLNAServiceInfo& getService(const char* id) {
141 static DLNAServiceInfo result{false};
142 for (auto& service : services) {
143 if (StrView(service.service_id).contains(id)) {
144 return service;
145 }
146 }
147 return result;
148 }
149
151
152 void clear() {
153 services.clear();
154 udn = nullptr;
155 ns = nullptr;
156 device_type = nullptr;
157 friendly_name = nullptr;
158 manufacturer = nullptr;
159 manufacturer_url = nullptr;
160 model_description = nullptr;
161 model_name = nullptr;
162 model_number = nullptr;
163 serial_number = nullptr;
164 universal_product_code = nullptr;
165 }
166
168 void clearIcons() { icons.clear(); }
169 void addIcon(Icon icon) { icons.push_back(icon); }
170 Icon getIcon(int idx = 0) {
171 if (icons.size()==0){
172 Icon empty;
173 return empty;
174 }
175 return icons[idx]; }
176
177 operator bool() { return is_active; }
178
180 void updateTimestamp() { timestamp = millis(); }
181
183 uint32_t getTimestamp() { return timestamp; }
184
185 void setActive(bool flag) { is_active = flag; }
186
187 virtual void loop(){
188 delay(1);
189 }
190
191 protected:
192 uint64_t timestamp = 0;
193 bool is_active = true;
196 IPAddress localhost;
199 const char* base_url = "http://localhost:9876/dlna";
200 const char* udn = "uuid:09349455-2941-4cf7-9847-0dd5ab210e97";
201 const char* ns = "xmlns=\"urn:schemas-upnp-org:device-1-0\"";
202 const char* device_type = nullptr;
203 const char* friendly_name = nullptr;
204 const char* manufacturer = nullptr;
205 const char* manufacturer_url = nullptr;
206 const char* model_description = nullptr;
207 const char* model_name = nullptr;
208 const char* model_url = nullptr;
209 const char* model_number = nullptr;
210 const char* serial_number = nullptr;
211 const char* universal_product_code = nullptr;
216
218 virtual void setupServices(HttpServer& server, IUDPService& udp){}
219
220 size_t printRoot() {
221 size_t result = 0;
222 auto printSpecVersionB = std::bind(&DLNADeviceInfo::printSpecVersion, this);
223 result += xml.printNode("specVersion", printSpecVersionB);
224 result += xml.printNode("URLBase", base_url);
225 auto printDeviceB = std::bind(&DLNADeviceInfo::printDevice, this);
226 result += xml.printNode("device", printDeviceB);
227 return result;
228 }
229
230 size_t printDevice() {
231 size_t result = 0;
232 result += xml.printNode("deviceType", getDeviceType());
233 result += xml.printNode("friendlyName", friendly_name);
234 result += xml.printNode("manufacturer", manufacturer);
235 result += xml.printNode("manufacturerURL", manufacturer_url);
236 result += xml.printNode("modelDescription", model_description);
237 result += xml.printNode("modelName", model_name);
238 result += xml.printNode("modelNumber", model_number);
239 result += xml.printNode("modelURL", model_url);
240 result += xml.printNode("serialNumber", serial_number);
241 result += xml.printNode("UDN", getUDN());
242 result += xml.printNode("UPC", universal_product_code);
243 auto printIconListCb = std::bind(&DLNADeviceInfo::printIconList, this);
244 result += xml.printNode("iconList", printIconListCb);
245 auto printServiceListCb = std::bind(&DLNADeviceInfo::printServiceList, this);
246 result += xml.printNode("serviceList", printServiceListCb);
247 return result;
248 }
249
251 char major[5], minor[5];
252 sprintf(major, "%d", this->version_major);
253 sprintf(minor, "%d", this->version_minor);
254 return xml.printNode("major", major) + xml.printNode("minor", minor);
255 }
256
258 size_t result = 0;
259 for (auto& service : services) {
260 auto printServiceCb =
261 std::bind(&DLNADeviceInfo::printService, this, &service);
262 result += xml.printNode("service", printServiceCb);
263 }
264 return result;
265 }
266
267 size_t printService(void* srv) {
268 size_t result = 0;
269 char buffer[DLNA_MAX_URL_LEN] = {0};
270 StrView url(buffer, DLNA_MAX_URL_LEN);
271 DLNAServiceInfo* service = (DLNAServiceInfo*)srv;
272 result += xml.printNode("serviceType", service->service_type);
273 result += xml.printNode("serviceId", service->service_id);
274 result += xml.printNode("SCPDURL",
275 url.buildPath(base_url, service->scpd_url));
276 result += xml.printNode(
277 "controlURL", url.buildPath(base_url, service->control_url));
278 result += xml.printNode(
279 "eventSubURL", url.buildPath(base_url, service->event_sub_url));
280 return result;
281 }
282
283 size_t printIconList() {
284 // make sure we have at least the default icon
285 Icon icon;
286 if (icons.empty()) {
287 icons.push_back(icon);
288 }
289 int result = 0;
290
291 // print all icons
292 for (auto icon : icons) {
293 auto printIconDlnaInfoCb =
294 std::bind(&DLNADeviceInfo::printIconDlnaInfo, this, icon);
295 result += xml.printNode("icon", printIconDlnaInfoCb);
296 }
297 return result;
298 }
299
301 size_t result = 0;
302 if (!StrView(icon.icon_url).isEmpty()) {
303 char buffer[DLNA_MAX_URL_LEN] = {0};
304 StrView url(buffer, DLNA_MAX_URL_LEN);
305 result += xml.printNode("mimetype", "image/png");
306 result += xml.printNode("width", icon.width);
307 result += xml.printNode("height", icon.height);
308 result += xml.printNode("depth", icon.depth);
309 result +=
311 }
312 return result;
313 }
314};
315
316} // namespace tiny_dlna
#define DLNA_MAX_URL_LEN
Definition: DLNAServiceInfo.h:5
Lightweight DLNA control point manager.
Definition: DLNAControlPointMgr.h:58
Device Attributes and generation of XML using urn:schemas-upnp-org:device-1-0. We could just return a...
Definition: DLNADeviceInfo.h:27
Vector< DLNAServiceInfo > & getServices()
Definition: DLNADeviceInfo.h:150
Vector< DLNAServiceInfo > services
Definition: DLNADeviceInfo.h:213
Url device_url
Definition: DLNADeviceInfo.h:195
void setUniveralProductCode(const char *upc)
Definition: DLNADeviceInfo.h:133
void setModelDescription(const char *descr)
Definition: DLNADeviceInfo.h:125
virtual void loop()
Definition: DLNADeviceInfo.h:187
uint32_t getTimestamp()
Returns the time when this object has been updated.
Definition: DLNADeviceInfo.h:183
const char * friendly_name
Definition: DLNADeviceInfo.h:203
int version_minor
Definition: DLNADeviceInfo.h:198
const char * universal_product_code
Definition: DLNADeviceInfo.h:211
IPAddress localhost
Definition: DLNADeviceInfo.h:196
~DLNADeviceInfo()
Definition: DLNADeviceInfo.h:34
void setDeviceType(const char *st)
Definition: DLNADeviceInfo.h:48
const char * getUniveralProductCode()
Definition: DLNADeviceInfo.h:134
Icon icon
Definition: DLNADeviceInfo.h:212
const char * getFriendlyName()
Definition: DLNADeviceInfo.h:120
size_t printSpecVersion()
Definition: DLNADeviceInfo.h:250
const char * manufacturer_url
Definition: DLNADeviceInfo.h:205
void print(Print &out)
renderes the device xml
Definition: DLNADeviceInfo.h:40
void updateTimestamp()
Update the timestamp.
Definition: DLNADeviceInfo.h:180
void addService(DLNAServiceInfo s)
Adds a service defintion.
Definition: DLNADeviceInfo.h:137
size_t printRoot()
Definition: DLNADeviceInfo.h:220
const char * serial_number
Definition: DLNADeviceInfo.h:210
Icon getIcon(int idx=0)
Definition: DLNADeviceInfo.h:170
void setNS(const char *ns)
Definition: DLNADeviceInfo.h:117
const char * getManufacturer()
Definition: DLNADeviceInfo.h:122
void addIcon(Icon icon)
Definition: DLNADeviceInfo.h:169
const char * model_url
Definition: DLNADeviceInfo.h:208
void setManufacturerURL(const char *url)
Definition: DLNADeviceInfo.h:123
DLNAServiceInfo & getService(const char *id)
Finds a service definition by name.
Definition: DLNADeviceInfo.h:140
size_t printService(void *srv)
Definition: DLNADeviceInfo.h:267
size_t printServiceList()
Definition: DLNADeviceInfo.h:257
Str url_str
Definition: DLNADeviceInfo.h:215
void clear()
Definition: DLNADeviceInfo.h:152
const char * model_name
Definition: DLNADeviceInfo.h:207
const char * model_description
Definition: DLNADeviceInfo.h:206
void clearIcons()
Overwrite the default icon.
Definition: DLNADeviceInfo.h:168
virtual void setupServices(HttpServer &server, IUDPService &udp)
to be implemented by subclasses
Definition: DLNADeviceInfo.h:218
const char * getUDN()
Provide the udn uuid.
Definition: DLNADeviceInfo.h:56
void setModelName(const char *name)
Definition: DLNADeviceInfo.h:127
const char * model_number
Definition: DLNADeviceInfo.h:209
const char * ns
Definition: DLNADeviceInfo.h:201
const char * getNS()
Definition: DLNADeviceInfo.h:118
void setSerialNumber(const char *sn)
Definition: DLNADeviceInfo.h:131
const char * getBaseURL()
Provides the base url.
Definition: DLNADeviceInfo.h:81
virtual bool begin()
Override to initialize the device.
Definition: DLNADeviceInfo.h:37
const char * getModelDescription()
Definition: DLNADeviceInfo.h:126
Url & getDeviceURL()
This method returns base url/device.xml.
Definition: DLNADeviceInfo.h:92
void setManufacturer(const char *man)
Definition: DLNADeviceInfo.h:121
void setUDN(const char *id)
Define the udn uuid.
Definition: DLNADeviceInfo.h:53
void setFriendlyName(const char *name)
Definition: DLNADeviceInfo.h:119
IPAddress getIPAddress()
Provides the local IP address.
Definition: DLNADeviceInfo.h:107
const char * base_url
Definition: DLNADeviceInfo.h:199
size_t printDevice()
Definition: DLNADeviceInfo.h:230
XMLPrinter xml
Definition: DLNADeviceInfo.h:194
void setActive(bool flag)
Definition: DLNADeviceInfo.h:185
const char * manufacturer
Definition: DLNADeviceInfo.h:204
const char * udn
Definition: DLNADeviceInfo.h:200
DLNADeviceInfo(bool ok=true)
Definition: DLNADeviceInfo.h:33
const char * getModelNumber()
Definition: DLNADeviceInfo.h:130
size_t printIconList()
Definition: DLNADeviceInfo.h:283
void setIPAddress(IPAddress address)
Defines the local IP address.
Definition: DLNADeviceInfo.h:104
const char * getSerialNumber()
Definition: DLNADeviceInfo.h:132
void setBaseURL(IPAddress ip, int port, const char *path="")
Defines the base URL.
Definition: DLNADeviceInfo.h:61
const char * getModelName()
Definition: DLNADeviceInfo.h:128
Vector< Icon > icons
Definition: DLNADeviceInfo.h:214
const char * getManufacturerURL()
Definition: DLNADeviceInfo.h:124
bool is_active
Definition: DLNADeviceInfo.h:193
size_t printIconDlnaInfo(Icon &icon)
Definition: DLNADeviceInfo.h:300
void setModelNumber(const char *number)
Definition: DLNADeviceInfo.h:129
void setBaseURL(const char *url)
Defines the base url.
Definition: DLNADeviceInfo.h:59
uint64_t timestamp
Definition: DLNADeviceInfo.h:192
const char * getDeviceType()
Definition: DLNADeviceInfo.h:50
int version_major
Definition: DLNADeviceInfo.h:197
const char * device_type
Definition: DLNADeviceInfo.h:202
const char * getIPStr()
Provides the local address as string.
Definition: DLNADeviceInfo.h:110
Setup of a Basic DLNA Device service. The device registers itself to the network and answers to the D...
Definition: DLNADevice.h:25
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
A Simple Header only implementation of Http Server that allows the registration of callback functions...
Definition: HttpServer.h:24
Abstract Interface for UDP API.
Definition: IUDPService.h:34
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
void log(DlnaLogLevel current_level, const char *fmt...)
Print log message.
Definition: Logger.h:40
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 bool isEmpty()
checks if the string is empty
Definition: StrView.h:383
const char * buildPath(const char *start, const char *p1=nullptr, const char *p2=nullptr)
Definition: StrView.h:746
virtual bool endsWith(const char *str)
checks if the string ends with the indicated substring
Definition: StrView.h:192
virtual bool replace(const char *toReplace, const int replaced)
Replaces the first instance of toReplace with replaced.
Definition: StrView.h:395
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
URL parser which breaks a full url string up into its individual parts.
Definition: Url.h:18
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
LoggerClass DlnaLogger
Definition: Logger.cpp:5
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