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>
37 #define API __attribute__((visibility("default")))
40 #define DEFAULT_INTERVAL POLL_10HZ_MS
44 static int g_power_save_state = 0;
46 static int get_power_save_state(void);
47 static void power_save_state_cb(keynode_t *node, void *data);
48 static void clean_up(void);
49 static bool change_sensor_rep(sensor_id_t sensor_id, sensor_rep &prev_rep, sensor_rep &cur_rep);
50 static void restore_session(void);
51 static bool register_event(int handle, unsigned int event_type, unsigned int interval, int max_batch_latency, int cb_type, void* cb, void *user_data);
57 sensor_event_listener::get_instance().set_hup_observer(restore_session);
63 _I("Good bye! %s\n", get_client_name());
67 static initiator g_initiator;
69 static int g_power_save_state_cb_cnt = 0;
71 static void set_power_save_state_cb(void)
73 if (g_power_save_state_cb_cnt < 0)
74 _E("g_power_save_state_cb_cnt(%d) is wrong", g_power_save_state_cb_cnt);
76 ++g_power_save_state_cb_cnt;
78 if (g_power_save_state_cb_cnt == 1) {
79 _D("Power save callback is registered");
80 g_power_save_state = get_power_save_state();
81 _D("power_save_state = [%d]", g_power_save_state);
82 vconf_notify_key_changed(VCONFKEY_PM_STATE, power_save_state_cb, NULL);
86 static void unset_power_save_state_cb(void)
88 --g_power_save_state_cb_cnt;
90 if (g_power_save_state_cb_cnt < 0)
91 _E("g_power_save_state_cb_cnt(%d) is wrong", g_power_save_state_cb_cnt);
93 if (g_power_save_state_cb_cnt == 0) {
94 _D("Power save callback is unregistered");
95 vconf_ignore_key_changed(VCONFKEY_PM_STATE, power_save_state_cb);
101 handle_vector handles;
103 sensor_client_info::get_instance().get_all_handles(handles);
105 auto it_handle = handles.begin();
107 while (it_handle != handles.end()) {
108 sensord_disconnect(*it_handle);
113 static int get_power_save_state (void)
118 vconf_get_int(VCONFKEY_PM_STATE, &pm_state);
120 if (pm_state == VCONFKEY_PM_STATE_LCDOFF)
121 state |= SENSOR_OPTION_ON_IN_SCREEN_OFF;
126 static void power_save_state_cb(keynode_t *node, void *data)
128 int cur_power_save_state;
129 sensor_id_vector sensors;
130 sensor_rep prev_rep, cur_rep;
134 cur_power_save_state = get_power_save_state();
136 if (cur_power_save_state == g_power_save_state) {
137 _D("g_power_save_state NOT changed : [%d]", cur_power_save_state);
141 g_power_save_state = cur_power_save_state;
142 _D("power_save_state: %d noti to %s", g_power_save_state, get_client_name());
144 sensor_client_info::get_instance().get_listening_sensors(sensors);
146 auto it_sensor = sensors.begin();
148 while (it_sensor != sensors.end()) {
149 sensor_client_info::get_instance().get_sensor_rep(*it_sensor, prev_rep);
150 sensor_event_listener::get_instance().operate_sensor(*it_sensor, cur_power_save_state);
151 sensor_client_info::get_instance().get_sensor_rep(*it_sensor, cur_rep);
152 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());
244 static bool change_sensor_rep(sensor_id_t sensor_id, sensor_rep &prev_rep, sensor_rep &cur_rep)
247 command_channel *cmd_channel;
248 event_type_vector add_event_types, del_event_types;
250 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
251 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
255 client_id = sensor_client_info::get_instance().get_client_id();
256 retvm_if ((client_id < 0), false, "Invalid client id : %d, %s, %s", client_id, get_sensor_name(sensor_id), get_client_name());
258 get_events_diff(prev_rep.event_types, cur_rep.event_types, add_event_types, del_event_types);
260 if (cur_rep.active) {
261 if (prev_rep.option != cur_rep.option) {
262 if (!cmd_channel->cmd_set_option(cur_rep.option)) {
263 _E("Sending cmd_set_option(%d, %s, %d) failed for %s", client_id, get_sensor_name(sensor_id), cur_rep.option, get_client_name());
268 if ( (prev_rep.interval != cur_rep.interval) || (prev_rep.latency != cur_rep.latency)) {
269 if (!cmd_channel->cmd_set_batch(cur_rep.interval, cur_rep.latency)) {
270 _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());
275 if (!add_event_types.empty()) {
276 if (!cmd_channel->cmd_register_events(add_event_types)) {
277 _E("Sending cmd_register_events(%d, add_event_types) failed for %s", client_id, get_client_name());
284 if (prev_rep.active && !del_event_types.empty()) {
285 if (!cmd_channel->cmd_unregister_events(del_event_types)) {
286 _E("Sending cmd_unregister_events(%d, del_event_types) failed for %s", client_id, get_client_name());
291 if (prev_rep.active != cur_rep.active) {
292 if (cur_rep.active) {
293 if (!cmd_channel->cmd_start()) {
294 _E("Sending cmd_start(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
298 if (!cmd_channel->cmd_unset_batch()) {
299 _E("Sending cmd_unset_interval(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
303 if (!cmd_channel->cmd_stop()) {
304 _E("Sending cmd_stop(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
313 static bool get_sensor_list(void)
316 static bool init = false;
321 command_channel cmd_channel;
323 if (!cmd_channel.create_channel()) {
324 _E("%s failed to create command channel", get_client_name());
328 if (!cmd_channel.cmd_get_sensor_list())
337 API int sensord_get_sensor_list_ex(sensor_type_t type, sensor_t **list, int *sensor_count)
339 retvm_if (!get_sensor_list(), -EPERM, "Fail to get sensor list from server");
341 vector<sensor_info *> sensor_infos = sensor_info_manager::get_instance().get_infos(type);
343 if (sensor_infos.empty()) {
348 if (type != ALL_SENSOR) {
349 if (sensor_infos[0]->get_id() == (sensor_id_t)(-EACCES))
353 *list = (sensor_t *) malloc(sizeof(sensor_info *) * sensor_infos.size());
354 retvm_if(!*list, false, "Failed to allocate memory");
356 for (unsigned int i = 0; i < sensor_infos.size(); ++i)
357 *(*list + i) = sensor_info_to_sensor(sensor_infos[i]);
359 *sensor_count = sensor_infos.size();
364 API int sensord_get_sensor_ex(sensor_type_t type, sensor_t *sensor)
366 retvm_if (!get_sensor_list(), -EPERM, "Fail to get sensor list from server");
368 const sensor_info *info;
370 info = sensor_info_manager::get_instance().get_info(type);
376 if ((const_cast<sensor_info *>(info))->get_id() == (sensor_id_t)(-EACCES))
379 *sensor = sensor_info_to_sensor(info);
384 API bool sensord_get_sensor_list(sensor_type_t type, sensor_t **list, int *sensor_count)
386 return (sensord_get_sensor_list_ex(type, list, sensor_count) == OP_SUCCESS);
389 API sensor_t sensord_get_sensor(sensor_type_t type)
392 sensord_get_sensor_ex(type, &sensor);
397 API bool sensord_get_type(sensor_t sensor, sensor_type_t *type)
399 sensor_info* info = sensor_to_sensor_info(sensor);
401 retvm_if (!sensor_info_manager::get_instance().is_valid(info) || !type,
402 NULL, "Invalid param: sensor (%p), type(%p)", sensor, type);
404 *type = info->get_type();
409 API const char* sensord_get_name(sensor_t sensor)
411 sensor_info* info = sensor_to_sensor_info(sensor);
413 retvm_if (!sensor_info_manager::get_instance().is_valid(info),
414 NULL, "Invalid param: sensor (%p)", sensor);
416 return info->get_name();
419 API const char* sensord_get_vendor(sensor_t sensor)
421 sensor_info* info = sensor_to_sensor_info(sensor);
423 retvm_if (!sensor_info_manager::get_instance().is_valid(info),
424 NULL, "Invalid param: sensor (%p)", sensor);
426 return info->get_vendor();
429 API bool sensord_get_privilege(sensor_t sensor, sensor_privilege_t *privilege)
431 sensor_info* info = sensor_to_sensor_info(sensor);
433 retvm_if (!sensor_info_manager::get_instance().is_valid(info) || !privilege,
434 false, "Invalid param: sensor (%p), privilege(%p)", sensor, privilege);
436 *privilege = info->get_privilege();
441 API bool sensord_get_min_range(sensor_t sensor, float *min_range)
443 sensor_info* info = sensor_to_sensor_info(sensor);
445 retvm_if (!sensor_info_manager::get_instance().is_valid(info) || !min_range,
446 false, "Invalid param: sensor (%p), min_range(%p)", sensor, min_range);
448 *min_range = info->get_min_range();
453 API bool sensord_get_max_range(sensor_t sensor, float *max_range)
455 sensor_info* info = sensor_to_sensor_info(sensor);
457 retvm_if (!sensor_info_manager::get_instance().is_valid(info) || !max_range,
458 false, "Invalid param: sensor (%p), max_range(%p)", sensor, max_range);
460 *max_range = info->get_max_range();
465 API bool sensord_get_resolution(sensor_t sensor, float *resolution)
467 sensor_info* info = sensor_to_sensor_info(sensor);
469 retvm_if (!sensor_info_manager::get_instance().is_valid(info) || !resolution,
470 false, "Invalid param: sensor (%p), resolution(%p)", sensor, resolution);
472 *resolution = info->get_resolution();
477 API bool sensord_get_min_interval(sensor_t sensor, int *min_interval)
479 sensor_info* info = sensor_to_sensor_info(sensor);
481 retvm_if (!sensor_info_manager::get_instance().is_valid(info) || !min_interval,
482 false, "Invalid param: sensor (%p), min_interval(%p)", sensor, min_interval);
484 *min_interval = info->get_min_interval();
489 API bool sensord_get_fifo_count(sensor_t sensor, int *fifo_count)
491 sensor_info* info = sensor_to_sensor_info(sensor);
493 retvm_if (!sensor_info_manager::get_instance().is_valid(info) || !fifo_count,
494 false, "Invalid param: sensor (%p), fifo_count(%p)", sensor, fifo_count);
496 *fifo_count = info->get_fifo_count();
501 API bool sensord_get_max_batch_count(sensor_t sensor, int *max_batch_count)
503 sensor_info* info = sensor_to_sensor_info(sensor);
505 retvm_if (!sensor_info_manager::get_instance().is_valid(info) || !max_batch_count,
506 false, "Invalid param: sensor (%p), max_batch_count(%p)", sensor, max_batch_count);
508 *max_batch_count = info->get_max_batch_count();
513 API bool sensord_get_supported_event_types(sensor_t sensor, unsigned int **event_types, int *count)
515 sensor_info* info = sensor_to_sensor_info(sensor);
517 retvm_if (!sensor_info_manager::get_instance().is_valid(info) || !event_types || !count,
518 false, "Invalid param: sensor (%p), event_types(%p), count(%p)", sensor, event_types, count);
520 unsigned int event_type;
521 event_type = info->get_supported_event();
522 *event_types = (unsigned int *)malloc(sizeof(unsigned int));
524 retvm_if(!*event_types, false, "Failed to allocate memory");
526 (*event_types)[0] = event_type;
532 API bool sensord_is_supported_event_type(sensor_t sensor, unsigned int event_type, bool *supported)
534 sensor_info* info = sensor_to_sensor_info(sensor);
536 retvm_if (!sensor_info_manager::get_instance().is_valid(info) || !event_type || !supported,
537 false, "Invalid param: sensor (%p), event_type(%p), supported(%p)", sensor, event_type, supported);
539 *supported = info->is_supported_event(event_type);
544 API bool sensord_is_wakeup_supported(sensor_t sensor)
546 sensor_info* info = sensor_to_sensor_info(sensor);
548 retvm_if (!sensor_info_manager::get_instance().is_valid(info),
549 false, "Invalid param: sensor (%p)", sensor);
551 return info->is_wakeup_supported();
554 API int sensord_connect(sensor_t sensor)
556 command_channel *cmd_channel = NULL;
559 bool sensor_registered;
560 bool first_connection = false;
562 sensor_info* info = sensor_to_sensor_info(sensor);
564 retvm_if (!sensor_info_manager::get_instance().is_valid(info),
565 OP_ERROR, "Invalid param: sensor (%p)", sensor);
567 sensor_id_t sensor_id = info->get_id();
571 sensor_registered = sensor_client_info::get_instance().is_sensor_registered(sensor_id);
573 // lazy loading after creating static variables
576 handle = sensor_client_info::get_instance().create_handle(sensor_id);
577 if (handle == MAX_HANDLE) {
578 _E("Maximum number of handles reached, sensor: %s in client %s", get_sensor_name(sensor_id), get_client_name());
582 if (!sensor_registered) {
583 cmd_channel = new(std::nothrow) command_channel();
585 _E("Failed to allocated memory");
586 sensor_client_info::get_instance().delete_handle(handle);
590 if (!cmd_channel->create_channel()) {
591 _E("%s failed to create command channel for %s", get_client_name(), get_sensor_name(sensor_id));
592 sensor_client_info::get_instance().delete_handle(handle);
597 sensor_client_info::get_instance().add_command_channel(sensor_id, cmd_channel);
600 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
601 _E("%s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
602 sensor_client_info::get_instance().delete_handle(handle);
606 if (!sensor_client_info::get_instance().has_client_id()) {
607 first_connection = true;
608 if(!cmd_channel->cmd_get_id(client_id)) {
609 _E("Sending cmd_get_id() failed for %s", get_sensor_name(sensor_id));
610 sensor_client_info::get_instance().close_command_channel(sensor_id);
611 sensor_client_info::get_instance().delete_handle(handle);
615 sensor_client_info::get_instance().set_client_id(client_id);
616 _I("%s gets client_id [%d]", get_client_name(), client_id);
617 sensor_event_listener::get_instance().start_event_listener();
618 _I("%s starts listening events with client_id [%d]", get_client_name(), client_id);
621 client_id = sensor_client_info::get_instance().get_client_id();
622 cmd_channel->set_client_id(client_id);
624 _I("%s[%d] connects with %s[%d]", get_client_name(), client_id, get_sensor_name(sensor_id), handle);
626 sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_STOPPED, SENSOR_OPTION_DEFAULT);
628 if (!sensor_registered) {
629 if(!cmd_channel->cmd_hello(sensor_id)) {
630 _E("Sending cmd_hello(%s, %d) failed for %s", get_sensor_name(sensor_id), client_id, get_client_name());
631 sensor_client_info::get_instance().close_command_channel(sensor_id);
632 sensor_client_info::get_instance().delete_handle(handle);
633 if (first_connection) {
634 sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
635 sensor_event_listener::get_instance().stop_event_listener();
641 set_power_save_state_cb();
645 API bool sensord_disconnect(int handle)
647 command_channel *cmd_channel;
648 sensor_id_t sensor_id;
654 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
655 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
656 _E("client %s failed to get handle information", get_client_name());
660 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
661 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
665 client_id = sensor_client_info::get_instance().get_client_id();
666 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());
668 _I("%s disconnects with %s[%d]", get_client_name(), get_sensor_name(sensor_id), handle);
670 if (sensor_state != SENSOR_STATE_STOPPED) {
671 _W("%s[%d] for %s is not stopped before disconnecting.",
672 get_sensor_name(sensor_id), handle, get_client_name());
673 sensord_stop(handle);
676 if (!sensor_client_info::get_instance().delete_handle(handle))
679 if (!sensor_client_info::get_instance().is_active())
680 sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
682 if (!sensor_client_info::get_instance().is_sensor_registered(sensor_id)) {
683 if(!cmd_channel->cmd_byebye()) {
684 _E("Sending cmd_byebye(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
687 sensor_client_info::get_instance().close_command_channel(sensor_id);
690 if (!sensor_client_info::get_instance().is_active()) {
691 _I("Stop listening events for client %s with client id [%d]", get_client_name(), sensor_client_info::get_instance().get_client_id());
692 sensor_event_listener::get_instance().stop_event_listener();
695 unset_power_save_state_cb();
701 static bool register_event(int handle, unsigned int event_type, unsigned int interval, int max_batch_latency, int cb_type, void* cb, void *user_data)
703 sensor_id_t sensor_id;
704 sensor_rep prev_rep, cur_rep;
707 retvm_if (!cb, false, "callback is NULL");
711 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
712 _E("client %s failed to get handle information", get_client_name());
717 interval = DEFAULT_INTERVAL;
719 _I("%s registers event %s[0x%x] for sensor %s[%d] with interval: %d, latency: %d, cb: 0x%x, user_data: 0x%x",
720 get_client_name(), get_event_name(event_type), event_type, get_sensor_name(sensor_id),
721 handle, interval, max_batch_latency, cb, user_data);
723 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
724 sensor_client_info::get_instance().register_event(handle, event_type, interval, max_batch_latency, cb_type, cb, user_data);
725 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
726 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
729 sensor_client_info::get_instance().unregister_event(handle, event_type);
734 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)
736 return register_event(handle, event_type, interval, max_batch_latency, SENSOR_EVENT_CB, (void *)cb, user_data);
739 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)
741 return register_event(handle, event_type, interval, max_batch_latency, SENSORHUB_EVENT_CB, (void *)cb, user_data);
745 API bool sensord_unregister_event(int handle, unsigned int event_type)
747 sensor_id_t sensor_id;
748 sensor_rep prev_rep, cur_rep;
750 unsigned int prev_interval, prev_latency;
753 void *prev_user_data;
757 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
758 _E("client %s failed to get handle information", get_client_name());
762 _I("%s unregisters event %s[0x%x] for sensor %s[%d]", get_client_name(), get_event_name(event_type),
763 event_type, get_sensor_name(sensor_id), handle);
765 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
766 sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb_type, prev_cb, prev_user_data);
768 if (!sensor_client_info::get_instance().unregister_event(handle, event_type)) {
769 _E("%s try to unregister non registered event %s[0x%x] for sensor %s[%d]",
770 get_client_name(),get_event_name(event_type), event_type, get_sensor_name(sensor_id), handle);
774 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
775 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
778 sensor_client_info::get_instance().register_event(handle, event_type, prev_interval, prev_latency, prev_cb_type, prev_cb, prev_user_data);
785 API bool sensord_register_accuracy_cb(int handle, sensor_accuracy_changed_cb_t cb, void *user_data)
787 sensor_id_t sensor_id;
789 retvm_if (!cb, false, "callback is NULL");
793 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
794 _E("client %s failed to get handle information", get_client_name());
799 _I("%s registers accuracy_changed_cb for sensor %s[%d] with cb: 0x%x, user_data: 0x%x",
800 get_client_name(), get_sensor_name(sensor_id), handle, cb, user_data);
802 sensor_client_info::get_instance().register_accuracy_cb(handle, cb , user_data);
808 API bool sensord_unregister_accuracy_cb(int handle)
810 sensor_id_t sensor_id;
814 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
815 _E("client %s failed to get handle information", get_client_name());
820 _I("%s unregisters accuracy_changed_cb for sensor %s[%d]",
821 get_client_name(), get_sensor_name(sensor_id), handle);
823 sensor_client_info::get_instance().unregister_accuracy_cb(handle);
829 API bool sensord_start(int handle, int option)
831 sensor_id_t sensor_id;
832 sensor_rep prev_rep, cur_rep;
834 int prev_state, prev_option;
838 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
839 _E("client %s failed to get handle information", get_client_name());
843 retvm_if ((option < 0) || (option >= SENSOR_OPTION_END), false, "Invalid option value : %d, handle: %d, %s, %s",
844 option, handle, get_sensor_name(sensor_id), get_client_name());
846 _I("%s starts %s[%d], with option: %d, power save state: %d", get_client_name(), get_sensor_name(sensor_id),
847 handle, option, g_power_save_state);
849 if (g_power_save_state && !(g_power_save_state & option)) {
850 sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_PAUSED, option);
854 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
855 sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_option);
856 sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_STARTED, option);
857 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
859 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
862 sensor_client_info::get_instance().set_sensor_params(handle, prev_state, prev_option);
867 API bool sensord_stop(int handle)
869 sensor_id_t sensor_id;
872 int prev_state, prev_option;
874 sensor_rep prev_rep, cur_rep;
878 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
879 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
880 _E("client %s failed to get handle information", get_client_name());
884 retvm_if ((sensor_state == SENSOR_STATE_STOPPED), true, "%s already stopped with %s[%d]",
885 get_client_name(), get_sensor_name(sensor_id), handle);
888 _I("%s stops sensor %s[%d]", get_client_name(), get_sensor_name(sensor_id), handle);
890 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
891 sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_option);
892 sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_STOPPED);
893 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
895 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
898 sensor_client_info::get_instance().set_sensor_params(handle, prev_state, prev_option);
904 static bool change_event_batch(int handle, unsigned int event_type, unsigned int interval, unsigned int latency)
906 sensor_id_t sensor_id;
907 sensor_rep prev_rep, cur_rep;
909 unsigned int prev_interval, prev_latency;
912 void *prev_user_data;
916 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
917 _E("client %s failed to get handle information", get_client_name());
922 interval = DEFAULT_INTERVAL;
924 _I("%s changes batch of event %s[0x%x] for %s[%d] to (%d, %d)", get_client_name(), get_event_name(event_type),
925 event_type, get_sensor_name(sensor_id), handle, interval, latency);
927 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
929 sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb_type, prev_cb, prev_user_data);
931 if (!sensor_client_info::get_instance().set_event_batch(handle, event_type, interval, latency))
934 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
936 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
939 sensor_client_info::get_instance().set_event_batch(handle, event_type, prev_interval, prev_latency);
944 API bool sensord_change_event_interval(int handle, unsigned int event_type, unsigned int interval)
946 unsigned int prev_interval, prev_latency;
949 void *prev_user_data;
953 if (!sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb_type, prev_cb, prev_user_data)) {
954 _E("Failed to get event info with handle = %d, event_type = 0x%x", handle, event_type);
958 _I("handle = %d, event_type = 0x%x, interval = %d, prev_latency = %d", handle, event_type, interval, prev_latency);
959 return change_event_batch(handle, event_type, interval, prev_latency);
962 API bool sensord_change_event_max_batch_latency(int handle, unsigned int event_type, unsigned int max_batch_latency)
964 unsigned int prev_interval, prev_latency;
967 void *prev_user_data;
971 if (!sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb_type, prev_cb, prev_user_data)) {
972 _E("Failed to get event info with handle = %d, event_type = 0x%x", handle, event_type);
976 return change_event_batch(handle, event_type, prev_interval, max_batch_latency);
979 API bool sensord_change_event_maincontext(int handle, unsigned int event_type, GMainContext *maincontext)
983 if (!sensor_client_info::get_instance().set_event_maincontext(handle, event_type, maincontext)) {
984 _E("Failed to get event info with handle = %d, event_type = 0x%x, maincontext = 0x%x", handle, event_type, maincontext);
988 _I("handle = %d, event_type = 0x%x, maincontext = 0x%x", handle, event_type, maincontext);
992 API bool sensord_set_option(int handle, int option)
994 sensor_id_t sensor_id;
995 sensor_rep prev_rep, cur_rep;
998 int prev_state, prev_option;
1002 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
1003 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1004 _E("client %s failed to get handle information", get_client_name());
1008 retvm_if ((option < 0) || (option >= SENSOR_OPTION_END), false, "Invalid option value : %d, handle: %d, %s, %s",
1009 option, handle, get_sensor_name(sensor_id), get_client_name());
1012 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
1013 sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_option);
1015 if (g_power_save_state) {
1016 if ((option & g_power_save_state) && (sensor_state == SENSOR_STATE_PAUSED))
1017 sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_STARTED);
1018 else if (!(option & g_power_save_state) && (sensor_state == SENSOR_STATE_STARTED))
1019 sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_PAUSED);
1021 sensor_client_info::get_instance().set_sensor_option(handle, option);
1023 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
1024 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
1027 sensor_client_info::get_instance().set_sensor_option(handle, prev_option);
1033 API int sensord_set_attribute_int(int handle, int attribute, int value)
1035 sensor_id_t sensor_id;
1036 command_channel *cmd_channel;
1041 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1042 _E("client %s failed to get handle information", get_client_name());
1046 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1047 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
1051 client_id = sensor_client_info::get_instance().get_client_id();
1052 retvm_if ((client_id < 0), -EPERM,
1053 "Invalid client id : %d, handle: %d, %s, %s",
1054 client_id, handle, get_sensor_name(sensor_id), get_client_name());
1056 if (!cmd_channel->cmd_set_attribute_int(attribute, value)) {
1057 _E("Sending cmd_set_attribute_int(%d, %d) failed for %s",
1058 client_id, value, get_client_name);
1065 API int sensord_set_attribute_str(int handle, int attribute, const char *value, int value_len)
1067 sensor_id_t sensor_id;
1068 command_channel *cmd_channel;
1073 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1074 _E("Client %s failed to get handle information", get_client_name());
1078 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1079 _E("Client %s failed to get command channel for %s",
1080 get_client_name(), get_sensor_name(sensor_id));
1084 retvm_if((value_len < 0) || (value == NULL), -EINVAL,
1085 "Invalid value_len: %d, value: 0x%x, handle: %d, %s, %s",
1086 value_len, value, handle, get_sensor_name(sensor_id), get_client_name());
1088 client_id = sensor_client_info::get_instance().get_client_id();
1089 retvm_if ((client_id < 0), -EPERM,
1090 "Invalid client id : %d, handle: %d, %s, %s",
1091 client_id, handle, get_sensor_name(sensor_id), get_client_name());
1093 if (!cmd_channel->cmd_set_attribute_str(attribute, value, value_len)) {
1094 _E("Sending cmd_set_attribute_str(%d, %d, 0x%x) failed for %s",
1095 client_id, value_len, value, get_client_name);
1102 API bool sensord_send_sensorhub_data(int handle, const char *data, int data_len)
1104 return (sensord_set_attribute_str(handle, 0, data, data_len) == OP_SUCCESS);
1107 API bool sensord_send_command(int handle, const char *command, int command_len)
1109 return (sensord_set_attribute_str(handle, 0, command, command_len) == OP_SUCCESS);
1112 API bool sensord_get_data(int handle, unsigned int data_id, sensor_data_t* sensor_data)
1114 sensor_id_t sensor_id;
1115 command_channel *cmd_channel;
1119 retvm_if ((!sensor_data), false, "sensor_data is NULL");
1123 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
1124 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1125 _E("client %s failed to get handle information", get_client_name());
1129 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1130 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
1134 client_id = sensor_client_info::get_instance().get_client_id();
1135 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());
1137 if (sensor_state != SENSOR_STATE_STARTED) {
1138 _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);
1142 if (!cmd_channel->cmd_get_data(data_id, sensor_data)) {
1143 _E("cmd_get_data(%d, %d, 0x%x) failed for %s", client_id, data_id, sensor_data, get_client_name());
1150 API bool sensord_flush(int handle)
1152 sensor_id_t sensor_id;
1153 command_channel *cmd_channel;
1159 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state) ||
1160 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1161 _E("client %s failed to get handle information", get_client_name());
1165 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1166 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
1170 client_id = sensor_client_info::get_instance().get_client_id();
1171 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());
1173 if (sensor_state != SENSOR_STATE_STARTED) {
1174 _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);
1178 if (!cmd_channel->cmd_flush()) {
1179 _E("cmd_flush() failed for %s", get_client_name());