9e5a0d68528b237889fa43d4697edbf1a22b4412
[platform/core/csapi/sensor.git] / Tizen.Sensor / Tizen.Sensor / Plugins / Pedometer.cs
1 // Copyright 2016 by Samsung Electronics, Inc.,
2 //
3 // This software is the confidential and proprietary information
4 // of Samsung Electronics, Inc. ("Confidential Information"). You
5 // shall not disclose such Confidential Information and shall use
6 // it only in accordance with the terms of the license agreement
7 // you entered into with Samsung.
8
9 using System;
10
11 namespace Tizen.Sensor
12 {
13     /// <summary>
14     /// Pedometer Sensor Class. Used for registering callbacks for pedometer and getting pedometer data
15     /// /// </summary>
16     public class Pedometer : Sensor
17     {
18         private static string PedometerKey = "http://tizen.org/feature/sensor.pedometer";
19
20         /// <summary>
21         /// Gets the step count
22         /// </summary>
23         public int StepCount { get; private set; }
24
25         /// <summary>
26         /// Gets the walking step count
27         /// </summary>
28         public int WalkStepCount { get; private set; }
29
30         /// <summary>
31         /// Gets the running step count
32         /// </summary>
33         public int RunStepCount { get; private set; }
34
35         /// <summary>
36         /// Gets the moving distance
37         /// </summary>
38         public float MovingDistance { get; private set; }
39
40         /// <summary>
41         /// Gets the calorie burned
42         /// </summary>
43         public float CalorieBurned { get; private set; }
44
45         /// <summary>
46         /// Gets the last speed
47         /// </summary>
48         public float LastSpeed { get; private set; }
49
50         /// <summary>
51         /// Gets the last stepping frequency
52         /// </summary>
53         public float LastSteppingFrequency { get; private set; }
54
55         /// <summary>
56         /// Gets the last step status
57         /// </summary>
58         public PedometerState LastStepStatus { get; private set; }
59
60         /// <summary>
61         /// Returns true or false based on whether pedometer sensor is supported by device.
62         /// </summary>
63         public static bool IsSupported
64         {
65             get
66             {
67                 Log.Info(Globals.LogTag, "Checking if the Pedometer sensor is supported");
68                 return CheckIfSupported(SensorType.Pedometer, PedometerKey);
69             }
70         }
71
72         /// <summary>
73         /// Returns the number of pedometer sensors available on the device.
74         /// </summary>
75         public static int Count
76         {
77             get
78             {
79                 Log.Info(Globals.LogTag, "Getting the count of pedometer sensors");
80                 return GetCount();
81             }
82         }
83
84         /// <summary>
85         /// Initializes a new instance of the <see cref="Tizen.Sensor.Pedometer"/> class.
86         /// </summary>
87         /// <remarks>
88         /// For accessing pedometer, app should have http://tizen.org/privilege/healthinfo privilege.
89         /// </remarks>
90         /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
91         /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
92         /// <exception cref="UnauthroizedAccessException">Thrown when the app has no privilege to use the sensor</exception>
93         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
94         /// <param name='index'>
95         /// Index. Default value for this is 0. Index refers to a particular pedometer sensor in case of multiple sensors
96         /// </param>
97         public Pedometer(int index = 0) : base(index)
98         {
99             Log.Info(Globals.LogTag, "Creating Pedometer object");
100         }
101
102         internal override SensorType GetSensorType()
103         {
104             return SensorType.Pedometer;
105         }
106
107         /// <summary>
108         /// Event Handler for storing the callback functions for event corresponding to change in pedometer sensor data.
109         /// </summary>
110
111         public event EventHandler<PedometerDataUpdatedEventArgs> DataUpdated;
112
113         private static int GetCount()
114         {
115             IntPtr list;
116             int count;
117             int error = Interop.SensorManager.GetSensorList(SensorType.Pedometer, out list, out count);
118             if (error != (int)SensorError.None)
119             {
120                 Log.Error(Globals.LogTag, "Error getting sensor list for pedometer");
121                 count = 0;
122             }
123             else
124                 Interop.Libc.Free(list);
125             return count;
126         }
127
128         protected override void EventListenStart()
129         {
130             int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero);
131             if (error != (int)SensorError.None)
132             {
133                 Log.Error(Globals.LogTag, "Error setting event callback for pedometer sensor");
134                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for pedometer");
135             }
136         }
137
138         protected override void EventListenStop()
139         {
140             int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
141             if (error != (int)SensorError.None)
142             {
143                 Log.Error(Globals.LogTag, "Error unsetting event callback for pedometer sensor");
144                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for pedometer");
145             }
146         }
147
148         private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data)
149         {
150             Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(sensorPtr);
151             TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
152             StepCount = (int)sensorData.values[0];
153             WalkStepCount = (int)sensorData.values[1];
154             RunStepCount = (int)sensorData.values[2];
155             MovingDistance = sensorData.values[3];
156             CalorieBurned = sensorData.values[4];
157             LastSpeed = sensorData.values[5];
158             LastSteppingFrequency = sensorData.values[6];
159             LastStepStatus = (PedometerState)sensorData.values[7];
160
161             DataUpdated?.Invoke(this, new PedometerDataUpdatedEventArgs(sensorData.values));
162         }
163     }
164 }