Arduino DLNA Server
XMLDeviceParser.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Print.h"
4 #include "assert.h"
5 #include "basic/Icon.h"
6 #include "basic/StrView.h"
7 #include "dlna/DLNADevice.h"
8 
9 namespace tiny_dlna {
10 
18  public:
19  void parse(DLNADevice& result, const char* xmlStr) {
20  StrView tmp(xmlStr);
21  str.swap(tmp);
22  p_device = &result;
23  parseVersion(result);
24  parseIcons(result);
25  parseDevice(result);
26  }
27 
28  protected:
30  DLNADevice* p_device = nullptr;
31 
33  const char* substring(char* in, int pos, int end) {
34  const int len = end - pos + 1;
35  char tmp[len];
36  StrView tmp_str(tmp, len);
37  tmp_str.substring(in, pos, end);
38  return p_device->addString((char*)tmp_str.c_str());
39  }
40 
41  void parseVersion(DLNADevice& result) {
42  parseInt(0, "<major>", result.version_major);
43  parseInt(0, "<minor>", result.version_minor);
44  }
45 
46  void parseDevice(DLNADevice& result) {
47  parseStr("<deviceType>", result.device_type);
48  parseStr("<friendlyName>", result.friendly_name);
49  parseStr("<manufacturer>", result.manufacturer);
50  parseStr("<manufacturerURL>", result.manufacturer_url);
51  parseStr("<modelDescription>", result.model_description);
52  parseStr("<modelName>", result.model_name);
53  parseStr("<modelNumber>", result.model_number);
54  parseStr("<modelURL>", result.model_url);
55  parseStr("<serialNumber>", result.serial_number);
56  parseServices();
57  };
58 
59  int parseStr(const char* name, const char*& result) {
60  return parseStr(0, name, result);
61  }
62 
63  int parseInt(int pos, const char* name, int& result) {
64  char temp[50] = {0};
65  StrView temp_view(temp, 50);
66  int start_pos = str.indexOf(name, pos);
67  int end_pos = -1;
68  if (start_pos > 0) {
69  StrView tag(name);
70  start_pos += tag.length();
71  end_pos = str.indexOf("</", start_pos);
72  temp_view.substring((char*)str.c_str(), start_pos, end_pos);
73  result = temp_view.toInt();
74  DlnaLogger.log(DlnaDebug, "device xml %s : %s / %d", name, temp_view.c_str(), result);
75 
76  end_pos = str.indexOf(">", end_pos);
77  }
78  return end_pos;
79  }
80 
81  int parseStr(int pos, const char* name, const char*& result) {
82  int start_pos = str.indexOf(name, pos);
83  int end = -1;
84  if (start_pos > 0) {
85  StrView tag(name);
86  start_pos += tag.length();
87  int end_str = str.indexOf("</", start_pos);
88  result = substring((char*)str.c_str(), start_pos, end_str);
89  DlnaLogger.log(DlnaDebug, "device xml %s : %s", name, result);
90  end = str.indexOf(">", end_str);
91  } else {
92  result = nullptr;
93  }
94  return end;
95  }
96 
97  void parseIcons(DLNADevice& device) {
98  int pos = 0;
99  do {
100  pos = parseIcon(device, pos);
101  } while (pos > 0);
102  }
103 
104  int parseIcon(DLNADevice& device, int pos) {
105  Icon icon;
106  int result = -1;
107  int iconPos = str.indexOf("<icon>", pos);
108  if (iconPos > 0) {
109  parseStr(iconPos, "<mimetype>", icon.mime);
110  parseInt(iconPos, "<width>", icon.width);
111  parseInt(iconPos, "<height>", icon.height);
112  parseInt(iconPos, "<depth>", icon.depth);
113  parseStr(iconPos, "<url>", icon.icon_url);
114  device.addIcon(icon);
115  result = str.indexOf("</icon>", pos) + 7;
116  }
117  return result;
118  }
119 
120  void parseServices() {
121  int pos = 0;
122  do {
123  pos = parseService(pos);
124  } while (pos > 0);
125  }
126 
127  int parseService(int pos) {
128  DLNAServiceInfo service;
129  int result = -1;
130  int servicePos = str.indexOf("<service>", pos);
131  if (servicePos > 0) {
132  parseStr(servicePos, "<serviceType>", service.service_type);
133  parseStr(servicePos, "<serviceId>", service.service_id);
134  parseStr(servicePos, "<SCPDURL>", service.scpd_url);
135  parseStr(servicePos, "<controlURL>", service.control_url);
136  parseStr(servicePos, "<eventSubURL>", service.event_sub_url);
137  p_device->addService(service);
138  result = str.indexOf("</service>", pos) + 10;
139  }
140  return result;
141  }
142 };
143 
144 } // namespace tiny_dlna
Device Attributes and generation of XML using urn:schemas-upnp-org:device-1-0. We could just return a...
Definition: DLNADevice.h:27
const char * model_number
Definition: DLNADevice.h:181
const char * model_description
Definition: DLNADevice.h:178
const char * friendly_name
Definition: DLNADevice.h:175
const char * device_type
Definition: DLNADevice.h:174
const char * manufacturer_url
Definition: DLNADevice.h:177
const char * addString(char *string)
Adds a string to the string repository.
Definition: DLNADevice.h:151
const char * model_url
Definition: DLNADevice.h:180
const char * model_name
Definition: DLNADevice.h:179
const char * manufacturer
Definition: DLNADevice.h:176
void addService(DLNAServiceInfo s)
Adds a service defintion.
Definition: DLNADevice.h:112
const char * serial_number
Definition: DLNADevice.h:182
int version_minor
Definition: DLNADevice.h:171
int version_major
Definition: DLNADevice.h:170
void addIcon(Icon icon)
Definition: DLNADevice.h:145
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
const char * mime
Definition: Icon.h:12
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:25
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 int length()
Definition: StrView.h:370
virtual const char * c_str()
provides the string value as const char*
Definition: StrView.h:366
virtual void swap(StrView &str)
Definition: StrView.h:100
virtual void substring(StrView &from, int start, int end)
copies a substring into the current string
Definition: StrView.h:483
Parses an DLNA device xml string to fill the DLNADevice data structure.
Definition: XMLDeviceParser.h:17
const char * substring(char *in, int pos, int end)
extract string, add to string repository and return repository string
Definition: XMLDeviceParser.h:33
int parseInt(int pos, const char *name, int &result)
Definition: XMLDeviceParser.h:63
DLNADevice * p_device
Definition: XMLDeviceParser.h:30
void parse(DLNADevice &result, const char *xmlStr)
Definition: XMLDeviceParser.h:19
void parseServices()
Definition: XMLDeviceParser.h:120
int parseIcon(DLNADevice &device, int pos)
Definition: XMLDeviceParser.h:104
StrView str
Definition: XMLDeviceParser.h:29
int parseStr(int pos, const char *name, const char *&result)
Definition: XMLDeviceParser.h:81
int parseStr(const char *name, const char *&result)
Definition: XMLDeviceParser.h:59
void parseDevice(DLNADevice &result)
Definition: XMLDeviceParser.h:46
int parseService(int pos)
Definition: XMLDeviceParser.h:127
void parseVersion(DLNADevice &result)
Definition: XMLDeviceParser.h:41
void parseIcons(DLNADevice &device)
Definition: XMLDeviceParser.h:97
Definition: Allocator.h:6
@ DlnaDebug
Definition: Logger.h:16
LoggerClass DlnaLogger
Definition: Logger.cpp:5