sensord: clean up sensor fw code
[platform/core/system/sensord.git] / src / server / plugins / 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_logs.h>
30 #include <sf_common.h>
31
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>
38
39 using std::bind1st;
40 using std::mem_fun;
41 using std::string;
42 using std::vector;
43
44 #define SENSOR_NAME                                             "AUTO_ROTATION_SENSOR"
45 #define SENSOR_TYPE_AUTO_ROTATION               "AUTO_ROTATION"
46
47 #define MS_TO_US 1000
48
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"
53
54 #define AUTO_ROTATION_LIB                               "/usr/lib/sensord/libauto_rotation_sensor.so"
55
56 auto_rotation_sensor::auto_rotation_sensor()
57 : m_accel_sensor(NULL)
58 , m_alg(NULL)
59 , m_rotation(0)
60 , m_interval(1)
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)
63 {
64         virtual_sensor_config &config = virtual_sensor_config::get_instance();
65
66         if (!config.get(SENSOR_TYPE_AUTO_ROTATION, ELEMENT_VENDOR, m_vendor)) {
67                 ERR("[VENDOR] is empty\n");
68                 throw ENXIO;
69         }
70
71         INFO("m_vendor = %s", m_vendor.c_str());
72
73         if (!config.get(SENSOR_TYPE_AUTO_ROTATION, ELEMENT_RAW_DATA_UNIT, m_raw_data_unit)) {
74                 ERR("[RAW_DATA_UNIT] is empty\n");
75                 throw ENXIO;
76         }
77
78         INFO("m_raw_data_unit = %s", m_raw_data_unit.c_str());
79
80         if (!config.get(SENSOR_TYPE_AUTO_ROTATION, ELEMENT_DEFAULT_SAMPLING_TIME, &m_default_sampling_time)) {
81                 ERR("[DEFAULT_SAMPLING_TIME] is empty\n");
82                 throw ENXIO;
83         }
84
85         INFO("m_default_sampling_time = %d", m_default_sampling_time);
86
87         m_interval = m_default_sampling_time * MS_TO_US;
88 }
89
90 auto_rotation_sensor::~auto_rotation_sensor()
91 {
92         delete m_alg;
93
94         INFO("auto_rotation_sensor is destroyed!\n");
95 }
96
97 bool auto_rotation_sensor::init(void)
98 {
99         m_accel_sensor = sensor_loader::get_instance().get_sensor(ACCELEROMETER_SENSOR);
100
101         if (!m_accel_sensor) {
102                 ERR("cannot load accel sensor_hal from %s", get_name());
103                 return false;
104         }
105
106         m_alg = get_alg();
107
108         if (!m_alg) {
109                 ERR("Not supported AUTO ROTATION sensor");
110                 return false;
111         }
112
113         if (!m_alg->open())
114                 return false;
115
116         INFO("%s is created!\n", get_name());
117
118         return true;
119 }
120
121 sensor_type_t auto_rotation_sensor::get_type(void)
122 {
123         return AUTO_ROTATION_SENSOR;
124 }
125
126 unsigned int auto_rotation_sensor::get_event_type(void)
127 {
128         return (AUTO_ROTATION_SENSOR << 16) | 0x0001;
129 }
130
131 const char* auto_rotation_sensor::get_name(void)
132 {
133         return SENSOR_NAME;
134 }
135
136 bool auto_rotation_sensor::get_sensor_info(sensor_info &info)
137 {
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);
151
152         return true;
153 }
154
155 void auto_rotation_sensor::synthesize(const sensor_event_t& event)
156 {
157         if (event.event_type != ACCELEROMETER_RAW_DATA_EVENT)
158                 return;
159
160         int rotation;
161         float acc[3];
162         acc[0] = event.data->values[0];
163         acc[1] = event.data->values[1];
164         acc[2] = event.data->values[2];
165
166         if (!m_alg->get_rotation(acc, event.data->timestamp, m_rotation, rotation))
167                 return;
168
169         m_rotation = rotation;
170         m_rotation_time = event.data->timestamp;
171
172         sensor_event_t *rotation_event;
173         sensor_data_t *rotation_data;
174         int data_length;
175         int remains;
176
177         rotation_event = (sensor_event_t *)malloc(sizeof(sensor_event_t));
178         remains = get_data(&rotation_data, &data_length);
179
180         if (remains < 0)
181                 return;
182
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;
187
188         push(rotation_event);
189
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]);
191         return;
192 }
193
194 int auto_rotation_sensor::get_data(sensor_data_t **data, int *length)
195 {
196         /* if It is batch sensor, remains can be 2+ */
197         int remains = 1;
198
199         sensor_data_t *sensor_data;
200         sensor_data = (sensor_data_t *)malloc(sizeof(sensor_data_t));
201
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;
206
207         *data = sensor_data;
208         *length = sizeof(sensor_data_t);
209
210         return --remains;
211 }
212
213 bool auto_rotation_sensor::set_interval(unsigned long interval)
214 {
215         return false;
216 }
217
218 bool auto_rotation_sensor::set_batch_latency(unsigned long latency)
219 {
220         return false;
221 }
222
223 bool auto_rotation_sensor::set_wakeup(int wakeup)
224 {
225         return false;
226 }
227
228 bool auto_rotation_sensor::on_start(void)
229 {
230         m_rotation = AUTO_ROTATION_DEGREE_UNKNOWN;
231
232         m_alg->start();
233
234         m_accel_sensor->add_interval((intptr_t)this , (m_interval/MS_TO_US), true);
235         m_accel_sensor->start();
236
237         return activate();
238 }
239
240 bool auto_rotation_sensor::on_stop(void)
241 {
242         m_accel_sensor->delete_interval((intptr_t)this , true);
243         m_accel_sensor->stop();
244
245         return deactivate();
246 }
247
248 bool auto_rotation_sensor::check_lib(void)
249 {
250         if (access(AUTO_ROTATION_LIB, F_OK) < 0)
251                 return false;
252
253         return true;
254 }
255
256 auto_rotation_alg *auto_rotation_sensor::get_alg()
257 {
258         auto_rotation_alg *alg = new(std::nothrow) auto_rotation_alg_emul();
259         retvm_if(!alg, NULL, "Failed to allocate memory");
260
261         return alg;
262 }
263