sensord: move operate_sensor function to sensor_client_info
[platform/core/system/sensord.git] / src / client / client.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2013 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
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>
26 #include <vconf.h>
27 #include <cmutex.h>
28 #include <sensor_log.h>
29 #include <sensor_info.h>
30 #include <sensor_info_manager.h>
31 #include <vector>
32 #include <algorithm>
33 #include "dbus_listener.h"
34
35 using std::vector;
36
37 #ifndef API
38 #define API __attribute__((visibility("default")))
39 #endif
40
41 #ifndef VCONFKEY_SETAPPL_PSMODE
42 #define VCONFKEY_SETAPPL_PSMODE "db/setting/psmode"
43 #endif
44
45 #define DEFAULT_INTERVAL POLL_10HZ_MS
46
47 #define CONVERT_OPTION_PAUSE_POLICY(option) \
48         (option == SENSOR_OPTION_DEFAULT || option == SENSOR_OPTION_ALWAYS_ON) ? \
49         (option ^ 0b11) : option
50
51 static cmutex lock;
52
53 static int g_power_save_state = SENSORD_PAUSE_NONE;
54
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);
61
62 class initiator {
63 public:
64         initiator()
65         {
66                 sensor_event_listener::get_instance().set_hup_observer(restore_session);
67         }
68 };
69
70 void good_bye(void)
71 {
72         _I("Good bye! %s\n", get_client_name());
73         clean_up();
74 }
75
76 static initiator g_initiator;
77
78 static int g_power_save_state_cb_cnt = 0;
79
80 static void set_power_save_state_cb(void)
81 {
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);
84
85         ++g_power_save_state_cb_cnt;
86
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);
93         }
94 }
95
96 static void unset_power_save_state_cb(void)
97 {
98         --g_power_save_state_cb_cnt;
99
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);
102
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);
107         }
108 }
109
110 void clean_up(void)
111 {
112         handle_vector handles;
113
114         sensor_client_info::get_instance().get_all_handles(handles);
115
116         auto it_handle = handles.begin();
117
118         while (it_handle != handles.end()) {
119                 sensord_disconnect(*it_handle);
120                 ++it_handle;
121         }
122 }
123
124 static int get_power_save_state(void)
125 {
126         int ret;
127         int state = 0;
128         int pm_state, ps_state;
129
130         ret = vconf_get_int(VCONFKEY_PM_STATE, &pm_state);
131
132         if (!ret && pm_state == VCONFKEY_PM_STATE_LCDOFF)
133                 state |= SENSORD_PAUSE_ON_DISPLAY_OFF;
134
135         ret = vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ps_state);
136
137         if (!ret && ps_state != SETTING_PSMODE_NORMAL)
138                 state |= SENSORD_PAUSE_ON_POWERSAVE_MODE;
139
140         return state;
141 }
142
143 static void power_save_state_cb(keynode_t *node, void *data)
144 {
145         int cur_power_save_state;
146         sensor_id_vector sensors;
147         sensor_rep prev_rep, cur_rep;
148
149         AUTOLOCK(lock);
150
151         cur_power_save_state = get_power_save_state();
152
153         if (cur_power_save_state == g_power_save_state) {
154                 _D("g_power_save_state NOT changed : [%d]", cur_power_save_state);
155                 return;
156         }
157
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());
160
161         sensor_client_info::get_instance().get_listening_sensors(sensors);
162
163         auto it_sensor = sensors.begin();
164
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);
170
171                 ++it_sensor;
172         }
173 }
174
175 void restore_session(void)
176 {
177         AUTOLOCK(lock);
178
179         _I("Trying to restore sensor client session for %s", get_client_name());
180
181         command_channel *cmd_channel;
182         int client_id;
183
184         sensor_client_info::get_instance().close_command_channel();
185         sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
186
187         sensor_id_vector sensors;
188
189         sensor_client_info::get_instance().get_listening_sensors(sensors);
190
191         bool first_connection = true;
192
193         auto it_sensor = sensors.begin();
194
195         while (it_sensor != sensors.end()) {
196                 cmd_channel = new(std::nothrow) command_channel();
197                 retm_if(!cmd_channel, "Failed to allocate memory");
198
199                 if (!cmd_channel->create_channel()) {
200                         _E("%s failed to create command channel for %s", get_client_name(), get_sensor_name(*it_sensor));
201                         delete cmd_channel;
202                         goto FAILED;
203                 }
204
205                 sensor_client_info::get_instance().add_command_channel(*it_sensor, cmd_channel);
206
207                 if (first_connection) {
208                         first_connection = false;
209                         if (!cmd_channel->cmd_get_id(client_id)) {
210                                 _E("Failed to get client id");
211                                 goto FAILED;
212                         }
213
214                         sensor_client_info::get_instance().set_client_id(client_id);
215                         sensor_event_listener::get_instance().start_event_listener();
216                 }
217
218                 cmd_channel->set_client_id(client_id);
219
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());
222                         goto FAILED;
223                 }
224
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;
229
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());
233                         goto FAILED;
234                 }
235
236                 ++it_sensor;
237         }
238
239         _I("Succeeded to restore sensor client session for %s", get_client_name());
240
241         return;
242
243 FAILED:
244         sensor_event_listener::get_instance().clear();
245         _E("Failed to restore session for %s", get_client_name());
246 }
247
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)
249 {
250         sort(a_vec.begin(), a_vec.end());
251         sort(b_vec.begin(), b_vec.end());
252
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));
255
256         return !(add_vec.empty() && del_vec.empty());
257 }
258
259 static bool change_sensor_rep(sensor_id_t sensor_id, sensor_rep &prev_rep, sensor_rep &cur_rep)
260 {
261         int client_id;
262         command_channel *cmd_channel;
263         event_type_vector add_event_types, del_event_types;
264
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));
267                 return false;
268         }
269
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());
272
273         get_events_diff(prev_rep.event_types, cur_rep.event_types, add_event_types, del_event_types);
274
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());
279                                 return false;
280                         }
281                 }
282
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());
286                                 return false;
287                         }
288                 }
289
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());
293                                 return false;
294                         }
295                 }
296         }
297
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());
301                         return false;
302                 }
303         }
304
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());
309                                 return false;
310                         }
311                 } else {
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());
314                                 return false;
315                         }
316
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());
319                                 return false;
320                         }
321                 }
322         }
323
324         return true;
325 }
326
327 static bool get_sensor_list(void)
328 {
329         static cmutex l;
330         static bool init = false;
331
332         AUTOLOCK(l);
333
334         if (!init) {
335                 command_channel cmd_channel;
336
337                 if (!cmd_channel.create_channel()) {
338                         _E("%s failed to create command channel", get_client_name());
339                         return false;
340                 }
341
342                 if (!cmd_channel.cmd_get_sensor_list())
343                         return false;
344
345                 init = true;
346         }
347
348         return true;
349 }
350
351 API int sensord_get_sensors(sensor_type_t type, sensor_t **list, int *sensor_count)
352 {
353         retvm_if(!get_sensor_list(), -EPERM, "Fail to get sensor list from server");
354
355         vector<sensor_info *> sensor_infos = sensor_info_manager::get_instance().get_infos(type);
356
357         if (sensor_infos.empty()) {
358                 *sensor_count = 0;
359                 return -ENODATA;
360         }
361
362         if (type != ALL_SENSOR) {
363                 if (sensor_infos[0]->get_id() == (sensor_id_t)(-EACCES))
364                         return -EACCES;
365         }
366
367         *list = (sensor_t *) malloc(sizeof(sensor_info *) * sensor_infos.size());
368         retvm_if(!*list, false, "Failed to allocate memory");
369
370         for (unsigned int i = 0; i < sensor_infos.size(); ++i)
371                 *(*list + i) = sensor_info_to_sensor(sensor_infos[i]);
372
373         *sensor_count = sensor_infos.size();
374
375         return OP_SUCCESS;
376 }
377
378 API int sensord_get_default_sensor(sensor_type_t type, sensor_t *sensor)
379 {
380         retvm_if(!get_sensor_list(), -EPERM, "Fail to get sensor list from server");
381
382         const sensor_info *info;
383
384         info = sensor_info_manager::get_instance().get_info(type);
385         if (info == NULL) {
386                 *sensor = NULL;
387                 return -ENODATA;
388         }
389
390         if ((const_cast<sensor_info *>(info))->get_id() == (sensor_id_t)(-EACCES))
391                 return -EACCES;
392
393         *sensor = sensor_info_to_sensor(info);
394
395         return OP_SUCCESS;
396 }
397
398 API bool sensord_get_sensor_list(sensor_type_t type, sensor_t **list, int *sensor_count)
399 {
400         return (sensord_get_sensors(type, list, sensor_count) == OP_SUCCESS);
401 }
402
403 API sensor_t sensord_get_sensor(sensor_type_t type)
404 {
405         sensor_t sensor;
406         sensord_get_default_sensor(type, &sensor);
407
408         return sensor;
409 }
410
411 API bool sensord_get_type(sensor_t sensor, sensor_type_t *type)
412 {
413         sensor_info* info = sensor_to_sensor_info(sensor);
414
415         retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !type,
416                 NULL, "Invalid param: sensor (%p), type(%p)", sensor, type);
417
418         *type = info->get_type();
419
420         return true;
421 }
422
423 API const char* sensord_get_name(sensor_t sensor)
424 {
425         sensor_info* info = sensor_to_sensor_info(sensor);
426
427         retvm_if(!sensor_info_manager::get_instance().is_valid(info),
428                 NULL, "Invalid param: sensor (%p)", sensor);
429
430         return info->get_name();
431 }
432
433 API const char* sensord_get_vendor(sensor_t sensor)
434 {
435         sensor_info* info = sensor_to_sensor_info(sensor);
436
437         retvm_if(!sensor_info_manager::get_instance().is_valid(info),
438                 NULL, "Invalid param: sensor (%p)", sensor);
439
440         return info->get_vendor();
441 }
442
443 API bool sensord_get_privilege(sensor_t sensor, sensor_privilege_t *privilege)
444 {
445         sensor_info* info = sensor_to_sensor_info(sensor);
446
447         retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !privilege,
448                 false, "Invalid param: sensor (%p), privilege(%p)", sensor, privilege);
449
450         *privilege = info->get_privilege();
451
452         return true;
453 }
454
455 API bool sensord_get_min_range(sensor_t sensor, float *min_range)
456 {
457         sensor_info* info = sensor_to_sensor_info(sensor);
458
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);
461
462         *min_range = info->get_min_range();
463
464         return true;
465 }
466
467 API bool sensord_get_max_range(sensor_t sensor, float *max_range)
468 {
469         sensor_info* info = sensor_to_sensor_info(sensor);
470
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);
473
474         *max_range = info->get_max_range();
475
476         return true;
477 }
478
479 API bool sensord_get_resolution(sensor_t sensor, float *resolution)
480 {
481         sensor_info* info = sensor_to_sensor_info(sensor);
482
483         retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !resolution,
484                 false, "Invalid param: sensor (%p), resolution(%p)", sensor, resolution);
485
486         *resolution = info->get_resolution();
487
488         return true;
489 }
490
491 API bool sensord_get_min_interval(sensor_t sensor, int *min_interval)
492 {
493         sensor_info* info = sensor_to_sensor_info(sensor);
494
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);
497
498         *min_interval = info->get_min_interval();
499
500         return true;
501 }
502
503 API bool sensord_get_fifo_count(sensor_t sensor, int *fifo_count)
504 {
505         sensor_info* info = sensor_to_sensor_info(sensor);
506
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);
509
510         *fifo_count = info->get_fifo_count();
511
512         return true;
513 }
514
515 API bool sensord_get_max_batch_count(sensor_t sensor, int *max_batch_count)
516 {
517         sensor_info* info = sensor_to_sensor_info(sensor);
518
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);
521
522         *max_batch_count = info->get_max_batch_count();
523
524         return true;
525 }
526
527 API bool sensord_get_supported_event_types(sensor_t sensor, unsigned int **event_types, int *count)
528 {
529         sensor_info* info = sensor_to_sensor_info(sensor);
530
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);
533
534         unsigned int event_type;
535         event_type = info->get_supported_event();
536         *event_types = (unsigned int *)malloc(sizeof(unsigned int));
537
538         retvm_if(!*event_types, false, "Failed to allocate memory");
539
540         (*event_types)[0] = event_type;
541         *count = 1;
542
543         return true;
544 }
545
546 API bool sensord_is_supported_event_type(sensor_t sensor, unsigned int event_type, bool *supported)
547 {
548         sensor_info* info = sensor_to_sensor_info(sensor);
549
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);
552
553         *supported = info->is_supported_event(event_type);
554
555         return true;
556 }
557
558 API bool sensord_is_wakeup_supported(sensor_t sensor)
559 {
560         sensor_info* info = sensor_to_sensor_info(sensor);
561
562         retvm_if(!sensor_info_manager::get_instance().is_valid(info),
563                 false, "Invalid param: sensor (%p)", sensor);
564
565         return info->is_wakeup_supported();
566 }
567
568 API int sensord_connect(sensor_t sensor)
569 {
570         command_channel *cmd_channel = NULL;
571         int handle;
572         int client_id;
573         bool sensor_registered;
574         bool first_connection = false;
575
576         sensor_info* info = sensor_to_sensor_info(sensor);
577
578         retvm_if(!sensor_info_manager::get_instance().is_valid(info),
579                 OP_ERROR, "Invalid param: sensor (%p)", sensor);
580
581         sensor_id_t sensor_id = info->get_id();
582
583         AUTOLOCK(lock);
584
585         sensor_registered = sensor_client_info::get_instance().is_sensor_registered(sensor_id);
586
587         // lazy loading after creating static variables
588         atexit(good_bye);
589
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());
593                 return OP_ERROR;
594         }
595
596         if (!sensor_registered) {
597                 cmd_channel = new(std::nothrow) command_channel();
598                 if (!cmd_channel) {
599                         _E("Failed to allocated memory");
600                         sensor_client_info::get_instance().delete_handle(handle);
601                         return OP_ERROR;
602                 }
603
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);
607                         delete cmd_channel;
608                         return OP_ERROR;
609                 }
610
611                 sensor_client_info::get_instance().add_command_channel(sensor_id, cmd_channel);
612         }
613
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);
617                 return OP_ERROR;
618         }
619
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);
626                         return OP_ERROR;
627                 }
628
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);
633         }
634
635         client_id = sensor_client_info::get_instance().get_client_id();
636         cmd_channel->set_client_id(client_id);
637
638         _I("%s[%d] connects with %s[%d]", get_client_name(), client_id, get_sensor_name(sensor_id), handle);
639
640         sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_STOPPED, SENSORD_PAUSE_ALL);
641
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();
650                         }
651                         return OP_ERROR;
652                 }
653         }
654
655         set_power_save_state_cb();
656         dbus_listener::init();
657         return handle;
658 }
659
660 API bool sensord_disconnect(int handle)
661 {
662         command_channel *cmd_channel;
663         sensor_id_t sensor_id;
664         int client_id;
665         int sensor_state;
666
667         AUTOLOCK(lock);
668
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());
672                 return false;
673         }
674
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));
677                 return false;
678         }
679
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());
682
683         _I("%s disconnects with %s[%d]", get_client_name(), get_sensor_name(sensor_id), handle);
684
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);
689         }
690
691         if (!sensor_client_info::get_instance().delete_handle(handle))
692                 return false;
693
694         if (!sensor_client_info::get_instance().is_active())
695                 sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
696
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());
700                         return false;
701                 }
702                 sensor_client_info::get_instance().close_command_channel(sensor_id);
703         }
704
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();
708         }
709
710         unset_power_save_state_cb();
711
712         return true;
713 }
714
715 static bool register_event(int handle, unsigned int event_type, unsigned int interval, int max_batch_latency, void* cb, void *user_data)
716 {
717         sensor_id_t sensor_id;
718         sensor_rep prev_rep, cur_rep;
719         bool ret;
720
721         retvm_if(!cb, false, "callback is NULL");
722
723         AUTOLOCK(lock);
724
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());
727                 return false;
728         }
729
730         if (interval == 0)
731                 interval = DEFAULT_INTERVAL;
732
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);
736
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);
741
742         if (!ret)
743                 sensor_client_info::get_instance().unregister_event(handle, event_type);
744
745         return ret;
746 }
747
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)
749 {
750         return register_event(handle, event_type, interval, max_batch_latency, (void *)cb, user_data);
751 }
752
753 API bool sensord_unregister_event(int handle, unsigned int event_type)
754 {
755         sensor_id_t sensor_id;
756         sensor_rep prev_rep, cur_rep;
757         bool ret;
758         unsigned int prev_interval, prev_latency;
759         void *prev_cb;
760         void *prev_user_data;
761
762         AUTOLOCK(lock);
763
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());
766                 return false;
767         }
768
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);
771
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);
774
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);
778                 return false;
779         }
780
781         sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
782         ret =  change_sensor_rep(sensor_id, prev_rep, cur_rep);
783
784         if (!ret)
785                 sensor_client_info::get_instance().register_event(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data);
786
787         return ret;
788 }
789
790 API bool sensord_register_accuracy_cb(int handle, sensor_accuracy_changed_cb_t cb, void *user_data)
791 {
792         sensor_id_t sensor_id;
793
794         retvm_if(!cb, false, "callback is NULL");
795
796         AUTOLOCK(lock);
797
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());
800                 return false;
801         }
802
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);
805
806         sensor_client_info::get_instance().register_accuracy_cb(handle, cb , user_data);
807
808         return true;
809 }
810
811 API bool sensord_unregister_accuracy_cb(int handle)
812 {
813         sensor_id_t sensor_id;
814
815         AUTOLOCK(lock);
816
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());
819                 return false;
820         }
821
822         _I("%s unregisters accuracy_changed_cb for sensor %s[%d]",
823                 get_client_name(), get_sensor_name(sensor_id), handle);
824
825         sensor_client_info::get_instance().unregister_accuracy_cb(handle);
826
827         return true;
828 }
829
830 API bool sensord_start(int handle, int option)
831 {
832         sensor_id_t sensor_id;
833         sensor_rep prev_rep, cur_rep;
834         bool ret;
835         int prev_state, prev_pause;
836         int pause;
837
838         AUTOLOCK(lock);
839
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());
842                 return false;
843         }
844
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());
847
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);
850
851         pause = CONVERT_OPTION_PAUSE_POLICY(option);
852
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);
855                 return true;
856         }
857
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);
862
863         ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
864
865         if (!ret)
866                 sensor_client_info::get_instance().set_sensor_params(handle, prev_state, prev_pause);
867
868         return ret;
869 }
870
871 API bool sensord_stop(int handle)
872 {
873         sensor_id_t sensor_id;
874         int sensor_state;
875         bool ret;
876         int prev_state, prev_pause;
877
878         sensor_rep prev_rep, cur_rep;
879
880         AUTOLOCK(lock);
881
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());
885                 return false;
886         }
887
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);
890
891         _I("%s stops sensor %s[%d]", get_client_name(), get_sensor_name(sensor_id), handle);
892
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);
897
898         ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
899
900         if (!ret)
901                 sensor_client_info::get_instance().set_sensor_params(handle, prev_state, prev_pause);
902
903         return ret;
904 }
905
906 static bool change_event_batch(int handle, unsigned int event_type, unsigned int interval, unsigned int latency)
907 {
908         sensor_id_t sensor_id;
909         sensor_rep prev_rep, cur_rep;
910         bool ret;
911         unsigned int prev_interval, prev_latency;
912         void *prev_cb;
913         void *prev_user_data;
914
915         AUTOLOCK(lock);
916
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());
919                 return false;
920         }
921
922         if (interval == 0)
923                 interval = DEFAULT_INTERVAL;
924
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);
927
928         sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
929
930         sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data);
931
932         if (!sensor_client_info::get_instance().set_event_batch(handle, event_type, interval, latency))
933                 return false;
934
935         sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
936
937         ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
938
939         if (!ret)
940                 sensor_client_info::get_instance().set_event_batch(handle, event_type, prev_interval, prev_latency);
941
942         return ret;
943 }
944
945 API bool sensord_change_event_interval(int handle, unsigned int event_type, unsigned int interval)
946 {
947         unsigned int prev_interval, prev_latency;
948         void *prev_cb;
949         void *prev_user_data;
950
951         AUTOLOCK(lock);
952
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);
955                 return false;
956         }
957
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);
960 }
961
962 API bool sensord_change_event_max_batch_latency(int handle, unsigned int event_type, unsigned int max_batch_latency)
963 {
964         unsigned int prev_interval, prev_latency;
965         void *prev_cb;
966         void *prev_user_data;
967
968         AUTOLOCK(lock);
969
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);
972                 return false;
973         }
974
975         return change_event_batch(handle, event_type, prev_interval, max_batch_latency);
976 }
977
978 static int change_pause_policy(int handle, int pause)
979 {
980         sensor_id_t sensor_id;
981         sensor_rep prev_rep, cur_rep;
982         int sensor_state;
983         bool ret;
984         int prev_state, prev_pause;
985
986         AUTOLOCK(lock);
987
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());
991
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());
995                 return -EPERM;
996         }
997
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);
1000
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);
1006         }
1007         sensor_client_info::get_instance().set_sensor_pause_policy(handle, pause);
1008
1009         sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
1010         ret =  change_sensor_rep(sensor_id, prev_rep, cur_rep);
1011
1012         if (!ret)
1013                 sensor_client_info::get_instance().set_sensor_pause_policy(handle, prev_pause);
1014
1015         return (ret ? OP_SUCCESS : OP_ERROR);
1016 }
1017
1018 static int change_axis_orientation(int handle, int axis_orientation)
1019 {
1020         return OP_SUCCESS;
1021 }
1022
1023 static int change_attribute_int(int handle, int attribute, int value)
1024 {
1025         sensor_id_t sensor_id;
1026         command_channel *cmd_channel;
1027         int client_id;
1028
1029         AUTOLOCK(lock);
1030
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());
1033                 return -EINVAL;
1034         }
1035
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));
1038                 return -EPERM;
1039         }
1040
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());
1045
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());
1049                 return -EPERM;
1050         }
1051
1052         return OP_SUCCESS;
1053 }
1054
1055 API bool sensord_set_option(int handle, int option)
1056 {
1057         return (change_pause_policy(handle, CONVERT_OPTION_PAUSE_POLICY(option)) == OP_SUCCESS);
1058 }
1059
1060 API int sensord_set_attribute_int(int handle, int attribute, int value)
1061 {
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);
1067         default:
1068                 return change_attribute_int(handle, attribute, value);
1069         }
1070
1071         return OP_SUCCESS;
1072 }
1073
1074 API int sensord_set_attribute_str(int handle, int attribute, const char *value, int value_len)
1075 {
1076         sensor_id_t sensor_id;
1077         command_channel *cmd_channel;
1078         int client_id;
1079
1080         AUTOLOCK(lock);
1081
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());
1084                 return -EINVAL;
1085         }
1086
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));
1090                 return -EPERM;
1091         }
1092
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());
1096
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());
1101
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());
1105                 return -EPERM;
1106         }
1107
1108         return OP_SUCCESS;
1109 }
1110
1111 API bool sensord_send_sensorhub_data(int handle, const char *data, int data_len)
1112 {
1113         return (sensord_set_attribute_str(handle, 0, data, data_len) == OP_SUCCESS);
1114 }
1115
1116 API bool sensord_send_command(int handle, const char *command, int command_len)
1117 {
1118         return (sensord_set_attribute_str(handle, 0, command, command_len) == OP_SUCCESS);
1119 }
1120
1121 API bool sensord_get_data(int handle, unsigned int data_id, sensor_data_t* sensor_data)
1122 {
1123         sensor_id_t sensor_id;
1124         command_channel *cmd_channel;
1125         int sensor_state;
1126         int client_id;
1127
1128         retvm_if((!sensor_data), false, "sensor_data is NULL");
1129
1130         AUTOLOCK(lock);
1131
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());
1135                 return false;
1136         }
1137
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));
1140                 return false;
1141         }
1142
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());
1145
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);
1148                 return false;
1149         }
1150
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());
1153                 return false;
1154         }
1155
1156         return true;
1157 }
1158
1159 API bool sensord_flush(int handle)
1160 {
1161         sensor_id_t sensor_id;
1162         command_channel *cmd_channel;
1163         int sensor_state;
1164         int client_id;
1165
1166         AUTOLOCK(lock);
1167
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());
1171                 return false;
1172         }
1173
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));
1176                 return false;
1177         }
1178
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());
1181
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);
1184                 return false;
1185         }
1186
1187         if (!cmd_channel->cmd_flush()) {
1188                 _E("cmd_flush() failed for %s", get_client_name());
1189                 return false;
1190         }
1191
1192         return true;
1193 }
1194
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)
1196 {
1197         return false;
1198 }
1199