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