Download this file

service.cxx    470 lines (414 with data), 14.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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/* 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;
#if UPNP_VERSION_MINOR < 8 && !defined(UpnpEvent_get_SID_cstr)
typedef struct Upnp_Event UpnpEvent;
#define UpnpEvent_get_SID_cstr(x) ((x)->Sid)
#define UpnpEvent_get_EventKey(x) ((x)->EventKey)
#define UpnpEvent_get_ChangedVariables(x) ((x)->ChangedVariables)
#endif
#if UPNP_VERSION_MAJOR > 1 || (UPNP_VERSION_MAJOR==1 && UPNP_VERSION_MINOR >= 8)
#define CBCONST const
#else
#define CBCONST
#endif
namespace UPnPClient {
static bool initEvents();
static int srvCB(Upnp_EventType et, CBCONST void* vevp, void*);
// 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;
}
/* Tell the UPnP device (through libupnp) that we want to receive
its events. This is called by registerCallback() and sets m_SID */
bool subscribe();
bool unSubscribe();
};
/** 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()
{
LOGDEB1("Service::~Service: " << m->eventURL << " 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;
// The static event callback given to libupnp
static int srvCB(Upnp_EventType et, CBCONST void* vevp, void*)
{
std::unique_lock<std::mutex> lock(cblock);
LOGDEB0("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:
{
UpnpEvent *evp = (UpnpEvent *)vevp;
LOGDEB1("Service:srvCB: var change event: SID " <<
UpnpEvent_get_SID_cstr(evp) << " EventKey " <<
UpnpEvent_get_EventKey(evp) << " changed " <<
ixmlwPrintDoc(UpnpEvent_get_ChangedVariables(evp)) << endl);
std::unordered_map<string, string> props;
if (!decodePropertySet(UpnpEvent_get_ChangedVariables(evp), 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);
//}
auto it = o_calls.find(UpnpEvent_get_SID_cstr(evp));
if (it != o_calls.end()) {
(it->second)(props);
} else {
LOGINF("Service::srvCB: no callback found for sid " <<
UpnpEvent_get_SID_cstr(evp) << endl);
}
break;
}
default:
// Ignore other events for now
LOGDEB("Service:srvCB: unprocessed evt type: [" <<
LibUPnP::evTypeAsString(et) << "]" << endl);
break;
}
return UPNP_E_SUCCESS;
}
// Only actually does something on the first call, to register our
// (static) library callback
static bool 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;
}
bool Service::Internal::subscribe()
{
LOGDEB1("Service::subscribe: " << eventURL << endl);
LibUPnP* lib = LibUPnP::getLibUPnP();
if (lib == 0) {
LOGINF("Service::subscribe: no lib" << endl);
return false;
}
int timeout = 1800;
int ret = UpnpSubscribe(lib->getclh(), eventURL.c_str(),
&timeout, SID);
if (ret != UPNP_E_SUCCESS) {
LOGERR("Service:subscribe: failed: " << ret << " : " <<
UpnpGetErrorMessage(ret) << endl);
return false;
}
LOGDEB1("Service::subs: " << eventURL << " SID " << SID << endl);
return true;
}
bool Service::Internal::unSubscribe()
{
LOGDEB1("Service::unSubs: " << m->eventURL << " SID " << m->SID << endl);
LibUPnP* lib = LibUPnP::getLibUPnP();
if (lib == 0) {
LOGINF("Service::unSubscribe: no lib" << endl);
return false;
}
if (SID[0]) {
int ret = UpnpUnSubscribe(lib->getclh(), 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 (!m || !m->subscribe())
return;
std::unique_lock<std::mutex> lock(cblock);
LOGDEB1("Service::registerCallback: " << m->eventURL << " SID " <<
m->SID << endl);
o_calls[m->SID] = c;
}
void Service::unregisterCallback()
{
LOGDEB1("Service::unregisterCallback: " << m->eventURL << " SID " <<
m->SID << endl);
if (m->SID[0]) {
m->unSubscribe();
std::unique_lock<std::mutex> lock(cblock);
o_calls.erase(m->SID);
m->SID[0] = 0;
}
}
VarEventReporter *Service::getReporter()
{
if (m)
return m->reporter;
return nullptr;
}
void Service::installReporter(VarEventReporter* reporter)
{
if (reporter) {
registerCallback();
} else {
unregisterCallback();
}
m->reporter = reporter;
}
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*);
}