Parent: [30b69c] (diff)

Download this file

device.cxx    451 lines (386 with data), 15.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
448
449
450
/* Copyright (C) 2014 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 "device.hxx"
#include <errno.h> // for ETIMEDOUT, errno
#include <sys/time.h> // for CLOCK_REALTIME
#include <time.h> // for timespec, clock_gettime
#include <iostream> // for endl, operator<<, etc
#include <utility> // for pair
#include "libupnpp/log.hxx" // for LOGERR, LOGFAT, LOGDEB, etc
#include "libupnpp/ixmlwrap.hxx"
#include "libupnpp/upnpplib.hxx" // for LibUPnP
#include "libupnpp/upnpputils.hxx" // for timespec_addnanos
#include "vdir.hxx" // for VirtualDir
using namespace std;
using namespace UPnPP;
namespace UPnPProvider {
unordered_map<std::string, UpnpDevice *> UpnpDevice::o_devices;
PTMutexInit o_devices_lock;
static bool vectorstoargslists(const vector<string>& names,
const vector<string>& values,
vector<string>& qvalues,
vector<const char *>& cnames,
vector<const char *>& cvalues)
{
if (names.size() != values.size()) {
LOGERR("vectorstoargslists: bad sizes" << endl);
return false;
}
cnames.reserve(names.size());
qvalues.clear();
qvalues.reserve(values.size());
cvalues.reserve(values.size());
for (unsigned int i = 0; i < values.size(); i++) {
cnames.push_back(names[i].c_str());
qvalues.push_back(SoapHelp::xmlQuote(values[i]));
cvalues.push_back(qvalues[i].c_str());
//LOGDEB("Edata: " << cnames[i] << " -> " << cvalues[i] << endl);
}
return true;
}
static const int expiretime = 3600;
UpnpDevice::UpnpDevice(const string& deviceId,
const unordered_map<string, VDirContent>& files)
: m_deviceId(deviceId), m_needExit(false), m_evloopcond(PTHREAD_COND_INITIALIZER)
{
//LOGDEB("UpnpDevice::UpnpDevice(" << m_deviceId << ")" << endl);
m_lib = LibUPnP::getLibUPnP(true);
if (!m_lib) {
LOGFAT(" Can't get LibUPnP" << endl);
return;
}
if (!m_lib->ok()) {
LOGFAT("Lib init failed: " <<
m_lib->errAsString("main", m_lib->getInitError()) << endl);
m_lib = 0;
return;
}
{
PTMutexLocker lock(o_devices_lock);
if (o_devices.empty()) {
// First call: init callbacks
m_lib->registerHandler(UPNP_CONTROL_ACTION_REQUEST, sCallBack, this);
m_lib->registerHandler(UPNP_CONTROL_GET_VAR_REQUEST, sCallBack, this);
m_lib->registerHandler(UPNP_EVENT_SUBSCRIPTION_REQUEST, sCallBack,this);
}
o_devices[m_deviceId] = this;
}
VirtualDir* theVD = VirtualDir::getVirtualDir();
if (theVD == 0) {
LOGFAT("UpnpDevice::UpnpDevice: can't get VirtualDir" << endl);
return;
}
unordered_map<string, VDirContent>::const_iterator it =
files.find("description.xml");
if (it == files.end()) {
LOGFAT("UpnpDevice::UpnpDevice: no description.xml found in xmlfiles"
<< endl);
return;
}
const string& description = it->second.content;
for (it = files.begin(); it != files.end(); it++) {
theVD->addFile("/", it->first, it->second.content, it->second.mimetype);
}
// Start up the web server for sending out description files
int ret;
if ((ret = m_lib->setupWebServer(description, &m_dvh)) != 0) {
LOGFAT("UpnpDevice: libupnp can't start service. Err " << ret << endl);
}
if ((ret = UpnpSendAdvertisement(m_dvh, expiretime)) != 0) {
LOGERR(m_lib->errAsString("UpnpDevice: UpnpSendAdvertisement", ret)
<< endl);
}
}
UpnpDevice::~UpnpDevice()
{
UpnpUnRegisterRootDevice(m_dvh);
PTMutexLocker lock(o_devices_lock);
unordered_map<std::string, UpnpDevice *>::iterator it = o_devices.find(m_deviceId);
if (it != o_devices.end())
o_devices.erase(it);
}
// Main libupnp callback: use the device id and call the right device
int UpnpDevice::sCallBack(Upnp_EventType et, void* evp, void* tok)
{
//LOGDEB("UpnpDevice::sCallBack" << endl);
string deviceid;
switch (et) {
case UPNP_CONTROL_ACTION_REQUEST:
deviceid = ((struct Upnp_Action_Request *)evp)->DevUDN;
break;
case UPNP_CONTROL_GET_VAR_REQUEST:
deviceid = ((struct Upnp_State_Var_Request *)evp)->DevUDN;
break;
case UPNP_EVENT_SUBSCRIPTION_REQUEST:
deviceid = ((struct Upnp_Subscription_Request*)evp)->UDN;
break;
default:
LOGERR("UpnpDevice::sCallBack: unknown event " << et << endl);
return UPNP_E_INVALID_PARAM;
}
// LOGDEB("UpnpDevice::sCallBack: deviceid[" << deviceid << "]" << endl);
unordered_map<std::string, UpnpDevice *>::iterator it;
{
PTMutexLocker lock(o_devices_lock);
it = o_devices.find(deviceid);
if (it == o_devices.end()) {
LOGERR("UpnpDevice::sCallBack: Device not found: [" <<
deviceid << "]" << endl);
return UPNP_E_INVALID_PARAM;
}
}
// LOGDEB("UpnpDevice::sCallBack: device found: [" << it->second
// << "]" << endl);
return (it->second)->callBack(et, evp);
}
unordered_map<string, UpnpService*>::const_iterator UpnpDevice::findService(const string& serviceid)
{
PTMutexLocker lock(m_lock);
auto servit = m_servicemap.find(serviceid);
if (servit == m_servicemap.end()) {
LOGERR("UpnpDevice: Bad serviceID: " << serviceid << endl);
}
return servit;
}
int UpnpDevice::callBack(Upnp_EventType et, void* evp)
{
switch (et) {
case UPNP_CONTROL_ACTION_REQUEST:
{
struct Upnp_Action_Request *act = (struct Upnp_Action_Request *)evp;
LOGDEB("UPNP_CONTROL_ACTION_REQUEST: " << act->ActionName <<
". Params: " << ixmlwPrintDoc(act->ActionRequest) << endl);
auto servit = findService(act->ServiceID);
if (servit == m_servicemap.end()) {
return UPNP_E_INVALID_PARAM;
}
SoapData dt;
{
PTMutexLocker lock(m_lock);
const string& servicetype = servit->second->getServiceType();
auto callit = m_calls.find(string(act->ActionName) + string(act->ServiceID));
if (callit == m_calls.end()) {
LOGINF("UpnpDevice: No such action: " << act->ActionName << endl);
return UPNP_E_INVALID_PARAM;
}
SoapArgs sc;
if (!decodeSoapBody(act->ActionName, act->ActionRequest, &sc)) {
LOGERR("Error decoding Action call arguments" << endl);
return UPNP_E_INVALID_PARAM;
}
dt.name = act->ActionName;
dt.serviceType = servicetype;
// Call the action routine
int ret = callit->second(sc, dt);
if (ret != UPNP_E_SUCCESS) {
LOGERR("UpnpDevice: Action failed: " << sc.name << endl);
return ret;
}
}
// Encode result data
act->ActionResult = buildSoapBody(dt);
//LOGDEB("Response data: " << ixmlwPrintDoc(act->ActionResult) << endl);
return UPNP_E_SUCCESS;
}
break;
case UPNP_CONTROL_GET_VAR_REQUEST:
// Note that the "Control: query for variable" action is
// deprecated (upnp arch v1), and we should never get these.
{
struct Upnp_State_Var_Request *act =
(struct Upnp_State_Var_Request *)evp;
LOGDEB("UPNP_CONTROL_GET_VAR__REQUEST?: " << act->StateVarName << endl);
}
break;
case UPNP_EVENT_SUBSCRIPTION_REQUEST:
{
struct Upnp_Subscription_Request *act =
(struct Upnp_Subscription_Request*)evp;
LOGDEB("UPNP_EVENT_SUBSCRIPTION_REQUEST: " << act->ServiceId << endl);
auto servit = findService(act->ServiceId);
if (servit == m_servicemap.end()) {
return UPNP_E_INVALID_PARAM;
}
vector<string> names, values, qvalues;
{
PTMutexLocker lock(m_lock);
if (!servit->second->getEventData(true, names, values)) {
break;
}
}
vector<const char *> cnames, cvalues;
vectorstoargslists(names, values, qvalues, cnames, cvalues);
int ret =
UpnpAcceptSubscription(m_dvh, act->UDN, act->ServiceId,
&cnames[0], &cvalues[0],
int(cnames.size()), act->Sid);
if (ret != UPNP_E_SUCCESS) {
LOGERR(m_lib->errAsString(
"UpnpDevice::callBack: UpnpAcceptSubscription", ret) <<
endl);
}
return ret;
}
break;
default:
LOGINF("UpnpDevice::callBack: unknown libupnp event type: " <<
LibUPnP::evTypeAsString(et).c_str() << endl);
return UPNP_E_INVALID_PARAM;
}
return UPNP_E_INVALID_PARAM;
}
void UpnpDevice::addService(UpnpService *serv, const std::string& serviceId)
{
PTMutexLocker lock(m_lock);
m_servicemap[serviceId] = serv;
m_serviceids.push_back(serviceId);
}
void UpnpDevice::addActionMapping(const UpnpService* serv,
const std::string& actName,
soapfun fun)
{
PTMutexLocker lock(m_lock);
// LOGDEB("UpnpDevice::addActionMapping:" << actName << endl);
m_calls[actName + serv->getServiceId()] = fun;
}
void UpnpDevice::notifyEvent(const string& serviceId,
const vector<string>& names,
const vector<string>& values)
{
LOGDEB1("UpnpDevice::notifyEvent " << serviceId << " " <<
(names.empty()?"Empty names??":names[0]) << endl);
if (names.empty())
return;
vector<const char *> cnames, cvalues;
vector<string> qvalues;
vectorstoargslists(names, values, qvalues, cnames, cvalues);
int ret = UpnpNotify(m_dvh, m_deviceId.c_str(),
serviceId.c_str(), &cnames[0], &cvalues[0],
int(cnames.size()));
if (ret != UPNP_E_SUCCESS) {
LOGERR(m_lib->errAsString("UpnpDevice::notifyEvent", ret) << endl);
}
}
int timespec_diffms(const struct timespec& old, const struct timespec& recent)
{
return (recent.tv_sec - old.tv_sec) * 1000 +
(recent.tv_nsec - old.tv_nsec) / (1000 * 1000);
}
#ifndef CLOCK_REALTIME
// Mac OS X for one does not have clock_gettime. Gettimeofday is more than
// enough for our needs.
#define CLOCK_REALTIME 0
int clock_gettime(int /*clk_id*/, struct timespec* t) {
struct timeval now;
int rv = gettimeofday(&now, NULL);
if (rv) return rv;
t->tv_sec = now.tv_sec;
t->tv_nsec = now.tv_usec * 1000;
return 0;
}
#endif // ! CLOCK_REALTIME
// Loop on services, and poll each for changed data. Generate event
// only if changed data exists. Every now and then, we generate an
// artificial event with all the current state. This is normally run by the main thread.
void UpnpDevice::eventloop()
{
int count = 0;
const int loopwait_ms = 1000; // Polling the services every 1 S
const int nloopstofull = 10; // Full state every 10 S
struct timespec wkuptime, earlytime;
bool didearly = false;
for (;;) {
clock_gettime(CLOCK_REALTIME, &wkuptime);
timespec_addnanos(&wkuptime, loopwait_ms * 1000 * 1000);
//LOGDEB("eventloop: now " << time(0) << " wkup at "<<
// wkuptime.tv_sec << " S " << wkuptime.tv_nsec << " ns" << endl);
PTMutexLocker lock(m_evlooplock);
int err = pthread_cond_timedwait(&m_evloopcond, lock.getMutex(),
&wkuptime);
if (m_needExit) {
break;
} else if (err && err != ETIMEDOUT) {
LOGINF("UpnpDevice:eventloop: wait errno " << errno << endl);
break;
} else if (err == 0) {
// Early wakeup. Only does something if it did not already
// happen recently
if (didearly) {
int millis = timespec_diffms(earlytime, wkuptime);
if (millis < loopwait_ms) {
// Do nothing. didearly stays true
// LOGDEB("eventloop: early, previous too close "<<endl);
continue;
} else {
// had an early wakeup previously, but it was a
// long time ago. Update state and wakeup
// LOGDEB("eventloop: early, previous is old "<<endl);
earlytime = wkuptime;
}
} else {
// Early wakeup, previous one was normal. Remember.
// LOGDEB("eventloop: early, no previous" << endl);
didearly = true;
earlytime = wkuptime;
}
} else {
// Normal wakeup
// LOGDEB("eventloop: normal wakeup" << endl);
didearly = false;
}
count++;
bool all = count && ((count % nloopstofull) == 0);
//LOGDEB("UpnpDevice::eventloop count "<<count<<" all "<<all<<endl);
// We can't lock m_lock around the loop because we don't want
// to hold id when calling notifyEvent() (which calls
// libupnp). This means that we should have a separate lock
// for the services arrays. This would only be useful during
// startup, while we add services, but the event loop is the
// last call the main program will make after adding the
// services, so locking does not seem necessary
for (auto it = m_serviceids.begin(); it != m_serviceids.end(); it++) {
vector<string> names, values;
{
PTMutexLocker lock(m_lock);
UpnpService* serv = m_servicemap[*it];
if (!serv->getEventData(all, names, values) || names.empty()) {
continue;
}
}
notifyEvent(*it, names, values);
}
}
}
// Can't take the loop lock here. We're called from the service and
// hold the device lock. The locks would be taken in opposite order,
// causing a potential deadlock:
// - device action takes device lock
// - loop wakes up, takes loop lock
// - blocks on device lock before calling getevent
// - device calls loopwakeup which blocks on loop lock
// -> deadlock
void UpnpDevice::loopWakeup()
{
pthread_cond_broadcast(&m_evloopcond);
}
void UpnpDevice::shouldExit()
{
m_needExit = true;
pthread_cond_broadcast(&m_evloopcond);
}
}// End namespace UPnPProvider