4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <sys/types.h>
29 #include <sf_common.h>
30 #include <virtual_sensor.h>
31 #include <linear_accel_sensor.h>
32 #include <sensor_plugin_loader.h>
34 #define SENSOR_NAME "LINEAR_ACCEL_SENSOR"
36 #define INITIAL_VALUE -1
37 #define INITIAL_TIME 0
38 #define GRAVITY 9.80665
40 linear_accel_sensor::linear_accel_sensor()
41 : m_gravity_sensor(NULL)
45 , m_time(INITIAL_TIME)
47 m_name = string(SENSOR_NAME);
48 register_supported_event(LINEAR_ACCEL_EVENT_RAW_DATA_REPORT_ON_TIME);
51 linear_accel_sensor::~linear_accel_sensor()
53 INFO("linear_accel_sensor is destroyed!");
56 bool linear_accel_sensor::init()
58 m_gravity_sensor = sensor_plugin_loader::get_instance().get_sensor(GRAVITY_SENSOR);
60 if (!m_gravity_sensor) {
61 ERR("cannot load gravity sensor_hal[%s]", sensor_base::get_name());
65 INFO("%s is created!", sensor_base::get_name());
69 sensor_type_t linear_accel_sensor::get_type(void)
71 return LINEAR_ACCEL_SENSOR;
74 bool linear_accel_sensor::on_start(void)
77 m_gravity_sensor->add_client(GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME);
78 m_gravity_sensor->start();
83 bool linear_accel_sensor::on_stop(void)
86 m_gravity_sensor->delete_client(GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME);
87 m_gravity_sensor->stop();
92 bool linear_accel_sensor::add_interval(int client_id, unsigned int interval, bool is_processor)
94 m_gravity_sensor->add_interval((int)this , interval, true);
95 return sensor_base::add_interval(client_id, interval, is_processor);
98 bool linear_accel_sensor::delete_interval(int client_id, bool is_processor)
100 m_gravity_sensor->delete_interval((int)this , true);
101 return sensor_base::delete_interval(client_id, is_processor);
104 void linear_accel_sensor::synthesize(const sensor_event_t &event, vector<sensor_event_t> &outs)
106 vector<sensor_event_t> gravity_event;
107 ((virtual_sensor *)m_gravity_sensor)->synthesize(event, gravity_event);
109 if (!gravity_event.empty() && event.event_type == ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME) {
110 AUTOLOCK(m_value_mutex);
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;
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]);
126 int linear_accel_sensor::get_sensor_data(const unsigned int data_id, sensor_data_t &data)
128 if (data_id != LINEAR_ACCEL_BASE_DATA_SET)
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;
142 bool linear_accel_sensor::get_properties(const unsigned int type, sensor_properties_t &properties)
144 m_gravity_sensor->get_properties(type, properties);
146 if (type != LINEAR_ACCEL_EVENT_RAW_DATA_REPORT_ON_TIME)
149 strncpy(properties.sensor_name, "Linear Accelerometer Sensor", MAX_KEY_LENGTH);
153 extern "C" void *create(void)
155 linear_accel_sensor *inst;
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));
167 extern "C" void destroy(void *inst)
169 delete (linear_accel_sensor *)inst;