Download this file

service.cxx    253 lines (219 with data), 8.2 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
/* 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 "libupnpp/control/service.hxx"
#include <upnp/upnp.h> // for Upnp_Event, UPNP_E_SUCCESS, etc
#include <upnp/upnptools.h> // for UpnpGetErrorMessage
#include <functional> // for function
#include <string> // for string, char_traits, etc
#include <unordered_map> // for unordered_map, operator!=, 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/ptmutex.hxx" // for PTMutexLocker, PTMutexInit
#include "libupnpp/upnpp_p.hxx" // for caturl
#include "libupnpp/upnpplib.hxx" // for LibUPnP
using namespace std;
using namespace std::placeholders;
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);
}
};
Service::Service(const UPnPDeviceDesc& device,
const UPnPServiceDesc& service, bool doSubscribe)
: m_reporter(0),
m_actionURL(caturl(device.URLBase, service.controlURL)),
m_eventURL(caturl(device.URLBase, service.eventSubURL)),
m_serviceType(service.serviceType),
m_deviceId(device.UDN),
m_friendlyName(device.friendlyName),
m_manufacturer(device.manufacturer),
m_modelName(device.modelName)
{
// Only does anything the first time
initEvents();
if (doSubscribe)
subscribe();
}
Service::~Service()
{
LOGDEB1("Service::~Service: unregister " << m_SID << endl);
if (m_SID[0]) {
unSubscribe();
o_calls.erase(m_SID);
}
}
int Service::runAction(const SoapEncodeInput& args, SoapDecodeOutput& 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 = buildSoapBody(args, false)) == 0) {
LOGINF("Service::runAction: buildSoapBody failed" << endl);
return UPNP_E_OUTOF_MEMORY;
}
LOGDEB1("Service::runAction: 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) {
LOGINF("Service::runAction: UpnpSendAction failed: " << ret <<
" : " << UpnpGetErrorMessage(ret) << " for " <<
ixmlwPrintDoc(request) << endl);
return ret;
}
LOGDEB1("Service::runAction: rslt: [" <<
ixmlwPrintDoc(response) << "]" << endl);
if (!decodeSoapBody(args.name.c_str(), response, &data)) {
LOGERR("Service::runAction: Could not decode response: " <<
ixmlwPrintDoc(response) << endl);
return UPNP_E_BAD_RESPONSE;
}
return UPNP_E_SUCCESS;
}
static PTMutexInit cblock;
int Service::srvCB(Upnp_EventType et, void* vevp, void*)
{
PTMutexLocker lock(cblock);
LOGDEB1("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;
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);
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);
PTMutexLocker 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 UPNP_E_OUTOF_MEMORY;
}
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 UPNP_E_OUTOF_MEMORY;
}
int ret = UpnpUnSubscribe(lib->getclh(), m_SID);
if (ret != UPNP_E_SUCCESS) {
LOGERR("Service:unSubscribe: failed: " << ret << " : " <<
UpnpGetErrorMessage(ret) << endl);
return false;
}
return true;
}
void Service::registerCallback(evtCBFunc c)
{
PTMutexLocker lock(cblock);
LOGDEB1("Service::registerCallback: " << m_SID << endl);
o_calls[m_SID] = c;
}
std::unordered_map<std::string, evtCBFunc> Service::o_calls;
}