ContentDirectoryService.cxx 5.29 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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"
21 22
#include "lib/upnp/ContentDirectoryService.hxx"
#include "lib/upnp/ixmlwrap.hxx"
23
#include "lib/upnp/UniqueIxml.hxx"
24
#include "lib/upnp/Action.hxx"
25
#include "Directory.hxx"
26
#include "util/NumberParser.hxx"
27
#include "util/UriUtil.hxx"
28
#include "util/RuntimeError.hxx"
29
#include "util/ScopeExit.hxx"
30
#include "util/StringFormat.hxx"
31 32 33

#include <stdio.h>

34 35
static void
ReadResultTag(UPnPDirContent &dirbuf, IXML_Document *response)
36 37 38 39 40
{
	const char *p = ixmlwrap::getFirstElementValue(response, "Result");
	if (p == nullptr)
		p = "";

41
	dirbuf.Parse(p);
42 43
}

44
inline void
45
ContentDirectoryService::readDirSlice(UpnpClient_Handle hdl,
46 47
				      const char *objectId, unsigned offset,
				      unsigned count, UPnPDirContent &dirbuf,
48 49
				      unsigned &didreadp,
				      unsigned &totalp) const
50 51
{
	// Some devices require an empty SortCriteria, else bad params
52
	IXML_Document *request =
53 54 55 56 57
		MakeActionHelper("Browse", m_serviceType.c_str(),
				 "ObjectID", objectId,
				 "BrowseFlag", "BrowseDirectChildren",
				 "Filter", "*",
				 "SortCriteria", "",
58 59 60 61
				 "StartingIndex",
				 StringFormat<32>("%u", offset).c_str(),
				 "RequestedCount",
				 StringFormat<32>("%u", count).c_str());
62 63
	if (request == nullptr)
		throw std::runtime_error("UpnpMakeAction() failed");
64

65 66
	AtScopeExit(request) { ixmlDocument_free(request); };

67
	IXML_Document *response;
68 69
	int code = UpnpSendAction(hdl, m_actionURL.c_str(), m_serviceType.c_str(),
				  0 /*devUDN*/, request, &response);
70 71 72
	if (code != UPNP_E_SUCCESS)
		throw FormatRuntimeError("UpnpSendAction() failed: %s",
					 UpnpGetErrorMessage(code));
73

74 75
	AtScopeExit(response) { ixmlDocument_free(response); };

76
	const char *value = ixmlwrap::getFirstElementValue(response, "NumberReturned");
77 78 79
	didreadp = value != nullptr
		? ParseUnsigned(value)
		: 0;
80

81 82
	value = ixmlwrap::getFirstElementValue(response, "TotalMatches");
	if (value != nullptr)
83
		totalp = ParseUnsigned(value);
84

85
	ReadResultTag(dirbuf, response);
86 87
}

88
UPnPDirContent
89
ContentDirectoryService::readDir(UpnpClient_Handle handle,
90
				 const char *objectId) const
91
{
92
	UPnPDirContent dirbuf;
93
	unsigned offset = 0, total = -1, count;
94

95
	do {
96 97
		readDirSlice(handle, objectId, offset, m_rdreqcnt, dirbuf,
			     count, total);
98 99

		offset += count;
100
	} while (count > 0 && offset < total);
101

102
	return dirbuf;
103 104
}

105
UPnPDirContent
106 107
ContentDirectoryService::search(UpnpClient_Handle hdl,
				const char *objectId,
108
				const char *ss) const
109
{
110
	UPnPDirContent dirbuf;
111
	unsigned offset = 0, total = -1, count;
112

113
	do {
114 115 116 117 118
		UniqueIxmlDocument request(MakeActionHelper("Search", m_serviceType.c_str(),
							    "ContainerID", objectId,
							    "SearchCriteria", ss,
							    "Filter", "*",
							    "SortCriteria", "",
119 120
							    "StartingIndex",
							    StringFormat<32>("%u", offset).c_str(),
121 122
							    "RequestedCount", "0")); // Setting a value here gets twonky into fits
		if (!request)
123
			throw std::runtime_error("UpnpMakeAction() failed");
124

125
		IXML_Document *_response;
126 127
		auto code = UpnpSendAction(hdl, m_actionURL.c_str(),
					   m_serviceType.c_str(),
128 129
					   0 /*devUDN*/,
					   request.get(), &_response);
130 131 132
		if (code != UPNP_E_SUCCESS)
			throw FormatRuntimeError("UpnpSendAction() failed: %s",
						 UpnpGetErrorMessage(code));
133

134 135
		UniqueIxmlDocument response(_response);

136
		const char *value =
137 138
			ixmlwrap::getFirstElementValue(response.get(),
						       "NumberReturned");
139
		count = value != nullptr
140 141
			? ParseUnsigned(value)
			: 0;
142 143 144

		offset += count;

145 146
		value = ixmlwrap::getFirstElementValue(response.get(),
						       "TotalMatches");
147
		if (value != nullptr)
148
			total = ParseUnsigned(value);
149

150
		ReadResultTag(dirbuf, response.get());
151
	} while (count > 0 && offset < total);
152

153
	return dirbuf;
154 155
}

156
UPnPDirContent
157
ContentDirectoryService::getMetadata(UpnpClient_Handle hdl,
158
				     const char *objectId) const
159 160
{
	// Create request
161 162 163 164 165 166 167
	UniqueIxmlDocument request(MakeActionHelper("Browse", m_serviceType.c_str(),
						    "ObjectID", objectId,
						    "BrowseFlag", "BrowseMetadata",
						    "Filter", "*",
						    "SortCriteria", "",
						    "StartingIndex", "0",
						    "RequestedCount", "1"));
168 169
	if (request == nullptr)
		throw std::runtime_error("UpnpMakeAction() failed");
170

171
	IXML_Document *_response;
172 173
	auto code = UpnpSendAction(hdl, m_actionURL.c_str(),
				   m_serviceType.c_str(),
174
				   0 /*devUDN*/, request.get(), &_response);
175 176 177
	if (code != UPNP_E_SUCCESS)
		throw FormatRuntimeError("UpnpSendAction() failed: %s",
					 UpnpGetErrorMessage(code));
178

179
	UniqueIxmlDocument response(_response);
180 181 182
	UPnPDirContent dirbuf;
	ReadResultTag(dirbuf, response.get());
	return dirbuf;
183
}