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