Child: [db078e] (diff)

Download this file

cdirectory.cxx    348 lines (291 with data), 9.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* Copyright (C) 2013 J.F.Dockes
* 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.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <set>
#include <vector>
using std::string;
using std::cerr;
using std::endl;
using std::vector;
using std::set;
#include "upnpp_p.hxx"
#include <upnp/upnp.h>
#include <upnp/upnptools.h>
#include "upnpplib.hxx"
#include "ixmlwrap.hxx"
#include "cdirectory.hxx"
#include "cdircontent.hxx"
class DirBResFree {
public:
IXML_Document **rqpp, **rspp;
DirBResFree(IXML_Document** _rqpp, IXML_Document **_rspp)
:rqpp(_rqpp), rspp(_rspp)
{}
~DirBResFree()
{
if (*rqpp)
ixmlDocument_free(*rqpp);
if (*rspp)
ixmlDocument_free(*rspp);
}
};
int ContentDirectoryService::readDirSlice(const string& objectId, int offset,
int count, UPnPDirContent& dirbuf,
int *didreadp, int *totalp)
{
PLOGDEB("CDService::readDirSlice: objId [%s] offset %d count %d\n",
objectId.c_str(), offset, count);
LibUPnP* lib = LibUPnP::getLibUPnP();
if (lib == 0) {
PLOGINF("CDService::readDir: no lib\n");
return UPNP_E_OUTOF_MEMORY;
}
UpnpClient_Handle hdl = lib->getclh();
IXML_Document *request(0);
IXML_Document *response(0);
DirBResFree cleaner(&request, &response);
// Create request
char ofbuf[100], cntbuf[100];
sprintf(ofbuf, "%d", offset);
sprintf(cntbuf, "%d", count);
int argcnt = 6;
// Some devices require an empty SortCriteria, else bad params
request = UpnpMakeAction("Browse", m_serviceType.c_str(), argcnt,
"ObjectID", objectId.c_str(),
"BrowseFlag", "BrowseDirectChildren",
"Filter", "*",
"SortCriteria", "",
"StartingIndex", ofbuf,
"RequestedCount", cntbuf,
NULL, NULL);
if (request == 0) {
PLOGINF("CDService::readDir: UpnpMakeAction failed\n");
return UPNP_E_OUTOF_MEMORY;
}
//cerr << "Action xml: [" << ixmlPrintDocument(request) << "]" << endl;
int ret = UpnpSendAction(hdl, m_actionURL.c_str(), m_serviceType.c_str(),
0 /*devUDN*/, request, &response);
if (ret != UPNP_E_SUCCESS) {
PLOGINF("CDService::readDir: UpnpSendAction failed: %s\n",
UpnpGetErrorMessage(ret));
return ret;
}
int didread = -1;
string tbuf = ixmlwrap::getFirstElementValue(response, "NumberReturned");
if (!tbuf.empty())
didread = atoi(tbuf.c_str());
if (count == -1 || count == 0) {
PLOGINF("CDService::readDir: got -1 or 0 entries\n");
return UPNP_E_BAD_RESPONSE;
}
tbuf = ixmlwrap::getFirstElementValue(response, "TotalMatches");
if (!tbuf.empty())
*totalp = atoi(tbuf.c_str());
tbuf = ixmlwrap::getFirstElementValue(response, "Result");
#if 0
cerr << "CDService::readDirSlice: count " << count <<
" offset " << offset <<
" total " << *totalp << endl;
cerr << " result " << tbuf << endl;
#endif
dirbuf.parse(tbuf);
*didreadp = didread;
return UPNP_E_SUCCESS;
}
int ContentDirectoryService::readDir(const string& objectId,
UPnPDirContent& dirbuf)
{
PLOGDEB("CDService::readDir: url [%s] type [%s] udn [%s] objId [%s]\n",
m_actionURL.c_str(), m_serviceType.c_str(), m_deviceId.c_str(),
objectId.c_str());
int offset = 0;
int total = 1000;// Updated on first read.
while (offset < total) {
int count;
int error = readDirSlice(objectId, offset, m_rdreqcnt, dirbuf,
&count, &total);
if (error != UPNP_E_SUCCESS)
return error;
offset += count;
}
return UPNP_E_SUCCESS;
}
int ContentDirectoryService::search(const string& objectId,
const string& ss,
UPnPDirContent& dirbuf)
{
PLOGDEB("CDService::search: url [%s] type [%s] udn [%s] objid [%s] "
"search [%s]\n",
m_actionURL.c_str(), m_serviceType.c_str(), m_deviceId.c_str(),
objectId.c_str(), ss.c_str());
LibUPnP* lib = LibUPnP::getLibUPnP();
if (lib == 0) {
PLOGINF("CDService::search: no lib\n");
return UPNP_E_OUTOF_MEMORY;
}
UpnpClient_Handle hdl = lib->getclh();
int ret = UPNP_E_SUCCESS;
IXML_Document *request(0);
IXML_Document *response(0);
int offset = 0;
int total = 1000;// Updated on first read.
while (offset < total) {
DirBResFree cleaner(&request, &response);
char ofbuf[100];
sprintf(ofbuf, "%d", offset);
// Create request
int argcnt = 6;
request = UpnpMakeAction(
"Search", m_serviceType.c_str(), argcnt,
"ContainerID", objectId.c_str(),
"SearchCriteria", ss.c_str(),
"Filter", "*",
"SortCriteria", "",
"StartingIndex", ofbuf,
"RequestedCount", "0", // Setting a value here gets twonky into fits
NULL, NULL);
if (request == 0) {
PLOGINF("CDService::search: UpnpMakeAction failed\n");
return UPNP_E_OUTOF_MEMORY;
}
// cerr << "Action xml: [" << ixmlPrintDocument(request) << "]" << endl;
ret = UpnpSendAction(hdl, m_actionURL.c_str(), m_serviceType.c_str(),
0 /*devUDN*/, request, &response);
if (ret != UPNP_E_SUCCESS) {
PLOGINF("CDService::search: UpnpSendAction failed: %s\n",
UpnpGetErrorMessage(ret));
return ret;
}
int count = -1;
string tbuf =
ixmlwrap::getFirstElementValue(response, "NumberReturned");
if (!tbuf.empty())
count = atoi(tbuf.c_str());
if (count == -1 || count == 0) {
PLOGINF("CDService::search: got -1 or 0 entries\n");
return count == -1 ? UPNP_E_BAD_RESPONSE : UPNP_E_SUCCESS;
}
offset += count;
tbuf = ixmlwrap::getFirstElementValue(response, "TotalMatches");
if (!tbuf.empty())
total = atoi(tbuf.c_str());
tbuf = ixmlwrap::getFirstElementValue(response, "Result");
#if 0
cerr << "CDService::search: count " << count <<
" offset " << offset <<
" total " << total << endl;
cerr << " result " << tbuf << endl;
#endif
dirbuf.parse(tbuf);
}
return UPNP_E_SUCCESS;
}
int ContentDirectoryService::getSearchCapabilities(set<string>& result)
{
PLOGDEB("CDService::getSearchCapabilities:\n");
LibUPnP* lib = LibUPnP::getLibUPnP();
if (lib == 0) {
PLOGINF("CDService::getSearchCapabilities: no lib\n");
return UPNP_E_OUTOF_MEMORY;
}
UpnpClient_Handle hdl = lib->getclh();
int ret = UPNP_E_SUCCESS;
IXML_Document *request(0);
IXML_Document *response(0);
request = UpnpMakeAction("GetSearchCapabilities", m_serviceType.c_str(),
0,
NULL, NULL);
if (request == 0) {
PLOGINF("CDService::getSearchCapa: UpnpMakeAction failed\n");
return UPNP_E_OUTOF_MEMORY;
}
//cerr << "Action xml: [" << ixmlPrintDocument(request) << "]" << endl;
ret = UpnpSendAction(hdl, m_actionURL.c_str(), m_serviceType.c_str(),
0 /*devUDN*/, request, &response);
if (ret != UPNP_E_SUCCESS) {
PLOGINF("CDService::getSearchCapa: UpnpSendAction failed: %s\n",
UpnpGetErrorMessage(ret));
return ret;
}
//cerr << "getSearchCapa: response xml: [" << ixmlPrintDocument(response)
// << "]" << endl;
string tbuf = ixmlwrap::getFirstElementValue(response, "SearchCaps");
// cerr << "getSearchCapa: capa: [" << tbuf << "]" << endl;
result.clear();
if (!tbuf.compare("*")) {
result.insert(result.end(), "*");
} else if (!tbuf.empty()) {
if (!csvToStrings(tbuf, result)) {
return UPNP_E_BAD_RESPONSE;
}
}
return UPNP_E_SUCCESS;
}
int ContentDirectoryService::getMetadata(const string& objectId,
UPnPDirContent& dirbuf)
{
PLOGDEB("CDService::getMetadata: url [%s] type [%s] udn [%s] objId [%s]\n",
m_actionURL.c_str(), m_serviceType.c_str(), m_deviceId.c_str(),
objectId.c_str());
LibUPnP* lib = LibUPnP::getLibUPnP();
if (lib == 0) {
PLOGINF("CDService::getMetadata: no lib\n");
return UPNP_E_OUTOF_MEMORY;
}
UpnpClient_Handle hdl = lib->getclh();
int ret = UPNP_E_SUCCESS;
IXML_Document *request(0);
IXML_Document *response(0);
DirBResFree cleaner(&request, &response);
// Create request
int argcnt = 6;
request = UpnpMakeAction("Browse", m_serviceType.c_str(), argcnt,
"ObjectID", objectId.c_str(),
"BrowseFlag", "BrowseMetadata",
"Filter", "*",
"SortCriteria", "",
"StartingIndex", "0",
"RequestedCount", "1",
NULL, NULL);
if (request == 0) {
PLOGINF("CDService::getmetadata: UpnpMakeAction failed\n");
return UPNP_E_OUTOF_MEMORY;
}
//cerr << "Action xml: [" << ixmlPrintDocument(request) << "]" << endl;
ret = UpnpSendAction(hdl, m_actionURL.c_str(), m_serviceType.c_str(),
0 /*devUDN*/, request, &response);
if (ret != UPNP_E_SUCCESS) {
PLOGINF("CDService::getmetadata: UpnpSendAction failed: %s\n",
UpnpGetErrorMessage(ret));
return ret;
}
string tbuf = ixmlwrap::getFirstElementValue(response, "Result");
if (dirbuf.parse(tbuf))
return UPNP_E_SUCCESS;
else
return UPNP_E_BAD_RESPONSE;
}
/* Local Variables: */
/* mode: c++ */
/* c-basic-offset: 4 */
/* tab-width: 4 */
/* indent-tabs-mode: t */
/* End: */