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.System.Sensor
14 internal static class Globals
16 internal const string LogTag = "Tizen.System.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 /// Gets the attribute.
198 public SensorPausePolicy PausePolicy
202 Log.Info(Globals.LogTag, "Setting the pause policy of the sensor");
203 _pausePolicy = value;
208 Log.Info(Globals.LogTag, "Getting the pause policy of the sensor");
213 public TimeSpan TimeSpan
217 Log.Info(Globals.LogTag, "Setting the timespan of the sensor values");
222 Log.Info(Globals.LogTag, "Getting the timespan of the sensor values");
227 public bool IsSensing
231 Log.Info(Globals.LogTag, "Checking if the sensor is started");
236 protected IntPtr ListenerHandle
240 return _listenerHandle;
245 /// Starts the sensor.
246 /// After this the event handlers will start receiving events.
250 Log.Info(Globals.LogTag, "Starting the sensor");
251 if (CheckListenerHandle())
253 int error = Interop.SensorListener.StartListener(_listenerHandle);
254 if (error != (int)SensorError.None)
256 Log.Error(Globals.LogTag, "Error starting sensor");
257 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to Start Sensor Listener");
261 Log.Info(Globals.LogTag, "Sensor started");
267 /// After this the event handlers will stop receiving the events
271 Log.Info(Globals.LogTag, "Stopping the sensor");
274 int error = Interop.SensorListener.StopListener(_listenerHandle);
275 if (error != (int)SensorError.None)
277 Log.Error(Globals.LogTag, "Error stopping the sensor");
278 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to Stop Sensor Listener");
282 Log.Info(Globals.LogTag, "Sensor stopped");
286 Log.Error(Globals.LogTag, "Can't stop sensor as it is already stopped");
287 throw new InvalidOperationException("Operation Failed: Sensor is already stopped");
291 public void Dispose()
294 GC.SuppressFinalize(this);
297 protected virtual void Dispose(bool disposing)
306 private void GetHandleList(SensorType type, int index)
311 int error = Interop.SensorManager.GetSensorList(type, out list, out count);
312 if (error != (int)SensorError.None)
314 Log.Error(Globals.LogTag, "Error getting sensor list");
315 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.GetSensorList Failed");
317 sensorList = Interop.IntPtrToIntPtrArray(list, count);
318 _sensorHandle = sensorList[index];
319 Interop.Libc.Free(list);
322 private void GetProperty()
324 int error = (int)SensorError.None;
326 error = Interop.Sensor.GetName(_sensorHandle, out _name);
327 if (error != (int)SensorError.None)
329 Log.Error(Globals.LogTag, "Error getting sensor name");
330 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Name Failed");
333 error = Interop.Sensor.GetVendor(_sensorHandle, out _vendor);
334 if (error != (int)SensorError.None)
336 Log.Error(Globals.LogTag, "Error getting sensor vendor name");
337 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Vendor Failed");
340 error = Interop.Sensor.GetMinRange(_sensorHandle, out _minValue);
341 if (error != (int)SensorError.None)
343 Log.Error(Globals.LogTag, "Error getting sensor min value");
344 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MinValue Failed");
347 error = Interop.Sensor.GetMaxRange(_sensorHandle, out _maxValue);
348 if (error != (int)SensorError.None)
350 Log.Error(Globals.LogTag, "Error getting sensor max value");
351 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MaxValue Failed");
354 error = Interop.Sensor.GetResolution(_sensorHandle, out _resolution);
355 if (error != (int)SensorError.None)
357 Log.Error(Globals.LogTag, "Error getting sensor resolution");
358 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Resolution Failed");
361 error = Interop.Sensor.GetMinInterval(_sensorHandle, out _minInterval);
362 if (error != (int)SensorError.None)
364 Log.Error(Globals.LogTag, "Error getting sensor min interval");
365 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MinInterval Failed");
368 error = Interop.Sensor.GetFifoCount(_sensorHandle, out _fifoCount);
369 if (error != (int)SensorError.None)
371 Log.Error(Globals.LogTag, "Error getting sensor fifo count");
372 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.FifoCount Failed");
375 error = Interop.Sensor.GetMaxBatchCount(_sensorHandle, out _maxBatchCount);
376 if (error != (int)SensorError.None)
378 Log.Error(Globals.LogTag, "Error getting sensor max batch count");
379 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MaxBatchCount Failed");
383 private void CreateListener()
385 int error = Interop.SensorListener.CreateListener(_sensorHandle, out _listenerHandle);
386 if (error != (int)SensorError.None)
388 Log.Error(Globals.LogTag, "Error cerating sensor listener handle");
389 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.CreateListener Failed");
393 private void SetInterval()
395 if (CheckListenerHandle())
399 int error = Interop.SensorListener.SetInterval(_listenerHandle, _interval);
400 if (error != (int)SensorError.None)
402 Log.Error(Globals.LogTag, "Error setting sensor interval");
403 throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.SetInterval Failed");
409 private void SetMaxBatchLatency()
411 if (CheckListenerHandle())
413 int error = Interop.SensorListener.SetMaxBatchLatency(_listenerHandle, _maxBatchLatency);
414 if (error != (int)SensorError.None)
416 Log.Error(Globals.LogTag, "Error setting max batch latency");
417 throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.MaxBatchLatency Failed");
422 private void SetPausePolicy()
424 if (CheckListenerHandle())
426 int error = Interop.SensorListener.SetAttribute(_listenerHandle, SensorAttribute.PausePolicy, (int)_pausePolicy);
427 if (error != (int)SensorError.None)
429 Log.Error(Globals.LogTag, "Error setting sensor pause policy");
430 throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.PausePolicy Failed");
435 private bool CheckListenerHandle()
438 if (_listenerHandle != IntPtr.Zero)
444 Log.Error(Globals.LogTag, "Sensor listener handle is null");
445 throw new ArgumentException("Invalid Parameter: Sensor is null");
450 private bool CheckSensorHandle()
453 if (_sensorHandle != IntPtr.Zero)
459 Log.Error(Globals.LogTag, "Sensor handle is null");
460 throw new ArgumentException("Invalid Parameter: Sensor is null");
465 private void DestroyHandles()
467 Interop.SensorListener.DestroyListener(_listenerHandle);