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).arg(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").arg(pad).arg(m_sServiceType) +
90  QString("%0 id: %1\n").arg(pad).arg(m_sServiceId) +
91  QString("%0 SCPD URL: %1\n").arg(pad).arg(m_sSCPDURL) +
92  QString("%0 Control URL: %1\n").arg(pad).arg(m_sControlURL) +
93  QString("%0 Event Sub URL: %1").arg(pad).arg(m_sEventSubURL);
94  }
95 };
96 
98 
100 {
101  public:
102 
103  QString m_sDeviceType;
108  QString m_sModelName;
109  QString m_sModelNumber;
110  QString m_sModelURL;
112  QString m_sUPC;
114  mutable QString m_sUDN;
115 
117 
119  bool m_securityPin { false };
121 
125 
126  public:
127  UPnpDevice();
128  ~UPnpDevice();
129 
130  QString GetUDN(void) const;
131 
132  void toMap(InfoMap &map) const;
133 
134  UPnpService GetService(const QString &urn, bool *found = nullptr) const;
135 
136  QString toString(uint padding = 0) const;
137 };
138 
139 
142 //
143 //
144 //
147 
149 {
150  public:
151 
153  QString m_sHostName;
154  QUrl m_hostUrl;
155 
156  protected:
157 
158  void InternalLoad( QDomNode oNode, UPnpDevice *pCurDevice );
159 
160  static void ProcessIconList ( const QDomNode& oListNode, UPnpDevice *pDevice );
161  static void ProcessServiceList( const QDomNode& oListNode, UPnpDevice *pDevice );
162  void ProcessDeviceList ( const QDomNode& oListNode, UPnpDevice *pDevice );
163 
164  void OutputDevice( QTextStream &os,
165  UPnpDevice *pDevice,
166  const QString &sUserAgent = "" );
167 
168  static void SetStrValue ( const QDomNode &n, QString &sValue );
169  static void SetNumValue ( const QDomNode &n, int &nValue );
170  static void SetBoolValue( const QDomNode &n, bool &nValue );
171 
172  static QString FormatValue ( const NameValue &node );
173  static QString FormatValue ( const QString &sName, const QString &sValue );
174  static QString FormatValue ( const QString &sName, int nValue );
175 
176  QString GetHostName () const;
177 
178  public:
179 
180  UPnpDeviceDesc() = default;
181  virtual ~UPnpDeviceDesc() = default;
182 
183  bool Load ( const QString &sFileName );
184  bool Load ( const QDomDocument &xmlDevDesc );
185 
186  void GetValidXML( const QString &sBaseAddress, int nPort, QTextStream &os, const QString &sUserAgent = "" );
187  QString GetValidXML( const QString &sBaseAddress, int nPort );
188 
189  QString FindDeviceUDN( UPnpDevice *pDevice, QString sST );
190 
191  UPnpDevice *FindDevice( const QString &sURI );
192 
193  static UPnpDevice *FindDevice( UPnpDevice *pDevice, const QString &sURI );
194  static UPnpDeviceDesc *Retrieve ( QString &sURL );
195 
196  void toMap(InfoMap &map) const
197  {
198  map["hostname"] = m_sHostName;
199  m_rootDevice.toMap(map);
200  }
201 };
202 
204 // DeviceLocation Class Definition/Implementation
206 
208 {
209  public:
210 
211  static int g_nAllocated; // Debugging only
212 
213  protected:
214 
215  // ==================================================================
216  // Destructor protected to force use of Release Method
217  // ==================================================================
218 
219  ~DeviceLocation() override
220  {
221  // Should be atomic decrement
222  g_nAllocated--;
223 
224  delete m_pDeviceDesc;
225  }
226 
227  UPnpDeviceDesc *m_pDeviceDesc; // We take ownership of this pointer.
228 
229  public:
230 
231  QString m_sURI; // Service Type URI
232  QString m_sUSN; // Unique Service Name
233  QString m_sLocation; // URL to Device Description
235  QString m_sSecurityPin; // Use for MythXML methods needed pin
236 
237  public:
238 
239  // ==================================================================
240 
241  DeviceLocation( QString sURI,
242  QString sUSN,
243  QString sLocation,
244  TaskTime ttExpires ) : ReferenceCounter(
245  "DeviceLocation" ),
246  m_pDeviceDesc( nullptr ),
247  m_sURI (std::move( sURI )),
248  m_sUSN (std::move( sUSN )),
249  m_sLocation (std::move( sLocation )),
250  m_ttExpires ( ttExpires )
251  {
252  // Should be atomic increment
253  g_nAllocated++;
254  }
255 
256  // ==================================================================
257 
258  int ExpiresInSecs(void) const
259  {
260  TaskTime ttNow;
261  gettimeofday( (&ttNow), nullptr );
262 
263  return m_ttExpires.tv_sec - ttNow.tv_sec;
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).arg(m_sUSN).arg(m_sLocation)
330  .arg(ExpiresInSecs()).arg(m_sSecurityPin);
331  }
332 };
333 
334 #endif // UPNPDEVICE_H
UPnpDevice::m_sModelURL
QString m_sModelURL
Definition: upnpdevice.h:110
NameValue
Definition: upnputil.h:51
DeviceLocation::g_nAllocated
static int g_nAllocated
Definition: upnpdevice.h:211
UPnpDevice::m_sManufacturer
QString m_sManufacturer
Definition: upnpdevice.h:105
UPnpDevice::m_listIcons
UPnpIconList m_listIcons
Definition: upnpdevice.h:122
UPnpDeviceDesc::Retrieve
static UPnpDeviceDesc * Retrieve(QString &sURL)
Definition: upnpdevice.cpp:615
UPnpIcon::m_sMimeType
QString m_sMimeType
Definition: upnpdevice.h:52
UPnpDevice::m_listServices
UPnpServiceList m_listServices
Definition: upnpdevice.h:123
UPnpDevice::m_sUPC
QString m_sUPC
Definition: upnpdevice.h:112
DeviceLocation::m_sURI
QString m_sURI
Definition: upnpdevice.h:231
UPnpDeviceDesc
Definition: upnpdevice.h:148
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:103
UPnpIconList
QList< UPnpIcon * > UPnpIconList
Definition: upnpdevice.h:42
UPnpService::m_sEventSubURL
QString m_sEventSubURL
Definition: upnpdevice.h:79
TaskTime
struct timeval TaskTime
Definition: httpserver.h:45
UPnpServiceList
QList< UPnpService * > UPnpServiceList
Definition: upnpdevice.h:41
DeviceLocation::GetDeviceDesc
UPnpDeviceDesc * GetDeviceDesc(void)
Definition: upnpdevice.h:268
DeviceLocation
Definition: upnpdevice.h:207
UPnpDevice::m_sUDN
QString m_sUDN
Definition: upnpdevice.h:114
DeviceLocation::~DeviceLocation
~DeviceLocation() override
Definition: upnpdevice.h:219
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:235
NameValues
Definition: upnputil.h:88
toString
QString toString(MarkTypes type)
Definition: programtypes.cpp:26
UPnpDeviceDesc::m_rootDevice
UPnpDevice m_rootDevice
Definition: upnpdevice.h:152
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:768
UPnpDeviceDesc::toMap
void toMap(InfoMap &map) const
Definition: upnpdevice.h:196
UPnpDevice::m_sModelName
QString m_sModelName
Definition: upnpdevice.h:108
DeviceLocation::m_sUSN
QString m_sUSN
Definition: upnpdevice.h:232
UPnpDevice::m_securityPin
bool m_securityPin
MythTV specific information.
Definition: upnpdevice.h:119
UPnpDeviceDesc::m_sHostName
QString m_sHostName
Definition: upnpdevice.h:153
UPnpDevice::m_sModelNumber
QString m_sModelNumber
Definition: upnpdevice.h:109
UPnpDevice::m_lstExtra
NameValues m_lstExtra
Definition: upnpdevice.h:116
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:111
UPnpDevice::m_listDevices
UPnpDeviceList m_listDevices
Definition: upnpdevice.h:124
DeviceLocation::m_sLocation
QString m_sLocation
Definition: upnpdevice.h:233
mythtypes.h
DeviceLocation::DeviceLocation
DeviceLocation(QString sURI, QString sUSN, QString sLocation, TaskTime ttExpires)
Definition: upnpdevice.h:241
UPnpDevice
Definition: upnpdevice.h:99
UPnpDevice::m_sFriendlyName
QString m_sFriendlyName
Definition: upnpdevice.h:104
UPnpDeviceDesc::m_hostUrl
QUrl m_hostUrl
Definition: upnpdevice.h:154
sLocation
static const QString sLocation
Definition: mythcontext.cpp:63
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:227
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
UPnpDevice::m_sModelDescription
QString m_sModelDescription
Definition: upnpdevice.h:107
DeviceLocation::ExpiresInSecs
int ExpiresInSecs(void) const
Definition: upnpdevice.h:258
UPNP_PUBLIC
#define UPNP_PUBLIC
Definition: upnpexp.h:9
UPnpDevice::m_sManufacturerURL
QString m_sManufacturerURL
Definition: upnpdevice.h:106
UPnpDevice::m_protocolVersion
QString m_protocolVersion
Definition: upnpdevice.h:120
UPnpDevice::m_sPresentationURL
QString m_sPresentationURL
Definition: upnpdevice.h:113
DeviceLocation::m_ttExpires
TaskTime m_ttExpires
Definition: upnpdevice.h:234
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