added: much better improved UPnP Renderer support. Now provides didl for currently...
[xbmc:xbmc-antiquated.git] / xbmc / lib / libUPnP / Platinum / Source / Devices / MediaConnect / PltMediaConnect.cpp
1 /*****************************************************************
2 |
3 |      Platinum - AV Media Connect Device
4 |
5 |      (c) 2004 Sylvain Rebaud
6 |      Author: Sylvain Rebaud (c0diq@yahoo.com)
7 |
8 ****************************************************************/
9
10 /*----------------------------------------------------------------------
11 |       includes
12 +---------------------------------------------------------------------*/
13 #include "Neptune.h"
14 #include "Platinum.h"
15 #include "PltMediaConnect.h"
16
17 /*----------------------------------------------------------------------
18 |       forward references
19 +---------------------------------------------------------------------*/
20 extern NPT_UInt8 X_MS_MediaReceiverRegistrarSCPD[];
21
22 /*----------------------------------------------------------------------
23 |       PLT_MediaConnect::PLT_MediaConnect
24 +---------------------------------------------------------------------*/
25 PLT_MediaConnect::PLT_MediaConnect(const char*  path, 
26                                    const char*  friendly_name, 
27                                    bool         show_ip, 
28                                    const char*  udn, 
29                                    unsigned int port,
30                                    NPT_UInt16   fileserver_port) :      
31     PLT_FileMediaServer(path, friendly_name, show_ip, udn, port, fileserver_port)
32 {
33     m_RegistrarService = new PLT_Service(
34         this,
35         "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1", 
36         "urn:microsoft.com:serviceId:X_MS_MediaReceiverRegistrar");
37
38     if (NPT_SUCCEEDED(m_RegistrarService->SetSCPDXML((const char*) X_MS_MediaReceiverRegistrarSCPD))) {
39         m_RegistrarService->InitURLs("X_MS_MediaReceiverRegistrar", m_UUID);
40         AddService(m_RegistrarService);
41         m_RegistrarService->SetStateVariable("AuthorizationGrantedUpdateID", "0", false);
42         m_RegistrarService->SetStateVariable("AuthorizationDeniedUpdateID", "0", false);
43         m_RegistrarService->SetStateVariable("ValidationSucceededUpdateID", "0", false);
44         m_RegistrarService->SetStateVariable("ValidationRevokedUpdateID", "0", false);
45     }
46
47     m_ModelName = "Windows Media Connect";
48     m_ModelNumber = "2.0";
49     m_ModelDescription = "Media Server";
50     m_Manufacturer = "Plutinosoft";
51     m_ManufacturerURL = "http://www.plutinosoft.com/";
52     m_ModelURL = "http://www.plutinosoft.com";
53     m_DlnaDoc = "DMS-1.00";
54 }
55
56 /*----------------------------------------------------------------------
57 |       PLT_MediaConnect::~PLT_MediaConnect
58 +---------------------------------------------------------------------*/
59 PLT_MediaConnect::~PLT_MediaConnect()
60 {
61 }
62
63 /*----------------------------------------------------------------------
64 |       PLT_MediaConnect::Authorize
65 +---------------------------------------------------------------------*/
66 NPT_Result
67 PLT_MediaConnect::Authorize(PLT_MediaConnectInfo* info, bool state)
68 {
69     info->m_Authorized = state;
70     if (state == true) {
71         return m_RegistrarService->IncStateVariable("AuthorizationGrantedUpdateID");
72     }
73
74     return m_RegistrarService->IncStateVariable("AuthorizationDeniedUpdateID");
75 }
76
77 /*----------------------------------------------------------------------
78 |       PLT_MediaConnect::Validate
79 +---------------------------------------------------------------------*/
80 NPT_Result
81 PLT_MediaConnect::Validate(PLT_MediaConnectInfo* info, bool state)
82 {
83     info->m_Validated = state;
84     if (state == true) {
85         return m_RegistrarService->IncStateVariable("ValidationSucceededUpdateID");
86     }
87
88     return m_RegistrarService->IncStateVariable("ValidationRevokedUpdateID");
89 }
90
91 /*----------------------------------------------------------------------
92 |       PLT_MediaConnect::OnAction
93 +---------------------------------------------------------------------*/
94 NPT_Result
95 PLT_MediaConnect::OnAction(PLT_ActionReference&          action, 
96                            const NPT_HttpRequestContext& context)
97 {
98       PLT_MediaConnectInfo* mc_info = NULL;
99
100 //    /* get MAC address from IP */
101 //    if (info != NULL) {
102 //        NPT_String ip = info->remote_address.GetIpAddress().ToString();
103 //        NPT_String MAC;
104 //        GetMACFromIP(ip, MAC);
105 //
106 //        if (MAC.GetLength()) {
107 //            NPT_Result res = m_MediaConnectDeviceInfoMap.Get(MAC, mc_info);
108 //            if (NPT_FAILED(res)) {
109 //                m_MediaConnectDeviceInfoMap.Put(MAC, PLT_MediaConnectInfo());
110 //                m_MediaConnectDeviceInfoMap.Get(MAC, mc_info);
111 //
112 //                // automatically validate for now
113 //                Authorize(mc_info, true);
114 //            }
115 //        }
116 //    }
117 //
118 //    /* verify device is allowed first */
119 //    if (mc_info == NULL || !mc_info->m_Authorized) {
120 //        action->SetError(801, "Access Denied");
121 //        return NPT_SUCCESS;
122 //    }
123
124     /* parse the action name */
125     NPT_String name = action->GetActionDesc()->GetName();
126
127     /* handle X_MS_MediaReceiverRegistrar actions here */
128     if (name.Compare("IsAuthorized") == 0) {
129         return OnIsAuthorized(action, mc_info);
130     }
131     if (name.Compare("RegisterDevice") == 0) {
132         return OnRegisterDevice(action, mc_info);
133     }
134     if (name.Compare("IsValidated") == 0) {
135         return OnIsValidated(action, mc_info);
136     }  
137
138     return PLT_FileMediaServer::OnAction(action, context);
139 }
140
141 /*----------------------------------------------------------------------
142 |       PLT_MediaConnect::LookUpMediaConnectInfo
143 +---------------------------------------------------------------------*/
144 NPT_Result
145 PLT_MediaConnect::LookUpMediaConnectInfo(NPT_String             deviceID, 
146                                          PLT_MediaConnectInfo*& mc_info)
147 {
148     mc_info = NULL;
149
150     if (deviceID.GetLength()) {
151         /* lookup the MAC from the UDN */
152         NPT_String* MAC;
153         if (NPT_SUCCEEDED(m_MediaConnectUDNMap.Get(deviceID, MAC))) {
154             /* lookup the PLT_MediaConnectInfo from the MAC now */
155             return m_MediaConnectDeviceInfoMap.Get(*MAC, mc_info);
156         }
157     }
158
159     return NPT_FAILURE;
160 }
161
162 /*----------------------------------------------------------------------
163 |       PLT_MediaConnect::OnIsAuthorized
164 +---------------------------------------------------------------------*/
165 NPT_Result
166 PLT_MediaConnect::OnIsAuthorized(PLT_ActionReference&  action, 
167                                  PLT_MediaConnectInfo* mc_info)
168 {
169     bool authorized = true;
170
171     NPT_String deviceID;
172     action->GetArgumentValue("DeviceID", deviceID);
173
174     /* is there a device ID passed ? */
175     if (deviceID.GetLength()) {
176         /* lookup the MediaConnectInfo from the UDN */
177         NPT_String MAC;
178         PLT_MediaConnectInfo* device_info;
179         if (NPT_FAILED(LookUpMediaConnectInfo(deviceID, device_info))) {
180             authorized = false;
181         } else {
182             authorized = device_info->m_Authorized;
183         }
184     } else {
185         authorized = mc_info?mc_info->m_Authorized:true;
186     }
187
188     action->SetArgumentValue("Result", authorized?"1":"0");
189     return NPT_SUCCESS;
190 }
191
192 /*----------------------------------------------------------------------
193 |       PLT_MediaConnect::OnRegisterDevice
194 +---------------------------------------------------------------------*/
195 NPT_Result
196 PLT_MediaConnect::OnRegisterDevice(PLT_ActionReference&  action, 
197                                    PLT_MediaConnectInfo* mc_info)
198 {
199     NPT_COMPILER_UNUSED(mc_info);
200
201     NPT_String reqMsgBase64;
202     action->GetArgumentValue("RegistrationReqMsg", reqMsgBase64);
203
204     NPT_String respMsgBase64;
205     action->SetArgumentValue("RegistrationRespMsg", respMsgBase64);
206     return NPT_SUCCESS;
207 }
208
209 /*----------------------------------------------------------------------
210 |       PLT_MediaConnect::OnIsValidated
211 +---------------------------------------------------------------------*/
212 NPT_Result
213 PLT_MediaConnect::OnIsValidated(PLT_ActionReference&  action, 
214                                 PLT_MediaConnectInfo* mc_info)
215 {
216     bool validated = true;
217
218     NPT_String deviceID;
219     action->GetArgumentValue("DeviceID", deviceID);
220
221     /* is there a device ID passed ? */
222     if (deviceID.GetLength()) {
223         /* lookup the MediaConnectInfo from the UDN */
224         NPT_String MAC;
225         PLT_MediaConnectInfo* device_info;
226         if (NPT_FAILED(LookUpMediaConnectInfo(deviceID, device_info))) {
227             validated = false;
228         } else {
229             validated = device_info->m_Validated;
230         }
231     } else {
232         validated = mc_info?mc_info->m_Validated:true;
233     }
234
235     action->SetArgumentValue("Result", validated?"1":"0");
236     return NPT_SUCCESS;
237 }