ContentDirectoryService.cxx 5.27 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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/UriUtil.hxx"
27
#include "util/RuntimeError.hxx"
28
#include "util/ScopeExit.hxx"
29
#include "util/StringFormat.hxx"
30 31 32

#include <stdio.h>

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

40
	dirbuf.Parse(p);
41 42
}

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

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

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

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

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

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

84
	ReadResultTag(dirbuf, response);
85 86
}

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

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

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

101
	return dirbuf;
102 103
}

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

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

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

133 134
		UniqueIxmlDocument response(_response);

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

		offset += count;

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

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

152
	return dirbuf;
153 154
}

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

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

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