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