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

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

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

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

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

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

87
	ReadResultTag(dirbuf, response);
88 89
}

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

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

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

104
	return dirbuf;
105 106
}

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

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

119 120 121 122 123 124 125 126
		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)
127
			throw std::runtime_error("UpnpMakeAction() failed");
128

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

138 139
		UniqueIxmlDocument response(_response);

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

		offset += count;

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

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

157
	return dirbuf;
158 159
}

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

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

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