2013-11-01 19:26:01 +01:00
|
|
|
/*
|
2015-01-01 19:48:13 +01:00
|
|
|
* Copyright (C) 2003-2015 The Music Player Daemon Project
|
2013-11-01 19:26:01 +01:00
|
|
|
* 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"
|
2014-01-26 11:51:56 +01:00
|
|
|
#include "lib/upnp/ContentDirectoryService.hxx"
|
|
|
|
#include "lib/upnp/Domain.hxx"
|
|
|
|
#include "lib/upnp/ixmlwrap.hxx"
|
|
|
|
#include "lib/upnp/Action.hxx"
|
2013-11-01 19:26:01 +01:00
|
|
|
#include "Directory.hxx"
|
2014-01-22 21:01:05 +01:00
|
|
|
#include "util/NumberParser.hxx"
|
2014-01-26 11:37:52 +01:00
|
|
|
#include "util/UriUtil.hxx"
|
2013-11-01 19:26:01 +01:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
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::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
|
|
|
}
|