MythTV  master
upnpdevice.h
Go to the documentation of this file.
1 // Program Name: upnpdevice.h
3 // Created : Oct. 24, 2005
4 //
5 // Purpose : UPnp Device Description parser/generator
6 //
7 // Copyright (c) 2005 David Blain <dblain@mythtv.org>
8 //
9 // Licensed under the GPL v2 or later, see LICENSE for details
10 //
12 
13 #ifndef UPNPDEVICE_H
14 #define UPNPDEVICE_H
15 
16 #include <utility>
17 
18 // Qt headers
19 #include <QDomDocument>
20 #include <QUrl>
21 #include <QUrlQuery>
22 
23 // MythTV headers
24 #include "libmythbase/compat.h"
25 #include "libmythbase/mythtypes.h"
27 
28 #include "upnpexp.h"
29 #include "upnputil.h"
30 
31 class UPnpDeviceDesc;
32 class UPnpDevice;
33 class UPnpService;
34 class UPnpIcon;
35 class QTextStream;
36 
38 // Typedefs
40 
41 using UPnpDeviceList = QList< UPnpDevice* >;
42 using UPnpServiceList = QList< UPnpService* >;
43 using UPnpIconList = QList< UPnpIcon* >;
44 
46 //
48 
50 {
51  public:
52  QString m_sURL;
53  QString m_sMimeType;
54  int m_nWidth { 0 };
55  int m_nHeight { 0 };
56  int m_nDepth { 0 };
57 
58  UPnpIcon() = default;
59 
60  QString toString(uint padding) const
61  {
62  QString pad;
63  for (uint i = 0; i < padding; i++)
64  pad += " ";
65  return QString("%0Icon %1 %2x%3^%4 %5")
66  .arg(pad, m_sURL).arg(m_nWidth).arg(m_nHeight)
67  .arg(m_nDepth).arg(m_sMimeType);
68  }
69 };
70 
72 
74 {
75  public:
76  QString m_sServiceType;
77  QString m_sServiceId;
78  QString m_sSCPDURL;
79  QString m_sControlURL;
80  QString m_sEventSubURL;
81 
82  UPnpService() = default;
83 
84  QString toString(uint padding) const
85  {
86  QString pad;
87  for (uint i = 0; i < padding; i++)
88  pad += " ";
89  return
90  QString("%0Service %1\n"
91  "%0 id: %2\n"
92  "%0 SCPD URL: %3\n"
93  "%0 Control URL: %4\n"
94  "%0 Event Sub URL: %5")
95  .arg(pad, m_sEventSubURL, m_sServiceType, m_sServiceId,
96  m_sSCPDURL, m_sControlURL, m_sEventSubURL);
97  }
98 };
99 
101 
103 {
104  public:
105 
106  QString m_sDeviceType;
111  QString m_sModelName;
112  QString m_sModelNumber;
113  QString m_sModelURL;
115  QString m_sUPC;
117  mutable QString m_sUDN;
118 
120 
122  bool m_securityPin { false };
124 
128 
129  public:
130  UPnpDevice();
131  ~UPnpDevice();
132 
133  QString GetUDN(void) const;
134 
135  void toMap(InfoMap &map) const;
136 
137  UPnpService GetService(const QString &urn, bool *found = nullptr) const;
138 
139  QString toString(uint padding = 0) const;
140 };
141 
142 
145 //
146 //
147 //
150 
152 {
153  public:
154 
156  QString m_sHostName;
157  QUrl m_hostUrl;
158 
159  protected:
160 
161  void InternalLoad( QDomNode oNode, UPnpDevice *pCurDevice );
162 
163  static void ProcessIconList ( const QDomNode& oListNode, UPnpDevice *pDevice );
164  static void ProcessServiceList( const QDomNode& oListNode, UPnpDevice *pDevice );
165  void ProcessDeviceList ( const QDomNode& oListNode, UPnpDevice *pDevice );
166 
167  void OutputDevice( QTextStream &os,
168  UPnpDevice *pDevice,
169  const QString &sUserAgent = "" );
170 
171  static void SetStrValue ( const QDomNode &n, QString &sValue );
172  static void SetNumValue ( const QDomNode &n, int &nValue );
173  static void SetBoolValue( const QDomNode &n, bool &nValue );
174 
175  static QString FormatValue ( const NameValue &node );
176  static QString FormatValue ( const QString &sName, const QString &sValue );
177  static QString FormatValue ( const QString &sName, int nValue );
178 
179  QString GetHostName () const;
180 
181  public:
182 
183  UPnpDeviceDesc() = default;
184  virtual ~UPnpDeviceDesc() = default;
185 
186  bool Load ( const QString &sFileName );
187  bool Load ( const QDomDocument &xmlDevDesc );
188 
189  void GetValidXML( const QString &sBaseAddress, int nPort, QTextStream &os, const QString &sUserAgent = "" );
190  QString GetValidXML( const QString &sBaseAddress, int nPort );
191 
192  QString FindDeviceUDN( UPnpDevice *pDevice, QString sST );
193 
194  UPnpDevice *FindDevice( const QString &sURI );
195 
196  static UPnpDevice *FindDevice( UPnpDevice *pDevice, const QString &sURI );
197  static UPnpDeviceDesc *Retrieve ( QString &sURL );
198 
199  void toMap(InfoMap &map) const
200  {
201  map["hostname"] = m_sHostName;
202  m_rootDevice.toMap(map);
203  }
204 };
205 
207 // DeviceLocation Class Definition/Implementation
209 
211 {
212  public:
213 
214  static int g_nAllocated; // Debugging only
215 
216  protected:
217 
218  // ==================================================================
219  // Destructor protected to force use of Release Method
220  // ==================================================================
221 
222  ~DeviceLocation() override
223  {
224  // Should be atomic decrement
225  g_nAllocated--;
226 
227  delete m_pDeviceDesc;
228  }
229 
230  UPnpDeviceDesc *m_pDeviceDesc { nullptr }; // We take ownership of this pointer.
231 
232  public:
233 
234  QString m_sURI; // Service Type URI
235  QString m_sUSN; // Unique Service Name
236  QString m_sLocation; // URL to Device Description
238  QString m_sSecurityPin; // Use for MythXML methods needed pin
239 
240  public:
241 
242  // ==================================================================
243 
244  DeviceLocation( QString sURI,
245  QString sUSN,
246  QString sLocation,
247  TaskTime ttExpires ) : ReferenceCounter(
248  "DeviceLocation" ),
249  m_sURI (std::move( sURI )),
250  m_sUSN (std::move( sUSN )),
251  m_sLocation (std::move( sLocation )),
252  m_ttExpires ( ttExpires )
253  {
254  // Should be atomic increment
255  g_nAllocated++;
256  }
257 
258  // ==================================================================
259 
260  std::chrono::seconds ExpiresInSecs(void) const
261  {
262  auto ttNow = nowAsDuration<std::chrono::microseconds>();
263  return duration_cast<std::chrono::seconds>(m_ttExpires - ttNow);
264  }
265 
266  // ==================================================================
267 
269  {
270  if (m_pDeviceDesc == nullptr)
271  m_pDeviceDesc = UPnpDeviceDesc::Retrieve( m_sLocation );
272 
273  return m_pDeviceDesc;
274  }
275 
276  // ==================================================================
277 
278  QString GetFriendlyName(void)
279  {
280  UPnpDeviceDesc *pDevice = GetDeviceDesc();
281 
282  if ( pDevice == nullptr)
283  return "<Unknown>";
284 
285  QString sName = pDevice->m_rootDevice.m_sFriendlyName;
286 
287  if (sName == "mythtv: MythTV AV Media Server")
288  return sName + " (" + pDevice->m_sHostName + ")";
289 
290  return sName;
291  }
292 
293  QString GetNameAndDetails(void)
294  {
295  UPnpDeviceDesc *pDevice = GetDeviceDesc();
296 
297  if ( pDevice == nullptr)
298  return "<Unknown> (" + m_sLocation + ")";
299 
300  return pDevice->m_rootDevice.m_sFriendlyName
301  + " (" + pDevice->m_sHostName + "), "
302  + pDevice->m_rootDevice.m_sUDN;
303  }
304 
306  {
307  map["location"] = m_sLocation;
308 
309  UPnpDeviceDesc *pDevice = GetDeviceDesc();
310  if (!pDevice)
311  return;
312 
313  pDevice->toMap(map);
314  }
315 
316  bool NeedSecurityPin(void)
317  {
318  UPnpDeviceDesc *pDevice = GetDeviceDesc();
319  if (!pDevice)
320  return false;
321 
322  return pDevice->m_rootDevice.m_securityPin;
323  }
324 
325  QString toString() const
326  {
327  return QString("\nURI:%1\nUSN:%2\nDeviceXML:%3\n"
328  "Expires:%4\nMythTV PIN:%5")
329  .arg(m_sURI, m_sUSN, m_sLocation,
330  QString::number(ExpiresInSecs().count()),
331  m_sSecurityPin);
332  }
333 };
334 
335 #endif // UPNPDEVICE_H
UPnpDevice::m_sModelURL
QString m_sModelURL
Definition: upnpdevice.h:113
NameValue
Definition: upnputil.h:40
DeviceLocation::g_nAllocated
static int g_nAllocated
Definition: upnpdevice.h:214
UPnpDevice::m_sManufacturer
QString m_sManufacturer
Definition: upnpdevice.h:108
MythDate::toString
QString toString(const QDateTime &raw_dt, uint format)
Returns formatted string representing the time.
Definition: mythdate.cpp:84
UPnpDevice::m_listIcons
UPnpIconList m_listIcons
Definition: upnpdevice.h:125
UPnpDeviceDesc::Retrieve
static UPnpDeviceDesc * Retrieve(QString &sURL)
Definition: upnpdevice.cpp:622
TaskTime
std::chrono::microseconds TaskTime
Definition: upnputil.h:31
UPnpIcon::m_sMimeType
QString m_sMimeType
Definition: upnpdevice.h:53
UPnpDevice::m_listServices
UPnpServiceList m_listServices
Definition: upnpdevice.h:126
UPnpDevice::m_sUPC
QString m_sUPC
Definition: upnpdevice.h:115
DeviceLocation::m_sURI
QString m_sURI
Definition: upnpdevice.h:234
UPnpDeviceDesc
Definition: upnpdevice.h:151
UPnpIcon::m_sURL
QString m_sURL
Definition: upnpdevice.h:52
DeviceLocation::GetFriendlyName
QString GetFriendlyName(void)
Definition: upnpdevice.h:278
UPnpDevice::m_sDeviceType
QString m_sDeviceType
Definition: upnpdevice.h:106
UPnpIconList
QList< UPnpIcon * > UPnpIconList
Definition: upnpdevice.h:43
UPnpService::m_sEventSubURL
QString m_sEventSubURL
Definition: upnpdevice.h:80
UPnpServiceList
QList< UPnpService * > UPnpServiceList
Definition: upnpdevice.h:42
DeviceLocation::GetDeviceDesc
UPnpDeviceDesc * GetDeviceDesc(void)
Definition: upnpdevice.h:268
DeviceLocation
Definition: upnpdevice.h:210
UPnpDevice::m_sUDN
QString m_sUDN
Definition: upnpdevice.h:117
DeviceLocation::~DeviceLocation
~DeviceLocation() override
Definition: upnpdevice.h:222
InfoMap
QHash< QString, QString > InfoMap
Definition: mythtypes.h:15
DeviceLocation::NeedSecurityPin
bool NeedSecurityPin(void)
Definition: upnpdevice.h:316
DeviceLocation::m_sSecurityPin
QString m_sSecurityPin
Definition: upnpdevice.h:238
NameValues
Definition: upnputil.h:77
UPnpDeviceDesc::m_rootDevice
UPnpDevice m_rootDevice
Definition: upnpdevice.h:155
UPnpIcon::toString
QString toString(uint padding) const
Definition: upnpdevice.h:60
UPnpService::m_sServiceType
QString m_sServiceType
Definition: upnpdevice.h:76
UPnpDevice::toMap
void toMap(InfoMap &map) const
Definition: upnpdevice.cpp:773
UPnpDeviceDesc::toMap
void toMap(InfoMap &map) const
Definition: upnpdevice.h:199
UPnpDevice::m_sModelName
QString m_sModelName
Definition: upnpdevice.h:111
DeviceLocation::m_sUSN
QString m_sUSN
Definition: upnpdevice.h:235
UPnpDevice::m_securityPin
bool m_securityPin
MythTV specific information.
Definition: upnpdevice.h:122
UPnpDeviceDesc::m_sHostName
QString m_sHostName
Definition: upnpdevice.h:156
UPnpDevice::m_sModelNumber
QString m_sModelNumber
Definition: upnpdevice.h:112
UPnpDevice::m_lstExtra
NameValues m_lstExtra
Definition: upnpdevice.h:119
UPnpIcon
Definition: upnpdevice.h:49
UPnpService::toString
QString toString(uint padding) const
Definition: upnpdevice.h:84
compat.h
UPnpDevice::m_sSerialNumber
QString m_sSerialNumber
Definition: upnpdevice.h:114
UPnpDevice::m_listDevices
UPnpDeviceList m_listDevices
Definition: upnpdevice.h:127
DeviceLocation::m_sLocation
QString m_sLocation
Definition: upnpdevice.h:236
mythtypes.h
DeviceLocation::DeviceLocation
DeviceLocation(QString sURI, QString sUSN, QString sLocation, TaskTime ttExpires)
Definition: upnpdevice.h:244
UPnpDevice
Definition: upnpdevice.h:102
UPnpDevice::m_sFriendlyName
QString m_sFriendlyName
Definition: upnpdevice.h:107
UPnpDeviceDesc::m_hostUrl
QUrl m_hostUrl
Definition: upnpdevice.h:157
sLocation
static const QString sLocation
Definition: mythcontext.cpp:59
DeviceLocation::ExpiresInSecs
std::chrono::seconds ExpiresInSecs(void) const
Definition: upnpdevice.h:260
UPnpDeviceList
QList< UPnpDevice * > UPnpDeviceList
Definition: upnpdevice.h:41
uint
unsigned int uint
Definition: compat.h:79
UPnpService::m_sControlURL
QString m_sControlURL
Definition: upnpdevice.h:79
referencecounter.h
UPnpService::m_sServiceId
QString m_sServiceId
Definition: upnpdevice.h:77
UPnpService
Definition: upnpdevice.h:73
std
Definition: mythchrono.h:23
UPnpDevice::m_sModelDescription
QString m_sModelDescription
Definition: upnpdevice.h:110
UPNP_PUBLIC
#define UPNP_PUBLIC
Definition: upnpexp.h:9
UPnpDevice::m_sManufacturerURL
QString m_sManufacturerURL
Definition: upnpdevice.h:109
UPnpDevice::m_protocolVersion
QString m_protocolVersion
Definition: upnpdevice.h:123
UPnpDevice::m_sPresentationURL
QString m_sPresentationURL
Definition: upnpdevice.h:116
DeviceLocation::m_ttExpires
TaskTime m_ttExpires
Definition: upnpdevice.h:237
DeviceLocation::GetNameAndDetails
QString GetNameAndDetails(void)
Definition: upnpdevice.h:293
upnputil.h
DeviceLocation::toString
QString toString() const
Definition: upnpdevice.h:325
UPnpService::m_sSCPDURL
QString m_sSCPDURL
Definition: upnpdevice.h:78
ReferenceCounter
General purpose reference counter.
Definition: referencecounter.h:26
upnpexp.h
DeviceLocation::GetDeviceDetail
void GetDeviceDetail(InfoMap &map)
Definition: upnpdevice.h:305