Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Sensor / Tizen.Sensor / Plugins / Pedometer.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     /// Pedometer Sensor Class. Used for registering callbacks for pedometer and getting pedometer data
23     /// </summary>
24     public sealed class Pedometer : Sensor
25     {
26         private static string PedometerKey = "http://tizen.org/feature/sensor.pedometer";
27
28         /// <summary>
29         /// Gets the step count
30         /// </summary>
31         /// <since_tizen> 3 </since_tizen>
32         /// <value> Step count </value>
33         public uint StepCount { get; private set; } = 0;
34
35         /// <summary>
36         /// Gets the walking step count
37         /// </summary>
38         /// <since_tizen> 3 </since_tizen>
39         /// <value> Walk step count </value>
40         public uint WalkStepCount { get; private set; } = 0;
41
42         /// <summary>
43         /// Gets the running step count
44         /// </summary>
45         /// <since_tizen> 3 </since_tizen>
46         /// <value> Run step count </value>
47         public uint RunStepCount { get; private set; } = 0;
48
49         /// <summary>
50         /// Gets the moving distance
51         /// </summary>
52         /// <since_tizen> 3 </since_tizen>
53         /// <value> Moving distance </value>
54         public float MovingDistance { get; private set; } = 0;
55
56         /// <summary>
57         /// Gets the calorie burned
58         /// </summary>
59         /// <since_tizen> 3 </since_tizen>
60         /// <value> Calorie Burned </value>
61         public float CalorieBurned { get; private set; } = 0;
62
63         /// <summary>
64         /// Gets the last speed
65         /// </summary>
66         /// <since_tizen> 3 </since_tizen>
67         /// <value> Last speed </value>
68         public float LastSpeed { get; private set; } = 0;
69
70         /// <summary>
71         /// Gets the last stepping frequency
72         /// </summary>
73         /// <since_tizen> 3 </since_tizen>
74         /// <value> Last stepping frequency </value>
75         public float LastSteppingFrequency { get; private set; } = 0;
76
77         /// <summary>
78         /// Gets the last step status
79         /// </summary>
80         /// <since_tizen> 3 </since_tizen>
81         /// <value> Last step status </value>
82         public PedometerState LastStepStatus { get; private set; } = PedometerState.Unknown;
83
84         /// <summary>
85         /// Returns true or false based on whether pedometer sensor is supported by device.
86         /// </summary>
87         /// <since_tizen> 3 </since_tizen>
88         /// <value><c>true</c> if supported; otherwise, <c>false</c>.</value>
89         public static bool IsSupported
90         {
91             get
92             {
93                 Log.Info(Globals.LogTag, "Checking if the Pedometer sensor is supported");
94                 return CheckIfSupported(SensorType.Pedometer, PedometerKey);
95             }
96         }
97
98         /// <summary>
99         /// Returns the number of pedometer sensors available on the device.
100         /// </summary>
101         /// <since_tizen> 3 </since_tizen>
102         /// <value> The count of pedometer sensors </value>
103         public static int Count
104         {
105             get
106             {
107                 Log.Info(Globals.LogTag, "Getting the count of pedometer sensors");
108                 return GetCount();
109             }
110         }
111
112         /// <summary>
113         /// Initializes a new instance of the <see cref="Tizen.Sensor.Pedometer"/> class.
114         /// </summary>
115         /// <since_tizen> 3 </since_tizen>
116         /// <privilege>http://tizen.org/privilege/healthinfo</privilege>
117         /// <privlevel>public</privlevel>
118         /// <feature>http://tizen.org/feature/sensor.pedometer</feature>
119         /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
120         /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
121         /// <exception cref="UnauthroizedAccessException">Thrown when the app has no privilege to use the sensor</exception>
122         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
123         /// <param name='index'>
124         /// Index. Default value for this is 0. Index refers to a particular pedometer sensor in case of multiple sensors
125         /// </param>
126         public Pedometer(uint index = 0) : base(index)
127         {
128             Log.Info(Globals.LogTag, "Creating Pedometer object");
129         }
130
131         internal override SensorType GetSensorType()
132         {
133             return SensorType.Pedometer;
134         }
135
136         /// <summary>
137         /// Event Handler for storing the callback functions for event corresponding to change in pedometer sensor data.
138         /// </summary>
139         /// <since_tizen> 3 </since_tizen>
140
141         public event EventHandler<PedometerDataUpdatedEventArgs> DataUpdated;
142
143         private static int GetCount()
144         {
145             IntPtr list;
146             int count;
147             int error = Interop.SensorManager.GetSensorList(SensorType.Pedometer, out list, out count);
148             if (error != (int)SensorError.None)
149             {
150                 Log.Error(Globals.LogTag, "Error getting sensor list for pedometer");
151                 count = 0;
152             }
153             else
154                 Interop.Libc.Free(list);
155             return count;
156         }
157
158         private static Interop.SensorListener.SensorEventCallback _callback;
159
160         internal override void EventListenStart()
161         {
162             _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
163                 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
164
165                 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
166                 StepCount = (uint)sensorData.values[0];
167                 WalkStepCount = (uint)sensorData.values[1];
168                 RunStepCount = (uint)sensorData.values[2];
169                 MovingDistance = sensorData.values[3];
170                 CalorieBurned = sensorData.values[4];
171                 LastSpeed = sensorData.values[5];
172                 LastSteppingFrequency = sensorData.values[6];
173                 LastStepStatus = (PedometerState)sensorData.values[7];
174
175                 DataUpdated?.Invoke(this, new PedometerDataUpdatedEventArgs(sensorData.values));
176             };
177
178             int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
179             if (error != (int)SensorError.None)
180             {
181                 Log.Error(Globals.LogTag, "Error setting event callback for pedometer sensor");
182                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for pedometer");
183             }
184         }
185
186         internal override void EventListenStop()
187         {
188             int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
189             if (error != (int)SensorError.None)
190             {
191                 Log.Error(Globals.LogTag, "Error unsetting event callback for pedometer sensor");
192                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for pedometer");
193             }
194         }
195     }
196 }