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