4 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <sensor_event_listener.h>
21 #include <client_common.h>
22 #include <sensor_info_manager.h>
30 sensor_client_info::sensor_client_info()
31 : m_client_id(CLIENT_ID_INVALID)
35 sensor_client_info::~sensor_client_info()
39 sensor_client_info& sensor_client_info::get_instance(void)
41 static sensor_client_info inst;
45 int sensor_client_info::create_handle(sensor_id_t sensor)
47 sensor_handle_info handle_info;
50 AUTOLOCK(m_handle_info_lock);
52 while (m_sensor_handle_infos.count(handle) > 0)
55 if (handle == MAX_HANDLE) {
56 _W("Handles of client %s are full", get_client_name());
57 return MAX_HANDLE_REACHED;
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;
68 m_sensor_handle_infos.insert(pair<int, sensor_handle_info>(handle, handle_info));
73 bool sensor_client_info::delete_handle(int handle)
75 AUTOLOCK(m_handle_info_lock);
77 auto it_handle = m_sensor_handle_infos.find(handle);
79 if (it_handle == m_sensor_handle_infos.end()) {
80 _W("Handle[%d] is not found for client %s", handle, get_client_name());
84 m_sensor_handle_infos.erase(it_handle);
88 bool sensor_client_info::is_active(void)
90 AUTOLOCK(m_handle_info_lock);
92 return !m_sensor_handle_infos.empty();
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)
98 AUTOLOCK(m_handle_info_lock);
100 auto it_handle = m_sensor_handle_infos.find(handle);
102 if (it_handle == m_sensor_handle_infos.end()) {
103 _W("Handle[%d] is not found for client %s", handle, get_client_name());
107 if (!it_handle->second.add_reg_event_info(event_type, interval, latency, cb, user_data))
113 bool sensor_client_info::unregister_event(int handle, unsigned int event_type)
115 AUTOLOCK(m_handle_info_lock);
117 auto it_handle = m_sensor_handle_infos.find(handle);
119 if (it_handle == m_sensor_handle_infos.end()) {
120 _W("Handle[%d] is not found for client %s", handle, get_client_name());
124 if (!it_handle->second.delete_reg_event_info(event_type))
130 bool sensor_client_info::register_accuracy_cb(int handle, sensor_accuracy_changed_cb_t cb, void* user_data)
132 AUTOLOCK(m_handle_info_lock);
134 auto it_handle = m_sensor_handle_infos.find(handle);
136 if (it_handle == m_sensor_handle_infos.end()) {
137 _W("Handle[%d] is not found for client %s", handle, get_client_name());
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;
148 bool sensor_client_info::unregister_accuracy_cb(int handle)
150 AUTOLOCK(m_handle_info_lock);
152 auto it_handle = m_sensor_handle_infos.find(handle);
154 if (it_handle == m_sensor_handle_infos.end()) {
155 _W("Handle[%d] is not found for client %s", handle, get_client_name());
159 it_handle->second.m_accuracy = -1;
160 it_handle->second.m_accuracy_cb = NULL;
161 it_handle->second.m_accuracy_user_data = NULL;
166 bool sensor_client_info::set_sensor_params(int handle, int sensor_state, int pause_policy)
168 AUTOLOCK(m_handle_info_lock);
170 auto it_handle = m_sensor_handle_infos.find(handle);
172 if (it_handle == m_sensor_handle_infos.end()) {
173 _W("Handle[%d] is not found for client %s", handle, get_client_name());
177 it_handle->second.m_sensor_state = sensor_state;
178 it_handle->second.m_pause_policy = pause_policy;
183 bool sensor_client_info::get_sensor_params(int handle, int &sensor_state, int &pause_policy)
185 AUTOLOCK(m_handle_info_lock);
187 auto it_handle = m_sensor_handle_infos.find(handle);
189 if (it_handle == m_sensor_handle_infos.end()) {
190 _W("Handle[%d] is not found for client %s", handle, get_client_name());
194 sensor_state = it_handle->second.m_sensor_state;
195 pause_policy = it_handle->second.m_pause_policy;
200 bool sensor_client_info::set_sensor_state(int handle, int sensor_state)
202 AUTOLOCK(m_handle_info_lock);
204 auto it_handle = m_sensor_handle_infos.find(handle);
206 if (it_handle == m_sensor_handle_infos.end()) {
207 _W("Handle[%d] is not found for client %s", handle, get_client_name());
211 it_handle->second.m_sensor_state = sensor_state;
216 bool sensor_client_info::get_passive_mode(int handle)
218 AUTOLOCK(m_handle_info_lock);
220 auto it_handle = m_sensor_handle_infos.find(handle);
222 if (it_handle == m_sensor_handle_infos.end()) {
223 _W("Handle[%d] is not found for client %s", handle, get_client_name());
227 return it_handle->second.get_passive_mode();
230 bool sensor_client_info::set_passive_mode(int handle, bool passive)
232 AUTOLOCK(m_handle_info_lock);
234 auto it_handle = m_sensor_handle_infos.find(handle);
236 if (it_handle == m_sensor_handle_infos.end()) {
237 _W("Handle[%d] is not found for client %s", handle, get_client_name());
241 it_handle->second.set_passive_mode(passive);
246 bool sensor_client_info::set_sensor_pause_policy(int handle, int pause_policy)
248 AUTOLOCK(m_handle_info_lock);
250 auto it_handle = m_sensor_handle_infos.find(handle);
252 if (it_handle == m_sensor_handle_infos.end()) {
253 _W("Handle[%d] is not found for client %s", handle, get_client_name());
257 it_handle->second.m_pause_policy = pause_policy;
262 bool sensor_client_info::set_event_batch(int handle, unsigned int event_type, unsigned int interval, unsigned int latency)
264 AUTOLOCK(m_handle_info_lock);
266 auto it_handle = m_sensor_handle_infos.find(handle);
268 if (it_handle == m_sensor_handle_infos.end()) {
269 _W("Handle[%d] is not found for client %s", handle, get_client_name());
273 if (!it_handle->second.change_reg_event_batch(event_type, interval, latency))
279 bool sensor_client_info::set_accuracy(int handle, int accuracy)
281 AUTOLOCK(m_handle_info_lock);
283 auto it_handle = m_sensor_handle_infos.find(handle);
285 if (it_handle == m_sensor_handle_infos.end()) {
286 _W("Handle[%d] is not found for client %s", handle, get_client_name());
290 it_handle->second.m_accuracy = accuracy;
295 bool sensor_client_info::set_bad_accuracy(int handle, int bad_accuracy)
297 AUTOLOCK(m_handle_info_lock);
299 auto it_handle = m_sensor_handle_infos.find(handle);
301 if (it_handle == m_sensor_handle_infos.end()) {
302 _W("Handle[%d] is not found for client %s", handle, get_client_name());
306 it_handle->second.m_bad_accuracy = bad_accuracy;
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)
313 AUTOLOCK(m_handle_info_lock);
315 auto it_handle = m_sensor_handle_infos.find(handle);
317 if (it_handle == m_sensor_handle_infos.end()) {
318 _W("Handle[%d] is not found for client %s", handle, get_client_name());
322 const reg_event_info *event_info;
324 event_info = it_handle->second.get_reg_event_info(event_type);
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;
337 void sensor_client_info::get_listening_sensors(sensor_id_vector &sensors)
339 AUTOLOCK(m_handle_info_lock);
341 auto it_handle = m_sensor_handle_infos.begin();
343 while (it_handle != m_sensor_handle_infos.end()) {
344 sensors.push_back(it_handle->second.m_sensor_id);
348 sort(sensors.begin(), sensors.end());
349 unique(sensors.begin(), sensors.end());
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();
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;
362 get_active_event_types(sensor, rep.event_types);
365 bool sensor_client_info::add_command_channel(sensor_id_t sensor, command_channel *cmd_channel)
367 auto it_channel = m_command_channels.find(sensor);
369 if (it_channel != m_command_channels.end()) {
370 _W("%s alreay has command_channel for %s", get_client_name(), get_sensor_name(sensor));
374 m_command_channels.insert(pair<sensor_id_t, command_channel *>(sensor, cmd_channel));
379 bool sensor_client_info::get_command_channel(sensor_id_t sensor, command_channel **cmd_channel)
381 auto it_channel = m_command_channels.find(sensor);
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));
388 *cmd_channel = it_channel->second;
393 bool sensor_client_info::close_command_channel(void)
395 auto it_channel = m_command_channels.begin();
397 if (it_channel != m_command_channels.end()) {
398 delete it_channel->second;
402 m_command_channels.clear();
407 bool sensor_client_info::close_command_channel(sensor_id_t sensor_id)
409 auto it_channel = m_command_channels.find(sensor_id);
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));
416 delete it_channel->second;
418 m_command_channels.erase(it_channel);
423 bool sensor_client_info::has_client_id(void)
425 return (m_client_id != CLIENT_ID_INVALID);
428 int sensor_client_info::get_client_id(void)
433 void sensor_client_info::set_client_id(int client_id)
435 m_client_id = client_id;
438 bool sensor_client_info::get_active_batch(sensor_id_t sensor, unsigned int &interval, unsigned int &latency)
440 unsigned int min_interval = POLL_MAX_HZ_MS;
441 unsigned int min_latency = std::numeric_limits<unsigned int>::max();
443 unsigned int _interval;
444 unsigned int _latency;
446 AUTOLOCK(m_handle_info_lock);
448 auto it_handle = m_sensor_handle_infos.begin();
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;
461 if (!is_sensor_active(sensor)) {
462 _D("Active sensor[%#llx] is not found for client %s", sensor, get_client_name());
466 interval = min_interval;
467 latency = min_latency;
472 unsigned int sensor_client_info::get_active_pause_policy(sensor_id_t sensor)
474 int active_pause = SENSORD_PAUSE_ALL;
477 AUTOLOCK(m_handle_info_lock);
479 auto it_handle = m_sensor_handle_infos.begin();
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;
491 if (!is_sensor_active(sensor))
492 _D("Active sensor[%#llx] is not found for client %s", sensor, get_client_name());
497 bool sensor_client_info::get_sensor_id(int handle, sensor_id_t &sensor)
499 AUTOLOCK(m_handle_info_lock);
501 auto it_handle = m_sensor_handle_infos.find(handle);
503 if (it_handle == m_sensor_handle_infos.end()) {
504 _W("Handle[%d] is not found for client %s", handle, get_client_name());
508 sensor = it_handle->second.m_sensor_id;
513 bool sensor_client_info::get_sensor_state(int handle, int &sensor_state)
515 AUTOLOCK(m_handle_info_lock);
517 auto it_handle = m_sensor_handle_infos.find(handle);
519 if (it_handle == m_sensor_handle_infos.end()) {
520 _W("Handle[%d] is not found for client %s", handle, get_client_name());
524 sensor_state = it_handle->second.m_sensor_state;
529 void sensor_client_info::get_active_event_types(sensor_id_t sensor, event_type_vector &active_event_types)
531 event_type_vector event_types;
533 AUTOLOCK(m_handle_info_lock);
535 auto it_handle = m_sensor_handle_infos.begin();
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);
545 if (event_types.empty())
548 sort(event_types.begin(), event_types.end());
550 unique_copy(event_types.begin(), event_types.end(), back_inserter(active_event_types));
553 void sensor_client_info::get_all_handles(handle_vector &handles)
555 AUTOLOCK(m_handle_info_lock);
557 auto it_handle = m_sensor_handle_infos.begin();
559 while (it_handle != m_sensor_handle_infos.end()) {
560 handles.push_back(it_handle->first);
565 void sensor_client_info::get_sensor_handle_info(sensor_id_t sensor, sensor_handle_info_map &handles_info)
567 AUTOLOCK(m_handle_info_lock);
569 auto it_handle = m_sensor_handle_infos.begin();
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));
579 void sensor_client_info::get_all_handle_info(sensor_handle_info_map &handles_info)
581 AUTOLOCK(m_handle_info_lock);
583 auto it_handle = m_sensor_handle_infos.begin();
585 while (it_handle != m_sensor_handle_infos.end()) {
586 handles_info.insert(pair<int, sensor_handle_info>(it_handle->first, it_handle->second));
591 bool sensor_client_info::is_sensor_registered(sensor_id_t sensor)
593 AUTOLOCK(m_handle_info_lock);
595 auto it_handle = m_sensor_handle_infos.begin();
597 while (it_handle != m_sensor_handle_infos.end()) {
598 if (it_handle->second.m_sensor_id == sensor)
607 bool sensor_client_info::is_sensor_active(sensor_id_t sensor)
609 AUTOLOCK(m_handle_info_lock);
611 auto it_handle = m_sensor_handle_infos.begin();
613 while (it_handle != m_sensor_handle_infos.end()) {
614 if ((it_handle->second.m_sensor_id == sensor) &&
615 it_handle->second.is_started())
624 bool sensor_client_info::is_event_active(int handle, unsigned int event_type, unsigned long long event_id)
626 reg_event_info *event_info;
628 AUTOLOCK(m_handle_info_lock);
630 auto it_handle = m_sensor_handle_infos.find(handle);
632 if (it_handle == m_sensor_handle_infos.end())
635 event_info = it_handle->second.get_reg_event_info(event_type);
639 if (event_info->m_id != event_id)
645 void sensor_client_info::set_pause_policy(sensor_id_t sensor, int pause_policy)
647 sensor_handle_info_map handles_info;
649 get_sensor_handle_info(sensor, handles_info);
651 for (auto it_handle = handles_info.begin(); it_handle != handles_info.end(); ++it_handle) {
652 if (it_handle->second.m_sensor_id != sensor)
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);
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);
669 bool sensor_client_info::set_attribute(int handle, int attribute, int value)
671 AUTOLOCK(m_handle_info_lock);
673 auto it_handle = m_sensor_handle_infos.find(handle);
675 if (it_handle == m_sensor_handle_infos.end()) {
676 _W("Handle[%d] is not found for client %s", handle, get_client_name());
680 it_handle->second.attributes_int[attribute] = value;
685 bool sensor_client_info::set_attribute(int handle, int attribute, const char *value, int len)
687 AUTOLOCK(m_handle_info_lock);
689 auto it_handle = m_sensor_handle_infos.find(handle);
691 if (it_handle == m_sensor_handle_infos.end()) {
692 _W("Handle[%d] is not found for client %s", handle, get_client_name());
696 auto it_attr = it_handle->second.attributes_str.find(attribute);
698 if (it_attr != it_handle->second.attributes_str.end()) {
699 it_attr->second->set(value, len);
703 attribute_info *info = new(std::nothrow) attribute_info();
704 retvm_if(!info, false, "Failed to allocate memory");
706 info->set(value, len);
707 it_handle->second.attributes_str[attribute] = info;
712 void sensor_client_info::clear(void)
714 auto it_handle = m_sensor_handle_infos.begin();
716 while (it_handle != m_sensor_handle_infos.end())
717 it_handle->second.clear();
719 close_command_channel();
720 m_sensor_handle_infos.clear();
721 m_command_channels.clear();
722 set_client_id(CLIENT_ID_INVALID);