sensord: clean up header dependency
[platform/core/system/sensord.git] / src / server / sensor_base.cpp
1 /*
2  * libsensord-share
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 <stdint.h>
21 #include <algorithm>
22 #include <utility>
23 #include <functional>
24 #include <sensor_hal.h>
25 #include <sensor_event_queue.h>
26 #include <sensor_base.h>
27
28 using std::make_pair;
29 using std::vector;
30
31 sensor_base::sensor_base()
32 : m_unique_id(-1)
33 , m_privilege(SENSOR_PRIVILEGE_PUBLIC)
34 , m_permission(SENSOR_PERMISSION_STANDARD)
35 , m_client(0)
36 , m_started(false)
37 {
38 }
39
40 sensor_base::~sensor_base()
41 {
42 }
43
44 sensor_type_t sensor_base::get_type(void)
45 {
46         return UNKNOWN_SENSOR;
47 }
48
49 bool sensor_base::start()
50 {
51         AUTOLOCK(m_client_mutex);
52
53         ++m_client;
54
55         if (m_client == 1) {
56                 if (!on_start()) {
57                         ERR("[%s] sensor failed to start", get_name());
58                         return false;
59                 }
60
61                 m_started = true;
62         }
63
64         INFO("[%s] sensor started, #client = %d", get_name(), m_client);
65
66         return true;
67 }
68
69 bool sensor_base::stop(void)
70 {
71         AUTOLOCK(m_client_mutex);
72
73         --m_client;
74
75         if (m_client == 0) {
76                 if (!on_stop()) {
77                         ERR("[%s] sensor faild to stop", get_name());
78                         return false;
79                 }
80
81                 m_started = false;
82         }
83
84         INFO("[%s] sensor stopped, #client = %d", get_name(), m_client);
85
86         return true;
87 }
88
89 bool sensor_base::on_start()
90 {
91         return false;
92 }
93
94 bool sensor_base::on_stop()
95 {
96         return false;
97 }
98
99 long sensor_base::set_command(unsigned int cmd, long value)
100 {
101         return -1;
102 }
103
104 bool sensor_base::add_interval(int client_id, unsigned int interval, bool is_processor)
105 {
106         unsigned int prev_min, cur_min;
107
108         AUTOLOCK(m_plugin_info_list_mutex);
109
110         prev_min = m_plugin_info_list.get_min_interval();
111
112         if (!m_plugin_info_list.add_interval(client_id, interval, is_processor))
113                 return false;
114
115         cur_min = m_plugin_info_list.get_min_interval();
116
117         if (cur_min != prev_min) {
118                 INFO("Min interval for sensor[0x%x] is changed from %dms to %dms"
119                         " by%sclient[%d] adding interval",
120                         get_id(), prev_min, cur_min,
121                         is_processor ? " processor " : " ", client_id);
122
123                 set_interval(cur_min);
124         }
125
126         return true;
127 }
128
129 bool sensor_base::delete_interval(int client_id, bool is_processor)
130 {
131         unsigned int prev_min, cur_min;
132         AUTOLOCK(m_plugin_info_list_mutex);
133
134         prev_min = m_plugin_info_list.get_min_interval();
135
136         if (!m_plugin_info_list.delete_interval(client_id, is_processor))
137                 return false;
138
139         cur_min = m_plugin_info_list.get_min_interval();
140
141         if (!cur_min) {
142                 INFO("No interval for sensor[0x%x] by%sclient[%d] deleting interval, "
143                          "so set to default %dms",
144                          get_id(), is_processor ? " processor " : " ",
145                          client_id, POLL_1HZ_MS);
146
147                 set_interval(POLL_1HZ_MS);
148         } else if (cur_min != prev_min) {
149                 INFO("Min interval for sensor[0x%x] is changed from %dms to %dms"
150                         " by%sclient[%d] deleting interval",
151                         get_id(), prev_min, cur_min,
152                         is_processor ? " processor " : " ", client_id);
153
154                 set_interval(cur_min);
155         }
156
157         return true;
158 }
159
160 unsigned int sensor_base::get_interval(int client_id, bool is_processor)
161 {
162         AUTOLOCK(m_plugin_info_list_mutex);
163
164         return m_plugin_info_list.get_interval(client_id, is_processor);
165 }
166
167 bool sensor_base::add_batch(int client_id, unsigned int latency)
168 {
169         unsigned int prev_max, cur_max;
170
171         AUTOLOCK(m_plugin_info_list_mutex);
172
173         prev_max = m_plugin_info_list.get_max_batch();
174
175         if (!m_plugin_info_list.add_batch(client_id, latency))
176                 return false;
177
178         cur_max = m_plugin_info_list.get_max_batch();
179
180         if (cur_max != prev_max) {
181                 INFO("Max latency for sensor[0x%x] is changed from %dms to %dms by client[%d] adding latency",
182                         get_id(), prev_max, cur_max, client_id);
183                 set_batch(cur_max);
184         }
185
186         return true;
187 }
188
189 bool sensor_base::delete_batch(int client_id)
190 {
191         unsigned int prev_max, cur_max;
192         AUTOLOCK(m_plugin_info_list_mutex);
193
194         prev_max = m_plugin_info_list.get_max_batch();
195
196         if (!m_plugin_info_list.delete_batch(client_id))
197                 return false;
198
199         cur_max = m_plugin_info_list.get_max_batch();
200
201         if (!cur_max) {
202                 INFO("No latency for sensor[0x%x] by client[%d] deleting latency, so set to default 0 ms",
203                          get_id(), client_id);
204
205                 set_batch(0);
206         } else if (cur_max != prev_max) {
207                 INFO("Max latency for sensor[0x%x] is changed from %dms to %dms by client[%d] deleting latency",
208                         get_id(), prev_max, cur_max, client_id);
209
210                 set_batch(cur_max);
211         }
212
213         return true;
214 }
215
216 unsigned int sensor_base::get_batch(int client_id)
217 {
218         AUTOLOCK(m_plugin_info_list_mutex);
219
220         return m_plugin_info_list.get_batch(client_id);
221 }
222
223 bool sensor_base::add_wakeup(int client_id, int wakeup)
224 {
225         int prev_wakeup, cur_wakeup;
226
227         AUTOLOCK(m_plugin_info_list_mutex);
228
229         prev_wakeup = m_plugin_info_list.is_wakeup_on();
230
231         if (!m_plugin_info_list.add_wakeup(client_id, wakeup))
232                 return false;
233
234         cur_wakeup = m_plugin_info_list.is_wakeup_on();
235
236         if ((cur_wakeup == SENSOR_WAKEUP_ON) && (prev_wakeup < SENSOR_WAKEUP_ON)) {
237                 INFO("Wakeup for sensor[0x%x] is changed from %d to %d by client[%d] adding wakeup",
238                         get_id(), prev_wakeup, cur_wakeup, client_id);
239                 set_wakeup(SENSOR_WAKEUP_ON);
240         }
241
242         return true;
243 }
244
245 bool sensor_base::delete_wakeup(int client_id)
246 {
247         int prev_wakeup, cur_wakeup;
248         AUTOLOCK(m_plugin_info_list_mutex);
249
250         prev_wakeup = m_plugin_info_list.is_wakeup_on();
251
252         if (!m_plugin_info_list.delete_wakeup(client_id))
253                 return false;
254
255         cur_wakeup = m_plugin_info_list.is_wakeup_on();
256
257         if ((cur_wakeup < SENSOR_WAKEUP_ON) && (prev_wakeup == SENSOR_WAKEUP_ON)) {
258                 INFO("Wakeup for sensor[0x%x] is changed from %d to %d by client[%d] deleting wakeup",
259                         get_id(), prev_wakeup, cur_wakeup, client_id);
260                 set_wakeup(SENSOR_WAKEUP_OFF);
261         }
262
263         return true;
264 }
265
266 int sensor_base::get_wakeup(int client_id)
267 {
268         AUTOLOCK(m_plugin_info_list_mutex);
269
270         return m_plugin_info_list.is_wakeup_on();
271 }
272
273 int sensor_base::get_sensor_data(sensor_data_t &data)
274 {
275         return -1;
276 }
277
278 int sensor_base::get_sensor_event(sensor_event_t **event)
279 {
280         return -1;
281 }
282
283 bool sensor_base::get_properties(sensor_properties_s &properties)
284 {
285         return false;
286 }
287
288 const char* sensor_base::get_name()
289 {
290         return NULL;
291 }
292
293 void sensor_base::set_id(sensor_id_t id)
294 {
295         m_unique_id = id;
296 }
297
298 sensor_id_t sensor_base::get_id(void)
299 {
300         if (m_unique_id == -1)
301                 return UNKNOWN_SENSOR;
302
303         return m_unique_id;
304 }
305
306 unsigned int sensor_base::get_event_type(void)
307 {
308         return -1;
309 }
310
311 sensor_privilege_t sensor_base::get_privilege(void)
312 {
313         return m_privilege;
314 }
315
316 int sensor_base::get_permission(void)
317 {
318         return m_permission;
319 }
320
321 bool sensor_base::is_started(void)
322 {
323         AUTOLOCK(m_client_mutex);
324
325         return m_started;
326 }
327
328 bool sensor_base::is_virtual()
329 {
330         return false;
331 }
332
333 void sensor_base::get_sensor_info(sensor_info &info)
334 {
335         sensor_properties_s properties;
336         properties.wakeup_supported = false;
337
338         get_properties(properties);
339
340         info.set_type(get_type());
341         info.set_id(get_id());
342         info.set_privilege(m_privilege);
343         info.set_name(properties.name.c_str());
344         info.set_vendor(properties.vendor.c_str());
345         info.set_min_range(properties.min_range);
346         info.set_max_range(properties.max_range);
347         info.set_resolution(properties.resolution);
348         info.set_min_interval(properties.min_interval);
349         info.set_fifo_count(properties.fifo_count);
350         info.set_max_batch_count(properties.max_batch_count);
351         info.set_supported_event(get_event_type());
352         info.set_wakeup_supported(properties.wakeup_supported);
353
354         return;
355 }
356
357 bool sensor_base::is_wakeup_supported(void)
358 {
359         return false;
360 }
361
362 bool sensor_base::set_interval(unsigned long interval)
363 {
364         return false;
365 }
366
367 bool sensor_base::set_wakeup(int wakeup)
368 {
369         return false;
370 }
371
372 bool sensor_base::set_batch(unsigned long latency)
373 {
374         return false;
375 }
376
377 void sensor_base::set_privilege(sensor_privilege_t privilege)
378 {
379         m_privilege = privilege;
380 }
381
382 void sensor_base::set_permission(int permission)
383 {
384         m_permission = permission;
385 }
386
387 int sensor_base::send_sensorhub_data(const char* data, int data_len)
388 {
389         return -1;
390 }
391
392 bool sensor_base::push(sensor_event_t *event, int event_length)
393 {
394         AUTOLOCK(m_client_mutex);
395
396         if (m_client <= 0)
397                 return false;
398
399         sensor_event_queue::get_instance().push(event, event_length);
400         return true;
401 }
402
403 unsigned long long sensor_base::get_timestamp(void)
404 {
405         struct timespec t;
406         clock_gettime(CLOCK_MONOTONIC, &t);
407         return ((unsigned long long)(t.tv_sec)*1000000000LL + t.tv_nsec) / 1000;
408 }
409
410 unsigned long long sensor_base::get_timestamp(timeval *t)
411 {
412         if (!t) {
413                 ERR("t is NULL");
414                 return 0;
415         }
416
417         return ((unsigned long long)(t->tv_sec)*1000000LL +t->tv_usec);
418 }
419