[Sensor] Modify implementation using new native API (#1367)
[platform/core/csapi/tizenfx.git] / src / Tizen.Sensor / Tizen.Sensor / Plugins / LightSensor.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     /// The LightSensor class is used for registering callbacks for the light sensor and getting the light data.
23     /// </summary>
24     /// <since_tizen> 3 </since_tizen>
25     public sealed class LightSensor : Sensor
26     {
27         private const string LightSensorKey = "http://tizen.org/feature/sensor.photometer";
28
29         /// <summary>
30         /// Gets the level of the light.
31         /// </summary>
32         /// <since_tizen> 3 </since_tizen>
33         /// <value> The light level. </value>
34         public float Level { get; private set; } = float.MinValue;
35
36         /// <summary>
37         /// Returns true or false based on whether the light sensor is supported by the device.
38         /// </summary>
39         /// <since_tizen> 3 </since_tizen>
40         /// <value><c>true</c> if supported; otherwise <c>false</c>.</value>
41         public static bool IsSupported
42         {
43             get
44             {
45                 Log.Info(Globals.LogTag, "Checking if the LightSensor is supported");
46                 return CheckIfSupported(SensorType.LightSensor, LightSensorKey);
47             }
48         }
49
50         /// <summary>
51         /// Returns the number of light sensors available on the device.
52         /// </summary>
53         /// <since_tizen> 3 </since_tizen>
54         /// <value> The count of light sensors. </value>
55         public static int Count
56         {
57             get
58             {
59                 Log.Info(Globals.LogTag, "Getting the count of light sensors");
60                 return GetCount();
61             }
62         }
63
64         /// <summary>
65         /// Initializes a new instance of the <see cref="Tizen.Sensor.LightSensor"/> class.
66         /// </summary>
67         /// <since_tizen> 3 </since_tizen>
68         /// <feature>http://tizen.org/feature/sensor.photometer</feature>
69         /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
70         /// <exception cref="NotSupportedException">Thrown when the sensor is not supported.</exception>
71         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
72         /// <param name='index'>
73         /// Index. Default value for this is 0. Index refers to a particular light sensor in case of multiple sensors.
74         /// </param>
75         public LightSensor(uint index = 0) : base(index)
76         {
77             Log.Info(Globals.LogTag, "Creating LightSensor object");
78         }
79
80         internal override SensorType GetSensorType()
81         {
82             return SensorType.LightSensor;
83         }
84
85         /// <summary>
86         /// An event handler for storing the callback functions for the event corresponding to the change in the light sensor data.
87         /// </summary>
88         /// <since_tizen> 3 </since_tizen>
89
90         public event EventHandler<LightSensorDataUpdatedEventArgs> DataUpdated;
91
92         private static int GetCount()
93         {
94             IntPtr list;
95             int count;
96             int error = Interop.SensorManager.GetSensorList(SensorType.LightSensor, out list, out count);
97             if (error != (int)SensorError.None)
98             {
99                 Log.Error(Globals.LogTag, "Error getting sensor list for light");
100                 count = 0;
101             }
102             else
103                 Interop.Libc.Free(list);
104             return count;
105         }
106
107         /// <summary>
108         /// Read light sensor data synchronously.
109         /// </summary>
110         internal override void ReadData()
111         {
112             Interop.SensorEventStruct sensorData;
113             int error = Interop.SensorListener.ReadData(ListenerHandle, out sensorData);
114             if (error != (int)SensorError.None)
115             {
116                 Log.Error(Globals.LogTag, "Error reading light sensor data");
117                 throw SensorErrorFactory.CheckAndThrowException(error, "Reading light sensor data failed");
118             }
119
120             TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
121             Level = sensorData.values[0];
122         }
123
124         private static Interop.SensorListener.SensorEventsCallback _callback;
125
126         internal override void EventListenStart()
127         {
128             _callback = (IntPtr sensorHandle, IntPtr eventPtr, uint events_count, IntPtr data) => {
129                 updateBatchEvents(eventPtr, events_count);
130                 Interop.SensorEventStruct sensorData = latestEvent();
131
132                 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
133                 Level = sensorData.values[0];
134
135                 DataUpdated?.Invoke(this, new LightSensorDataUpdatedEventArgs(sensorData.values[0]));
136             };
137
138             int error = Interop.SensorListener.SetEventsCallback(ListenerHandle, _callback, IntPtr.Zero);
139             if (error != (int)SensorError.None)
140             {
141                 Log.Error(Globals.LogTag, "Error setting event callback for light sensor");
142                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for light");
143             }
144         }
145
146         internal override void EventListenStop()
147         {
148             int error = Interop.SensorListener.UnsetEventsCallback(ListenerHandle);
149             if (error != (int)SensorError.None)
150             {
151                 Log.Error(Globals.LogTag, "Error unsetting event callback for light sensor");
152                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for light");
153             }
154         }
155     }
156 }