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 #define DEFAULT_INTERVAL POLL_10HZ_MS
45 static int g_power_save_state = 0;
47 static int get_power_save_state(void);
48 static void power_save_state_cb(keynode_t *node, void *data);
49 static void clean_up(void);
50 static bool change_sensor_rep(sensor_id_t sensor_id, sensor_rep &prev_rep, sensor_rep &cur_rep);
51 static void restore_session(void);
52 static bool register_event(int handle, unsigned int event_type, unsigned int interval, int max_batch_latency, void* cb, void *user_data);
58 sensor_event_listener::get_instance().set_hup_observer(restore_session);
64 _I("Good bye! %s\n", get_client_name());
68 static initiator g_initiator;
70 static int g_power_save_state_cb_cnt = 0;
72 static void set_power_save_state_cb(void)
74 if (g_power_save_state_cb_cnt < 0)
75 _E("g_power_save_state_cb_cnt(%d) is wrong", g_power_save_state_cb_cnt);
77 ++g_power_save_state_cb_cnt;
79 if (g_power_save_state_cb_cnt == 1) {
80 _D("Power save callback is registered");
81 g_power_save_state = get_power_save_state();
82 _D("power_save_state = [%d]", g_power_save_state);
83 vconf_notify_key_changed(VCONFKEY_PM_STATE, power_save_state_cb, NULL);
87 static void unset_power_save_state_cb(void)
89 --g_power_save_state_cb_cnt;
91 if (g_power_save_state_cb_cnt < 0)
92 _E("g_power_save_state_cb_cnt(%d) is wrong", g_power_save_state_cb_cnt);
94 if (g_power_save_state_cb_cnt == 0) {
95 _D("Power save callback is unregistered");
96 vconf_ignore_key_changed(VCONFKEY_PM_STATE, power_save_state_cb);
102 handle_vector handles;
104 sensor_client_info::get_instance().get_all_handles(handles);
106 auto it_handle = handles.begin();
108 while (it_handle != handles.end()) {
109 sensord_disconnect(*it_handle);
114 static int get_power_save_state(void)
119 vconf_get_int(VCONFKEY_PM_STATE, &pm_state);
121 if (pm_state == VCONFKEY_PM_STATE_LCDOFF)
122 state |= SENSOR_OPTION_ON_IN_SCREEN_OFF;
127 static void power_save_state_cb(keynode_t *node, void *data)
129 int cur_power_save_state;
130 sensor_id_vector sensors;
131 sensor_rep prev_rep, cur_rep;
135 cur_power_save_state = get_power_save_state();
137 if (cur_power_save_state == g_power_save_state) {
138 _D("g_power_save_state NOT changed : [%d]", cur_power_save_state);
142 g_power_save_state = cur_power_save_state;
143 _D("power_save_state: %d noti to %s", g_power_save_state, get_client_name());
145 sensor_client_info::get_instance().get_listening_sensors(sensors);
147 auto it_sensor = sensors.begin();
149 while (it_sensor != sensors.end()) {
150 sensor_client_info::get_instance().get_sensor_rep(*it_sensor, prev_rep);
151 sensor_event_listener::get_instance().operate_sensor(*it_sensor, cur_power_save_state);
152 sensor_client_info::get_instance().get_sensor_rep(*it_sensor, cur_rep);
153 change_sensor_rep(*it_sensor, prev_rep, cur_rep);
159 void restore_session(void)
163 _I("Trying to restore sensor client session for %s", get_client_name());
165 command_channel *cmd_channel;
168 sensor_client_info::get_instance().close_command_channel();
169 sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
171 sensor_id_vector sensors;
173 sensor_client_info::get_instance().get_listening_sensors(sensors);
175 bool first_connection = true;
177 auto it_sensor = sensors.begin();
179 while (it_sensor != sensors.end()) {
180 cmd_channel = new(std::nothrow) command_channel();
181 retm_if(!cmd_channel, "Failed to allocate memory");
183 if (!cmd_channel->create_channel()) {
184 _E("%s failed to create command channel for %s", get_client_name(), get_sensor_name(*it_sensor));
189 sensor_client_info::get_instance().add_command_channel(*it_sensor, cmd_channel);
191 if (first_connection) {
192 first_connection = false;
193 if (!cmd_channel->cmd_get_id(client_id)) {
194 _E("Failed to get client id");
198 sensor_client_info::get_instance().set_client_id(client_id);
199 sensor_event_listener::get_instance().start_event_listener();
202 cmd_channel->set_client_id(client_id);
204 if (!cmd_channel->cmd_hello(*it_sensor)) {
205 _E("Sending cmd_hello(%s, %d) failed for %s", get_sensor_name(*it_sensor), client_id, get_client_name());
209 sensor_rep prev_rep, cur_rep;
210 prev_rep.active = false;
211 prev_rep.option = SENSOR_OPTION_DEFAULT;
212 prev_rep.interval = 0;
214 sensor_client_info::get_instance().get_sensor_rep(*it_sensor, cur_rep);
215 if (!change_sensor_rep(*it_sensor, prev_rep, cur_rep)) {
216 _E("Failed to change rep(%s) for %s", get_sensor_name(*it_sensor), get_client_name());
223 _I("Succeeded to restore sensor client session for %s", get_client_name());
228 sensor_event_listener::get_instance().clear();
229 _E("Failed to restore session for %s", get_client_name());
232 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)
234 sort(a_vec.begin(), a_vec.end());
235 sort(b_vec.begin(), b_vec.end());
237 set_difference(a_vec.begin(), a_vec.end(), b_vec.begin(), b_vec.end(), back_inserter(del_vec));
238 set_difference(b_vec.begin(), b_vec.end(), a_vec.begin(), a_vec.end(), back_inserter(add_vec));
240 return !(add_vec.empty() && del_vec.empty());
243 static bool change_sensor_rep(sensor_id_t sensor_id, sensor_rep &prev_rep, sensor_rep &cur_rep)
246 command_channel *cmd_channel;
247 event_type_vector add_event_types, del_event_types;
249 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
250 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
254 client_id = sensor_client_info::get_instance().get_client_id();
255 retvm_if((client_id < 0), false, "Invalid client id : %d, %s, %s", client_id, get_sensor_name(sensor_id), get_client_name());
257 get_events_diff(prev_rep.event_types, cur_rep.event_types, add_event_types, del_event_types);
259 if (cur_rep.active) {
260 if (prev_rep.option != cur_rep.option) {
261 if (!cmd_channel->cmd_set_option(cur_rep.option)) {
262 _E("Sending cmd_set_option(%d, %s, %d) failed for %s", client_id, get_sensor_name(sensor_id), cur_rep.option, get_client_name());
267 if ( (prev_rep.interval != cur_rep.interval) || (prev_rep.latency != cur_rep.latency)) {
268 if (!cmd_channel->cmd_set_batch(cur_rep.interval, cur_rep.latency)) {
269 _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());
274 if (!add_event_types.empty()) {
275 if (!cmd_channel->cmd_register_events(add_event_types)) {
276 _E("Sending cmd_register_events(%d, add_event_types) failed for %s", client_id, get_client_name());
282 if (prev_rep.active && !del_event_types.empty()) {
283 if (!cmd_channel->cmd_unregister_events(del_event_types)) {
284 _E("Sending cmd_unregister_events(%d, del_event_types) failed for %s", client_id, get_client_name());
289 if (prev_rep.active != cur_rep.active) {
290 if (cur_rep.active) {
291 if (!cmd_channel->cmd_start()) {
292 _E("Sending cmd_start(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
296 if (!cmd_channel->cmd_unset_batch()) {
297 _E("Sending cmd_unset_interval(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
301 if (!cmd_channel->cmd_stop()) {
302 _E("Sending cmd_stop(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
311 static bool get_sensor_list(void)
314 static bool init = false;
319 command_channel cmd_channel;
321 if (!cmd_channel.create_channel()) {
322 _E("%s failed to create command channel", get_client_name());
326 if (!cmd_channel.cmd_get_sensor_list())
335 API int sensord_get_sensors(sensor_type_t type, sensor_t **list, int *sensor_count)
337 retvm_if(!get_sensor_list(), -EPERM, "Fail to get sensor list from server");
339 vector<sensor_info *> sensor_infos = sensor_info_manager::get_instance().get_infos(type);
341 if (sensor_infos.empty()) {
346 if (type != ALL_SENSOR) {
347 if (sensor_infos[0]->get_id() == (sensor_id_t)(-EACCES))
351 *list = (sensor_t *) malloc(sizeof(sensor_info *) * sensor_infos.size());
352 retvm_if(!*list, false, "Failed to allocate memory");
354 for (unsigned int i = 0; i < sensor_infos.size(); ++i)
355 *(*list + i) = sensor_info_to_sensor(sensor_infos[i]);
357 *sensor_count = sensor_infos.size();
362 API int sensord_get_default_sensor(sensor_type_t type, sensor_t *sensor)
364 retvm_if(!get_sensor_list(), -EPERM, "Fail to get sensor list from server");
366 const sensor_info *info;
368 info = sensor_info_manager::get_instance().get_info(type);
374 if ((const_cast<sensor_info *>(info))->get_id() == (sensor_id_t)(-EACCES))
377 *sensor = sensor_info_to_sensor(info);
382 API bool sensord_get_sensor_list(sensor_type_t type, sensor_t **list, int *sensor_count)
384 return (sensord_get_sensors(type, list, sensor_count) == OP_SUCCESS);
387 API sensor_t sensord_get_sensor(sensor_type_t type)
390 sensord_get_default_sensor(type, &sensor);
395 API bool sensord_get_type(sensor_t sensor, sensor_type_t *type)
397 sensor_info* info = sensor_to_sensor_info(sensor);
399 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !type,
400 NULL, "Invalid param: sensor (%p), type(%p)", sensor, type);
402 *type = info->get_type();
407 API const char* sensord_get_name(sensor_t sensor)
409 sensor_info* info = sensor_to_sensor_info(sensor);
411 retvm_if(!sensor_info_manager::get_instance().is_valid(info),
412 NULL, "Invalid param: sensor (%p)", sensor);
414 return info->get_name();
417 API const char* sensord_get_vendor(sensor_t sensor)
419 sensor_info* info = sensor_to_sensor_info(sensor);
421 retvm_if(!sensor_info_manager::get_instance().is_valid(info),
422 NULL, "Invalid param: sensor (%p)", sensor);
424 return info->get_vendor();
427 API bool sensord_get_privilege(sensor_t sensor, sensor_privilege_t *privilege)
429 sensor_info* info = sensor_to_sensor_info(sensor);
431 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !privilege,
432 false, "Invalid param: sensor (%p), privilege(%p)", sensor, privilege);
434 *privilege = info->get_privilege();
439 API bool sensord_get_min_range(sensor_t sensor, float *min_range)
441 sensor_info* info = sensor_to_sensor_info(sensor);
443 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !min_range,
444 false, "Invalid param: sensor (%p), min_range(%p)", sensor, min_range);
446 *min_range = info->get_min_range();
451 API bool sensord_get_max_range(sensor_t sensor, float *max_range)
453 sensor_info* info = sensor_to_sensor_info(sensor);
455 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !max_range,
456 false, "Invalid param: sensor (%p), max_range(%p)", sensor, max_range);
458 *max_range = info->get_max_range();
463 API bool sensord_get_resolution(sensor_t sensor, float *resolution)
465 sensor_info* info = sensor_to_sensor_info(sensor);
467 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !resolution,
468 false, "Invalid param: sensor (%p), resolution(%p)", sensor, resolution);
470 *resolution = info->get_resolution();
475 API bool sensord_get_min_interval(sensor_t sensor, int *min_interval)
477 sensor_info* info = sensor_to_sensor_info(sensor);
479 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !min_interval,
480 false, "Invalid param: sensor (%p), min_interval(%p)", sensor, min_interval);
482 *min_interval = info->get_min_interval();
487 API bool sensord_get_fifo_count(sensor_t sensor, int *fifo_count)
489 sensor_info* info = sensor_to_sensor_info(sensor);
491 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !fifo_count,
492 false, "Invalid param: sensor (%p), fifo_count(%p)", sensor, fifo_count);
494 *fifo_count = info->get_fifo_count();
499 API bool sensord_get_max_batch_count(sensor_t sensor, int *max_batch_count)
501 sensor_info* info = sensor_to_sensor_info(sensor);
503 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !max_batch_count,
504 false, "Invalid param: sensor (%p), max_batch_count(%p)", sensor, max_batch_count);
506 *max_batch_count = info->get_max_batch_count();
511 API bool sensord_get_supported_event_types(sensor_t sensor, unsigned int **event_types, int *count)
513 sensor_info* info = sensor_to_sensor_info(sensor);
515 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !event_types || !count,
516 false, "Invalid param: sensor (%p), event_types(%p), count(%p)", sensor, event_types, count);
518 unsigned int event_type;
519 event_type = info->get_supported_event();
520 *event_types = (unsigned int *)malloc(sizeof(unsigned int));
522 retvm_if(!*event_types, false, "Failed to allocate memory");
524 (*event_types)[0] = event_type;
530 API bool sensord_is_supported_event_type(sensor_t sensor, unsigned int event_type, bool *supported)
532 sensor_info* info = sensor_to_sensor_info(sensor);
534 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !event_type || !supported,
535 false, "Invalid param: sensor (%p), event_type(%p), supported(%p)", sensor, event_type, supported);
537 *supported = info->is_supported_event(event_type);
542 API bool sensord_is_wakeup_supported(sensor_t sensor)
544 sensor_info* info = sensor_to_sensor_info(sensor);
546 retvm_if(!sensor_info_manager::get_instance().is_valid(info),
547 false, "Invalid param: sensor (%p)", sensor);
549 return info->is_wakeup_supported();
552 API int sensord_connect(sensor_t sensor)
554 command_channel *cmd_channel = NULL;
557 bool sensor_registered;
558 bool first_connection = false;
560 sensor_info* info = sensor_to_sensor_info(sensor);
562 retvm_if(!sensor_info_manager::get_instance().is_valid(info),
563 OP_ERROR, "Invalid param: sensor (%p)", sensor);
565 sensor_id_t sensor_id = info->get_id();
569 sensor_registered = sensor_client_info::get_instance().is_sensor_registered(sensor_id);
571 // lazy loading after creating static variables
574 handle = sensor_client_info::get_instance().create_handle(sensor_id);
575 if (handle == MAX_HANDLE) {
576 _E("Maximum number of handles reached, sensor: %s in client %s", get_sensor_name(sensor_id), get_client_name());
580 if (!sensor_registered) {
581 cmd_channel = new(std::nothrow) command_channel();
583 _E("Failed to allocated memory");
584 sensor_client_info::get_instance().delete_handle(handle);
588 if (!cmd_channel->create_channel()) {
589 _E("%s failed to create command channel for %s", get_client_name(), get_sensor_name(sensor_id));
590 sensor_client_info::get_instance().delete_handle(handle);
595 sensor_client_info::get_instance().add_command_channel(sensor_id, cmd_channel);
598 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
599 _E("%s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
600 sensor_client_info::get_instance().delete_handle(handle);
604 if (!sensor_client_info::get_instance().has_client_id()) {
605 first_connection = true;
606 if (!cmd_channel->cmd_get_id(client_id)) {
607 _E("Sending cmd_get_id() failed for %s", get_sensor_name(sensor_id));
608 sensor_client_info::get_instance().close_command_channel(sensor_id);
609 sensor_client_info::get_instance().delete_handle(handle);
613 sensor_client_info::get_instance().set_client_id(client_id);
614 _I("%s gets client_id [%d]", get_client_name(), client_id);
615 sensor_event_listener::get_instance().start_event_listener();
616 _I("%s starts listening events with client_id [%d]", get_client_name(), client_id);
619 client_id = sensor_client_info::get_instance().get_client_id();
620 cmd_channel->set_client_id(client_id);
622 _I("%s[%d] connects with %s[%d]", get_client_name(), client_id, get_sensor_name(sensor_id), handle);
624 sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_STOPPED, SENSOR_OPTION_DEFAULT);
626 if (!sensor_registered) {
627 if (!cmd_channel->cmd_hello(sensor_id)) {
628 _E("Sending cmd_hello(%s, %d) failed for %s", get_sensor_name(sensor_id), client_id, get_client_name());
629 sensor_client_info::get_instance().close_command_channel(sensor_id);
630 sensor_client_info::get_instance().delete_handle(handle);
631 if (first_connection) {
632 sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
633 sensor_event_listener::get_instance().stop_event_listener();
639 set_power_save_state_cb();
640 dbus_listener::init();
644 API bool sensord_disconnect(int handle)
646 command_channel *cmd_channel;
647 sensor_id_t sensor_id;
653 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
654 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
655 _E("client %s failed to get handle information", get_client_name());
659 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
660 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
664 client_id = sensor_client_info::get_instance().get_client_id();
665 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());
667 _I("%s disconnects with %s[%d]", get_client_name(), get_sensor_name(sensor_id), handle);
669 if (sensor_state != SENSOR_STATE_STOPPED) {
670 _W("%s[%d] for %s is not stopped before disconnecting.",
671 get_sensor_name(sensor_id), handle, get_client_name());
672 sensord_stop(handle);
675 if (!sensor_client_info::get_instance().delete_handle(handle))
678 if (!sensor_client_info::get_instance().is_active())
679 sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
681 if (!sensor_client_info::get_instance().is_sensor_registered(sensor_id)) {
682 if (!cmd_channel->cmd_byebye()) {
683 _E("Sending cmd_byebye(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
686 sensor_client_info::get_instance().close_command_channel(sensor_id);
689 if (!sensor_client_info::get_instance().is_active()) {
690 _I("Stop listening events for client %s with client id [%d]", get_client_name(), sensor_client_info::get_instance().get_client_id());
691 sensor_event_listener::get_instance().stop_event_listener();
694 unset_power_save_state_cb();
699 static bool register_event(int handle, unsigned int event_type, unsigned int interval, int max_batch_latency, void* cb, void *user_data)
701 sensor_id_t sensor_id;
702 sensor_rep prev_rep, cur_rep;
705 retvm_if(!cb, false, "callback is NULL");
709 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
710 _E("client %s failed to get handle information", get_client_name());
715 interval = DEFAULT_INTERVAL;
717 _I("%s registers event %s[%#x] for sensor %s[%d] with interval: %d, latency: %d, cb: %#x, user_data: %#x",
718 get_client_name(), get_event_name(event_type), event_type, get_sensor_name(sensor_id),
719 handle, interval, max_batch_latency, cb, user_data);
721 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
722 sensor_client_info::get_instance().register_event(handle, event_type, interval, max_batch_latency, cb, user_data);
723 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
724 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
727 sensor_client_info::get_instance().unregister_event(handle, event_type);
732 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)
734 return register_event(handle, event_type, interval, max_batch_latency, (void *)cb, user_data);
737 API bool sensord_unregister_event(int handle, unsigned int event_type)
739 sensor_id_t sensor_id;
740 sensor_rep prev_rep, cur_rep;
742 unsigned int prev_interval, prev_latency;
744 void *prev_user_data;
748 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
749 _E("client %s failed to get handle information", get_client_name());
753 _I("%s unregisters event %s[%#x] for sensor %s[%d]", get_client_name(), get_event_name(event_type),
754 event_type, get_sensor_name(sensor_id), handle);
756 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
757 sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data);
759 if (!sensor_client_info::get_instance().unregister_event(handle, event_type)) {
760 _E("%s try to unregister non registered event %s[%#x] for sensor %s[%d]",
761 get_client_name(), get_event_name(event_type), event_type, get_sensor_name(sensor_id), handle);
765 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
766 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
769 sensor_client_info::get_instance().register_event(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data);
774 API bool sensord_register_accuracy_cb(int handle, sensor_accuracy_changed_cb_t cb, void *user_data)
776 sensor_id_t sensor_id;
778 retvm_if(!cb, false, "callback is NULL");
782 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
783 _E("client %s failed to get handle information", get_client_name());
787 _I("%s registers accuracy_changed_cb for sensor %s[%d] with cb: %#x, user_data: %#x",
788 get_client_name(), get_sensor_name(sensor_id), handle, cb, user_data);
790 sensor_client_info::get_instance().register_accuracy_cb(handle, cb , user_data);
795 API bool sensord_unregister_accuracy_cb(int handle)
797 sensor_id_t sensor_id;
801 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
802 _E("client %s failed to get handle information", get_client_name());
806 _I("%s unregisters accuracy_changed_cb for sensor %s[%d]",
807 get_client_name(), get_sensor_name(sensor_id), handle);
809 sensor_client_info::get_instance().unregister_accuracy_cb(handle);
814 API bool sensord_start(int handle, int option)
816 sensor_id_t sensor_id;
817 sensor_rep prev_rep, cur_rep;
819 int prev_state, prev_option;
823 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
824 _E("client %s failed to get handle information", get_client_name());
828 retvm_if((option < 0) || (option >= SENSOR_OPTION_END), false, "Invalid option value : %d, handle: %d, %s, %s",
829 option, handle, get_sensor_name(sensor_id), get_client_name());
831 _I("%s starts %s[%d], with option: %d, power save state: %d", get_client_name(), get_sensor_name(sensor_id),
832 handle, option, g_power_save_state);
834 if (g_power_save_state && !(g_power_save_state & option)) {
835 sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_PAUSED, option);
839 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
840 sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_option);
841 sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_STARTED, option);
842 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
844 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
847 sensor_client_info::get_instance().set_sensor_params(handle, prev_state, prev_option);
852 API bool sensord_stop(int handle)
854 sensor_id_t sensor_id;
857 int prev_state, prev_option;
859 sensor_rep prev_rep, cur_rep;
863 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
864 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
865 _E("client %s failed to get handle information", get_client_name());
869 retvm_if((sensor_state == SENSOR_STATE_STOPPED), true, "%s already stopped with %s[%d]",
870 get_client_name(), get_sensor_name(sensor_id), handle);
872 _I("%s stops sensor %s[%d]", get_client_name(), get_sensor_name(sensor_id), handle);
874 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
875 sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_option);
876 sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_STOPPED);
877 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
879 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
882 sensor_client_info::get_instance().set_sensor_params(handle, prev_state, prev_option);
887 static bool change_event_batch(int handle, unsigned int event_type, unsigned int interval, unsigned int latency)
889 sensor_id_t sensor_id;
890 sensor_rep prev_rep, cur_rep;
892 unsigned int prev_interval, prev_latency;
894 void *prev_user_data;
898 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
899 _E("client %s failed to get handle information", get_client_name());
904 interval = DEFAULT_INTERVAL;
906 _I("%s changes batch of event %s[%#x] for %s[%d] to (%d, %d)", get_client_name(), get_event_name(event_type),
907 event_type, get_sensor_name(sensor_id), handle, interval, latency);
909 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
911 sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data);
913 if (!sensor_client_info::get_instance().set_event_batch(handle, event_type, interval, latency))
916 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
918 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
921 sensor_client_info::get_instance().set_event_batch(handle, event_type, prev_interval, prev_latency);
926 API bool sensord_change_event_interval(int handle, unsigned int event_type, unsigned int interval)
928 unsigned int prev_interval, prev_latency;
930 void *prev_user_data;
934 if (!sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data)) {
935 _E("Failed to get event info with handle = %d, event_type = %#x", handle, event_type);
939 _I("handle = %d, event_type = %#x, interval = %d, prev_latency = %d", handle, event_type, interval, prev_latency);
940 return change_event_batch(handle, event_type, interval, prev_latency);
943 API bool sensord_change_event_max_batch_latency(int handle, unsigned int event_type, unsigned int max_batch_latency)
945 unsigned int prev_interval, prev_latency;
947 void *prev_user_data;
951 if (!sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data)) {
952 _E("Failed to get event info with handle = %d, event_type = %#x", handle, event_type);
956 return change_event_batch(handle, event_type, prev_interval, max_batch_latency);
959 API bool sensord_set_option(int handle, int option)
961 sensor_id_t sensor_id;
962 sensor_rep prev_rep, cur_rep;
965 int prev_state, prev_option;
969 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
970 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
971 _E("client %s failed to get handle information", get_client_name());
975 retvm_if((option < 0) || (option >= SENSOR_OPTION_END), false, "Invalid option value : %d, handle: %d, %s, %s",
976 option, handle, get_sensor_name(sensor_id), get_client_name());
978 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
979 sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_option);
981 if (g_power_save_state) {
982 if ((option & g_power_save_state) && (sensor_state == SENSOR_STATE_PAUSED))
983 sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_STARTED);
984 else if (!(option & g_power_save_state) && (sensor_state == SENSOR_STATE_STARTED))
985 sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_PAUSED);
987 sensor_client_info::get_instance().set_sensor_option(handle, option);
989 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
990 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
993 sensor_client_info::get_instance().set_sensor_option(handle, prev_option);
998 API int sensord_set_attribute_int(int handle, int attribute, int value)
1000 sensor_id_t sensor_id;
1001 command_channel *cmd_channel;
1006 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1007 _E("client %s failed to get handle information", get_client_name());
1011 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1012 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
1016 client_id = sensor_client_info::get_instance().get_client_id();
1017 retvm_if((client_id < 0), -EPERM,
1018 "Invalid client id : %d, handle: %d, %s, %s",
1019 client_id, handle, get_sensor_name(sensor_id), get_client_name());
1021 if (!cmd_channel->cmd_set_attribute_int(attribute, value)) {
1022 _E("Sending cmd_set_attribute_int(%d, %d) failed for %s",
1023 client_id, value, get_client_name());
1030 API int sensord_set_attribute_str(int handle, int attribute, const char *value, int value_len)
1032 sensor_id_t sensor_id;
1033 command_channel *cmd_channel;
1038 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1039 _E("Client %s failed to get handle information", get_client_name());
1043 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1044 _E("Client %s failed to get command channel for %s",
1045 get_client_name(), get_sensor_name(sensor_id));
1049 retvm_if((value_len < 0) || (value == NULL), -EINVAL,
1050 "Invalid value_len: %d, value: %#x, handle: %d, %s, %s",
1051 value_len, value, handle, get_sensor_name(sensor_id), get_client_name());
1053 client_id = sensor_client_info::get_instance().get_client_id();
1054 retvm_if((client_id < 0), -EPERM,
1055 "Invalid client id : %d, handle: %d, %s, %s",
1056 client_id, handle, get_sensor_name(sensor_id), get_client_name());
1058 if (!cmd_channel->cmd_set_attribute_str(attribute, value, value_len)) {
1059 _E("Sending cmd_set_attribute_str(%d, %d, %#x) failed for %s",
1060 client_id, value_len, value, get_client_name());
1067 API bool sensord_send_sensorhub_data(int handle, const char *data, int data_len)
1069 return (sensord_set_attribute_str(handle, 0, data, data_len) == OP_SUCCESS);
1072 API bool sensord_send_command(int handle, const char *command, int command_len)
1074 return (sensord_set_attribute_str(handle, 0, command, command_len) == OP_SUCCESS);
1077 API bool sensord_get_data(int handle, unsigned int data_id, sensor_data_t* sensor_data)
1079 sensor_id_t sensor_id;
1080 command_channel *cmd_channel;
1084 retvm_if((!sensor_data), false, "sensor_data is NULL");
1088 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
1089 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1090 _E("client %s failed to get handle information", get_client_name());
1094 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1095 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
1099 client_id = sensor_client_info::get_instance().get_client_id();
1100 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());
1102 if (sensor_state != SENSOR_STATE_STARTED) {
1103 _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);
1107 if (!cmd_channel->cmd_get_data(data_id, sensor_data)) {
1108 _E("cmd_get_data(%d, %d, %#x) failed for %s", client_id, data_id, sensor_data, get_client_name());
1115 API bool sensord_flush(int handle)
1117 sensor_id_t sensor_id;
1118 command_channel *cmd_channel;
1124 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state) ||
1125 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1126 _E("client %s failed to get handle information", get_client_name());
1130 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1131 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
1135 client_id = sensor_client_info::get_instance().get_client_id();
1136 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());
1138 if (sensor_state != SENSOR_STATE_STARTED) {
1139 _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);
1143 if (!cmd_channel->cmd_flush()) {
1144 _E("cmd_flush() failed for %s", get_client_name());
1151 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)