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);
122 static int get_power_save_state(void)
126 int pm_state, ps_state;
128 ret = vconf_get_int(VCONFKEY_PM_STATE, &pm_state);
130 if (!ret && pm_state == VCONFKEY_PM_STATE_LCDOFF)
131 state |= SENSORD_PAUSE_ON_DISPLAY_OFF;
133 ret = vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ps_state);
135 if (!ret && ps_state != SETTING_PSMODE_NORMAL)
136 state |= SENSORD_PAUSE_ON_POWERSAVE_MODE;
141 static void power_save_state_cb(keynode_t *node, void *data)
143 int cur_power_save_state;
144 sensor_id_vector sensors;
145 sensor_rep prev_rep, cur_rep;
149 cur_power_save_state = get_power_save_state();
151 if (cur_power_save_state == g_power_save_state) {
152 _D("g_power_save_state NOT changed : [%d]", cur_power_save_state);
156 g_power_save_state = cur_power_save_state;
157 _D("power_save_state: %d noti to %s", g_power_save_state, get_client_name());
159 sensor_client_info::get_instance().get_listening_sensors(sensors);
161 auto it_sensor = sensors.begin();
163 while (it_sensor != sensors.end()) {
164 sensor_client_info::get_instance().get_sensor_rep(*it_sensor, prev_rep);
165 sensor_client_info::get_instance().set_pause_policy(*it_sensor, cur_power_save_state);
166 sensor_client_info::get_instance().get_sensor_rep(*it_sensor, cur_rep);
167 change_sensor_rep(*it_sensor, prev_rep, cur_rep);
173 bool restore_attributes(int client_id, sensor_id_t sensor, command_channel *cmd_channel)
175 sensor_handle_info_map handle_infos;
177 sensor_client_info::get_instance().get_sensor_handle_info(sensor, handle_infos);
179 for (auto it_handles = handle_infos.begin(); it_handles != handle_infos.end(); ++it_handles) {
180 sensor_handle_info info = it_handles->second;
182 for (auto it = info.attributes_int.begin(); it != info.attributes_int.end(); ++it) {
183 int attribute = it->first;
184 int value = it->second;
185 if (!cmd_channel->cmd_set_attribute_int(attribute, value)) {
186 _E("Failed to send cmd_set_attribute_int(%d, %d) for %s",
187 client_id, value, get_client_name());
192 for (auto it = info.attributes_str.begin(); it != info.attributes_str.end(); ++it) {
193 int attribute = it->first;
194 const char *value = it->second.c_str();
195 int value_len = it->second.size();
196 if (!cmd_channel->cmd_set_attribute_str(attribute, value, value_len)) {
197 _E("Failed to send cmd_set_attribute_str(%d, %d, %s) for %s",
198 client_id, value_len, value, get_client_name());
207 void restore_session(void)
211 _I("Trying to restore sensor client session for %s", get_client_name());
213 command_channel *cmd_channel;
216 sensor_client_info::get_instance().close_command_channel();
217 sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
219 sensor_id_vector sensors;
221 sensor_client_info::get_instance().get_listening_sensors(sensors);
223 bool first_connection = true;
225 auto it_sensor = sensors.begin();
227 while (it_sensor != sensors.end()) {
228 cmd_channel = new(std::nothrow) command_channel();
229 retm_if(!cmd_channel, "Failed to allocate memory");
231 if (!cmd_channel->create_channel()) {
232 _E("%s failed to create command channel for %s", get_client_name(), get_sensor_name(*it_sensor));
237 sensor_client_info::get_instance().add_command_channel(*it_sensor, cmd_channel);
239 if (first_connection) {
240 first_connection = false;
241 if (!cmd_channel->cmd_get_id(client_id)) {
242 _E("Failed to get client id");
246 sensor_client_info::get_instance().set_client_id(client_id);
247 sensor_event_listener::get_instance().start_event_listener();
250 cmd_channel->set_client_id(client_id);
252 if (!cmd_channel->cmd_hello(*it_sensor)) {
253 _E("Sending cmd_hello(%s, %d) failed for %s", get_sensor_name(*it_sensor), client_id, get_client_name());
257 sensor_rep prev_rep, cur_rep;
258 prev_rep.active = false;
259 prev_rep.pause_policy = SENSORD_PAUSE_ALL;
260 prev_rep.interval = 0;
262 sensor_client_info::get_instance().get_sensor_rep(*it_sensor, cur_rep);
263 if (!change_sensor_rep(*it_sensor, prev_rep, cur_rep)) {
264 _E("Failed to change rep(%s) for %s", get_sensor_name(*it_sensor), get_client_name());
268 if (!restore_attributes(client_id, *it_sensor, cmd_channel)) {
269 _E("Failed to restore attributes(%s) for %s", get_sensor_name(*it_sensor), get_client_name());
275 _I("Succeeded to restore sensor client session for %s", get_client_name());
280 sensor_event_listener::get_instance().clear();
281 _E("Failed to restore session for %s", get_client_name());
284 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)
286 sort(a_vec.begin(), a_vec.end());
287 sort(b_vec.begin(), b_vec.end());
289 set_difference(a_vec.begin(), a_vec.end(), b_vec.begin(), b_vec.end(), back_inserter(del_vec));
290 set_difference(b_vec.begin(), b_vec.end(), a_vec.begin(), a_vec.end(), back_inserter(add_vec));
292 return !(add_vec.empty() && del_vec.empty());
295 static bool change_sensor_rep(sensor_id_t sensor_id, sensor_rep &prev_rep, sensor_rep &cur_rep)
298 command_channel *cmd_channel;
299 event_type_vector add_event_types, del_event_types;
301 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
302 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
306 client_id = sensor_client_info::get_instance().get_client_id();
307 retvm_if((client_id < 0), false, "Invalid client id : %d, %s, %s", client_id, get_sensor_name(sensor_id), get_client_name());
309 get_events_diff(prev_rep.event_types, cur_rep.event_types, add_event_types, del_event_types);
311 if (cur_rep.active) {
312 if (prev_rep.pause_policy != cur_rep.pause_policy) {
313 if (!cmd_channel->cmd_set_pause_policy(cur_rep.pause_policy)) {
314 _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());
319 if ( (prev_rep.interval != cur_rep.interval) || (prev_rep.latency != cur_rep.latency)) {
320 if (!cmd_channel->cmd_set_batch(cur_rep.interval, cur_rep.latency)) {
321 _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());
326 if (!add_event_types.empty()) {
327 if (!cmd_channel->cmd_register_events(add_event_types)) {
328 _E("Sending cmd_register_events(%d, add_event_types) failed for %s", client_id, get_client_name());
334 if (prev_rep.active && !del_event_types.empty()) {
335 if (!cmd_channel->cmd_unregister_events(del_event_types)) {
336 _E("Sending cmd_unregister_events(%d, del_event_types) failed for %s", client_id, get_client_name());
341 if (prev_rep.active != cur_rep.active) {
342 if (cur_rep.active) {
343 if (!cmd_channel->cmd_start()) {
344 _E("Sending cmd_start(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
348 if (!cmd_channel->cmd_unset_batch()) {
349 _E("Sending cmd_unset_interval(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
353 if (!cmd_channel->cmd_stop()) {
354 _E("Sending cmd_stop(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
363 static bool get_sensor_list(void)
366 static bool init = false;
371 command_channel cmd_channel;
373 if (!cmd_channel.create_channel()) {
374 _E("%s failed to create command channel", get_client_name());
378 if (!cmd_channel.cmd_get_sensor_list())
387 API int sensord_get_sensors(sensor_type_t type, sensor_t **list, int *sensor_count)
389 retvm_if(!get_sensor_list(), -EPERM, "Fail to get sensor list from server");
391 vector<sensor_info *> sensor_infos = sensor_info_manager::get_instance().get_infos(type);
393 if (sensor_infos.empty()) {
398 if (type != ALL_SENSOR) {
399 if (sensor_infos[0]->get_id() == (sensor_id_t)(-EACCES))
403 *list = (sensor_t *) malloc(sizeof(sensor_info *) * sensor_infos.size());
404 retvm_if(!*list, false, "Failed to allocate memory");
406 for (unsigned int i = 0; i < sensor_infos.size(); ++i)
407 *(*list + i) = sensor_info_to_sensor(sensor_infos[i]);
409 *sensor_count = sensor_infos.size();
414 API int sensord_get_default_sensor(sensor_type_t type, sensor_t *sensor)
416 retvm_if(!get_sensor_list(), -EPERM, "Fail to get sensor list from server");
418 const sensor_info *info;
420 info = sensor_info_manager::get_instance().get_info(type);
426 if ((const_cast<sensor_info *>(info))->get_id() == (sensor_id_t)(-EACCES))
429 *sensor = sensor_info_to_sensor(info);
434 API bool sensord_get_sensor_list(sensor_type_t type, sensor_t **list, int *sensor_count)
436 return (sensord_get_sensors(type, list, sensor_count) == OP_SUCCESS);
439 API sensor_t sensord_get_sensor(sensor_type_t type)
442 sensord_get_default_sensor(type, &sensor);
447 API bool sensord_get_type(sensor_t sensor, sensor_type_t *type)
449 sensor_info* info = sensor_to_sensor_info(sensor);
451 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !type,
452 NULL, "Invalid param: sensor (%p), type(%p)", sensor, type);
454 *type = info->get_type();
459 API const char* sensord_get_name(sensor_t sensor)
461 sensor_info* info = sensor_to_sensor_info(sensor);
463 retvm_if(!sensor_info_manager::get_instance().is_valid(info),
464 NULL, "Invalid param: sensor (%p)", sensor);
466 return info->get_name();
469 API const char* sensord_get_vendor(sensor_t sensor)
471 sensor_info* info = sensor_to_sensor_info(sensor);
473 retvm_if(!sensor_info_manager::get_instance().is_valid(info),
474 NULL, "Invalid param: sensor (%p)", sensor);
476 return info->get_vendor();
479 API bool sensord_get_privilege(sensor_t sensor, sensor_privilege_t *privilege)
481 sensor_info* info = sensor_to_sensor_info(sensor);
483 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !privilege,
484 false, "Invalid param: sensor (%p), privilege(%p)", sensor, privilege);
486 *privilege = info->get_privilege();
491 API bool sensord_get_min_range(sensor_t sensor, float *min_range)
493 sensor_info* info = sensor_to_sensor_info(sensor);
495 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !min_range,
496 false, "Invalid param: sensor (%p), min_range(%p)", sensor, min_range);
498 *min_range = info->get_min_range();
503 API bool sensord_get_max_range(sensor_t sensor, float *max_range)
505 sensor_info* info = sensor_to_sensor_info(sensor);
507 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !max_range,
508 false, "Invalid param: sensor (%p), max_range(%p)", sensor, max_range);
510 *max_range = info->get_max_range();
515 API bool sensord_get_resolution(sensor_t sensor, float *resolution)
517 sensor_info* info = sensor_to_sensor_info(sensor);
519 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !resolution,
520 false, "Invalid param: sensor (%p), resolution(%p)", sensor, resolution);
522 *resolution = info->get_resolution();
527 API bool sensord_get_min_interval(sensor_t sensor, int *min_interval)
529 sensor_info* info = sensor_to_sensor_info(sensor);
531 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !min_interval,
532 false, "Invalid param: sensor (%p), min_interval(%p)", sensor, min_interval);
534 *min_interval = info->get_min_interval();
539 API bool sensord_get_fifo_count(sensor_t sensor, int *fifo_count)
541 sensor_info* info = sensor_to_sensor_info(sensor);
543 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !fifo_count,
544 false, "Invalid param: sensor (%p), fifo_count(%p)", sensor, fifo_count);
546 *fifo_count = info->get_fifo_count();
551 API bool sensord_get_max_batch_count(sensor_t sensor, int *max_batch_count)
553 sensor_info* info = sensor_to_sensor_info(sensor);
555 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !max_batch_count,
556 false, "Invalid param: sensor (%p), max_batch_count(%p)", sensor, max_batch_count);
558 *max_batch_count = info->get_max_batch_count();
563 API bool sensord_get_supported_event_types(sensor_t sensor, unsigned int **event_types, int *count)
565 sensor_info* info = sensor_to_sensor_info(sensor);
567 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !event_types || !count,
568 false, "Invalid param: sensor (%p), event_types(%p), count(%p)", sensor, event_types, count);
570 unsigned int event_type;
571 event_type = info->get_supported_event();
572 *event_types = (unsigned int *)malloc(sizeof(unsigned int));
574 retvm_if(!*event_types, false, "Failed to allocate memory");
576 (*event_types)[0] = event_type;
582 API bool sensord_is_supported_event_type(sensor_t sensor, unsigned int event_type, bool *supported)
584 sensor_info* info = sensor_to_sensor_info(sensor);
586 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !event_type || !supported,
587 false, "Invalid param: sensor (%p), event_type(%p), supported(%p)", sensor, event_type, supported);
589 *supported = info->is_supported_event(event_type);
594 API bool sensord_is_wakeup_supported(sensor_t sensor)
596 sensor_info* info = sensor_to_sensor_info(sensor);
598 retvm_if(!sensor_info_manager::get_instance().is_valid(info),
599 false, "Invalid param: sensor (%p)", sensor);
601 return info->is_wakeup_supported();
604 API int sensord_connect(sensor_t sensor)
606 command_channel *cmd_channel = NULL;
609 bool sensor_registered;
610 bool first_connection = false;
612 sensor_info* info = sensor_to_sensor_info(sensor);
614 retvm_if(!sensor_info_manager::get_instance().is_valid(info),
615 OP_ERROR, "Invalid param: sensor (%p)", sensor);
617 sensor_id_t sensor_id = info->get_id();
621 sensor_registered = sensor_client_info::get_instance().is_sensor_registered(sensor_id);
623 // lazy loading after creating static variables
626 handle = sensor_client_info::get_instance().create_handle(sensor_id);
627 if (handle == MAX_HANDLE) {
628 _E("Maximum number of handles reached, sensor: %s in client %s", get_sensor_name(sensor_id), get_client_name());
632 if (!sensor_registered) {
633 cmd_channel = new(std::nothrow) command_channel();
635 _E("Failed to allocated memory");
636 sensor_client_info::get_instance().delete_handle(handle);
640 if (!cmd_channel->create_channel()) {
641 _E("%s failed to create command channel for %s", get_client_name(), get_sensor_name(sensor_id));
642 sensor_client_info::get_instance().delete_handle(handle);
647 sensor_client_info::get_instance().add_command_channel(sensor_id, cmd_channel);
650 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
651 _E("%s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
652 sensor_client_info::get_instance().delete_handle(handle);
656 if (!sensor_client_info::get_instance().has_client_id()) {
657 first_connection = true;
658 if (!cmd_channel->cmd_get_id(client_id)) {
659 _E("Sending cmd_get_id() failed for %s", get_sensor_name(sensor_id));
660 sensor_client_info::get_instance().close_command_channel(sensor_id);
661 sensor_client_info::get_instance().delete_handle(handle);
665 sensor_client_info::get_instance().set_client_id(client_id);
666 _I("%s gets client_id [%d]", get_client_name(), client_id);
667 sensor_event_listener::get_instance().start_event_listener();
668 _I("%s starts listening events with client_id [%d]", get_client_name(), client_id);
671 client_id = sensor_client_info::get_instance().get_client_id();
672 cmd_channel->set_client_id(client_id);
674 _I("%s[%d] connects with %s[%d]", get_client_name(), client_id, get_sensor_name(sensor_id), handle);
676 sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_STOPPED, SENSORD_PAUSE_ALL);
678 if (!sensor_registered) {
679 if (!cmd_channel->cmd_hello(sensor_id)) {
680 _E("Sending cmd_hello(%s, %d) failed for %s", get_sensor_name(sensor_id), client_id, get_client_name());
681 sensor_client_info::get_instance().close_command_channel(sensor_id);
682 sensor_client_info::get_instance().delete_handle(handle);
683 if (first_connection) {
684 sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
685 sensor_event_listener::get_instance().stop_event_listener();
691 set_power_save_state_cb();
692 dbus_listener::init();
696 API bool sensord_disconnect(int handle)
698 command_channel *cmd_channel;
699 sensor_id_t sensor_id;
705 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
706 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
707 _E("client %s failed to get handle information", get_client_name());
711 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
712 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
716 client_id = sensor_client_info::get_instance().get_client_id();
717 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());
719 _I("%s disconnects with %s[%d]", get_client_name(), get_sensor_name(sensor_id), handle);
721 if (sensor_client_info::get_instance().get_passive_mode(handle)) {
722 _W("%s[%d] for %s is on passive mode while disconnecting.",
723 get_sensor_name(sensor_id), handle, get_client_name());
725 command_channel *cmd_channel;
726 event_type_vector event_types;
727 sensor_client_info::get_instance().get_active_event_types(sensor_id, event_types);
729 for (auto it = event_types.begin(); it != event_types.end(); ++it)
730 sensord_unregister_event(handle, *it);
732 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
733 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
737 if (!cmd_channel->cmd_unset_batch()) {
738 _E("Sending cmd_unset_interval(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
743 if (sensor_state != SENSOR_STATE_STOPPED) {
744 _W("%s[%d] for %s is not stopped before disconnecting.",
745 get_sensor_name(sensor_id), handle, get_client_name());
746 sensord_stop(handle);
749 if (!sensor_client_info::get_instance().delete_handle(handle))
752 if (!sensor_client_info::get_instance().is_active())
753 sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
755 if (!sensor_client_info::get_instance().is_sensor_registered(sensor_id)) {
756 if (!cmd_channel->cmd_byebye()) {
757 _E("Sending cmd_byebye(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
760 sensor_client_info::get_instance().close_command_channel(sensor_id);
763 if (!sensor_client_info::get_instance().is_active()) {
764 _I("Stop listening events for client %s with client id [%d]", get_client_name(), sensor_client_info::get_instance().get_client_id());
765 sensor_event_listener::get_instance().stop_event_listener();
768 unset_power_save_state_cb();
773 static bool register_event(int handle, unsigned int event_type, unsigned int interval, int max_batch_latency, void* cb, void *user_data)
775 sensor_id_t sensor_id;
776 sensor_rep prev_rep, cur_rep;
779 retvm_if(!cb, false, "callback is NULL");
783 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
784 _E("client %s failed to get handle information", get_client_name());
789 interval = DEFAULT_INTERVAL;
791 _I("%s registers event %s[%#x] for sensor %s[%d] with interval: %d, latency: %d, cb: %#x, user_data: %#x",
792 get_client_name(), get_event_name(event_type), event_type, get_sensor_name(sensor_id),
793 handle, interval, max_batch_latency, cb, user_data);
795 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
796 sensor_client_info::get_instance().register_event(handle, event_type, interval, max_batch_latency, cb, user_data);
797 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
798 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
801 sensor_client_info::get_instance().unregister_event(handle, event_type);
806 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)
808 return register_event(handle, event_type, interval, max_batch_latency, (void *)cb, user_data);
811 API bool sensord_unregister_event(int handle, unsigned int event_type)
813 sensor_id_t sensor_id;
814 sensor_rep prev_rep, cur_rep;
816 unsigned int prev_interval, prev_latency;
818 void *prev_user_data;
822 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
823 _E("client %s failed to get handle information", get_client_name());
827 _I("%s unregisters event %s[%#x] for sensor %s[%d]", get_client_name(), get_event_name(event_type),
828 event_type, get_sensor_name(sensor_id), handle);
830 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
831 sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data);
833 if (!sensor_client_info::get_instance().unregister_event(handle, event_type)) {
834 _E("%s try to unregister non registered event %s[%#x] for sensor %s[%d]",
835 get_client_name(), get_event_name(event_type), event_type, get_sensor_name(sensor_id), handle);
839 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
840 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
842 if (sensor_client_info::get_instance().get_passive_mode(handle))
843 sensor_client_info::get_instance().set_passive_mode(handle, false);
846 sensor_client_info::get_instance().register_event(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data);
851 API bool sensord_register_accuracy_cb(int handle, sensor_accuracy_changed_cb_t cb, void *user_data)
853 sensor_id_t sensor_id;
855 retvm_if(!cb, false, "callback is NULL");
859 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
860 _E("client %s failed to get handle information", get_client_name());
864 _I("%s registers accuracy_changed_cb for sensor %s[%d] with cb: %#x, user_data: %#x",
865 get_client_name(), get_sensor_name(sensor_id), handle, cb, user_data);
867 sensor_client_info::get_instance().register_accuracy_cb(handle, cb , user_data);
872 API bool sensord_unregister_accuracy_cb(int handle)
874 sensor_id_t sensor_id;
878 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
879 _E("client %s failed to get handle information", get_client_name());
883 _I("%s unregisters accuracy_changed_cb for sensor %s[%d]",
884 get_client_name(), get_sensor_name(sensor_id), handle);
886 sensor_client_info::get_instance().unregister_accuracy_cb(handle);
891 API bool sensord_start(int handle, int option)
893 sensor_id_t sensor_id;
894 sensor_rep prev_rep, cur_rep;
896 int prev_state, prev_pause;
901 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
902 _E("client %s failed to get handle information", get_client_name());
906 retvm_if((option < 0) || (option >= SENSOR_OPTION_END), false, "Invalid option value : %d, handle: %d, %s, %s",
907 option, handle, get_sensor_name(sensor_id), get_client_name());
909 _I("%s starts %s[%d], with option: %d, power save state: %d", get_client_name(), get_sensor_name(sensor_id),
910 handle, option, g_power_save_state);
912 pause = CONVERT_OPTION_PAUSE_POLICY(option);
914 if (g_power_save_state && (g_power_save_state & pause)) {
915 sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_PAUSED, pause);
919 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
920 sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_pause);
921 sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_STARTED, pause);
922 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
924 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
927 sensor_client_info::get_instance().set_sensor_params(handle, prev_state, prev_pause);
932 API bool sensord_stop(int handle)
934 sensor_id_t sensor_id;
937 int prev_state, prev_pause;
939 sensor_rep prev_rep, cur_rep;
943 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
944 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
945 _E("client %s failed to get handle information", get_client_name());
949 retvm_if((sensor_state == SENSOR_STATE_STOPPED), true, "%s already stopped with %s[%d]",
950 get_client_name(), get_sensor_name(sensor_id), handle);
952 _I("%s stops sensor %s[%d]", get_client_name(), get_sensor_name(sensor_id), handle);
954 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
955 sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_pause);
956 sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_STOPPED);
957 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
959 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
962 sensor_client_info::get_instance().set_sensor_params(handle, prev_state, prev_pause);
967 static bool change_event_batch(int handle, unsigned int event_type, unsigned int interval, unsigned int latency)
969 sensor_id_t sensor_id;
970 sensor_rep prev_rep, cur_rep;
972 unsigned int prev_interval, prev_latency;
974 void *prev_user_data;
978 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
979 _E("client %s failed to get handle information", get_client_name());
984 interval = DEFAULT_INTERVAL;
986 _I("%s changes batch of event %s[%#x] for %s[%d] to (%d, %d)", get_client_name(), get_event_name(event_type),
987 event_type, get_sensor_name(sensor_id), handle, interval, latency);
989 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
991 sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data);
993 if (!sensor_client_info::get_instance().set_event_batch(handle, event_type, interval, latency))
996 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
998 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
1001 sensor_client_info::get_instance().set_event_batch(handle, event_type, prev_interval, prev_latency);
1006 API bool sensord_change_event_interval(int handle, unsigned int event_type, unsigned int interval)
1008 unsigned int prev_interval, prev_latency;
1010 void *prev_user_data;
1014 if (!sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data)) {
1015 _E("Failed to get event info with handle = %d, event_type = %#x", handle, event_type);
1019 _I("handle = %d, event_type = %#x, interval = %d, prev_latency = %d", handle, event_type, interval, prev_latency);
1020 return change_event_batch(handle, event_type, interval, prev_latency);
1023 API bool sensord_change_event_max_batch_latency(int handle, unsigned int event_type, unsigned int max_batch_latency)
1025 unsigned int prev_interval, prev_latency;
1027 void *prev_user_data;
1031 if (!sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data)) {
1032 _E("Failed to get event info with handle = %d, event_type = %#x", handle, event_type);
1036 return change_event_batch(handle, event_type, prev_interval, max_batch_latency);
1039 static int change_pause_policy(int handle, int pause)
1041 sensor_id_t sensor_id;
1042 sensor_rep prev_rep, cur_rep;
1045 int prev_state, prev_pause;
1049 retvm_if((pause < 0) || (pause >= SENSORD_PAUSE_END), -EINVAL,
1050 "Invalid pause value : %d, handle: %d, %s, %s",
1051 pause, handle, get_sensor_name(sensor_id), get_client_name());
1053 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
1054 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1055 _E("client %s failed to get handle information", get_client_name());
1059 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
1060 sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_pause);
1062 if (g_power_save_state) {
1063 if ((pause & g_power_save_state) && (sensor_state == SENSOR_STATE_STARTED))
1064 sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_PAUSED);
1065 else if (!(pause & g_power_save_state) && (sensor_state == SENSOR_STATE_PAUSED))
1066 sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_STARTED);
1068 sensor_client_info::get_instance().set_sensor_pause_policy(handle, pause);
1070 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
1071 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
1074 sensor_client_info::get_instance().set_sensor_pause_policy(handle, prev_pause);
1076 return (ret ? OP_SUCCESS : OP_ERROR);
1079 static int change_axis_orientation(int handle, int axis_orientation)
1081 sensor_event_listener::get_instance().set_sensor_axis(axis_orientation);
1085 static int change_attribute_int(int handle, int attribute, int value)
1087 sensor_id_t sensor_id;
1088 command_channel *cmd_channel;
1093 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1094 _E("client %s failed to get handle information", get_client_name());
1098 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1099 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
1103 client_id = sensor_client_info::get_instance().get_client_id();
1104 retvm_if((client_id < 0), -EPERM,
1105 "Invalid client id : %d, handle: %d, %s, %s",
1106 client_id, handle, get_sensor_name(sensor_id), get_client_name());
1108 if (!cmd_channel->cmd_set_attribute_int(attribute, value)) {
1109 _E("Sending cmd_set_attribute_int(%d, %d) failed for %s",
1110 client_id, value, get_client_name());
1114 sensor_client_info::get_instance().set_attribute(handle, attribute, value);
1119 API bool sensord_set_option(int handle, int option)
1121 return (change_pause_policy(handle, CONVERT_OPTION_PAUSE_POLICY(option)) == OP_SUCCESS);
1124 API int sensord_set_attribute_int(int handle, int attribute, int value)
1126 switch (attribute) {
1127 case SENSORD_ATTRIBUTE_PAUSE_POLICY:
1128 return change_pause_policy(handle, value);
1129 case SENSORD_ATTRIBUTE_AXIS_ORIENTATION:
1130 return change_axis_orientation(handle, value);
1132 return change_attribute_int(handle, attribute, value);
1138 API int sensord_set_attribute_str(int handle, int attribute, const char *value, int value_len)
1140 sensor_id_t sensor_id;
1141 command_channel *cmd_channel;
1146 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1147 _E("Client %s failed to get handle information", get_client_name());
1151 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1152 _E("Client %s failed to get command channel for %s",
1153 get_client_name(), get_sensor_name(sensor_id));
1157 retvm_if((value_len < 0) || (value == NULL), -EINVAL,
1158 "Invalid value_len: %d, value: %#x, handle: %d, %s, %s",
1159 value_len, value, handle, get_sensor_name(sensor_id), get_client_name());
1161 client_id = sensor_client_info::get_instance().get_client_id();
1162 retvm_if((client_id < 0), -EPERM,
1163 "Invalid client id : %d, handle: %d, %s, %s",
1164 client_id, handle, get_sensor_name(sensor_id), get_client_name());
1166 if (!cmd_channel->cmd_set_attribute_str(attribute, value, value_len)) {
1167 _E("Sending cmd_set_attribute_str(%d, %d, %#x) failed for %s",
1168 client_id, value_len, value, get_client_name());
1172 sensor_client_info::get_instance().set_attribute(handle, attribute, value);
1177 API bool sensord_send_sensorhub_data(int handle, const char *data, int data_len)
1179 return (sensord_set_attribute_str(handle, 0, data, data_len) == OP_SUCCESS);
1182 API bool sensord_send_command(int handle, const char *command, int command_len)
1184 return (sensord_set_attribute_str(handle, 0, command, command_len) == OP_SUCCESS);
1187 API bool sensord_get_data(int handle, unsigned int data_id, sensor_data_t* sensor_data)
1189 sensor_id_t sensor_id;
1190 command_channel *cmd_channel;
1194 retvm_if((!sensor_data), false, "sensor_data is NULL");
1198 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
1199 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1200 _E("client %s failed to get handle information", get_client_name());
1204 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1205 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
1209 client_id = sensor_client_info::get_instance().get_client_id();
1210 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());
1212 if (sensor_state != SENSOR_STATE_STARTED) {
1213 _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);
1217 if (!cmd_channel->cmd_get_data(data_id, sensor_data)) {
1218 _E("cmd_get_data(%d, %d, %#x) failed for %s", client_id, data_id, sensor_data, get_client_name());
1225 API bool sensord_flush(int handle)
1227 sensor_id_t sensor_id;
1228 command_channel *cmd_channel;
1234 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state) ||
1235 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1236 _E("client %s failed to get handle information", get_client_name());
1240 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1241 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
1245 client_id = sensor_client_info::get_instance().get_client_id();
1246 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());
1248 if (sensor_state != SENSOR_STATE_STARTED) {
1249 _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);
1253 if (!cmd_channel->cmd_flush()) {
1254 _E("cmd_flush() failed for %s", get_client_name());
1261 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)
1266 API bool sensord_set_passive_mode(int handle, bool passive)
1268 if (!sensor_client_info::get_instance().set_passive_mode(handle, passive)) {
1269 _E("Failed to set passive mode %d", passive);