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) \
48 (option == SENSOR_OPTION_DEFAULT || option == SENSOR_OPTION_ALWAYS_ON) ? \
49 (option ^ 0b11) : option
53 static int g_power_save_state = SENSORD_PAUSE_NONE;
55 static int get_power_save_state(void);
56 static void power_save_state_cb(keynode_t *node, void *data);
57 static void clean_up(void);
58 static bool change_sensor_rep(sensor_id_t sensor_id, sensor_rep &prev_rep, sensor_rep &cur_rep);
59 static void restore_session(void);
60 static bool register_event(int handle, unsigned int event_type, unsigned int interval, int max_batch_latency, void* cb, void *user_data);
66 sensor_event_listener::get_instance().set_hup_observer(restore_session);
72 _I("Good bye! %s\n", get_client_name());
76 static initiator g_initiator;
78 static int g_power_save_state_cb_cnt = 0;
80 static void set_power_save_state_cb(void)
82 if (g_power_save_state_cb_cnt < 0)
83 _E("g_power_save_state_cb_cnt(%d) is wrong", g_power_save_state_cb_cnt);
85 ++g_power_save_state_cb_cnt;
87 if (g_power_save_state_cb_cnt == 1) {
88 _D("Power save callback is registered");
89 g_power_save_state = get_power_save_state();
90 _D("power_save_state = [%d]", g_power_save_state);
91 vconf_notify_key_changed(VCONFKEY_PM_STATE, power_save_state_cb, NULL);
92 vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, power_save_state_cb, NULL);
96 static void unset_power_save_state_cb(void)
98 --g_power_save_state_cb_cnt;
100 if (g_power_save_state_cb_cnt < 0)
101 _E("g_power_save_state_cb_cnt(%d) is wrong", g_power_save_state_cb_cnt);
103 if (g_power_save_state_cb_cnt == 0) {
104 _D("Power save callback is unregistered");
105 vconf_ignore_key_changed(VCONFKEY_PM_STATE, power_save_state_cb);
106 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE, power_save_state_cb);
112 handle_vector handles;
114 sensor_client_info::get_instance().get_all_handles(handles);
116 auto it_handle = handles.begin();
118 while (it_handle != handles.end()) {
119 sensord_disconnect(*it_handle);
124 static int get_power_save_state(void)
128 int pm_state, ps_state;
130 ret = vconf_get_int(VCONFKEY_PM_STATE, &pm_state);
132 if (!ret && pm_state == VCONFKEY_PM_STATE_LCDOFF)
133 state |= SENSORD_PAUSE_ON_DISPLAY_OFF;
135 ret = vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ps_state);
137 if (!ret && ps_state != SETTING_PSMODE_NORMAL)
138 state |= SENSORD_PAUSE_ON_POWERSAVE_MODE;
143 static void power_save_state_cb(keynode_t *node, void *data)
145 int cur_power_save_state;
146 sensor_id_vector sensors;
147 sensor_rep prev_rep, cur_rep;
151 cur_power_save_state = get_power_save_state();
153 if (cur_power_save_state == g_power_save_state) {
154 _D("g_power_save_state NOT changed : [%d]", cur_power_save_state);
158 g_power_save_state = cur_power_save_state;
159 _D("power_save_state: %d noti to %s", g_power_save_state, get_client_name());
161 sensor_client_info::get_instance().get_listening_sensors(sensors);
163 auto it_sensor = sensors.begin();
165 while (it_sensor != sensors.end()) {
166 sensor_client_info::get_instance().get_sensor_rep(*it_sensor, prev_rep);
167 sensor_client_info::get_instance().set_pause_policy(*it_sensor, cur_power_save_state);
168 sensor_client_info::get_instance().get_sensor_rep(*it_sensor, cur_rep);
169 change_sensor_rep(*it_sensor, prev_rep, cur_rep);
175 void restore_session(void)
179 _I("Trying to restore sensor client session for %s", get_client_name());
181 command_channel *cmd_channel;
184 sensor_client_info::get_instance().close_command_channel();
185 sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
187 sensor_id_vector sensors;
189 sensor_client_info::get_instance().get_listening_sensors(sensors);
191 bool first_connection = true;
193 auto it_sensor = sensors.begin();
195 while (it_sensor != sensors.end()) {
196 cmd_channel = new(std::nothrow) command_channel();
197 retm_if(!cmd_channel, "Failed to allocate memory");
199 if (!cmd_channel->create_channel()) {
200 _E("%s failed to create command channel for %s", get_client_name(), get_sensor_name(*it_sensor));
205 sensor_client_info::get_instance().add_command_channel(*it_sensor, cmd_channel);
207 if (first_connection) {
208 first_connection = false;
209 if (!cmd_channel->cmd_get_id(client_id)) {
210 _E("Failed to get client id");
214 sensor_client_info::get_instance().set_client_id(client_id);
215 sensor_event_listener::get_instance().start_event_listener();
218 cmd_channel->set_client_id(client_id);
220 if (!cmd_channel->cmd_hello(*it_sensor)) {
221 _E("Sending cmd_hello(%s, %d) failed for %s", get_sensor_name(*it_sensor), client_id, get_client_name());
225 sensor_rep prev_rep, cur_rep;
226 prev_rep.active = false;
227 prev_rep.pause_policy = SENSORD_PAUSE_ALL;
228 prev_rep.interval = 0;
230 sensor_client_info::get_instance().get_sensor_rep(*it_sensor, cur_rep);
231 if (!change_sensor_rep(*it_sensor, prev_rep, cur_rep)) {
232 _E("Failed to change rep(%s) for %s", get_sensor_name(*it_sensor), get_client_name());
239 _I("Succeeded to restore sensor client session for %s", get_client_name());
244 sensor_event_listener::get_instance().clear();
245 _E("Failed to restore session for %s", get_client_name());
248 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)
250 sort(a_vec.begin(), a_vec.end());
251 sort(b_vec.begin(), b_vec.end());
253 set_difference(a_vec.begin(), a_vec.end(), b_vec.begin(), b_vec.end(), back_inserter(del_vec));
254 set_difference(b_vec.begin(), b_vec.end(), a_vec.begin(), a_vec.end(), back_inserter(add_vec));
256 return !(add_vec.empty() && del_vec.empty());
259 static bool change_sensor_rep(sensor_id_t sensor_id, sensor_rep &prev_rep, sensor_rep &cur_rep)
262 command_channel *cmd_channel;
263 event_type_vector add_event_types, del_event_types;
265 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
266 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
270 client_id = sensor_client_info::get_instance().get_client_id();
271 retvm_if((client_id < 0), false, "Invalid client id : %d, %s, %s", client_id, get_sensor_name(sensor_id), get_client_name());
273 get_events_diff(prev_rep.event_types, cur_rep.event_types, add_event_types, del_event_types);
275 if (cur_rep.active) {
276 if (prev_rep.pause_policy != cur_rep.pause_policy) {
277 if (!cmd_channel->cmd_set_pause_policy(cur_rep.pause_policy)) {
278 _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());
283 if ( (prev_rep.interval != cur_rep.interval) || (prev_rep.latency != cur_rep.latency)) {
284 if (!cmd_channel->cmd_set_batch(cur_rep.interval, cur_rep.latency)) {
285 _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());
290 if (!add_event_types.empty()) {
291 if (!cmd_channel->cmd_register_events(add_event_types)) {
292 _E("Sending cmd_register_events(%d, add_event_types) failed for %s", client_id, get_client_name());
298 if (prev_rep.active && !del_event_types.empty()) {
299 if (!cmd_channel->cmd_unregister_events(del_event_types)) {
300 _E("Sending cmd_unregister_events(%d, del_event_types) failed for %s", client_id, get_client_name());
305 if (prev_rep.active != cur_rep.active) {
306 if (cur_rep.active) {
307 if (!cmd_channel->cmd_start()) {
308 _E("Sending cmd_start(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
312 if (!cmd_channel->cmd_unset_batch()) {
313 _E("Sending cmd_unset_interval(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
317 if (!cmd_channel->cmd_stop()) {
318 _E("Sending cmd_stop(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
327 static bool get_sensor_list(void)
330 static bool init = false;
335 command_channel cmd_channel;
337 if (!cmd_channel.create_channel()) {
338 _E("%s failed to create command channel", get_client_name());
342 if (!cmd_channel.cmd_get_sensor_list())
351 API int sensord_get_sensors(sensor_type_t type, sensor_t **list, int *sensor_count)
353 retvm_if(!get_sensor_list(), -EPERM, "Fail to get sensor list from server");
355 vector<sensor_info *> sensor_infos = sensor_info_manager::get_instance().get_infos(type);
357 if (sensor_infos.empty()) {
362 if (type != ALL_SENSOR) {
363 if (sensor_infos[0]->get_id() == (sensor_id_t)(-EACCES))
367 *list = (sensor_t *) malloc(sizeof(sensor_info *) * sensor_infos.size());
368 retvm_if(!*list, false, "Failed to allocate memory");
370 for (unsigned int i = 0; i < sensor_infos.size(); ++i)
371 *(*list + i) = sensor_info_to_sensor(sensor_infos[i]);
373 *sensor_count = sensor_infos.size();
378 API int sensord_get_default_sensor(sensor_type_t type, sensor_t *sensor)
380 retvm_if(!get_sensor_list(), -EPERM, "Fail to get sensor list from server");
382 const sensor_info *info;
384 info = sensor_info_manager::get_instance().get_info(type);
390 if ((const_cast<sensor_info *>(info))->get_id() == (sensor_id_t)(-EACCES))
393 *sensor = sensor_info_to_sensor(info);
398 API bool sensord_get_sensor_list(sensor_type_t type, sensor_t **list, int *sensor_count)
400 return (sensord_get_sensors(type, list, sensor_count) == OP_SUCCESS);
403 API sensor_t sensord_get_sensor(sensor_type_t type)
406 sensord_get_default_sensor(type, &sensor);
411 API bool sensord_get_type(sensor_t sensor, sensor_type_t *type)
413 sensor_info* info = sensor_to_sensor_info(sensor);
415 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !type,
416 NULL, "Invalid param: sensor (%p), type(%p)", sensor, type);
418 *type = info->get_type();
423 API const char* sensord_get_name(sensor_t sensor)
425 sensor_info* info = sensor_to_sensor_info(sensor);
427 retvm_if(!sensor_info_manager::get_instance().is_valid(info),
428 NULL, "Invalid param: sensor (%p)", sensor);
430 return info->get_name();
433 API const char* sensord_get_vendor(sensor_t sensor)
435 sensor_info* info = sensor_to_sensor_info(sensor);
437 retvm_if(!sensor_info_manager::get_instance().is_valid(info),
438 NULL, "Invalid param: sensor (%p)", sensor);
440 return info->get_vendor();
443 API bool sensord_get_privilege(sensor_t sensor, sensor_privilege_t *privilege)
445 sensor_info* info = sensor_to_sensor_info(sensor);
447 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !privilege,
448 false, "Invalid param: sensor (%p), privilege(%p)", sensor, privilege);
450 *privilege = info->get_privilege();
455 API bool sensord_get_min_range(sensor_t sensor, float *min_range)
457 sensor_info* info = sensor_to_sensor_info(sensor);
459 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !min_range,
460 false, "Invalid param: sensor (%p), min_range(%p)", sensor, min_range);
462 *min_range = info->get_min_range();
467 API bool sensord_get_max_range(sensor_t sensor, float *max_range)
469 sensor_info* info = sensor_to_sensor_info(sensor);
471 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !max_range,
472 false, "Invalid param: sensor (%p), max_range(%p)", sensor, max_range);
474 *max_range = info->get_max_range();
479 API bool sensord_get_resolution(sensor_t sensor, float *resolution)
481 sensor_info* info = sensor_to_sensor_info(sensor);
483 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !resolution,
484 false, "Invalid param: sensor (%p), resolution(%p)", sensor, resolution);
486 *resolution = info->get_resolution();
491 API bool sensord_get_min_interval(sensor_t sensor, int *min_interval)
493 sensor_info* info = sensor_to_sensor_info(sensor);
495 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !min_interval,
496 false, "Invalid param: sensor (%p), min_interval(%p)", sensor, min_interval);
498 *min_interval = info->get_min_interval();
503 API bool sensord_get_fifo_count(sensor_t sensor, int *fifo_count)
505 sensor_info* info = sensor_to_sensor_info(sensor);
507 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !fifo_count,
508 false, "Invalid param: sensor (%p), fifo_count(%p)", sensor, fifo_count);
510 *fifo_count = info->get_fifo_count();
515 API bool sensord_get_max_batch_count(sensor_t sensor, int *max_batch_count)
517 sensor_info* info = sensor_to_sensor_info(sensor);
519 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !max_batch_count,
520 false, "Invalid param: sensor (%p), max_batch_count(%p)", sensor, max_batch_count);
522 *max_batch_count = info->get_max_batch_count();
527 API bool sensord_get_supported_event_types(sensor_t sensor, unsigned int **event_types, int *count)
529 sensor_info* info = sensor_to_sensor_info(sensor);
531 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !event_types || !count,
532 false, "Invalid param: sensor (%p), event_types(%p), count(%p)", sensor, event_types, count);
534 unsigned int event_type;
535 event_type = info->get_supported_event();
536 *event_types = (unsigned int *)malloc(sizeof(unsigned int));
538 retvm_if(!*event_types, false, "Failed to allocate memory");
540 (*event_types)[0] = event_type;
546 API bool sensord_is_supported_event_type(sensor_t sensor, unsigned int event_type, bool *supported)
548 sensor_info* info = sensor_to_sensor_info(sensor);
550 retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !event_type || !supported,
551 false, "Invalid param: sensor (%p), event_type(%p), supported(%p)", sensor, event_type, supported);
553 *supported = info->is_supported_event(event_type);
558 API bool sensord_is_wakeup_supported(sensor_t sensor)
560 sensor_info* info = sensor_to_sensor_info(sensor);
562 retvm_if(!sensor_info_manager::get_instance().is_valid(info),
563 false, "Invalid param: sensor (%p)", sensor);
565 return info->is_wakeup_supported();
568 API int sensord_connect(sensor_t sensor)
570 command_channel *cmd_channel = NULL;
573 bool sensor_registered;
574 bool first_connection = false;
576 sensor_info* info = sensor_to_sensor_info(sensor);
578 retvm_if(!sensor_info_manager::get_instance().is_valid(info),
579 OP_ERROR, "Invalid param: sensor (%p)", sensor);
581 sensor_id_t sensor_id = info->get_id();
585 sensor_registered = sensor_client_info::get_instance().is_sensor_registered(sensor_id);
587 // lazy loading after creating static variables
590 handle = sensor_client_info::get_instance().create_handle(sensor_id);
591 if (handle == MAX_HANDLE) {
592 _E("Maximum number of handles reached, sensor: %s in client %s", get_sensor_name(sensor_id), get_client_name());
596 if (!sensor_registered) {
597 cmd_channel = new(std::nothrow) command_channel();
599 _E("Failed to allocated memory");
600 sensor_client_info::get_instance().delete_handle(handle);
604 if (!cmd_channel->create_channel()) {
605 _E("%s failed to create command channel for %s", get_client_name(), get_sensor_name(sensor_id));
606 sensor_client_info::get_instance().delete_handle(handle);
611 sensor_client_info::get_instance().add_command_channel(sensor_id, cmd_channel);
614 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
615 _E("%s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
616 sensor_client_info::get_instance().delete_handle(handle);
620 if (!sensor_client_info::get_instance().has_client_id()) {
621 first_connection = true;
622 if (!cmd_channel->cmd_get_id(client_id)) {
623 _E("Sending cmd_get_id() failed for %s", get_sensor_name(sensor_id));
624 sensor_client_info::get_instance().close_command_channel(sensor_id);
625 sensor_client_info::get_instance().delete_handle(handle);
629 sensor_client_info::get_instance().set_client_id(client_id);
630 _I("%s gets client_id [%d]", get_client_name(), client_id);
631 sensor_event_listener::get_instance().start_event_listener();
632 _I("%s starts listening events with client_id [%d]", get_client_name(), client_id);
635 client_id = sensor_client_info::get_instance().get_client_id();
636 cmd_channel->set_client_id(client_id);
638 _I("%s[%d] connects with %s[%d]", get_client_name(), client_id, get_sensor_name(sensor_id), handle);
640 sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_STOPPED, SENSORD_PAUSE_ALL);
642 if (!sensor_registered) {
643 if (!cmd_channel->cmd_hello(sensor_id)) {
644 _E("Sending cmd_hello(%s, %d) failed for %s", get_sensor_name(sensor_id), client_id, get_client_name());
645 sensor_client_info::get_instance().close_command_channel(sensor_id);
646 sensor_client_info::get_instance().delete_handle(handle);
647 if (first_connection) {
648 sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
649 sensor_event_listener::get_instance().stop_event_listener();
655 set_power_save_state_cb();
656 dbus_listener::init();
660 API bool sensord_disconnect(int handle)
662 command_channel *cmd_channel;
663 sensor_id_t sensor_id;
669 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
670 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
671 _E("client %s failed to get handle information", get_client_name());
675 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
676 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
680 client_id = sensor_client_info::get_instance().get_client_id();
681 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());
683 _I("%s disconnects with %s[%d]", get_client_name(), get_sensor_name(sensor_id), handle);
685 if (sensor_state != SENSOR_STATE_STOPPED) {
686 _W("%s[%d] for %s is not stopped before disconnecting.",
687 get_sensor_name(sensor_id), handle, get_client_name());
688 sensord_stop(handle);
691 if (!sensor_client_info::get_instance().delete_handle(handle))
694 if (!sensor_client_info::get_instance().is_active())
695 sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
697 if (!sensor_client_info::get_instance().is_sensor_registered(sensor_id)) {
698 if (!cmd_channel->cmd_byebye()) {
699 _E("Sending cmd_byebye(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
702 sensor_client_info::get_instance().close_command_channel(sensor_id);
705 if (!sensor_client_info::get_instance().is_active()) {
706 _I("Stop listening events for client %s with client id [%d]", get_client_name(), sensor_client_info::get_instance().get_client_id());
707 sensor_event_listener::get_instance().stop_event_listener();
710 unset_power_save_state_cb();
715 static bool register_event(int handle, unsigned int event_type, unsigned int interval, int max_batch_latency, void* cb, void *user_data)
717 sensor_id_t sensor_id;
718 sensor_rep prev_rep, cur_rep;
721 retvm_if(!cb, false, "callback is NULL");
725 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
726 _E("client %s failed to get handle information", get_client_name());
731 interval = DEFAULT_INTERVAL;
733 _I("%s registers event %s[%#x] for sensor %s[%d] with interval: %d, latency: %d, cb: %#x, user_data: %#x",
734 get_client_name(), get_event_name(event_type), event_type, get_sensor_name(sensor_id),
735 handle, interval, max_batch_latency, cb, user_data);
737 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
738 sensor_client_info::get_instance().register_event(handle, event_type, interval, max_batch_latency, cb, user_data);
739 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
740 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
743 sensor_client_info::get_instance().unregister_event(handle, event_type);
748 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)
750 return register_event(handle, event_type, interval, max_batch_latency, (void *)cb, user_data);
753 API bool sensord_unregister_event(int handle, unsigned int event_type)
755 sensor_id_t sensor_id;
756 sensor_rep prev_rep, cur_rep;
758 unsigned int prev_interval, prev_latency;
760 void *prev_user_data;
764 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
765 _E("client %s failed to get handle information", get_client_name());
769 _I("%s unregisters event %s[%#x] for sensor %s[%d]", get_client_name(), get_event_name(event_type),
770 event_type, get_sensor_name(sensor_id), handle);
772 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
773 sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data);
775 if (!sensor_client_info::get_instance().unregister_event(handle, event_type)) {
776 _E("%s try to unregister non registered event %s[%#x] for sensor %s[%d]",
777 get_client_name(), get_event_name(event_type), event_type, get_sensor_name(sensor_id), handle);
781 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
782 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
785 sensor_client_info::get_instance().register_event(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data);
790 API bool sensord_register_accuracy_cb(int handle, sensor_accuracy_changed_cb_t cb, void *user_data)
792 sensor_id_t sensor_id;
794 retvm_if(!cb, false, "callback is NULL");
798 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
799 _E("client %s failed to get handle information", get_client_name());
803 _I("%s registers accuracy_changed_cb for sensor %s[%d] with cb: %#x, user_data: %#x",
804 get_client_name(), get_sensor_name(sensor_id), handle, cb, user_data);
806 sensor_client_info::get_instance().register_accuracy_cb(handle, cb , user_data);
811 API bool sensord_unregister_accuracy_cb(int handle)
813 sensor_id_t sensor_id;
817 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
818 _E("client %s failed to get handle information", get_client_name());
822 _I("%s unregisters accuracy_changed_cb for sensor %s[%d]",
823 get_client_name(), get_sensor_name(sensor_id), handle);
825 sensor_client_info::get_instance().unregister_accuracy_cb(handle);
830 API bool sensord_start(int handle, int option)
832 sensor_id_t sensor_id;
833 sensor_rep prev_rep, cur_rep;
835 int prev_state, prev_pause;
840 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
841 _E("client %s failed to get handle information", get_client_name());
845 retvm_if((option < 0) || (option >= SENSOR_OPTION_END), false, "Invalid option value : %d, handle: %d, %s, %s",
846 option, handle, get_sensor_name(sensor_id), get_client_name());
848 _I("%s starts %s[%d], with option: %d, power save state: %d", get_client_name(), get_sensor_name(sensor_id),
849 handle, option, g_power_save_state);
851 pause = CONVERT_OPTION_PAUSE_POLICY(option);
853 if (g_power_save_state && (g_power_save_state & pause)) {
854 sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_PAUSED, pause);
858 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
859 sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_pause);
860 sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_STARTED, pause);
861 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
863 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
866 sensor_client_info::get_instance().set_sensor_params(handle, prev_state, prev_pause);
871 API bool sensord_stop(int handle)
873 sensor_id_t sensor_id;
876 int prev_state, prev_pause;
878 sensor_rep prev_rep, cur_rep;
882 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
883 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
884 _E("client %s failed to get handle information", get_client_name());
888 retvm_if((sensor_state == SENSOR_STATE_STOPPED), true, "%s already stopped with %s[%d]",
889 get_client_name(), get_sensor_name(sensor_id), handle);
891 _I("%s stops sensor %s[%d]", get_client_name(), get_sensor_name(sensor_id), handle);
893 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
894 sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_pause);
895 sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_STOPPED);
896 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
898 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
901 sensor_client_info::get_instance().set_sensor_params(handle, prev_state, prev_pause);
906 static bool change_event_batch(int handle, unsigned int event_type, unsigned int interval, unsigned int latency)
908 sensor_id_t sensor_id;
909 sensor_rep prev_rep, cur_rep;
911 unsigned int prev_interval, prev_latency;
913 void *prev_user_data;
917 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
918 _E("client %s failed to get handle information", get_client_name());
923 interval = DEFAULT_INTERVAL;
925 _I("%s changes batch of event %s[%#x] for %s[%d] to (%d, %d)", get_client_name(), get_event_name(event_type),
926 event_type, get_sensor_name(sensor_id), handle, interval, latency);
928 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
930 sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data);
932 if (!sensor_client_info::get_instance().set_event_batch(handle, event_type, interval, latency))
935 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
937 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
940 sensor_client_info::get_instance().set_event_batch(handle, event_type, prev_interval, prev_latency);
945 API bool sensord_change_event_interval(int handle, unsigned int event_type, unsigned int interval)
947 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, prev_user_data)) {
954 _E("Failed to get event info with handle = %d, event_type = %#x", handle, event_type);
958 _I("handle = %d, event_type = %#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;
966 void *prev_user_data;
970 if (!sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data)) {
971 _E("Failed to get event info with handle = %d, event_type = %#x", handle, event_type);
975 return change_event_batch(handle, event_type, prev_interval, max_batch_latency);
978 static int change_pause_policy(int handle, int pause)
980 sensor_id_t sensor_id;
981 sensor_rep prev_rep, cur_rep;
984 int prev_state, prev_pause;
988 retvm_if((pause < 0) || (pause >= SENSORD_PAUSE_END), -EINVAL,
989 "Invalid pause value : %d, handle: %d, %s, %s",
990 pause, handle, get_sensor_name(sensor_id), get_client_name());
992 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
993 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
994 _E("client %s failed to get handle information", get_client_name());
998 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
999 sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_pause);
1001 if (g_power_save_state) {
1002 if ((pause & g_power_save_state) && (sensor_state == SENSOR_STATE_STARTED))
1003 sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_PAUSED);
1004 else if (!(pause & g_power_save_state) && (sensor_state == SENSOR_STATE_PAUSED))
1005 sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_STARTED);
1007 sensor_client_info::get_instance().set_sensor_pause_policy(handle, pause);
1009 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
1010 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
1013 sensor_client_info::get_instance().set_sensor_pause_policy(handle, prev_pause);
1015 return (ret ? OP_SUCCESS : OP_ERROR);
1018 static int change_axis_orientation(int handle, int axis_orientation)
1023 static int change_attribute_int(int handle, int attribute, int value)
1025 sensor_id_t sensor_id;
1026 command_channel *cmd_channel;
1031 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1032 _E("client %s failed to get handle information", get_client_name());
1036 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1037 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
1041 client_id = sensor_client_info::get_instance().get_client_id();
1042 retvm_if((client_id < 0), -EPERM,
1043 "Invalid client id : %d, handle: %d, %s, %s",
1044 client_id, handle, get_sensor_name(sensor_id), get_client_name());
1046 if (!cmd_channel->cmd_set_attribute_int(attribute, value)) {
1047 _E("Sending cmd_set_attribute_int(%d, %d) failed for %s",
1048 client_id, value, get_client_name());
1055 API bool sensord_set_option(int handle, int option)
1057 return (change_pause_policy(handle, CONVERT_OPTION_PAUSE_POLICY(option)) == OP_SUCCESS);
1060 API int sensord_set_attribute_int(int handle, int attribute, int value)
1062 switch (attribute) {
1063 case SENSORD_ATTRIBUTE_PAUSE_POLICY:
1064 return change_pause_policy(handle, value);
1065 case SENSORD_ATTRIBUTE_AXIS_ORIENTATION:
1066 return change_axis_orientation(handle, value);
1068 return change_attribute_int(handle, attribute, value);
1074 API int sensord_set_attribute_str(int handle, int attribute, const char *value, int value_len)
1076 sensor_id_t sensor_id;
1077 command_channel *cmd_channel;
1082 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1083 _E("Client %s failed to get handle information", get_client_name());
1087 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1088 _E("Client %s failed to get command channel for %s",
1089 get_client_name(), get_sensor_name(sensor_id));
1093 retvm_if((value_len < 0) || (value == NULL), -EINVAL,
1094 "Invalid value_len: %d, value: %#x, handle: %d, %s, %s",
1095 value_len, value, handle, get_sensor_name(sensor_id), get_client_name());
1097 client_id = sensor_client_info::get_instance().get_client_id();
1098 retvm_if((client_id < 0), -EPERM,
1099 "Invalid client id : %d, handle: %d, %s, %s",
1100 client_id, handle, get_sensor_name(sensor_id), get_client_name());
1102 if (!cmd_channel->cmd_set_attribute_str(attribute, value, value_len)) {
1103 _E("Sending cmd_set_attribute_str(%d, %d, %#x) failed for %s",
1104 client_id, value_len, value, get_client_name());
1111 API bool sensord_send_sensorhub_data(int handle, const char *data, int data_len)
1113 return (sensord_set_attribute_str(handle, 0, data, data_len) == OP_SUCCESS);
1116 API bool sensord_send_command(int handle, const char *command, int command_len)
1118 return (sensord_set_attribute_str(handle, 0, command, command_len) == OP_SUCCESS);
1121 API bool sensord_get_data(int handle, unsigned int data_id, sensor_data_t* sensor_data)
1123 sensor_id_t sensor_id;
1124 command_channel *cmd_channel;
1128 retvm_if((!sensor_data), false, "sensor_data is NULL");
1132 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
1133 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1134 _E("client %s failed to get handle information", get_client_name());
1138 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1139 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
1143 client_id = sensor_client_info::get_instance().get_client_id();
1144 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());
1146 if (sensor_state != SENSOR_STATE_STARTED) {
1147 _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);
1151 if (!cmd_channel->cmd_get_data(data_id, sensor_data)) {
1152 _E("cmd_get_data(%d, %d, %#x) failed for %s", client_id, data_id, sensor_data, get_client_name());
1159 API bool sensord_flush(int handle)
1161 sensor_id_t sensor_id;
1162 command_channel *cmd_channel;
1168 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state) ||
1169 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1170 _E("client %s failed to get handle information", get_client_name());
1174 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1175 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
1179 client_id = sensor_client_info::get_instance().get_client_id();
1180 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());
1182 if (sensor_state != SENSOR_STATE_STARTED) {
1183 _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);
1187 if (!cmd_channel->cmd_flush()) {
1188 _E("cmd_flush() failed for %s", get_client_name());
1195 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)