1 // Copyright 2016 by Samsung Electronics, Inc.,
3 // This software is the confidential and proprietary information
4 // of Samsung Electronics, Inc. ("Confidential Information"). You
5 // shall not disclose such Confidential Information and shall use
6 // it only in accordance with the terms of the license agreement
7 // you entered into with Samsung.
12 namespace Tizen.Sensor
14 internal static class Globals
16 internal const string LogTag = "Tizen.Sensor";
20 /// Sensor class for storing hardware information about a particular sensor
22 public abstract class Sensor : IDisposable
25 private string _vendor;
26 private float _minValue;
27 private float _maxValue;
28 private float _resolution;
29 private int _minInterval;
30 private int _fifoCount;
31 private int _maxBatchCount;
32 private bool _isSensing = false;
33 private bool _disposed = false;
34 private TimeSpan _timeSpan;
35 private uint _interval = 0;
36 private uint _maxBatchLatency = 0;
37 private SensorPausePolicy _pausePolicy = SensorPausePolicy.None;
38 private IntPtr _sensorHandle = IntPtr.Zero;
39 private IntPtr _listenerHandle = IntPtr.Zero;
41 internal abstract SensorType GetSensorType();
42 protected abstract void EventListenStart();
43 protected abstract void EventListenStop();
45 internal Sensor(int index)
47 SensorType type = GetSensorType();
48 GetHandleList(type, index);
49 if (CheckSensorHandle())
62 /// Property: For getting the name of the sensor
68 Log.Info(Globals.LogTag, "Getting the sensor name");
74 /// Property: Gets the vendor.
80 Log.Info(Globals.LogTag, "Getting the sensor vendor name");
86 /// Property: Gets the minimum value of range of sensor data.
92 Log.Info(Globals.LogTag, "Getting the min value of the sensor");
98 /// Property: Gets the maximum value of range of sensor data.
100 public float MaxValue
104 Log.Info(Globals.LogTag, "Getting the max value of the sensor");
110 /// Property: Gets the resolution.
112 public float Resolution
116 Log.Info(Globals.LogTag, "Getting the resolution of the sensor");
122 /// Property: Gets the minimum interval.
124 public int MinInterval
128 Log.Info(Globals.LogTag, "Getting the min interval for the sensor");
134 /// Property: Gets the fifo count.
140 Log.Info(Globals.LogTag, "Getting the fifo count of the sensor");
146 /// Property: Gets the maximum batch count.
148 public int MaxBatchCount
152 Log.Info(Globals.LogTag, "Getting the max batch count of the sensor");
153 return _maxBatchCount;
158 /// Sets the interval of the sensor for sensor data event
159 /// Callbacks will be called at frequency of this interval
165 Log.Info(Globals.LogTag, "Setting the interval of the sensor");
171 Log.Info(Globals.LogTag, "Getting the interval of the sensor");
177 /// Sets the max batch latency for the sensor corresponding to the sensor data event.
179 public uint MaxBatchLatency
183 Log.Info(Globals.LogTag, "Setting the max batch latency of the sensor");
184 _maxBatchLatency = value;
185 SetMaxBatchLatency();
189 Log.Info(Globals.LogTag, "Getting the max batch latency of the sensor");
190 return _maxBatchLatency;
195 /// Sets the pause policy of the sensor.
198 public SensorPausePolicy PausePolicy
202 Log.Info(Globals.LogTag, "Setting the pause policy of the sensor");
203 _pausePolicy = value;
204 SetAttribute(SensorAttribute.PausePolicy, (int)_pausePolicy);
208 Log.Info(Globals.LogTag, "Getting the pause policy of the sensor");
214 /// Gets or sets the time span.
216 /// <value>The time span.</value>
217 public TimeSpan TimeSpan
221 Log.Info(Globals.LogTag, "Setting the timespan of the sensor values");
226 Log.Info(Globals.LogTag, "Getting the timespan of the sensor values");
232 /// Indicates whether this sensor is sensing.
234 /// <value><c>true</c> if this sensor is sensing; otherwise, <c>false</c>.</value>
235 public bool IsSensing
239 Log.Info(Globals.LogTag, "Checking if the sensor is started");
244 internal IntPtr ListenerHandle
248 return _listenerHandle;
252 internal static bool CheckIfSupported(SensorType type, String key)
254 bool isSupported = false;
255 bool error = SystemInfo.TryGetValue(key, out isSupported);
257 if (!error || !isSupported)
259 Log.Error(Globals.LogTag, "Error checking if sensor is supported(systeminfo)");
263 int ret = Interop.SensorManager.SensorIsSupported(type, out isSupported);
264 if (ret != (int)SensorError.None)
266 Log.Error(Globals.LogTag, "Error checking if sensor is supported");
274 /// Starts the sensor.
275 /// After this the event handlers will start receiving events.
279 Log.Info(Globals.LogTag, "Starting the sensor");
280 if (CheckListenerHandle())
282 int error = Interop.SensorListener.StartListener(_listenerHandle);
283 if (error != (int)SensorError.None)
285 Log.Error(Globals.LogTag, "Error starting sensor");
286 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to Start Sensor Listener");
290 Log.Info(Globals.LogTag, "Sensor started");
296 /// After this the event handlers will stop receiving the events
300 Log.Info(Globals.LogTag, "Stopping the sensor");
303 int error = Interop.SensorListener.StopListener(_listenerHandle);
304 if (error != (int)SensorError.None)
306 Log.Error(Globals.LogTag, "Error stopping the sensor");
307 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to Stop Sensor Listener");
311 Log.Info(Globals.LogTag, "Sensor stopped");
315 Log.Error(Globals.LogTag, "Can't stop sensor as it is already stopped");
316 throw new InvalidOperationException("Operation Failed: Sensor is already stopped");
320 public void Dispose()
323 GC.SuppressFinalize(this);
326 protected virtual void Dispose(bool disposing)
335 protected void SetAttribute(SensorAttribute attribute, int option)
337 if (CheckListenerHandle())
339 int error = Interop.SensorListener.SetAttribute(_listenerHandle, attribute, option);
340 if (error != (int)SensorError.None)
342 Log.Error(Globals.LogTag, "Error setting sensor pause policy");
343 throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.PausePolicy Failed");
348 private void GetHandleList(SensorType type, int index)
353 int error = Interop.SensorManager.GetSensorList(type, out list, out count);
354 if (error != (int)SensorError.None)
356 Log.Error(Globals.LogTag, "Error getting sensor list");
357 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.GetSensorList Failed");
359 sensorList = Interop.IntPtrToIntPtrArray(list, count);
360 _sensorHandle = sensorList[index];
361 Interop.Libc.Free(list);
364 private void GetProperty()
366 int error = (int)SensorError.None;
368 error = Interop.Sensor.GetName(_sensorHandle, out _name);
369 if (error != (int)SensorError.None)
371 Log.Error(Globals.LogTag, "Error getting sensor name");
372 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Name Failed");
375 error = Interop.Sensor.GetVendor(_sensorHandle, out _vendor);
376 if (error != (int)SensorError.None)
378 Log.Error(Globals.LogTag, "Error getting sensor vendor name");
379 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Vendor Failed");
382 error = Interop.Sensor.GetMinRange(_sensorHandle, out _minValue);
383 if (error != (int)SensorError.None)
385 Log.Error(Globals.LogTag, "Error getting sensor min value");
386 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MinValue Failed");
389 error = Interop.Sensor.GetMaxRange(_sensorHandle, out _maxValue);
390 if (error != (int)SensorError.None)
392 Log.Error(Globals.LogTag, "Error getting sensor max value");
393 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MaxValue Failed");
396 error = Interop.Sensor.GetResolution(_sensorHandle, out _resolution);
397 if (error != (int)SensorError.None)
399 Log.Error(Globals.LogTag, "Error getting sensor resolution");
400 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Resolution Failed");
403 error = Interop.Sensor.GetMinInterval(_sensorHandle, out _minInterval);
404 if (error != (int)SensorError.None)
406 Log.Error(Globals.LogTag, "Error getting sensor min interval");
407 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MinInterval Failed");
410 error = Interop.Sensor.GetFifoCount(_sensorHandle, out _fifoCount);
411 if (error != (int)SensorError.None)
413 Log.Error(Globals.LogTag, "Error getting sensor fifo count");
414 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.FifoCount Failed");
417 error = Interop.Sensor.GetMaxBatchCount(_sensorHandle, out _maxBatchCount);
418 if (error != (int)SensorError.None)
420 Log.Error(Globals.LogTag, "Error getting sensor max batch count");
421 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MaxBatchCount Failed");
425 private void CreateListener()
427 int error = Interop.SensorListener.CreateListener(_sensorHandle, out _listenerHandle);
428 if (error != (int)SensorError.None)
430 Log.Error(Globals.LogTag, "Error cerating sensor listener handle");
431 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.CreateListener Failed");
435 private void SetInterval()
437 if (CheckListenerHandle())
441 int error = Interop.SensorListener.SetInterval(_listenerHandle, _interval);
442 if (error != (int)SensorError.None)
444 Log.Error(Globals.LogTag, "Error setting sensor interval");
445 throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.SetInterval Failed");
451 private void SetMaxBatchLatency()
453 if (CheckListenerHandle())
455 int error = Interop.SensorListener.SetMaxBatchLatency(_listenerHandle, _maxBatchLatency);
456 if (error != (int)SensorError.None)
458 Log.Error(Globals.LogTag, "Error setting max batch latency");
459 throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.MaxBatchLatency Failed");
464 private bool CheckListenerHandle()
467 if (_listenerHandle != IntPtr.Zero)
473 Log.Error(Globals.LogTag, "Sensor listener handle is null");
474 throw new ArgumentException("Invalid Parameter: Sensor is null");
479 private bool CheckSensorHandle()
482 if (_sensorHandle != IntPtr.Zero)
488 Log.Error(Globals.LogTag, "Sensor handle is null");
489 throw new ArgumentException("Invalid Parameter: Sensor is null");
494 private void DestroyHandles()
496 Interop.SensorListener.DestroyListener(_listenerHandle);