Parent: [c20795] (diff)

Child: [5c8b3d] (diff)

Download this file

cdirectory.cxx    404 lines (348 with data), 13.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* 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 "libupnpp/config.h"
#include "libupnpp/control/cdirectory.hxx"
#include <sys/types.h>
#include <regex.h>
#include <upnp/upnp.h> // for UPNP_E_SUCCESS, etc
#include <upnp/upnptools.h> // for UpnpGetErrorMessage
#include <functional> // for _Bind, bind, _1, _2
#include <iostream> // for operator<<, basic_ostream, etc
#include <set> // for set
#include <string> // for string, operator<<, etc
#include <vector> // for vector
#include "libupnpp/control/cdircontent.hxx" // for UPnPDirContent
#include "libupnpp/control/description.hxx" // for UPnPDeviceDesc, etc
#include "libupnpp/control/discovery.hxx" // for UPnPDeviceDirectory, etc
#include "libupnpp/log.hxx" // for LOGDEB, LOGINF, LOGERR
#include "libupnpp/soaphelp.hxx" // for SoapOutgoing, SoapOutgoing, etc
#include "libupnpp/upnpp_p.hxx" // for csvToStrings
using namespace std;
using namespace STD_PLACEHOLDERS;
using namespace UPnPP;
namespace UPnPClient {
// The service type string for Content Directories:
const string ContentDirectory::SType("urn:schemas-upnp-org:service:ContentDirectory:1");
class SimpleRegexp {
public:
SimpleRegexp(const string& exp, int flags) : m_ok(false) {
if (regcomp(&m_expr, exp.c_str(), flags) == 0) {
m_ok = true;
}
}
~SimpleRegexp() {
regfree(&m_expr);
}
bool simpleMatch(const string& val) const {
if (!ok())
return false;
if (regexec(&m_expr, val.c_str(), 0, 0, 0) == 0) {
return true;
} else {
return false;
}
}
bool operator() (const string& val) const {
return simpleMatch(val);
}
bool ok() const {return m_ok;}
private:
bool m_ok;
regex_t m_expr;
};
/*
manufacturer: Bubblesoft model BubbleUPnP Media Server
manufacturer: Justin Maggard model Windows Media Connect compatible (MiniDLNA)
manufacturer: minimserver.com model MinimServer
manufacturer: PacketVideo model TwonkyMedia Server
manufacturer: ? model MediaTomb
*/
static const SimpleRegexp bubble_rx("bubble", REG_ICASE|REG_NOSUB);
static const SimpleRegexp mediatomb_rx("mediatomb", REG_ICASE|REG_NOSUB);
static const SimpleRegexp minidlna_rx("minidlna", REG_ICASE|REG_NOSUB);
static const SimpleRegexp minim_rx("minim", REG_ICASE|REG_NOSUB);
static const SimpleRegexp twonky_rx("twonky", REG_ICASE|REG_NOSUB);
ContentDirectory::ContentDirectory(const UPnPDeviceDesc& device,
const UPnPServiceDesc& service)
: Service(device, service), m_rdreqcnt(200), m_serviceKind(CDSKIND_UNKNOWN)
{
LOGERR("ContentDirectory::ContentDirectory: manufacturer: " <<
getManufacturer() << " model " << getModelName() << endl);
if (bubble_rx(getModelName())) {
m_serviceKind = CDSKIND_BUBBLE;
LOGDEB1("ContentDirectory::ContentDirectory: BUBBLE" << endl);
} else if (mediatomb_rx(getModelName())) {
// Readdir by 200 entries is good for most, but MediaTomb likes
// them really big. Actually 1000 is better but I don't dare
m_rdreqcnt = 500;
m_serviceKind = CDSKIND_MEDIATOMB;
LOGDEB1("ContentDirectory::ContentDirectory: MEDIATOMB" << endl);
} else if (minidlna_rx(getModelName())) {
m_serviceKind = CDSKIND_MINIDLNA;
LOGDEB1("ContentDirectory::ContentDirectory: MINIDLNA" << endl);
} else if (minim_rx(getModelName())) {
m_serviceKind = CDSKIND_MINIM;
LOGDEB1("ContentDirectory::ContentDirectory: MINIM" << endl);
} else if (twonky_rx(getModelName())) {
m_serviceKind = CDSKIND_TWONKY;
LOGDEB1("ContentDirectory::ContentDirectory: TWONKY" << endl);
}
registerCallback();
}
ContentDirectory::~ContentDirectory()
{
unregisterCallback();
}
// We don't include a version in comparisons, as we are satisfied with
// version 1
bool ContentDirectory::isCDService(const string& st)
{
const string::size_type sz(SType.size()-2);
return !SType.compare(0, sz, st, 0, sz);
}
static bool DSAccum(vector<CDSH>* out,
const UPnPDeviceDesc& device,
const UPnPServiceDesc& service)
{
if (ContentDirectory::isCDService(service.serviceType)) {
out->push_back(CDSH(new ContentDirectory(device, service)));
}
return true;
}
bool ContentDirectory::getServices(vector<CDSH>& vds)
{
//LOGDEB("UPnPDeviceDirectory::getDirServices" << endl);
UPnPDeviceDirectory::Visitor visitor = bind(DSAccum, &vds, _1, _2);
UPnPDeviceDirectory::getTheDir()->traverse(visitor);
return !vds.empty();
}
// Get server by friendly name.
bool ContentDirectory::getServerByName(const string& fname, CDSH& server)
{
UPnPDeviceDesc ddesc;
bool found = UPnPDeviceDirectory::getTheDir()->getDevByFName(fname, ddesc);
if (!found)
return false;
found = false;
for (std::vector<UPnPServiceDesc>::const_iterator it =
ddesc.services.begin(); it != ddesc.services.end(); it++) {
if (isCDService(it->serviceType)) {
server = CDSH(new ContentDirectory(ddesc, *it));
found = true;
break;
}
}
return found;
}
#if 0
static int asyncReaddirCB(Upnp_EventType et, void *vev, void *cookie)
{
LOGDEB("asyncReaddirCB: " << LibUPnP::evTypeAsString(et) << endl);
struct Upnp_Action_Complete *act = (struct Upnp_Action_Complete*)vev;
LOGDEB("asyncReaddirCB: errcode " << act->ErrCode <<
" cturl " << UpnpString_get_String(act->CtrlUrl) <<
" actionrequest " << endl <<
ixmlwPrintDoc(act->ActionRequest) << endl <<
" actionresult " << ixmlwPrintDoc(act->ActionResult) << endl);
return 0;
}
int ret =
UpnpSendActionAsync(hdl, getActionURL().c_str(), getServiceType().c_str(),
0 /*devUDN*/, request, asyncReaddirCB, 0);
sleep(10);
return -1;
#endif
void ContentDirectory::evtCallback(const STD_UNORDERED_MAP<string, string>&)
{
}
void ContentDirectory::registerCallback()
{
LOGDEB("ContentDirectory::registerCallback"<< endl);
Service::registerCallback(bind(&ContentDirectory::evtCallback,
this, _1));
}
int ContentDirectory::readDirSlice(const string& objectId, int offset,
int count, UPnPDirContent& dirbuf,
int *didread, int *total)
{
LOGDEB("CDService::readDirSlice: objId [" << objectId << "] offset " <<
offset << " count " << count << endl);
// Create request
// Some devices require an empty SortCriteria, else bad params
SoapOutgoing args(getServiceType(), "Browse");
args("ObjectID", objectId)
("BrowseFlag", "BrowseDirectChildren")
("Filter", "*")
("SortCriteria", "")
("StartingIndex", SoapHelp::i2s(offset))
("RequestedCount", SoapHelp::i2s(count));
SoapIncoming data;
int ret = runAction(args, data);
if (ret != UPNP_E_SUCCESS) {
return ret;
}
string tbuf;
if (!data.get("NumberReturned", didread) ||
!data.get("TotalMatches", total) ||
!data.get("Result", &tbuf)) {
LOGERR("CDService::readDir: missing elts in response" << endl);
return UPNP_E_BAD_RESPONSE;
}
if (*didread <= 0) {
LOGINF("CDService::readDir: got -1 or 0 entries" << endl);
return UPNP_E_BAD_RESPONSE;
}
#if 0
cerr << "CDService::readDirSlice: count " << count <<
" offset " << offset <<
" total " << *total << endl;
cerr << " result " << tbuf << endl;
#endif
dirbuf.parse(tbuf);
return UPNP_E_SUCCESS;
}
int ContentDirectory::readDir(const string& objectId,
UPnPDirContent& dirbuf)
{
LOGDEB("CDService::readDir: url [" << getActionURL() << "] type [" <<
getServiceType() << "] udn [" << getDeviceId() << "] objId [" <<
objectId << endl);
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 ContentDirectory::searchSlice(const string& objectId,
const string& ss,
int offset, int count, UPnPDirContent& dirbuf,
int *didread, int *total)
{
LOGDEB("CDService::searchSlice: objId [" << objectId << "] offset " <<
offset << " count " << count << endl);
// Create request
SoapOutgoing args(getServiceType(), "Search");
args("ContainerID", objectId)
("SearchCriteria", ss)
("Filter", "*")
("SortCriteria", "")
("StartingIndex", SoapHelp::i2s(offset))
("RequestedCount", SoapHelp::i2s(count));
SoapIncoming data;
int ret = runAction(args, data);
if (ret != UPNP_E_SUCCESS) {
LOGINF("CDService::search: UpnpSendAction failed: " <<
UpnpGetErrorMessage(ret) << endl);
return ret;
}
string tbuf;
if (!data.get("NumberReturned", didread) ||
!data.get("TotalMatches", total) ||
!data.get("Result", &tbuf)) {
LOGERR("CDService::search: missing elts in response" << endl);
return UPNP_E_BAD_RESPONSE;
}
if (*didread <= 0) {
LOGINF("CDService::search: got -1 or 0 entries" << endl);
return count < 0 ? UPNP_E_BAD_RESPONSE : UPNP_E_SUCCESS;
}
dirbuf.parse(tbuf);
return UPNP_E_SUCCESS;
}
int ContentDirectory::search(const string& objectId,
const string& ss,
UPnPDirContent& dirbuf)
{
LOGDEB("CDService::search: url [" << getActionURL() << "] type [" <<
getServiceType() << "] udn [" << getDeviceId() << "] objid [" <<
objectId << "] search [" << ss << "]" << endl);
int offset = 0;
int total = 1000;// Updated on first read.
while (offset < total) {
int count;
int error = searchSlice(objectId, ss, offset, m_rdreqcnt, dirbuf,
&count, &total);
if (error != UPNP_E_SUCCESS)
return error;
offset += count;
}
return UPNP_E_SUCCESS;
}
int ContentDirectory::getSearchCapabilities(set<string>& result)
{
LOGDEB("CDService::getSearchCapabilities:" << endl);
SoapOutgoing args(getServiceType(), "GetSearchCapabilities");
SoapIncoming data;
int ret = runAction(args, data);
if (ret != UPNP_E_SUCCESS) {
LOGINF("CDService::getSearchCapa: UpnpSendAction failed: " <<
UpnpGetErrorMessage(ret) << endl);
return ret;
}
string tbuf;
if (!data.get("SearchCaps", &tbuf)) {
LOGERR("CDService::getSearchCaps: missing Result in response" << endl);
cerr << tbuf << endl;
return UPNP_E_BAD_RESPONSE;
}
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 ContentDirectory::getMetadata(const string& objectId,
UPnPDirContent& dirbuf)
{
LOGDEB("CDService::getMetadata: url [" << getActionURL() << "] type [" <<
getServiceType() << "] udn [" << getDeviceId() << "] objId [" <<
objectId << "]" << endl);
SoapOutgoing args(getServiceType(), "Browse");
SoapIncoming data;
args("ObjectID", objectId)
("BrowseFlag", "BrowseMetadata")
("Filter", "*")
("SortCriteria", "")
("StartingIndex", "0")
("RequestedCount", "1");
int ret = runAction(args, data);
if (ret != UPNP_E_SUCCESS) {
LOGINF("CDService::getmetadata: UpnpSendAction failed: " <<
UpnpGetErrorMessage(ret) << endl);
return ret;
}
string tbuf;
if (!data.get("Result", &tbuf)) {
LOGERR("CDService::getmetadata: missing Result in response" << endl);
return UPNP_E_BAD_RESPONSE;
}
if (dirbuf.parse(tbuf))
return UPNP_E_SUCCESS;
else
return UPNP_E_BAD_RESPONSE;
}
} // namespace UPnPClient