sensord: enable samsung pedometer sensor for fused location
[platform/core/system/sensord.git] / src / sensor / pedometer / pedometer_sensor.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
18 #include "pedometer_sensor.h"
19
20 #include <sensor_log.h>
21 #include <sensor_types.h>
22 #include <fusion_util.h>
23 #include <cmath>
24
25 #define NAME_SENSOR "http://samsung.com/sensor/healthinfo/pedometer/samsung_pedometer"
26 #define NAME_VENDOR "samsung.com"
27
28 #define SRC_ID_ACC   0x1
29 #define SRC_STR_ACC  "http://tizen.org/sensor/general/accelerometer"
30
31 #define NORM(x, y, z) sqrt((x)*(x) + (y)*(y) + (z)*(z))
32
33 #define US_TO_NS(x) (x * 1000)
34
35 /* Sensor information */
36 static sensor_info2_t sensor_info = {
37         id: 0x1,
38         type: HUMAN_PEDOMETER_SENSOR,
39         uri: NAME_SENSOR,
40         vendor: NAME_VENDOR,
41         min_range: 0,
42         max_range: 1,
43         resolution: 1,
44         min_interval: 0,
45         max_batch_count: 0,
46         wakeup_supported: false,
47         privilege: "http://tizen.org/privilege/healthinfo",
48 };
49
50 /* Required sensor list */
51 static required_sensor_s required_sensors[] = {
52         {SRC_ID_ACC, SRC_STR_ACC}
53 };
54
55 pedometer_sensor::pedometer_sensor()
56 : m_step_count(-1)
57 , m_step_length(-1)
58 , m_step_total_length(-1)
59 , m_step_speed(-1)
60 , m_time(0)
61 {
62 }
63
64 pedometer_sensor::~pedometer_sensor()
65 {
66 }
67
68 int pedometer_sensor::get_sensor_info(const sensor_info2_t **info)
69 {
70         *info = &sensor_info;
71         return OP_SUCCESS;
72 }
73
74 int pedometer_sensor::get_required_sensors(const required_sensor_s **sensors)
75 {
76         *sensors = required_sensors;
77
78         /* You should return the number of required sensor */
79         return 1;
80 }
81
82 int pedometer_sensor::update(uint32_t id, sensor_data_t *data, int len)
83 {
84         pedometer_info info;
85         double acc = NORM(data->values[0], data->values[1], data->values[2]);
86
87         if (!m_pedometer.get_pedometer(US_TO_NS(data->timestamp), acc, &info))
88                 return OP_ERROR;
89
90         m_step_count = info.step_count;
91         m_step_length = info.step_length;
92         m_step_total_length = info.total_step_length;
93         m_step_speed = info.step_speed;
94         m_time = data->timestamp;
95
96         _D("[%lld] %lld %f %f %f", data->timestamp,
97                         info.step_count, info.step_length, info.total_step_length, info.step_speed);
98
99         return OP_SUCCESS;
100 }
101
102 int pedometer_sensor::get_data(sensor_data_t **data, int *len)
103 {
104         sensor_data_t *sensor_data;
105         sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t));
106
107         sensor_data->accuracy = SENSOR_ACCURACY_GOOD;
108         sensor_data->timestamp = m_time;
109         sensor_data->value_count = 8;
110         sensor_data->values[0] = (float)m_step_count;
111         sensor_data->values[1] = (float)m_step_count;
112         sensor_data->values[2] = 0;
113         sensor_data->values[3] = m_step_total_length;
114         sensor_data->values[4] = 0;
115         sensor_data->values[5] = m_step_speed;
116         sensor_data->values[6] = 0;
117         sensor_data->values[7] = 0;
118
119         *data = sensor_data;
120         *len = sizeof(sensor_data_t);
121
122         return 0;
123 }
124
125 int pedometer_sensor::start(observer_h ob)
126 {
127         m_pedometer.reset();
128         return OP_DEFAULT;
129 }
130
131 int pedometer_sensor::stop(observer_h ob)
132 {
133         return OP_DEFAULT;
134 }