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 COPYING 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 "compat.h"
25 #include "upnpexp.h"
26 #include "upnputil.h"
27 #include "mythtypes.h"
28 #include "referencecounter.h"
29 
30 class UPnpDeviceDesc;
31 class UPnpDevice;
32 class UPnpService;
33 class UPnpIcon;
34 class QTextStream;
35 
37 // Typedefs
39 
40 using UPnpDeviceList = QList< UPnpDevice* >;
41 using UPnpServiceList = QList< UPnpService* >;
42 using UPnpIconList = QList< UPnpIcon* >;
43 
45 //
47 
49 {
50  public:
51  QString m_sURL;
52  QString m_sMimeType;
53  int m_nWidth { 0 };
54  int m_nHeight { 0 };
55  int m_nDepth { 0 };
56 
57  UPnpIcon() = default;
58 
59  QString toString(uint padding) const
60  {
61  QString pad;
62  for (uint i = 0; i < padding; i++)
63  pad += " ";
64  return QString("%0Icon %1 %2x%3^%4 %5")
65  .arg(pad, m_sURL).arg(m_nWidth).arg(m_nHeight)
66  .arg(m_nDepth).arg(m_sMimeType);
67  }
68 };
69 
71 
73 {
74  public:
75  QString m_sServiceType;
76  QString m_sServiceId;
77  QString m_sSCPDURL;
78  QString m_sControlURL;
79  QString m_sEventSubURL;
80 
81  UPnpService() = default;
82 
83  QString toString(uint padding) const
84  {
85  QString pad;
86  for (uint i = 0; i < padding; i++)
87  pad += " ";
88  return
89  QString("%0Service %1\n"
90  "%0 id: %2\n"
91  "%0 SCPD URL: %3\n"
92  "%0 Control URL: %4\n"
93  "%0 Event Sub URL: %5")
94  .arg(pad, m_sEventSubURL, m_sServiceType, m_sServiceId,
95  m_sSCPDURL, m_sControlURL, m_sEventSubURL);
96  }
97 };
98 
100 
102 {
103  public:
104 
105  QString m_sDeviceType;
110  QString m_sModelName;
111  QString m_sModelNumber;
112  QString m_sModelURL;
114  QString m_sUPC;
116  mutable QString m_sUDN;
117 
119 
121  bool m_securityPin { false };
123 
127 
128  public:
129  UPnpDevice();
130  ~UPnpDevice();
131 
132  QString GetUDN(void) const;
133 
134  void toMap(InfoMap &map) const;
135 
136  UPnpService GetService(const QString &urn, bool *found = nullptr) const;
137 
138  QString toString(uint padding = 0) const;
139 };
140 
141 
144 //
145 //
146 //
149 
151 {
152  public:
153 
155  QString m_sHostName;
156  QUrl m_hostUrl;
157 
158  protected:
159 
160  void InternalLoad( QDomNode oNode, UPnpDevice *pCurDevice );
161 
162  static void ProcessIconList ( const QDomNode& oListNode, UPnpDevice *pDevice );
163  static void ProcessServiceList( const QDomNode& oListNode, UPnpDevice *pDevice );
164  void ProcessDeviceList ( const QDomNode& oListNode, UPnpDevice *pDevice );
165 
166  void OutputDevice( QTextStream &os,
167  UPnpDevice *pDevice,
168  const QString &sUserAgent = "" );
169 
170  static void SetStrValue ( const QDomNode &n, QString &sValue );
171  static void SetNumValue ( const QDomNode &n, int &nValue );
172  static void SetBoolValue( const QDomNode &n, bool &nValue );
173 
174  static QString FormatValue ( const NameValue &node );
175  static QString FormatValue ( const QString &sName, const QString &sValue );
176  static QString FormatValue ( const QString &sName, int nValue );
177 
178  QString GetHostName () const;
179 
180  public:
181 
182  UPnpDeviceDesc() = default;
183  virtual ~UPnpDeviceDesc() = default;
184 
185  bool Load ( const QString &sFileName );
186  bool Load ( const QDomDocument &xmlDevDesc );
187 
188  void GetValidXML( const QString &sBaseAddress, int nPort, QTextStream &os, const QString &sUserAgent = "" );
189  QString GetValidXML( const QString &sBaseAddress, int nPort );
190 
191  QString FindDeviceUDN( UPnpDevice *pDevice, QString sST );
192 
193  UPnpDevice *FindDevice( const QString &sURI );
194 
195  static UPnpDevice *FindDevice( UPnpDevice *pDevice, const QString &sURI );
196  static UPnpDeviceDesc *Retrieve ( QString &sURL );
197 
198  void toMap(InfoMap &map) const
199  {
200  map["hostname"] = m_sHostName;
201  m_rootDevice.toMap(map);
202  }
203 };
204 
206 // DeviceLocation Class Definition/Implementation
208 
210 {
211  public:
212 
213  static int g_nAllocated; // Debugging only
214 
215  protected:
216 
217  // ==================================================================
218  // Destructor protected to force use of Release Method
219  // ==================================================================
220 
221  ~DeviceLocation() override
222  {
223  // Should be atomic decrement
224  g_nAllocated--;
225 
226  delete m_pDeviceDesc;
227  }
228 
229  UPnpDeviceDesc *m_pDeviceDesc; // We take ownership of this pointer.
230 
231  public:
232 
233  QString m_sURI; // Service Type URI
234  QString m_sUSN; // Unique Service Name
235  QString m_sLocation; // URL to Device Description
237  QString m_sSecurityPin; // Use for MythXML methods needed pin
238 
239  public:
240 
241  // ==================================================================
242 
243  DeviceLocation( QString sURI,
244  QString sUSN,
245  QString sLocation,
246  TaskTime ttExpires ) : ReferenceCounter(
247  "DeviceLocation" ),
248  m_pDeviceDesc( nullptr ),
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:112
NameValue
Definition: upnputil.h:52
DeviceLocation::g_nAllocated
static int g_nAllocated
Definition: upnpdevice.h:213
UPnpDevice::m_sManufacturer
QString m_sManufacturer
Definition: upnpdevice.h:107
UPnpDevice::m_listIcons
UPnpIconList m_listIcons
Definition: upnpdevice.h:124
UPnpDeviceDesc::Retrieve
static UPnpDeviceDesc * Retrieve(QString &sURL)
Definition: upnpdevice.cpp:622
TaskTime
std::chrono::microseconds TaskTime
Definition: upnputil.h:43
UPnpIcon::m_sMimeType
QString m_sMimeType
Definition: upnpdevice.h:52
UPnpDevice::m_listServices
UPnpServiceList m_listServices
Definition: upnpdevice.h:125
UPnpDevice::m_sUPC
QString m_sUPC
Definition: upnpdevice.h:114
DeviceLocation::m_sURI
QString m_sURI
Definition: upnpdevice.h:233
UPnpDeviceDesc
Definition: upnpdevice.h:150
UPnpIcon::m_sURL
QString m_sURL
Definition: upnpdevice.h:51
DeviceLocation::GetFriendlyName
QString GetFriendlyName(void)
Definition: upnpdevice.h:278
UPnpDevice::m_sDeviceType
QString m_sDeviceType
Definition: upnpdevice.h:105
UPnpIconList
QList< UPnpIcon * > UPnpIconList
Definition: upnpdevice.h:42
UPnpService::m_sEventSubURL
QString m_sEventSubURL
Definition: upnpdevice.h:79
UPnpServiceList
QList< UPnpService * > UPnpServiceList
Definition: upnpdevice.h:41
DeviceLocation::GetDeviceDesc
UPnpDeviceDesc * GetDeviceDesc(void)
Definition: upnpdevice.h:268
DeviceLocation
Definition: upnpdevice.h:209
UPnpDevice::m_sUDN
QString m_sUDN
Definition: upnpdevice.h:116
DeviceLocation::~DeviceLocation
~DeviceLocation() override
Definition: upnpdevice.h:221
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:237
NameValues
Definition: upnputil.h:89
toString
QString toString(MarkTypes type)
Definition: programtypes.cpp:26
UPnpDeviceDesc::m_rootDevice
UPnpDevice m_rootDevice
Definition: upnpdevice.h:154
UPnpIcon::toString
QString toString(uint padding) const
Definition: upnpdevice.h:59
UPnpService::m_sServiceType
QString m_sServiceType
Definition: upnpdevice.h:75
UPnpDevice::toMap
void toMap(InfoMap &map) const
Definition: upnpdevice.cpp:774
UPnpDeviceDesc::toMap
void toMap(InfoMap &map) const
Definition: upnpdevice.h:198
UPnpDevice::m_sModelName
QString m_sModelName
Definition: upnpdevice.h:110
DeviceLocation::m_sUSN
QString m_sUSN
Definition: upnpdevice.h:234
UPnpDevice::m_securityPin
bool m_securityPin
MythTV specific information.
Definition: upnpdevice.h:121
UPnpDeviceDesc::m_sHostName
QString m_sHostName
Definition: upnpdevice.h:155
UPnpDevice::m_sModelNumber
QString m_sModelNumber
Definition: upnpdevice.h:111
UPnpDevice::m_lstExtra
NameValues m_lstExtra
Definition: upnpdevice.h:118
UPnpIcon
Definition: upnpdevice.h:48
UPnpService::toString
QString toString(uint padding) const
Definition: upnpdevice.h:83
compat.h
UPnpDevice::m_sSerialNumber
QString m_sSerialNumber
Definition: upnpdevice.h:113
UPnpDevice::m_listDevices
UPnpDeviceList m_listDevices
Definition: upnpdevice.h:126
DeviceLocation::m_sLocation
QString m_sLocation
Definition: upnpdevice.h:235
mythtypes.h
DeviceLocation::DeviceLocation
DeviceLocation(QString sURI, QString sUSN, QString sLocation, TaskTime ttExpires)
Definition: upnpdevice.h:243
UPnpDevice
Definition: upnpdevice.h:101
UPnpDevice::m_sFriendlyName
QString m_sFriendlyName
Definition: upnpdevice.h:106
UPnpDeviceDesc::m_hostUrl
QUrl m_hostUrl
Definition: upnpdevice.h:156
sLocation
static const QString sLocation
Definition: mythcontext.cpp:66
DeviceLocation::ExpiresInSecs
std::chrono::seconds ExpiresInSecs(void) const
Definition: upnpdevice.h:260
UPnpDeviceList
QList< UPnpDevice * > UPnpDeviceList
Definition: upnpdevice.h:40
uint
unsigned int uint
Definition: compat.h:140
DeviceLocation::m_pDeviceDesc
UPnpDeviceDesc * m_pDeviceDesc
Definition: upnpdevice.h:229
UPnpService::m_sControlURL
QString m_sControlURL
Definition: upnpdevice.h:78
referencecounter.h
UPnpService::m_sServiceId
QString m_sServiceId
Definition: upnpdevice.h:76
UPnpService
Definition: upnpdevice.h:72
std
Definition: mythchrono.h:23
UPnpDevice::m_sModelDescription
QString m_sModelDescription
Definition: upnpdevice.h:109
UPNP_PUBLIC
#define UPNP_PUBLIC
Definition: upnpexp.h:9
UPnpDevice::m_sManufacturerURL
QString m_sManufacturerURL
Definition: upnpdevice.h:108
UPnpDevice::m_protocolVersion
QString m_protocolVersion
Definition: upnpdevice.h:122
UPnpDevice::m_sPresentationURL
QString m_sPresentationURL
Definition: upnpdevice.h:115
DeviceLocation::m_ttExpires
TaskTime m_ttExpires
Definition: upnpdevice.h:236
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:77
ReferenceCounter
General purpose reference counter.
Definition: referencecounter.h:26
upnpexp.h
DeviceLocation::GetDeviceDetail
void GetDeviceDetail(InfoMap &map)
Definition: upnpdevice.h:305