2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 namespace Tizen.Sensor
22 internal static class Globals
24 internal const string LogTag = "Tizen.Sensor";
28 /// The Sensor class is used for storing the hardware information about a particular sensor.
30 /// <since_tizen> 3 </since_tizen>
31 public abstract class Sensor : IDisposable
34 private string _vendor;
35 private float _minValue;
36 private float _maxValue;
37 private float _resolution;
38 private int _minInterval;
39 private int _fifoCount;
40 private int _maxBatchCount;
41 private bool _isSensing = false;
42 private bool _disposed = false;
43 private TimeSpan _timeSpan;
44 private uint _interval = 0;
45 private uint _maxBatchLatency = 0;
46 private SensorPausePolicy _pausePolicy = SensorPausePolicy.None;
47 private IntPtr _sensorHandle = IntPtr.Zero;
48 private IntPtr _listenerHandle = IntPtr.Zero;
50 internal abstract SensorType GetSensorType();
51 internal abstract void EventListenStart();
52 internal abstract void EventListenStop();
54 internal Sensor(uint index)
56 SensorType type = GetSensorType();
57 GetHandleList(type, index);
58 if (CheckSensorHandle())
66 /// Destroy the Sensor object.
74 /// Property: Gets the name of the sensor.
76 /// <since_tizen> 3 </since_tizen>
77 /// <value> The name of the sensor. </value>
82 Log.Info(Globals.LogTag, "Getting the sensor name");
88 /// Property: Gets the vendor.
90 /// <since_tizen> 3 </since_tizen>
91 /// <value> The vendor name of the sensor. </value>
96 Log.Info(Globals.LogTag, "Getting the sensor vendor name");
102 /// Property: Gets the minimum value of the range of the sensor data.
104 /// <since_tizen> 3 </since_tizen>
105 /// <value> The lower bound of the range of the sensor reading. </value>
106 public float MinValue
110 Log.Info(Globals.LogTag, "Getting the min value of the sensor");
116 /// Property: Gets the maximum value of the range of the sensor data.
118 /// <since_tizen> 3 </since_tizen>
119 /// <value> The upper bound of the range of the sensor reading. </value>
120 public float MaxValue
124 Log.Info(Globals.LogTag, "Getting the max value of the sensor");
130 /// Property: Gets the resolution.
132 /// <since_tizen> 3 </since_tizen>
133 /// <value> The resolution. </value>
134 public float Resolution
138 Log.Info(Globals.LogTag, "Getting the resolution of the sensor");
144 /// Property: Gets the minimum interval.
146 /// <since_tizen> 3 </since_tizen>
147 /// <value> The minimum update interval. </value>
148 public int MinInterval
152 Log.Info(Globals.LogTag, "Getting the min interval for the sensor");
158 /// Property: Gets the FIFO count.
160 /// <since_tizen> 3 </since_tizen>
161 /// <value> The size of the hardware FIFO. </value>
166 Log.Info(Globals.LogTag, "Getting the fifo count of the sensor");
172 /// Property: Gets the maximum batch count.
174 /// <since_tizen> 3 </since_tizen>
175 /// <value> The maximum batch count. </value>
176 public int MaxBatchCount
180 Log.Info(Globals.LogTag, "Getting the max batch count of the sensor");
181 return _maxBatchCount;
186 /// Sets the interval of the sensor for the sensor data event.
187 /// Callbacks will be called at the frequency of this interval.
189 /// <since_tizen> 3 </since_tizen>
190 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
191 /// <value> The interval of the sensor. </value>
196 Log.Info(Globals.LogTag, "Setting the interval of the sensor");
202 Log.Info(Globals.LogTag, "Getting the interval of the sensor");
208 /// Sets the maximum batch latency for the sensor corresponding to the sensor data event.
210 /// <since_tizen> 3 </since_tizen>
211 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
212 /// <value> The maximum batch latency. </value>
213 public uint MaxBatchLatency
217 Log.Info(Globals.LogTag, "Setting the max batch latency of the sensor");
218 _maxBatchLatency = value;
219 SetMaxBatchLatency();
223 Log.Info(Globals.LogTag, "Getting the max batch latency of the sensor");
224 return _maxBatchLatency;
229 /// Sets the pause policy of the sensor.
231 /// <since_tizen> 3 </since_tizen>
232 /// <value>The pause policy.</value>
233 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
234 /// <value> The pause policy. </value>
235 public SensorPausePolicy PausePolicy
239 Log.Info(Globals.LogTag, "Setting the pause policy of the sensor");
240 _pausePolicy = value;
241 SetAttribute(SensorAttribute.PausePolicy, (int)_pausePolicy);
245 Log.Info(Globals.LogTag, "Getting the pause policy of the sensor");
251 /// Gets or sets the time span.
253 /// <since_tizen> 3 </since_tizen>
254 /// <value> The time span. </value>
255 public TimeSpan TimeSpan
259 Log.Info(Globals.LogTag, "Setting the timespan of the sensor values");
264 Log.Info(Globals.LogTag, "Getting the timespan of the sensor values");
270 /// Indicates whether this sensor is sensing.
272 /// <since_tizen> 3 </since_tizen>
273 /// <value><c>true</c> if this sensor is sensing; otherwise <c>false</c>.</value>
274 public bool IsSensing
278 Log.Info(Globals.LogTag, "Checking if the sensor is started");
283 internal IntPtr ListenerHandle
287 return _listenerHandle;
291 internal static bool CheckIfSupported(SensorType type, String key)
293 bool isSupported = false;
294 bool error = Information.TryGetValue(key, out isSupported);
296 if (!error || !isSupported)
298 Log.Error(Globals.LogTag, "Error checking if sensor is supported(systeminfo)");
302 int ret = Interop.SensorManager.SensorIsSupported(type, out isSupported);
303 if (ret != (int)SensorError.None)
305 Log.Error(Globals.LogTag, "Error checking if sensor is supported");
313 /// Starts the sensor.
314 /// After this, event handlers will start receiving events.
316 /// <since_tizen> 3 </since_tizen>
317 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
320 Log.Info(Globals.LogTag, "Starting the sensor");
321 if (CheckListenerHandle())
323 int error = Interop.SensorListener.StartListener(_listenerHandle);
324 if (error != (int)SensorError.None)
326 Log.Error(Globals.LogTag, "Error starting sensor");
327 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to Start Sensor Listener");
331 Log.Info(Globals.LogTag, "Sensor started");
336 /// Stops the sensor.
337 /// After this, event handlers will stop receiving events.
339 /// <since_tizen> 3 </since_tizen>
340 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
343 Log.Info(Globals.LogTag, "Stopping the sensor");
346 int error = Interop.SensorListener.StopListener(_listenerHandle);
347 if (error != (int)SensorError.None)
349 Log.Error(Globals.LogTag, "Error stopping the sensor");
350 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to Stop Sensor Listener");
354 Log.Info(Globals.LogTag, "Sensor stopped");
359 /// Destroy the current object.
361 /// <since_tizen> 3 </since_tizen>
362 public void Dispose()
365 GC.SuppressFinalize(this);
369 /// Releases all resources currently used by this instance.
371 /// <param name="disposing">
372 /// true if managed resources should be disposed
373 /// otherwise, false.
375 /// <since_tizen> 3 </since_tizen>
376 protected virtual void Dispose(bool disposing)
385 internal void SetAttribute(SensorAttribute attribute, int option)
387 if (CheckListenerHandle())
389 int error = Interop.SensorListener.SetAttribute(_listenerHandle, attribute, option);
390 if (error != (int)SensorError.None)
392 Log.Error(Globals.LogTag, "Error setting sensor pause policy");
393 throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.PausePolicy Failed");
398 private void GetHandleList(SensorType type, uint index)
403 int error = Interop.SensorManager.GetSensorList(type, out list, out count);
404 if (error != (int)SensorError.None)
406 Log.Error(Globals.LogTag, "Error getting sensor list");
407 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.GetSensorList Failed");
409 sensorList = Interop.IntPtrToIntPtrArray(list, count);
410 _sensorHandle = sensorList[index];
411 Interop.Libc.Free(list);
414 private void GetProperty()
416 int error = (int)SensorError.None;
418 error = Interop.Sensor.GetName(_sensorHandle, out _name);
419 if (error != (int)SensorError.None)
421 Log.Error(Globals.LogTag, "Error getting sensor name");
422 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Name Failed");
425 error = Interop.Sensor.GetVendor(_sensorHandle, out _vendor);
426 if (error != (int)SensorError.None)
428 Log.Error(Globals.LogTag, "Error getting sensor vendor name");
429 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Vendor Failed");
432 error = Interop.Sensor.GetMinRange(_sensorHandle, out _minValue);
433 if (error != (int)SensorError.None)
435 Log.Error(Globals.LogTag, "Error getting sensor min value");
436 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MinValue Failed");
439 error = Interop.Sensor.GetMaxRange(_sensorHandle, out _maxValue);
440 if (error != (int)SensorError.None)
442 Log.Error(Globals.LogTag, "Error getting sensor max value");
443 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MaxValue Failed");
446 error = Interop.Sensor.GetResolution(_sensorHandle, out _resolution);
447 if (error != (int)SensorError.None)
449 Log.Error(Globals.LogTag, "Error getting sensor resolution");
450 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Resolution Failed");
453 error = Interop.Sensor.GetMinInterval(_sensorHandle, out _minInterval);
454 if (error != (int)SensorError.None)
456 Log.Error(Globals.LogTag, "Error getting sensor min interval");
457 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MinInterval Failed");
460 error = Interop.Sensor.GetFifoCount(_sensorHandle, out _fifoCount);
461 if (error != (int)SensorError.None)
463 Log.Error(Globals.LogTag, "Error getting sensor fifo count");
464 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.FifoCount Failed");
467 error = Interop.Sensor.GetMaxBatchCount(_sensorHandle, out _maxBatchCount);
468 if (error != (int)SensorError.None)
470 Log.Error(Globals.LogTag, "Error getting sensor max batch count");
471 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MaxBatchCount Failed");
475 private void CreateListener()
477 int error = Interop.SensorListener.CreateListener(_sensorHandle, out _listenerHandle);
478 if (error != (int)SensorError.None)
480 Log.Error(Globals.LogTag, "Error cerating sensor listener handle");
481 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.CreateListener Failed");
485 private void SetInterval()
487 if (CheckListenerHandle())
491 int error = Interop.SensorListener.SetInterval(_listenerHandle, _interval);
492 if (error != (int)SensorError.None)
494 Log.Error(Globals.LogTag, "Error setting sensor interval");
495 throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.SetInterval Failed");
501 private void SetMaxBatchLatency()
503 if (CheckListenerHandle())
505 int error = Interop.SensorListener.SetMaxBatchLatency(_listenerHandle, _maxBatchLatency);
506 if (error != (int)SensorError.None)
508 Log.Error(Globals.LogTag, "Error setting max batch latency");
509 throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.MaxBatchLatency Failed");
514 private bool CheckListenerHandle()
517 if (_listenerHandle != IntPtr.Zero)
523 Log.Error(Globals.LogTag, "Sensor listener handle is null");
524 throw new ArgumentException("Invalid Parameter: Sensor is null");
529 private bool CheckSensorHandle()
532 if (_sensorHandle != IntPtr.Zero)
538 Log.Error(Globals.LogTag, "Sensor handle is null");
539 throw new ArgumentException("Invalid Parameter: Sensor is null");
544 private void DestroyHandles()
546 Interop.SensorListener.DestroyListener(_listenerHandle);