33bc4a39059a6f370f8c86c6adf2fc768b7c59a9
[platform/core/csapi/sensor.git] / Tizen.System.Sensor / Tizen.System.Sensor / Sensor.cs
1 // Copyright 2016 by Samsung Electronics, Inc.,
2 //
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.
8
9 using System;
10
11
12 namespace Tizen.System.Sensor
13 {
14     internal static class Globals
15     {
16         internal const string LogTag = "Tizen.System.Sensor";
17     }
18
19     /// <summary>
20     /// Sensor class for storing hardware information about a particular sensor
21     /// </summary>
22     public abstract class Sensor : IDisposable
23     {
24         private string _name;
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;
40
41         internal abstract SensorType GetSensorType();
42         protected abstract void EventListenStart();
43         protected abstract void EventListenStop();
44
45         internal Sensor(int index)
46         {
47             SensorType type = GetSensorType();
48             GetHandleList(type, index);
49             if (CheckSensorHandle())
50             {
51                 CreateListener();
52                 GetProperty();
53             }
54         }
55
56         ~Sensor()
57         {
58             Dispose(false);
59         }
60
61         /// <summary>
62         /// Property: For getting the name of the sensor
63         /// </summary>
64         public string Name
65         {
66             get
67             {
68                 Log.Info(Globals.LogTag, "Getting the sensor name");
69                 return _name;
70             }
71         }
72
73         /// <summary>
74         /// Property: Gets the vendor.
75         /// </summary>
76         public string Vendor
77         {
78             get
79             {
80                 Log.Info(Globals.LogTag, "Getting the sensor vendor name");
81                 return _vendor;
82             }
83         }
84
85         /// <summary>
86         /// Property: Gets the minimum value of range of sensor data.
87         /// </summary>
88         public float MinValue
89         {
90             get
91             {
92                 Log.Info(Globals.LogTag, "Getting the min value of the sensor");
93                 return _minValue;
94             }
95         }
96
97         /// <summary>
98         /// Property: Gets the maximum value of range of sensor data.
99         /// </summary>
100         public float MaxValue
101         {
102             get
103             {
104                 Log.Info(Globals.LogTag, "Getting the max value of the sensor");
105                 return _maxValue;
106             }
107         }
108
109         /// <summary>
110         /// Property: Gets the resolution.
111         /// </summary>
112         public float Resolution
113         {
114             get
115             {
116                 Log.Info(Globals.LogTag, "Getting the resolution of the sensor");
117                 return _resolution;
118             }
119         }
120
121         /// <summary>
122         /// Property: Gets the minimum interval.
123         /// </summary>
124         public int MinInterval
125         {
126             get
127             {
128                 Log.Info(Globals.LogTag, "Getting the min interval for the sensor");
129                 return _minInterval;
130             }
131         }
132
133         /// <summary>
134         /// Property: Gets the fifo count.
135         /// </summary>
136         public int FifoCount
137         {
138             get
139             {
140                 Log.Info(Globals.LogTag, "Getting the fifo count of the sensor");
141                 return _fifoCount;
142             }
143         }
144
145         /// <summary>
146         /// Property: Gets the maximum batch count.
147         /// </summary>
148         public int MaxBatchCount
149         {
150             get
151             {
152                 Log.Info(Globals.LogTag, "Getting the max batch count of the sensor");
153                 return _maxBatchCount;
154             }
155         }
156
157         /// <summary>
158         /// Sets the interval of the sensor for sensor data event
159         /// Callbacks will be called at frequency of this interval
160         /// </summary>
161         public uint Interval
162         {
163             set
164             {
165                 Log.Info(Globals.LogTag, "Setting the interval of the sensor");
166                 _interval = value;
167                 SetInterval();
168             }
169             get
170             {
171                 Log.Info(Globals.LogTag, "Getting the interval of the sensor");
172                 return _interval;
173             }
174         }
175
176         /// <summary>
177         /// Sets the max batch latency for the sensor corresponding to the sensor data event.
178         /// </summary>
179         public uint MaxBatchLatency
180         {
181             set
182             {
183                 Log.Info(Globals.LogTag, "Setting the max batch latency of the sensor");
184                 _maxBatchLatency = value;
185                 SetMaxBatchLatency();
186             }
187             get
188             {
189                 Log.Info(Globals.LogTag, "Getting the max batch latency of the sensor");
190                 return _maxBatchLatency;
191             }
192         }
193
194         /// <summary>
195         /// Gets the attribute.
196         /// </summary>
197         /// <value>
198         public SensorPausePolicy PausePolicy
199         {
200             set
201             {
202                 Log.Info(Globals.LogTag, "Setting the pause policy of the sensor");
203                 _pausePolicy = value;
204                 SetPausePolicy();
205             }
206             get
207             {
208                 Log.Info(Globals.LogTag, "Getting the pause policy of the sensor");
209                 return _pausePolicy;
210             }
211         }
212
213         public TimeSpan TimeSpan
214         {
215             set
216             {
217                 Log.Info(Globals.LogTag, "Setting the timespan of the sensor values");
218                 _timeSpan = value;
219             }
220             get
221             {
222                 Log.Info(Globals.LogTag, "Getting the timespan of the sensor values");
223                 return _timeSpan;
224             }
225         }
226
227         public bool IsSensing
228         {
229             get
230             {
231                 Log.Info(Globals.LogTag, "Checking if the sensor is started");
232                 return _isSensing;
233             }
234         }
235
236         protected IntPtr ListenerHandle
237         {
238             get
239             {
240                 return _listenerHandle;
241             }
242         }
243
244         /// <summary>
245         /// Starts the sensor.
246         /// After this the event handlers will start receiving events.
247         /// </summary>
248         public void Start()
249         {
250             Log.Info(Globals.LogTag, "Starting the sensor");
251             if (CheckListenerHandle())
252             {
253                 int error = Interop.SensorListener.StartListener(_listenerHandle);
254                 if (error != (int)SensorError.None)
255                 {
256                     Log.Error(Globals.LogTag, "Error starting sensor");
257                     throw SensorErrorFactory.CheckAndThrowException(error, "Unable to Start Sensor Listener");
258                 }
259                 EventListenStart();
260                 _isSensing = true;
261                 Log.Info(Globals.LogTag, "Sensor started");
262             }
263         }
264
265         /// <summary>
266         /// Stop the sensor.
267         /// After this the event handlers will stop receiving the events
268         /// </summary>
269         public void Stop()
270         {
271             Log.Info(Globals.LogTag, "Stopping the sensor");
272             if (_isSensing)
273             {
274                 int error = Interop.SensorListener.StopListener(_listenerHandle);
275                 if (error != (int)SensorError.None)
276                 {
277                     Log.Error(Globals.LogTag, "Error stopping the sensor");
278                     throw SensorErrorFactory.CheckAndThrowException(error, "Unable to Stop Sensor Listener");
279                 }
280                 EventListenStop();
281                 _isSensing = false;
282                 Log.Info(Globals.LogTag, "Sensor stopped");
283             }
284             else
285             {
286                 Log.Error(Globals.LogTag, "Can't stop sensor as it is already stopped");
287                 throw new InvalidOperationException("Operation Failed: Sensor is already stopped");
288             }
289         }
290
291         public void Dispose()
292         {
293             Dispose(true);
294             GC.SuppressFinalize(this);
295         }
296
297         protected virtual void Dispose(bool disposing)
298         {
299             if (_disposed)
300                 return;
301
302             DestroyHandles();
303             _disposed = true;
304         }
305
306         private void GetHandleList(SensorType type, int index)
307         {
308             IntPtr list;
309             IntPtr[] sensorList;
310             int count;
311             int error = Interop.SensorManager.GetSensorList(type, out list, out count);
312             if (error != (int)SensorError.None)
313             {
314                 Log.Error(Globals.LogTag, "Error getting sensor list");
315                 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.GetSensorList Failed");
316             }
317             sensorList = Interop.IntPtrToIntPtrArray(list, count);
318             _sensorHandle = sensorList[index];
319             Interop.Libc.Free(list);
320         }
321
322         private void GetProperty()
323         {
324             int error = (int)SensorError.None;
325
326             error = Interop.Sensor.GetName(_sensorHandle, out _name);
327             if (error != (int)SensorError.None)
328             {
329                 Log.Error(Globals.LogTag, "Error getting sensor name");
330                 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Name Failed");
331             }
332
333             error = Interop.Sensor.GetVendor(_sensorHandle, out _vendor);
334             if (error != (int)SensorError.None)
335             {
336                 Log.Error(Globals.LogTag, "Error getting sensor vendor name");
337                 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Vendor Failed");
338             }
339
340             error = Interop.Sensor.GetMinRange(_sensorHandle, out _minValue);
341             if (error != (int)SensorError.None)
342             {
343                 Log.Error(Globals.LogTag, "Error getting sensor min value");
344                 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MinValue Failed");
345             }
346
347             error = Interop.Sensor.GetMaxRange(_sensorHandle, out _maxValue);
348             if (error != (int)SensorError.None)
349             {
350                 Log.Error(Globals.LogTag, "Error getting sensor max value");
351                 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MaxValue Failed");
352             }
353
354             error = Interop.Sensor.GetResolution(_sensorHandle, out _resolution);
355             if (error != (int)SensorError.None)
356             {
357                 Log.Error(Globals.LogTag, "Error getting sensor resolution");
358                 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.Resolution Failed");
359             }
360
361             error = Interop.Sensor.GetMinInterval(_sensorHandle, out _minInterval);
362             if (error != (int)SensorError.None)
363             {
364                 Log.Error(Globals.LogTag, "Error getting sensor min interval");
365                 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MinInterval Failed");
366             }
367
368             error = Interop.Sensor.GetFifoCount(_sensorHandle, out _fifoCount);
369             if (error != (int)SensorError.None)
370             {
371                 Log.Error(Globals.LogTag, "Error getting sensor fifo count");
372                 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.FifoCount Failed");
373             }
374
375             error = Interop.Sensor.GetMaxBatchCount(_sensorHandle, out _maxBatchCount);
376             if (error != (int)SensorError.None)
377             {
378                 Log.Error(Globals.LogTag, "Error getting sensor max batch count");
379                 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.MaxBatchCount Failed");
380             }
381         }
382
383         private void CreateListener()
384         {
385             int error = Interop.SensorListener.CreateListener(_sensorHandle, out _listenerHandle);
386             if (error != (int)SensorError.None)
387             {
388                 Log.Error(Globals.LogTag, "Error cerating sensor listener handle");
389                 throw SensorErrorFactory.CheckAndThrowException(error, "Sensor.CreateListener Failed");
390             }
391         }
392
393         private void SetInterval()
394         {
395             if (CheckListenerHandle())
396             {
397                 if (_isSensing)
398                 {
399                     int error = Interop.SensorListener.SetInterval(_listenerHandle, _interval);
400                     if (error != (int)SensorError.None)
401                     {
402                         Log.Error(Globals.LogTag, "Error setting sensor interval");
403                         throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.SetInterval Failed");
404                     }
405                 }
406             }
407         }
408
409         private void SetMaxBatchLatency()
410         {
411             if (CheckListenerHandle())
412             {
413                 int error = Interop.SensorListener.SetMaxBatchLatency(_listenerHandle, _maxBatchLatency);
414                 if (error != (int)SensorError.None)
415                 {
416                     Log.Error(Globals.LogTag, "Error setting max batch latency");
417                     throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.MaxBatchLatency Failed");
418                 }
419             }
420         }
421
422         private void SetPausePolicy()
423         {
424             if (CheckListenerHandle())
425             {
426                 int error = Interop.SensorListener.SetAttribute(_listenerHandle, SensorAttribute.PausePolicy, (int)_pausePolicy);
427                 if (error != (int)SensorError.None)
428                 {
429                     Log.Error(Globals.LogTag, "Error setting sensor pause policy");
430                     throw SensorErrorFactory.CheckAndThrowException(error, "Setting Sensor.PausePolicy Failed");
431                 }
432             }
433         }
434
435         private bool CheckListenerHandle()
436         {
437             bool result = false;
438             if (_listenerHandle != IntPtr.Zero)
439             {
440                 result = true;
441             }
442             else
443             {
444                 Log.Error(Globals.LogTag, "Sensor listener handle is null");
445                 throw new ArgumentException("Invalid Parameter: Sensor is null");
446             }
447             return result;
448         }
449
450         private bool CheckSensorHandle()
451         {
452             bool result = false;
453             if (_sensorHandle != IntPtr.Zero)
454             {
455                 result = true;
456             }
457             else
458             {
459                 Log.Error(Globals.LogTag, "Sensor handle is null");
460                 throw new ArgumentException("Invalid Parameter: Sensor is null");
461             }
462             return result;
463         }
464
465         private void DestroyHandles()
466         {
467             Interop.SensorListener.DestroyListener(_listenerHandle);
468         }
469     }
470 }