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 <sensor_logs.h>
30 #include <sf_common.h>
32 #include <virtual_sensor.h>
33 #include <auto_rotation_sensor.h>
34 #include <sensor_loader.h>
35 #include <virtual_sensor_config.h>
36 #include <auto_rotation_alg.h>
37 #include <auto_rotation_alg_emul.h>
44 #define SENSOR_NAME "AUTO_ROTATION_SENSOR"
45 #define SENSOR_TYPE_AUTO_ROTATION "AUTO_ROTATION"
49 #define ELEMENT_NAME "NAME"
50 #define ELEMENT_VENDOR "VENDOR"
51 #define ELEMENT_RAW_DATA_UNIT "RAW_DATA_UNIT"
52 #define ELEMENT_DEFAULT_SAMPLING_TIME "DEFAULT_SAMPLING_TIME"
54 #define AUTO_ROTATION_LIB "/usr/lib/sensord/libauto_rotation_sensor.so"
56 auto_rotation_sensor::auto_rotation_sensor()
57 : m_accel_sensor(NULL)
61 , m_rotation_time(1) // rotation state is valid from initial state, so set rotation time to non-zero value
62 , m_default_sampling_time(1)
64 virtual_sensor_config &config = virtual_sensor_config::get_instance();
66 if (!config.get(SENSOR_TYPE_AUTO_ROTATION, ELEMENT_VENDOR, m_vendor)) {
67 ERR("[VENDOR] is empty\n");
71 INFO("m_vendor = %s", m_vendor.c_str());
73 if (!config.get(SENSOR_TYPE_AUTO_ROTATION, ELEMENT_RAW_DATA_UNIT, m_raw_data_unit)) {
74 ERR("[RAW_DATA_UNIT] is empty\n");
78 INFO("m_raw_data_unit = %s", m_raw_data_unit.c_str());
80 if (!config.get(SENSOR_TYPE_AUTO_ROTATION, ELEMENT_DEFAULT_SAMPLING_TIME, &m_default_sampling_time)) {
81 ERR("[DEFAULT_SAMPLING_TIME] is empty\n");
85 INFO("m_default_sampling_time = %d", m_default_sampling_time);
87 m_interval = m_default_sampling_time * MS_TO_US;
90 auto_rotation_sensor::~auto_rotation_sensor()
94 INFO("auto_rotation_sensor is destroyed!\n");
97 bool auto_rotation_sensor::init(void)
99 m_accel_sensor = sensor_loader::get_instance().get_sensor(ACCELEROMETER_SENSOR);
101 if (!m_accel_sensor) {
102 ERR("cannot load accel sensor_hal from %s", get_name());
109 ERR("Not supported AUTO ROTATION sensor");
116 INFO("%s is created!\n", get_name());
121 sensor_type_t auto_rotation_sensor::get_type(void)
123 return AUTO_ROTATION_SENSOR;
126 unsigned int auto_rotation_sensor::get_event_type(void)
128 return (AUTO_ROTATION_SENSOR << 16) | 0x0001;
131 const char* auto_rotation_sensor::get_name(void)
136 bool auto_rotation_sensor::get_sensor_info(sensor_info &info)
138 info.set_type(get_type());
139 info.set_id(get_id());
140 info.set_privilege(SENSOR_PRIVILEGE_PUBLIC); // FIXME
141 info.set_name("Auto Rotation Sensor");
142 info.set_vendor("Samsung Electronics");
143 info.set_min_range(AUTO_ROTATION_DEGREE_UNKNOWN);
144 info.set_max_range(AUTO_ROTATION_DEGREE_270);
145 info.set_resolution(1);
146 info.set_min_interval(1);
147 info.set_fifo_count(0);
148 info.set_max_batch_count(0);
149 info.set_supported_event(get_event_type());
150 info.set_wakeup_supported(false);
155 void auto_rotation_sensor::synthesize(const sensor_event_t& event)
157 if (event.event_type != ACCELEROMETER_RAW_DATA_EVENT)
162 acc[0] = event.data->values[0];
163 acc[1] = event.data->values[1];
164 acc[2] = event.data->values[2];
166 if (!m_alg->get_rotation(acc, event.data->timestamp, m_rotation, rotation))
169 m_rotation = rotation;
170 m_rotation_time = event.data->timestamp;
172 sensor_event_t *rotation_event;
173 sensor_data_t *rotation_data;
177 rotation_event = (sensor_event_t *)malloc(sizeof(sensor_event_t));
178 remains = get_data(&rotation_data, &data_length);
183 rotation_event->sensor_id = get_id();
184 rotation_event->event_type = AUTO_ROTATION_CHANGE_STATE_EVENT;
185 rotation_event->data_length = data_length;
186 rotation_event->data = rotation_data;
188 push(rotation_event);
190 DBG("Rotation: %d, ACC[0]: %f, ACC[1]: %f, ACC[2]: %f", rotation, event.data.values[0], event.data.values[1], event.data.values[2]);
194 int auto_rotation_sensor::get_data(sensor_data_t **data, int *length)
196 /* if It is batch sensor, remains can be 2+ */
199 sensor_data_t *sensor_data;
200 sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t));
202 sensor_data->accuracy = SENSOR_ACCURACY_GOOD;
203 sensor_data->timestamp = m_rotation_time;
204 sensor_data->values[0] = m_rotation;
205 sensor_data->value_count = 1;
208 *length = sizeof(sensor_data_t);
213 bool auto_rotation_sensor::set_interval(unsigned long interval)
218 bool auto_rotation_sensor::set_batch_latency(unsigned long latency)
223 bool auto_rotation_sensor::set_wakeup(int wakeup)
228 bool auto_rotation_sensor::on_start(void)
230 m_rotation = AUTO_ROTATION_DEGREE_UNKNOWN;
234 m_accel_sensor->add_interval((intptr_t)this , (m_interval/MS_TO_US), true);
235 m_accel_sensor->start();
240 bool auto_rotation_sensor::on_stop(void)
242 m_accel_sensor->delete_interval((intptr_t)this , true);
243 m_accel_sensor->stop();
248 bool auto_rotation_sensor::check_lib(void)
250 if (access(AUTO_ROTATION_LIB, F_OK) < 0)
256 auto_rotation_alg *auto_rotation_sensor::get_alg()
258 auto_rotation_alg *alg = new(std::nothrow) auto_rotation_alg_emul();
259 retvm_if(!alg, NULL, "Failed to allocate memory");