ContentDirectoryService.cxx 5.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (C) 2003-2014 The Music Player Daemon Project
 * 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 23 24
#include "lib/upnp/ContentDirectoryService.hxx"
#include "lib/upnp/Domain.hxx"
#include "lib/upnp/ixmlwrap.hxx"
#include "lib/upnp/Action.hxx"
25
#include "Directory.hxx"
26
#include "util/NumberParser.hxx"
27 28 29 30
#include "util/Error.hxx"

#include <stdio.h>

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

	return dirbuf.parse(p, error);
}

41
inline bool
42
ContentDirectoryService::readDirSlice(UpnpClient_Handle hdl,
43 44 45
				      const char *objectId, unsigned offset,
				      unsigned count, UPnPDirContent &dirbuf,
				      unsigned &didreadp, unsigned &totalp,
46
				      Error &error) const
47 48 49
{
	// Create request
	char ofbuf[100], cntbuf[100];
50 51
	sprintf(ofbuf, "%u", offset);
	sprintf(cntbuf, "%u", count);
52
	// Some devices require an empty SortCriteria, else bad params
53
	IXML_Document *request =
54 55 56 57 58 59 60
		MakeActionHelper("Browse", m_serviceType.c_str(),
				 "ObjectID", objectId,
				 "BrowseFlag", "BrowseDirectChildren",
				 "Filter", "*",
				 "SortCriteria", "",
				 "StartingIndex", ofbuf,
				 "RequestedCount", cntbuf);
61 62 63 64 65
	if (request == nullptr) {
		error.Set(upnp_domain, "UpnpMakeAction() failed");
		return false;
	}

66
	IXML_Document *response;
67 68
	int code = UpnpSendAction(hdl, m_actionURL.c_str(), m_serviceType.c_str(),
				  0 /*devUDN*/, request, &response);
69
	ixmlDocument_free(request);
70 71 72 73 74 75 76
	if (code != UPNP_E_SUCCESS) {
		error.Format(upnp_domain, code,
			     "UpnpSendAction() failed: %s",
			     UpnpGetErrorMessage(code));
		return false;
	}

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 87 88
	bool success = ReadResultTag(dirbuf, response, error);
	ixmlDocument_free(response);
	return success;
89 90 91
}

bool
92 93
ContentDirectoryService::readDir(UpnpClient_Handle handle,
				 const char *objectId,
94
				 UPnPDirContent &dirbuf,
95
				 Error &error) const
96
{
97
	unsigned offset = 0, total = -1, count;
98

99
	do {
100
		if (!readDirSlice(handle, objectId, offset, m_rdreqcnt, dirbuf,
101
				  count, total, error))
102 103 104
			return false;

		offset += count;
105
	} while (count > 0 && offset < total);
106 107 108 109 110

	return true;
}

bool
111 112
ContentDirectoryService::search(UpnpClient_Handle hdl,
				const char *objectId,
113 114
				const char *ss,
				UPnPDirContent &dirbuf,
115
				Error &error) const
116
{
117
	unsigned offset = 0, total = -1, count;
118

119
	do {
120 121
		char ofbuf[100];
		sprintf(ofbuf, "%d", offset);
122 123

		IXML_Document *request =
124 125 126 127 128 129 130
			MakeActionHelper("Search", m_serviceType.c_str(),
					 "ContainerID", objectId,
					 "SearchCriteria", ss,
					 "Filter", "*",
					 "SortCriteria", "",
					 "StartingIndex", ofbuf,
					 "RequestedCount", "0"); // Setting a value here gets twonky into fits
131 132 133 134 135
		if (request == 0) {
			error.Set(upnp_domain, "UpnpMakeAction() failed");
			return false;
		}

136
		IXML_Document *response;
137 138 139
		auto code = UpnpSendAction(hdl, m_actionURL.c_str(),
					   m_serviceType.c_str(),
					   0 /*devUDN*/, request, &response);
140
		ixmlDocument_free(request);
141 142 143 144 145 146 147
		if (code != UPNP_E_SUCCESS) {
			error.Format(upnp_domain, code,
				     "UpnpSendAction() failed: %s",
				     UpnpGetErrorMessage(code));
			return false;
		}

148
		const char *value =
149
			ixmlwrap::getFirstElementValue(response, "NumberReturned");
150
		count = value != nullptr
151 152
			? ParseUnsigned(value)
			: 0;
153 154 155

		offset += count;

156 157
		value = ixmlwrap::getFirstElementValue(response, "TotalMatches");
		if (value != nullptr)
158
			total = ParseUnsigned(value);
159

160 161 162
		bool success = ReadResultTag(dirbuf, response, error);
		ixmlDocument_free(response);
		if (!success)
163
			return false;
164
	} while (count > 0 && offset < total);
165 166 167 168 169

	return true;
}

bool
170 171
ContentDirectoryService::getMetadata(UpnpClient_Handle hdl,
				     const char *objectId,
172
				     UPnPDirContent &dirbuf,
173
				     Error &error) const
174 175 176
{
	// Create request
	IXML_Document *request =
177 178 179 180 181 182 183
		MakeActionHelper("Browse", m_serviceType.c_str(),
				 "ObjectID", objectId,
				 "BrowseFlag", "BrowseMetadata",
				 "Filter", "*",
				 "SortCriteria", "",
				 "StartingIndex", "0",
				 "RequestedCount", "1");
184 185 186 187 188
	if (request == nullptr) {
		error.Set(upnp_domain, "UpnpMakeAction() failed");
		return false;
	}

189
	IXML_Document *response;
190 191 192
	auto code = UpnpSendAction(hdl, m_actionURL.c_str(),
				   m_serviceType.c_str(),
				   0 /*devUDN*/, request, &response);
193
	ixmlDocument_free(request);
194 195 196 197 198 199 200
	if (code != UPNP_E_SUCCESS) {
		error.Format(upnp_domain, code,
			     "UpnpSendAction() failed: %s",
			     UpnpGetErrorMessage(code));
		return false;
	}

201
	bool success = ReadResultTag(dirbuf, response, error);
202
	ixmlDocument_free(response);
203
	return success;
204
}