csapi-sensor: remove interval parameter of accuracy listeners
[platform/core/csapi/tizenfx.git] / src / Tizen.Sensor / Tizen.Sensor / Plugins / LinearAccelerationSensor.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18
19 namespace Tizen.Sensor
20 {
21     /// <summary>
22     /// LinearAccelerationSensor Class. Used for registering callbacks for linear acceleration sensor and getting linear acceleration data
23     /// /// </summary>
24     public sealed class LinearAccelerationSensor : Sensor
25     {
26         private const string LinearAccelerationSensorKey = "http://tizen.org/feature/sensor.linear_acceleration";
27
28         private event EventHandler<SensorAccuracyChangedEventArgs> _accuracyChanged;
29         /// <summary>
30         /// Gets the X component of the linear acceleration.
31         /// </summary>
32         public float X { get; private set; } = float.MinValue;
33
34         /// <summary>
35         /// Gets the Y component of the linear acceleration.
36         /// </summary>
37         public float Y { get; private set; } = float.MinValue;
38
39         /// <summary>
40         /// Gets the Z component of the linear acceleration.
41         /// </summary>
42         public float Z { get; private set; } = float.MinValue;
43
44         /// <summary>
45         /// Returns true or false based on whether linear acceleration sensor is supported by device.
46         /// </summary>
47         public static bool IsSupported
48         {
49             get
50             {
51                 Log.Info(Globals.LogTag, "Checking if the LinearAccelerationSensor is supported");
52                 return CheckIfSupported(SensorType.LinearAccelerationSensor, LinearAccelerationSensorKey);
53             }
54         }
55
56         /// <summary>
57         /// Returns the number of linear acceleration sensors available on the device.
58         /// </summary>
59         public static int Count
60         {
61             get
62             {
63                 Log.Info(Globals.LogTag, "Getting the count of linear acceleration sensors");
64                 return GetCount();
65             }
66         }
67
68         /// <summary>
69         /// Initializes a new instance of the <see cref="Tizen.Sensor.LinearAccelerationSensor"/> class.
70         /// </summary>
71         /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
72         /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
73         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
74         /// <param name='index'>
75         /// Index. Default value for this is 0. Index refers to a particular linear acceleration sensor in case of multiple sensors
76         /// </param>
77         public LinearAccelerationSensor(uint index = 0) : base(index)
78         {
79             Log.Info(Globals.LogTag, "Creating LinearAccelerationSensor object");
80         }
81
82         internal override SensorType GetSensorType()
83         {
84             return SensorType.LinearAccelerationSensor;
85         }
86
87         /// <summary>
88         /// Event Handler for storing the callback functions for event corresponding to change in linear acceleration sensor data.
89         /// </summary>
90
91         public event EventHandler<LinearAccelerationSensorDataUpdatedEventArgs> DataUpdated;
92
93         /// <summary>
94         /// Event handler for accuracy changed events.
95         /// </summary>
96         public event EventHandler<SensorAccuracyChangedEventArgs> AccuracyChanged
97         {
98             add
99             {
100                 if (_accuracyChanged == null)
101                 {
102                     AccuracyListenStart();
103                 }
104                 _accuracyChanged += value;
105             }
106             remove
107             {
108                 _accuracyChanged -= value;
109                 if (_accuracyChanged == null)
110                 {
111                     AccuracyListenStop();
112                 }
113             }
114         }
115
116         private static int GetCount()
117         {
118             IntPtr list;
119             int count;
120             int error = Interop.SensorManager.GetSensorList(SensorType.LinearAccelerationSensor, out list, out count);
121             if (error != (int)SensorError.None)
122             {
123                 Log.Error(Globals.LogTag, "Error getting sensor list for linear acceleration sensor");
124                 count = 0;
125             }
126             else
127                 Interop.Libc.Free(list);
128             return count;
129         }
130
131         protected internal override void EventListenStart()
132         {
133             int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero);
134             if (error != (int)SensorError.None)
135             {
136                 Log.Error(Globals.LogTag, "Error setting event callback for linear acceleration sensor");
137                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for linear acceleration sensor");
138             }
139         }
140
141         protected internal override void EventListenStop()
142         {
143             int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
144             if (error != (int)SensorError.None)
145             {
146                 Log.Error(Globals.LogTag, "Error unsetting event callback for linear acceleration sensor");
147                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for linear acceleration");
148             }
149         }
150
151         private void AccuracyListenStart()
152         {
153             int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, AccuracyEventCallback, IntPtr.Zero);
154             if (error != (int)SensorError.None)
155             {
156                 Log.Error(Globals.LogTag, "Error setting accuracy event callback for linear acceleration sensor");
157                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set accuracy event callback for linear acceleration sensor");
158             }
159         }
160
161         private void AccuracyListenStop()
162         {
163             int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
164             if (error != (int)SensorError.None)
165             {
166                 Log.Error(Globals.LogTag, "Error unsetting accuracy event callback for linear acceleration sensor");
167                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset accuracy event callback for linear acceleration sensor");
168             }
169         }
170
171         private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data)
172         {
173             Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(sensorPtr);
174             TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
175             X = sensorData.values[0];
176             Y = sensorData.values[1];
177             Z = sensorData.values[2];
178
179             DataUpdated?.Invoke(this, new LinearAccelerationSensorDataUpdatedEventArgs(sensorData.values));
180         }
181
182         private void AccuracyEventCallback(IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data)
183         {
184             TimeSpan = new TimeSpan((Int64)timestamp);
185             _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(new TimeSpan((Int64)timestamp), accuracy));
186         }
187     }
188 }