forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upnp.cpp
186 lines (162 loc) · 6.08 KB
/
upnp.cpp
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
/*
* Copyright (c) 2016 dresden elektronik ingenieurtechnik gmbh.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*/
#include <QString>
#include <QTcpSocket>
#include <QTimer>
#include <QFile>
#include <QUdpSocket>
#include <QVariantMap>
#include "de_web_plugin.h"
#include "de_web_plugin_private.h"
/*! Inits the UPnP discorvery. */
void DeRestPluginPrivate::initUpnpDiscovery()
{
DBG_Assert(udpSock == 0);
udpSock = new QUdpSocket(this);
udpSockOut = new QUdpSocket(this);
joinedMulticastGroup = false;
connect(udpSock, SIGNAL(readyRead()),
this, SLOT(upnpReadyRead()));
upnpTimer = new QTimer(this);
upnpTimer->setSingleShot(false);
connect(upnpTimer, SIGNAL(timeout()),
this, SLOT(announceUpnp()));
upnpTimer->start(1000); // setup phase fast interval
initDescriptionXml();
}
/*! Replaces description_in.xml template with dynamic content. */
void DeRestPluginPrivate::initDescriptionXml()
{
deCONZ::ApsController *apsCtrl = deCONZ::ApsController::instance();
QString serverRoot = apsCtrl->getParameter(deCONZ::ParamHttpRoot);
if (!serverRoot.isEmpty())
{
descriptionXml.clear();
QFile f(serverRoot + "/description_in.xml");
if (f.open(QFile::ReadOnly))
{
QByteArray line;
do {
line = f.readLine(320);
if (!line.isEmpty())
{
line.replace(QString("{{PORT}}"), qPrintable(QString::number(gwPort)));
line.replace(QString("{{IPADDRESS}}"), qPrintable(gwIpAddress));
line.replace(QString("{{UUID}}"), qPrintable(gwUuid));
line.replace(QString("{{GWNAME}}"), qPrintable(gwName));
line.replace(QString("{{BRIDGEID}}"), qPrintable(gwBridgeId));
descriptionXml.append(line);
DBG_Printf(DBG_INFO_L2, "%s", line.constData());
}
} while (!line.isEmpty());
}
}
}
/*! Sends SSDP broadcast for announcement. */
void DeRestPluginPrivate::announceUpnp()
{
if (udpSock->state() != QAbstractSocket::BoundState)
{
joinedMulticastGroup = false;
DBG_Printf(DBG_ERROR, "UPNP socket not bound, state: %d\n", udpSock->state());
// retry
if (!udpSock->bind(QHostAddress("0.0.0.0"), 1900, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint)) // SSDP
{
DBG_Printf(DBG_ERROR, "UPNP error %s\n", qPrintable(udpSock->errorString()));
}
return;
}
if (!joinedMulticastGroup)
{
QHostAddress groupAddress("239.255.255.250");
if (!udpSock->joinMulticastGroup(groupAddress))
{
DBG_Printf(DBG_ERROR, "UPNP error %s\n", qPrintable(udpSock->errorString()));
return;
}
joinedMulticastGroup = true;
}
if (upnpTimer->interval() != (20 * 1000))
upnpTimer->start(20 * 1000);
quint16 port = 1900;
QHostAddress host;
QByteArray datagram = QString(QLatin1String(
"NOTIFY * HTTP/1.1\r\n"
"HOST: 239.255.255.250:1900\r\n"
"CACHE-CONTROL: max-age=100\r\n"
"LOCATION: http://%1:%2/description.xml\r\n"
"SERVER: FreeRTOS/7.4.2, UPnP/1.0, IpBridge/1.8.0\r\n"
"NTS: ssdp:alive\r\n"
"NT: upnp:rootdevice\r\n"
"USN: uuid:%3::upnp:rootdevice\r\n"
"GWID.phoscon.de: %4\r\n"
"hue-bridgeid: %4\r\n"
"\r\n"))
.arg(gwConfig["ipaddress"].toString())
.arg(gwConfig["port"].toDouble())
.arg(gwConfig["uuid"].toString())
.arg(gwBridgeId).toLocal8Bit();
host.setAddress(QLatin1String("239.255.255.250"));
if (udpSockOut->writeDatagram(datagram, host, port) == -1)
{
DBG_Printf(DBG_ERROR, "UDP send error %s\n", qPrintable(udpSockOut->errorString()));
}
}
/*! Handles SSDP packets. */
void DeRestPluginPrivate::upnpReadyRead()
{
while (udpSock->hasPendingDatagrams())
{
quint16 port;
QHostAddress host;
QByteArray datagram;
datagram.resize(udpSock->pendingDatagramSize());
udpSock->readDatagram(datagram.data(), datagram.size(), &host, &port);
QTextStream stream(datagram);
QString searchTarget;
while (!stream.atEnd())
{
QString line = stream.readLine();
if (!line.startsWith(QLatin1String("ST:")))
{
continue;
}
if (line.contains(QLatin1String("ssdp:all")) ||
line.contains(QLatin1String("device:basic")) ||
line.contains(QLatin1String("upnp:rootdevice")))
{
searchTarget = line;
break;
}
}
if (datagram.startsWith("M-SEARCH *") && !searchTarget.isEmpty())
{
DBG_Printf(DBG_HTTP, "UPNP %s:%u\n%s\n", qPrintable(host.toString()), port, datagram.data());
datagram.clear();
datagram.append("HTTP/1.1 200 OK\r\n");
datagram.append("CACHE-CONTROL: max-age=100\r\n");
datagram.append("EXT:\r\n");
datagram.append(QString("LOCATION: http://%1:%2/description.xml\r\n")
.arg(gwConfig["ipaddress"].toString())
.arg(gwConfig["port"].toDouble()).toLocal8Bit());
datagram.append("SERVER: FreeRTOS/7.4.2, UPnP/1.0, IpBridge/1.8.0\r\n");
datagram.append(searchTarget);
datagram.append(QLatin1String("\r\n"));
datagram.append(QString("USN: uuid:%1::upnp:rootdevice\r\n").arg(gwUuid));
datagram.append(QString("GWID.phoscon.de: %1\r\n").arg(gwBridgeId));
datagram.append(QString("hue-bridgeid: %1\r\n").arg(gwBridgeId));
datagram.append("\r\n");
if (udpSockOut->writeDatagram(datagram.data(), datagram.size(), host, port) == -1)
{
DBG_Printf(DBG_ERROR, "UDP send error %s\n", qPrintable(udpSockOut->errorString()));
}
}
}
}