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