13 #if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
14 #include <QStringConverter>
29 static constexpr std::chrono::milliseconds
MAX_WAIT { 30s };
31 #define LOC QString("UPnPSub: ")
54 if (addr.protocol() == QAbstractSocket::IPv6Protocol)
55 host =
"[" + addr.toString() +
"]";
57 host = addr.toString();
59 m_callback = QString(
"http://%1:%2/Subscriptions/event?usn=")
60 .arg(host, QString::number(port));
67 while (!usns.isEmpty())
72 LOG(VB_UPNP, LOG_DEBUG,
LOC +
"Finished");
78 LOG(VB_UPNP, LOG_DEBUG,
LOC + QString(
"Subscribe %1 %2 %3")
79 .arg(usn, url.toString(), path));
92 LOG(VB_GENERAL, LOG_WARNING,
LOC +
93 "Re-subscribing with different url and path.");
112 QString uuid = QString();
130 LOG(VB_UPNP, LOG_DEBUG,
LOC + QString(
"Renew: %1").arg(usn));
146 LOG(VB_UPNP, LOG_ERR,
LOC + QString(
"Unrecognised renewal usn: %1")
157 LOG(VB_UPNP, LOG_ERR,
LOC + QString(
"No uuid - not renewing usn: %1")
171 LOG(VB_UPNP, LOG_INFO,
LOC + QString(
"Removing %1").arg(usn));
188 LOG(VB_UPNP, LOG_DEBUG,
LOC + QString(
"%1\n%2")
200 if (nt.isEmpty() || nts.isEmpty() || !no)
207 if (nt !=
"upnp:event" || nts !=
"upnp:propchange")
212 if (usn.isEmpty() || sid.isEmpty())
225 int loc = pRequest->
m_sPayload.lastIndexOf(
"propertyset>");
226 QString payload = (loc > -1) ? pRequest->
m_sPayload.left(loc + 12) :
229 LOG(VB_UPNP, LOG_DEBUG,
LOC + QString(
"Payload:\n%1").arg(payload));
236 if (!body.setContent(payload,
true, &
error, &errorLine, &errorCol))
238 LOG(VB_GENERAL, LOG_ERR,
LOC +
239 QString(
"Failed to parse event: Line: %1 Col: %2 Error: '%3'")
240 .arg(errorLine).arg(errorCol).arg(
error));
244 LOG(VB_UPNP, LOG_DEBUG,
LOC +
"/n/n" + body.toString(4) +
"/n/n");
246 QDomNodeList properties = body.elementsByTagName(
"property");
251 for (
int i = 0; i < properties.size(); i++)
253 QDomNodeList arguments = properties.at(i).childNodes();
254 for (
int j = 0; j < arguments.size(); j++)
256 QDomElement e = arguments.at(j).toElement();
257 if (!e.isNull() && !e.text().isEmpty() && !e.tagName().isEmpty())
258 results.insert(e.tagName(), e.text());
282 bool success =
false;
283 QString host = url.host();
284 int port = url.port();
287 QTextStream data(&sub);
288 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
289 data.setCodec(QTextCodec::codecForName(
"UTF-8"));
291 data.setEncoding(QStringConverter::Utf8);
294 data << QString(
"UNSUBSCRIBE %1 HTTP/1.1\r\n").arg(path);
295 data << QString(
"HOST: %1:%2\r\n").arg(host, QString::number(port));
296 data << QString(
"SID: uuid:%1\r\n").arg(uuid);
300 LOG(VB_UPNP, LOG_DEBUG,
LOC +
"\n\n" + sub);
302 auto *sockdev =
new MSocketDevice(MSocketDevice::Stream);
304 sockdev->setBlocking(
true);
306 if (sock->Connect(QHostAddress(host), port))
308 if (sock->WriteBlockDirect(sub.constData(), sub.size()) != -1)
310 QString line = sock->ReadLine(
MAX_WAIT);
311 success = !line.isEmpty();
315 LOG(VB_GENERAL, LOG_ERR,
LOC +
316 QString(
"Socket write error for %1:%2") .arg(host).arg(port));
322 LOG(VB_GENERAL, LOG_ERR,
LOC +
323 QString(
"Failed to open socket for %1:%2") .arg(host).arg(port));
329 LOG(VB_GENERAL, LOG_INFO,
LOC + QString(
"Unsubscribed to %1").arg(usn));
331 LOG(VB_UPNP, LOG_WARNING,
LOC + QString(
"Failed to unsubscribe to %1")
340 const QString &uuidin,
343 QString host = url.host();
344 int port = url.port();
347 QTextStream data(&sub);
348 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
349 data.setCodec(QTextCodec::codecForName(
"UTF-8"));
351 data.setEncoding(QStringConverter::Utf8);
354 data << QString(
"SUBSCRIBE %1 HTTP/1.1\r\n").arg(path);
355 data << QString(
"HOST: %1:%2\r\n").arg(host, QString::number(port));
358 if (uuidin.isEmpty())
360 data << QString(
"CALLBACK: <%1%2>\r\n")
362 data <<
"NT: upnp:event\r\n";
365 data << QString(
"SID: uuid:%1\r\n").arg(uuidin);
371 LOG(VB_UPNP, LOG_DEBUG,
LOC +
"\n\n" + sub);
373 auto *sockdev =
new MSocketDevice(MSocketDevice::Stream);
375 sockdev->setBlocking(
true);
379 std::chrono::seconds result = 0s;
381 if (sock->Connect(QHostAddress(host), port))
383 if (sock->WriteBlockDirect(sub.constData(), sub.size()) != -1)
386 QString line = sock->ReadLine(
MAX_WAIT);
387 while (!line.isEmpty())
389 LOG(VB_UPNP, LOG_DEBUG,
LOC + line);
390 if (line.contains(
"HTTP/1.1 200 OK", Qt::CaseInsensitive))
392 if (line.startsWith(
"SID:", Qt::CaseInsensitive))
393 uuid = line.mid(4).trimmed().mid(5).trimmed();
394 if (line.startsWith(
"TIMEOUT:", Qt::CaseInsensitive))
395 timeout = line.mid(8).trimmed().mid(7).trimmed();
396 if (ok && !uuid.isEmpty() && !
timeout.isEmpty())
401 if (ok && !uuid.isEmpty() && !
timeout.isEmpty())
404 result = std::chrono::seconds(
timeout.toUInt());
408 LOG(VB_GENERAL, LOG_ERR,
LOC +
409 QString(
"Failed to subscribe to %1").arg(usn));
414 LOG(VB_GENERAL, LOG_ERR,
LOC +
415 QString(
"Socket write error for %1:%2") .arg(host).arg(port));
421 LOG(VB_GENERAL, LOG_ERR,
LOC +
422 QString(
"Failed to open socket for %1:%2") .arg(host).arg(port));