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