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"
|
|
|
|
#include "util/Error.hxx"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <upnp/upnptools.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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class DirBResFree {
|
|
|
|
public:
|
2014-01-18 14:38:52 +01:00
|
|
|
IXML_Document **rspp;
|
|
|
|
DirBResFree(IXML_Document **_rspp)
|
|
|
|
:rspp(_rspp) {}
|
2013-11-01 19:26:01 +01:00
|
|
|
~DirBResFree()
|
|
|
|
{
|
|
|
|
if (*rspp)
|
|
|
|
ixmlDocument_free(*rspp);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-11-01 19:26:01 +01:00
|
|
|
bool
|
2014-01-16 09:06:01 +01:00
|
|
|
ContentDirectoryService::readDirSlice(UpnpClient_Handle hdl,
|
|
|
|
const char *objectId, int offset,
|
2013-11-01 19:26:01 +01:00
|
|
|
int count, UPnPDirContent &dirbuf,
|
|
|
|
int *didreadp, int *totalp,
|
|
|
|
Error &error)
|
|
|
|
{
|
|
|
|
// Create request
|
|
|
|
char ofbuf[100], cntbuf[100];
|
|
|
|
sprintf(ofbuf, "%d", offset);
|
|
|
|
sprintf(cntbuf, "%d", count);
|
|
|
|
int argcnt = 6;
|
|
|
|
// Some devices require an empty SortCriteria, else bad params
|
2014-01-18 14:38:52 +01:00
|
|
|
IXML_Document *request =
|
|
|
|
UpnpMakeAction("Browse", m_serviceType.c_str(), argcnt,
|
|
|
|
"ObjectID", objectId,
|
|
|
|
"BrowseFlag", "BrowseDirectChildren",
|
|
|
|
"Filter", "*",
|
|
|
|
"SortCriteria", "",
|
|
|
|
"StartingIndex", ofbuf,
|
|
|
|
"RequestedCount", cntbuf,
|
|
|
|
nullptr, nullptr);
|
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 14:38:52 +01:00
|
|
|
DirBResFree cleaner(&response);
|
|
|
|
|
2013-11-01 19:26:01 +01:00
|
|
|
int didread = -1;
|
2014-01-18 15:01:19 +01:00
|
|
|
const char *value = ixmlwrap::getFirstElementValue(response, "NumberReturned");
|
|
|
|
if (value != nullptr)
|
|
|
|
didread = atoi(value);
|
2013-11-01 19:26:01 +01:00
|
|
|
|
|
|
|
if (count == -1 || count == 0) {
|
|
|
|
// TODO: what's this?
|
|
|
|
error.Set(upnp_domain, "got -1 or 0 entries");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-18 15:01:19 +01:00
|
|
|
value = ixmlwrap::getFirstElementValue(response, "TotalMatches");
|
|
|
|
if (value != nullptr)
|
|
|
|
*totalp = atoi(value);
|
2013-11-01 19:26:01 +01:00
|
|
|
|
2014-01-18 15:01:19 +01:00
|
|
|
if (!ReadResultTag(dirbuf, response, error))
|
2013-11-01 19:26:01 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
*didreadp = didread;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
Error &error)
|
|
|
|
{
|
|
|
|
int offset = 0;
|
|
|
|
int total = 1000;// Updated on first read.
|
|
|
|
|
|
|
|
while (offset < total) {
|
|
|
|
int count;
|
2014-01-16 09:06:01 +01:00
|
|
|
if (!readDirSlice(handle, objectId, offset, m_rdreqcnt, dirbuf,
|
2013-11-01 19:26:01 +01:00
|
|
|
&count, &total, error))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
offset += count;
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
Error &error)
|
|
|
|
{
|
|
|
|
int offset = 0;
|
|
|
|
int total = 1000;// Updated on first read.
|
|
|
|
|
|
|
|
while (offset < total) {
|
|
|
|
char ofbuf[100];
|
|
|
|
sprintf(ofbuf, "%d", offset);
|
|
|
|
// Create request
|
|
|
|
int argcnt = 6;
|
2014-01-18 14:38:52 +01:00
|
|
|
|
|
|
|
IXML_Document *request =
|
|
|
|
UpnpMakeAction("Search", m_serviceType.c_str(), argcnt,
|
|
|
|
"ContainerID", objectId,
|
|
|
|
"SearchCriteria", ss,
|
|
|
|
"Filter", "*",
|
|
|
|
"SortCriteria", "",
|
|
|
|
"StartingIndex", ofbuf,
|
|
|
|
"RequestedCount", "0", // Setting a value here gets twonky into fits
|
|
|
|
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-18 14:38:52 +01:00
|
|
|
DirBResFree cleaner(&response);
|
|
|
|
|
2013-11-01 19:26:01 +01:00
|
|
|
int count = -1;
|
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-18 15:01:19 +01:00
|
|
|
if (value != nullptr)
|
|
|
|
count = atoi(value);
|
2013-11-01 19:26:01 +01:00
|
|
|
|
|
|
|
if (count == -1 || count == 0) {
|
|
|
|
// TODO: what's this?
|
|
|
|
error.Set(upnp_domain, "got -1 or 0 entries");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += count;
|
|
|
|
|
2014-01-18 15:01:19 +01:00
|
|
|
value = ixmlwrap::getFirstElementValue(response, "TotalMatches");
|
|
|
|
if (value != nullptr)
|
|
|
|
total = atoi(value);
|
2013-11-01 19:26:01 +01:00
|
|
|
|
2014-01-18 15:01:19 +01:00
|
|
|
if (!ReadResultTag(dirbuf, response, error))
|
2013-11-01 19:26:01 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-01-16 09:06:01 +01:00
|
|
|
ContentDirectoryService::getSearchCapabilities(UpnpClient_Handle hdl,
|
|
|
|
std::set<std::string> &result,
|
2013-11-01 19:26:01 +01:00
|
|
|
Error &error)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string tbuf = ixmlwrap::getFirstElementValue(response, "SearchCaps");
|
2014-01-18 14:38:52 +01:00
|
|
|
ixmlDocument_free(response);
|
2013-11-01 19:26:01 +01:00
|
|
|
|
|
|
|
result.clear();
|
|
|
|
if (!tbuf.compare("*")) {
|
|
|
|
result.insert(result.end(), "*");
|
|
|
|
} else if (!tbuf.empty()) {
|
|
|
|
if (!csvToStrings(tbuf, result)) {
|
|
|
|
error.Set(upnp_domain, "Bad response");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
Error &error)
|
|
|
|
{
|
|
|
|
// Create request
|
|
|
|
int argcnt = 6;
|
|
|
|
IXML_Document *request =
|
|
|
|
UpnpMakeAction("Browse", m_serviceType.c_str(), argcnt,
|
|
|
|
"ObjectID", objectId,
|
|
|
|
"BrowseFlag", "BrowseMetadata",
|
|
|
|
"Filter", "*",
|
|
|
|
"SortCriteria", "",
|
|
|
|
"StartingIndex", "0",
|
|
|
|
"RequestedCount", "1",
|
|
|
|
nullptr, nullptr);
|
|
|
|
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
|
|
|
}
|