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