aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/upnp/RootService.cpp
blob: 6ec7116694d9195c57ddf3b3e0e6ed03ec5fcb96 (about) (plain) (blame)
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
//=============================================================================
//
//   File : RootService.cpp
//   Creation date : Fri Aug 08 18:00:00 2008 GMT by Fabio Bas
//
//   This file is part of the KVIrc IRC client distribution
//   Copyright (C) 2008 Fabio Bas (ctrlaltca at gmail dot com)
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your option) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//   Original Copyright follows:
//=============================================================================

/***************************************************************************
                          RootService.cpp -  description
                             -------------------
    begin                : Mon Jul 25 2005
    copyright            : (C) 2005 by Diederik van der Boor
    email                : vdboor --at-- codingdomain.com
 ***************************************************************************/

#include "RootService.h"
#include "XmlFunctions.h"
#include "KviLocale.h"
#include "kvi_out.h"
#include "KviApplication.h"
#include "KviConsoleWindow.h"

#include <QDebug>

namespace UPnP
{
	// The constructor
	RootService::RootService(const QString & hostname, int port, const QString & rootUrl)
	    : Service(hostname, port, rootUrl), m_szHostname(hostname), m_iPort(port)
	{
	}

	// The destructor
	RootService::~RootService()
	    = default;

	// Recursively add all devices and embedded devices to the deviceServices_ map
	void RootService::addDeviceServices(const QDomNode & device)
	{
		qDebug() << "UPnP discovered device " << XmlFunctions::getNodeValue(device, "/UDN") << Qt::endl;

		if(XmlFunctions::getNodeValue(device, "/deviceType") == InternetGatewayDeviceType)
		{
			QString description;
			description = XmlFunctions::getNodeValue(device, "/friendlyName");
			if(description.isNull())
				description = XmlFunctions::getNodeValue(device, "/modelDescription");
			if(description.isNull())
				description = XmlFunctions::getNodeValue(device, "/modelName") + " " + XmlFunctions::getNodeValue(device, "/modelNumber");
			if(description.isNull())
				description = __tr2qs("Unknown");

			qDebug() << "Model: " << description << Qt::endl;

			g_pApp->activeConsole()->output(KVI_OUT_GENERICSTATUS, __tr2qs_ctx("[UPNP]: found gateway device: %s", "upnp"), description.toUtf8().data());
		}
		// Insert the given device node
		// The "key" is the device/UDN tag, the value is a list of device/serviceList/service nodes
		m_deviceServices.insert(XmlFunctions::getNodeValue(device, "/UDN"),
		    device.namedItem("serviceList").childNodes());

		// Find all embedded device nodes
		QDomNodeList embeddedDevices = device.namedItem("deviceList").childNodes();
		for(int i = 0; i < embeddedDevices.count(); i++)
		{
			if(embeddedDevices.item(i).nodeName() != "device")
				continue;
			addDeviceServices(embeddedDevices.item(i));
		}
	}

	// Return the device type
	QString RootService::getDeviceType() const
	{
		return m_szDeviceType;
	}

	// Return a service from the cached root device entry
	ServiceParameters RootService::getServiceById(const QString & serviceId) const
	{
		// Get a /root/device/serviceList/service/ tag

		ServiceParameters params;
		QMap<QString, QDomNodeList>::const_iterator i = m_deviceServices.constBegin();
		while(i != m_deviceServices.constEnd())
		{
			if(getServiceById(serviceId, i.key(), params))
				return params;
			++i;
		}

		//calling function check this field to understand if the struct is null
		params.controlUrl = QString();
		return params;
	}

	// Return a service from a cached embedded device entry
	bool RootService::getServiceById(const QString & serviceId, const QString & deviceUdn, ServiceParameters & params) const
	{
		// Get a /root/device/deviceList/device/.../serviceList/service/serviceId tag
		QDomNode service = XmlFunctions::getNodeChildByKey(m_deviceServices[deviceUdn], "serviceId", serviceId);

		if(!service.isNull())
		{
			params.hostname = m_szHostname;
			params.port = m_iPort;
			params.controlUrl = XmlFunctions::getNodeValue(service, "/controlURL");
			params.scpdUrl = XmlFunctions::getNodeValue(service, "/SCPDURL");
			params.serviceId = XmlFunctions::getNodeValue(service, "/serviceId");
			params.serviceType = XmlFunctions::getNodeValue(service, "/serviceType");

			return true;
		}
		else
		{
			return false;
		}
	}

	// Return a service from the cached root device entry
	ServiceParameters RootService::getServiceByType(const QString & serviceType) const
	{
		// Get a /root/device/serviceList/service/ tag

		ServiceParameters params;
		QMap<QString, QDomNodeList>::const_iterator i = m_deviceServices.constBegin();
		while(i != m_deviceServices.constEnd())
		{
			if(getServiceByType(serviceType, i.key(), params))
				return params;
			++i;
		}

		//calling function check this field to understand if the struct is null
		params.controlUrl = QString();
		return params;
	}

	// Return a service from a cached embedded device entry
	bool RootService::getServiceByType(const QString & serviceType, const QString & deviceUdn, ServiceParameters & params) const
	{
		// Get a /root/device/deviceList/device/.../serviceList/service/serviceType tag
		QDomNode service = XmlFunctions::getNodeChildByKey(m_deviceServices[deviceUdn], "serviceType", serviceType);

		if(!service.isNull())
		{
			params.hostname = m_szHostname;
			params.port = m_iPort;
			params.controlUrl = XmlFunctions::getNodeValue(service, "/controlURL");
			params.scpdUrl = XmlFunctions::getNodeValue(service, "/SCPDURL");
			params.serviceId = XmlFunctions::getNodeValue(service, "/serviceId");
			params.serviceType = serviceType;

			return true;
		}
		else
		{
			qWarning() << "UPnP::RootService::getServiceByType -"
			           << " type '" << serviceType << "' not found for device '" << deviceUdn << "'." << Qt::endl;

			return false;
		}
	}

	// The control point received a response to callInformationUrl()
	void RootService::gotInformationResponse(const QDomNode & response)
	{
		// Register all device UDN nodes for later
		m_deviceServices.clear();
		addDeviceServices(XmlFunctions::getNode(response, "/device"));

		// Fetch the required data
		m_szDeviceType = XmlFunctions::getNodeValue(response, "/device/deviceType");
		m_szRootUdn = XmlFunctions::getNodeValue(response, "/device/UDN");

		// The rootUdn_ is used to retrieve
		// the /root/device/serviceList/service
		// nodes from the deviceServices_ map
	}

	// Query the device for its service list
	void RootService::queryDevice()
	{
		callInformationUrl();
	}

} // end of namespae