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>
30 #include <sf_common.h>
32 #include <virtual_sensor.h>
33 #include <auto_rotation_sensor.h>
34 #include <sensor_plugin_loader.h>
35 #include <auto_rotation_alg.h>
36 #include <auto_rotation_alg_emul.h>
41 #define SENSOR_NAME "AUTO_ROTATION_SENSOR"
42 #define AUTO_ROTATION_LIB "/usr/lib/sensord/libauto-rotation.so"
44 auto_rotation_sensor::auto_rotation_sensor()
45 : m_accel_sensor(NULL)
47 , m_rotation_time(1) // rotation state is valid from initial state, so set rotation time to non-zero value
50 m_name = string(SENSOR_NAME);
52 register_supported_event(AUTO_ROTATION_EVENT_CHANGE_STATE);
55 auto_rotation_sensor::~auto_rotation_sensor()
59 INFO("auto_rotation_sensor is destroyed!\n");
62 bool auto_rotation_sensor::check_lib(void)
64 if (access(AUTO_ROTATION_LIB, F_OK) < 0)
70 auto_rotation_alg *auto_rotation_sensor::get_alg()
72 return new auto_rotation_alg_emul();
75 bool auto_rotation_sensor::init()
77 m_accel_sensor = sensor_plugin_loader::get_instance().get_sensor(ACCELEROMETER_SENSOR);
79 if (!m_accel_sensor) {
80 ERR("cannot load accel sensor_hal[%s]", sensor_base::get_name());
87 ERR("Not supported AUTO ROTATION sensor");
94 set_privilege(SENSOR_PRIVILEGE_INTERNAL);
96 INFO("%s is created!\n", sensor_base::get_name());
101 sensor_type_t auto_rotation_sensor::get_type(void)
103 return AUTO_ROTATION_SENSOR;
106 bool auto_rotation_sensor::on_start(void)
108 const int SAMPLING_TIME = 60;
109 m_rotation = AUTO_ROTATION_DEGREE_UNKNOWN;
113 m_accel_sensor->add_client(ACCELEROMETER_RAW_DATA_EVENT);
114 m_accel_sensor->add_interval((int)this , SAMPLING_TIME, true);
115 m_accel_sensor->start();
120 bool auto_rotation_sensor::on_stop(void)
122 m_accel_sensor->delete_client(ACCELEROMETER_RAW_DATA_EVENT);
123 m_accel_sensor->delete_interval((int)this , true);
124 m_accel_sensor->stop();
129 void auto_rotation_sensor::synthesize(const sensor_event_t& event, vector<sensor_event_t> &outs)
133 if (event.event_type == ACCELEROMETER_RAW_DATA_EVENT) {
136 acc[0] = event.data.values[0];
137 acc[1] = event.data.values[1];
138 acc[2] = event.data.values[2];
140 if (!m_alg->get_rotation(acc, event.data.timestamp, m_rotation, rotation))
143 AUTOLOCK(m_value_mutex);
144 sensor_event_t rotation_event;
146 INFO("Rotation: %d, ACC[0]: %f, ACC[1]: %f, ACC[2]: %f", rotation, event.data.values[0], event.data.values[1], event.data.values[2]);
147 rotation_event.sensor_id = get_id();
148 rotation_event.data.accuracy = SENSOR_ACCURACY_GOOD;
149 rotation_event.event_type = AUTO_ROTATION_EVENT_CHANGE_STATE;
150 rotation_event.data.timestamp = event.data.timestamp;
151 rotation_event.data.values[0] = rotation;
152 rotation_event.data.value_count = 1;
153 outs.push_back(rotation_event);
154 m_rotation = rotation;
155 m_rotation_time = event.data.timestamp;
162 int auto_rotation_sensor::get_sensor_data(unsigned int data_id, sensor_data_t &data)
164 if (data_id != AUTO_ROTATION_BASE_DATA_SET)
167 AUTOLOCK(m_value_mutex);
169 data.accuracy = SENSOR_ACCURACY_GOOD;
170 data.timestamp = m_rotation_time;
171 data.values[0] = m_rotation;
172 data.value_count = 1;
177 bool auto_rotation_sensor::get_properties(sensor_properties_t &properties)
179 properties.name = "Auto Rotation Sensor";
180 properties.vendor = "Samsung Electronics";
181 properties.min_range = AUTO_ROTATION_DEGREE_UNKNOWN;
182 properties.max_range = AUTO_ROTATION_DEGREE_270;
183 properties.min_interval = 1;
184 properties.resolution = 1;
185 properties.fifo_count = 0;
186 properties.max_batch_count = 0;
191 extern "C" sensor_module* create(void)
193 auto_rotation_sensor *sensor;
196 sensor = new(std::nothrow) auto_rotation_sensor;
198 ERR("Failed to create module, err: %d, cause: %s", err, strerror(err));
202 sensor_module *module = new(std::nothrow) sensor_module;
203 retvm_if(!module || !sensor, NULL, "Failed to allocate memory");
205 module->sensors.push_back(sensor);