Parent: [e44d29] (diff)

Child: [c7b91f] (diff)

Download this file

service.cxx    448 lines (393 with data), 13.5 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* Copyright (C) 2006-2016 J.F.Dockes
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "libupnpp/config.h"
#include "libupnpp/control/service.hxx"
#include <upnp/upnp.h> // for Upnp_Event, UPNP_E_SUCCESS, etc
#include <upnp/upnptools.h> // for UpnpGetErrorMessage
#include <string> // for string, char_traits, etc
#include <utility> // for pair
#include "libupnpp/control/description.hxx" // for UPnPDeviceDesc, etc
#include "libupnpp/ixmlwrap.hxx"
#include "libupnpp/log.hxx" // for LOGDEB1, LOGINF, LOGERR, etc
#include "libupnpp/upnpp_p.hxx" // for caturl
#include "libupnpp/upnpplib.hxx" // for LibUPnP
using namespace std;
using namespace std::placeholders;
using namespace UPnPP;
namespace UPnPClient {
// A small helper class for the functions which perform
// UpnpSendAction calls: get rid of IXML docs when done.
class IxmlCleaner {
public:
IXML_Document **rqpp, **rspp;
IxmlCleaner(IXML_Document** _rqpp, IXML_Document **_rspp)
: rqpp(_rqpp), rspp(_rspp) {}
~IxmlCleaner()
{
if (*rqpp) ixmlDocument_free(*rqpp);
if (*rspp) ixmlDocument_free(*rspp);
}
};
class Service::Internal {
public:
/** Upper level client code event callbacks. To be called by derived class
* for reporting events. */
VarEventReporter *reporter{nullptr};
std::string actionURL;
std::string eventURL;
std::string serviceType;
std::string deviceId;
std::string friendlyName;
std::string manufacturer;
std::string modelName;
Upnp_SID SID{0}; /* Subscription Id */
void initFromDeviceAndService(const UPnPDeviceDesc& devdesc,
const UPnPServiceDesc& servdesc) {
actionURL = caturl(devdesc.URLBase, servdesc.controlURL);
eventURL = caturl(devdesc.URLBase, servdesc.eventSubURL);
serviceType = servdesc.serviceType;
deviceId = devdesc.UDN;
friendlyName = devdesc.friendlyName;
manufacturer = devdesc.manufacturer;
modelName = devdesc.modelName;
}
};
/** Registered callbacks for all the service objects. The map is
* indexed by SID, the subscription id which was obtained by
* each object when subscribing to receive the events for its
* device. The map allows the static function registered with
* libupnp to call the appropriate object method when it receives
* an event. */
static std::unordered_map<std::string, evtCBFunc> o_calls;
Service::Service(const UPnPDeviceDesc& devdesc,
const UPnPServiceDesc& servdesc)
{
if ((m = new Internal()) == 0) {
LOGERR("Device::Device: out of memory" << endl);
return;
}
m->initFromDeviceAndService(devdesc, servdesc);
// Only does anything the first time
initEvents();
// serviceInit() will be called from the derived class constructor
// if needed
}
bool Service::initFromDescription(const UPnPDeviceDesc& devdesc)
{
if (!m) {
LOGERR("Device::Device: Internal is null" << endl);
return false;
}
for (auto& servdesc : devdesc.services) {
if (serviceTypeMatch(servdesc.serviceType)) {
m->initFromDeviceAndService(devdesc, servdesc);
// Only does anything the first time
initEvents();
return serviceInit(devdesc, servdesc);
}
}
return false;
}
Service::Service()
{
if ((m = new Internal()) == 0) {
LOGERR("Device::Device: out of memory" << endl);
return;
}
}
Service::~Service()
{
//LOGDEB("Service::~Service: " << m->serviceType << " SID " <<
// m->SID << endl);
unregisterCallback();
delete m;
m = 0;
}
const string& Service::getFriendlyName() const
{
return m->friendlyName;
}
const string& Service::getDeviceId() const
{
return m->deviceId;
}
const string& Service::getServiceType() const
{
return m->serviceType;
}
const string& Service::getActionURL() const
{
return m->actionURL;
}
const string& Service::getModelName() const
{
return m->modelName;
}
const string& Service::getManufacturer() const
{
return m->manufacturer;
}
int Service::runAction(const SoapOutgoing& args, SoapIncoming& data)
{
LibUPnP* lib = LibUPnP::getLibUPnP();
if (lib == 0) {
LOGINF("Service::runAction: no lib" << endl);
return UPNP_E_OUTOF_MEMORY;
}
UpnpClient_Handle hdl = lib->getclh();
IXML_Document *request(0);
IXML_Document *response(0);
IxmlCleaner cleaner(&request, &response);
if ((request = args.buildSoapBody(false)) == 0) {
LOGINF("Service::runAction: buildSoapBody failed" << endl);
return UPNP_E_OUTOF_MEMORY;
}
LOGDEB1("Service::runAction: url [" << m->actionURL <<
" serviceType " << m->serviceType <<
" rqst: [" << ixmlwPrintDoc(request) << "]" << endl);
int ret = UpnpSendAction(hdl, m->actionURL.c_str(), m->serviceType.c_str(),
0 /*devUDN*/, request, &response);
if (ret != UPNP_E_SUCCESS) {
if (ret < 0) {
LOGINF("Service::runAction: UpnpSendAction failed: " << ret <<
" : " << UpnpGetErrorMessage(ret) << " for " <<
ixmlwPrintDoc(request) << endl);
} else {
// A remote error then
SoapIncoming error;
error.decode("UPnPError", response);
int code = -1;
string desc;
error.get("errorCode", &code);
error.get("errorDescription", &desc);
LOGINF("Service::runAction: failed: errcode: " << code << " : \""
<< desc << "\" for request: " <<
ixmlwPrintDoc(request) << endl);
}
return ret;
}
LOGDEB1("Service::runAction: rslt: [" <<
ixmlwPrintDoc(response) << "]" << endl);
if (!data.decode(args.getName().c_str(), response)) {
LOGERR("Service::runAction: Could not decode response: " <<
ixmlwPrintDoc(response) << endl);
return UPNP_E_BAD_RESPONSE;
}
return UPNP_E_SUCCESS;
}
int Service::runTrivialAction(const std::string& actionName)
{
SoapOutgoing args(m->serviceType, actionName);
SoapIncoming data;
return runAction(args, data);
}
template <class T> int Service::runSimpleGet(const std::string& actnm,
const std::string& valnm,
T *valuep)
{
SoapOutgoing args(m->serviceType, actnm);
SoapIncoming data;
int ret = runAction(args, data);
if (ret != UPNP_E_SUCCESS) {
return ret;
}
if (!data.get(valnm.c_str(), valuep)) {
LOGERR("Service::runSimpleAction: " << actnm <<
" missing " << valnm << " in response" << std::endl);
return UPNP_E_BAD_RESPONSE;
}
return 0;
}
template <class T> int Service::runSimpleAction(const std::string& actnm,
const std::string& valnm,
T value)
{
SoapOutgoing args(m->serviceType, actnm);
args(valnm, SoapHelp::val2s(value));
SoapIncoming data;
return runAction(args, data);
}
static std::mutex cblock;
int Service::srvCB(Upnp_EventType et, void* vevp, void*)
{
std::unique_lock<std::mutex> lock(cblock);
LOGDEB("Service:srvCB: " << LibUPnP::evTypeAsString(et) << endl);
switch (et) {
case UPNP_EVENT_RENEWAL_COMPLETE:
case UPNP_EVENT_SUBSCRIBE_COMPLETE:
case UPNP_EVENT_UNSUBSCRIBE_COMPLETE:
case UPNP_EVENT_AUTORENEWAL_FAILED:
{
const char *ff = (const char *)vevp;
(void)ff;
LOGDEB1("Service:srvCB: subs event: " << ff << endl);
break;
}
case UPNP_EVENT_RECEIVED:
{
struct Upnp_Event *evp = (struct Upnp_Event *)vevp;
LOGDEB1("Service:srvCB: var change event: Sid: " <<
evp->Sid << " EventKey " << evp->EventKey <<
" changed " << ixmlwPrintDoc(evp->ChangedVariables) << endl);
std::unordered_map<string, string> props;
if (!decodePropertySet(evp->ChangedVariables, props)) {
LOGERR("Service::srvCB: could not decode EVENT propertyset" <<endl);
return UPNP_E_BAD_RESPONSE;
}
//for (auto& entry: props) {
//LOGDEB("srvCB: " << entry.first << " -> " << entry.second << endl);
//}
std::unordered_map<std::string, evtCBFunc>::iterator it =
o_calls.find(evp->Sid);
if (it!= o_calls.end()) {
(it->second)(props);
} else {
LOGINF("Service::srvCB: no callback found for sid " << evp->Sid <<
endl);
}
break;
}
default:
// Ignore other events for now
LOGDEB("Service:srvCB: unprocessed evt type: [" <<
LibUPnP::evTypeAsString(et) << "]" << endl);
break;
}
return UPNP_E_SUCCESS;
}
// This is called once per process.
bool Service::initEvents()
{
LOGDEB1("Service::initEvents" << endl);
std::unique_lock<std::mutex> lock(cblock);
static bool eventinit(false);
if (eventinit)
return true;
eventinit = true;
LibUPnP *lib = LibUPnP::getLibUPnP();
if (lib == 0) {
LOGERR("Service::initEvents: Can't get lib" << endl);
return false;
}
lib->registerHandler(UPNP_EVENT_RENEWAL_COMPLETE, srvCB, 0);
lib->registerHandler(UPNP_EVENT_SUBSCRIBE_COMPLETE, srvCB, 0);
lib->registerHandler(UPNP_EVENT_UNSUBSCRIBE_COMPLETE, srvCB, 0);
lib->registerHandler(UPNP_EVENT_AUTORENEWAL_FAILED, srvCB, 0);
lib->registerHandler(UPNP_EVENT_RECEIVED, srvCB, 0);
return true;
}
//void Service::evtCallback(
// const std::unordered_map<std::string, std::string>*)
//{
// LOGDEB("Service::evtCallback!! service: " << m->serviceType << endl);
//}
bool Service::subscribe()
{
LOGDEB1("Service::subscribe" << endl);
LibUPnP* lib = LibUPnP::getLibUPnP();
if (lib == 0) {
LOGINF("Service::subscribe: no lib" << endl);
return false;
}
int timeout = 1800;
int ret = UpnpSubscribe(lib->getclh(), m->eventURL.c_str(),
&timeout, m->SID);
if (ret != UPNP_E_SUCCESS) {
LOGERR("Service:subscribe: failed: " << ret << " : " <<
UpnpGetErrorMessage(ret) << endl);
return false;
}
LOGDEB1("Service::subscribe: sid: " << m->SID << endl);
return true;
}
bool Service::unSubscribe()
{
LOGDEB1("Service::unSubscribe" << endl);
LibUPnP* lib = LibUPnP::getLibUPnP();
if (lib == 0) {
LOGINF("Service::unSubscribe: no lib" << endl);
return false;
}
if (m->SID[0]) {
int ret = UpnpUnSubscribe(lib->getclh(), m->SID);
if (ret != UPNP_E_SUCCESS) {
LOGERR("Service:unSubscribe: failed: " << ret << " : " <<
UpnpGetErrorMessage(ret) << endl);
return false;
}
// Let the caller erase m->SID[] because there may be other
// cleanup to do, based on its value
}
return true;
}
void Service::registerCallback(evtCBFunc c)
{
if (!subscribe())
return;
std::unique_lock<std::mutex> lock(cblock);
LOGDEB1("Service::registerCallback: " << m->SID << endl);
o_calls[m->SID] = c;
}
void Service::unregisterCallback()
{
LOGDEB1("Service::unregisterCallback: " << m->SID << endl);
if (m->SID[0]) {
unSubscribe();
std::unique_lock<std::mutex> lock(cblock);
o_calls.erase(m->SID);
m->SID[0] = 0;
}
}
VarEventReporter *Service::getReporter()
{
return m->reporter;
}
void Service::installReporter(VarEventReporter* reporter)
{
m->reporter = reporter;
registerCallback();
}
void Service::reSubscribe()
{
LOGDEB("Service::reSubscribe()\n");
if (m->SID[0] == 0) {
LOGINF("Service::reSubscribe: no subscription (null SID)\n");
return;
}
evtCBFunc c;
{
std::unique_lock<std::mutex> lock(cblock);
auto it = o_calls.find(m->SID);
if (it == o_calls.end()) {
LOGINF("Service::reSubscribe: no callback found for m->SID " <<
m->SID << endl);
return;
}
c = it->second;
}
unregisterCallback();
registerCallback(c);
}
template int Service::runSimpleAction<int>(string const&, string const&, int);
template int Service::runSimpleAction<string>(string const&, string const&,
string);
template int Service::runSimpleGet<int>(string const&, string const&, int*);
template int Service::runSimpleGet<bool>(string const&, string const&, bool*);
template int Service::runSimpleAction<bool>(string const&, string const&, bool);
template int Service::runSimpleGet<string>(string const&,string const&,string*);
}