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