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