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 "ContentDirectoryService.hxx"
|
|
|
|
#include "Domain.hxx"
|
|
|
|
#include "Device.hxx"
|
|
|
|
#include "ixmlwrap.hxx"
|
|
|
|
#include "Directory.hxx"
|
|
|
|
#include "Util.hxx"
|
2014-01-18 15:27:54 +01:00
|
|
|
#include "Action.hxx"
|
2014-01-22 21:01:05 +01:00
|
|
|
#include "util/NumberParser.hxx"
|
2013-11-01 19:26:01 +01:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
|
2014-01-22 19:49:02 +01:00
|
|
|
#include <string.h>
|
2013-11-01 19:26:01 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
ContentDirectoryService::ContentDirectoryService(const UPnPDevice &device,
|
|
|
|
const UPnPService &service)
|
|
|
|
:m_actionURL(caturl(device.URLBase, service.controlURL)),
|
|
|
|
m_serviceType(service.serviceType),
|
|
|
|
m_deviceId(device.UDN),
|
|
|
|
m_friendlyName(device.friendlyName),
|
|
|
|
m_manufacturer(device.manufacturer),
|
|
|
|
m_modelName(device.modelName),
|
|
|
|
m_rdreqcnt(200)
|
|
|
|
{
|
|
|
|
if (!m_modelName.compare("MediaTomb")) {
|
|
|
|
// Readdir by 200 entries is good for most, but MediaTomb likes
|
|
|
|
// them really big. Actually 1000 is better but I don't dare
|
|
|
|
m_rdreqcnt = 500;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-23 21:17:40 +01:00
|
|
|
ContentDirectoryService::~ContentDirectoryService()
|
|
|
|
{
|
|
|
|
/* this destructor exists here just so it won't get inlined */
|
|
|
|
}
|
|
|
|
|
2014-01-18 15:01:19 +01:00
|
|
|
static bool
|
|
|
|
ReadResultTag(UPnPDirContent &dirbuf, IXML_Document *response, Error &error)
|
|
|
|
{
|
|
|
|
const char *p = ixmlwrap::getFirstElementValue(response, "Result");
|
|
|
|
if (p == nullptr)
|
|
|
|
p = "";
|
|
|
|
|
|
|
|
return dirbuf.parse(p, error);
|
|
|
|
}
|
|
|
|
|
2014-01-22 20:42:36 +01:00
|
|
|
inline bool
|
2014-01-16 09:06:01 +01:00
|
|
|
ContentDirectoryService::readDirSlice(UpnpClient_Handle hdl,
|
2014-01-22 20:44:24 +01:00
|
|
|
const char *objectId, unsigned offset,
|
|
|
|
unsigned count, UPnPDirContent &dirbuf,
|
|
|
|
unsigned &didreadp, unsigned &totalp,
|
2014-01-22 23:50:33 +01:00
|
|
|
Error &error) const
|
2013-11-01 19:26:01 +01:00
|
|
|
{
|
|
|
|
// Create request
|
|
|
|
char ofbuf[100], cntbuf[100];
|
2014-01-22 20:44:24 +01:00
|
|
|
sprintf(ofbuf, "%u", offset);
|
|
|
|
sprintf(cntbuf, "%u", count);
|
2013-11-01 19:26:01 +01:00
|
|
|
// Some devices require an empty SortCriteria, else bad params
|
2014-01-18 14:38:52 +01:00
|
|
|
IXML_Document *request =
|
2014-01-18 15:27:54 +01:00
|
|
|
MakeActionHelper("Browse", m_serviceType.c_str(),
|
|
|
|
"ObjectID", objectId,
|
|
|
|
"BrowseFlag", "BrowseDirectChildren",
|
|
|
|
"Filter", "*",
|
|
|
|
"SortCriteria", "",
|
|
|
|
"StartingIndex", ofbuf,
|
|
|
|
"RequestedCount", cntbuf);
|
2013-11-01 19:26:01 +01:00
|
|
|
if (request == nullptr) {
|
|
|
|
error.Set(upnp_domain, "UpnpMakeAction() failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-18 14:38:52 +01:00
|
|
|
IXML_Document *response;
|
2013-11-01 19:26:01 +01:00
|
|
|
int code = UpnpSendAction(hdl, m_actionURL.c_str(), m_serviceType.c_str(),
|
|
|
|
0 /*devUDN*/, request, &response);
|
2014-01-18 14:38:52 +01:00
|
|
|
ixmlDocument_free(request);
|
2013-11-01 19:26:01 +01:00
|
|
|
if (code != UPNP_E_SUCCESS) {
|
|
|
|
error.Format(upnp_domain, code,
|
|
|
|
"UpnpSendAction() failed: %s",
|
|
|
|
UpnpGetErrorMessage(code));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-18 15:01:19 +01:00
|
|
|
const char *value = ixmlwrap::getFirstElementValue(response, "NumberReturned");
|
2014-01-22 21:01:05 +01:00
|
|
|
didreadp = value != nullptr
|
|
|
|
? ParseUnsigned(value)
|
|
|
|
: 0;
|
2013-11-01 19:26:01 +01:00
|
|
|
|
2014-01-18 15:01:19 +01:00
|
|
|
value = ixmlwrap::getFirstElementValue(response, "TotalMatches");
|
|
|
|
if (value != nullptr)
|
2014-01-22 21:01:05 +01:00
|
|
|
totalp = ParseUnsigned(value);
|
2013-11-01 19:26:01 +01:00
|
|
|
|
2014-01-22 21:09:05 +01:00
|
|
|
bool success = ReadResultTag(dirbuf, response, error);
|
|
|
|
ixmlDocument_free(response);
|
|
|
|
return success;
|
2013-11-01 19:26:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-01-16 09:06:01 +01:00
|
|
|
ContentDirectoryService::readDir(UpnpClient_Handle handle,
|
|
|
|
const char *objectId,
|
2013-11-01 19:26:01 +01:00
|
|
|
UPnPDirContent &dirbuf,
|
2014-01-22 23:50:33 +01:00
|
|
|
Error &error) const
|
2013-11-01 19:26:01 +01:00
|
|
|
{
|
2014-01-22 21:03:45 +01:00
|
|
|
unsigned offset = 0, total = -1, count;
|
2013-11-01 19:26:01 +01:00
|
|
|
|
2014-01-22 21:03:45 +01:00
|
|
|
do {
|
2014-01-16 09:06:01 +01:00
|
|
|
if (!readDirSlice(handle, objectId, offset, m_rdreqcnt, dirbuf,
|
2014-01-22 20:44:24 +01:00
|
|
|
count, total, error))
|
2013-11-01 19:26:01 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
offset += count;
|
2014-01-22 21:03:45 +01:00
|
|
|
} while (count > 0 && offset < total);
|
2013-11-01 19:26:01 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-01-16 09:06:01 +01:00
|
|
|
ContentDirectoryService::search(UpnpClient_Handle hdl,
|
|
|
|
const char *objectId,
|
2013-11-01 19:26:01 +01:00
|
|
|
const char *ss,
|
|
|
|
UPnPDirContent &dirbuf,
|
2014-01-22 23:50:33 +01:00
|
|
|
Error &error) const
|
2013-11-01 19:26:01 +01:00
|
|
|
{
|
2014-01-22 21:03:45 +01:00
|
|
|
unsigned offset = 0, total = -1, count;
|
2013-11-01 19:26:01 +01:00
|
|
|
|
2014-01-22 21:03:45 +01:00
|
|
|
do {
|
2013-11-01 19:26:01 +01:00
|
|
|
char ofbuf[100];
|
|
|
|
sprintf(ofbuf, "%d", offset);
|
2014-01-18 14:38:52 +01:00
|
|
|
|
|
|
|
IXML_Document *request =
|
2014-01-18 15:27:54 +01:00
|
|
|
MakeActionHelper("Search", m_serviceType.c_str(),
|
|
|
|
"ContainerID", objectId,
|
|
|
|
"SearchCriteria", ss,
|
|
|
|
"Filter", "*",
|
|
|
|
"SortCriteria", "",
|
|
|
|
"StartingIndex", ofbuf,
|
|
|
|
"RequestedCount", "0"); // Setting a value here gets twonky into fits
|
2013-11-01 19:26:01 +01:00
|
|
|
if (request == 0) {
|
|
|
|
error.Set(upnp_domain, "UpnpMakeAction() failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-18 14:38:52 +01:00
|
|
|
IXML_Document *response;
|
2013-11-01 19:26:01 +01:00
|
|
|
auto code = UpnpSendAction(hdl, m_actionURL.c_str(),
|
|
|
|
m_serviceType.c_str(),
|
|
|
|
0 /*devUDN*/, request, &response);
|
2014-01-18 14:38:52 +01:00
|
|
|
ixmlDocument_free(request);
|
2013-11-01 19:26:01 +01:00
|
|
|
if (code != UPNP_E_SUCCESS) {
|
|
|
|
error.Format(upnp_domain, code,
|
|
|
|
"UpnpSendAction() failed: %s",
|
|
|
|
UpnpGetErrorMessage(code));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-18 15:01:19 +01:00
|
|
|
const char *value =
|
2013-11-01 19:26:01 +01:00
|
|
|
ixmlwrap::getFirstElementValue(response, "NumberReturned");
|
2014-01-22 21:03:45 +01:00
|
|
|
count = value != nullptr
|
2014-01-22 21:01:05 +01:00
|
|
|
? ParseUnsigned(value)
|
|
|
|
: 0;
|
2013-11-01 19:26:01 +01:00
|
|
|
|
|
|
|
offset += count;
|
|
|
|
|
2014-01-18 15:01:19 +01:00
|
|
|
value = ixmlwrap::getFirstElementValue(response, "TotalMatches");
|
|
|
|
if (value != nullptr)
|
2014-01-22 21:01:05 +01:00
|
|
|
total = ParseUnsigned(value);
|
2013-11-01 19:26:01 +01:00
|
|
|
|
2014-01-22 21:09:05 +01:00
|
|
|
bool success = ReadResultTag(dirbuf, response, error);
|
|
|
|
ixmlDocument_free(response);
|
|
|
|
if (!success)
|
2013-11-01 19:26:01 +01:00
|
|
|
return false;
|
2014-01-22 21:03:45 +01:00
|
|
|
} while (count > 0 && offset < total);
|
2013-11-01 19:26:01 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-01-16 09:06:01 +01:00
|
|
|
ContentDirectoryService::getSearchCapabilities(UpnpClient_Handle hdl,
|
2014-01-22 21:41:05 +01:00
|
|
|
std::list<std::string> &result,
|
2014-01-22 23:50:33 +01:00
|
|
|
Error &error) const
|
2013-11-01 19:26:01 +01:00
|
|
|
{
|
2014-01-22 20:21:01 +01:00
|
|
|
assert(result.empty());
|
|
|
|
|
2014-01-18 14:38:52 +01:00
|
|
|
IXML_Document *request =
|
|
|
|
UpnpMakeAction("GetSearchCapabilities", m_serviceType.c_str(),
|
|
|
|
0,
|
|
|
|
nullptr, nullptr);
|
2013-11-01 19:26:01 +01:00
|
|
|
if (request == 0) {
|
|
|
|
error.Set(upnp_domain, "UpnpMakeAction() failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-18 14:38:52 +01:00
|
|
|
IXML_Document *response;
|
2013-11-01 19:26:01 +01:00
|
|
|
auto code = UpnpSendAction(hdl, m_actionURL.c_str(),
|
|
|
|
m_serviceType.c_str(),
|
|
|
|
0 /*devUDN*/, request, &response);
|
2014-01-18 14:38:52 +01:00
|
|
|
ixmlDocument_free(request);
|
2013-11-01 19:26:01 +01:00
|
|
|
if (code != UPNP_E_SUCCESS) {
|
|
|
|
error.Format(upnp_domain, code,
|
|
|
|
"UpnpSendAction() failed: %s",
|
|
|
|
UpnpGetErrorMessage(code));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-22 19:49:02 +01:00
|
|
|
const char *s = ixmlwrap::getFirstElementValue(response, "SearchCaps");
|
|
|
|
if (s == nullptr || *s == 0) {
|
|
|
|
ixmlDocument_free(response);
|
|
|
|
return true;
|
|
|
|
}
|
2013-11-01 19:26:01 +01:00
|
|
|
|
2014-01-22 19:49:02 +01:00
|
|
|
bool success = true;
|
2014-01-22 20:37:17 +01:00
|
|
|
if (!csvToStrings(s, result)) {
|
2014-01-22 19:49:02 +01:00
|
|
|
error.Set(upnp_domain, "Bad response");
|
|
|
|
success = false;
|
2013-11-01 19:26:01 +01:00
|
|
|
}
|
|
|
|
|
2014-01-22 19:49:02 +01:00
|
|
|
ixmlDocument_free(response);
|
|
|
|
return success;
|
2013-11-01 19:26:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-01-16 09:06:01 +01:00
|
|
|
ContentDirectoryService::getMetadata(UpnpClient_Handle hdl,
|
|
|
|
const char *objectId,
|
2013-11-01 19:26:01 +01:00
|
|
|
UPnPDirContent &dirbuf,
|
2014-01-22 23:50:33 +01:00
|
|
|
Error &error) const
|
2013-11-01 19:26:01 +01:00
|
|
|
{
|
|
|
|
// Create request
|
|
|
|
IXML_Document *request =
|
2014-01-18 15:27:54 +01:00
|
|
|
MakeActionHelper("Browse", m_serviceType.c_str(),
|
|
|
|
"ObjectID", objectId,
|
|
|
|
"BrowseFlag", "BrowseMetadata",
|
|
|
|
"Filter", "*",
|
|
|
|
"SortCriteria", "",
|
|
|
|
"StartingIndex", "0",
|
|
|
|
"RequestedCount", "1");
|
2013-11-01 19:26:01 +01:00
|
|
|
if (request == nullptr) {
|
|
|
|
error.Set(upnp_domain, "UpnpMakeAction() failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-18 14:38:52 +01:00
|
|
|
IXML_Document *response;
|
2013-11-01 19:26:01 +01:00
|
|
|
auto code = UpnpSendAction(hdl, m_actionURL.c_str(),
|
|
|
|
m_serviceType.c_str(),
|
|
|
|
0 /*devUDN*/, request, &response);
|
2014-01-18 14:38:52 +01:00
|
|
|
ixmlDocument_free(request);
|
2013-11-01 19:26:01 +01:00
|
|
|
if (code != UPNP_E_SUCCESS) {
|
|
|
|
error.Format(upnp_domain, code,
|
|
|
|
"UpnpSendAction() failed: %s",
|
|
|
|
UpnpGetErrorMessage(code));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-18 15:01:19 +01:00
|
|
|
bool success = ReadResultTag(dirbuf, response, error);
|
2014-01-18 14:38:52 +01:00
|
|
|
ixmlDocument_free(response);
|
2014-01-18 15:01:19 +01:00
|
|
|
return success;
|
2013-11-01 19:26:01 +01:00
|
|
|
}
|