2013-11-01 19:26:01 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2014 The Music Player Daemon Project
|
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "Device.hxx"
|
|
|
|
#include "Util.hxx"
|
|
|
|
#include "Expat.hxx"
|
|
|
|
#include "Log.hxx"
|
|
|
|
#include "util/Error.hxx"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An XML parser which constructs an UPnP device object from the
|
|
|
|
* device descriptor.
|
|
|
|
*/
|
|
|
|
class UPnPDeviceParser final : public CommonExpatParser {
|
|
|
|
UPnPDevice &m_device;
|
|
|
|
std::vector<std::string> m_path;
|
|
|
|
UPnPService m_tservice;
|
|
|
|
|
|
|
|
public:
|
|
|
|
UPnPDeviceParser(UPnPDevice& device)
|
|
|
|
:m_device(device) {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void StartElement(const XML_Char *name, const XML_Char **) {
|
|
|
|
m_path.push_back(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void EndElement(const XML_Char *name) {
|
|
|
|
if (!strcmp(name, "service")) {
|
|
|
|
m_device.services.push_back(m_tservice);
|
|
|
|
m_tservice.clear();
|
|
|
|
}
|
2014-01-10 22:14:27 +01:00
|
|
|
|
2013-11-01 19:26:01 +01:00
|
|
|
m_path.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void CharacterData(const XML_Char *s, int len) {
|
2014-01-18 13:36:50 +01:00
|
|
|
std::string str = trimstring(s, len);
|
2013-11-01 19:26:01 +01:00
|
|
|
switch (m_path.back()[0]) {
|
|
|
|
case 'c':
|
|
|
|
if (!m_path.back().compare("controlURL"))
|
2014-01-18 13:39:31 +01:00
|
|
|
m_tservice.controlURL = std::move(str);
|
2013-11-01 19:26:01 +01:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
if (!m_path.back().compare("deviceType"))
|
2014-01-18 13:39:31 +01:00
|
|
|
m_device.deviceType = std::move(str);
|
2013-11-01 19:26:01 +01:00
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
if (!m_path.back().compare("eventSubURL"))
|
2014-01-18 13:39:31 +01:00
|
|
|
m_tservice.eventSubURL = std::move(str);
|
2013-11-01 19:26:01 +01:00
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
if (!m_path.back().compare("friendlyName"))
|
2014-01-18 13:39:31 +01:00
|
|
|
m_device.friendlyName = std::move(str);
|
2013-11-01 19:26:01 +01:00
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
if (!m_path.back().compare("manufacturer"))
|
2014-01-18 13:39:31 +01:00
|
|
|
m_device.manufacturer = std::move(str);
|
2013-11-01 19:26:01 +01:00
|
|
|
else if (!m_path.back().compare("modelName"))
|
2014-01-18 13:39:31 +01:00
|
|
|
m_device.modelName = std::move(str);
|
2013-11-01 19:26:01 +01:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
if (!m_path.back().compare("serviceType"))
|
2014-01-18 13:39:31 +01:00
|
|
|
m_tservice.serviceType = std::move(str);
|
2013-11-01 19:26:01 +01:00
|
|
|
else if (!m_path.back().compare("serviceId"))
|
2014-01-18 13:39:31 +01:00
|
|
|
m_tservice.serviceId = std::move(str);
|
2013-11-01 19:26:01 +01:00
|
|
|
case 'S':
|
|
|
|
if (!m_path.back().compare("SCPDURL"))
|
2014-01-18 13:39:31 +01:00
|
|
|
m_tservice.SCPDURL = std::move(str);
|
2013-11-01 19:26:01 +01:00
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
if (!m_path.back().compare("UDN"))
|
2014-01-18 13:39:31 +01:00
|
|
|
m_device.UDN = std::move(str);
|
2013-11-01 19:26:01 +01:00
|
|
|
else if (!m_path.back().compare("URLBase"))
|
2014-01-18 13:39:31 +01:00
|
|
|
m_device.URLBase = std::move(str);
|
2013-11-01 19:26:01 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-18 14:24:38 +01:00
|
|
|
UPnPDevice::UPnPDevice(const std::string &url, const char *description)
|
2013-11-01 19:26:01 +01:00
|
|
|
:ok(false)
|
|
|
|
{
|
|
|
|
UPnPDeviceParser mparser(*this);
|
|
|
|
Error error;
|
2014-01-18 14:24:38 +01:00
|
|
|
if (!mparser.Parse(description, strlen(description), true,
|
2013-11-01 19:26:01 +01:00
|
|
|
error)) {
|
|
|
|
// TODO: pass Error to caller
|
|
|
|
LogError(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (URLBase.empty()) {
|
|
|
|
// The standard says that if the URLBase value is empty, we should use
|
|
|
|
// the url the description was retrieved from. However this is
|
|
|
|
// sometimes something like http://host/desc.xml, sometimes something
|
|
|
|
// like http://host/
|
|
|
|
|
|
|
|
if (url.size() < 8) {
|
|
|
|
// ???
|
|
|
|
URLBase = url;
|
|
|
|
} else {
|
|
|
|
auto hostslash = url.find_first_of("/", 7);
|
|
|
|
if (hostslash == std::string::npos || hostslash == url.size()-1) {
|
|
|
|
URLBase = url;
|
|
|
|
} else {
|
|
|
|
URLBase = path_getfather(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ok = true;
|
|
|
|
}
|