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