4 * Copyright (c) 2015 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>
28 #define MIN_DELIVERY_DIFF_FACTOR 0.75f
33 sensor_client_info::sensor_client_info()
34 : m_client_id(CLIENT_ID_INVALID)
38 sensor_client_info::~sensor_client_info()
43 sensor_client_info& sensor_client_info::get_instance(void)
45 static sensor_client_info inst;
50 int sensor_client_info::create_handle(sensor_id_t sensor)
52 sensor_handle_info handle_info;
55 AUTOLOCK(m_handle_info_lock);
57 while (m_sensor_handle_infos.count(handle) > 0)
60 if (handle == MAX_HANDLE) {
61 ERR("Handles of client %s are full", get_client_name());
62 return MAX_HANDLE_REACHED;
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;
73 m_sensor_handle_infos.insert(pair<int,sensor_handle_info> (handle, handle_info));
78 bool sensor_client_info::delete_handle(int handle)
80 AUTOLOCK(m_handle_info_lock);
82 auto it_handle = m_sensor_handle_infos.find(handle);
84 if (it_handle == m_sensor_handle_infos.end()) {
85 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
89 m_sensor_handle_infos.erase(it_handle);
94 bool sensor_client_info::is_active()
96 AUTOLOCK(m_handle_info_lock);
98 return !m_sensor_handle_infos.empty();
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)
104 AUTOLOCK(m_handle_info_lock);
106 auto it_handle = m_sensor_handle_infos.find(handle);
108 if (it_handle == m_sensor_handle_infos.end()) {
109 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
113 if (!it_handle->second.add_reg_event_info(event_type, interval, latency, cb_type, cb, user_data))
119 bool sensor_client_info::unregister_event(int handle, unsigned int event_type)
121 AUTOLOCK(m_handle_info_lock);
123 auto it_handle = m_sensor_handle_infos.find(handle);
125 if (it_handle == m_sensor_handle_infos.end()) {
126 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
130 if (!it_handle->second.delete_reg_event_info(event_type))
136 bool sensor_client_info::register_accuracy_cb(int handle, sensor_accuracy_changed_cb_t cb, void* user_data)
138 AUTOLOCK(m_handle_info_lock);
140 auto it_handle = m_sensor_handle_infos.find(handle);
142 if (it_handle == m_sensor_handle_infos.end()) {
143 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
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;
154 bool sensor_client_info::unregister_accuracy_cb(int handle)
156 AUTOLOCK(m_handle_info_lock);
158 auto it_handle = m_sensor_handle_infos.find(handle);
160 if (it_handle == m_sensor_handle_infos.end()) {
161 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
165 it_handle->second.m_accuracy = -1;
166 it_handle->second.m_accuracy_cb = NULL;
167 it_handle->second.m_accuracy_user_data = NULL;
172 bool sensor_client_info::set_sensor_params(int handle, int sensor_state, int sensor_option)
174 AUTOLOCK(m_handle_info_lock);
176 auto it_handle = m_sensor_handle_infos.find(handle);
178 if (it_handle == m_sensor_handle_infos.end()) {
179 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
183 it_handle->second.m_sensor_state = sensor_state;
184 it_handle->second.m_sensor_option = sensor_option;
189 bool sensor_client_info::get_sensor_params(int handle, int &sensor_state, int &sensor_option)
191 AUTOLOCK(m_handle_info_lock);
193 auto it_handle = m_sensor_handle_infos.find(handle);
195 if (it_handle == m_sensor_handle_infos.end()) {
196 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
200 sensor_state = it_handle->second.m_sensor_state;
201 sensor_option = it_handle->second.m_sensor_option;
206 bool sensor_client_info::set_sensor_state(int handle, int sensor_state)
208 AUTOLOCK(m_handle_info_lock);
210 auto it_handle = m_sensor_handle_infos.find(handle);
212 if (it_handle == m_sensor_handle_infos.end()) {
213 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
217 it_handle->second.m_sensor_state = sensor_state;
222 bool sensor_client_info::set_sensor_option(int handle, int sensor_option)
224 AUTOLOCK(m_handle_info_lock);
226 auto it_handle = m_sensor_handle_infos.find(handle);
228 if (it_handle == m_sensor_handle_infos.end()) {
229 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
233 it_handle->second.m_sensor_option = sensor_option;
238 bool sensor_client_info::set_event_batch(int handle, unsigned int event_type, unsigned int interval, unsigned int latency)
240 AUTOLOCK(m_handle_info_lock);
242 auto it_handle = m_sensor_handle_infos.find(handle);
244 if (it_handle == m_sensor_handle_infos.end()) {
245 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
249 if (!it_handle->second.change_reg_event_batch(event_type, interval, latency))
255 bool sensor_client_info::set_event_maincontext(int handle, unsigned int event_type, GMainContext *maincontext)
257 AUTOLOCK(m_handle_info_lock);
259 auto it_handle = m_sensor_handle_infos.find(handle);
261 if (it_handle == m_sensor_handle_infos.end()) {
262 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
266 if (!it_handle->second.change_reg_event_maincontext(event_type, maincontext))
272 bool sensor_client_info::set_accuracy(int handle, int accuracy)
274 AUTOLOCK(m_handle_info_lock);
276 auto it_handle = m_sensor_handle_infos.find(handle);
278 if (it_handle == m_sensor_handle_infos.end()) {
279 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
283 it_handle->second.m_accuracy = accuracy;
288 bool sensor_client_info::set_bad_accuracy(int handle, int bad_accuracy)
290 AUTOLOCK(m_handle_info_lock);
292 auto it_handle = m_sensor_handle_infos.find(handle);
294 if (it_handle == m_sensor_handle_infos.end()) {
295 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
299 it_handle->second.m_bad_accuracy = bad_accuracy;
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)
306 AUTOLOCK(m_handle_info_lock);
308 auto it_handle = m_sensor_handle_infos.find(handle);
310 if (it_handle == m_sensor_handle_infos.end()) {
311 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
315 const reg_event_info *event_info;
317 event_info = it_handle->second.get_reg_event_info(event_type);
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;
333 void sensor_client_info::get_listening_sensors(sensor_id_vector &sensors)
335 AUTOLOCK(m_handle_info_lock);
337 auto it_handle = m_sensor_handle_infos.begin();
339 while (it_handle != m_sensor_handle_infos.end()) {
340 sensors.push_back(it_handle->second.m_sensor_id);
344 sort(sensors.begin(), sensors.end());
345 unique(sensors.begin(),sensors.end());
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();
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;
358 get_active_event_types(sensor, rep.event_types);
361 bool sensor_client_info::add_command_channel(sensor_id_t sensor, command_channel *cmd_channel)
363 auto it_channel = m_command_channels.find(sensor);
365 if (it_channel != m_command_channels.end()) {
366 ERR("%s alreay has command_channel for %s", get_client_name(), get_sensor_name(sensor));
370 m_command_channels.insert(pair<sensor_id_t, command_channel *> (sensor, cmd_channel));
375 bool sensor_client_info::get_command_channel(sensor_id_t sensor, command_channel **cmd_channel)
377 auto it_channel = m_command_channels.find(sensor);
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));
384 *cmd_channel = it_channel->second;
390 bool sensor_client_info::close_command_channel(void)
392 auto it_channel = m_command_channels.begin();
394 if (it_channel != m_command_channels.end()) {
395 delete it_channel->second;
399 m_command_channels.clear();
404 bool sensor_client_info::close_command_channel(sensor_id_t sensor_id)
406 auto it_channel = m_command_channels.find(sensor_id);
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));
413 delete it_channel->second;
415 m_command_channels.erase(it_channel);
421 bool sensor_client_info::has_client_id(void)
423 return (m_client_id != CLIENT_ID_INVALID);
426 int sensor_client_info::get_client_id(void)
431 void sensor_client_info::set_client_id(int client_id)
433 m_client_id = client_id;
436 bool sensor_client_info::get_active_batch(sensor_id_t sensor, unsigned int &interval, unsigned int &latency)
438 unsigned int min_interval = POLL_MAX_HZ_MS;
439 unsigned int min_latency = std::numeric_limits<unsigned int>::max();
441 bool active_sensor_found = false;
442 unsigned int _interval;
443 unsigned int _latency;
445 AUTOLOCK(m_handle_info_lock);
447 auto it_handle = m_sensor_handle_infos.begin();
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;
461 if (!active_sensor_found) {
462 DBG("Active sensor[0x%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_option(sensor_id_t sensor)
474 int active_option = SENSOR_OPTION_DEFAULT;
475 bool active_sensor_found = false;
478 AUTOLOCK(m_handle_info_lock);
480 auto it_handle = m_sensor_handle_infos.begin();
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;
493 if (!active_sensor_found)
494 DBG("Active sensor[0x%llx] is not found for client %s", sensor, get_client_name());
496 return active_option;
499 bool sensor_client_info::get_sensor_id(int handle, sensor_id_t &sensor)
501 AUTOLOCK(m_handle_info_lock);
503 auto it_handle = m_sensor_handle_infos.find(handle);
505 if (it_handle == m_sensor_handle_infos.end()) {
506 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
510 sensor = it_handle->second.m_sensor_id;
515 bool sensor_client_info::get_sensor_state(int handle, int &sensor_state)
517 AUTOLOCK(m_handle_info_lock);
519 auto it_handle = m_sensor_handle_infos.find(handle);
521 if (it_handle == m_sensor_handle_infos.end()) {
522 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
526 sensor_state = it_handle->second.m_sensor_state;
531 bool sensor_client_info::get_sensor_wakeup(int handle, int &sensor_wakeup)
533 AUTOLOCK(m_handle_info_lock);
535 auto it_handle = m_sensor_handle_infos.find(handle);
537 if (it_handle == m_sensor_handle_infos.end()) {
538 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
542 sensor_wakeup = it_handle->second.m_sensor_wakeup;
547 bool sensor_client_info::set_sensor_wakeup(int handle, int sensor_wakeup)
549 AUTOLOCK(m_handle_info_lock);
551 auto it_handle = m_sensor_handle_infos.find(handle);
553 if (it_handle == m_sensor_handle_infos.end()) {
554 ERR("Handle[%d] is not found for client %s", handle, get_client_name());
558 it_handle->second.m_sensor_wakeup = sensor_wakeup;
563 void sensor_client_info::get_active_event_types(sensor_id_t sensor, event_type_vector &active_event_types)
565 event_type_vector event_types;
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 (it_handle->second.m_sensor_state == SENSOR_STATE_STARTED))
574 it_handle->second.get_reg_event_types(event_types);
579 if (event_types.empty())
582 sort(event_types.begin(), event_types.end());
584 unique_copy(event_types.begin(), event_types.end(), back_inserter(active_event_types));
589 void sensor_client_info::get_all_handles(handle_vector &handles)
591 AUTOLOCK(m_handle_info_lock);
593 auto it_handle = m_sensor_handle_infos.begin();
595 while (it_handle != m_sensor_handle_infos.end()) {
596 handles.push_back(it_handle->first);
601 void sensor_client_info::get_sensor_handle_info(sensor_id_t sensor, sensor_handle_info_map &handles_info) {
603 AUTOLOCK(m_handle_info_lock);
605 auto it_handle = m_sensor_handle_infos.begin();
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));
616 void sensor_client_info::get_all_handle_info(sensor_handle_info_map &handles_info) {
618 AUTOLOCK(m_handle_info_lock);
620 auto it_handle = m_sensor_handle_infos.begin();
622 while (it_handle != m_sensor_handle_infos.end()) {
623 handles_info.insert(pair<int,sensor_handle_info> (it_handle->first, it_handle->second));
628 bool sensor_client_info::is_sensor_registered(sensor_id_t sensor)
630 AUTOLOCK(m_handle_info_lock);
632 auto it_handle = m_sensor_handle_infos.begin();
634 while (it_handle != m_sensor_handle_infos.end()) {
635 if (it_handle->second.m_sensor_id == sensor)
645 bool sensor_client_info::is_sensor_active(sensor_id_t sensor)
647 AUTOLOCK(m_handle_info_lock);
649 auto it_handle = m_sensor_handle_infos.begin();
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))
662 bool sensor_client_info::is_event_active(int handle, unsigned int event_type, unsigned long long event_id)
664 reg_event_info *event_info;
666 AUTOLOCK(m_handle_info_lock);
668 auto it_handle = m_sensor_handle_infos.find(handle);
670 if (it_handle == m_sensor_handle_infos.end())
673 event_info = it_handle->second.get_reg_event_info(event_type);
677 if (event_info->m_id != event_id)
683 void sensor_client_info::clear(void)
685 close_command_channel();
686 m_sensor_handle_infos.clear();
687 m_command_channels.clear();
688 set_client_id(CLIENT_ID_INVALID);