|
1 /* |
|
2 Copyright © 2009 Nokia. All rights reserved. |
|
3 Code licensed under the BSD License: |
|
4 Software License Agreement (BSD License) Copyright © 2009 Nokia. |
|
5 All rights reserved. |
|
6 Redistribution and use of this software in source and binary forms, with or without modification, are permitted provided that the following conditions are met: |
|
7 |
|
8 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
|
9 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. |
|
10 Neither the name of Nokia Corporation. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission of Nokia Corporation. |
|
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
12 |
|
13 version: 1.0 |
|
14 */ |
|
15 |
|
16 |
|
17 var __device_debug_on__ = true; |
|
18 var err_missing_argument = 1003; |
|
19 var event_cancelled = 3; |
|
20 var err_bad_argument = 1002; |
|
21 var err_InvalidService_Argument = 1000; |
|
22 var err_ServiceNotReady = 1006; |
|
23 var err_ServiceNotSupported = 1004; |
|
24 |
|
25 function __device_debug(text){ |
|
26 //if(__device_debug_on__) alert(text); |
|
27 } |
|
28 |
|
29 function __device_handle_exception(e, text){ |
|
30 __device_debug(text); |
|
31 throw(e); |
|
32 } |
|
33 |
|
34 function __device_typeof(value) |
|
35 { |
|
36 // First check to see if the value is undefined. |
|
37 if (value == undefined) { |
|
38 return "undefined"; |
|
39 } |
|
40 // Check for objects created with the "new" keyword. |
|
41 if (value instanceof Object) { |
|
42 // Check whether it's a string object. |
|
43 if (value instanceof String) { |
|
44 return "String"; |
|
45 } |
|
46 // Check whether it's an array object/array literal. |
|
47 else |
|
48 if (value instanceof Array) { |
|
49 return "Array"; |
|
50 } |
|
51 } |
|
52 // dealing with a literal. |
|
53 if (typeof value) { |
|
54 if (typeof value == "object") { |
|
55 if (typeof value == "object" && !value) { |
|
56 return "null"; |
|
57 } |
|
58 } |
|
59 // if not null check for other types |
|
60 |
|
61 // Check if it's a string literal. |
|
62 else if (typeof value == "string") { |
|
63 return "string"; |
|
64 } |
|
65 } |
|
66 } |
|
67 |
|
68 |
|
69 // The top-level service object. It would be nice to use a namespace here |
|
70 // (com.nokia.device.service), but emulating namespaces still allows name clashes. |
|
71 /* |
|
72 var sp_device = { |
|
73 //services: null; // TBD: Make the services list a member of this object? |
|
74 load: __device_service_load, |
|
75 listServices: __device_service_list, |
|
76 listInterfaces: __device_service_interfaces, |
|
77 version: "0.1", |
|
78 info: "device prototype" |
|
79 }; |
|
80 */ |
|
81 |
|
82 if(undefined == com) |
|
83 var com={}; |
|
84 |
|
85 if( typeof com != "object") |
|
86 throw("com defined as non object"); |
|
87 |
|
88 if(undefined == com.nokia) |
|
89 com.nokia = {}; |
|
90 |
|
91 if( typeof com.nokia != "object") |
|
92 throw("com.nokia defined as non object"); |
|
93 |
|
94 if(undefined == com.nokia.device) |
|
95 com.nokia.device = { |
|
96 load: __device_service_load, |
|
97 listServices: __device_service_list, |
|
98 listInterfaces: __device_service_interfaces, |
|
99 version: "0.1", |
|
100 info: "device prototype" |
|
101 }; |
|
102 else |
|
103 throw("com.nokia.device already defined"); |
|
104 |
|
105 com.nokia.device.SORT_ASCENDING = 0; |
|
106 com.nokia.device.SORT_DESCENDING = 1; |
|
107 |
|
108 com.nokia.device.SORT_BY_DATE = 0; |
|
109 com.nokia.device.SORT_BY_SENDER = 1; |
|
110 |
|
111 com.nokia.device.STATUS_READ = 0; |
|
112 com.nokia.device.STATUS_UNREAD = 1; |
|
113 |
|
114 |
|
115 // Configure the services offered. |
|
116 |
|
117 var __device_services_inited = false; |
|
118 |
|
119 var __device_services = [ |
|
120 |
|
121 // For now, the only service is the base "device"" service |
|
122 { |
|
123 "name":"com.nokia.device", |
|
124 "version": 0.1, |
|
125 "interfaces": [] |
|
126 } |
|
127 ]; |
|
128 |
|
129 // Initialize the configured services. |
|
130 |
|
131 function __device_services_init(){ |
|
132 if(__device_services_inited){ |
|
133 return; |
|
134 } |
|
135 __device_services_inited = true; |
|
136 |
|
137 // Get the service-specific service entries. Note that these |
|
138 // need to be individually wrapped by try/catch blocks so that the |
|
139 // interpreter gracefully handles missing services. |
|
140 |
|
141 try { |
|
142 __device_services[0].interfaces.push(__device_geolocation_service_entry); |
|
143 }catch (e){ |
|
144 __device_debug("Missing library implementation: " + e); |
|
145 } |
|
146 try { |
|
147 __device_services[0].interfaces.push(__device_camera_service_entry); |
|
148 }catch (e){ |
|
149 __device_debug("Missing library implementation: " + e); |
|
150 } |
|
151 try { |
|
152 __device_services[0].interfaces.push(__device_media_service_entry); |
|
153 }catch (e){ |
|
154 // __device_debug("Missing library implementation: " + e); |
|
155 } |
|
156 try { |
|
157 __device_services[0].interfaces.push(__device_contacts_service_entry); |
|
158 }catch (e){ |
|
159 // __device_debug("Missing library implementation: " + e); |
|
160 } |
|
161 try { |
|
162 __device_services[0].interfaces.push(__device_messaging_service_entry); |
|
163 }catch (e){ |
|
164 __device_debug("Missing library implementation: " + e); |
|
165 } |
|
166 try { |
|
167 __device_services[0].interfaces.push(__device_calendar_service_entry); |
|
168 }catch (e){ |
|
169 __device_debug("Missing library implementation: " + e); |
|
170 } |
|
171 try { |
|
172 __device_services[0].interfaces.push(__device_landmarks_service_entry); |
|
173 }catch (e){ |
|
174 __device_debug("Missing library implementation: " + e); |
|
175 } |
|
176 try { |
|
177 __device_services[0].interfaces.push(__device_event_service_entry); |
|
178 }catch (e){ |
|
179 __device_debug("Missing library implementation: " + e); |
|
180 } |
|
181 try { |
|
182 __device_services[0].interfaces.push(__device_sysinfo_service_entry); |
|
183 }catch (e){ |
|
184 __device_debug("Missing library implementation: " + e); |
|
185 } |
|
186 try { |
|
187 __device_services[0].interfaces.push(__device_sensors_service_entry); |
|
188 }catch (e){ |
|
189 __device_debug("Missing library implementation: " + e); |
|
190 } |
|
191 |
|
192 } |
|
193 |
|
194 function __device_get_implementation(i){ |
|
195 //__device_debug("get_implementation: " + i); |
|
196 return new i.proto(new(i.providers[0].instance)); |
|
197 } |
|
198 |
|
199 function __device_get_descriptor(i){ |
|
200 //__device_debug("get_descriptor: " + i); |
|
201 return new i.descriptor(new(i.providers[0].descriptor)); |
|
202 } |
|
203 |
|
204 function __device_get_interface(s, interfaceName, version){ |
|
205 //__device_debug("get_interface: " + s + " " + interfaceName); |
|
206 var i = s.interfaces; |
|
207 if((interfaceName == null) || (interfaceName == '')){ |
|
208 // Interface name not specified, get first interface, ignoring version |
|
209 return __device_get_implementation(i[0]); |
|
210 } |
|
211 |
|
212 // Find first match of name and version |
|
213 for (var d in i){ |
|
214 |
|
215 if(i[d].name == null){ |
|
216 __device_update_descriptor(i[d]); |
|
217 } |
|
218 if(i[d].name == undefined){ |
|
219 continue; |
|
220 } |
|
221 if (i[d].name == interfaceName){ |
|
222 // Match version if specified |
|
223 if ((version == null) || (version == '') || (i[d].version >= version)){ |
|
224 return __device_get_implementation(i[d]); |
|
225 } |
|
226 } |
|
227 } |
|
228 return null; |
|
229 } |
|
230 |
|
231 // Implemention of the load method |
|
232 |
|
233 function __device_service_load(serviceName, interfaceName, version){ |
|
234 |
|
235 __device_services_init(); |
|
236 |
|
237 // Service name is specified |
|
238 if ((serviceName != null) && (serviceName != '') &&(serviceName != "*")){ |
|
239 for(var s in __device_services){ |
|
240 if (serviceName == __device_services[s].name){ |
|
241 return __device_get_interface(__device_services[s], interfaceName, version); |
|
242 } |
|
243 } |
|
244 // Service name not specified, get first implementation |
|
245 } else { |
|
246 //__device_debug("Trying to get interface implementations: "); |
|
247 for(var s in __device_services){ |
|
248 //__device_debug("service_load: " + s + ":" + __device_services[s].name + ": " + interfaceName); |
|
249 var i = __device_get_interface(__device_services[s], interfaceName, version); |
|
250 if (i != null){ |
|
251 return i; |
|
252 } |
|
253 } |
|
254 } |
|
255 return null; |
|
256 } |
|
257 |
|
258 // Lazily fill in the descriptor table |
|
259 |
|
260 function __device_update_descriptor(i){ |
|
261 var d = __device_get_descriptor(i); |
|
262 i.name = d.interfaceName; |
|
263 i.version = d.version; |
|
264 } |
|
265 // Get an array of interface descriptors for a service |
|
266 |
|
267 function __device_interface_list(s){ |
|
268 var retval = new Array(); |
|
269 for(var i in s.interfaces){ |
|
270 if(s.interfaces[i].name == null){ |
|
271 __device_update_descriptor(s.interfaces[i]); |
|
272 } |
|
273 if(s.interfaces[i].name == undefined){ |
|
274 continue; |
|
275 } |
|
276 retval[i] = new Object(); |
|
277 retval[i].name = s.interfaces[i].name; |
|
278 retval[i].version = s.interfaces[i].version; |
|
279 } |
|
280 return retval; |
|
281 } |
|
282 |
|
283 // Get a service description |
|
284 |
|
285 function __device_service_descriptor(s){ |
|
286 this.name = s.name; |
|
287 this.version = s.version; |
|
288 this.interfaces = __device_interface_list(s); |
|
289 this.toString = __device_service_descriptor_to_string; |
|
290 } |
|
291 |
|
292 function __device_service_descriptor_to_string(){ |
|
293 var is = "\nInterfaces(s): "; |
|
294 |
|
295 for (i in this.interfaces){ |
|
296 is += "\n" + this.interfaces[i].name + " " + this.interfaces[0].version; |
|
297 } |
|
298 return ("Service: " + this.name + is); |
|
299 } |
|
300 |
|
301 // Implement the listServices method |
|
302 |
|
303 function __device_service_list(serviceName, interfaceName, version){ |
|
304 //__device_debug("__device_service_list: " + serviceName + " " + interfaceName); |
|
305 __device_services_init(); |
|
306 var retval = new Array(); |
|
307 var n = 0; |
|
308 |
|
309 //Treat empty service and interface names as wildcards |
|
310 if ((serviceName == null)|| (serviceName == '')/* || (serviceName == undefined)*/){ |
|
311 serviceName = ".*"; |
|
312 } |
|
313 if ((interfaceName == null) || (interfaceName == '') /*|| (serviceName == undefined)*/){ |
|
314 interfaceName = ".*"; |
|
315 } |
|
316 |
|
317 if ((typeof serviceName != "string") || (typeof interfaceName != "string")) { |
|
318 return retval; |
|
319 } |
|
320 |
|
321 // This method does regular expression matching of service and interface |
|
322 |
|
323 var sregx = new RegExp(serviceName); |
|
324 var iregx = new RegExp(interfaceName); |
|
325 |
|
326 for(var s in __device_services){ |
|
327 //__device_debug (serviceName + "==" + __device_services[s].name + "?:" + sregx.test(__device_services[s].name)); |
|
328 if (sregx.test(__device_services[s].name)){ |
|
329 // Find the first matching interface |
|
330 |
|
331 for(var i in __device_services[s].interfaces){ |
|
332 if(__device_services[s].interfaces[i].name == null){ |
|
333 __device_update_descriptor(__device_services[s].interfaces[i]); |
|
334 } |
|
335 if(__device_services[s].interfaces[i].name == undefined){ |
|
336 continue; |
|
337 } |
|
338 //__device_debug (interfaceName + "==" + __device_services[s].interfaces[i].name + "?:" + iregx.test(__device_services[s].interfaces[i].name)); |
|
339 if (iregx.test(__device_services[s].interfaces[i].name)){ |
|
340 if ((version == null) || (version == '') || (__device_services[s].interfaces[i].version >= version)){ |
|
341 // An interface matched, we're done. |
|
342 retval[n] = new __device_service_descriptor(__device_services[s]); |
|
343 break; |
|
344 } |
|
345 } |
|
346 } |
|
347 } |
|
348 ++n; |
|
349 } |
|
350 return retval; |
|
351 } |
|
352 |
|
353 // Implement the listInterfaces method |
|
354 |
|
355 function __device_service_interfaces(serviceName){ |
|
356 __device_services_init(); |
|
357 if(serviceName==null||serviceName==undefined||serviceName==''){ |
|
358 throw new DeviceError("Framework: listInterfaces: serviceName is missing", err_missing_argument); |
|
359 } |
|
360 for (var s in __device_services){ |
|
361 if(__device_services[s].name == serviceName){ |
|
362 return __device_interface_list(__device_services[s]); |
|
363 } |
|
364 } |
|
365 return null; |
|
366 } |
|
367 |
|
368 function modifyObjectBaseProp(obj){ |
|
369 for (pro in obj) { |
|
370 if(typeof obj[pro] == "function" ) |
|
371 obj[pro] = 0; |
|
372 } |
|
373 }; |