ContentDirectoryService.cxx 5.3 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 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 51
{
	// Create request
	char ofbuf[100], cntbuf[100];
52 53
	sprintf(ofbuf, "%u", offset);
	sprintf(cntbuf, "%u", count);
54
	// Some devices require an empty SortCriteria, else bad params
55
	IXML_Document *request =
56 57 58 59 60 61 62
		MakeActionHelper("Browse", m_serviceType.c_str(),
				 "ObjectID", objectId,
				 "BrowseFlag", "BrowseDirectChildren",
				 "Filter", "*",
				 "SortCriteria", "",
				 "StartingIndex", ofbuf,
				 "RequestedCount", cntbuf);
63 64
	if (request == nullptr)
		throw std::runtime_error("UpnpMakeAction() failed");
65

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

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

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

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

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

86
	ReadResultTag(dirbuf, response);
87 88
}

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

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

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

103
	return dirbuf;
104 105
}

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

114
	do {
115 116
		char ofbuf[100];
		sprintf(ofbuf, "%d", offset);
117

118 119 120 121 122 123 124 125
		UniqueIxmlDocument request(MakeActionHelper("Search", m_serviceType.c_str(),
							    "ContainerID", objectId,
							    "SearchCriteria", ss,
							    "Filter", "*",
							    "SortCriteria", "",
							    "StartingIndex", ofbuf,
							    "RequestedCount", "0")); // Setting a value here gets twonky into fits
		if (!request)
126
			throw std::runtime_error("UpnpMakeAction() failed");
127

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

137 138
		UniqueIxmlDocument response(_response);

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

		offset += count;

148 149
		value = ixmlwrap::getFirstElementValue(response.get(),
						       "TotalMatches");
150
		if (value != nullptr)
151
			total = ParseUnsigned(value);
152

153
		ReadResultTag(dirbuf, response.get());
154
	} while (count > 0 && offset < total);
155

156
	return dirbuf;
157 158
}

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

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

182
	UniqueIxmlDocument response(_response);
183 184 185
	UPnPDirContent dirbuf;
	ReadResultTag(dirbuf, response.get());
	return dirbuf;
186
}