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_log.h>
30 #include <sensor_types.h>
32 #include <sensor_common.h>
33 #include <virtual_sensor.h>
34 #include <auto_rotation_sensor.h>
35 #include <sensor_loader.h>
36 #include <auto_rotation_alg.h>
37 #include <auto_rotation_alg_emul.h>
39 #define SENSOR_NAME "SENSOR_AUTO_ROTATION"
41 auto_rotation_sensor::auto_rotation_sensor()
42 : m_accel_sensor(NULL)
50 auto_rotation_sensor::~auto_rotation_sensor()
54 _I("auto_rotation_sensor is destroyed!\n");
57 bool auto_rotation_sensor::init(void)
59 m_accel_sensor = sensor_loader::get_instance().get_sensor(ACCELEROMETER_SENSOR);
61 if (!m_accel_sensor) {
62 _E("cannot load accel sensor_hal from %s", get_name());
69 _E("Not supported AUTO ROTATION sensor");
76 _I("%s is created!\n", get_name());
81 sensor_type_t auto_rotation_sensor::get_type(void)
83 return AUTO_ROTATION_SENSOR;
86 unsigned int auto_rotation_sensor::get_event_type(void)
88 return AUTO_ROTATION_EVENT_CHANGE_STATE;
91 const char* auto_rotation_sensor::get_name(void)
96 bool auto_rotation_sensor::get_sensor_info(sensor_info &info)
98 info.set_type(get_type());
99 info.set_id(get_id());
100 info.set_privilege(SENSOR_PRIVILEGE_PUBLIC); // FIXME
101 info.set_name("Auto Rotation Sensor");
102 info.set_vendor("Samsung Electronics");
103 info.set_min_range(AUTO_ROTATION_DEGREE_UNKNOWN);
104 info.set_max_range(AUTO_ROTATION_DEGREE_270);
105 info.set_resolution(1);
106 info.set_min_interval(1);
107 info.set_fifo_count(0);
108 info.set_max_batch_count(0);
109 info.set_supported_event(get_event_type());
110 info.set_wakeup_supported(false);
115 void auto_rotation_sensor::synthesize(const sensor_event_t& event)
117 if (event.event_type != ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME)
122 acc[0] = event.data->values[0];
123 acc[1] = event.data->values[1];
124 acc[2] = event.data->values[2];
126 if (!m_alg->get_rotation(acc, event.data->timestamp, m_rotation, rotation))
129 m_rotation = rotation;
130 m_rotation_time = event.data->timestamp;
132 sensor_event_t *rotation_event;
133 sensor_data_t *rotation_data;
137 rotation_event = (sensor_event_t *)malloc(sizeof(sensor_event_t));
138 if (!rotation_event) {
139 _E("Failed to allocate memory");
143 remains = get_data(&rotation_data, &data_length);
146 free(rotation_event);
150 rotation_event->sensor_id = get_id();
151 rotation_event->event_type = AUTO_ROTATION_EVENT_CHANGE_STATE;
152 rotation_event->data_length = data_length;
153 rotation_event->data = rotation_data;
155 push(rotation_event);
157 _D("Rotation: %d, ACC[0]: %f, ACC[1]: %f, ACC[2]: %f", rotation, event.data->values[0], event.data->values[1], event.data->values[2]);
160 int auto_rotation_sensor::get_data(sensor_data_t **data, int *length)
162 /* if It is batch sensor, remains can be 2+ */
165 sensor_data_t *sensor_data;
166 sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t));
168 sensor_data->accuracy = SENSOR_ACCURACY_GOOD;
169 sensor_data->timestamp = m_rotation_time;
170 sensor_data->values[0] = m_rotation;
171 sensor_data->value_count = 1;
174 *length = sizeof(sensor_data_t);
179 bool auto_rotation_sensor::set_interval(unsigned long interval)
181 m_accel_sensor->add_interval((intptr_t)this , interval, true);
183 m_interval = interval;
187 bool auto_rotation_sensor::set_batch_latency(unsigned long latency)
192 bool auto_rotation_sensor::set_wakeup(int wakeup)
197 bool auto_rotation_sensor::on_start(void)
199 m_rotation = AUTO_ROTATION_DEGREE_UNKNOWN;
203 m_accel_sensor->add_interval((intptr_t)this , m_interval, true);
204 m_accel_sensor->start();
209 bool auto_rotation_sensor::on_stop(void)
211 m_accel_sensor->delete_interval((intptr_t)this , true);
212 m_accel_sensor->stop();
217 auto_rotation_alg *auto_rotation_sensor::get_alg()
219 auto_rotation_alg *alg = new(std::nothrow) auto_rotation_alg_emul();
220 retvm_if(!alg, NULL, "Failed to allocate memory");