4 * Copyright (c) 2014 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_internal_deprecated.h>
21 #include <sensor_internal.h>
22 #include <sensor_event_listener.h>
23 #include <client_common.h>
26 #include <sensor_logs.h>
27 #include <sensor_info.h>
28 #include <sensor_info_manager.h>
35 #define API __attribute__((visibility("default")))
38 #define DEFAULT_INTERVAL POLL_10HZ_MS
40 static const int OP_SUCCESS = 0;
41 static const int OP_ERROR = -1;
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, int cb_type, void* cb, void *user_data);
58 sensor_event_listener::get_instance().set_hup_observer(restore_session);
63 _D("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);
160 void restore_session(void)
164 _I("Trying to restore session for %s", get_client_name());
166 command_channel *cmd_channel;
169 sensor_client_info::get_instance().close_command_channel();
170 sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
172 sensor_id_vector sensors;
174 sensor_client_info::get_instance().get_listening_sensors(sensors);
176 bool first_connection = true;
178 auto it_sensor = sensors.begin();
180 while (it_sensor != sensors.end()) {
181 cmd_channel = new(std::nothrow) command_channel();
182 retm_if (!cmd_channel, "Failed to allocate memory");
184 if (!cmd_channel->create_channel()) {
185 _E("%s failed to create command channel for %s", get_client_name(), get_sensor_name(*it_sensor));
190 sensor_client_info::get_instance().add_command_channel(*it_sensor, cmd_channel);
192 if (first_connection) {
193 first_connection = false;
194 if (!cmd_channel->cmd_get_id(client_id)) {
195 _E("Failed to get client id");
199 sensor_client_info::get_instance().set_client_id(client_id);
200 sensor_event_listener::get_instance().start_event_listener();
203 cmd_channel->set_client_id(client_id);
205 if (!cmd_channel->cmd_hello(*it_sensor)) {
206 _E("Sending cmd_hello(%s, %d) failed for %s", get_sensor_name(*it_sensor), client_id, get_client_name());
210 sensor_rep prev_rep, cur_rep;
211 prev_rep.active = false;
212 prev_rep.option = SENSOR_OPTION_DEFAULT;
213 prev_rep.interval = 0;
215 sensor_client_info::get_instance().get_sensor_rep(*it_sensor, cur_rep);
216 if (!change_sensor_rep(*it_sensor, prev_rep, cur_rep)) {
217 _E("Failed to change rep(%s) for %s", get_sensor_name(*it_sensor), get_client_name());
224 _I("Succeeded to restore session for %s", get_client_name());
229 sensor_event_listener::get_instance().clear();
230 _E("Failed to restore session for %s", get_client_name());
233 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)
235 sort(a_vec.begin(), a_vec.end());
236 sort(b_vec.begin(), b_vec.end());
238 set_difference(a_vec.begin(), a_vec.end(), b_vec.begin(), b_vec.end(), back_inserter(del_vec));
239 set_difference(b_vec.begin(), b_vec.end(), a_vec.begin(), a_vec.end(), back_inserter(add_vec));
241 return !(add_vec.empty() && del_vec.empty());
245 static bool change_sensor_rep(sensor_id_t sensor_id, sensor_rep &prev_rep, sensor_rep &cur_rep)
248 command_channel *cmd_channel;
249 event_type_vector add_event_types, del_event_types;
251 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
252 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
256 client_id = sensor_client_info::get_instance().get_client_id();
257 retvm_if ((client_id < 0), false, "Invalid client id : %d, %s, %s", client_id, get_sensor_name(sensor_id), get_client_name());
259 get_events_diff(prev_rep.event_types, cur_rep.event_types, add_event_types, del_event_types);
261 if (cur_rep.active) {
262 if (prev_rep.option != cur_rep.option) {
263 if (!cmd_channel->cmd_set_option(cur_rep.option)) {
264 _E("Sending cmd_set_option(%d, %s, %d) failed for %s", client_id, get_sensor_name(sensor_id), cur_rep.option, get_client_name());
269 if ( (prev_rep.interval != cur_rep.interval) || (prev_rep.latency != cur_rep.latency)) {
270 if (!cmd_channel->cmd_set_batch(cur_rep.interval, cur_rep.latency)) {
271 _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());
276 if (!add_event_types.empty()) {
277 if (!cmd_channel->cmd_register_events(add_event_types)) {
278 _E("Sending cmd_register_events(%d, add_event_types) failed for %s", client_id, get_client_name());
285 if (prev_rep.active && !del_event_types.empty()) {
286 if (!cmd_channel->cmd_unregister_events(del_event_types)) {
287 _E("Sending cmd_unregister_events(%d, del_event_types) failed for %s", client_id, get_client_name());
292 if (prev_rep.active != cur_rep.active) {
293 if (cur_rep.active) {
294 if (!cmd_channel->cmd_start()) {
295 _E("Sending cmd_start(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
299 if (!cmd_channel->cmd_unset_batch()) {
300 _E("Sending cmd_unset_interval(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
304 if (!cmd_channel->cmd_stop()) {
305 _E("Sending cmd_stop(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
314 static bool get_sensor_list(void)
317 static bool init = false;
322 command_channel cmd_channel;
324 if (!cmd_channel.create_channel()) {
325 _E("%s failed to create command channel", get_client_name());
329 if (!cmd_channel.cmd_get_sensor_list())
338 API bool sensord_get_sensor_list(sensor_type_t type, sensor_t **list, int *sensor_count)
340 retvm_if (!get_sensor_list(), false, "Fail to get sensor list from server");
342 vector<sensor_info *> sensor_infos = sensor_info_manager::get_instance().get_infos(type);
344 if (!sensor_infos.empty()) {
345 *list = (sensor_t *) malloc(sizeof(sensor_info *) * sensor_infos.size());
346 retvm_if(!*list, false, "Failed to allocate memory");
349 for (unsigned int i = 0; i < sensor_infos.size(); ++i)
350 *(*list + i) = sensor_info_to_sensor(sensor_infos[i]);
352 *sensor_count = sensor_infos.size();
357 API sensor_t sensord_get_sensor(sensor_type_t type)
359 retvm_if (!get_sensor_list(), NULL, "Fail to get sensor list from server");
361 const sensor_info *info;
363 info = sensor_info_manager::get_instance().get_info(type);
365 return sensor_info_to_sensor(info);
368 API bool sensord_get_type(sensor_t sensor, sensor_type_t *type)
370 sensor_info* info = sensor_to_sensor_info(sensor);
372 retvm_if (!sensor_info_manager::get_instance().is_valid(info) || !type,
373 NULL, "Invalid param: sensor (%p), type(%p)", sensor, type);
375 *type = info->get_type();
380 API const char* sensord_get_name(sensor_t sensor)
382 sensor_info* info = sensor_to_sensor_info(sensor);
384 retvm_if (!sensor_info_manager::get_instance().is_valid(info),
385 NULL, "Invalid param: sensor (%p)", sensor);
387 return info->get_name();
390 API const char* sensord_get_vendor(sensor_t sensor)
392 sensor_info* info = sensor_to_sensor_info(sensor);
394 retvm_if (!sensor_info_manager::get_instance().is_valid(info),
395 NULL, "Invalid param: sensor (%p)", sensor);
397 return info->get_vendor();
400 API bool sensord_get_privilege(sensor_t sensor, sensor_privilege_t *privilege)
402 sensor_info* info = sensor_to_sensor_info(sensor);
404 retvm_if (!sensor_info_manager::get_instance().is_valid(info) || !privilege,
405 false, "Invalid param: sensor (%p), privilege(%p)", sensor, privilege);
407 *privilege = info->get_privilege();
412 API bool sensord_get_min_range(sensor_t sensor, float *min_range)
414 sensor_info* info = sensor_to_sensor_info(sensor);
416 retvm_if (!sensor_info_manager::get_instance().is_valid(info) || !min_range,
417 false, "Invalid param: sensor (%p), min_range(%p)", sensor, min_range);
419 *min_range = info->get_min_range();
424 API bool sensord_get_max_range(sensor_t sensor, float *max_range)
426 sensor_info* info = sensor_to_sensor_info(sensor);
428 retvm_if (!sensor_info_manager::get_instance().is_valid(info) || !max_range,
429 false, "Invalid param: sensor (%p), max_range(%p)", sensor, max_range);
431 *max_range = info->get_max_range();
436 API bool sensord_get_resolution(sensor_t sensor, float *resolution)
438 sensor_info* info = sensor_to_sensor_info(sensor);
440 retvm_if (!sensor_info_manager::get_instance().is_valid(info) || !resolution,
441 false, "Invalid param: sensor (%p), resolution(%p)", sensor, resolution);
443 *resolution = info->get_resolution();
448 API bool sensord_get_min_interval(sensor_t sensor, int *min_interval)
450 sensor_info* info = sensor_to_sensor_info(sensor);
452 retvm_if (!sensor_info_manager::get_instance().is_valid(info) || !min_interval,
453 false, "Invalid param: sensor (%p), min_interval(%p)", sensor, min_interval);
455 *min_interval = info->get_min_interval();
460 API bool sensord_get_fifo_count(sensor_t sensor, int *fifo_count)
462 sensor_info* info = sensor_to_sensor_info(sensor);
464 retvm_if (!sensor_info_manager::get_instance().is_valid(info) || !fifo_count,
465 false, "Invalid param: sensor (%p), fifo_count(%p)", sensor, fifo_count);
467 *fifo_count = info->get_fifo_count();
472 API bool sensord_get_max_batch_count(sensor_t sensor, int *max_batch_count)
474 sensor_info* info = sensor_to_sensor_info(sensor);
476 retvm_if (!sensor_info_manager::get_instance().is_valid(info) || !max_batch_count,
477 false, "Invalid param: sensor (%p), max_batch_count(%p)", sensor, max_batch_count);
479 *max_batch_count = info->get_max_batch_count();
484 API bool sensord_get_supported_event_types(sensor_t sensor, unsigned int **event_types, int *count)
486 sensor_info* info = sensor_to_sensor_info(sensor);
488 retvm_if (!sensor_info_manager::get_instance().is_valid(info) || !event_types || !count,
489 false, "Invalid param: sensor (%p), event_types(%p), count(%)", sensor, event_types, count);
491 unsigned int event_type;
492 event_type = info->get_supported_event();
493 *event_types = (unsigned int *) malloc(sizeof(unsigned int));
494 retvm_if(!*event_types, false, "Failed to allocate memory");
496 (*event_types)[0] = event_type;
502 API bool sensord_is_supported_event_type(sensor_t sensor, unsigned int event_type, bool *supported)
504 sensor_info* info = sensor_to_sensor_info(sensor);
506 retvm_if (!sensor_info_manager::get_instance().is_valid(info) || !event_type || !supported,
507 false, "Invalid param: sensor (%p), event_type(%p), supported(%)", sensor, event_type, supported);
509 *supported = info->is_supported_event(event_type);
514 API bool sensord_is_wakeup_supported(sensor_t sensor)
516 sensor_info* info = sensor_to_sensor_info(sensor);
518 retvm_if (!sensor_info_manager::get_instance().is_valid(info),
519 false, "Invalid param: sensor (%p)", sensor);
521 return info->is_wakeup_supported();
524 API int sensord_connect(sensor_t sensor)
526 command_channel *cmd_channel = NULL;
529 bool sensor_registered;
530 bool first_connection = false;
532 sensor_info* info = sensor_to_sensor_info(sensor);
534 retvm_if (!sensor_info_manager::get_instance().is_valid(info),
535 OP_ERROR, "Invalid param: sensor (%p)", sensor);
537 sensor_id_t sensor_id = info->get_id();
541 sensor_registered = sensor_client_info::get_instance().is_sensor_registered(sensor_id);
543 handle = sensor_client_info::get_instance().create_handle(sensor_id);
544 if (handle == MAX_HANDLE_REACHED) {
545 _E("Maximum number of handles reached, sensor: %s in client %s", get_sensor_name(sensor_id), get_client_name());
549 if (!sensor_registered) {
550 cmd_channel = new(std::nothrow) command_channel();
551 retvm_if (!cmd_channel, OP_ERROR, "Failed to allocate memory");
553 if (!cmd_channel->create_channel()) {
554 _E("%s failed to create command channel for %s", get_client_name(), get_sensor_name(sensor_id));
555 sensor_client_info::get_instance().delete_handle(handle);
560 sensor_client_info::get_instance().add_command_channel(sensor_id, cmd_channel);
563 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
564 _E("%s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
565 sensor_client_info::get_instance().delete_handle(handle);
569 if (!sensor_client_info::get_instance().has_client_id()) {
570 first_connection = true;
571 if(!cmd_channel->cmd_get_id(client_id)) {
572 _E("Sending cmd_get_id() failed for %s", get_sensor_name(sensor_id));
573 sensor_client_info::get_instance().close_command_channel(sensor_id);
574 sensor_client_info::get_instance().delete_handle(handle);
578 sensor_client_info::get_instance().set_client_id(client_id);
579 _I("%s gets client_id [%d]", get_client_name(), client_id);
580 sensor_event_listener::get_instance().start_event_listener();
581 _I("%s starts listening events with client_id [%d]", get_client_name(), client_id);
584 client_id = sensor_client_info::get_instance().get_client_id();
585 cmd_channel->set_client_id(client_id);
587 _I("%s[%d] connects with %s[%d]", get_client_name(), client_id, get_sensor_name(sensor_id), handle);
589 sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_STOPPED, SENSOR_OPTION_DEFAULT);
591 if (!sensor_registered) {
592 if(!cmd_channel->cmd_hello(sensor_id)) {
593 _E("Sending cmd_hello(%s, %d) failed for %s", get_sensor_name(sensor_id), client_id, get_client_name());
594 sensor_client_info::get_instance().close_command_channel(sensor_id);
595 sensor_client_info::get_instance().delete_handle(handle);
596 if (first_connection) {
597 sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
598 sensor_event_listener::get_instance().stop_event_listener();
604 set_power_save_state_cb();
608 API bool sensord_disconnect(int handle)
610 command_channel *cmd_channel;
611 sensor_id_t sensor_id;
617 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
618 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
619 _E("client %s failed to get handle information", get_client_name());
623 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
624 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
628 client_id = sensor_client_info::get_instance().get_client_id();
629 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());
631 _I("%s disconnects with %s[%d]", get_client_name(), get_sensor_name(sensor_id), handle);
633 if (sensor_state != SENSOR_STATE_STOPPED) {
634 _W("%s[%d] for %s is not stopped before disconnecting.",
635 get_sensor_name(sensor_id), handle, get_client_name());
636 sensord_stop(handle);
639 if (!sensor_client_info::get_instance().delete_handle(handle))
642 if (!sensor_client_info::get_instance().is_active())
643 sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
645 if (!sensor_client_info::get_instance().is_sensor_registered(sensor_id)) {
646 if(!cmd_channel->cmd_byebye()) {
647 _E("Sending cmd_byebye(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
650 sensor_client_info::get_instance().close_command_channel(sensor_id);
653 if (!sensor_client_info::get_instance().is_active()) {
654 _I("Stop listening events for client %s with client id [%d]", get_client_name(), sensor_client_info::get_instance().get_client_id());
655 sensor_event_listener::get_instance().stop_event_listener();
658 unset_power_save_state_cb();
664 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)
666 sensor_id_t sensor_id;
667 sensor_rep prev_rep, cur_rep;
670 retvm_if (!cb, false, "callback is NULL");
674 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
675 _E("client %s failed to get handle information", get_client_name());
680 interval = DEFAULT_INTERVAL;
682 _I("%s registers event %s[0x%x] for sensor %s[%d] with interval: %d, latency: %d, cb: 0x%x, user_data: 0x%x",
683 get_client_name(), get_event_name(event_type), event_type, get_sensor_name(sensor_id),
684 handle, interval, max_batch_latency, cb, user_data);
686 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
687 sensor_client_info::get_instance().register_event(handle, event_type, interval, max_batch_latency, cb_type, cb, user_data);
688 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
689 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
692 sensor_client_info::get_instance().unregister_event(handle, event_type);
697 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)
699 return register_event(handle, event_type, interval, max_batch_latency, SENSOR_EVENT_CB, (void *)cb, user_data);
702 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)
704 return register_event(handle, event_type, interval, max_batch_latency, SENSORHUB_EVENT_CB, (void *)cb, user_data);
708 API bool sensord_unregister_event(int handle, unsigned int event_type)
710 sensor_id_t sensor_id;
711 sensor_rep prev_rep, cur_rep;
713 unsigned int prev_interval, prev_latency;
716 void *prev_user_data;
720 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
721 _E("client %s failed to get handle information", get_client_name());
725 _I("%s unregisters event %s[0x%x] for sensor %s[%d]", get_client_name(), get_event_name(event_type),
726 event_type, get_sensor_name(sensor_id), handle);
728 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
729 sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb_type, prev_cb, prev_user_data);
731 if (!sensor_client_info::get_instance().unregister_event(handle, event_type)) {
732 _E("%s try to unregister non registered event %s[0x%x] for sensor %s[%d]",
733 get_client_name(),get_event_name(event_type), event_type, get_sensor_name(sensor_id), handle);
737 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
738 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
741 sensor_client_info::get_instance().register_event(handle, event_type, prev_interval, prev_latency, prev_cb_type, prev_cb, prev_user_data);
748 API bool sensord_register_accuracy_cb(int handle, sensor_accuracy_changed_cb_t cb, void *user_data)
750 sensor_id_t sensor_id;
752 retvm_if (!cb, false, "callback is NULL");
756 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
757 _E("client %s failed to get handle information", get_client_name());
762 _I("%s registers accuracy_changed_cb for sensor %s[%d] with cb: 0x%x, user_data: 0x%x",
763 get_client_name(), get_sensor_name(sensor_id), handle, cb, user_data);
765 sensor_client_info::get_instance().register_accuracy_cb(handle, cb , user_data);
771 API bool sensord_unregister_accuracy_cb(int handle)
773 sensor_id_t sensor_id;
777 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
778 _E("client %s failed to get handle information", get_client_name());
783 _I("%s unregisters accuracy_changed_cb for sensor %s[%d]",
784 get_client_name(), get_sensor_name(sensor_id), handle);
786 sensor_client_info::get_instance().unregister_accuracy_cb(handle);
792 API bool sensord_start(int handle, int option)
794 sensor_id_t sensor_id;
795 sensor_rep prev_rep, cur_rep;
797 int prev_state, prev_option;
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 retvm_if ((option < 0) || (option >= SENSOR_OPTION_END), false, "Invalid option value : %d, handle: %d, %s, %s",
807 option, handle, get_sensor_name(sensor_id), get_client_name());
809 _I("%s starts %s[%d], with option: %d, power save state: %d", get_client_name(), get_sensor_name(sensor_id),
810 handle, option, g_power_save_state);
812 if (g_power_save_state && !(g_power_save_state & option)) {
813 sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_PAUSED, option);
817 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
818 sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_option);
819 sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_STARTED, option);
820 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
822 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
825 sensor_client_info::get_instance().set_sensor_params(handle, prev_state, prev_option);
830 API bool sensord_stop(int handle)
832 sensor_id_t sensor_id;
835 int prev_state, prev_option;
837 sensor_rep prev_rep, cur_rep;
841 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
842 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
843 _E("client %s failed to get handle information", get_client_name());
847 retvm_if ((sensor_state == SENSOR_STATE_STOPPED), true, "%s already stopped with %s[%d]",
848 get_client_name(), get_sensor_name(sensor_id), handle);
851 _I("%s stops sensor %s[%d]", get_client_name(), get_sensor_name(sensor_id), handle);
853 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
854 sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_option);
855 sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_STOPPED);
856 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
858 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
861 sensor_client_info::get_instance().set_sensor_params(handle, prev_state, prev_option);
867 static bool change_event_batch(int handle, unsigned int event_type, unsigned int interval, unsigned int latency)
869 sensor_id_t sensor_id;
870 sensor_rep prev_rep, cur_rep;
872 unsigned int prev_interval, prev_latency;
875 void *prev_user_data;
879 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
880 _E("client %s failed to get handle information", get_client_name());
884 _I("%s changes batch of event %s[0x%x] for %s[%d] to (%d, %d)", get_client_name(), get_event_name(event_type),
885 event_type, get_sensor_name(sensor_id), handle, interval, latency);
887 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
889 sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb_type, prev_cb, prev_user_data);
892 interval = DEFAULT_INTERVAL;
894 if (!sensor_client_info::get_instance().set_event_batch(handle, event_type, interval, latency))
897 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
899 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
902 sensor_client_info::get_instance().set_event_batch(handle, event_type, prev_interval, prev_latency);
907 API bool sensord_change_event_interval(int handle, unsigned int event_type, unsigned int interval)
909 unsigned int prev_interval, prev_latency;
912 void *prev_user_data;
916 if (!sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb_type, prev_cb, prev_user_data)) {
917 _E("Failed to get event info with handle = %d, event_type = 0x%x", handle, event_type);
921 _I("handle = %d, event_type = 0x%x, interval = %d, prev_latency = %d", handle, event_type, interval, prev_latency);
922 return change_event_batch(handle, event_type, interval, prev_latency);
925 API bool sensord_change_event_max_batch_latency(int handle, unsigned int event_type, unsigned int max_batch_latency)
927 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_type, prev_cb, prev_user_data)) {
935 _E("Failed to get event info with handle = %d, event_type = 0x%x", handle, event_type);
939 return change_event_batch(handle, event_type, prev_interval, max_batch_latency);
942 API bool sensord_change_event_maincontext(int handle, unsigned int event_type, GMainContext *maincontext)
946 if (!sensor_client_info::get_instance().set_event_maincontext(handle, event_type, maincontext)) {
947 _E("Failed to get event info with handle = %d, event_type = 0x%x, maincontext = 0x%x", handle, event_type, maincontext);
951 _I("handle = %d, event_type = 0x%x, maincontext = 0x%x", handle, event_type, maincontext);
955 API bool sensord_set_option(int handle, int option)
957 sensor_id_t sensor_id;
958 sensor_rep prev_rep, cur_rep;
961 int prev_state, prev_option;
965 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
966 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
967 _E("client %s failed to get handle information", get_client_name());
971 retvm_if ((option < 0) || (option >= SENSOR_OPTION_END), false, "Invalid option value : %d, handle: %d, %s, %s",
972 option, handle, get_sensor_name(sensor_id), get_client_name());
975 sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
976 sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_option);
978 if (g_power_save_state) {
979 if ((option & g_power_save_state) && (sensor_state == SENSOR_STATE_PAUSED))
980 sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_STARTED);
981 else if (!(option & g_power_save_state) && (sensor_state == SENSOR_STATE_STARTED))
982 sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_PAUSED);
984 sensor_client_info::get_instance().set_sensor_option(handle, option);
986 sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
987 ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
990 sensor_client_info::get_instance().set_sensor_option(handle, prev_option);
996 API bool sensord_set_wakeup(int handle, int wakeup)
998 sensor_id_t sensor_id;
999 command_channel *cmd_channel;
1004 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1005 _E("client %s failed to get handle information", get_client_name());
1009 retvm_if ((wakeup != SENSOR_WAKEUP_ON) && (wakeup != SENSOR_WAKEUP_OFF), false, "Invalid wakeup value : %d, handle: %d, %s, %s",
1010 wakeup, handle, get_sensor_name(sensor_id), get_client_name());
1012 sensor_client_info::get_instance().set_sensor_wakeup(handle, wakeup);
1014 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1015 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
1019 client_id = sensor_client_info::get_instance().get_client_id();
1020 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());
1022 if (!cmd_channel->cmd_set_wakeup(wakeup)) {
1023 _E("Sending cmd_set_wakeup(%d, %s, %d) failed for %s", client_id, get_sensor_name(sensor_id), wakeup, get_client_name());
1030 API bool sensord_set_attribute_int(int handle, int attribute, int value)
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", get_client_name(), get_sensor_name(sensor_id));
1048 client_id = sensor_client_info::get_instance().get_client_id();
1049 retvm_if ((client_id < 0), false,
1050 "Invalid client id : %d, handle: %d, %s, %s",
1051 client_id, handle, get_sensor_name(sensor_id), get_client_name());
1053 if (!cmd_channel->cmd_set_attribute_int(attribute, value)) {
1054 _E("Sending cmd_set_attribute_int(%d, %d) failed for %s",
1055 client_id, value, get_client_name);
1062 API bool sensord_set_attribute_str(int handle, int attribute, const char *value, int value_len)
1064 sensor_id_t sensor_id;
1065 command_channel *cmd_channel;
1070 if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1071 _E("client %s failed to get handle information", get_client_name());
1075 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1076 _E("client %s failed to get command channel for %s",
1077 get_client_name(), get_sensor_name(sensor_id));
1081 retvm_if((value_len < 0) || (value == NULL), false,
1082 "Invalid value_len: %d, value: 0x%x, handle: %d, %s, %s",
1083 value_len, value, handle, get_sensor_name(sensor_id), get_client_name());
1085 client_id = sensor_client_info::get_instance().get_client_id();
1086 retvm_if ((client_id < 0), false,
1087 "Invalid client id : %d, handle: %d, %s, %s",
1088 client_id, handle, get_sensor_name(sensor_id), get_client_name());
1090 if (!cmd_channel->cmd_set_attribute_str(attribute, value, value_len)) {
1091 _E("Sending cmd_set_attribute_str(%d, %d, 0x%x) failed for %s",
1092 client_id, value_len, value, get_client_name);
1099 API bool sensord_send_sensorhub_data(int handle, const char *data, int data_len)
1101 return sensord_set_attribute_str(handle, 0, data, data_len);
1104 API bool sensord_get_data(int handle, unsigned int data_id, sensor_data_t* sensor_data)
1106 sensor_id_t sensor_id;
1107 command_channel *cmd_channel;
1111 retvm_if ((!sensor_data), false, "sensor_data is NULL");
1115 if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
1116 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1117 _E("client %s failed to get handle information", get_client_name());
1121 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1122 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
1126 client_id = sensor_client_info::get_instance().get_client_id();
1127 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());
1129 if (sensor_state != SENSOR_STATE_STARTED) {
1130 _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);
1134 if(!cmd_channel->cmd_get_data(data_id, sensor_data)) {
1135 _E("cmd_get_data(%d, %d, 0x%x) failed for %s", client_id, data_id, sensor_data, get_client_name());
1143 /* deprecated APIs */
1144 API int sf_connect(sensor_type_t sensor_type)
1148 sensor = sensord_get_sensor(sensor_type);
1150 return sensord_connect(sensor);
1153 API int sf_disconnect(int handle)
1155 return sensord_disconnect(handle) ? OP_SUCCESS : OP_ERROR;
1158 API int sf_start(int handle, int option)
1160 return sensord_start(handle, option) ? OP_SUCCESS : OP_ERROR;
1163 API int sf_stop(int handle)
1165 return sensord_stop(handle) ? OP_SUCCESS : OP_ERROR;
1168 API int sf_register_event(int handle, unsigned int event_type, event_condition_t *event_condition, sensor_callback_func_t cb, void *user_data)
1170 unsigned int interval = BASE_GATHERING_INTERVAL;
1172 if (event_condition != NULL) {
1173 if ((event_condition->cond_op == CONDITION_EQUAL) && (event_condition->cond_value1 > 0))
1174 interval = event_condition->cond_value1;
1177 return register_event(handle, event_type, interval, 0, SENSOR_LEGACY_CB, (void*) cb, user_data) ? OP_SUCCESS : OP_ERROR;
1180 API int sf_unregister_event(int handle, unsigned int event_type)
1182 return sensord_unregister_event(handle, event_type) ? OP_SUCCESS : OP_ERROR;
1185 API int sf_change_event_condition(int handle, unsigned int event_type, event_condition_t *event_condition)
1187 unsigned int interval = BASE_GATHERING_INTERVAL;
1189 if (event_condition != NULL) {
1190 if ((event_condition->cond_op == CONDITION_EQUAL) && (event_condition->cond_value1 > 0))
1191 interval = event_condition->cond_value1;
1194 return sensord_change_event_interval(handle, event_type, interval) ? OP_SUCCESS : OP_ERROR;
1197 API int sf_change_sensor_option(int handle, int option)
1199 return sensord_set_option(handle, option) ? OP_SUCCESS : OP_ERROR;
1202 API int sf_send_sensorhub_data(int handle, const char* data, int data_len)
1204 return sensord_send_sensorhub_data(handle, data, data_len) ? OP_SUCCESS : OP_ERROR;
1207 API int sf_get_data(int handle, unsigned int data_id, sensor_data_t* sensor_data)
1209 return sensord_get_data(handle, data_id, sensor_data) ? OP_SUCCESS : OP_ERROR;
1212 API int sf_check_rotation(unsigned long *rotation)
1218 API int sf_is_sensor_event_available(sensor_type_t sensor_type, unsigned int event_type)
1223 API int sf_get_data_properties(unsigned int data_id, sensor_data_properties_t *return_data_properties)
1228 API int sf_get_properties(sensor_type_t sensor_type, sensor_properties_t *return_properties)