4 * Copyright (c) 2016 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 <orientation_sensor.h>
35 #include <sensor_loader.h>
36 #include <fusion_util.h>
38 #define SENSOR_NAME "SENSOR_ORIENTATION"
40 orientation_sensor::orientation_sensor()
41 : m_rotation_vector_sensor(NULL)
50 orientation_sensor::~orientation_sensor()
52 _I("%s is destroyed!", SENSOR_NAME);
55 bool orientation_sensor::init()
57 m_rotation_vector_sensor = sensor_loader::get_instance().get_sensor(ROTATION_VECTOR_SENSOR);
59 if (!m_rotation_vector_sensor) {
60 _E("cannot load sensor[%s]", SENSOR_NAME);
63 _I("%s is created!", SENSOR_NAME);
67 sensor_type_t orientation_sensor::get_type(void)
69 return ORIENTATION_SENSOR;
72 unsigned int orientation_sensor::get_event_type(void)
74 return ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME;
77 const char* orientation_sensor::get_name(void)
82 bool orientation_sensor::get_sensor_info(sensor_info &info)
84 info.set_type(get_type());
85 info.set_id(get_id());
86 info.set_privilege(SENSOR_PRIVILEGE_PUBLIC);
87 info.set_name(get_name());
88 info.set_vendor("Samsung Electronics");
89 info.set_min_range(-180);
90 info.set_max_range(360);
91 info.set_resolution(0.01);
92 info.set_min_interval(1);
93 info.set_fifo_count(0);
94 info.set_max_batch_count(0);
95 info.set_supported_event(get_event_type());
96 info.set_wakeup_supported(false);
101 void orientation_sensor::synthesize(const sensor_event_t& event)
104 sensor_event_t *orientation_event;
105 float azimuth, pitch, roll;
107 if (CONVERT_ID_TYPE(event.sensor_id) != ROTATION_VECTOR_SENSOR)
110 error = quat_to_orientation(event.data->values, azimuth, pitch, roll);
113 orientation_event = (sensor_event_t *)malloc(sizeof(sensor_event_t));
114 if (!orientation_event) {
115 _E("Failed to allocate memory");
118 orientation_event->data = (sensor_data_t *)malloc(sizeof(sensor_data_t));
119 if (!orientation_event->data) {
120 _E("Failed to allocate memory");
121 free(orientation_event);
125 orientation_event->sensor_id = get_id();
126 orientation_event->event_type = CONVERT_TYPE_EVENT(ORIENTATION_SENSOR);
127 orientation_event->data_length = sizeof(sensor_data_t);
128 orientation_event->data->accuracy = event.data->accuracy;
129 orientation_event->data->timestamp = event.data->timestamp;
130 orientation_event->data->value_count = 3;
131 orientation_event->data->values[0] = azimuth;
132 orientation_event->data->values[1] = pitch;
133 orientation_event->data->values[2] = roll;
134 push(orientation_event);
139 m_time = event.data->timestamp;
140 m_accuracy = event.data->accuracy;
142 _D("[orientation] : [%10f] [%10f] [%10f]", m_azimuth, m_pitch, m_roll);
145 int orientation_sensor::get_data(sensor_data_t **data, int *length)
147 /* if It is batch sensor, remains can be 2+ */
150 sensor_data_t *sensor_data;
151 sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t));
153 sensor_data->accuracy = m_accuracy;
154 sensor_data->timestamp = m_time;
155 sensor_data->value_count = 3;
156 sensor_data->values[0] = m_azimuth;
157 sensor_data->values[1] = m_pitch;
158 sensor_data->values[2] = m_roll;
161 *length = sizeof(sensor_data_t);
166 bool orientation_sensor::set_interval(unsigned long interval)
168 m_interval = interval;
172 bool orientation_sensor::set_batch_latency(unsigned long latency)
177 bool orientation_sensor::on_start(void)
179 if (m_rotation_vector_sensor)
180 m_rotation_vector_sensor->start();
187 bool orientation_sensor::on_stop(void)
189 if (m_rotation_vector_sensor)
190 m_rotation_vector_sensor->stop();
197 bool orientation_sensor::add_interval(int client_id, unsigned int interval, bool is_processor)
199 if (m_rotation_vector_sensor)
200 m_rotation_vector_sensor->add_interval(client_id, interval, true);
202 return sensor_base::add_interval(client_id, interval, is_processor);
205 bool orientation_sensor::delete_interval(int client_id, bool is_processor)
207 if (m_rotation_vector_sensor)
208 m_rotation_vector_sensor->delete_interval(client_id, true);
210 return sensor_base::delete_interval(client_id, is_processor);