summaryrefslogtreecommitdiff
path: root/server/connectionManager.cpp
blob: 64f68dc802809af1a0d8bae3b004769fe939d0bf (plain)
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
/*
 * connectionManager.cpp
 *
 *  Created on: 27.08.2012
 *      Author: savop
 */

#include "../include/server.h"
#include "../include/connectionManager.h"
#include "../include/tools.h"
#include "../include/media/mediaManager.h"
#include <vdr/i18n.h>
#include <upnp/upnptools.h>
#include <string>
#include <sstream>

namespace upnp {

#define UPNP_CMS_ACTION_GETPROTOCOLINFO "GetProtocolInfo"
#define UPNP_CMS_ACTION_GETCURRENTCONNECTIONIDS "GetCurrentConnectionIDs"
#define UPNP_CMS_ACTION_GETCURRENTCONNECTIONINFO "GetCurrentConnectionInfo"
#define UPNP_CMS_ACTION_PREPAREFORCONNECTION "PrepareForConnection"
#define UPNP_CMS_ACTION_CONNECTIONCOMPLETE "ConnectionComplete"

cConnectionManager::cConnectionManager()
: cUPnPService(
    cUPnPService::Description(
        "urn:schemas-upnp-org:service:ConnectionManager:1",
        "urn:upnp-org:serviceId:ConnectionManager",
        "cms_scpd.xml",
        "cms_control",
        "cms_event"
        ))
{

  cVirtualConnection* connection = cVirtualConnection::GenerateVirtualConnection(std::string(), std::string(),
                                                                                -1, cVirtualConnection::VC_OUTPUT);
  mVirtualConnections[connection->GetConnectionID()] = connection;
}

cConnectionManager::~cConnectionManager(){
  DeleteConnections();
}

int cConnectionManager::Subscribe(Upnp_Subscription_Request* request){
  IXML_Document* PropertySet = NULL;

  std::string protocolInfo = tools::StringListToCSV(mMediaServer->GetManager().GetSupportedProtocolInfos());

  /* The protocol infos which this server supports */
  UpnpAddToPropertySet(&PropertySet, "SourceProtocolInfo", protocolInfo.c_str());
  /* Not set, this field is only used by Media Renderers */
  UpnpAddToPropertySet(&PropertySet, "SinkProtocolInfo", "");
  /* The current connection IDs of all virtual connections */
  UpnpAddToPropertySet(&PropertySet, "CurrentConnectionIDs", GetConnectionIDsCVS().c_str());
  // Accept subscription
  int ret = UpnpAcceptSubscriptionExt(this->mDeviceHandle, request->UDN, request->ServiceId, PropertySet, request->Sid);

  if(ret != UPNP_E_SUCCESS){
    esyslog("UPnP\tSubscription failed (Error code: %d)", ret);
  }

  ixmlDocument_free(PropertySet);
  return ret;
}

int cConnectionManager::Execute(Upnp_Action_Request* request){
  if (request == NULL) {
    esyslog("UPnP\tCMS 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::GetCurrentConnectionIDs(Upnp_Action_Request* request){
  std::string IDs = this->GetConnectionIDsCVS();
  if(IDs.empty()){
    SetError(request, UPNP_E_INTERNAL_ERROR);
    return request->ErrCode;
  }

  std::stringstream ss;

  ss << "<u:" << request->ActionName << "Response xmlns:u=\"" << GetServiceDescription().serviceType << "\">"
     << "  <ConnectionIDs>" << IDs << "</ConnectionIDs>"
     << "</u:" << request->ActionName << "Response>";

  request->ActionResult = ixmlParseBuffer(ss.str().c_str());
  request->ErrCode = UPNP_E_SUCCESS;
  return request->ErrCode;
}

int cConnectionManager::GetCurrentConnectionInfo(Upnp_Action_Request* request){

  long id;
  if(ParseIntegerValue(request->ActionRequest, "ConnectionID", id) != UPNP_E_SUCCESS){
    esyslog("UPnP\tInvalid arguments. ConnectionID missing or wrong");
    SetError(request, UPNP_SOAP_E_INVALID_ARGS);
    return request->ErrCode;
  }
  int32_t connectionID = id;

  cVirtualConnection* connection = mVirtualConnections[connectionID];

  if(connection == NULL){
    esyslog("UPnP\tNo valid connection found with given ID=%d!", connectionID);
    SetError(request, UPNP_CMS_E_INVALID_CONNECTION_REFERENCE);
    return request->ErrCode;
  }

  std::stringstream ss;
  ss << "<u:" << request->ActionName << "Response xmlns:u=\"" << GetServiceDescription().serviceType << "\">"
     << "  <ProtocolInfo>" << connection->GetRemoteProtocolInfo() << "</ProtocolInfo>"
     << "  <PeerConnectionManager>" << connection->GetPeerConnectionManager() << "</PeerConnectionManager>"
     << "  <PeerConnectionID>" << connection->GetPeerConnectionID() << "</PeerConnectionID>"
     << "  <Direction>" << connection->GetDirectionString() << "</Direction>"
     << "  <RcsID>" << connection->GetRcsID() << "</RcsID>"
     << "  <AVTransportID>" << connection->GetAVTransportID() << "</AVTransportID>"
     << "  <Status>" << connection->GetStatusString() << "</Status>"
     << "</u:" << request->ActionName << "Response>";

  request->ActionResult = ixmlParseBuffer(ss.str().c_str());
  request->ErrCode = UPNP_E_SUCCESS;
  return request->ErrCode;
}

int cConnectionManager::GetProtocolInfo(Upnp_Action_Request* request){
  std::stringstream ss;

  std::string protocolInfo = tools::StringListToCSV(mMediaServer->GetManager().GetSupportedProtocolInfos());

  ss << "<u:" << request->ActionName << "Response xmlns:u=\"" << GetServiceDescription().serviceType << "\">"
     << "  <Source>" << protocolInfo.c_str() << "</Source>"
     << "  <Sink></Sink>"
     << "</u:" << request->ActionName << "Response>";

  request->ActionResult = ixmlParseBuffer(ss.str().c_str());
  request->ErrCode = UPNP_E_SUCCESS;
  return request->ErrCode;
}

int cConnectionManager::ConnectionComplete(Upnp_Action_Request* request){
  SetError(request, UPNP_SOAP_E_ACTION_NOT_IMPLEMENTED);
  return request->ErrCode;
}

int cConnectionManager::PrepareForConnection(Upnp_Action_Request* request){
  SetError(request, UPNP_SOAP_E_ACTION_NOT_IMPLEMENTED);
  return request->ErrCode;
}

const std::string cConnectionManager::GetConnectionIDsCVS(){
  std::stringstream ss;

  ConnectionList::iterator it = mVirtualConnections.begin();
  ss << (*it).second->GetConnectionID();
  for(++it; it != mVirtualConnections.end(); ++it){
    ss << "," << (*it).second->GetConnectionID();
  }

  return ss.str();
}

void cConnectionManager::DeleteConnections(){
  for(ConnectionList::iterator it = mVirtualConnections.begin(); it != mVirtualConnections.end();++it){
    cVirtualConnection::DestroyVirtualConnection((*it).second);
  }
  mVirtualConnections.clear();
}

bool cConnectionManager::DeleteConnection(int32_t connectionID){
  if(connectionID == 0){
    esyslog("UPnP\tCannot delete default connection with connectionID = 0");
    return false;
  }

  cVirtualConnection::DestroyVirtualConnection(mVirtualConnections[connectionID]);
  mVirtualConnections.erase(connectionID);

  return OnConnectionChange();
}

bool cConnectionManager::CreateConnection(const std::string & remoteProtocolInfo,
    const std::string & peerConnectionManager,
    int32_t peerConnectionID,
    const std::string & direction)
{
  cVirtualConnection* connection = cVirtualConnection::GenerateVirtualConnection(
      remoteProtocolInfo, peerConnectionManager,
      peerConnectionID, direction);

  mVirtualConnections[connection->GetConnectionID()] = connection;

  return OnConnectionChange();
}

bool cConnectionManager::OnConnectionChange(){
  IXML_Document* PropertySet = NULL;
  UpnpAddToPropertySet(&PropertySet, "CurrentConnectionIDs", GetConnectionIDsCVS().c_str());
  int ret = UpnpNotifyExt(mDeviceHandle, mMediaServer->GetDeviceUUID().c_str(), mServiceDescription.serviceID.c_str(), PropertySet);
  ixmlDocument_free(PropertySet);

  if(ret != UPNP_E_SUCCESS){
      esyslog("UPnP\tState change notification failed (Error code: %d)",ret);
      return false;
  }

  return true;
}

void cConnectionManager::SetError(Upnp_Action_Request* request, int error){
  request->ErrCode = error;
  switch(error){
  case UPNP_CMS_E_INCOMPATIBLE_PROTOCOL_INFO:
    strn0cpy(request->ErrStr,tr("Incompatible protocol info"),LINE_SIZE);
    break;
  case UPNP_CMS_E_INCOMPATIBLE_DIRECTIONS:
    strn0cpy(request->ErrStr,tr("Incompatible directions"),LINE_SIZE);
    break;
  case UPNP_CMS_E_INSUFFICIENT_RESOURCES:
    strn0cpy(request->ErrStr,tr("Insufficient network resources"),LINE_SIZE);
    break;
  case UPNP_CMS_E_LOCAL_RESTRICTIONS:
    strn0cpy(request->ErrStr,tr("Local restrictions"),LINE_SIZE);
    break;
  case UPNP_CMS_E_ACCESS_DENIED:
    strn0cpy(request->ErrStr,tr("Access denied"),LINE_SIZE);
    break;
  case UPNP_CMS_E_INVALID_CONNECTION_REFERENCE:
    strn0cpy(request->ErrStr,tr("Invalid connection reference"),LINE_SIZE);
    break;
  case UPNP_CMS_E_NOT_IN_NETWORK:
    strn0cpy(request->ErrStr,tr("Not in network"),LINE_SIZE);
    break;
  default:
    cUPnPService::SetError(request, error);
    break;
  }
}

}  // namespace upnp