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