sensor: csapi: add feature/privilege tags to doxygen
[platform/core/csapi/tizenfx.git] / src / Tizen.Sensor / Tizen.Sensor / Plugins / UltravioletSensor.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     /// UltravioletSensor Class. Used for registering callbacks for ultraviolet sensor and getting ultraviolet data
23     /// /// </summary>
24     public sealed class UltravioletSensor : Sensor
25     {
26         private static string UltravioletSensorKey = "http://tizen.org/feature/sensor.ultraviolet";
27
28         /// <summary>
29         /// Gets the value of the ultraviolet sensor.
30         /// </summary>
31         public float UltravioletIndex { get; private set; } = float.MinValue;
32
33         /// <summary>
34         /// Returns true or false based on whether ultraviolet sensor is supported by device.
35         /// </summary>
36         public static bool IsSupported
37         {
38             get
39             {
40                 Log.Info(Globals.LogTag, "Checking if the UltravioletSensor is supported");
41                 return CheckIfSupported(SensorType.UltravioletSensor, UltravioletSensorKey);
42             }
43         }
44
45         /// <summary>
46         /// Returns the number of ultraviolet sensors available on the device.
47         /// </summary>
48         public static int Count
49         {
50             get
51             {
52                 Log.Info(Globals.LogTag, "Getting the count of ultraviolet sensors");
53                 return GetCount();
54             }
55         }
56
57         /// <summary>
58         /// Initializes a new instance of the <see cref="Tizen.Sensor.UltravioletSensor"/> class.
59         /// </summary>
60         /// <feature>http://tizen.org/feature/sensor.ultraviolet</feature>
61         /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
62         /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
63         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
64         /// <param name='index'>
65         /// Index. Default value for this is 0. Index refers to a particular ultraviolet sensor in case of multiple sensors
66         /// </param>
67         public UltravioletSensor(uint index = 0) : base(index)
68         {
69             Log.Info(Globals.LogTag, "Creating UltravioletSensor object");
70         }
71
72         internal override SensorType GetSensorType()
73         {
74             return SensorType.UltravioletSensor;
75         }
76
77         /// <summary>
78         /// Event Handler for storing the callback functions for event corresponding to change in ultraviolet sensor data.
79         /// </summary>
80
81         public event EventHandler<UltravioletSensorDataUpdatedEventArgs> DataUpdated;
82
83
84         private static int GetCount()
85         {
86             IntPtr list;
87             int count;
88             int error = Interop.SensorManager.GetSensorList(SensorType.UltravioletSensor, out list, out count);
89             if (error != (int)SensorError.None)
90             {
91                 Log.Error(Globals.LogTag, "Error getting sensor list for ultraviolet");
92                 count = 0;
93             }
94             else
95                 Interop.Libc.Free(list);
96             return count;
97         }
98
99         private static Interop.SensorListener.SensorEventCallback _callback;
100
101         internal override void EventListenStart()
102         {
103             _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
104                 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
105
106                 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
107                 UltravioletIndex = sensorData.values[0];
108
109                 DataUpdated?.Invoke(this, new UltravioletSensorDataUpdatedEventArgs(sensorData.values[0]));
110             };
111
112             int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
113             if (error != (int)SensorError.None)
114             {
115                 Log.Error(Globals.LogTag, "Error setting event callback for ultraviolet sensor");
116                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for ultraviolet");
117             }
118         }
119
120         internal override void EventListenStop()
121         {
122             int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
123             if (error != (int)SensorError.None)
124             {
125                 Log.Error(Globals.LogTag, "Error unsetting event callback for ultraviolet sensor");
126                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for ultraviolet");
127             }
128         }
129     }
130 }