sensord: clean up sf_common.h/sensor_common.h/sensor_logs.h
[platform/core/system/sensord.git] / src / client / sensor_client_info.cpp
1 /*
2  * libsensord
3  *
4  * Copyright (c) 2015 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 <sensor_event_listener.h>
21 #include <client_common.h>
22 #include <sensor_info_manager.h>
23
24 #include <thread>
25 #include <chrono>
26
27 #define MS_TO_US 1000
28 #define MIN_DELIVERY_DIFF_FACTOR 0.75f
29
30 using std::thread;
31 using std::pair;
32
33 sensor_client_info::sensor_client_info()
34 : m_client_id(CLIENT_ID_INVALID)
35 {
36 }
37
38 sensor_client_info::~sensor_client_info()
39 {
40 }
41
42
43 sensor_client_info& sensor_client_info::get_instance(void)
44 {
45         static sensor_client_info inst;
46         return inst;
47 }
48
49
50 int sensor_client_info::create_handle(sensor_id_t sensor)
51 {
52         sensor_handle_info handle_info;
53         int handle = 0;
54
55         AUTOLOCK(m_handle_info_lock);
56
57         while (m_sensor_handle_infos.count(handle) > 0)
58                 handle++;
59
60         if (handle == MAX_HANDLE) {
61                 ERR("Handles of client %s are full", get_client_name());
62                 return MAX_HANDLE_REACHED;
63         }
64
65         handle_info.m_sensor_id = sensor;
66         handle_info.m_sensor_state = SENSOR_STATE_STOPPED;
67         handle_info.m_sensor_option = SENSOR_OPTION_DEFAULT;
68         handle_info.m_handle = handle;
69         handle_info.m_accuracy = -1;
70         handle_info.m_accuracy_cb = NULL;
71         handle_info.m_accuracy_user_data = NULL;
72
73         m_sensor_handle_infos.insert(pair<int,sensor_handle_info> (handle, handle_info));
74
75         return handle;
76 }
77
78 bool sensor_client_info::delete_handle(int handle)
79 {
80         AUTOLOCK(m_handle_info_lock);
81
82         auto it_handle = m_sensor_handle_infos.find(handle);
83
84         if (it_handle == m_sensor_handle_infos.end()) {
85                 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
86                 return false;
87         }
88
89         m_sensor_handle_infos.erase(it_handle);
90         return true;
91 }
92
93
94 bool sensor_client_info::is_active()
95 {
96         AUTOLOCK(m_handle_info_lock);
97
98         return !m_sensor_handle_infos.empty();
99 }
100
101 bool sensor_client_info::register_event(int handle, unsigned int event_type,
102                 unsigned int interval, unsigned int latency, int cb_type, void *cb, void* user_data)
103 {
104         AUTOLOCK(m_handle_info_lock);
105
106         auto it_handle = m_sensor_handle_infos.find(handle);
107
108         if (it_handle == m_sensor_handle_infos.end()) {
109                 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
110                 return false;
111         }
112
113         if (!it_handle->second.add_reg_event_info(event_type, interval, latency, cb_type, cb, user_data))
114                 return false;
115
116         return true;
117 }
118
119 bool sensor_client_info::unregister_event(int handle, unsigned int event_type)
120 {
121         AUTOLOCK(m_handle_info_lock);
122
123         auto it_handle = m_sensor_handle_infos.find(handle);
124
125         if (it_handle == m_sensor_handle_infos.end()) {
126                 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
127                 return false;
128         }
129
130         if (!it_handle->second.delete_reg_event_info(event_type))
131                 return false;
132
133         return true;
134 }
135
136 bool sensor_client_info::register_accuracy_cb(int handle, sensor_accuracy_changed_cb_t cb, void* user_data)
137 {
138         AUTOLOCK(m_handle_info_lock);
139
140         auto it_handle = m_sensor_handle_infos.find(handle);
141
142         if (it_handle == m_sensor_handle_infos.end()) {
143                 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
144                 return false;
145         }
146
147         it_handle->second.m_accuracy = -1;
148         it_handle->second.m_accuracy_cb = cb;
149         it_handle->second.m_accuracy_user_data = user_data;
150
151         return true;
152 }
153
154 bool sensor_client_info::unregister_accuracy_cb(int handle)
155 {
156         AUTOLOCK(m_handle_info_lock);
157
158         auto it_handle = m_sensor_handle_infos.find(handle);
159
160         if (it_handle == m_sensor_handle_infos.end()) {
161                 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
162                 return false;
163         }
164
165         it_handle->second.m_accuracy = -1;
166         it_handle->second.m_accuracy_cb = NULL;
167         it_handle->second.m_accuracy_user_data = NULL;
168
169         return true;
170 }
171
172 bool sensor_client_info::set_sensor_params(int handle, int sensor_state, int sensor_option)
173 {
174         AUTOLOCK(m_handle_info_lock);
175
176         auto it_handle = m_sensor_handle_infos.find(handle);
177
178         if (it_handle == m_sensor_handle_infos.end()) {
179                 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
180                 return false;
181         }
182
183         it_handle->second.m_sensor_state = sensor_state;
184         it_handle->second.m_sensor_option = sensor_option;
185
186         return true;
187 }
188
189 bool sensor_client_info::get_sensor_params(int handle, int &sensor_state, int &sensor_option)
190 {
191         AUTOLOCK(m_handle_info_lock);
192
193         auto it_handle = m_sensor_handle_infos.find(handle);
194
195         if (it_handle == m_sensor_handle_infos.end()) {
196                 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
197                 return false;
198         }
199
200         sensor_state = it_handle->second.m_sensor_state;
201         sensor_option = it_handle->second.m_sensor_option;
202
203         return true;
204 }
205
206 bool sensor_client_info::set_sensor_state(int handle, int sensor_state)
207 {
208         AUTOLOCK(m_handle_info_lock);
209
210         auto it_handle = m_sensor_handle_infos.find(handle);
211
212         if (it_handle == m_sensor_handle_infos.end()) {
213                 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
214                 return false;
215         }
216
217         it_handle->second.m_sensor_state = sensor_state;
218
219         return true;
220 }
221
222 bool sensor_client_info::set_sensor_option(int handle, int sensor_option)
223 {
224         AUTOLOCK(m_handle_info_lock);
225
226         auto it_handle = m_sensor_handle_infos.find(handle);
227
228         if (it_handle == m_sensor_handle_infos.end()) {
229                 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
230                 return false;
231         }
232
233         it_handle->second.m_sensor_option = sensor_option;
234
235         return true;
236 }
237
238 bool sensor_client_info::set_event_batch(int handle, unsigned int event_type, unsigned int interval, unsigned int latency)
239 {
240         AUTOLOCK(m_handle_info_lock);
241
242         auto it_handle = m_sensor_handle_infos.find(handle);
243
244         if (it_handle == m_sensor_handle_infos.end()) {
245                 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
246                 return false;
247         }
248
249         if (!it_handle->second.change_reg_event_batch(event_type, interval, latency))
250                 return false;
251
252         return true;
253 }
254
255 bool sensor_client_info::set_event_maincontext(int handle, unsigned int event_type, GMainContext *maincontext)
256 {
257         AUTOLOCK(m_handle_info_lock);
258
259         auto it_handle = m_sensor_handle_infos.find(handle);
260
261         if (it_handle == m_sensor_handle_infos.end()) {
262                 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
263                 return false;
264         }
265
266         if (!it_handle->second.change_reg_event_maincontext(event_type, maincontext))
267                 return false;
268
269         return true;
270 }
271
272 bool sensor_client_info::set_accuracy(int handle, int accuracy)
273 {
274         AUTOLOCK(m_handle_info_lock);
275
276         auto it_handle = m_sensor_handle_infos.find(handle);
277
278         if (it_handle == m_sensor_handle_infos.end()) {
279                 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
280                 return false;
281         }
282
283         it_handle->second.m_accuracy = accuracy;
284
285         return true;
286 }
287
288 bool sensor_client_info::set_bad_accuracy(int handle, int bad_accuracy)
289 {
290         AUTOLOCK(m_handle_info_lock);
291
292         auto it_handle = m_sensor_handle_infos.find(handle);
293
294         if (it_handle == m_sensor_handle_infos.end()) {
295                 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
296                 return false;
297         }
298
299         it_handle->second.m_bad_accuracy = bad_accuracy;
300
301         return true;
302 }
303
304 bool sensor_client_info::get_event_info(int handle, unsigned int event_type, unsigned int &interval, unsigned int &latency, int &cb_type, void* &cb, void* &user_data)
305 {
306         AUTOLOCK(m_handle_info_lock);
307
308         auto it_handle = m_sensor_handle_infos.find(handle);
309
310         if (it_handle == m_sensor_handle_infos.end()) {
311                 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
312                 return false;
313         }
314
315         const reg_event_info *event_info;
316
317         event_info = it_handle->second.get_reg_event_info(event_type);
318
319         if (!event_info)
320                 return NULL;
321
322
323         interval = event_info->m_interval;
324         cb_type = event_info->m_cb_type;
325         cb = event_info->m_cb;
326         user_data = event_info->m_user_data;
327         latency = event_info->m_latency;
328
329         return true;
330 }
331
332
333 void sensor_client_info::get_listening_sensors(sensor_id_vector &sensors)
334 {
335         AUTOLOCK(m_handle_info_lock);
336
337         auto it_handle = m_sensor_handle_infos.begin();
338
339         while (it_handle != m_sensor_handle_infos.end()) {
340                 sensors.push_back(it_handle->second.m_sensor_id);
341                 ++it_handle;
342         }
343
344         sort(sensors.begin(), sensors.end());
345         unique(sensors.begin(),sensors.end());
346 }
347
348 void sensor_client_info::get_sensor_rep(sensor_id_t sensor, sensor_rep& rep) {
349         const unsigned int INVALID_BATCH_VALUE = std::numeric_limits<unsigned int>::max();
350
351         rep.active = is_sensor_active(sensor);
352         rep.option = get_active_option(sensor);
353         if (!get_active_batch(sensor, rep.interval, rep.latency)) {
354                 rep.interval = INVALID_BATCH_VALUE;
355                 rep.latency = INVALID_BATCH_VALUE;
356         }
357
358         get_active_event_types(sensor, rep.event_types);
359 }
360
361 bool sensor_client_info::add_command_channel(sensor_id_t sensor, command_channel *cmd_channel)
362 {
363         auto it_channel = m_command_channels.find(sensor);
364
365         if (it_channel != m_command_channels.end()) {
366                 ERR("%s alreay has command_channel for %s", get_client_name(), get_sensor_name(sensor));
367                 return false;
368         }
369
370         m_command_channels.insert(pair<sensor_id_t, command_channel *> (sensor, cmd_channel));
371
372         return true;
373
374 }
375 bool sensor_client_info::get_command_channel(sensor_id_t sensor, command_channel **cmd_channel)
376 {
377         auto it_channel = m_command_channels.find(sensor);
378
379         if (it_channel == m_command_channels.end()) {
380                 ERR("%s doesn't have command_channel for %s", get_client_name(), get_sensor_name(sensor));
381                 return false;
382         }
383
384         *cmd_channel = it_channel->second;
385
386         return true;
387 }
388
389
390 bool sensor_client_info::close_command_channel(void)
391 {
392         auto it_channel = m_command_channels.begin();
393
394         if (it_channel != m_command_channels.end()) {
395                 delete it_channel->second;
396                 ++it_channel;
397         }
398
399         m_command_channels.clear();
400
401         return true;
402 }
403
404 bool sensor_client_info::close_command_channel(sensor_id_t sensor_id)
405 {
406         auto it_channel = m_command_channels.find(sensor_id);
407
408         if (it_channel == m_command_channels.end()) {
409                 ERR("%s doesn't have command_channel for %s", get_client_name(), get_sensor_name(sensor_id));
410                 return false;
411         }
412
413         delete it_channel->second;
414
415         m_command_channels.erase(it_channel);
416
417         return true;
418 }
419
420
421 bool sensor_client_info::has_client_id(void)
422 {
423         return (m_client_id != CLIENT_ID_INVALID);
424 }
425
426 int sensor_client_info::get_client_id(void)
427 {
428         return m_client_id;
429 }
430
431 void sensor_client_info::set_client_id(int client_id)
432 {
433         m_client_id = client_id;
434 }
435
436 bool sensor_client_info::get_active_batch(sensor_id_t sensor, unsigned int &interval, unsigned int &latency)
437 {
438         unsigned int min_interval = POLL_MAX_HZ_MS;
439         unsigned int min_latency = std::numeric_limits<unsigned int>::max();
440
441         bool active_sensor_found = false;
442         unsigned int _interval;
443         unsigned int _latency;
444
445         AUTOLOCK(m_handle_info_lock);
446
447         auto it_handle = m_sensor_handle_infos.begin();
448
449         while (it_handle != m_sensor_handle_infos.end()) {
450                 if ((it_handle->second.m_sensor_id == sensor) &&
451                         (it_handle->second.m_sensor_state == SENSOR_STATE_STARTED)) {
452                                 active_sensor_found = true;
453                                 it_handle->second.get_batch(_interval, _latency);
454                                 min_interval = (_interval < min_interval) ? _interval : min_interval;
455                                 min_latency = (_latency < min_latency) ? _latency : min_latency;
456                 }
457
458                 ++it_handle;
459         }
460
461         if (!active_sensor_found) {
462                 DBG("Active sensor[0x%llx] is not found for client %s", sensor, get_client_name());
463                 return false;
464         }
465
466         interval = min_interval;
467         latency = min_latency;
468
469         return true;
470 }
471
472 unsigned int sensor_client_info::get_active_option(sensor_id_t sensor)
473 {
474         int active_option = SENSOR_OPTION_DEFAULT;
475         bool active_sensor_found = false;
476         int option;
477
478         AUTOLOCK(m_handle_info_lock);
479
480         auto it_handle = m_sensor_handle_infos.begin();
481
482         while (it_handle != m_sensor_handle_infos.end()) {
483                 if ((it_handle->second.m_sensor_id == sensor) &&
484                         (it_handle->second.m_sensor_state == SENSOR_STATE_STARTED)) {
485                                 active_sensor_found = true;
486                                 option = it_handle->second.m_sensor_option;
487                                 active_option = (option > active_option) ? option : active_option;
488                 }
489
490                 ++it_handle;
491         }
492
493         if (!active_sensor_found)
494                 DBG("Active sensor[0x%llx] is not found for client %s", sensor, get_client_name());
495
496         return active_option;
497 }
498
499 bool sensor_client_info::get_sensor_id(int handle, sensor_id_t &sensor)
500 {
501         AUTOLOCK(m_handle_info_lock);
502
503         auto it_handle = m_sensor_handle_infos.find(handle);
504
505         if (it_handle == m_sensor_handle_infos.end()) {
506                 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
507                 return false;
508         }
509
510         sensor = it_handle->second.m_sensor_id;
511
512         return true;
513 }
514
515 bool sensor_client_info::get_sensor_state(int handle, int &sensor_state)
516 {
517         AUTOLOCK(m_handle_info_lock);
518
519         auto it_handle = m_sensor_handle_infos.find(handle);
520
521         if (it_handle == m_sensor_handle_infos.end()) {
522                 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
523                 return false;
524         }
525
526         sensor_state = it_handle->second.m_sensor_state;
527
528         return true;
529 }
530
531 bool sensor_client_info::get_sensor_wakeup(int handle, int &sensor_wakeup)
532 {
533         AUTOLOCK(m_handle_info_lock);
534
535         auto it_handle = m_sensor_handle_infos.find(handle);
536
537         if (it_handle == m_sensor_handle_infos.end()) {
538                 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
539                 return false;
540         }
541
542         sensor_wakeup = it_handle->second.m_sensor_wakeup;
543
544         return true;
545 }
546
547 bool sensor_client_info::set_sensor_wakeup(int handle, int sensor_wakeup)
548 {
549         AUTOLOCK(m_handle_info_lock);
550
551         auto it_handle = m_sensor_handle_infos.find(handle);
552
553         if (it_handle == m_sensor_handle_infos.end()) {
554                 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
555                 return false;
556         }
557
558         it_handle->second.m_sensor_wakeup = sensor_wakeup;
559
560         return true;
561 }
562
563 void sensor_client_info::get_active_event_types(sensor_id_t sensor, event_type_vector &active_event_types)
564 {
565         event_type_vector event_types;
566
567         AUTOLOCK(m_handle_info_lock);
568
569         auto it_handle = m_sensor_handle_infos.begin();
570
571         while (it_handle != m_sensor_handle_infos.end()) {
572                 if ((it_handle->second.m_sensor_id == sensor) &&
573                         (it_handle->second.m_sensor_state == SENSOR_STATE_STARTED))
574                                 it_handle->second.get_reg_event_types(event_types);
575
576                 ++it_handle;
577         }
578
579         if (event_types.empty())
580                 return;
581
582         sort(event_types.begin(), event_types.end());
583
584         unique_copy(event_types.begin(), event_types.end(), back_inserter(active_event_types));
585
586 }
587
588
589 void sensor_client_info::get_all_handles(handle_vector &handles)
590 {
591         AUTOLOCK(m_handle_info_lock);
592
593         auto it_handle = m_sensor_handle_infos.begin();
594
595         while (it_handle != m_sensor_handle_infos.end()) {
596                 handles.push_back(it_handle->first);
597                 ++it_handle;
598         }
599 }
600
601 void sensor_client_info::get_sensor_handle_info(sensor_id_t sensor, sensor_handle_info_map &handles_info) {
602
603         AUTOLOCK(m_handle_info_lock);
604
605         auto it_handle = m_sensor_handle_infos.begin();
606
607         while (it_handle != m_sensor_handle_infos.end()) {
608                 if (it_handle->second.m_sensor_id == sensor) {
609                         handles_info.insert(pair<int,sensor_handle_info> (it_handle->first, it_handle->second));
610                 }
611
612                 ++it_handle;
613         }
614 }
615
616 void sensor_client_info::get_all_handle_info(sensor_handle_info_map &handles_info) {
617
618         AUTOLOCK(m_handle_info_lock);
619
620         auto it_handle = m_sensor_handle_infos.begin();
621
622         while (it_handle != m_sensor_handle_infos.end()) {
623                 handles_info.insert(pair<int,sensor_handle_info> (it_handle->first, it_handle->second));
624                 ++it_handle;
625         }
626 }
627
628 bool sensor_client_info::is_sensor_registered(sensor_id_t sensor)
629 {
630         AUTOLOCK(m_handle_info_lock);
631
632         auto it_handle = m_sensor_handle_infos.begin();
633
634         while (it_handle != m_sensor_handle_infos.end()) {
635                 if (it_handle->second.m_sensor_id == sensor)
636                         return true;
637
638                 ++it_handle;
639         }
640
641         return false;
642 }
643
644
645 bool sensor_client_info::is_sensor_active(sensor_id_t sensor)
646 {
647         AUTOLOCK(m_handle_info_lock);
648
649         auto it_handle = m_sensor_handle_infos.begin();
650
651         while (it_handle != m_sensor_handle_infos.end()) {
652                 if ((it_handle->second.m_sensor_id == sensor) &&
653                         (it_handle->second.m_sensor_state == SENSOR_STATE_STARTED))
654                         return true;
655
656                 ++it_handle;
657         }
658
659         return false;
660 }
661
662 bool sensor_client_info::is_event_active(int handle, unsigned int event_type, unsigned long long event_id)
663 {
664         reg_event_info *event_info;
665
666         AUTOLOCK(m_handle_info_lock);
667
668         auto it_handle = m_sensor_handle_infos.find(handle);
669
670         if (it_handle == m_sensor_handle_infos.end())
671                 return false;
672
673         event_info = it_handle->second.get_reg_event_info(event_type);
674         if (!event_info)
675                 return false;
676
677         if (event_info->m_id != event_id)
678                 return false;
679
680         return true;
681 }
682
683 void sensor_client_info::clear(void)
684 {
685         close_command_channel();
686         m_sensor_handle_infos.clear();
687         m_command_channels.clear();
688         set_client_id(CLIENT_ID_INVALID);
689 }