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
|
/*
* File: connectionmanager.cpp
* Author: savop
*
* Created on 21. August 2009, 18:35
*/
#include <string.h>
#include <upnp/ixml.h>
#include <upnp/upnptools.h>
#include <vdr/tools.h>
#include "upnp/connectionmanager.h"
#include "../common.h"
#include "upnp/dlna.h"
cVirtualConnection::cVirtualConnection() : mRcsID(-1) {}
cConnectionManager::cConnectionManager(UpnpDevice_Handle DeviceHandle) : cUpnpService(DeviceHandle) {
this->mVirtualConnections = new cList<cVirtualConnection>;
this->mDefaultConnection = this->createVirtualConnection();
this->mSupportedProtocols = cDlna::getInstance()->getSupportedProtocols();
}
cConnectionManager::~cConnectionManager() {
delete this->mDefaultConnection;
delete this->mVirtualConnections;
}
int cConnectionManager::subscribe(Upnp_Subscription_Request* Request){
IXML_Document* PropertySet = NULL;
/* The protocol infos which this server supports */
UpnpAddToPropertySet(&PropertySet, "SourceProtocolInfo", this->mSupportedProtocols);
/* Not set, this field is only used by Media Renderers */
UpnpAddToPropertySet(&PropertySet, "SinkProtocolInfo", "");
/* The current connection IDs of all virtual connections */
const char* IDs = this->getConnectionIDsCVS();
if(!IDs){
return UPNP_E_INTERNAL_ERROR;
}
UpnpAddToPropertySet(&PropertySet, "CurrentConnectionIDs", IDs);
// Accept subscription
int ret = UpnpAcceptSubscriptionExt(this->mDeviceHandle, Request->UDN, Request->ServiceId, PropertySet, Request->Sid);
if(ret != UPNP_E_SUCCESS){
ERROR("Subscription failed (Error code: %d)", ret);
}
ixmlDocument_free(PropertySet);
return ret;
}
int cConnectionManager::execute(Upnp_Action_Request* Request){
if (Request == NULL) {
ERROR("CMS Action Handler - request is null");
return UPNP_E_BAD_REQUEST;
}
if(!strcmp(Request->ActionName, UPNP_CMS_ACTION_GETPROTOCOLINFO))
return this->getProtocolInfo(Request);
if(!strcmp(Request->ActionName, UPNP_CMS_ACTION_GETCURRENTCONNECTIONIDS))
return this->getCurrentConnectionIDs(Request);
if(!strcmp(Request->ActionName, UPNP_CMS_ACTION_GETCURRENTCONNECTIONINFO))
return this->getCurrentConnectionInfo(Request);
if(!strcmp(Request->ActionName, UPNP_CMS_ACTION_PREPAREFORCONNECTION))
return this->prepareForConnection(Request);
if(!strcmp(Request->ActionName, UPNP_CMS_ACTION_CONNECTIONCOMPLETE))
return this->connectionComplete(Request);
return UPNP_E_BAD_REQUEST;
}
int cConnectionManager::getProtocolInfo(Upnp_Action_Request* Request){
MESSAGE(VERBOSE_CMS, "Protocol info requested by %s.", inet_ntoa(Request->CtrlPtIPAddr));
cString Result = cString::sprintf(
"<u:%sResponse xmlns:u=\"%s\"> \
<Source>%s</Source> \
<Sink></Sink> \
</u:%sResponse>",
Request->ActionName,
UPNP_CMS_SERVICE_TYPE,
*this->mSupportedProtocols,
Request->ActionName
);
Request->ActionResult = ixmlParseBuffer(Result);
Request->ErrCode = UPNP_E_SUCCESS;
return Request->ErrCode;
}
int cConnectionManager::getCurrentConnectionIDs(Upnp_Action_Request* Request){
MESSAGE(VERBOSE_CMS, "Current connection IDs requested by %s.", inet_ntoa(Request->CtrlPtIPAddr));
cString Result;
const char* IDs = this->getConnectionIDsCVS();
if(!IDs){
Request->ErrCode = UPNP_E_INTERNAL_ERROR;
return Request->ErrCode;
}
Result = cString::sprintf(
"<u:%sResponse xmlns:u=\"%s\"> \
<ConnectionIDs>%s</ConnectionIDs> \
</u:%sResponse>",
Request->ActionName,
UPNP_CMS_SERVICE_TYPE,
IDs,
Request->ActionName
);
Request->ActionResult = ixmlParseBuffer(Result);
Request->ErrCode = UPNP_E_SUCCESS;
return Request->ErrCode;
}
int cConnectionManager::getCurrentConnectionInfo(Upnp_Action_Request* Request){
MESSAGE(VERBOSE_CMS, "Current connection info requested by %s.", inet_ntoa(Request->CtrlPtIPAddr));
int ConnectionID;
if(this->parseIntegerValue(Request->ActionRequest, "ConnectionID", &ConnectionID) != 0){
ERROR("Invalid arguments. ConnectionID missing or wrong");
this->setError(Request, 402);
return Request->ErrCode;
}
cVirtualConnection* Connection;
for(Connection = this->mVirtualConnections->First(); Connection && Connection->mConnectionID != ConnectionID; Connection = this->mVirtualConnections->Next(Connection)){}
if(Connection){
cString Result = cString::sprintf(
"<u:%sResponse xmlns:u=\"%s\">\
<ProtocolInfo>%s</ProtocolInfo>\
<PeerConnectionManager>%s</PeerConnectionManager>\
<PeerConnectionID>%d</PeerConnectionID>\
<Direction>%s</Direction>\
<RcsID>%d</RcsID>\
<AVTransportID>%d</AVTransportID>\
<Status>%s</Status>\
</u:%sResponse>",
Request->ActionName,
UPNP_CMS_SERVICE_TYPE,
*Connection->mRemoteProtocolInfo,
*Connection->mRemoteConnectionManager,
-1,
cVirtualConnection::getDirectionString(OUTPUT),
Connection->mRcsID,
Connection->mAVTransportID,
cVirtualConnection::getStatusString(Connection->mStatus),
Request->ActionName
);
Request->ActionResult = ixmlParseBuffer(Result);
Request->ErrCode = UPNP_E_SUCCESS;
}
else {
ERROR("No valid connection found with given ID=%d!", ConnectionID);
this->setError(Request, 706);
}
return Request->ErrCode;
}
int cConnectionManager::prepareForConnection(Upnp_Action_Request* Request){
MESSAGE(VERBOSE_CMS, "Request for a new connection by %s.", inet_ntoa(Request->CtrlPtIPAddr));
//char* Result = NULL;
char* RemoteProtocolInfo = NULL;
char* PeerConnectionManager = NULL;
int PeerConnectionID = 0;
char* DirectionStr = NULL;
int Direction;
if(this->parseStringValue(Request->ActionRequest, "RemoteProtocolInfo", &RemoteProtocolInfo) != 0){
ERROR("Invalid argument RemoteProtocolInfo: Missing or wrong");
this->setError(Request, 402);
return Request->ErrCode;
}
if(this->parseStringValue(Request->ActionRequest, "PeerConnectionManager", &PeerConnectionManager) != 0){
ERROR("Invalid argument PeerConnectionManager: Missing or wrong");
this->setError(Request, 402);
return Request->ErrCode;
}
if(this->parseStringValue(Request->ActionRequest, "Direction", &DirectionStr) != 0 && (Direction = cVirtualConnection::getDirection(DirectionStr)) == -1){
ERROR("Invalid argument Direction: Missing or wrong");
this->setError(Request, 402);
return Request->ErrCode;
}
if(this->parseIntegerValue(Request->ActionRequest, "PeerConnectionID", &PeerConnectionID) != 0){
ERROR("Invalid argument PeerConnectionID: Missing or wrong");
this->setError(Request, 402);
return Request->ErrCode;
}
/* TODO:
Create Connection
Notify AVTransport that a new connection was established
Send back the response */
this->setError(Request, UPNP_SOAP_E_ACTION_NOT_IMPLEMENTED);
return Request->ErrCode;
}
int cConnectionManager::connectionComplete(Upnp_Action_Request* Request){
MESSAGE(VERBOSE_CMS, "Request for closing an open connection by %s.", inet_ntoa(Request->CtrlPtIPAddr));
//char* Result = NULL;
int ConnectionID;
if(this->parseIntegerValue(Request->ActionRequest, "ConnectionID", &ConnectionID) != 0){
ERROR("Invalid argument ConnectionID: Missing or wrong");
this->setError(Request, 402);
return Request->ErrCode;
}
// TODO:
// Close and clear any open resources
// Close and delete the connection
// Free other resources left
this->setError(Request, UPNP_SOAP_E_ACTION_NOT_IMPLEMENTED);
return Request->ErrCode;
}
//bool cConnectionManager::setProtocolInfo(const char* ProtocolInfo){
// if(strcmp(this->mSupportedProtocols, ProtocolInfo)){
// // ProtocolInfo changed, save and invoke a event notification
// this->mSupportedProtocols = ProtocolInfo;
//
// IXML_Document* PropertySet = NULL;
// UpnpAddToPropertySet(&PropertySet, "SourceProtocolInfo", this->mSupportedProtocols);
// int ret = UpnpNotifyExt(this->mDeviceHandle, UPNP_DEVICE_UDN, UPNP_CMS_SERVICE_ID, PropertySet);
// ixmlDocument_free(PropertySet);
//
// if(ret != UPNP_E_SUCCESS){
// ERROR("State change notification failed (Error code: %d)",ret);
// return false;
// }
// }
// return true;
//}
cVirtualConnection* cConnectionManager::createVirtualConnection(const char* RemoteProtocolInfo, const char* RemoteConnectionManager, int RemoteConnectionID, eDirection Direction){
static int lastConnectionID = 0;
MESSAGE(VERBOSE_CMS, "Create virtual connection");
if(lastConnectionID == 2147483647) lastConnectionID = 1;
cVirtualConnection* Connection = new cVirtualConnection;
// AVT is available
Connection->mAVTransportID = 0;
// The ProtocolInfo of the remote device (i.e. Media Renderer)
Connection->mRemoteProtocolInfo = RemoteProtocolInfo;
// The responsible connection manager
Connection->mRemoteConnectionManager = RemoteConnectionManager;
// The virtual connection direction is output
Connection->mDirection = Direction;
// The remote connection ID, -1 says ID is unknown
Connection->mRemoteConnectionID = RemoteConnectionID;
// Connection status, assume that its ok.
Connection->mStatus = OK;
// new assigned ConnectionID
Connection->mConnectionID = lastConnectionID++;
// Notify the subscribers
IXML_Document* PropertySet = NULL;
const char* IDs = this->getConnectionIDsCVS();
if(!IDs){
return NULL;
}
UpnpAddToPropertySet(&PropertySet, "CurrentConnectionIDs", IDs);
int ret = UpnpNotifyExt(this->mDeviceHandle, UPNP_DEVICE_UDN, UPNP_CMS_SERVICE_ID, PropertySet);
ixmlDocument_free(PropertySet);
if(ret != UPNP_E_SUCCESS){
ERROR("State change notification failed (Error code: %d)",ret);
return NULL;
}
MESSAGE(VERBOSE_CMS, "Notification of connection creation sent");
this->mVirtualConnections->Add(Connection);
return Connection;
}
bool cConnectionManager::destroyVirtualConnection(int ConnectionID){
if(ConnectionID == 0){
ERROR("Cannot delete default connection with ID 0!");
return false;
}
cVirtualConnection* Connection;
for(Connection = this->mVirtualConnections->First(); Connection && Connection->mConnectionID != ConnectionID; Connection = this->mVirtualConnections->Next(Connection)){}
if(Connection){
this->mVirtualConnections->Del(Connection);
// Notify the subscribers
IXML_Document* PropertySet = NULL;
const char* IDs = this->getConnectionIDsCVS();
if(!IDs){
return false;
}
UpnpAddToPropertySet(&PropertySet, "CurrentConnectionIDs", IDs);
int ret = UpnpNotifyExt(this->mDeviceHandle, UPNP_DEVICE_UDN, UPNP_CMS_SERVICE_ID, PropertySet);
ixmlDocument_free(PropertySet);
if(ret != UPNP_E_SUCCESS){
ERROR("State change notification failed (Error code: %d)",ret);
return false;
}
return true;
}
ERROR("No connection with ID=%d found!", ConnectionID);
return false;
}
const char* cConnectionManager::getConnectionIDsCVS(){
cString IDs;
for(cVirtualConnection* Connection = this->mVirtualConnections->First(); Connection; Connection = this->mVirtualConnections->Next(Connection)){
IDs = cString::sprintf("%s,%d", (*IDs)?*IDs:"", Connection->mConnectionID);
}
return IDs;
}
void cConnectionManager::setError(Upnp_Action_Request* Request, int Error){
Request->ErrCode = Error;
switch(Error){
case 701:
strn0cpy(Request->ErrStr,_("Incompatible protocol info"),LINE_SIZE);
break;
case 702:
strn0cpy(Request->ErrStr,_("Incompatible directions"),LINE_SIZE);
break;
case 703:
strn0cpy(Request->ErrStr,_("Insufficient network resources"),LINE_SIZE);
break;
case 704:
strn0cpy(Request->ErrStr,_("Local restrictions"),LINE_SIZE);
break;
case 705:
strn0cpy(Request->ErrStr,_("Access denied"),LINE_SIZE);
break;
case 706:
strn0cpy(Request->ErrStr,_("Invalid connection reference"),LINE_SIZE);
break;
case 707:
strn0cpy(Request->ErrStr,_("Not in network"),LINE_SIZE);
break;
default:
cUpnpService::setError(Request, Error);
break;
}
}
const char* cVirtualConnection::getDirectionString(eDirection Direction){
switch(Direction){
case INPUT:
return "Input";
case OUTPUT:
return "Output";
default:
return NULL;
}
}
const char* cVirtualConnection::getStatusString(eConnectionStatus Status){
switch(Status){
case OK:
return "OK";
case CONTENT_FORMAT_MISMATCH:
return "ContentFormatMismatch";
case INSUFFICIENT_BANDWIDTH:
return "InsufficientBandwidth";
case UNRELIABLE_CHANNEL:
return "UnreliableChannel";
case UNKNOWN:
return "Unknown";
default:
return NULL;
}
}
int cVirtualConnection::getConnectionStatus(const char* eConnectionStatus){
if(!strcasecmp(eConnectionStatus,"OK"))
return OK;
if(!strcasecmp(eConnectionStatus,"ContentFormatMismatch"))
return CONTENT_FORMAT_MISMATCH;
if(!strcasecmp(eConnectionStatus,"InsufficientBandwidth"))
return INSUFFICIENT_BANDWIDTH;
if(!strcasecmp(eConnectionStatus,"UnreliableChannel"))
return UNRELIABLE_CHANNEL;
if(!strcasecmp(eConnectionStatus,"Unknown"))
return UNKNOWN;
return -1;
}
int cVirtualConnection::getDirection(const char* Direction){
if(!strcasecmp(Direction, "Output"))
return OUTPUT;
if(!strcasecmp(Direction, "Input"))
return INPUT;
return -1;
}
|