1 /*
2  *			GPAC - Multimedia Framework C SDK
3  *
4  *			Authors: Jean Le Feuvre
5  *			Copyright (c) Telecom ParisTech 2009-2012
6  *			All rights reserved
7  *
8  *  This file is part of GPAC / Platinum UPnP module
9  *
10  *  GPAC is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU Lesser General Public License as published by
12  *  the Free Software Foundation; either version 2, or (at your option)
13  *  any later version.
14  *
15  *  GPAC is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU Lesser General Public License for more details.
19  *
20  *  You should have received a copy of the GNU Lesser General Public
21  *  License along with this library; see the file COPYING.  If not, write to
22  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  *
25  *	----------------------------------------------------------------------------------
26  *		PLATINUM IS LICENSED UNDER GPL or commercial agreement - cf platinum license
27  *	----------------------------------------------------------------------------------
28  *
29  */
30 
31 
32 #ifndef _GPAC_GENERIC_DEVICE_H_
33 #define _GPAC_GENERIC_DEVICE_H_
34 
35 #include "Neptune.h"
36 #include "PltCtrlPoint.h"
37 #include "PltDeviceHost.h"
38 #include "PltService.h"
39 
40 #include <gpac/thread.h>
41 #include <gpac/list.h>
42 
43 #ifdef GPAC_HAS_SPIDERMONKEY
44 #include <gpac/internal/scenegraph_dev.h>
45 #include <gpac/internal/smjs_api.h>
46 #endif
47 
48 class GF_UPnP;
49 class GPAC_ServiceItem;
50 
51 class GPAC_DeviceItem
52 {
53 public:
54 	GPAC_DeviceItem(PLT_DeviceDataReference device, NPT_String uuid);
55 	~GPAC_DeviceItem();
56 
57 	PLT_DeviceDataReference m_device;
58 	NPT_String m_UUID;
59 	GF_UPnP *m_pUPnP;
60 
61 	GPAC_ServiceItem *FindService(const char *type);
62 	void RefreshServiceList();
63 
64 	GF_List *m_Services;
65 #ifdef GPAC_HAS_SPIDERMONKEY
66 	void DetachJS();
67 	JSObject *obj;
68 	JSContext *js_ctx;
69 #endif
70 };
71 
72 #ifdef GPAC_HAS_SPIDERMONKEY
73 class GPAC_StateVariableListener
74 {
75 public:
GPAC_StateVariableListener()76 	GPAC_StateVariableListener() {
77 		on_event = JSVAL_NULL;
78 		name = "";
79 		var = NULL;
80 	}
81 	NPT_String name;
82 	jsval on_event;
83 	PLT_StateVariable *var;
84 };
85 
86 
87 class GPAC_ActionArgListener
88 {
89 public:
GPAC_ActionArgListener()90 	GPAC_ActionArgListener() {
91 		on_event = JSVAL_NULL;
92 		arg = NULL;
93 		action = NULL;
94 	}
95 	jsval on_event;
96 	PLT_ActionDesc *action;
97 	PLT_ArgumentDesc *arg;
98 	Bool is_script;
99 };
100 
101 class GPAC_ActionUDTA
102 {
103 public:
GPAC_ActionUDTA()104 	GPAC_ActionUDTA() {
105 		m_Reserved = NULL;
106 		udta = JSVAL_NULL;
107 	}
108 	/*members*/
109 
110 	/*this is used to differentiate browse request from JS (using BrowseDataReference)
111 	as used in GPAC_MediaController and actionResponse used by the generic controller*/
112 	void *m_Reserved;
113 	jsval udta;
114 };
115 
116 #endif
117 
118 
119 class GPAC_ServiceItem
120 {
121 public:
122 	GPAC_ServiceItem(GPAC_DeviceItem *device, PLT_Service *service);
123 	~GPAC_ServiceItem();
124 
125 
126 	GPAC_DeviceItem *m_device;
127 	PLT_Service *m_service;
128 
129 	NPT_List<PLT_StateVariable*>*vars;
130 
131 #ifdef GPAC_HAS_SPIDERMONKEY
132 	void DetachJS();
133 	JSObject *obj;
134 	jsval on_event;
135 	JSContext *js_ctx;
136 	GF_List *m_StateListeners;
137 	GF_List *m_ArgListeners;
138 	Bool subscribed;
139 #endif
140 
141 };
142 
143 
144 class GPAC_GenericController :  public PLT_CtrlPointListener
145 {
146 public:
147 	GPAC_GenericController(PLT_CtrlPointReference& ctrlPoint, GF_UPnP *upnp);
148 	~GPAC_GenericController();
149 
150 	/*any device control point*/
151 	virtual NPT_Result OnDeviceAdded(PLT_DeviceDataReference& device);
152 	virtual NPT_Result OnDeviceRemoved(PLT_DeviceDataReference& device);
153 	virtual NPT_Result OnActionResponse(NPT_Result res, PLT_ActionReference& action, void* userdata);
154 	virtual NPT_Result OnEventNotify(PLT_Service* service, NPT_List<PLT_StateVariable*>* vars);
155 
156 	GF_List *m_Devices;
157 	GF_UPnP *m_pUPnP;
158 	PLT_CtrlPointReference m_CtrlPoint;
159 	GF_Mutex *m_ControlPointLock;
160 };
161 
162 
163 class GPAC_GenericDevice : public PLT_DeviceHost
164 {
165 public:
166 	GPAC_GenericDevice(const char* FriendlyName, const char *device_id);
167 	virtual ~GPAC_GenericDevice();
168 
169 protected:
170 	// PLT_DeviceHost methods
171 	virtual NPT_Result SetupServices();
172 	virtual NPT_Result OnAction(PLT_ActionReference&          action,
173 	                            const PLT_HttpRequestContext& context);
174 public:
175 	GF_UPnP *m_pUPnP;
176 	GF_List *m_pServices;
177 
178 #ifdef GPAC_HAS_SPIDERMONKEY
179 	void DetachJS(JSContext *c);
180 	jsval run_proc;
181 	jsval act_proc;
182 	JSObject *obj;
183 	NPT_String js_source;
184 	PLT_ActionReference act_ref;
185 	GF_Semaphore *m_pSema;
186 	GF_Mutex *m_pMutex;
187 #endif
188 };
189 
190 class GPAC_Service : public PLT_Service
191 {
192 public:
193 	GPAC_Service(PLT_DeviceData* device, const char*     type = NULL,  const char*     id = NULL, const char*     name = NULL, const char*     last_change_namespace = NULL);
194 	~GPAC_Service();
195 
196 #ifdef GPAC_HAS_SPIDERMONKEY
197 	void SetupJS(JSContext *c, GF_UPnP *upnp, JSObject *parent);
198 	JSObject *m_pObj;
199 	JSContext *m_pCtx;
200 	jsval on_action;
201 #endif
202 
203 };
204 
205 
206 
207 #endif //_GPAC_GENERIC_DEVICE_H_
208