sensord: fix the bug which converts wrong pause policy
[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) ((option) ^ 0b11)
48
49 static cmutex lock;
50
51 static int g_power_save_state = SENSORD_PAUSE_NONE;
52
53 static int get_power_save_state(void);
54 static void power_save_state_cb(keynode_t *node, void *data);
55 static void clean_up(void);
56 static bool change_sensor_rep(sensor_id_t sensor_id, sensor_rep &prev_rep, sensor_rep &cur_rep);
57 static void restore_session(void);
58 static bool register_event(int handle, unsigned int event_type, unsigned int interval, int max_batch_latency, void* cb, void *user_data);
59
60 class initiator {
61 public:
62         initiator()
63         {
64                 sensor_event_listener::get_instance().set_hup_observer(restore_session);
65         }
66 };
67
68 void good_bye(void)
69 {
70         _I("Good bye! %s\n", get_client_name());
71         clean_up();
72 }
73
74 static initiator g_initiator;
75
76 static int g_power_save_state_cb_cnt = 0;
77
78 static void set_power_save_state_cb(void)
79 {
80         if (g_power_save_state_cb_cnt < 0)
81                 _E("g_power_save_state_cb_cnt(%d) is wrong", g_power_save_state_cb_cnt);
82
83         ++g_power_save_state_cb_cnt;
84
85         if (g_power_save_state_cb_cnt == 1) {
86                 _D("Power save callback is registered");
87                 g_power_save_state = get_power_save_state();
88                 _D("power_save_state = [%d]", g_power_save_state);
89                 vconf_notify_key_changed(VCONFKEY_PM_STATE, power_save_state_cb, NULL);
90                 vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE, power_save_state_cb, NULL);
91         }
92 }
93
94 static void unset_power_save_state_cb(void)
95 {
96         --g_power_save_state_cb_cnt;
97
98         if (g_power_save_state_cb_cnt < 0)
99                 _E("g_power_save_state_cb_cnt(%d) is wrong", g_power_save_state_cb_cnt);
100
101         if (g_power_save_state_cb_cnt == 0) {
102                 _D("Power save callback is unregistered");
103                 vconf_ignore_key_changed(VCONFKEY_PM_STATE, power_save_state_cb);
104                 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE, power_save_state_cb);
105         }
106 }
107
108 void clean_up(void)
109 {
110         handle_vector handles;
111
112         sensor_client_info::get_instance().get_all_handles(handles);
113
114         auto it_handle = handles.begin();
115
116         while (it_handle != handles.end()) {
117                 sensord_disconnect(*it_handle);
118                 ++it_handle;
119         }
120 }
121
122 static int get_power_save_state(void)
123 {
124         int ret;
125         int state = 0;
126         int pm_state, ps_state;
127
128         ret = vconf_get_int(VCONFKEY_PM_STATE, &pm_state);
129
130         if (!ret && pm_state == VCONFKEY_PM_STATE_LCDOFF)
131                 state |= SENSORD_PAUSE_ON_DISPLAY_OFF;
132
133         ret = vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &ps_state);
134
135         if (!ret && ps_state != SETTING_PSMODE_NORMAL)
136                 state |= SENSORD_PAUSE_ON_POWERSAVE_MODE;
137
138         return state;
139 }
140
141 static void power_save_state_cb(keynode_t *node, void *data)
142 {
143         int cur_power_save_state;
144         sensor_id_vector sensors;
145         sensor_rep prev_rep, cur_rep;
146
147         AUTOLOCK(lock);
148
149         cur_power_save_state = get_power_save_state();
150
151         if (cur_power_save_state == g_power_save_state) {
152                 _D("g_power_save_state NOT changed : [%d]", cur_power_save_state);
153                 return;
154         }
155
156         g_power_save_state = cur_power_save_state;
157         _D("power_save_state: %d noti to %s", g_power_save_state, get_client_name());
158
159         sensor_client_info::get_instance().get_listening_sensors(sensors);
160
161         auto it_sensor = sensors.begin();
162
163         while (it_sensor != sensors.end()) {
164                 sensor_client_info::get_instance().get_sensor_rep(*it_sensor, prev_rep);
165                 sensor_client_info::get_instance().set_pause_policy(*it_sensor, cur_power_save_state);
166                 sensor_client_info::get_instance().get_sensor_rep(*it_sensor, cur_rep);
167                 change_sensor_rep(*it_sensor, prev_rep, cur_rep);
168
169                 ++it_sensor;
170         }
171 }
172
173 bool restore_attributes(int client_id, sensor_id_t sensor, command_channel *cmd_channel)
174 {
175         sensor_handle_info_map handle_infos;
176
177         sensor_client_info::get_instance().get_sensor_handle_info(sensor, handle_infos);
178
179         for (auto it_handles = handle_infos.begin(); it_handles != handle_infos.end(); ++it_handles) {
180                 sensor_handle_info info = it_handles->second;
181
182                 for (auto it = info.attributes_int.begin(); it != info.attributes_int.end(); ++it) {
183                         int attribute = it->first;
184                         int value = it->second;
185                         if (!cmd_channel->cmd_set_attribute_int(attribute, value)) {
186                                 _E("Failed to send cmd_set_attribute_int(%d, %d) for %s",
187                                     client_id, value, get_client_name());
188                                 return false;
189                         }
190                 }
191
192                 for (auto it = info.attributes_str.begin(); it != info.attributes_str.end(); ++it) {
193                         int attribute = it->first;
194                         const char *value = it->second.c_str();
195                         int value_len = it->second.size();
196                         if (!cmd_channel->cmd_set_attribute_str(attribute, value, value_len)) {
197                                 _E("Failed to send cmd_set_attribute_str(%d, %d, %s) for %s",
198                                     client_id, value_len, value, get_client_name());
199                                 return false;
200                         }
201                 }
202         }
203
204         return true;
205 }
206
207 void restore_session(void)
208 {
209         AUTOLOCK(lock);
210
211         _I("Trying to restore sensor client session for %s", get_client_name());
212
213         command_channel *cmd_channel;
214         int client_id;
215
216         sensor_client_info::get_instance().close_command_channel();
217         sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
218
219         sensor_id_vector sensors;
220
221         sensor_client_info::get_instance().get_listening_sensors(sensors);
222
223         bool first_connection = true;
224
225         auto it_sensor = sensors.begin();
226
227         while (it_sensor != sensors.end()) {
228                 cmd_channel = new(std::nothrow) command_channel();
229                 retm_if(!cmd_channel, "Failed to allocate memory");
230
231                 if (!cmd_channel->create_channel()) {
232                         _E("%s failed to create command channel for %s", get_client_name(), get_sensor_name(*it_sensor));
233                         delete cmd_channel;
234                         goto FAILED;
235                 }
236
237                 sensor_client_info::get_instance().add_command_channel(*it_sensor, cmd_channel);
238
239                 if (first_connection) {
240                         first_connection = false;
241                         if (!cmd_channel->cmd_get_id(client_id)) {
242                                 _E("Failed to get client id");
243                                 goto FAILED;
244                         }
245
246                         sensor_client_info::get_instance().set_client_id(client_id);
247                         sensor_event_listener::get_instance().start_event_listener();
248                 }
249
250                 cmd_channel->set_client_id(client_id);
251
252                 if (!cmd_channel->cmd_hello(*it_sensor)) {
253                         _E("Sending cmd_hello(%s, %d) failed for %s", get_sensor_name(*it_sensor), client_id, get_client_name());
254                         goto FAILED;
255                 }
256
257                 sensor_rep prev_rep, cur_rep;
258                 prev_rep.active = false;
259                 prev_rep.pause_policy = SENSORD_PAUSE_ALL;
260                 prev_rep.interval = 0;
261
262                 sensor_client_info::get_instance().get_sensor_rep(*it_sensor, cur_rep);
263                 if (!change_sensor_rep(*it_sensor, prev_rep, cur_rep)) {
264                         _E("Failed to change rep(%s) for %s", get_sensor_name(*it_sensor), get_client_name());
265                         goto FAILED;
266                 }
267
268                 if (!restore_attributes(client_id, *it_sensor, cmd_channel)) {
269                         _E("Failed to restore attributes(%s) for %s", get_sensor_name(*it_sensor), get_client_name());
270                         goto FAILED;
271                 }
272                 ++it_sensor;
273         }
274
275         _I("Succeeded to restore sensor client session for %s", get_client_name());
276
277         return;
278
279 FAILED:
280         sensor_event_listener::get_instance().clear();
281         _E("Failed to restore session for %s", get_client_name());
282 }
283
284 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)
285 {
286         sort(a_vec.begin(), a_vec.end());
287         sort(b_vec.begin(), b_vec.end());
288
289         set_difference(a_vec.begin(), a_vec.end(), b_vec.begin(), b_vec.end(), back_inserter(del_vec));
290         set_difference(b_vec.begin(), b_vec.end(), a_vec.begin(), a_vec.end(), back_inserter(add_vec));
291
292         return !(add_vec.empty() && del_vec.empty());
293 }
294
295 static bool change_sensor_rep(sensor_id_t sensor_id, sensor_rep &prev_rep, sensor_rep &cur_rep)
296 {
297         int client_id;
298         command_channel *cmd_channel;
299         event_type_vector add_event_types, del_event_types;
300
301         if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
302                 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
303                 return false;
304         }
305
306         client_id = sensor_client_info::get_instance().get_client_id();
307         retvm_if((client_id < 0), false, "Invalid client id : %d, %s, %s", client_id, get_sensor_name(sensor_id), get_client_name());
308
309         get_events_diff(prev_rep.event_types, cur_rep.event_types, add_event_types, del_event_types);
310
311         if (cur_rep.active) {
312                 if (prev_rep.pause_policy != cur_rep.pause_policy) {
313                         if (!cmd_channel->cmd_set_pause_policy(cur_rep.pause_policy)) {
314                                 _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());
315                                 return false;
316                         }
317                 }
318
319                 if ( (prev_rep.interval != cur_rep.interval) || (prev_rep.latency != cur_rep.latency)) {
320                         if (!cmd_channel->cmd_set_batch(cur_rep.interval, cur_rep.latency)) {
321                                 _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());
322                                 return false;
323                         }
324                 }
325
326                 if (!add_event_types.empty()) {
327                         if (!cmd_channel->cmd_register_events(add_event_types)) {
328                                 _E("Sending cmd_register_events(%d, add_event_types) failed for %s", client_id, get_client_name());
329                                 return false;
330                         }
331                 }
332         }
333
334         if (prev_rep.active && !del_event_types.empty()) {
335                 if (!cmd_channel->cmd_unregister_events(del_event_types)) {
336                         _E("Sending cmd_unregister_events(%d, del_event_types) failed for %s", client_id, get_client_name());
337                         return false;
338                 }
339         }
340
341         if (prev_rep.active != cur_rep.active) {
342                 if (cur_rep.active) {
343                         if (!cmd_channel->cmd_start()) {
344                                 _E("Sending cmd_start(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
345                                 return false;
346                         }
347                 } else {
348                         if (!cmd_channel->cmd_unset_batch()) {
349                                 _E("Sending cmd_unset_interval(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
350                                 return false;
351                         }
352
353                         if (!cmd_channel->cmd_stop()) {
354                                 _E("Sending cmd_stop(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
355                                 return false;
356                         }
357                 }
358         }
359
360         return true;
361 }
362
363 static bool get_sensor_list(void)
364 {
365         static cmutex l;
366         static bool init = false;
367
368         AUTOLOCK(l);
369
370         if (!init) {
371                 command_channel cmd_channel;
372
373                 if (!cmd_channel.create_channel()) {
374                         _E("%s failed to create command channel", get_client_name());
375                         return false;
376                 }
377
378                 if (!cmd_channel.cmd_get_sensor_list())
379                         return false;
380
381                 init = true;
382         }
383
384         return true;
385 }
386
387 API int sensord_get_sensors(sensor_type_t type, sensor_t **list, int *sensor_count)
388 {
389         retvm_if(!get_sensor_list(), -EPERM, "Fail to get sensor list from server");
390
391         vector<sensor_info *> sensor_infos = sensor_info_manager::get_instance().get_infos(type);
392
393         if (sensor_infos.empty()) {
394                 *sensor_count = 0;
395                 return -ENODATA;
396         }
397
398         if (type != ALL_SENSOR) {
399                 if (sensor_infos[0]->get_id() == (sensor_id_t)(-EACCES))
400                         return -EACCES;
401         }
402
403         *list = (sensor_t *) malloc(sizeof(sensor_info *) * sensor_infos.size());
404         retvm_if(!*list, false, "Failed to allocate memory");
405
406         for (unsigned int i = 0; i < sensor_infos.size(); ++i)
407                 *(*list + i) = sensor_info_to_sensor(sensor_infos[i]);
408
409         *sensor_count = sensor_infos.size();
410
411         return OP_SUCCESS;
412 }
413
414 API int sensord_get_default_sensor(sensor_type_t type, sensor_t *sensor)
415 {
416         retvm_if(!get_sensor_list(), -EPERM, "Fail to get sensor list from server");
417
418         const sensor_info *info;
419
420         info = sensor_info_manager::get_instance().get_info(type);
421         if (info == NULL) {
422                 *sensor = NULL;
423                 return -ENODATA;
424         }
425
426         if ((const_cast<sensor_info *>(info))->get_id() == (sensor_id_t)(-EACCES))
427                 return -EACCES;
428
429         *sensor = sensor_info_to_sensor(info);
430
431         return OP_SUCCESS;
432 }
433
434 API bool sensord_get_sensor_list(sensor_type_t type, sensor_t **list, int *sensor_count)
435 {
436         return (sensord_get_sensors(type, list, sensor_count) == OP_SUCCESS);
437 }
438
439 API sensor_t sensord_get_sensor(sensor_type_t type)
440 {
441         sensor_t sensor;
442         sensord_get_default_sensor(type, &sensor);
443
444         return sensor;
445 }
446
447 API bool sensord_get_type(sensor_t sensor, sensor_type_t *type)
448 {
449         sensor_info* info = sensor_to_sensor_info(sensor);
450
451         retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !type,
452                 NULL, "Invalid param: sensor (%p), type(%p)", sensor, type);
453
454         *type = info->get_type();
455
456         return true;
457 }
458
459 API const char* sensord_get_name(sensor_t sensor)
460 {
461         sensor_info* info = sensor_to_sensor_info(sensor);
462
463         retvm_if(!sensor_info_manager::get_instance().is_valid(info),
464                 NULL, "Invalid param: sensor (%p)", sensor);
465
466         return info->get_name();
467 }
468
469 API const char* sensord_get_vendor(sensor_t sensor)
470 {
471         sensor_info* info = sensor_to_sensor_info(sensor);
472
473         retvm_if(!sensor_info_manager::get_instance().is_valid(info),
474                 NULL, "Invalid param: sensor (%p)", sensor);
475
476         return info->get_vendor();
477 }
478
479 API bool sensord_get_privilege(sensor_t sensor, sensor_privilege_t *privilege)
480 {
481         sensor_info* info = sensor_to_sensor_info(sensor);
482
483         retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !privilege,
484                 false, "Invalid param: sensor (%p), privilege(%p)", sensor, privilege);
485
486         *privilege = info->get_privilege();
487
488         return true;
489 }
490
491 API bool sensord_get_min_range(sensor_t sensor, float *min_range)
492 {
493         sensor_info* info = sensor_to_sensor_info(sensor);
494
495         retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !min_range,
496                 false, "Invalid param: sensor (%p), min_range(%p)", sensor, min_range);
497
498         *min_range = info->get_min_range();
499
500         return true;
501 }
502
503 API bool sensord_get_max_range(sensor_t sensor, float *max_range)
504 {
505         sensor_info* info = sensor_to_sensor_info(sensor);
506
507         retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !max_range,
508                 false, "Invalid param: sensor (%p), max_range(%p)", sensor, max_range);
509
510         *max_range = info->get_max_range();
511
512         return true;
513 }
514
515 API bool sensord_get_resolution(sensor_t sensor, float *resolution)
516 {
517         sensor_info* info = sensor_to_sensor_info(sensor);
518
519         retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !resolution,
520                 false, "Invalid param: sensor (%p), resolution(%p)", sensor, resolution);
521
522         *resolution = info->get_resolution();
523
524         return true;
525 }
526
527 API bool sensord_get_min_interval(sensor_t sensor, int *min_interval)
528 {
529         sensor_info* info = sensor_to_sensor_info(sensor);
530
531         retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !min_interval,
532                 false, "Invalid param: sensor (%p), min_interval(%p)", sensor, min_interval);
533
534         *min_interval = info->get_min_interval();
535
536         return true;
537 }
538
539 API bool sensord_get_fifo_count(sensor_t sensor, int *fifo_count)
540 {
541         sensor_info* info = sensor_to_sensor_info(sensor);
542
543         retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !fifo_count,
544                 false, "Invalid param: sensor (%p), fifo_count(%p)", sensor, fifo_count);
545
546         *fifo_count = info->get_fifo_count();
547
548         return true;
549 }
550
551 API bool sensord_get_max_batch_count(sensor_t sensor, int *max_batch_count)
552 {
553         sensor_info* info = sensor_to_sensor_info(sensor);
554
555         retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !max_batch_count,
556                 false, "Invalid param: sensor (%p), max_batch_count(%p)", sensor, max_batch_count);
557
558         *max_batch_count = info->get_max_batch_count();
559
560         return true;
561 }
562
563 API bool sensord_get_supported_event_types(sensor_t sensor, unsigned int **event_types, int *count)
564 {
565         sensor_info* info = sensor_to_sensor_info(sensor);
566
567         retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !event_types || !count,
568                 false, "Invalid param: sensor (%p), event_types(%p), count(%p)", sensor, event_types, count);
569
570         unsigned int event_type;
571         event_type = info->get_supported_event();
572         *event_types = (unsigned int *)malloc(sizeof(unsigned int));
573
574         retvm_if(!*event_types, false, "Failed to allocate memory");
575
576         (*event_types)[0] = event_type;
577         *count = 1;
578
579         return true;
580 }
581
582 API bool sensord_is_supported_event_type(sensor_t sensor, unsigned int event_type, bool *supported)
583 {
584         sensor_info* info = sensor_to_sensor_info(sensor);
585
586         retvm_if(!sensor_info_manager::get_instance().is_valid(info) || !event_type || !supported,
587                 false, "Invalid param: sensor (%p), event_type(%p), supported(%p)", sensor, event_type, supported);
588
589         *supported = info->is_supported_event(event_type);
590
591         return true;
592 }
593
594 API bool sensord_is_wakeup_supported(sensor_t sensor)
595 {
596         sensor_info* info = sensor_to_sensor_info(sensor);
597
598         retvm_if(!sensor_info_manager::get_instance().is_valid(info),
599                 false, "Invalid param: sensor (%p)", sensor);
600
601         return info->is_wakeup_supported();
602 }
603
604 API int sensord_connect(sensor_t sensor)
605 {
606         command_channel *cmd_channel = NULL;
607         int handle;
608         int client_id;
609         bool sensor_registered;
610         bool first_connection = false;
611
612         sensor_info* info = sensor_to_sensor_info(sensor);
613
614         retvm_if(!sensor_info_manager::get_instance().is_valid(info),
615                 OP_ERROR, "Invalid param: sensor (%p)", sensor);
616
617         sensor_id_t sensor_id = info->get_id();
618
619         AUTOLOCK(lock);
620
621         sensor_registered = sensor_client_info::get_instance().is_sensor_registered(sensor_id);
622
623         // lazy loading after creating static variables
624         atexit(good_bye);
625
626         handle = sensor_client_info::get_instance().create_handle(sensor_id);
627         if (handle == MAX_HANDLE) {
628                 _E("Maximum number of handles reached, sensor: %s in client %s", get_sensor_name(sensor_id), get_client_name());
629                 return OP_ERROR;
630         }
631
632         if (!sensor_registered) {
633                 cmd_channel = new(std::nothrow) command_channel();
634                 if (!cmd_channel) {
635                         _E("Failed to allocated memory");
636                         sensor_client_info::get_instance().delete_handle(handle);
637                         return OP_ERROR;
638                 }
639
640                 if (!cmd_channel->create_channel()) {
641                         _E("%s failed to create command channel for %s", get_client_name(), get_sensor_name(sensor_id));
642                         sensor_client_info::get_instance().delete_handle(handle);
643                         delete cmd_channel;
644                         return OP_ERROR;
645                 }
646
647                 sensor_client_info::get_instance().add_command_channel(sensor_id, cmd_channel);
648         }
649
650         if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
651                 _E("%s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
652                 sensor_client_info::get_instance().delete_handle(handle);
653                 return OP_ERROR;
654         }
655
656         if (!sensor_client_info::get_instance().has_client_id()) {
657                 first_connection = true;
658                 if (!cmd_channel->cmd_get_id(client_id)) {
659                         _E("Sending cmd_get_id() failed for %s", get_sensor_name(sensor_id));
660                         sensor_client_info::get_instance().close_command_channel(sensor_id);
661                         sensor_client_info::get_instance().delete_handle(handle);
662                         return OP_ERROR;
663                 }
664
665                 sensor_client_info::get_instance().set_client_id(client_id);
666                 _I("%s gets client_id [%d]", get_client_name(), client_id);
667                 sensor_event_listener::get_instance().start_event_listener();
668                 _I("%s starts listening events with client_id [%d]", get_client_name(), client_id);
669         }
670
671         client_id = sensor_client_info::get_instance().get_client_id();
672         cmd_channel->set_client_id(client_id);
673
674         _I("%s[%d] connects with %s[%d]", get_client_name(), client_id, get_sensor_name(sensor_id), handle);
675
676         sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_STOPPED, SENSORD_PAUSE_ALL);
677
678         if (!sensor_registered) {
679                 if (!cmd_channel->cmd_hello(sensor_id)) {
680                         _E("Sending cmd_hello(%s, %d) failed for %s", get_sensor_name(sensor_id), client_id, get_client_name());
681                         sensor_client_info::get_instance().close_command_channel(sensor_id);
682                         sensor_client_info::get_instance().delete_handle(handle);
683                         if (first_connection) {
684                                 sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
685                                 sensor_event_listener::get_instance().stop_event_listener();
686                         }
687                         return OP_ERROR;
688                 }
689         }
690
691         set_power_save_state_cb();
692         dbus_listener::init();
693         return handle;
694 }
695
696 API bool sensord_disconnect(int handle)
697 {
698         command_channel *cmd_channel;
699         sensor_id_t sensor_id;
700         int client_id;
701         int sensor_state;
702
703         AUTOLOCK(lock);
704
705         if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
706                 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
707                 _E("client %s failed to get handle information", get_client_name());
708                 return false;
709         }
710
711         if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
712                 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
713                 return false;
714         }
715
716         client_id = sensor_client_info::get_instance().get_client_id();
717         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());
718
719         _I("%s disconnects with %s[%d]", get_client_name(), get_sensor_name(sensor_id), handle);
720
721         if (sensor_client_info::get_instance().get_passive_mode(handle)) {
722                 _W("%s[%d] for %s is on passive mode while disconnecting.",
723                         get_sensor_name(sensor_id), handle, get_client_name());
724
725                 command_channel *cmd_channel;
726                 event_type_vector event_types;
727                 sensor_client_info::get_instance().get_active_event_types(sensor_id, event_types);
728
729                 for (auto it = event_types.begin(); it != event_types.end(); ++it)
730                         sensord_unregister_event(handle, *it);
731
732                 if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
733                         _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
734                         return false;
735                 }
736
737                 if (!cmd_channel->cmd_unset_batch()) {
738                         _E("Sending cmd_unset_interval(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
739                         return false;
740                 }
741         }
742
743         if (sensor_state != SENSOR_STATE_STOPPED) {
744                 _W("%s[%d] for %s is not stopped before disconnecting.",
745                         get_sensor_name(sensor_id), handle, get_client_name());
746                 sensord_stop(handle);
747         }
748
749         if (!sensor_client_info::get_instance().delete_handle(handle))
750                 return false;
751
752         if (!sensor_client_info::get_instance().is_active())
753                 sensor_client_info::get_instance().set_client_id(CLIENT_ID_INVALID);
754
755         if (!sensor_client_info::get_instance().is_sensor_registered(sensor_id)) {
756                 if (!cmd_channel->cmd_byebye()) {
757                         _E("Sending cmd_byebye(%d, %s) failed for %s", client_id, get_sensor_name(sensor_id), get_client_name());
758                         return false;
759                 }
760                 sensor_client_info::get_instance().close_command_channel(sensor_id);
761         }
762
763         if (!sensor_client_info::get_instance().is_active()) {
764                 _I("Stop listening events for client %s with client id [%d]", get_client_name(), sensor_client_info::get_instance().get_client_id());
765                 sensor_event_listener::get_instance().stop_event_listener();
766         }
767
768         unset_power_save_state_cb();
769
770         return true;
771 }
772
773 static bool register_event(int handle, unsigned int event_type, unsigned int interval, int max_batch_latency, void* cb, void *user_data)
774 {
775         sensor_id_t sensor_id;
776         sensor_rep prev_rep, cur_rep;
777         bool ret;
778
779         retvm_if(!cb, false, "callback is NULL");
780
781         AUTOLOCK(lock);
782
783         if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
784                 _E("client %s failed to get handle information", get_client_name());
785                 return false;
786         }
787
788         if (interval == 0)
789                 interval = DEFAULT_INTERVAL;
790
791         _I("%s registers event %s[%#x] for sensor %s[%d] with interval: %d, latency: %d,  cb: %#x, user_data: %#x",
792                 get_client_name(), get_event_name(event_type), event_type, get_sensor_name(sensor_id),
793                 handle, interval, max_batch_latency, cb, user_data);
794
795         sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
796         sensor_client_info::get_instance().register_event(handle, event_type, interval, max_batch_latency, cb, user_data);
797         sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
798         ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
799
800         if (!ret)
801                 sensor_client_info::get_instance().unregister_event(handle, event_type);
802
803         return ret;
804 }
805
806 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)
807 {
808         return register_event(handle, event_type, interval, max_batch_latency, (void *)cb, user_data);
809 }
810
811 API bool sensord_unregister_event(int handle, unsigned int event_type)
812 {
813         sensor_id_t sensor_id;
814         sensor_rep prev_rep, cur_rep;
815         bool ret;
816         unsigned int prev_interval, prev_latency;
817         void *prev_cb;
818         void *prev_user_data;
819
820         AUTOLOCK(lock);
821
822         if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
823                 _E("client %s failed to get handle information", get_client_name());
824                 return false;
825         }
826
827         _I("%s unregisters event %s[%#x] for sensor %s[%d]", get_client_name(), get_event_name(event_type),
828                 event_type, get_sensor_name(sensor_id), handle);
829
830         sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
831         sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data);
832
833         if (!sensor_client_info::get_instance().unregister_event(handle, event_type)) {
834                 _E("%s try to unregister non registered event %s[%#x] for sensor %s[%d]",
835                         get_client_name(), get_event_name(event_type), event_type, get_sensor_name(sensor_id), handle);
836                 return false;
837         }
838
839         sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
840         ret =  change_sensor_rep(sensor_id, prev_rep, cur_rep);
841
842         if (sensor_client_info::get_instance().get_passive_mode(handle))
843                 sensor_client_info::get_instance().set_passive_mode(handle, false);
844
845         if (!ret)
846                 sensor_client_info::get_instance().register_event(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data);
847
848         return ret;
849 }
850
851 API bool sensord_register_accuracy_cb(int handle, sensor_accuracy_changed_cb_t cb, void *user_data)
852 {
853         sensor_id_t sensor_id;
854
855         retvm_if(!cb, false, "callback is NULL");
856
857         AUTOLOCK(lock);
858
859         if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
860                 _E("client %s failed to get handle information", get_client_name());
861                 return false;
862         }
863
864         _I("%s registers accuracy_changed_cb for sensor %s[%d] with cb: %#x, user_data: %#x",
865                 get_client_name(), get_sensor_name(sensor_id), handle, cb, user_data);
866
867         sensor_client_info::get_instance().register_accuracy_cb(handle, cb , user_data);
868
869         return true;
870 }
871
872 API bool sensord_unregister_accuracy_cb(int handle)
873 {
874         sensor_id_t sensor_id;
875
876         AUTOLOCK(lock);
877
878         if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
879                 _E("client %s failed to get handle information", get_client_name());
880                 return false;
881         }
882
883         _I("%s unregisters accuracy_changed_cb for sensor %s[%d]",
884                 get_client_name(), get_sensor_name(sensor_id), handle);
885
886         sensor_client_info::get_instance().unregister_accuracy_cb(handle);
887
888         return true;
889 }
890
891 API bool sensord_start(int handle, int option)
892 {
893         sensor_id_t sensor_id;
894         sensor_rep prev_rep, cur_rep;
895         bool ret;
896         int prev_state, prev_pause;
897         int pause;
898
899         AUTOLOCK(lock);
900
901         if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
902                 _E("client %s failed to get handle information", get_client_name());
903                 return false;
904         }
905
906         retvm_if((option < 0) || (option >= SENSOR_OPTION_END), false, "Invalid option value : %d, handle: %d, %s, %s",
907                 option, handle, get_sensor_name(sensor_id), get_client_name());
908
909         _I("%s starts %s[%d], with option: %d, power save state: %d", get_client_name(), get_sensor_name(sensor_id),
910                 handle, option, g_power_save_state);
911
912         pause = CONVERT_OPTION_PAUSE_POLICY(option);
913
914         if (g_power_save_state && (g_power_save_state & pause)) {
915                 sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_PAUSED, pause);
916                 return true;
917         }
918
919         sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
920         sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_pause);
921         sensor_client_info::get_instance().set_sensor_params(handle, SENSOR_STATE_STARTED, pause);
922         sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
923
924         ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
925
926         if (!ret)
927                 sensor_client_info::get_instance().set_sensor_params(handle, prev_state, prev_pause);
928
929         return ret;
930 }
931
932 API bool sensord_stop(int handle)
933 {
934         sensor_id_t sensor_id;
935         int sensor_state;
936         bool ret;
937         int prev_state, prev_pause;
938
939         sensor_rep prev_rep, cur_rep;
940
941         AUTOLOCK(lock);
942
943         if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
944                 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
945                 _E("client %s failed to get handle information", get_client_name());
946                 return false;
947         }
948
949         retvm_if((sensor_state == SENSOR_STATE_STOPPED), true, "%s already stopped with %s[%d]",
950                 get_client_name(), get_sensor_name(sensor_id), handle);
951
952         _I("%s stops sensor %s[%d]", get_client_name(), get_sensor_name(sensor_id), handle);
953
954         sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
955         sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_pause);
956         sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_STOPPED);
957         sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
958
959         ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
960
961         if (!ret)
962                 sensor_client_info::get_instance().set_sensor_params(handle, prev_state, prev_pause);
963
964         return ret;
965 }
966
967 static bool change_event_batch(int handle, unsigned int event_type, unsigned int interval, unsigned int latency)
968 {
969         sensor_id_t sensor_id;
970         sensor_rep prev_rep, cur_rep;
971         bool ret;
972         unsigned int prev_interval, prev_latency;
973         void *prev_cb;
974         void *prev_user_data;
975
976         AUTOLOCK(lock);
977
978         if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
979                 _E("client %s failed to get handle information", get_client_name());
980                 return false;
981         }
982
983         if (interval == 0)
984                 interval = DEFAULT_INTERVAL;
985
986         _I("%s changes batch of event %s[%#x] for %s[%d] to (%d, %d)", get_client_name(), get_event_name(event_type),
987                         event_type, get_sensor_name(sensor_id), handle, interval, latency);
988
989         sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
990
991         sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data);
992
993         if (!sensor_client_info::get_instance().set_event_batch(handle, event_type, interval, latency))
994                 return false;
995
996         sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
997
998         ret = change_sensor_rep(sensor_id, prev_rep, cur_rep);
999
1000         if (!ret)
1001                 sensor_client_info::get_instance().set_event_batch(handle, event_type, prev_interval, prev_latency);
1002
1003         return ret;
1004 }
1005
1006 API bool sensord_change_event_interval(int handle, unsigned int event_type, unsigned int interval)
1007 {
1008         unsigned int prev_interval, prev_latency;
1009         void *prev_cb;
1010         void *prev_user_data;
1011
1012         AUTOLOCK(lock);
1013
1014         if (!sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data)) {
1015                 _E("Failed to get event info with handle = %d, event_type = %#x", handle, event_type);
1016                 return false;
1017         }
1018
1019         _I("handle = %d, event_type = %#x, interval = %d, prev_latency = %d", handle, event_type, interval, prev_latency);
1020         return change_event_batch(handle, event_type, interval, prev_latency);
1021 }
1022
1023 API bool sensord_change_event_max_batch_latency(int handle, unsigned int event_type, unsigned int max_batch_latency)
1024 {
1025         unsigned int prev_interval, prev_latency;
1026         void *prev_cb;
1027         void *prev_user_data;
1028
1029         AUTOLOCK(lock);
1030
1031         if (!sensor_client_info::get_instance().get_event_info(handle, event_type, prev_interval, prev_latency, prev_cb, prev_user_data)) {
1032                 _E("Failed to get event info with handle = %d, event_type = %#x", handle, event_type);
1033                 return false;
1034         }
1035
1036         return change_event_batch(handle, event_type, prev_interval, max_batch_latency);
1037 }
1038
1039 static int change_pause_policy(int handle, int pause)
1040 {
1041         sensor_id_t sensor_id;
1042         sensor_rep prev_rep, cur_rep;
1043         int sensor_state;
1044         bool ret;
1045         int prev_state, prev_pause;
1046
1047         AUTOLOCK(lock);
1048
1049         retvm_if((pause < 0) || (pause >= SENSORD_PAUSE_END), -EINVAL,
1050                 "Invalid pause value : %d, handle: %d, %s, %s",
1051                 pause, handle, get_sensor_name(sensor_id), get_client_name());
1052
1053         if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
1054                 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1055                 _E("client %s failed to get handle information", get_client_name());
1056                 return -EPERM;
1057         }
1058
1059         sensor_client_info::get_instance().get_sensor_rep(sensor_id, prev_rep);
1060         sensor_client_info::get_instance().get_sensor_params(handle, prev_state, prev_pause);
1061
1062         if (g_power_save_state) {
1063                 if ((pause & g_power_save_state) && (sensor_state == SENSOR_STATE_STARTED))
1064                         sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_PAUSED);
1065                 else if (!(pause & g_power_save_state) && (sensor_state == SENSOR_STATE_PAUSED))
1066                         sensor_client_info::get_instance().set_sensor_state(handle, SENSOR_STATE_STARTED);
1067         }
1068         sensor_client_info::get_instance().set_sensor_pause_policy(handle, pause);
1069
1070         sensor_client_info::get_instance().get_sensor_rep(sensor_id, cur_rep);
1071         ret =  change_sensor_rep(sensor_id, prev_rep, cur_rep);
1072
1073         if (!ret)
1074                 sensor_client_info::get_instance().set_sensor_pause_policy(handle, prev_pause);
1075
1076         return (ret ? OP_SUCCESS : OP_ERROR);
1077 }
1078
1079 static int change_axis_orientation(int handle, int axis_orientation)
1080 {
1081         sensor_event_listener::get_instance().set_sensor_axis(axis_orientation);
1082         return OP_SUCCESS;
1083 }
1084
1085 static int change_attribute_int(int handle, int attribute, int value)
1086 {
1087         sensor_id_t sensor_id;
1088         command_channel *cmd_channel;
1089         int client_id;
1090
1091         AUTOLOCK(lock);
1092
1093         if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1094                 _E("client %s failed to get handle information", get_client_name());
1095                 return -EINVAL;
1096         }
1097
1098         if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1099                 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
1100                 return -EPERM;
1101         }
1102
1103         client_id = sensor_client_info::get_instance().get_client_id();
1104         retvm_if((client_id < 0), -EPERM,
1105                         "Invalid client id : %d, handle: %d, %s, %s",
1106                         client_id, handle, get_sensor_name(sensor_id), get_client_name());
1107
1108         if (!cmd_channel->cmd_set_attribute_int(attribute, value)) {
1109                 _E("Sending cmd_set_attribute_int(%d, %d) failed for %s",
1110                         client_id, value, get_client_name());
1111                 return -EPERM;
1112         }
1113
1114         sensor_client_info::get_instance().set_attribute(handle, attribute, value);
1115
1116         return OP_SUCCESS;
1117 }
1118
1119 API bool sensord_set_option(int handle, int option)
1120 {
1121         return (change_pause_policy(handle, CONVERT_OPTION_PAUSE_POLICY(option)) == OP_SUCCESS);
1122 }
1123
1124 API int sensord_set_attribute_int(int handle, int attribute, int value)
1125 {
1126         switch (attribute) {
1127         case SENSORD_ATTRIBUTE_PAUSE_POLICY:
1128                 return change_pause_policy(handle, value);
1129         case SENSORD_ATTRIBUTE_AXIS_ORIENTATION:
1130                 return change_axis_orientation(handle, value);
1131         default:
1132                 return change_attribute_int(handle, attribute, value);
1133         }
1134
1135         return OP_SUCCESS;
1136 }
1137
1138 API int sensord_set_attribute_str(int handle, int attribute, const char *value, int value_len)
1139 {
1140         sensor_id_t sensor_id;
1141         command_channel *cmd_channel;
1142         int client_id;
1143
1144         AUTOLOCK(lock);
1145
1146         if (!sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1147                 _E("Client %s failed to get handle information", get_client_name());
1148                 return -EINVAL;
1149         }
1150
1151         if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1152                 _E("Client %s failed to get command channel for %s",
1153                         get_client_name(), get_sensor_name(sensor_id));
1154                 return -EPERM;
1155         }
1156
1157         retvm_if((value_len < 0) || (value == NULL), -EINVAL,
1158                         "Invalid value_len: %d, value: %#x, handle: %d, %s, %s",
1159                         value_len, value, handle, get_sensor_name(sensor_id), get_client_name());
1160
1161         client_id = sensor_client_info::get_instance().get_client_id();
1162         retvm_if((client_id < 0), -EPERM,
1163                         "Invalid client id : %d, handle: %d, %s, %s",
1164                         client_id, handle, get_sensor_name(sensor_id), get_client_name());
1165
1166         if (!cmd_channel->cmd_set_attribute_str(attribute, value, value_len)) {
1167                 _E("Sending cmd_set_attribute_str(%d, %d, %#x) failed for %s",
1168                         client_id, value_len, value, get_client_name());
1169                 return -EPERM;
1170         }
1171
1172         sensor_client_info::get_instance().set_attribute(handle, attribute, value);
1173
1174         return OP_SUCCESS;
1175 }
1176
1177 API bool sensord_send_sensorhub_data(int handle, const char *data, int data_len)
1178 {
1179         return (sensord_set_attribute_str(handle, 0, data, data_len) == OP_SUCCESS);
1180 }
1181
1182 API bool sensord_send_command(int handle, const char *command, int command_len)
1183 {
1184         return (sensord_set_attribute_str(handle, 0, command, command_len) == OP_SUCCESS);
1185 }
1186
1187 API bool sensord_get_data(int handle, unsigned int data_id, sensor_data_t* sensor_data)
1188 {
1189         sensor_id_t sensor_id;
1190         command_channel *cmd_channel;
1191         int sensor_state;
1192         int client_id;
1193
1194         retvm_if((!sensor_data), false, "sensor_data is NULL");
1195
1196         AUTOLOCK(lock);
1197
1198         if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state)||
1199                 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1200                 _E("client %s failed to get handle information", get_client_name());
1201                 return false;
1202         }
1203
1204         if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1205                 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
1206                 return false;
1207         }
1208
1209         client_id = sensor_client_info::get_instance().get_client_id();
1210         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());
1211
1212         if (sensor_state != SENSOR_STATE_STARTED) {
1213                 _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);
1214                 return false;
1215         }
1216
1217         if (!cmd_channel->cmd_get_data(data_id, sensor_data)) {
1218                 _E("cmd_get_data(%d, %d, %#x) failed for %s", client_id, data_id, sensor_data, get_client_name());
1219                 return false;
1220         }
1221
1222         return true;
1223 }
1224
1225 API bool sensord_flush(int handle)
1226 {
1227         sensor_id_t sensor_id;
1228         command_channel *cmd_channel;
1229         int sensor_state;
1230         int client_id;
1231
1232         AUTOLOCK(lock);
1233
1234         if (!sensor_client_info::get_instance().get_sensor_state(handle, sensor_state) ||
1235                 !sensor_client_info::get_instance().get_sensor_id(handle, sensor_id)) {
1236                 _E("client %s failed to get handle information", get_client_name());
1237                 return false;
1238         }
1239
1240         if (!sensor_client_info::get_instance().get_command_channel(sensor_id, &cmd_channel)) {
1241                 _E("client %s failed to get command channel for %s", get_client_name(), get_sensor_name(sensor_id));
1242                 return false;
1243         }
1244
1245         client_id = sensor_client_info::get_instance().get_client_id();
1246         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());
1247
1248         if (sensor_state != SENSOR_STATE_STARTED) {
1249                 _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);
1250                 return false;
1251         }
1252
1253         if (!cmd_channel->cmd_flush()) {
1254                 _E("cmd_flush() failed for %s", get_client_name());
1255                 return false;
1256         }
1257
1258         return true;
1259 }
1260
1261 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)
1262 {
1263         return false;
1264 }
1265
1266 API bool sensord_set_passive_mode(int handle, bool passive)
1267 {
1268         if (!sensor_client_info::get_instance().set_passive_mode(handle, passive)) {
1269                 _E("Failed to set passive mode %d", passive);
1270                 return false;
1271         }
1272
1273         return true;
1274 }