ContentDirectoryService.cxx 5.24 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20 21
#include "lib/upnp/ContentDirectoryService.hxx"
#include "lib/upnp/ixmlwrap.hxx"
22
#include "lib/upnp/UniqueIxml.hxx"
23
#include "lib/upnp/Action.hxx"
24
#include "Directory.hxx"
25
#include "util/NumberParser.hxx"
26
#include "util/RuntimeError.hxx"
27
#include "util/ScopeExit.hxx"
28
#include "util/StringFormat.hxx"
29

30 31
static void
ReadResultTag(UPnPDirContent &dirbuf, IXML_Document *response)
32 33 34 35 36
{
	const char *p = ixmlwrap::getFirstElementValue(response, "Result");
	if (p == nullptr)
		p = "";

37
	dirbuf.Parse(p);
38 39
}

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

61 62
	AtScopeExit(request) { ixmlDocument_free(request); };

63
	IXML_Document *response;
64
	int code = UpnpSendAction(hdl, m_actionURL.c_str(), m_serviceType.c_str(),
65
				  nullptr /*devUDN*/, request, &response);
66 67 68
	if (code != UPNP_E_SUCCESS)
		throw FormatRuntimeError("UpnpSendAction() failed: %s",
					 UpnpGetErrorMessage(code));
69

70 71
	AtScopeExit(response) { ixmlDocument_free(response); };

72
	const char *value = ixmlwrap::getFirstElementValue(response, "NumberReturned");
73 74 75
	didreadp = value != nullptr
		? ParseUnsigned(value)
		: 0;
76

77 78
	value = ixmlwrap::getFirstElementValue(response, "TotalMatches");
	if (value != nullptr)
79
		totalp = ParseUnsigned(value);
80

81
	ReadResultTag(dirbuf, response);
82 83
}

84
UPnPDirContent
85
ContentDirectoryService::readDir(UpnpClient_Handle handle,
86
				 const char *objectId) const
87
{
88
	UPnPDirContent dirbuf;
89
	unsigned offset = 0, total = -1, count;
90

91
	do {
92 93
		readDirSlice(handle, objectId, offset, m_rdreqcnt, dirbuf,
			     count, total);
94 95

		offset += count;
96
	} while (count > 0 && offset < total);
97

98
	return dirbuf;
99 100
}

101
UPnPDirContent
102 103
ContentDirectoryService::search(UpnpClient_Handle hdl,
				const char *objectId,
104
				const char *ss) const
105
{
106
	UPnPDirContent dirbuf;
107
	unsigned offset = 0, total = -1, count;
108

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

121
		IXML_Document *_response;
122 123
		auto code = UpnpSendAction(hdl, m_actionURL.c_str(),
					   m_serviceType.c_str(),
124
					   nullptr /*devUDN*/,
125
					   request.get(), &_response);
126 127 128
		if (code != UPNP_E_SUCCESS)
			throw FormatRuntimeError("UpnpSendAction() failed: %s",
						 UpnpGetErrorMessage(code));
129

130 131
		UniqueIxmlDocument response(_response);

132
		const char *value =
133 134
			ixmlwrap::getFirstElementValue(response.get(),
						       "NumberReturned");
135
		count = value != nullptr
136 137
			? ParseUnsigned(value)
			: 0;
138 139 140

		offset += count;

141 142
		value = ixmlwrap::getFirstElementValue(response.get(),
						       "TotalMatches");
143
		if (value != nullptr)
144
			total = ParseUnsigned(value);
145

146
		ReadResultTag(dirbuf, response.get());
147
	} while (count > 0 && offset < total);
148

149
	return dirbuf;
150 151
}

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

167
	IXML_Document *_response;
168 169
	auto code = UpnpSendAction(hdl, m_actionURL.c_str(),
				   m_serviceType.c_str(),
170
				   nullptr /*devUDN*/, request.get(), &_response);
171 172 173
	if (code != UPNP_E_SUCCESS)
		throw FormatRuntimeError("UpnpSendAction() failed: %s",
					 UpnpGetErrorMessage(code));
174

175
	UniqueIxmlDocument response(_response);
176 177 178
	UPnPDirContent dirbuf;
	ReadResultTag(dirbuf, response.get());
	return dirbuf;
179
}