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