5e4cd74444e2448d27cc2d2c1c82e6934b1b4b5e
[platform/core/system/sensord.git] / src / sensor / rotation_vector / magnetic_rv_sensor.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include "magnetic_rv_sensor.h"
21
22 #include <sensor_log.h>
23 #include <sensor_types.h>
24 #include <fusion_util.h>
25
26 #define NAME_SENSOR  "http://tizen.org/sensor/general/geomagnetic_rotation_vector/tizen_default"
27 #define NAME_VENDOR  "tizen.org"
28
29 #define SRC_ID_ACC   0x1
30 #define SRC_STR_ACC  "http://tizen.org/sensor/general/accelerometer"
31
32 #define SRC_ID_MAG   0x3
33 #define SRC_STR_MAG  "http://tizen.org/sensor/general/magnetic"
34
35 static sensor_info2_t sensor_info = {
36         id: 0x1,
37         type: GEOMAGNETIC_RV_SENSOR,
38         uri: NAME_SENSOR,
39         vendor: NAME_VENDOR,
40         min_range: 0,
41         max_range: 1,
42         resolution: 1,
43         min_interval: 10,
44         max_batch_count: 0,
45         wakeup_supported: false,
46         privilege:"",
47 };
48
49 static required_sensor_s required_sensors[] = {
50         {SRC_ID_ACC,     SRC_STR_ACC},
51         {SRC_ID_MAG,     SRC_STR_MAG},
52 };
53
54 magnetic_rv_sensor::magnetic_rv_sensor()
55 : m_x(-1)
56 , m_y(-1)
57 , m_z(-1)
58 , m_w(-1)
59 , m_time(0)
60 , m_interval(100)
61 , m_accuracy(SENSOR_ACCURACY_UNDEFINED)
62 {
63 }
64
65 magnetic_rv_sensor::~magnetic_rv_sensor()
66 {
67 }
68
69 int magnetic_rv_sensor::get_sensor_info(const sensor_info2_t **info)
70 {
71         *info = &sensor_info;
72         return OP_SUCCESS;
73 }
74
75 int magnetic_rv_sensor::get_required_sensors(const required_sensor_s **sensors)
76 {
77         *sensors = required_sensors;
78         return 2;
79 }
80
81 int magnetic_rv_sensor::update(uint32_t id, sensor_data_t *data, int len)
82 {
83         unsigned long long timestamp;
84
85         if (id == SRC_ID_ACC)
86                 m_fusion.push_accel(*data);
87         else if (id == SRC_ID_MAG)
88                 m_fusion.push_mag(*data);
89
90         if (!m_fusion.get_rv(timestamp, m_w, m_x, m_y, m_z))
91                 return OP_ERROR;
92
93         if (timestamp == m_time)
94                 return OP_ERROR;
95
96         m_time = timestamp;
97         m_accuracy = data->accuracy;
98
99         _D("[rotation_vector] : [%10f] [%10f] [%10f] [%10f]", m_x, m_y, m_z, m_w);
100         return OP_SUCCESS;
101 }
102
103 int magnetic_rv_sensor::get_data(sensor_data_t **data, int *length)
104 {
105         sensor_data_t *sensor_data;
106         sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t));
107
108         sensor_data->accuracy = m_accuracy;
109         sensor_data->timestamp = m_time;
110         sensor_data->value_count = 4;
111         sensor_data->values[0] = m_w;
112         sensor_data->values[1] = m_x;
113         sensor_data->values[2] = m_y;
114         sensor_data->values[3] = m_z;
115
116         *data = sensor_data;
117         *length = sizeof(sensor_data_t);
118
119         return 0;
120 }