MythTV  master
upnpdevice.cpp
Go to the documentation of this file.
1 // Program Name: upnpdevice.cpp
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 #include "upnp.h"
14 #include "upnpdevice.h"
15 #include "mythdownloadmanager.h"
16 #include "mythlogging.h"
17 #include "mythversion.h" // for MYTH_BINARY_VERSION
18 #include "mythcorecontext.h"
19 
20 // MythDB
21 #include "mythdb.h"
22 
23 #include <unistd.h> // for gethostname
24 
25 #include <QFile>
26 #include <QTextStream>
27 #include <QHostAddress>
28 
29 int DeviceLocation::g_nAllocated = 0; // Debugging only
30 
33 //
34 // UPnpDeviceDesc Class Implementation
35 //
38 
40 //
42 
43 bool UPnpDeviceDesc::Load( const QString &sFileName )
44 {
45  // ----------------------------------------------------------------------
46  // Open Supplied XML uPnp Description file.
47  // ----------------------------------------------------------------------
48 
49  QDomDocument doc ( "upnp" );
50  QFile file( sFileName );
51 
52  if ( !file.open( QIODevice::ReadOnly ) )
53  return false;
54 
55  QString sErrMsg;
56  int nErrLine = 0;
57  int nErrCol = 0;
58  bool bSuccess = doc.setContent( &file, false,
59  &sErrMsg, &nErrLine, &nErrCol );
60 
61  file.close();
62 
63  if (!bSuccess)
64  {
65  LOG(VB_GENERAL, LOG_ERR,
66  QString("UPnpDeviceDesc::Load - Error parsing: %1 "
67  "at line: %2 column: %3")
68  .arg(sFileName) .arg(nErrLine)
69  .arg(nErrCol));
70  LOG(VB_GENERAL, LOG_ERR,
71  QString("UPnpDeviceDesc::Load - Error Msg: %1" ) .arg(sErrMsg));
72  return false;
73  }
74 
75  // --------------------------------------------------------------
76  // XML Document Loaded... now parse it into the UPnpDevice Hierarchy
77  // --------------------------------------------------------------
78 
79  return Load( doc );
80 }
81 
83 //
85 
86 bool UPnpDeviceDesc::Load( const QDomDocument &xmlDevDesc )
87 {
88  // --------------------------------------------------------------
89  // Parse XML into the UPnpDevice Hierarchy
90  // --------------------------------------------------------------
91 
92  QDomNode oNode = xmlDevDesc.documentElement();
93 
94  InternalLoad( oNode.namedItem( "device" ), &m_rootDevice );
95 
96  return true;
97 }
98 
100 //
102 
103 void UPnpDeviceDesc::InternalLoad( QDomNode oNode, UPnpDevice *pCurDevice )
104 {
105  QString pin = GetMythDB()->GetSetting( "SecurityPin", "");
106  pCurDevice->m_securityPin = !(pin.isEmpty() || pin == "0000");
107 
108  for ( oNode = oNode.firstChild();
109  !oNode.isNull();
110  oNode = oNode.nextSibling() )
111  {
112  QDomElement e = oNode.toElement();
113 
114  if (e.isNull())
115  continue;
116 
117  // TODO: make this table driven (using offset within structure)
118  if ( e.tagName() == "deviceType" )
119  SetStrValue( e, pCurDevice->m_sDeviceType);
120  else if ( e.tagName() == "friendlyName" )
121  SetStrValue( e, pCurDevice->m_sFriendlyName );
122  else if ( e.tagName() == "manufacturer" )
123  SetStrValue( e, pCurDevice->m_sManufacturer );
124  else if ( e.tagName() == "manufacturerURL" )
125  SetStrValue( e, pCurDevice->m_sManufacturerURL );
126  else if ( e.tagName() == "modelDescription" )
127  SetStrValue( e, pCurDevice->m_sModelDescription);
128  else if ( e.tagName() == "modelName" )
129  SetStrValue( e, pCurDevice->m_sModelName );
130  else if ( e.tagName() == "modelNumber" )
131  SetStrValue( e, pCurDevice->m_sModelNumber );
132  else if ( e.tagName() == "modelURL" )
133  SetStrValue( e, pCurDevice->m_sModelURL );
134  else if ( e.tagName() == "serialNumber" )
135  SetStrValue( e, pCurDevice->m_sSerialNumber );
136  else if ( e.tagName() == "UPC" )
137  SetStrValue( e, pCurDevice->m_sUPC );
138  else if ( e.tagName() == "presentationURL" )
139  SetStrValue( e, pCurDevice->m_sPresentationURL );
140  else if ( e.tagName() == "UDN" )
141  SetStrValue( e, pCurDevice->m_sUDN );
142  else if ( e.tagName() == "iconList" )
143  ProcessIconList( oNode, pCurDevice );
144  else if ( e.tagName() == "serviceList" )
145  ProcessServiceList( oNode, pCurDevice );
146  else if ( e.tagName() == "deviceList" )
147  ProcessDeviceList ( oNode, pCurDevice );
148  else if ( e.tagName() == "mythtv:X_secure" )
149  SetBoolValue( e, pCurDevice->m_securityPin );
150  else if ( e.tagName() == "mythtv:X_protocol" )
151  SetStrValue( e, pCurDevice->m_protocolVersion );
152  else
153  {
154  // Not one of the expected element names... add to extra list.
155  QString sValue = "";
156  SetStrValue( e, sValue );
157  NameValue node = NameValue(e.tagName(), sValue);
158  QDomNamedNodeMap attributes = e.attributes();
159  for (int i = 0; i < attributes.size(); i++)
160  {
161  node.AddAttribute(attributes.item(i).nodeName(),
162  attributes.item(i).nodeValue(),
163  true); // TODO Check whether all attributes are in fact requires for the device xml
164  }
165  pCurDevice->m_lstExtra.push_back(node);
166  }
167  }
168 }
169 
171 //
173 
174 void UPnpDeviceDesc::ProcessIconList( const QDomNode& oListNode, UPnpDevice *pDevice )
175 {
176  for ( QDomNode oNode = oListNode.firstChild();
177  !oNode.isNull();
178  oNode = oNode.nextSibling() )
179  {
180  QDomElement e = oNode.toElement();
181 
182  if (e.isNull())
183  continue;
184 
185  if ( e.tagName() == "icon" )
186  {
187  auto *pIcon = new UPnpIcon();
188  pDevice->m_listIcons.append( pIcon );
189 
190  SetStrValue( e.namedItem( "mimetype" ), pIcon->m_sMimeType );
191  SetNumValue( e.namedItem( "width" ), pIcon->m_nWidth );
192  SetNumValue( e.namedItem( "height" ), pIcon->m_nHeight );
193  SetNumValue( e.namedItem( "depth" ), pIcon->m_nDepth );
194  SetStrValue( e.namedItem( "url" ), pIcon->m_sURL );
195  }
196  }
197 }
198 
200 //
202 
203 void UPnpDeviceDesc::ProcessServiceList( const QDomNode& oListNode, UPnpDevice *pDevice )
204 {
205  for ( QDomNode oNode = oListNode.firstChild();
206  !oNode.isNull();
207  oNode = oNode.nextSibling() )
208  {
209  QDomElement e = oNode.toElement();
210 
211  if (e.isNull())
212  continue;
213 
214  if ( e.tagName() == "service" )
215  {
216  auto *pService = new UPnpService();
217  pDevice->m_listServices.append( pService );
218 
219  SetStrValue(e.namedItem( "serviceType" ), pService->m_sServiceType);
220  SetStrValue(e.namedItem( "serviceId" ), pService->m_sServiceId);
221  SetStrValue(e.namedItem( "SCPDURL" ), pService->m_sSCPDURL);
222  SetStrValue(e.namedItem( "controlURL" ), pService->m_sControlURL);
223  SetStrValue(e.namedItem( "eventSubURL" ), pService->m_sEventSubURL);
224 
225  LOG(VB_UPNP, LOG_INFO,
226  QString("ProcessServiceList adding service : %1 : %2 :")
227  .arg(pService->m_sServiceType)
228  .arg(pService->m_sServiceId));
229  }
230  }
231 }
232 
234 //
236 
237 void UPnpDeviceDesc::ProcessDeviceList( const QDomNode& oListNode,
238  UPnpDevice *pDevice )
239 {
240  for ( QDomNode oNode = oListNode.firstChild();
241  !oNode.isNull();
242  oNode = oNode.nextSibling() )
243  {
244  QDomElement e = oNode.toElement();
245 
246  if (e.isNull())
247  continue;
248 
249  if ( e.tagName() == "device")
250  {
251  auto *pNewDevice = new UPnpDevice();
252  pDevice->m_listDevices.append( pNewDevice );
253  InternalLoad( e, pNewDevice );
254  }
255  }
256 }
257 
260 void UPnpDeviceDesc::SetStrValue( const QDomNode &n, QString &sValue )
261 {
262  if (!n.isNull())
263  {
264  QDomText oText = n.firstChild().toText();
265 
266  if (!oText.isNull())
267  sValue = oText.nodeValue();
268  }
269 }
270 
273 void UPnpDeviceDesc::SetNumValue( const QDomNode &n, int &nValue )
274 {
275  if (!n.isNull())
276  {
277  QDomText oText = n.firstChild().toText();
278 
279  if (!oText.isNull())
280  nValue = oText.nodeValue().toInt();
281  }
282 }
283 
284 void UPnpDeviceDesc::SetBoolValue( const QDomNode &n, bool &nValue )
285 {
286  if (!n.isNull())
287  {
288  QDomText oText = n.firstChild().toText();
289 
290  if (!oText.isNull())
291  {
292  QString s = oText.nodeValue();
293  nValue = (s == "yes" || s == "true" || (s.toInt() != 0));
294  }
295  }
296 }
297 
299 //
301 
302 QString UPnpDeviceDesc::GetValidXML( const QString &sBaseAddress, int nPort )
303 {
304  QString sXML;
305  QTextStream os( &sXML, QIODevice::WriteOnly );
306 
307  GetValidXML( sBaseAddress, nPort, os );
308  os << flush;
309  return( sXML );
310 }
311 
313 //
315 
317  const QString &/*sBaseAddress*/, int /*nPort*/,
318  QTextStream &os, const QString &sUserAgent )
319 {
320 #if 0
321  os.setEncoding( QTextStream::UnicodeUTF8 );
322 #endif
323  os << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
324  "<root xmlns=\"urn:schemas-upnp-org:device-1-0\" "
325  " xmlns:mythtv=\"mythtv.org\">\n"
326  "<specVersion>\n"
327  "<major>1</major>\n"
328  "<minor>0</minor>\n"
329  "</specVersion>\n";
330 
331  OutputDevice( os, &m_rootDevice, sUserAgent );
332 
333  os << "</root>\n";
334  os << flush;
335 }
336 
338 //
340 
341 void UPnpDeviceDesc::OutputDevice( QTextStream &os,
342  UPnpDevice *pDevice,
343  const QString &sUserAgent )
344 {
345  if (pDevice == nullptr)
346  return;
347 
348  QString sFriendlyName = QString( "%1: %2" )
349  .arg( GetHostName() )
350  .arg( pDevice->m_sFriendlyName );
351 
352  // ----------------------------------------------------------------------
353  // Only override the root device
354  // ----------------------------------------------------------------------
355 
356  if (pDevice == &m_rootDevice)
357  sFriendlyName = UPnp::GetConfiguration()->GetValue( "UPnP/FriendlyName",
358  sFriendlyName );
359 
360  os << "<device>\n";
361  os << FormatValue( "deviceType" , pDevice->m_sDeviceType );
362  os << FormatValue( "friendlyName" , sFriendlyName );
363 
364  // ----------------------------------------------------------------------
365  // XBox 360 needs specific values in the Device Description.
366  //
367  // -=>TODO: This should be externalized in a more generic/extension
368  // kind of way.
369  // ----------------------------------------------------------------------
370 
371  bool bIsXbox360 =
372  sUserAgent.startsWith(QString("Xbox/2.0"), Qt::CaseInsensitive) ||
373  sUserAgent.startsWith(QString("Mozilla/4.0"), Qt::CaseInsensitive);
374 
375  os << FormatValue( "manufacturer" , pDevice->m_sManufacturer );
376  os << FormatValue( "modelURL" , pDevice->m_sModelURL );
377 
378  // HACK
379 // if ( bIsXbox360 )
380 // {
381 // os << FormatValue( "modelName" ,
382 // "Windows Media Connect Compatible (MythTV)");
383 // }
384 // else
385 // {
386  os << FormatValue( "modelName" , pDevice->m_sModelName );
387 // }
388 
389  os << FormatValue( "manufacturerURL" , pDevice->m_sManufacturerURL );
390  os << FormatValue( "modelDescription" , pDevice->m_sModelDescription);
391  os << FormatValue( "modelNumber" , pDevice->m_sModelNumber );
392  os << FormatValue( "serialNumber" , pDevice->m_sSerialNumber );
393  os << FormatValue( "UPC" , pDevice->m_sUPC );
394  os << FormatValue( "presentationURL" , pDevice->m_sPresentationURL );
395 
396  // MythTV Custom information
397  os << FormatValue( "mythtv:X_secure" ,
398  pDevice->m_securityPin ? "true" : "false");
399  os << FormatValue( "mythtv:X_protocol", pDevice->m_protocolVersion );
400 
401  foreach (const auto & nit, pDevice->m_lstExtra)
402  {
403  os << FormatValue( nit );
404  }
405 
406  // ----------------------------------------------------------------------
407  // Output Any Icons.
408  // ----------------------------------------------------------------------
409 
410  if (pDevice->m_listIcons.count() > 0)
411  {
412  os << "<iconList>\n";
413 
414  foreach (auto icon, pDevice->m_listIcons)
415  {
416  os << "<icon>\n";
417  os << FormatValue( "mimetype", icon->m_sMimeType );
418  os << FormatValue( "width" , icon->m_nWidth );
419  os << FormatValue( "height" , icon->m_nHeight );
420  os << FormatValue( "depth" , icon->m_nDepth );
421  os << FormatValue( "url" , icon->m_sURL );
422  os << "</icon>\n";
423  }
424  os << "</iconList>\n";
425  }
426 
427  os << FormatValue( "UDN" , pDevice->GetUDN() );
428 
429  // ----------------------------------------------------------------------
430  // Output any Services
431  // ----------------------------------------------------------------------
432 
433  if (pDevice->m_listServices.count() > 0)
434  {
435  // ------------------------------------------------------------------
436  // -=>TODO: As a temporary fix don't expose the MSRR service unless we
437  // as an XBox360 or Windows MediaPlayer.
438  //
439  // There is a problem with a DSM-520 with firmware 1.04 and
440  // the Denon AVR-4306 receiver.
441  //
442  // If the MSRR Service is exposed, it won't let us browse
443  // any media content.
444  //
445  // Need to find out real fix and remove this code.
446  // ------------------------------------------------------------------
447 
448 #if 0
449  bool bDSM = sUserAgent.startsWith("INTEL_NMPR/2.1 DLNADOC/1.00", false);
450 #endif
451 
452  os << "<serviceList>\n";
453 
454  foreach (auto service, pDevice->m_listServices)
455  {
456  if (!bIsXbox360 && service->m_sServiceType.startsWith(
457  "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar",
458  Qt::CaseInsensitive))
459  {
460  continue;
461  }
462 
463  os << "<service>\n";
464  os << FormatValue( "serviceType", service->m_sServiceType );
465  os << FormatValue( "serviceId" , service->m_sServiceId );
466  os << FormatValue( "SCPDURL" , service->m_sSCPDURL );
467  os << FormatValue( "controlURL" , service->m_sControlURL );
468  os << FormatValue( "eventSubURL", service->m_sEventSubURL );
469  os << "</service>\n";
470  }
471  os << "</serviceList>\n";
472  }
473 
474  // ----------------------------------------------------------------------
475  // Output any Embedded Devices
476  // ----------------------------------------------------------------------
477 
478  if (pDevice->m_listDevices.count() > 0)
479  {
480  os << "<deviceList>";
481 
482  UPnpDeviceList::iterator it;
483  for ( it = pDevice->m_listDevices.begin();
484  it != pDevice->m_listDevices.end();
485  ++it )
486  {
487  UPnpDevice *pEmbeddedDevice = (*it);
488  OutputDevice( os, pEmbeddedDevice );
489  }
490  os << "</deviceList>";
491  }
492  os << "</device>\n";
493  os << flush;
494 }
495 
497 //
499 
501 {
502  QString sStr;
503 
504  QString sAttributes;
505  NameValues::iterator it;
506  for (it = node.m_pAttributes->begin(); it != node.m_pAttributes->end(); ++it)
507  {
508  sAttributes += QString(" %1='%2'").arg((*it).m_sName).arg((*it).m_sValue);
509  }
510  sStr = QString("<%1%2>%3</%1>\n").arg(node.m_sName).arg(sAttributes).arg(node.m_sValue);
511 
512  return( sStr );
513 }
514 
516 //
518 
519 QString UPnpDeviceDesc::FormatValue( const QString &sName,
520  const QString &sValue )
521 {
522  QString sStr;
523 
524  if (sValue.length() > 0)
525  sStr = QString("<%1>%2</%1>\n") .arg(sName) .arg(sValue);
526 
527  return( sStr );
528 }
529 
531 
532 QString UPnpDeviceDesc::FormatValue( const QString &sName, int nValue )
533 {
534  return( QString("<%1>%2</%1>\n") .arg(sName) .arg(nValue) );
535 }
536 
538 //
540 
541 QString UPnpDeviceDesc::FindDeviceUDN( UPnpDevice *pDevice, QString sST )
542 {
543  // Ignore device version, UPnP is backwards compatible
544  if (sST.section(':', 0, -2) == pDevice->m_sDeviceType.section(':', 0, -2))
545  return pDevice->GetUDN();
546 
547  if (sST.section(':', 0, -2) == pDevice->GetUDN().section(':', 0, -2))
548  return sST;
549 
550  // ----------------------------------------------------------------------
551  // Check for matching Service
552  // ----------------------------------------------------------------------
553 
554  for (auto sit = pDevice->m_listServices.cbegin();
555  sit != pDevice->m_listServices.cend(); ++sit)
556  {
557  // Ignore the service version, UPnP is backwards compatible
558  if (sST.section(':', 0, -2) == (*sit)->m_sServiceType.section(':', 0, -2))
559  return pDevice->GetUDN();
560  }
561 
562  // ----------------------------------------------------------------------
563  // Check Embedded Devices for a Match
564  // ----------------------------------------------------------------------
565 
566  foreach (auto device, pDevice->m_listDevices)
567  {
568  QString sUDN = FindDeviceUDN( device, sST );
569  if (sUDN.length() > 0)
570  return sUDN;
571  }
572 
573  return "";
574 }
575 
577 //
579 
580 UPnpDevice *UPnpDeviceDesc::FindDevice( const QString &sURI )
581 {
582  return FindDevice( &m_rootDevice, sURI );
583 }
584 
586 //
588 
590  const QString &sURI )
591 {
592  // Ignore device version, UPnP is backwards compatible
593  if ( sURI.section(':', 0, -2) == pDevice->m_sDeviceType.section(':', 0, -2) )
594  return pDevice;
595 
596  // ----------------------------------------------------------------------
597  // Check Embedded Devices for a Match
598  // ----------------------------------------------------------------------
599 
600  foreach (auto & dev, pDevice->m_listDevices)
601  {
602  UPnpDevice *pFound = FindDevice(dev, sURI);
603 
604  if (pFound != nullptr)
605  return pFound;
606  }
607 
608  return nullptr;
609 }
610 
612 //
614 
616 {
617  UPnpDeviceDesc *pDevice = nullptr;
618 
619  LOG(VB_UPNP, LOG_DEBUG, QString("UPnpDeviceDesc::Retrieve( %1 )")
620  .arg(sURL));
621 
622  QByteArray buffer;
623 
624  bool ok = GetMythDownloadManager()->download(sURL, &buffer);
625 
626  QString sXml(buffer);
627 
628  if (ok && sXml.startsWith( QString("<?xml") ))
629  {
630  QString sErrorMsg;
631 
632  QDomDocument xml( "upnp" );
633 
634  if ( xml.setContent( sXml, false, &sErrorMsg ))
635  {
636  pDevice = new UPnpDeviceDesc();
637  pDevice->Load( xml );
638  pDevice->m_hostUrl = sURL;
639  pDevice->m_sHostName = pDevice->m_hostUrl.host();
640  }
641  else
642  {
643  LOG(VB_UPNP, LOG_ERR,
644  QString("Error parsing device description xml [%1]")
645  .arg(sErrorMsg));
646  }
647  }
648  else
649  {
650  LOG(VB_UPNP, LOG_ERR, QString("Invalid response '%1'").arg(sXml));
651  }
652 
653  return pDevice;
654 }
655 
657 //
659 
661 {
662  if (m_sHostName.length() == 0)
663  {
664  // Get HostName
665 
666  char localHostName[1024];
667 
668  if (gethostname(localHostName, 1024))
669  LOG(VB_GENERAL, LOG_ERR,
670  "UPnpDeviceDesc: Error, could not determine host name." + ENO);
671 
672  return UPnp::GetConfiguration()->GetValue("Settings/HostName",
673  QString(localHostName));
674  }
675 
676  return m_sHostName;
677 }
678 
681 //
682 // UPnpDevice Class Implementation
683 //
686 
688  m_sModelNumber(MYTH_BINARY_VERSION),
689  m_sSerialNumber(MYTH_SOURCE_VERSION),
690  m_protocolVersion(MYTH_PROTO_VERSION)
691 {
692 
693 // FIXME: UPnPDevice is created via the static initialisation of UPnPDeviceDesc
694 // in upnp.cpp ln 21. This means it's created before the core context is
695 // even intialised so we can't use settings, or any 'core' methods to
696 // help in populating the Device metadata. That is why the URLs below
697 // are relative, not absolute since at this stage we can't determine
698 // which IP or port to use!
699 
700 // NOTE: The icons are defined here and not in devicemaster.xml because they
701 // are also used for the MediaRenderer device and other possible future
702 // devices too.
703 
704  // Large PNG Icon
705  auto *pngIconLrg = new UPnpIcon();
706  pngIconLrg->m_nDepth = 24;
707  pngIconLrg->m_nHeight = 120;
708  pngIconLrg->m_nWidth = 120;
709  pngIconLrg->m_sMimeType = "image/png";
710  pngIconLrg->m_sURL = "/images/icons/upnp_large_icon.png";
711  m_listIcons.append(pngIconLrg);
712 
713  // Large JPG Icon
714  auto *jpgIconLrg = new UPnpIcon();
715  jpgIconLrg->m_nDepth = 24;
716  jpgIconLrg->m_nHeight = 120;
717  jpgIconLrg->m_nWidth = 120;
718  jpgIconLrg->m_sMimeType = "image/jpeg";
719  jpgIconLrg->m_sURL = "/images/icons/upnp_large_icon.jpg";
720  m_listIcons.append(jpgIconLrg);
721 
722  // Small PNG Icon
723  auto *pngIconSm = new UPnpIcon();
724  pngIconSm->m_nDepth = 24;
725  pngIconSm->m_nHeight = 48;
726  pngIconSm->m_nWidth = 48;
727  pngIconSm->m_sMimeType = "image/png";
728  pngIconSm->m_sURL = "/images/icons/upnp_small_icon.png";
729  m_listIcons.append(pngIconSm);
730 
731  // Small JPG Icon
732  auto *jpgIconSm = new UPnpIcon();
733  jpgIconSm->m_nDepth = 24;
734  jpgIconSm->m_nHeight = 48;
735  jpgIconSm->m_nWidth = 48;
736  jpgIconSm->m_sMimeType = "image/jpeg";
737  jpgIconSm->m_sURL = "/images/icons/upnp_small_icon.jpg";
738  m_listIcons.append(jpgIconSm);
739 }
740 
742 {
743  while (!m_listIcons.empty())
744  {
745  delete m_listIcons.back();
746  m_listIcons.pop_back();
747  }
748  while (!m_listServices.empty())
749  {
750  delete m_listServices.back();
751  m_listServices.pop_back();
752  }
753  while (!m_listDevices.empty())
754  {
755  delete m_listDevices.back();
756  m_listDevices.pop_back();
757  }
758 }
759 
760 QString UPnpDevice::GetUDN(void) const
761 {
762  if (m_sUDN.isEmpty())
763  m_sUDN = "uuid:" + LookupUDN( m_sDeviceType );
764 
765  return m_sUDN;
766 }
767 
769 {
770  map["name"] = m_sFriendlyName;
771  map["modelname"] = m_sModelName;
772  map["modelnumber"] = m_sModelNumber;
773  map["modelurl"] = m_sModelURL;
774  map["modeldescription"] = m_sModelDescription;
775  map["manufacturer"] = m_sManufacturer;
776  map["manufacturerurl"] = m_sManufacturerURL;
777  map["devicetype"] = m_sDeviceType;
778  map["serialnumber"] = m_sSerialNumber;
779  map["UDN"] = m_sUDN;
780  map["UPC"] = m_sUPC;
781  map["protocolversion"] = m_protocolVersion;
782 }
783 
784 UPnpService UPnpDevice::GetService(const QString &urn, bool *found) const
785 {
786  UPnpService srv;
787 
788  bool done = false;
789 
790  foreach (auto service, m_listServices)
791  {
792  // Ignore the service version
793  if (service->m_sServiceType.section(':', 0, -2) == urn.section(':', 0, -2))
794  {
795  srv = *service;
796  done = true;
797  break;
798  }
799  }
800 
801  if (!done)
802  {
803  UPnpDeviceList::const_iterator dit = m_listDevices.begin();
804  for (; dit != m_listDevices.end() && !done; ++dit)
805  srv = (*dit)->GetService(urn, &done);
806  }
807 
808  if (found)
809  *found = done;
810 
811  return srv;
812 }
813 
814 QString UPnpDevice::toString(uint padding) const
815 {
816  QString ret =
817  QString("UPnP Device\n"
818  "===========\n"
819  "deviceType: %1\n"
820  "friendlyName: %2\n"
821  "manufacturer: %3\n"
822  "manufacturerURL: %4\n"
823  "modelDescription: %5\n"
824  "modelName: %6\n"
825  "modelNumber: %7\n"
826  "modelURL: %8\n")
827  .arg(m_sDeviceType )
828  .arg(m_sFriendlyName )
829  .arg(m_sManufacturer )
830  .arg(m_sManufacturerURL )
831  .arg(m_sModelDescription)
832  .arg(m_sModelName )
833  .arg(m_sModelNumber )
834  .arg(m_sModelURL ) +
835  QString("serialNumber: %1\n"
836  "UPC: %2\n"
837  "presentationURL: %3\n"
838  "UDN: %4\n")
839  .arg(m_sSerialNumber )
840  .arg(m_sUPC )
841  .arg(m_sPresentationURL )
842  .arg(m_sUDN );
843 
844  if (!m_lstExtra.empty())
845  {
846  ret += "Extra key value pairs\n";
847  foreach (const auto & extra, m_lstExtra)
848  {
849  ret += extra.m_sName;
850  ret += ":";
851  int int_padding = 18 - (extra.m_sName.length() + 1);
852  for (int i = 0; i < int_padding; i++)
853  ret += " ";
854  ret += QString("%1\n").arg(extra.m_sValue);
855  }
856  }
857 
858  if (!m_listIcons.empty())
859  {
860  ret += "Icon List:\n";
861  foreach (auto icon, m_listIcons)
862  ret += icon->toString(padding+2) + "\n";
863  }
864 
865  if (!m_listServices.empty())
866  {
867  ret += "Service List:\n";
868  foreach (auto service, m_listServices)
869  ret += service->toString(padding+2) + "\n";
870  }
871 
872  if (!m_listDevices.empty())
873  {
874  ret += "Device List:\n";
875  foreach (auto device, m_listDevices)
876  ret += device->toString(padding+2) + "\n";
877  ret += "\n";
878  }
879 
880  // remove trailing newline
881  if (ret.endsWith("\n"))
882  ret = ret.left(ret.length()-1);
883 
884  // add any padding as necessary
885  if (padding)
886  {
887  QString pad;
888  for (uint i = 0; i < padding; i++)
889  pad += " ";
890  ret = pad + ret.replace("\n", QString("\n%1").arg(pad));
891  }
892 
893  return ret;
894 }
UPnpDevice * FindDevice(const QString &sURI)
Definition: upnpdevice.cpp:580
QString LookupUDN(const QString &sDeviceType)
Definition: upnputil.cpp:45
void OutputDevice(QTextStream &os, UPnpDevice *pDevice, const QString &sUserAgent="")
Definition: upnpdevice.cpp:341
virtual int GetValue(const QString &sSetting, int Default)=0
NameValues * m_pAttributes
Definition: upnputil.h:58
UPnpDeviceDesc()=default
QHash< QString, QString > InfoMap
Definition: mythtypes.h:15
static UPnpDeviceDesc * Retrieve(QString &sURL)
Definition: upnpdevice.cpp:615
static void ProcessServiceList(const QDomNode &oListNode, UPnpDevice *pDevice)
Definition: upnpdevice.cpp:203
void toMap(InfoMap &map)
Definition: upnpdevice.cpp:768
bool download(const QString &url, const QString &dest, bool reload=false)
Downloads a URL to a file in blocking mode.
QString m_sModelURL
Definition: upnpdevice.h:110
UPnpServiceList m_listServices
Definition: upnpdevice.h:123
void InternalLoad(QDomNode oNode, UPnpDevice *pCurDevice)
Definition: upnpdevice.cpp:103
QString GetUDN(void) const
Definition: upnpdevice.cpp:760
QString m_sManufacturerURL
Definition: upnpdevice.h:106
QString GetHostName()
Definition: upnpdevice.cpp:660
QString m_sHostName
Definition: upnpdevice.h:153
QString FindDeviceUDN(UPnpDevice *pDevice, QString sST)
Definition: upnpdevice.cpp:541
UPnpDevice m_rootDevice
Definition: upnpdevice.h:152
static void SetBoolValue(const QDomNode &n, bool &nValue)
Definition: upnpdevice.cpp:284
MythDownloadManager * GetMythDownloadManager(void)
Gets the pointer to the MythDownloadManager singleton.
QString toString(uint padding=0) const
Definition: upnpdevice.cpp:814
#define MYTH_PROTO_VERSION
Increment this whenever the MythTV network protocol changes.
Definition: mythversion.h:48
QString m_sPresentationURL
Definition: upnpdevice.h:113
QString m_sUDN
Definition: upnpdevice.h:114
QString m_sName
Definition: upnputil.h:54
unsigned int uint
Definition: compat.h:140
static Configuration * GetConfiguration()
Definition: upnp.cpp:71
QString m_sModelNumber
Definition: upnpdevice.h:109
#define ENO
This can be appended to the LOG args with "+".
Definition: mythlogging.h:99
QString m_sUPC
Definition: upnpdevice.h:112
static void SetNumValue(const QDomNode &n, int &nValue)
Sets nValue param to n.firstChild().toText().nodeValue(), iff neither n.isNull() nor n....
Definition: upnpdevice.cpp:273
static QString FormatValue(const NameValue &node)
Definition: upnpdevice.cpp:500
NameValues m_lstExtra
Definition: upnpdevice.h:116
QString m_sModelDescription
Definition: upnpdevice.h:107
QString GetSetting(const QString &_key, const QString &defaultval)
Definition: mythdb.cpp:370
UPnpDeviceList m_listDevices
Definition: upnpdevice.h:124
static int g_nAllocated
Definition: upnpdevice.h:211
QString m_sDeviceType
Definition: upnpdevice.h:103
void ProcessDeviceList(const QDomNode &oListNode, UPnpDevice *pDevice)
Definition: upnpdevice.cpp:237
QString m_sModelName
Definition: upnpdevice.h:108
bool m_securityPin
MythTV specific information.
Definition: upnpdevice.h:119
QString m_sManufacturer
Definition: upnpdevice.h:105
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
QString m_sValue
Definition: upnputil.h:55
static void SetStrValue(const QDomNode &n, QString &sValue)
Sets sValue param to n.firstChild().toText().nodeValue(), iff neither n.isNull() nor n....
Definition: upnpdevice.cpp:260
UPnpIconList m_listIcons
Definition: upnpdevice.h:122
#define MYTH_BINARY_VERSION
Update this whenever the plug-in ABI changes.
Definition: mythversion.h:16
QString m_sFriendlyName
Definition: upnpdevice.h:104
bool Load(const QString &sFileName)
Definition: upnpdevice.cpp:43
MythDB * GetMythDB(void)
Definition: mythdb.cpp:46
void AddAttribute(const QString &name, const QString &value, bool required)
Definition: upnputil.h:128
UPnpService GetService(const QString &urn, bool *found=nullptr) const
Definition: upnpdevice.cpp:784
QString m_sSerialNumber
Definition: upnpdevice.h:111
void GetValidXML(const QString &sBaseAddress, int nPort, QTextStream &os, const QString &sUserAgent="")
Definition: upnpdevice.cpp:316
QString m_protocolVersion
Definition: upnpdevice.h:120
static void ProcessIconList(const QDomNode &oListNode, UPnpDevice *pDevice)
Definition: upnpdevice.cpp:174