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