serviceproviders/jsapi/platformservices/src/sensors.js
changeset 27 02682e02e51f
child 34 5dae2c62e9b6
equal deleted inserted replaced
26:5d0ec8b709be 27:02682e02e51f
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Defination of class CCalendarInterface
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // sensors service interface
       
    20 
       
    21 var __device_sensors_service_entry = {"name": null, 
       
    22 				      "version": null,
       
    23 				      "proto": __device_sensors,
       
    24 				      "descriptor": __device_sensors_descriptor,
       
    25 				      "providers": [{"descriptor": __sp_sensors_descriptor, "instance": __sp_sensors_instance}]
       
    26 				     };
       
    27 		   
       
    28 
       
    29 // Private location prototype: called from service factory
       
    30 function __device_sensors(provider){
       
    31   //Private properties
       
    32   this.provider = provider;
       
    33   //Read-only properties
       
    34   this.interfaceName = provider.descriptor.interfaceName;
       
    35   this.version = provider.descriptor.version;  
       
    36   //Extended methods
       
    37   this.getChannels = __device_sensors_getChannels;
       
    38   this.startChannel = __device_sensors_setNotifier;
       
    39   this.stopChannel = __device_sensors_cancelNotifier;
       
    40   this.getScaleFactor = __device_sensors_getScaleFactor;
       
    41 }
       
    42 
       
    43 function __device_sensors_descriptor(provider){
       
    44   this.interfaceName = provider.interfaceName;
       
    45   this.version = provider.version;
       
    46 }
       
    47 
       
    48 function __device_sensors_getChannels(){
       
    49   return this.provider.getChannels();
       
    50 }
       
    51 
       
    52 function __device_sensors_setNotifier(sensors_cb, channel,__fail_cb){
       
    53   return this.provider.startChannel(sensors_cb, channel,__fail_cb);
       
    54 }
       
    55 
       
    56 function __device_sensors_cancelNotifier(channel){
       
    57   return this.provider.stopChannel(channel);
       
    58 }
       
    59 
       
    60 function __device_sensors_getScaleFactor(channel){
       
    61   return this.provider.getScaleFactor(channel);
       
    62 }
       
    63 
       
    64 
       
    65 
       
    66 
       
    67 
       
    68 
       
    69 /*
       
    70 Copyright © 2009 Nokia. All rights reserved.
       
    71 Code licensed under the BSD License:
       
    72 Software License Agreement (BSD License) Copyright © 2009 Nokia.
       
    73 All rights reserved.
       
    74 Redistribution and use of this software in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
       
    75 
       
    76 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
       
    77 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. 
       
    78 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. 
       
    79 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.
       
    80 
       
    81 version: 2.7.0
       
    82 */
       
    83 
       
    84 
       
    85 // S60 sp-based sensors provider
       
    86 
       
    87 
       
    88 
       
    89 function __sp_sensors_descriptor(){
       
    90   //Read-only properties
       
    91   this.interfaceName = "sensors";
       
    92   if(window.__Service_Interface_Ver)
       
    93 	this.version =__Service_Interface_Ver;
       
    94   else
       
    95 	this.version =1.0;
       
    96   //Class-static properties 
       
    97 }
       
    98 
       
    99 function __sp_sensors_instance(){
       
   100   //Descriptor
       
   101   this.descriptor = new __sp_sensors_descriptor();
       
   102   //Extended methods
       
   103   this.getChannels = __sp_sensors_getChannels;
       
   104   this.startChannel =  __sp_sensors_setNotifier;
       
   105   this.stopChannel =  __sp_sensors_cancelNotifier;
       
   106   this.getScaleFactor = __sp_sensors_getScaleFactor;
       
   107   //Public properties
       
   108   
       
   109   try {
       
   110     //__device_debug("Trying sensors service");
       
   111     this.so = device.getServiceObject('Service.Sensor', 'ISensor');
       
   112     //__device_debug("Got sensors service object: ");    
       
   113   }
       
   114   catch (e){
       
   115     this.so = null;
       
   116     __device_handle_exception (e, "sensors service not available");
       
   117   }
       
   118 }
       
   119 
       
   120 var __rotation_channel = {
       
   121   ucb: null,
       
   122   tids: null
       
   123 };
       
   124 
       
   125 
       
   126 var __XYZ_channel = {
       
   127   ucb: null,
       
   128   tids: null
       
   129 };
       
   130 
       
   131 var __orientation_channel = {
       
   132   ucb: null,
       
   133   tids: null
       
   134 };
       
   135 
       
   136 function __rotation_cb(arg1, arg2, arg3){
       
   137     if(arg2 != event_cancelled)
       
   138         {
       
   139         var output = null ;    
       
   140         if (arg3.ReturnValue)
       
   141             {
       
   142             var time = arg3.ReturnValue.TimeStamp;
       
   143             var xrot = arg3.ReturnValue.XRotation;
       
   144             var yrot = arg3.ReturnValue.YRotation;
       
   145             var zrot = arg3.ReturnValue.ZRotation;
       
   146 			      arg3.ReturnValue.close();
       
   147             output = {};
       
   148 	          modifyObjectBaseProp(output); 
       
   149             output.timeStamp = time;
       
   150             output.rotationAboutXAxis = xrot;
       
   151             output.rotationAboutYAxis = yrot;
       
   152             output.rotationAboutZAxis = zrot;
       
   153             }
       
   154          __rotation_channel.ucb(output);
       
   155         }
       
   156 		
       
   157 }
       
   158 
       
   159 function __XYZ_cb(arg1, arg2, arg3){
       
   160     if(arg2 != event_cancelled)
       
   161         {
       
   162         var output = null ;    
       
   163         if (arg3.ReturnValue)
       
   164             {
       
   165             var time = arg3.ReturnValue.TimeStamp;
       
   166             var xaxis = arg3.ReturnValue.XAxisData;
       
   167             var yaxis = arg3.ReturnValue.YAxisData;
       
   168             var zaxis = arg3.ReturnValue.ZAxisData;
       
   169 			      arg3.ReturnValue.close();
       
   170             output = {}; 
       
   171 	          modifyObjectBaseProp(output);
       
   172             output.timeStamp = time;
       
   173             output.axisX = xaxis;
       
   174             output.axisY = yaxis;
       
   175             output.axisZ = zaxis;
       
   176             }    
       
   177         __XYZ_channel.ucb(output);
       
   178         }
       
   179 		
       
   180 }
       
   181 
       
   182 function __orientation_cb(arg1, arg2, arg3){
       
   183     if(arg2 != event_cancelled)
       
   184         {
       
   185         var output = null ;    
       
   186         if (arg3.ReturnValue)
       
   187             {
       
   188             var time = arg3.ReturnValue.TimeStamp;
       
   189             var orientation = arg3.ReturnValue.DeviceOrientation;
       
   190 			arg3.ReturnValue.close();
       
   191             output = {}; 
       
   192 	    modifyObjectBaseProp(output);
       
   193             output.timeStamp = time;
       
   194             output.deviceOrientation = orientation;
       
   195             }        
       
   196         __orientation_channel.ucb(output);
       
   197         }
       
   198 		
       
   199 }
       
   200 
       
   201 
       
   202 // For now this is statically configured though this could be dynamically populated by the sensors sp
       
   203 
       
   204 
       
   205 
       
   206 
       
   207 function __sp_sensors_getChannels(){
       
   208   return ["Rotation", "AccelerometerAxis", "Orientation"];
       
   209 }
       
   210 
       
   211 function __sp_sensors_setNotifier(__succes_cb, __channel,__fail_cb){ 
       
   212 	    
       
   213     	var error = new DeviceException(0, 'dummy');
       
   214 	    if (!__succes_cb) {//alert("!__succes_cb");
       
   215 			throw new DeviceAPIError(error.MISSING_ARG_ERR, "Callback is missing");
       
   216 		}		
       
   217 		if ((typeof __succes_cb) != "function") {
       
   218 			throw new DeviceAPIError(error.INVALID_ARG_ERR, "Callback is of invalid type");
       
   219 		}	
       
   220 		if (__fail_cb && ((typeof __fail_cb) != "function")) {
       
   221 			throw new DeviceAPIError(error.INVALID_ARG_ERR, "InValid error Callback");
       
   222 		} 
       
   223 		if (!__channel) {
       
   224 			throw new DeviceAPIError(error.MISSING_ARG_ERR, "Channel is missing");
       
   225 		}			
       
   226 		if ((typeof __channel) != "string") {
       
   227 			
       
   228 			throw new DeviceAPIError(error.INVALID_ARG_ERR, "Channel is of invalid type");
       
   229 		}
       
   230 		
       
   231 		
       
   232 	  var rval;
       
   233 	  var cb;
       
   234 	  var channelInfo = {};
       
   235 	  modifyObjectBaseProp(channelInfo);
       
   236    switch(__channel){
       
   237    	  
       
   238 	  case "Rotation":
       
   239 		channelInfo.SearchCriterion = "Rotation";
       
   240 		cb = __rotation_cb;
       
   241 		__rotation_channel.ucb = __succes_cb;
       
   242 		break;
       
   243 	  case "AccelerometerAxis":
       
   244 	  //alert("case AccelerometerAxis");
       
   245 	  channelInfo.SearchCriterion = "AccelerometerAxis";
       
   246 		cb = __XYZ_cb;
       
   247 		__XYZ_channel.ucb = __succes_cb;
       
   248 	 break;
       
   249 	  case "Orientation":
       
   250 		channelInfo.SearchCriterion = "Orientation";
       
   251 		cb = __orientation_cb;
       
   252 		__orientation_channel.ucb = __succes_cb;
       
   253 		break;
       
   254 	  default:
       
   255 	    //alert("default");
       
   256 	    throw  new DeviceAPIError(error.NOT_SUPPORTED_ERR, "Unsupported input channel");	
       
   257 	 }
       
   258   
       
   259   try{
       
   260   	rval = this.so.ISensor.FindSensorChannel(channelInfo);
       
   261 		if (channelInfo) {
       
   262 		delete channelInfo.SearchCriterion;
       
   263 		}
       
   264     if(rval.ErrorCode != 0)
       
   265       {
       
   266       throw  new DeviceAPIError(error.NOT_SUPPORTED_ERR, "StartChannel:Operation Failed");
       
   267       }
       
   268 
       
   269   var cmap = [];
       
   270   var transactionid = [];
       
   271   var returnvalue = rval["ReturnValue"];
       
   272   var count = returnvalue.length;
       
   273   cmap = returnvalue[0];
       
   274   var sensorParams = {};
       
   275 	modifyObjectBaseProp(sensorParams);
       
   276 	sensorParams.ListeningType = "ChannelData";
       
   277 	sensorParams.ChannelInfoMap = cmap;
       
   278 	var result = this.so.ISensor.RegisterForNotification(sensorParams, cb);
       
   279 	  if(sensorParams){
       
   280 	  	delete sensorParams.ChannelInfoMap;
       
   281 		  delete sensorParams.ListeningType;
       
   282 	  }
       
   283 	  if(cmap){
       
   284 	  	delete cmap.index;
       
   285 	  }
       
   286     transactionid[0] = result["TransactionID"];
       
   287     if(result.ErrorCode != 0)
       
   288         {
       
   289         //throw result.ErrorCode;
       
   290 		
       
   291 						 if( result.ErrorCode == 1005)
       
   292 						 {
       
   293 						 __fail_cb(new DeviceAPIError(error.SERVICE_IN_USE_ERR,"Not Allowed Operation"));
       
   294 						 return;	 
       
   295 						 }
       
   296 						 else
       
   297 						 	{
       
   298 						 	throw  new DeviceAPIError(error.NOT_SUPPORTED_ERR, "StartChannel:Operation Failed");	
       
   299 						 	}
       
   300 				 }
       
   301         		 
       
   302 			
       
   303     }catch(e2){
       
   304       __device_handle_exception (e2, "__sp_sensors_setNotifier: RegisterForNotification: " + e2);
       
   305         }
       
   306   
       
   307   
       
   308   switch(__channel){
       
   309   case "Rotation":
       
   310     __rotation_channel.tid = transactionid;
       
   311     break;
       
   312   case "AccelerometerAxis":
       
   313   __XYZ_channel.tid = transactionid;
       
   314     break;
       
   315   case "Orientation":
       
   316     __orientation_channel.tid = transactionid;
       
   317     break;
       
   318   }
       
   319   return transactionid;
       
   320  }
       
   321 
       
   322 function __sp_sensors_cancelNotifier(__channel){
       
   323  
       
   324     var error1 = new DeviceException(0, 'dummy');
       
   325 	  if(!__channel)
       
   326 			    throw  new DeviceAPIError(error1.MISSING_ARG_ERR, "Channel is missing");
       
   327 							
       
   328 			if((typeof __channel) != "string")
       
   329 					throw  new DeviceAPIError(error1.INVALID_ARG_ERR, "Channel is of invalid type");
       
   330   
       
   331 	  var id ;  
       
   332 	  
       
   333 	  switch (__channel)
       
   334 	  {
       
   335 		case "Rotation":
       
   336 		  id = __rotation_channel.tid;
       
   337 		  __rotation_channel.tid = null;
       
   338 		  break;
       
   339 		case "AccelerometerAxis":
       
   340 		id = __XYZ_channel.tid;
       
   341 		__XYZ_channel.tid = null;
       
   342 		  break;
       
   343 		case "Orientation":
       
   344 		  id = __orientation_channel.tid;
       
   345 		  __orientation_channel.tid = null;
       
   346 		  break;
       
   347 		default:
       
   348 		  throw  new DeviceAPIError(error1.NOT_SUPPORTED_ERR, "Unsupported input channel");	
       
   349 		}     
       
   350 	
       
   351 	 if(!id)
       
   352 	 {
       
   353 	 throw  new DeviceAPIError(error1.DATA_NOT_FOUND_ERR, "Stop Channel:Operation Failed");		
       
   354 	 }
       
   355 	 
       
   356 	var cancelParams = {};
       
   357   modifyObjectBaseProp(cancelParams);
       
   358   for (var i in id) {
       
   359   		  cancelParams.TransactionID = id[i];
       
   360 				try {
       
   361 					var result = this.so.ISensor.Cancel(cancelParams);
       
   362 					if (cancelParams) {
       
   363 						delete cancelParams.TransactionID;
       
   364 					}
       
   365 					if (result.ErrorCode != 0) {
       
   366 						throw  new DeviceAPIError(error1.DATA_NOT_FOUND_ERR, "Stop Channel:Operation Failed");
       
   367 						}
       
   368 				} 
       
   369 				catch (e1) {
       
   370 					__device_handle_exception(e1, "__sp_sensors_cancelNotifier: " + e1);
       
   371 				}
       
   372 		}
       
   373 }
       
   374 
       
   375 function __sp_sensors_getScaleFactor(__channel){
       
   376 	var error2 = new DeviceException(0, 'dummy');
       
   377 	if(!__channel)
       
   378 		    throw  new DeviceAPIError(error2.MISSING_ARG_ERR, "Channel is missing");
       
   379 						
       
   380 		if((typeof __channel) != "string")
       
   381 				throw  new DeviceAPIError(error2.INVALID_ARG_ERR, "Channel is of invalid type");
       
   382 				
       
   383 	if(__channel != "AccelerometerAxis")
       
   384 		    throw  new DeviceAPIError(error2.NOT_SUPPORTED_ERR, "Unsupported input channel");
       
   385 		    
       
   386  try{    
       
   387 	  var channelInfo = {};	    
       
   388 	  channelInfo.SearchCriterion = "AccelerometerAxis";
       
   389 	  var rval = this.so.ISensor.FindSensorChannel(channelInfo);
       
   390 		if (channelInfo) {
       
   391 		delete channelInfo.SearchCriterion;
       
   392 		}
       
   393     if(rval.ErrorCode != 0)
       
   394       {
       
   395       throw  new DeviceAPIError(error2.NOT_SUPPORTED_ERR, "getScaleFactor:Operation Failed");
       
   396       }
       
   397     var cmap = [];    		
       
   398 		var returnvalue = rval["ReturnValue"];
       
   399     cmap = returnvalue[0];
       
   400     var sensorParams = {};
       
   401 	  modifyObjectBaseProp(sensorParams);
       
   402 	  sensorParams.ListeningType = "ChannelData";
       
   403 	  sensorParams.ChannelInfoMap = cmap;
       
   404 	  var result = this.so.ISensor.GetScaleFactor(sensorParams);
       
   405 	  if(sensorParams){
       
   406 	  	delete sensorParams.ChannelInfoMap;
       
   407 		  delete sensorParams.ListeningType;
       
   408 	  }
       
   409 	  if(cmap){
       
   410 	  	delete cmap.index;
       
   411 	  }
       
   412 	  if(result.ErrorCode != 0)
       
   413         {
       
   414         throw  new DeviceAPIError(error2.NOT_SUPPORTED_ERR, "getScaleFactor:Operation Failed");	
       
   415         }
       
   416    }
       
   417    
       
   418    catch (e1) {
       
   419 					__device_handle_exception(e1, "__sp_sensors_getScaleFactor: " + e1);
       
   420 				}
       
   421 		 return result["ReturnValue"];  
       
   422 				
       
   423 }