Merge "Adding AK8975 geo-sensor info in sensors.xml.in required by geo-plugin" into...
[platform/core/system/sensord.git] / src / gravity / gravity_sensor.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2014 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 <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <math.h>
25 #include <time.h>
26 #include <sys/types.h>
27 #include <dlfcn.h>
28 #include <common.h>
29 #include <sf_common.h>
30 #include <sensor_base.h>
31 #include <gravity_sensor.h>
32 #include <sensor_plugin_loader.h>
33
34 #define INITIAL_VALUE -1
35 #define INITIAL_TIME 0
36 #define GRAVITY 9.80665
37
38 #define SENSOR_NAME "GRAVITY_SENSOR"
39
40 gravity_sensor::gravity_sensor()
41 : m_orientation_sensor(NULL)
42 , m_x(INITIAL_VALUE)
43 , m_y(INITIAL_VALUE)
44 , m_z(INITIAL_VALUE)
45 , m_timestamp(INITIAL_TIME)
46 {
47         m_name = string(SENSOR_NAME);
48
49         register_supported_event(GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME);
50 }
51
52 gravity_sensor::~gravity_sensor()
53 {
54         INFO("gravity_sensor is destroyed!");
55 }
56
57 bool gravity_sensor::init()
58 {
59         m_orientation_sensor = sensor_plugin_loader::get_instance().get_sensor(ORIENTATION_SENSOR);
60
61         if (!m_orientation_sensor) {
62                 ERR("Failed to load orientation sensor: 0x%x", m_orientation_sensor);
63                 return false;
64         }
65
66         INFO("%s is created!", sensor_base::get_name());
67         return true;
68 }
69
70 sensor_type_t gravity_sensor::get_type(void)
71 {
72         return GRAVITY_SENSOR;
73 }
74
75 bool gravity_sensor::on_start(void)
76 {
77         AUTOLOCK(m_mutex);
78
79         m_orientation_sensor->add_client(ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME);
80         m_orientation_sensor->start();
81
82         activate();
83         return true;
84 }
85
86 bool gravity_sensor::on_stop(void)
87 {
88         AUTOLOCK(m_mutex);
89
90         m_orientation_sensor->delete_client(ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME);
91         m_orientation_sensor->stop();
92
93         deactivate();
94         return true;
95 }
96
97 bool gravity_sensor::add_interval(int client_id, unsigned int interval)
98 {
99         AUTOLOCK(m_mutex);
100         m_orientation_sensor->add_interval(client_id , interval, true);
101
102         return sensor_base::add_interval(client_id, interval, true);
103 }
104
105 bool gravity_sensor::delete_interval(int client_id)
106 {
107         AUTOLOCK(m_mutex);
108         m_orientation_sensor->delete_interval(client_id , true);
109
110         return sensor_base::delete_interval(client_id, true);
111 }
112
113 void gravity_sensor::synthesize(const sensor_event_t &event, vector<sensor_event_t> &outs)
114 {
115         sensor_event_t gravity_event;
116         vector<sensor_event_t> orientation_event;
117         ((virtual_sensor *)m_orientation_sensor)->synthesize(event, orientation_event);
118
119         if (!orientation_event.empty()) {
120                 AUTOLOCK(m_mutex);
121
122                 m_timestamp = orientation_event[0].data.timestamp;
123                 gravity_event.data.values[0] = GRAVITY * sin(orientation_event[0].data.values[1]);
124                 gravity_event.data.values[1] = GRAVITY * sin(orientation_event[0].data.values[0]);
125                 gravity_event.data.values[2] = GRAVITY * cos(orientation_event[0].data.values[1] *
126                                                                                 orientation_event[0].data.values[0]);
127                 gravity_event.data.values_num = 3;
128                 gravity_event.data.timestamp = m_timestamp;
129                 gravity_event.data.data_accuracy = SENSOR_ACCURACY_GOOD;
130                 gravity_event.data.data_unit_idx = SENSOR_UNIT_METRE_PER_SECOND_SQUARED;
131
132                 outs.push_back(gravity_event);
133                 return;
134         }
135 }
136
137 int gravity_sensor::get_sensor_data(const unsigned int event_type, sensor_data_t &data)
138 {
139         sensor_data_t orientation_data;
140
141         if (event_type != GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME)
142                 return -1;
143
144         m_orientation_sensor->get_sensor_data(ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME, orientation_data);
145
146         data.data_accuracy = SENSOR_ACCURACY_GOOD;
147         data.data_unit_idx = SENSOR_UNIT_METRE_PER_SECOND_SQUARED;
148         data.timestamp = orientation_data.timestamp;
149         data.values[0] = GRAVITY * sin(orientation_data.values[1]);
150         data.values[1] = GRAVITY * sin(orientation_data.values[0]);
151         data.values[2] = GRAVITY * cos(orientation_data.values[1] * orientation_data.values[0]);
152         data.values_num = 3;
153
154         return 0;
155 }
156
157 bool gravity_sensor::get_properties(const unsigned int type, sensor_properties_t &properties)
158 {
159         properties.sensor_unit_idx = SENSOR_UNIT_DEGREE;
160         properties.sensor_min_range = -GRAVITY;
161         properties.sensor_max_range = GRAVITY;
162         properties.sensor_resolution = 1;
163         strncpy(properties.sensor_vendor, "Samsung", MAX_KEY_LENGTH);
164         strncpy(properties.sensor_name, SENSOR_NAME, MAX_KEY_LENGTH);
165
166         return true;
167 }
168
169 extern "C" void *create(void)
170 {
171         gravity_sensor *inst;
172
173         try {
174                 inst = new gravity_sensor();
175         } catch (int err) {
176                 ERR("Failed to create gravity_sensor class, errno : %d, errstr : %s", err, strerror(err));
177                 return NULL;
178         }
179
180         return (void *)inst;
181 }
182
183 extern "C" void destroy(void *inst)
184 {
185         delete (gravity_sensor *)inst;
186 }