4 * Copyright (c) 2013 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_common.h>
21 #include <sensor_internal_deprecated.h>
22 #include <sensor_internal.h>
23 #include <sensor_event_listener.h>
24 #include <sensor_client_info.h>
25 #include <client_common.h>
28 #include <sensor_log.h>
29 #include <sensor_info.h>
30 #include <sensor_info_manager.h>
33 #include "dbus_listener.h"
38 #define API __attribute__((visibility("default")))
41 #ifndef VCONFKEY_SETAPPL_PSMODE
42 #define VCONFKEY_SETAPPL_PSMODE "db/setting/psmode"
45 #define DEFAULT_INTERVAL POLL_10HZ_MS
47 #define CONVERT_OPTION_PAUSE_POLICY(option) ((option) ^ 0b11)
51 static int g_power_save_state = SENSORD_PAUSE_NONE;
53 static int get_power_save_state(void);
54 static void power_save_state_cb(keynode_t *node, void *data);
55 static void clean_up(void);
56 static bool change_sensor_rep(sensor_id_t sensor_id, sensor_rep &prev_rep, sensor_rep &cur_rep);
57 static void restore_session(void);
58 static bool register_event(int handle, unsigned int event_type, unsigned int interval, int max_batch_latency, void* cb, void *user_data);
64 sensor_event_listener::get_instance().set_hup_observer(restore_session);
70 _I("Good bye! %s\n", get_client_name());
74 static initiator g_initiator;
76 static int g_power_save_state_cb_cnt = 0;
78 static void set_power_save_state_cb(void)
80 if (g_power_save_state_cb_cnt < 0)
81 _E("g_power_save_state_cb_cnt(%d) is wrong", g_power_save_state_cb_cnt);
83 ++g_power_save_state_cb_cnt;
85 if (g_power_save_state_cb_cnt == 1) {
86 _D("Power save callback is registered");
87 g_power_save_state = get_power_save_state();
88 _D("power_save_state = [%d]", g_power_save_state);
89 vconf_notify_key_changed(VCONFKEY_PM_STATE, power_save_state_cb, NULL);
90 vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, power_save_state_cb, NULL);
94 static void unset_power_save_state_cb(void)
96 --g_power_save_state_cb_cnt;
98 if (g_power_save_state_cb_cnt < 0)
99 _E("g_power_save_state_cb_cnt(%d) is wrong", g_power_save_state_cb_cnt);
101 if (g_power_save_state_cb_cnt == 0) {
102 _D("Power save callback is unregistered");
103 vconf_ignore_key_changed(VCONFKEY_PM_STATE, power_save_state_cb);
104 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE, power_save_state_cb);
110 handle_vector handles;
112 sensor_client_info::get_instance().get_all_handles(handles);
114 auto it_handle = handles.begin();
116 while (it_handle != handles.end()) {
117 sensord_disconnect(*it_handle);
121 sensor_event_listener::get_instance().clear();
124 static int get_power_save_state(void)
128 int pm_state, ps_state;
130 ret = vconf_get_int(VCONFKEY_PM_STATE, &pm_state);
132 if (!ret && pm_state == VCONFKEY_PM_STATE_LCDOFF)
133 state |= SENSORD_PAUSE_ON_DISPLAY_OFF;
135 ret = vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ps_state);
137 if (!ret && ps_state != SETTING_PSMODE_NORMAL)
138 state |= SENSORD_PAUSE_ON_POWERSAVE_MODE;
143 static void power_save_state_cb(keynode_t *node, void *data)
145 int cur_power_save_state;
146 sensor_id_vector sensors;
147 sensor_rep prev_rep, cur_rep;
151 cur_power_save_state = get_power_save_state();
153 if (cur_power_save_state == g_power_save_state) {
154 _D("g_power_save_state NOT changed : [%d]", cur_power_save_state);
158 g_power_save_state = cur_power_save_state;
159 _D("power_save_state: %d noti to %s", g_power_save_state, get_client_name());
161 sensor_client_info::get_instance().get_listening_sensors(sensors);
163 auto it_sensor = sensors.begin();
165 while (it_sensor != sensors.end()) {
166 sensor_client_info::get_instance().get_sensor_rep(*it_sensor, prev_rep);
167 sensor_client_info::get_instance().set_pause_policy(*it_sensor, cur_power_save_state);
168 sensor_client_info::get_instance().get_sensor_rep(*it_sensor, cur_rep);
169 change_sensor_rep(*it_sensor, prev_rep, cur_rep);
175 bool restore_attributes(int client_id, sensor_id_t sensor, command_channel *cmd_channel)
177 sensor_handle_info_map handle_infos;
179 sensor_client_info::get_instance().get_sensor_handle_info(sensor, handle_infos);
181 for (auto it_handles = handle_infos.begin(); it_handles != handle_infos.end(); ++it_handles) {
182 sensor_handle_info info = it_handles->second;
184 for (auto it = info.attributes_int.begin(); it != info.attributes_int.end(); ++it) {
185 int attribute = it->first;
186 int value = it->second;
187 if (!cmd_channel->cmd_set_attribute_int(attribute, value)) {
188 _E("Failed to send cmd_set_attribute_int(%d, %d) for %s",
189 client_id, value, get_client_name());
194 for (auto it = info.attributes_str.begin(); it != info.attributes_str.end(); ++it) {
195 int attribute = it->first;
196 const char *value = it->second->get();
197 int len = it->second->size();
198 if (!cmd_channel->cmd_set_attribute_str(attribute, value, len)) {
199 _E("Failed to send cmd_set_attribute_str(%d, %d, %s) for %s",
200 client_id, len, value, get_client_name());
209 void restore_session(void)
213 _I("Trying to restore sensor client session for %s", get_client_name());
215 command_channel *cmd_channel;
218 sensor_client_info::get_instance().close_command_channel();
219 sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
221 sensor_id_vector sensors;
223 sensor_client_info::get_instance().get_listening_sensors(sensors);
225 bool first_connection = true;
227 auto it_sensor = sensors.begin();
229 while (it_sensor != sensors.end()) {
230 cmd_channel = new(std::nothrow) command_channel();
231 retm_if(!cmd_channel, "Failed to allocate memory");
233 if (!cmd_channel->create_channel()) {
234 _E("%s failed to create command channel for %s", get_client_name(), get_sensor_name(*it_sensor));
239 sensor_client_info::get_instance().add_command_channel(*it_sensor, cmd_channel);
241 if (first_connection) {
242 first_connection = false;
243 if (!cmd_channel->cmd_get_id(client_id)) {
244 _E("Failed to get client id");
248 sensor_client_info::get_instance().set_client_id(client_id);
249 sensor_event_listener::get_instance().start_event_listener();
252 cmd_channel->set_client_id(client_id);
254 if (!cmd_channel->cmd_hello(*it_sensor)) {
255 _E("Sending cmd_hello(%s, %d) failed for %s", get_sensor_name(*it_sensor), client_id, get_client_name());
259 sensor_rep prev_rep, cur_rep;
260 prev_rep.active = false;
261 prev_rep.pause_policy = SENSORD_PAUSE_ALL;
262 prev_rep.interval = 0;
264 sensor_client_info::get_instance().get_sensor_rep(*it_sensor, cur_rep);
265 if (!change_sensor_rep(*it_sensor, prev_rep, cur_rep)) {
266 _E("Failed to change rep(%s) for %s", get_sensor_name(*it_sensor), get_client_name());
270 if (!restore_attributes(client_id, *it_sensor, cmd_channel)) {
271 _E("Failed to restore attributes(%s) for %s", get_sensor_name(*it_sensor), get_client_name());
277 _I("Succeeded to restore sensor client session for %s", get_client_name());
282 sensor_event_listener::get_instance().clear();
283 _E("Failed to restore session for %s", get_client_name());
286 static bool get_events_diff(event_type_vector &a_vec, event_type_vector &b_vec, event_type_vector &add_vec, event_type_vector &del_vec)
288 sort(a_vec.begin(), a_vec.end());
289 sort(b_vec.begin(), b_vec.end());
291 set_difference(a_vec.begin(), a_vec.end(), b_vec.begin(), b_vec.end(), back_inserter(del_vec));
292 set_difference(b_vec.begin(), b_vec.end(), a_vec.begin(), a_vec.end(), back_inserter(add_vec));
294 return !(add_vec.empty() && del_vec.empty());
297 static bool change_sensor_rep(sensor_id_t sensor_id, sensor_rep &prev_rep, sensor_rep &cur_rep)
300 command_channel *cmd_channel;
301 event_type_vector add_event_types, del_event_types;
303 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
304 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
308 client_id = sensor_client_info::get_instance().get_client_id();
309 retvm_if((client_id < 0), false, "Invalid client id : %d, %s, %s", client_id, get_sensor_name(sensor_id), get_client_name());
311 get_events_diff(prev_rep.event_types, cur_rep.event_types, add_event_types, del_event_types);
313 if (cur_rep.active) {
314 if (prev_rep.pause_policy != cur_rep.pause_policy) {
315 if (!cmd_channel->cmd_set_pause_policy(cur_rep.pause_policy)) {
316 _E("Sending cmd_set_pause_policy(%d, %s, %d) failed for %s", client_id, get_sensor_name(sensor_id), cur_rep.pause_policy, get_client_name());
321 if ( (prev_rep.interval != cur_rep.interval) || (prev_rep.latency != cur_rep.latency)) {
322 if (!cmd_channel->cmd_set_batch(cur_rep.interval, cur_rep.latency)) {
323 _E("Sending cmd_set_batch(%d, %s, %d, %d) failed for %s", client_id, get_sensor_name(sensor_id), cur_rep.interval, cur_rep.latency, get_client_name());
328 if (!add_event_types.empty()) {
329 if (!cmd_channel->cmd_register_events(add_event_types)) {
330 _E("Sending cmd_register_events(%d, add_event_types) failed for %s", client_id, get_client_name());
336 if (prev_rep.active && !del_event_types.empty()) {
337 if (!cmd_channel->cmd_unregister_events(del_event_types)) {
338 _E("Sending cmd_unregister_events(%d, del_event_types) failed for %s", client_id, get_client_name());
343 if (prev_rep.active != cur_rep.active) {
344 if (cur_rep.active) {
345 if (!cmd_channel->cmd_start()) {
346 _E("Sending cmd_start(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
350 if (!cmd_channel->cmd_unset_batch()) {
351 _E("Sending cmd_unset_interval(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
355 if (!cmd_channel->cmd_stop()) {
356 _E("Sending cmd_stop(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
365 static bool get_sensor_list(void)
368 static bool init = false;
373 command_channel cmd_channel;
375 if (!cmd_channel.create_channel()) {
376 _E("%s failed to create command channel", get_client_name());
380 if (!cmd_channel.cmd_get_sensor_list())
389 API int sensord_get_sensors(sensor_type_t type, sensor_t **list, int *sensor_count)
391 retvm_if(!get_sensor_list(), -EPERM, "Fail to get sensor list from server");
393 vector<sensor_info *> sensor_infos = sensor_info_manager::get_instance().get_infos(type);
395 if (sensor_infos.empty()) {
400 if (type != ALL_SENSOR) {
401 if (sensor_infos[0]->get_id() == (sensor_id_t)(-EACCES))
405 *list = (sensor_t *) malloc(sizeof(sensor_info *) * sensor_infos.size());
406 retvm_if(!*list, false, "Failed to allocate memory");
408 for (unsigned int i = 0; i < sensor_infos.size(); ++i)
409 *(*list + i) = sensor_info_to_sensor(sensor_infos[i]);
411 *sensor_count = sensor_infos.size();
416 API int sensord_get_default_sensor(sensor_type_t type, sensor_t *sensor)
418 retvm_if(!get_sensor_list(), -EPERM, "Fail to get sensor list from server");
420 const sensor_info *info;
422 info = sensor_info_manager::get_instance().get_info(type);
428 if ((const_cast<sensor_info *>(info))->get_id() == (sensor_id_t)(-EACCES))
431 *sensor = sensor_info_to_sensor(info);
436 API bool sensord_get_sensor_list(sensor_type_t type, sensor_t **list, int *sensor_count)
438 return (sensord_get_sensors(type, list, sensor_count) == OP_SUCCESS);
441 API sensor_t sensord_get_sensor(sensor_type_t type)
444 sensord_get_default_sensor(type, &sensor);
449 API bool sensord_get_type(sensor_t sensor, sensor_type_t *type)
451 sensor_info* info = sensor_to_sensor_info(sensor);
453 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !type,
454 NULL, "Invalid param: sensor (%p), type(%p)", sensor, type);
456 *type = info->get_type();
461 API const char* sensord_get_name(sensor_t sensor)
463 sensor_info* info = sensor_to_sensor_info(sensor);
465 retvm_if(!sensor_info_manager::get_instance().is_valid(info),
466 NULL, "Invalid param: sensor (%p)", sensor);
468 return info->get_name();
471 API const char* sensord_get_vendor(sensor_t sensor)
473 sensor_info* info = sensor_to_sensor_info(sensor);
475 retvm_if(!sensor_info_manager::get_instance().is_valid(info),
476 NULL, "Invalid param: sensor (%p)", sensor);
478 return info->get_vendor();
481 API bool sensord_get_privilege(sensor_t sensor, sensor_privilege_t *privilege)
483 sensor_info* info = sensor_to_sensor_info(sensor);
485 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !privilege,
486 false, "Invalid param: sensor (%p), privilege(%p)", sensor, privilege);
488 *privilege = info->get_privilege();
493 API bool sensord_get_min_range(sensor_t sensor, float *min_range)
495 sensor_info* info = sensor_to_sensor_info(sensor);
497 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !min_range,
498 false, "Invalid param: sensor (%p), min_range(%p)", sensor, min_range);
500 *min_range = info->get_min_range();
505 API bool sensord_get_max_range(sensor_t sensor, float *max_range)
507 sensor_info* info = sensor_to_sensor_info(sensor);
509 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !max_range,
510 false, "Invalid param: sensor (%p), max_range(%p)", sensor, max_range);
512 *max_range = info->get_max_range();
517 API bool sensord_get_resolution(sensor_t sensor, float *resolution)
519 sensor_info* info = sensor_to_sensor_info(sensor);
521 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !resolution,
522 false, "Invalid param: sensor (%p), resolution(%p)", sensor, resolution);
524 *resolution = info->get_resolution();
529 API bool sensord_get_min_interval(sensor_t sensor, int *min_interval)
531 sensor_info* info = sensor_to_sensor_info(sensor);
533 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !min_interval,
534 false, "Invalid param: sensor (%p), min_interval(%p)", sensor, min_interval);
536 *min_interval = info->get_min_interval();
541 API bool sensord_get_fifo_count(sensor_t sensor, int *fifo_count)
543 sensor_info* info = sensor_to_sensor_info(sensor);
545 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !fifo_count,
546 false, "Invalid param: sensor (%p), fifo_count(%p)", sensor, fifo_count);
548 *fifo_count = info->get_fifo_count();
553 API bool sensord_get_max_batch_count(sensor_t sensor, int *max_batch_count)
555 sensor_info* info = sensor_to_sensor_info(sensor);
557 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !max_batch_count,
558 false, "Invalid param: sensor (%p), max_batch_count(%p)", sensor, max_batch_count);
560 *max_batch_count = info->get_max_batch_count();
565 API bool sensord_get_supported_event_types(sensor_t sensor, unsigned int **event_types, int *count)
567 sensor_info* info = sensor_to_sensor_info(sensor);
569 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !event_types || !count,
570 false, "Invalid param: sensor (%p), event_types(%p), count(%p)", sensor, event_types, count);
572 unsigned int event_type;
573 event_type = info->get_supported_event();
574 *event_types = (unsigned int *)malloc(sizeof(unsigned int));
576 retvm_if(!*event_types, false, "Failed to allocate memory");
578 (*event_types)[0] = event_type;
584 API bool sensord_is_supported_event_type(sensor_t sensor, unsigned int event_type, bool *supported)
586 sensor_info* info = sensor_to_sensor_info(sensor);
588 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !event_type || !supported,
589 false, "Invalid param: sensor (%p), event_type(%p), supported(%p)", sensor, event_type, supported);
591 *supported = info->is_supported_event(event_type);
596 API bool sensord_is_wakeup_supported(sensor_t sensor)
598 sensor_info* info = sensor_to_sensor_info(sensor);
600 retvm_if(!sensor_info_manager::get_instance().is_valid(info),
601 false, "Invalid param: sensor (%p)", sensor);
603 return info->is_wakeup_supported();
606 API int sensord_connect(sensor_t sensor)
608 command_channel *cmd_channel = NULL;
611 bool sensor_registered;
612 bool first_connection = false;
614 sensor_info* info = sensor_to_sensor_info(sensor);
616 retvm_if(!sensor_info_manager::get_instance().is_valid(info),
617 OP_ERROR, "Invalid param: sensor (%p)", sensor);
619 sensor_id_t sensor_id = info->get_id();
623 sensor_registered = sensor_client_info::get_instance().is_sensor_registered(sensor_id);
625 // lazy loading after creating static variables
628 handle = sensor_client_info::get_instance().create_handle(sensor_id);
629 if (handle == MAX_HANDLE) {
630 _E("Maximum number of handles reached, sensor: %s in client %s", get_sensor_name(sensor_id), get_client_name());
634 if (!sensor_registered) {
635 cmd_channel = new(std::nothrow) command_channel();
637 _E("Failed to allocated memory");
638 sensor_client_info::get_instance().delete_handle(handle);
642 if (!cmd_channel->create_channel()) {
643 _E("%s failed to create command channel for %s", get_client_name(), get_sensor_name(sensor_id));
644 sensor_client_info::get_instance().delete_handle(handle);
649 sensor_client_info::get_instance().add_command_channel(sensor_id, cmd_channel);
652 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
653 _E("%s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
654 sensor_client_info::get_instance().delete_handle(handle);
658 if (!sensor_client_info::get_instance().has_client_id()) {
659 first_connection = true;
660 if (!cmd_channel->cmd_get_id(client_id)) {
661 _E("Sending cmd_get_id() failed for %s", get_sensor_name(sensor_id));
662 sensor_client_info::get_instance().close_command_channel(sensor_id);
663 sensor_client_info::get_instance().delete_handle(handle);
667 sensor_client_info::get_instance().set_client_id(client_id);
668 _I("%s gets client_id [%d]", get_client_name(), client_id);
669 sensor_event_listener::get_instance().start_event_listener();
670 _I("%s starts listening events with client_id [%d]", get_client_name(), client_id);
673 client_id = sensor_client_info::get_instance().get_client_id();
674 cmd_channel->set_client_id(client_id);
676 _I("%s[%d] connects with %s[%d]", get_client_name(), client_id, get_sensor_name(sensor_id), handle);
678 sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_STOPPED, SENSORD_PAUSE_ALL);
680 if (!sensor_registered) {
681 if (!cmd_channel->cmd_hello(sensor_id)) {
682 _E("Sending cmd_hello(%s, %d) failed for %s", get_sensor_name(sensor_id), client_id, get_client_name());
683 sensor_client_info::get_instance().close_command_channel(sensor_id);
684 sensor_client_info::get_instance().delete_handle(handle);
685 if (first_connection) {
686 sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
687 sensor_event_listener::get_instance().stop_event_listener();
693 set_power_save_state_cb();
694 dbus_listener::init();
698 API bool sensord_disconnect(int handle)
700 command_channel *cmd_channel;
701 sensor_id_t sensor_id;
707 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
708 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
709 _E("client %s failed to get handle information", get_client_name());
713 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
714 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
718 client_id = sensor_client_info::get_instance().get_client_id();
719 retvm_if((client_id < 0), false, "Invalid client id : %d, handle: %d, %s, %s", client_id, handle, get_sensor_name(sensor_id), get_client_name());
721 _I("%s disconnects with %s[%d]", get_client_name(), get_sensor_name(sensor_id), handle);
723 if (sensor_client_info::get_instance().get_passive_mode(handle)) {
724 _W("%s[%d] for %s is on passive mode while disconnecting.",
725 get_sensor_name(sensor_id), handle, get_client_name());
727 command_channel *cmd_channel;
728 event_type_vector event_types;
729 sensor_client_info::get_instance().get_active_event_types(sensor_id, event_types);
731 for (auto it = event_types.begin(); it != event_types.end(); ++it)
732 sensord_unregister_event(handle, *it);
734 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
735 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
739 if (!cmd_channel->cmd_unset_batch()) {
740 _E("Sending cmd_unset_interval(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
745 if (sensor_state != SENSOR_STATE_STOPPED) {
746 _W("%s[%d] for %s is not stopped before disconnecting.",
747 get_sensor_name(sensor_id), handle, get_client_name());
748 sensord_stop(handle);
751 if (!sensor_client_info::get_instance().delete_handle(handle))
754 if (!sensor_client_info::get_instance().is_active())
755 sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
757 if (!sensor_client_info::get_instance().is_sensor_registered(sensor_id)) {
758 if (!cmd_channel->cmd_byebye()) {
759 _E("Sending cmd_byebye(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
762 sensor_client_info::get_instance().close_command_channel(sensor_id);
765 if (!sensor_client_info::get_instance().is_active()) {
766 _I("Stop listening events for client %s with client id [%d]", get_client_name(), sensor_client_info::get_instance().get_client_id());
767 sensor_event_listener::get_instance().stop_event_listener();
770 unset_power_save_state_cb();
775 static bool register_event(int handle, unsigned int event_type, unsigned int interval, int max_batch_latency, void* cb, void *user_data)
777 sensor_id_t sensor_id;
778 sensor_rep prev_rep, cur_rep;
781 retvm_if(!cb, false, "callback is NULL");
785 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
786 _E("client %s failed to get handle information", get_client_name());
791 interval = DEFAULT_INTERVAL;
793 _I("%s registers event %s[%#x] for sensor %s[%d] with interval: %d, latency: %d, cb: %#x, user_data: %#x",
794 get_client_name(), get_event_name(event_type), event_type, get_sensor_name(sensor_id),
795 handle, interval, max_batch_latency, cb, user_data);
797 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
798 sensor_client_info::get_instance().register_event(handle, event_type, interval, max_batch_latency, cb, user_data);
799 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
800 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
803 sensor_client_info::get_instance().unregister_event(handle, event_type);
808 API bool sensord_register_event(int handle, unsigned int event_type, unsigned int interval, unsigned int max_batch_latency, sensor_cb_t cb, void *user_data)
810 return register_event(handle, event_type, interval, max_batch_latency, (void *)cb, user_data);
813 API bool sensord_unregister_event(int handle, unsigned int event_type)
815 sensor_id_t sensor_id;
816 sensor_rep prev_rep, cur_rep;
818 unsigned int prev_interval, prev_latency;
820 void *prev_user_data;
824 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
825 _E("client %s failed to get handle information", get_client_name());
829 _I("%s unregisters event %s[%#x] for sensor %s[%d]", get_client_name(), get_event_name(event_type),
830 event_type, get_sensor_name(sensor_id), handle);
832 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
833 sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data);
835 if (!sensor_client_info::get_instance().unregister_event(handle, event_type)) {
836 _E("%s try to unregister non registered event %s[%#x] for sensor %s[%d]",
837 get_client_name(), get_event_name(event_type), event_type, get_sensor_name(sensor_id), handle);
841 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
842 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
844 if (sensor_client_info::get_instance().get_passive_mode(handle))
845 sensor_client_info::get_instance().set_passive_mode(handle, false);
848 sensor_client_info::get_instance().register_event(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data);
853 API bool sensord_register_accuracy_cb(int handle, sensor_accuracy_changed_cb_t cb, void *user_data)
855 sensor_id_t sensor_id;
857 retvm_if(!cb, false, "callback is NULL");
861 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
862 _E("client %s failed to get handle information", get_client_name());
866 _I("%s registers accuracy_changed_cb for sensor %s[%d] with cb: %#x, user_data: %#x",
867 get_client_name(), get_sensor_name(sensor_id), handle, cb, user_data);
869 sensor_client_info::get_instance().register_accuracy_cb(handle, cb , user_data);
874 API bool sensord_unregister_accuracy_cb(int handle)
876 sensor_id_t sensor_id;
880 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
881 _E("client %s failed to get handle information", get_client_name());
885 _I("%s unregisters accuracy_changed_cb for sensor %s[%d]",
886 get_client_name(), get_sensor_name(sensor_id), handle);
888 sensor_client_info::get_instance().unregister_accuracy_cb(handle);
893 API bool sensord_start(int handle, int option)
895 sensor_id_t sensor_id;
896 sensor_rep prev_rep, cur_rep;
898 int prev_state, prev_pause;
903 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
904 _E("client %s failed to get handle information", get_client_name());
908 retvm_if((option < 0) || (option >= SENSOR_OPTION_END), false, "Invalid option value : %d, handle: %d, %s, %s",
909 option, handle, get_sensor_name(sensor_id), get_client_name());
911 _I("%s starts %s[%d], with option: %d, power save state: %d", get_client_name(), get_sensor_name(sensor_id),
912 handle, option, g_power_save_state);
914 pause = CONVERT_OPTION_PAUSE_POLICY(option);
916 if (g_power_save_state && (g_power_save_state & pause)) {
917 sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_PAUSED, pause);
921 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
922 sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_pause);
923 sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_STARTED, pause);
924 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
926 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
929 sensor_client_info::get_instance().set_sensor_params(handle, prev_state, prev_pause);
934 API bool sensord_stop(int handle)
936 sensor_id_t sensor_id;
939 int prev_state, prev_pause;
941 sensor_rep prev_rep, cur_rep;
945 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
946 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
947 _E("client %s failed to get handle information", get_client_name());
951 retvm_if((sensor_state == SENSOR_STATE_STOPPED), true, "%s already stopped with %s[%d]",
952 get_client_name(), get_sensor_name(sensor_id), handle);
954 _I("%s stops sensor %s[%d]", get_client_name(), get_sensor_name(sensor_id), handle);
956 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
957 sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_pause);
958 sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_STOPPED);
959 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
961 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
964 sensor_client_info::get_instance().set_sensor_params(handle, prev_state, prev_pause);
969 static bool change_event_batch(int handle, unsigned int event_type, unsigned int interval, unsigned int latency)
971 sensor_id_t sensor_id;
972 sensor_rep prev_rep, cur_rep;
974 unsigned int prev_interval, prev_latency;
976 void *prev_user_data;
980 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
981 _E("client %s failed to get handle information", get_client_name());
986 interval = DEFAULT_INTERVAL;
988 _I("%s changes batch of event %s[%#x] for %s[%d] to (%d, %d)", get_client_name(), get_event_name(event_type),
989 event_type, get_sensor_name(sensor_id), handle, interval, latency);
991 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
993 sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data);
995 if (!sensor_client_info::get_instance().set_event_batch(handle, event_type, interval, latency))
998 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
1000 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
1003 sensor_client_info::get_instance().set_event_batch(handle, event_type, prev_interval, prev_latency);
1008 API bool sensord_change_event_interval(int handle, unsigned int event_type, unsigned int interval)
1010 unsigned int prev_interval, prev_latency;
1012 void *prev_user_data;
1016 if (!sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data)) {
1017 _E("Failed to get event info with handle = %d, event_type = %#x", handle, event_type);
1021 _I("handle = %d, event_type = %#x, interval = %d, prev_latency = %d", handle, event_type, interval, prev_latency);
1022 return change_event_batch(handle, event_type, interval, prev_latency);
1025 API bool sensord_change_event_max_batch_latency(int handle, unsigned int event_type, unsigned int max_batch_latency)
1027 unsigned int prev_interval, prev_latency;
1029 void *prev_user_data;
1033 if (!sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data)) {
1034 _E("Failed to get event info with handle = %d, event_type = %#x", handle, event_type);
1038 return change_event_batch(handle, event_type, prev_interval, max_batch_latency);
1041 static int change_pause_policy(int handle, int pause)
1043 sensor_id_t sensor_id;
1044 sensor_rep prev_rep, cur_rep;
1047 int prev_state, prev_pause;
1051 retvm_if((pause < 0) || (pause >= SENSORD_PAUSE_END), -EINVAL,
1052 "Invalid pause value : %d, handle: %d, %s, %s",
1053 pause, handle, get_sensor_name(sensor_id), get_client_name());
1055 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
1056 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1057 _E("client %s failed to get handle information", get_client_name());
1061 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
1062 sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_pause);
1064 if (g_power_save_state) {
1065 if ((pause & g_power_save_state) && (sensor_state == SENSOR_STATE_STARTED))
1066 sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_PAUSED);
1067 else if (!(pause & g_power_save_state) && (sensor_state == SENSOR_STATE_PAUSED))
1068 sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_STARTED);
1070 sensor_client_info::get_instance().set_sensor_pause_policy(handle, pause);
1072 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
1073 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
1076 sensor_client_info::get_instance().set_sensor_pause_policy(handle, prev_pause);
1078 return (ret ? OP_SUCCESS : OP_ERROR);
1081 static int change_axis_orientation(int handle, int axis_orientation)
1083 sensor_event_listener::get_instance().set_sensor_axis(axis_orientation);
1087 static int change_attribute_int(int handle, int attribute, int value)
1089 sensor_id_t sensor_id;
1090 command_channel *cmd_channel;
1095 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1096 _E("client %s failed to get handle information", get_client_name());
1100 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1101 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
1105 client_id = sensor_client_info::get_instance().get_client_id();
1106 retvm_if((client_id < 0), -EPERM,
1107 "Invalid client id : %d, handle: %d, %s, %s",
1108 client_id, handle, get_sensor_name(sensor_id), get_client_name());
1110 if (!cmd_channel->cmd_set_attribute_int(attribute, value)) {
1111 _E("Sending cmd_set_attribute_int(%d, %d) failed for %s",
1112 client_id, value, get_client_name());
1116 sensor_client_info::get_instance().set_attribute(handle, attribute, value);
1121 API bool sensord_set_option(int handle, int option)
1123 return (change_pause_policy(handle, CONVERT_OPTION_PAUSE_POLICY(option)) == OP_SUCCESS);
1126 API int sensord_set_attribute_int(int handle, int attribute, int value)
1128 switch (attribute) {
1129 case SENSORD_ATTRIBUTE_PAUSE_POLICY:
1130 return change_pause_policy(handle, value);
1131 case SENSORD_ATTRIBUTE_AXIS_ORIENTATION:
1132 return change_axis_orientation(handle, value);
1137 return change_attribute_int(handle, attribute, value);
1140 API int sensord_set_attribute_str(int handle, int attribute, const char *value, int value_len)
1142 sensor_id_t sensor_id;
1143 command_channel *cmd_channel;
1148 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1149 _E("Client %s failed to get handle information", get_client_name());
1153 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1154 _E("Client %s failed to get command channel for %s",
1155 get_client_name(), get_sensor_name(sensor_id));
1159 retvm_if((value_len < 0) || (value == NULL), -EINVAL,
1160 "Invalid value_len: %d, value: %#x, handle: %d, %s, %s",
1161 value_len, value, handle, get_sensor_name(sensor_id), get_client_name());
1163 client_id = sensor_client_info::get_instance().get_client_id();
1164 retvm_if((client_id < 0), -EPERM,
1165 "Invalid client id : %d, handle: %d, %s, %s",
1166 client_id, handle, get_sensor_name(sensor_id), get_client_name());
1168 if (!cmd_channel->cmd_set_attribute_str(attribute, value, value_len)) {
1169 _E("Sending cmd_set_attribute_str(%d, %d, %#x) failed for %s",
1170 client_id, value_len, value, get_client_name());
1174 sensor_client_info::get_instance().set_attribute(handle, attribute, value, value_len);
1179 API bool sensord_send_sensorhub_data(int handle, const char *data, int data_len)
1181 return (sensord_set_attribute_str(handle, 0, data, data_len) == OP_SUCCESS);
1184 API bool sensord_send_command(int handle, const char *command, int command_len)
1186 return (sensord_set_attribute_str(handle, 0, command, command_len) == OP_SUCCESS);
1189 API bool sensord_get_data(int handle, unsigned int data_id, sensor_data_t* sensor_data)
1191 sensor_id_t sensor_id;
1192 command_channel *cmd_channel;
1196 retvm_if((!sensor_data), false, "sensor_data is NULL");
1200 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
1201 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1202 _E("client %s failed to get handle information", get_client_name());
1206 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1207 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
1211 client_id = sensor_client_info::get_instance().get_client_id();
1212 retvm_if((client_id < 0), false, "Invalid client id : %d, handle: %d, %s, %s", client_id, handle, get_sensor_name(sensor_id), get_client_name());
1214 if (sensor_state != SENSOR_STATE_STARTED) {
1215 _E("Sensor %s is not started for client %s with handle: %d, sensor_state: %d", get_sensor_name(sensor_id), get_client_name(), handle, sensor_state);
1219 if (!cmd_channel->cmd_get_data(data_id, sensor_data)) {
1220 _E("cmd_get_data(%d, %d, %#x) failed for %s", client_id, data_id, sensor_data, get_client_name());
1227 API bool sensord_flush(int handle)
1229 sensor_id_t sensor_id;
1230 command_channel *cmd_channel;
1236 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state) ||
1237 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1238 _E("client %s failed to get handle information", get_client_name());
1242 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1243 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
1247 client_id = sensor_client_info::get_instance().get_client_id();
1248 retvm_if((client_id < 0), false, "Invalid client id : %d, handle: %d, %s, %s", client_id, handle, get_sensor_name(sensor_id), get_client_name());
1250 if (sensor_state != SENSOR_STATE_STARTED) {
1251 _E("Sensor %s is not started for client %s with handle: %d, sensor_state: %d", get_sensor_name(sensor_id), get_client_name(), handle, sensor_state);
1255 if (!cmd_channel->cmd_flush()) {
1256 _E("cmd_flush() failed for %s", get_client_name());
1263 API bool sensord_register_hub_event(int handle, unsigned int event_type, unsigned int interval, unsigned int max_batch_latency, sensorhub_cb_t cb, void *user_data)
1268 API bool sensord_set_passive_mode(int handle, bool passive)
1270 if (!sensor_client_info::get_instance().set_passive_mode(handle, passive)) {
1271 _E("Failed to set passive mode %d", passive);