sensord: auto_rotation: fix interval setting in auto_rotation
[platform/core/system/sensord.git] / src / sensor / auto_rotation / auto_rotation_sensor.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <math.h>
25 #include <time.h>
26 #include <sys/types.h>
27 #include <dlfcn.h>
28
29 #include <sensor_log.h>
30 #include <sensor_types.h>
31
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>
38
39 #define SENSOR_NAME "SENSOR_AUTO_ROTATION"
40
41 auto_rotation_sensor::auto_rotation_sensor()
42 : m_accel_sensor(NULL)
43 , m_alg(NULL)
44 , m_rotation(0)
45 , m_interval(100)
46 , m_rotation_time(0)
47 {
48 }
49
50 auto_rotation_sensor::~auto_rotation_sensor()
51 {
52         delete m_alg;
53
54         _I("auto_rotation_sensor is destroyed!\n");
55 }
56
57 bool auto_rotation_sensor::init(void)
58 {
59         m_accel_sensor = sensor_loader::get_instance().get_sensor(ACCELEROMETER_SENSOR);
60
61         if (!m_accel_sensor) {
62                 _E("cannot load accel sensor_hal from %s", get_name());
63                 return false;
64         }
65
66         m_alg = get_alg();
67
68         if (!m_alg) {
69                 _E("Not supported AUTO ROTATION sensor");
70                 return false;
71         }
72
73         if (!m_alg->open())
74                 return false;
75
76         _I("%s is created!\n", get_name());
77
78         return true;
79 }
80
81 sensor_type_t auto_rotation_sensor::get_type(void)
82 {
83         return AUTO_ROTATION_SENSOR;
84 }
85
86 unsigned int auto_rotation_sensor::get_event_type(void)
87 {
88         return AUTO_ROTATION_EVENT_CHANGE_STATE;
89 }
90
91 const char* auto_rotation_sensor::get_name(void)
92 {
93         return SENSOR_NAME;
94 }
95
96 bool auto_rotation_sensor::get_sensor_info(sensor_info &info)
97 {
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);
111
112         return true;
113 }
114
115 void auto_rotation_sensor::synthesize(const sensor_event_t& event)
116 {
117         if (event.event_type != ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME)
118                 return;
119
120         int rotation;
121         float acc[3];
122         acc[0] = event.data->values[0];
123         acc[1] = event.data->values[1];
124         acc[2] = event.data->values[2];
125
126         if (!m_alg->get_rotation(acc, event.data->timestamp, m_rotation, rotation))
127                 return;
128
129         m_rotation = rotation;
130         m_rotation_time = event.data->timestamp;
131
132         sensor_event_t *rotation_event;
133         sensor_data_t *rotation_data;
134         int data_length;
135         int remains;
136
137         rotation_event = (sensor_event_t *)malloc(sizeof(sensor_event_t));
138         if (!rotation_event) {
139                 _E("Failed to allocate memory");
140                 return;
141         }
142
143         remains = get_data(&rotation_data, &data_length);
144
145         if (remains < 0) {
146                 free(rotation_event);
147                 return;
148         }
149
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;
154
155         push(rotation_event);
156
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]);
158 }
159
160 int auto_rotation_sensor::get_data(sensor_data_t **data, int *length)
161 {
162         /* if It is batch sensor, remains can be 2+ */
163         int remains = 1;
164
165         sensor_data_t *sensor_data;
166         sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t));
167
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;
172
173         *data = sensor_data;
174         *length = sizeof(sensor_data_t);
175
176         return --remains;
177 }
178
179 bool auto_rotation_sensor::set_interval(unsigned long interval)
180 {
181         m_accel_sensor->add_interval((intptr_t)this , interval, true);
182
183         m_interval = interval;
184         return false;
185 }
186
187 bool auto_rotation_sensor::set_batch_latency(unsigned long latency)
188 {
189         return false;
190 }
191
192 bool auto_rotation_sensor::set_wakeup(int wakeup)
193 {
194         return false;
195 }
196
197 bool auto_rotation_sensor::on_start(void)
198 {
199         m_rotation = AUTO_ROTATION_DEGREE_UNKNOWN;
200
201         m_alg->start();
202
203         m_accel_sensor->add_interval((intptr_t)this , m_interval, true);
204         m_accel_sensor->start();
205
206         return activate();
207 }
208
209 bool auto_rotation_sensor::on_stop(void)
210 {
211         m_accel_sensor->delete_interval((intptr_t)this , true);
212         m_accel_sensor->stop();
213
214         return deactivate();
215 }
216
217 auto_rotation_alg *auto_rotation_sensor::get_alg()
218 {
219         auto_rotation_alg *alg = new(std::nothrow) auto_rotation_alg_emul();
220         retvm_if(!alg, NULL, "Failed to allocate memory");
221
222         return alg;
223 }