[Sensor] Sync with API7 branch (#1303)
[platform/core/csapi/tizenfx.git] / src / Tizen.Sensor / Tizen.Sensor / Plugins / GravitySensor.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     /// The GravitySensor class is used for registering callbacks for the gravity sensor and getting the gravity data.
23     /// </summary>
24     /// <since_tizen> 3 </since_tizen>
25     public sealed class GravitySensor : Sensor
26     {
27         private const string GravitySensorKey = "http://tizen.org/feature/sensor.gravity";
28
29         private event EventHandler<SensorAccuracyChangedEventArgs> _accuracyChanged;
30         /// <summary>
31         /// Gets the X component of the gravity.
32         /// </summary>
33         /// <since_tizen> 3 </since_tizen>
34         /// <value> X </value>
35         public float X { get; private set; } = float.MinValue;
36
37         /// <summary>
38         /// Gets the Y component of the gravity.
39         /// </summary>
40         /// <since_tizen> 3 </since_tizen>
41         /// <value> Y </value>
42         public float Y { get; private set; } = float.MinValue;
43
44         /// <summary>
45         /// Gets the Z component of the gravity.
46         /// </summary>
47         /// <since_tizen> 3 </since_tizen>
48         /// <value> Z </value>
49         public float Z { get; private set; } = float.MinValue;
50
51         /// <summary>
52         /// Returns true or false based on whether the gravity sensor is supported by the device.
53         /// </summary>
54         /// <since_tizen> 3 </since_tizen>
55         /// <value><c>true</c> if supported; otherwise <c>false</c>.</value>
56         public static bool IsSupported
57         {
58             get
59             {
60                 Log.Info(Globals.LogTag, "Checking if the GravitySensor is supported");
61                 return CheckIfSupported(SensorType.GravitySensor, GravitySensorKey);
62             }
63         }
64
65         /// <summary>
66         /// Returns the number of gravity sensors available on the device.
67         /// </summary>
68         /// <since_tizen> 3 </since_tizen>
69         /// <value> The count of gravity sensors. </value>
70         public static int Count
71         {
72             get
73             {
74                 Log.Info(Globals.LogTag, "Getting the count of gravity sensors");
75                 return GetCount();
76             }
77         }
78
79         /// <summary>
80         /// Initializes a new instance of the <see cref="Tizen.Sensor.GravitySensor"/> class.
81         /// </summary>
82         /// <since_tizen> 3 </since_tizen>
83         /// <feature>http://tizen.org/feature/sensor.gravity</feature>
84         /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
85         /// <exception cref="NotSupportedException">Thrown when the sensor is not supported.</exception>
86         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
87         /// <param name='index'>
88         /// Index. Default value for this is 0. Index refers to a particular gravity sensor in case of multiple sensors.
89         /// </param>
90         public GravitySensor (uint index = 0) : base(index)
91         {
92             Log.Info(Globals.LogTag, "Creating GravitySensor object");
93         }
94
95         internal override SensorType GetSensorType()
96         {
97             return SensorType.GravitySensor;
98         }
99
100         /// <summary>
101         /// An event handler for storing the callback functions for the event corresponding to the change in the gravity sensor data.
102         /// </summary>
103         /// <since_tizen> 3 </since_tizen>
104
105         public event EventHandler<GravitySensorDataUpdatedEventArgs> DataUpdated;
106
107         /// <summary>
108         /// An event handler for accuracy changed events.
109         /// </summary>
110         /// <since_tizen> 3 </since_tizen>
111         public event EventHandler<SensorAccuracyChangedEventArgs> AccuracyChanged
112         {
113             add
114             {
115                 if (_accuracyChanged == null)
116                 {
117                     AccuracyListenStart();
118                 }
119                 _accuracyChanged += value;
120             }
121             remove
122             {
123                 _accuracyChanged -= value;
124                 if (_accuracyChanged == null)
125                 {
126                     AccuracyListenStop();
127                 }
128             }
129         }
130
131         private static int GetCount()
132         {
133             IntPtr list;
134             int count;
135             int error = Interop.SensorManager.GetSensorList(SensorType.GravitySensor, out list, out count);
136             if (error != (int)SensorError.None)
137             {
138                 Log.Error(Globals.LogTag, "Error getting sensor list for gravity");
139                 count = 0;
140             }
141             else
142                 Interop.Libc.Free(list);
143             return count;
144         }
145
146         /// <summary>
147         /// Read gravity sensor data synchronously.
148         /// </summary>
149         internal override void ReadData()
150         {
151             Interop.SensorEventStruct sensorData;
152             int error = Interop.SensorListener.ReadData(ListenerHandle, out sensorData);
153             if (error != (int)SensorError.None)
154             {
155                 Log.Error(Globals.LogTag, "Error reading gravity sensor data");
156                 throw SensorErrorFactory.CheckAndThrowException(error, "Reading gravity sensor data failed");
157             }
158
159             TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
160             X = sensorData.values[0];
161             Y = sensorData.values[1];
162             Z = sensorData.values[2];
163         }
164
165         private static Interop.SensorListener.SensorEventCallback _callback;
166
167         internal override void EventListenStart()
168         {
169             _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
170                 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
171
172                 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
173                 X = sensorData.values[0];
174                 Y = sensorData.values[1];
175                 Z = sensorData.values[2];
176
177                 DataUpdated?.Invoke(this, new GravitySensorDataUpdatedEventArgs(sensorData.values));
178             };
179
180             int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
181             if (error != (int)SensorError.None)
182             {
183                 Log.Error(Globals.LogTag, "Error setting event callback for gravity sensor");
184                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for gravity");
185             }
186         }
187
188         internal override void EventListenStop()
189         {
190             int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
191             if (error != (int)SensorError.None)
192             {
193                 Log.Error(Globals.LogTag, "Error unsetting event callback for gravity sensor");
194                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for gravity");
195             }
196         }
197
198         private static Interop.SensorListener.SensorAccuracyCallback _accuracyCallback;
199
200         private void AccuracyListenStart()
201         {
202             _accuracyCallback = (IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data) => {
203                 TimeSpan = new TimeSpan((Int64)timestamp);
204                 _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(new TimeSpan((Int64)timestamp), accuracy));
205             };
206
207             int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, _accuracyCallback, IntPtr.Zero);
208             if (error != (int)SensorError.None)
209             {
210                 Log.Error(Globals.LogTag, "Error setting accuracy event callback for gravity sensor");
211                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set accuracy event callback for gravity");
212             }
213         }
214
215         private void AccuracyListenStop()
216         {
217             int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
218             if (error != (int)SensorError.None)
219             {
220                 Log.Error(Globals.LogTag, "Error unsetting accuracy event callback for gravity sensor");
221                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset accuracy event callback for gravity");
222             }
223         }
224     }
225 }