Parent: [f45b50] (diff)

Child: [30b69c] (diff)

Download this file

cdirectory.cxx    308 lines (263 with data), 10.1 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
/* 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 "libupnpp/control/cdirectory.hxx"
#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 SoapEncodeInput, SoapArgs, etc
#include "libupnpp/upnpp_p.hxx" // for csvToStrings
using namespace std;
using namespace std::placeholders;
namespace UPnPClient {
// The service type string for Content Directories:
const string ContentDirectory::SType("urn:schemas-upnp-org:service:ContentDirectory:1");
// 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 (auto 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, m_actionURL.c_str(), m_serviceType.c_str(),
0 /*devUDN*/, request, asyncReaddirCB, 0);
sleep(10);
return -1;
#endif
void ContentDirectory::evtCallback(const 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 *didreadp, int *totalp)
{
LOGDEB("CDService::readDirSlice: objId [" << objectId << "] offset " <<
offset << " count " << count << endl);
// Create request
// Some devices require an empty SortCriteria, else bad params
SoapData args(m_serviceType, "Browse");
args("ObjectID", objectId)
("BrowseFlag", "BrowseDirectChildren")
("Filter", "*")
("SortCriteria", "")
("StartingIndex", SoapHelp::i2s(offset))
("RequestedCount", SoapHelp::i2s(count));
SoapArgs data;
int ret = runAction(args, data);
if (ret != UPNP_E_SUCCESS) {
return ret;
}
int didread;
string tbuf;
if (!data.getInt("NumberReturned", &didread) ||
!data.getInt("TotalMatches", totalp) ||
!data.getString("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 " << *totalp << endl;
cerr << " result " << tbuf << endl;
#endif
dirbuf.parse(tbuf);
*didreadp = didread;
return UPNP_E_SUCCESS;
}
int ContentDirectory::readDir(const string& objectId,
UPnPDirContent& dirbuf)
{
LOGDEB("CDService::readDir: url [" << m_actionURL << "] type [" <<
m_serviceType << "] udn [" << m_deviceId << "] 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::search(const string& objectId,
const string& ss,
UPnPDirContent& dirbuf)
{
LOGDEB("CDService::search: url [" << m_actionURL << "] type [" <<
m_serviceType << "] udn [" << m_deviceId << "] objid [" <<
objectId << "] search [" << ss << "]" << endl);
int offset = 0;
int total = 1000;// Updated on first read.
while (offset < total) {
// Create request
SoapData args(m_serviceType, "Search");
args("ContainerID", objectId)
("SearchCriteria", ss)
("Filter", "*")
("SortCriteria", "")
("StartingIndex", SoapHelp::i2s(offset))
("RequestedCount", "10");
SoapArgs data;
int ret = runAction(args, data);
if (ret != UPNP_E_SUCCESS) {
LOGINF("CDService::search: UpnpSendAction failed: " <<
UpnpGetErrorMessage(ret) << endl);
return ret;
}
int count = -1;
string tbuf;
if (!data.getInt("NumberReturned", &count) ||
!data.getInt("TotalMatches", &total) ||
!data.getString("Result", &tbuf)) {
LOGERR("CDService::search: missing elts in response" << endl);
return UPNP_E_BAD_RESPONSE;
}
if (count <= 0) {
LOGINF("CDService::search: got -1 or 0 entries" << endl);
return count < 0 ? UPNP_E_BAD_RESPONSE : UPNP_E_SUCCESS;
}
offset += count;
dirbuf.parse(tbuf);
}
return UPNP_E_SUCCESS;
}
int ContentDirectory::getSearchCapabilities(set<string>& result)
{
LOGDEB("CDService::getSearchCapabilities:" << endl);
SoapData args(m_serviceType, "GetSearchCapabilities");
SoapArgs 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.getString("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 [" << m_actionURL << "] type [" <<
m_serviceType << "] udn [" << m_deviceId << "] objId [" <<
objectId << "]" << endl);
SoapData args(m_serviceType, "Browse");
SoapArgs 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.getString("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