f0f4cd3c32e1ddb1cb7fe86022bbc601e9667761
[framework/api/sensor.git] / src / sensor.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. 
15  */
16
17
18
19
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <sensor.h>
24 #include <sensor_accel.h>
25 #include <sensor_geomag.h>
26 #include <sensor_light.h>
27 #include <sensor_proxi.h>
28 #include <sensor_motion.h>
29 #include <sensor_gyro.h>
30 #include <sensors.h>
31 #include <sensor_private.h>
32 #include <dlog.h>
33
34 #define _DEBUG 1
35
36 #ifdef _DEBUG
37 #undef LOG_TAG
38 #define LOG_TAG "TIZEN_SYSTEM_SENSOR"
39 #include <stdio.h>
40 #include <libgen.h>
41 static char* _DONT_USE_THIS_ARRAY_DIRECTLY[] = {
42         "ACCELEROMETER",
43         "MAGNETIC",
44         "ORIENTATION",
45         "GYROSCOPE",
46         "LIGHT",
47         "PROXIMITY",
48         "MOTION_SNAP",
49         "MOTION_SHAKE",
50         "MOTION_DOUBLETAP",
51     "MOTION_PANNING",
52     "MOTION_FACEDOWN"
53 };
54
55 #define _MSG_SENSOR_ERROR_IO_ERROR "Io Error"
56 #define _MSG_SENSOR_ERROR_INVALID_PARAMETER "Invalid Parameter"
57 #define _MSG_SENSOR_ERROR_OUT_OF_MEMORY "Out of Memory"
58 #define _MSG_SENSOR_ERROR_NOT_NEED_CALIBRATION "Not need calibration"
59 #define _MSG_SENSOR_ERROR_NOT_SUPPORTED "Not supported"
60 #define _MSG_SENSOR_ERROR_OPERATION_FAILED "Operation failed"
61
62 #define TYPE_NAME(type) _DONT_USE_THIS_ARRAY_DIRECTLY[type]
63
64 #define DEBUG_PRINT(txt) LOGD("%s : " txt, __FUNCTION__)
65 #define DEBUG_PRINTF(fmt, ...) LOGD("%s : " fmt, __FUNCTION__, __VA_ARGS__)
66 #define ERROR_PRINT(err) LOGD("[%s]" _MSG_##err "(0x%08x)", __FUNCTION__, err)
67 #define ERROR_PRINTF(err, fmt, ...) LOGD("[%s]" _MSG_##err "(0x%08x) : " fmt, __FUNCTION__, err, __VA_ARGS__)
68 #else
69 #define TYPE_NAME(type) ""
70 #define DEBUG_PRINT(txt)
71 #define DEBUG_PRINTF(fmt, ...)
72 #define ERROR_PRINT(err)
73 #define ERROR_PRINTF(err)
74 #endif
75         
76 #define RETURN_VAL_IF(expr, err) \
77         do { \
78                 if (expr) { \
79             ERROR_PRINT(err); \
80                         return (err); \
81                 } \
82         } while(0)
83
84 #define RETURN_ERROR(err) \
85     do { \
86         ERROR_PRINT(err); \
87         return err; \
88     } while(0)
89
90
91 #define RETURN_IF_NOT_HANDLE(handle) \
92         RETURN_VAL_IF(handle == NULL, SENSOR_ERROR_INVALID_PARAMETER)
93
94 #define RETURN_IF_NOT_TYPE(type) \
95         RETURN_VAL_IF(type > SENSOR_MOTION_FACEDOWN || type < 0, SENSOR_ERROR_INVALID_PARAMETER)
96
97 #define RETURN_IF_MOTION_TYPE(type) \
98         RETURN_VAL_IF(type > SENSOR_PROXIMITY && type <= SENSOR_MOTION_FACEDOWN, SENSOR_ERROR_INVALID_PARAMETER)
99
100 #define RETURN_IF_ERROR(val) \
101         RETURN_VAL_IF(val < 0, val)
102
103 sensor_data_accuracy_e _accu_table[] = {
104         SENSOR_ACCURACY_UNDEFINED,
105         SENSOR_ACCURACY_BAD,
106         SENSOR_ACCURACY_NORMAL,
107         SENSOR_ACCURACY_GOOD,
108         SENSOR_ACCURACY_VERYGOOD,
109 };
110
111 sensor_type_t _TYPE[] = {
112         ACCELEROMETER_SENSOR,
113         GEOMAGNETIC_SENSOR,
114         GEOMAGNETIC_SENSOR,
115         GYROSCOPE_SENSOR,
116         LIGHT_SENSOR,
117         PROXIMITY_SENSOR,
118         MOTION_SENSOR,
119         MOTION_SENSOR,
120         MOTION_SENSOR,
121         MOTION_SENSOR,
122         MOTION_SENSOR,
123 };
124
125 int _DTYPE[] = {
126         ACCELEROMETER_BASE_DATA_SET,
127         GEOMAGNETIC_RAW_DATA_SET, // really magnetic?
128         GEOMAGNETIC_BASE_DATA_SET, // really orientation?
129         GYRO_BASE_DATA_SET,
130         LIGHT_BASE_DATA_SET,
131         PROXIMITY_BASE_DATA_SET,
132 };
133
134 int _EVENT[] = {
135         ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME,
136         GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME, // really magnetic?
137         GEOMAGNETIC_EVENT_ATTITUDE_DATA_REPORT_ON_TIME, // really orientation?
138         GYROSCOPE_EVENT_RAW_DATA_REPORT_ON_TIME,
139         LIGHT_EVENT_LEVEL_DATA_REPORT_ON_TIME, // rate = 500ms
140     PROXIMITY_EVENT_CHANGE_STATE,
141         MOTION_ENGINE_EVENT_SNAP,
142         MOTION_ENGINE_EVENT_SHAKE,
143         MOTION_ENGINE_EVENT_DOUBLETAP,
144         MOTION_ENGINE_EVENT_PANNING,
145         MOTION_ENGINE_EVENT_TOP_TO_BOTTOM,
146 };
147
148 int _CALIBRATION[] = {
149         ACCELEROMETER_EVENT_CALIBRATION_NEEDED,
150         GEOMAGNETIC_EVENT_CALIBRATION_NEEDED,
151         GEOMAGNETIC_EVENT_CALIBRATION_NEEDED,
152 };
153
154 int _sensor_ids[] = {
155     ID_ACCELEOMETER,
156     ID_GEOMAGNETIC,
157     ID_GEOMAGNETIC,
158     ID_GYROSCOPE,
159     ID_LIGHT,
160     ID_PROXIMITY,
161     ID_MOTION,
162     ID_MOTION,
163     ID_MOTION,
164     ID_MOTION,
165     ID_MOTION
166 };
167
168 #define _SID(id) (_sensor_ids[id])
169 #define _ACCU(accuracy) (_accu_table[accuracy + 1])
170
171 static int _sensor_connect(sensor_h handle, sensor_type_e type)
172 {
173     int id = 0;
174     bool support = true;
175
176         RETURN_IF_NOT_TYPE(type);
177
178     if(handle->ids[_SID(type)] < 0){
179         sensor_is_supported(type, &support); 
180         if(!support)
181             return SENSOR_ERROR_NOT_SUPPORTED;
182
183         id = sf_connect(_TYPE[type]);
184
185         DEBUG_PRINTF("%s sensor connect legacy=[%d] type=[%d]", TYPE_NAME(type), type, _TYPE[type]);
186         if(id < 0){
187             return id == -2 ? SENSOR_ERROR_IO_ERROR : SENSOR_ERROR_OPERATION_FAILED;
188         }
189         DEBUG_PRINTF("%s sensor id created [%d]", TYPE_NAME(type), id);
190         handle->ids[_SID(type)] = id;
191     }
192     return SENSOR_ERROR_NONE;
193 }
194
195 static void _sensor_callback (unsigned int event_type, sensor_event_data_t* event, void* udata)
196 {
197         int i = 0;
198         int data_num = 0;
199         sensor_data_t *data = NULL;
200     sensor_panning_data_t *panning_data = NULL;
201         int motion = 0;
202     int nid = 0;
203     bool proximity = 0;
204
205         sensor_h sensor = (sensor_h)udata;
206
207         switch(event_type)
208         {
209                 case MOTION_ENGINE_EVENT_SNAP: 
210             nid = SENSOR_MOTION_SNAP;
211             break;
212                 case MOTION_ENGINE_EVENT_SHAKE:
213             nid = SENSOR_MOTION_SHAKE;
214             break;
215                 case MOTION_ENGINE_EVENT_DOUBLETAP:
216             nid = SENSOR_MOTION_DOUBLETAP;
217             break;
218                 case MOTION_ENGINE_EVENT_PANNING:
219             nid = SENSOR_MOTION_PANNING;
220             break;
221                 case MOTION_ENGINE_EVENT_TOP_TO_BOTTOM:
222             nid = SENSOR_MOTION_FACEDOWN;
223             break;
224                 case ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME :
225             nid = SENSOR_ACCELEROMETER;
226             break;
227                 case GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME :
228             nid = SENSOR_MAGNETIC;
229             break;
230                 case GEOMAGNETIC_EVENT_ATTITUDE_DATA_REPORT_ON_TIME :
231             nid = SENSOR_ORIENTATION;
232             break;
233                 case GYROSCOPE_EVENT_RAW_DATA_REPORT_ON_TIME :
234             nid = SENSOR_GYROSCOPE;
235             break;
236                 case LIGHT_EVENT_LEVEL_DATA_REPORT_ON_TIME :
237             nid = SENSOR_LIGHT;
238             break;
239                 case PROXIMITY_EVENT_CHANGE_STATE :
240             nid = SENSOR_PROXIMITY;
241             break;
242         }
243
244     if(sensor->cb_func[nid] == NULL || sensor->started[nid] == 0)
245         return;
246         
247         switch(event_type)
248         {
249                 case MOTION_ENGINE_EVENT_SNAP:
250                 case MOTION_ENGINE_EVENT_SHAKE:
251                         motion = *(int*)event->event_data;
252                         break;
253                 case MOTION_ENGINE_EVENT_PANNING:
254             panning_data = (sensor_panning_data_t *)event->event_data;
255             break;
256                 case MOTION_ENGINE_EVENT_DOUBLETAP:
257                         motion = *(int*)event->event_data;
258             if(motion != MOTION_ENGIEN_DOUBLTAP_DETECTION)
259                 return;
260             break;
261                 case MOTION_ENGINE_EVENT_TOP_TO_BOTTOM:
262                         motion = *(int*)event->event_data;
263             if(motion != MOTION_ENGIEN_TOP_TO_BOTTOM_DETECTION)
264                 return;
265             break;
266
267                 case ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME :
268                 case GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME :
269                 case GEOMAGNETIC_EVENT_ATTITUDE_DATA_REPORT_ON_TIME :
270                 case GYROSCOPE_EVENT_RAW_DATA_REPORT_ON_TIME :
271                 case LIGHT_EVENT_LEVEL_DATA_REPORT_ON_TIME :
272                         data = (sensor_data_t*)(event->event_data);
273                         data_num = (event->event_data_size)/sizeof(sensor_data_t);
274                         break;
275                 case PROXIMITY_EVENT_CHANGE_STATE :
276             proximity = *(int*)(event->event_data) == PROXIMITY_STATE_FAR ? 0 : 1;
277             break;
278                 default:
279                         DEBUG_PRINTF("unknown typed sensor happen!! event=%d\n", event_type);
280                         return;
281
282         }
283
284         switch(event_type)
285         {
286                 case MOTION_ENGINE_EVENT_SNAP:
287                         ((sensor_motion_snap_event_cb)sensor->cb_func[nid])(motion, sensor->cb_user_data[nid]);
288                         break;
289                 case MOTION_ENGINE_EVENT_SHAKE:
290                         ((sensor_motion_shake_event_cb)sensor->cb_func[nid])(motion, sensor->cb_user_data[nid]);
291                         break;
292                 case MOTION_ENGINE_EVENT_DOUBLETAP:
293                         ((sensor_motion_doubletap_event_cb)sensor->cb_func[nid])(sensor->cb_user_data[nid]);
294                         break;
295                 case MOTION_ENGINE_EVENT_TOP_TO_BOTTOM:
296                         ((sensor_motion_facedown_event_cb)sensor->cb_func[nid])(sensor->cb_user_data[nid]);
297                         break;
298                 case MOTION_ENGINE_EVENT_PANNING:
299                         ((sensor_motion_panning_event_cb)sensor->cb_func[nid])(panning_data->x, panning_data->y, 
300                 sensor->cb_user_data[nid]);
301             break;
302                 case ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME :
303                         for(i=0; i<data_num; i++){
304                                 ((sensor_accelerometer_event_cb)sensor->cb_func[nid])
305                                         (_ACCU(data[i].data_accuracy), 
306                                          data[i].values[0],  data[i].values[1], data[i].values[2], 
307                                          sensor->cb_user_data[nid]);
308                         }
309                         break;
310                 case GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME :
311                         for(i=0; i<data_num; i++){
312                                 ((sensor_magnetic_event_cb)sensor->cb_func[nid])
313                                         (_ACCU(data[i].data_accuracy), 
314                                          data[i].values[0],  data[i].values[1], data[i].values[2], 
315                                          sensor->cb_user_data[nid]);
316                         }
317                         break;
318                 case GEOMAGNETIC_EVENT_ATTITUDE_DATA_REPORT_ON_TIME :
319                         for(i=0; i<data_num; i++){
320                                 ((sensor_orientation_event_cb)sensor->cb_func[nid])
321                                         (_ACCU(data[i].data_accuracy), 
322                                          data[i].values[0],  data[i].values[1], data[i].values[2], 
323                                          sensor->cb_user_data[nid]);
324                         }
325                         break;
326                 case GYROSCOPE_EVENT_RAW_DATA_REPORT_ON_TIME :
327                         for(i=0; i<data_num; i++){
328                                 ((sensor_gyroscope_event_cb)sensor->cb_func[nid])
329                                         (_ACCU(data[i].data_accuracy), 
330                                          data[i].values[0],  data[i].values[1], data[i].values[2], 
331                                          sensor->cb_user_data[nid]);
332                         }
333                         break;
334                 case LIGHT_EVENT_LEVEL_DATA_REPORT_ON_TIME :
335                         for(i=0; i<data_num; i++){
336                                 ((sensor_light_event_cb)sensor->cb_func[nid])
337                                         (_ACCU(data[i].data_accuracy), 
338                                          (int)data[i].values[0], 
339                                          sensor->cb_user_data[nid]);
340                         }
341                         break;
342                 case PROXIMITY_EVENT_CHANGE_STATE :
343                                 ((sensor_proximity_event_cb)sensor->cb_func[nid])
344                                     (proximity, sensor->cb_user_data[nid]);
345                         break;
346         }
347 }
348
349 int sensor_is_supported(sensor_type_e type, bool* supported)
350 {
351     DEBUG_PRINT("sensor_is_support");
352
353         RETURN_IF_NOT_TYPE(type);
354
355     if(supported == NULL)
356         RETURN_ERROR(SENSOR_ERROR_INVALID_PARAMETER);
357
358     *supported = !(sf_is_sensor_event_available(_TYPE[type], _EVENT[type]) < 0);
359     DEBUG_PRINTF("%s sensor available function return [%d]", TYPE_NAME(type), *supported);
360
361     return SENSOR_ERROR_NONE;
362 }
363
364 int sensor_get_spec(sensor_type_e type, float* max, float* min, float* resolution)
365 {
366         sensor_properties_t property;
367     
368     DEBUG_PRINT("sensor_get_spec");
369
370     RETURN_IF_MOTION_TYPE(type); 
371
372         RETURN_IF_NOT_TYPE(type);
373
374         if(sf_get_properties(_TYPE[type], &property) < 0)
375         RETURN_ERROR(SENSOR_ERROR_NOT_SUPPORTED);
376
377         *max = property.sensor_max_range;
378         *min = property.sensor_min_range;
379         *resolution = property.sensor_resolution;
380
381         DEBUG_PRINTF("success get %s's format max=%f, min=%f, res=%f\n", TYPE_NAME(type), *max, *min, *resolution);
382
383         return SENSOR_ERROR_NONE;
384 }
385
386
387 int sensor_create(sensor_h* handle)
388 {
389         struct sensor_handle_s* sensor = NULL;
390
391     DEBUG_PRINT("sensor_create");
392
393     if(handle == NULL)
394         RETURN_ERROR(SENSOR_ERROR_INVALID_PARAMETER);
395
396         sensor = (struct sensor_handle_s*)malloc( sizeof(struct sensor_handle_s) );
397         if(sensor==NULL)
398         RETURN_ERROR(SENSOR_ERROR_OUT_OF_MEMORY);
399         else
400         {
401         SENSOR_INIT(sensor);
402
403                 *handle = (sensor_h)sensor;
404
405                 return SENSOR_ERROR_NONE;
406         }
407 }
408
409 int sensor_destroy(sensor_h handle)
410 {
411
412     int i=0;
413     bool failed = false;
414         RETURN_IF_NOT_HANDLE(handle);
415
416     DEBUG_PRINT("sensor_destroy");
417
418     for(i=0; i<ID_NUMBERS; i++){
419         if( handle->ids[i] >= 0 ){
420             if(sf_disconnect(handle->ids[i]) < 0)
421                 failed = true;
422             else
423                 handle->ids[i] = -1;
424         }
425     }
426
427     free(handle);
428     handle = NULL;
429
430     return SENSOR_ERROR_NONE;
431 }
432
433 int sensor_start(sensor_h handle, sensor_type_e type)
434 {
435     int err;
436     DEBUG_PRINT("sensor_start");
437         RETURN_IF_NOT_HANDLE(handle);
438     RETURN_IF_NOT_TYPE(type);
439
440     if( (err = _sensor_connect(handle, type)) != SENSOR_ERROR_NONE){
441         return err;
442     }
443
444         if (sf_start(handle->ids[_SID(type)], 0) < 0) {
445         RETURN_ERROR(SENSOR_ERROR_IO_ERROR);
446     } else {
447         handle->started[type] = 1;
448         return SENSOR_ERROR_NONE;
449     }
450 }
451
452 int sensor_stop(sensor_h handle, sensor_type_e type)
453 {
454     DEBUG_PRINT("sensor_stop");
455         RETURN_IF_NOT_HANDLE(handle);
456     RETURN_IF_NOT_TYPE(type);
457         if (sf_stop(handle->ids[_SID(type)]) < 0) {
458         RETURN_ERROR(SENSOR_ERROR_IO_ERROR);
459     } else {
460         handle->started[type] = 0;
461         return SENSOR_ERROR_NONE;
462     }
463 }
464
465 static void _sensor_calibration (unsigned int event_type, sensor_event_data_t* event, void* udata)
466 {
467         sensor_h sensor = (sensor_h)udata;
468
469         switch (event_type) {
470                 case ACCELEROMETER_EVENT_CALIBRATION_NEEDED:
471             if(sensor->calib_func[SENSOR_ACCELEROMETER] != NULL){
472                 ((sensor_calibration_cb)sensor->calib_func[SENSOR_ACCELEROMETER])(sensor->calib_user_data[SENSOR_ACCELEROMETER]);
473             }
474             break;
475                 case GEOMAGNETIC_EVENT_CALIBRATION_NEEDED:
476             if(sensor->calib_func[SENSOR_MAGNETIC] != NULL){
477                 ((sensor_calibration_cb)sensor->calib_func[SENSOR_MAGNETIC])(sensor->calib_user_data[SENSOR_MAGNETIC]);
478             }
479             if(sensor->calib_func[SENSOR_ORIENTATION] != NULL){
480                 ((sensor_calibration_cb)sensor->calib_func[SENSOR_ORIENTATION])(sensor->calib_user_data[SENSOR_ORIENTATION]);
481             }
482                         break;
483                 default:
484                         DEBUG_PRINTF("not calibration event happened in calibration callback!! event=%d", event_type);
485                         return;
486         }
487 }
488
489 static int _sensor_set_calibration_cb(sensor_h handle, sensor_type_e type, sensor_calibration_cb callback, void *user_data)
490 {
491         int ret, err;
492
493     DEBUG_PRINTF("%s sensor register calibration callback", TYPE_NAME(type));
494
495         RETURN_IF_NOT_HANDLE(handle);
496     switch(type){
497         case SENSOR_ACCELEROMETER:
498         case SENSOR_MAGNETIC:
499         case SENSOR_ORIENTATION:
500             break;
501         default:
502             RETURN_ERROR(SENSOR_ERROR_NOT_NEED_CALIBRATION);
503     }
504
505     ret = sf_is_sensor_event_available( _TYPE[type], _CALIBRATION[type] );
506     if (ret != 0 ){
507         DEBUG_PRINTF("Unsupported calibration ret=[%d] error=[%d] legacy=[%d] type=[%d] cal_id=[%d]", ret, SENSOR_ERROR_NOT_NEED_CALIBRATION, type, _TYPE[type], _CALIBRATION[type]);
508         RETURN_ERROR(SENSOR_ERROR_NOT_NEED_CALIBRATION);
509     }
510
511     if( (err = _sensor_connect(handle, type)) != SENSOR_ERROR_NONE){
512         return err;
513     }
514         
515         handle->calib_func[type] = callback;
516         handle->calib_user_data[type] = user_data;
517
518     DEBUG_PRINTF("type : %s / id : %d / event : %x ", TYPE_NAME(type), handle->ids[_SID(type)], _CALIBRATION[type]);
519
520         ret = sf_register_event(handle->ids[_SID(type)], _CALIBRATION[type], NULL, _sensor_calibration, handle);
521         if(ret < 0){
522                 handle->calib_func[type] = NULL;
523                 handle->calib_user_data[type] = NULL;
524         if(ret == -2)
525             RETURN_ERROR(SENSOR_ERROR_IO_ERROR);
526         else
527             RETURN_ERROR(SENSOR_ERROR_OPERATION_FAILED);
528         }
529
530     return SENSOR_ERROR_NONE;
531 }
532
533 static int _sensor_unset_calibration_cb(sensor_h handle, sensor_type_e type)
534 {
535         int ret;
536
537     DEBUG_PRINTF("%s sensor register calibration callback", TYPE_NAME(type));
538
539         RETURN_IF_NOT_HANDLE(handle);
540         switch (type) {
541                 case SENSOR_ACCELEROMETER:
542                 case SENSOR_MAGNETIC:
543                 case SENSOR_ORIENTATION:
544                         break;
545                 default:
546                         RETURN_ERROR(SENSOR_ERROR_NOT_NEED_CALIBRATION);
547         }
548
549     if(handle->calib_func[type] == NULL)
550         return SENSOR_ERROR_NONE;
551
552         ret = sf_unregister_event(handle->ids[_SID(type)], _CALIBRATION[type]);
553
554     if (ret < 0){
555         if(ret == -2)
556             RETURN_ERROR(SENSOR_ERROR_IO_ERROR);
557         else
558             RETURN_ERROR(SENSOR_ERROR_OPERATION_FAILED);
559     }
560
561     handle->calib_func[type] = NULL;
562     handle->calib_user_data[type] = NULL;
563
564     return SENSOR_ERROR_NONE;
565 }
566
567
568 static int _sensor_set_data_cb (sensor_h handle, sensor_type_e type, int rate, void* cb, void* user_data)
569 {
570     int err = 0;
571         event_condition_t condition;
572
573         RETURN_IF_NOT_HANDLE(handle);
574     RETURN_IF_NOT_TYPE(type);
575
576     DEBUG_PRINTF("sensor register callback %s", TYPE_NAME(type));
577
578     if(rate < 0){
579         RETURN_ERROR(SENSOR_ERROR_INVALID_PARAMETER);
580     }
581         
582         if(rate > 0){
583                 condition.cond_op = CONDITION_EQUAL;
584                 condition.cond_value1 = rate;
585         }
586
587         handle->cb_func[type] = cb; 
588         handle->cb_user_data[type] = user_data;
589
590     if( (err = _sensor_connect(handle, type)) != SENSOR_ERROR_NONE){
591         DEBUG_PRINTF("%s sensor connect error handle=[%d] legacy=[%d] err=[%d]", TYPE_NAME(type), handle, type, err);
592         return err;
593     }
594
595     err = sf_register_event(handle->ids[_SID(type)], _EVENT[type],
596                                 (rate > 0 ? &condition : NULL), _sensor_callback, handle);
597
598     DEBUG_PRINTF("%s sensor register function return [%d] event=[%d]", TYPE_NAME(type), err, _EVENT[type]);
599
600     if(err < 0){
601         handle->cb_func[type] = NULL;
602         handle->cb_user_data[type] = NULL;
603         if(err == -2)
604             RETURN_ERROR(SENSOR_ERROR_IO_ERROR);
605         else
606             RETURN_ERROR(SENSOR_ERROR_OPERATION_FAILED);
607     }
608
609     return SENSOR_ERROR_NONE;
610 }
611
612 static int _sensor_unset_data_cb (sensor_h handle, sensor_type_e type)
613 {
614     int error;
615     DEBUG_PRINTF("sensor unregister callback %s", TYPE_NAME(type));
616         RETURN_IF_NOT_HANDLE(handle);
617     if (handle->ids[_SID(type)] < 0 )
618         return SENSOR_ERROR_INVALID_PARAMETER;
619
620     error = sf_unregister_event(handle->ids[_SID(type)], _EVENT[type]);
621
622     if (error < 0){
623         if(error == -2)
624             RETURN_ERROR(SENSOR_ERROR_IO_ERROR);
625         else
626             RETURN_ERROR(SENSOR_ERROR_OPERATION_FAILED);
627     }
628
629     handle->cb_func[type] = NULL;
630     handle->cb_user_data[type] = NULL;
631     return SENSOR_ERROR_NONE;
632 }
633
634 int sensor_accelerometer_set_cb (sensor_h handle, 
635                 int rate, sensor_accelerometer_event_cb callback, void *user_data)
636 {
637         return _sensor_set_data_cb(handle, SENSOR_ACCELEROMETER, rate, (void*) callback, user_data);
638 }
639
640 int sensor_accelerometer_unset_cb              (sensor_h handle)
641 {
642     return _sensor_unset_data_cb(handle, SENSOR_ACCELEROMETER);
643 }
644
645 int sensor_magnetic_set_cb (sensor_h handle, 
646                 int rate, sensor_magnetic_event_cb callback, void *user_data)
647 {
648         return _sensor_set_data_cb(handle, SENSOR_MAGNETIC, rate, (void*) callback, user_data);
649 }
650
651 int sensor_magnetic_unset_cb                   (sensor_h handle)
652 {
653     return _sensor_unset_data_cb(handle, SENSOR_MAGNETIC);
654 }
655
656 int sensor_magnetic_set_calibration_cb         (sensor_h handle, sensor_calibration_cb callback, void *user_data)
657 {
658     return _sensor_set_calibration_cb(handle, SENSOR_MAGNETIC, callback, user_data);
659 }
660 int sensor_magnetic_unset_calibration_cb       (sensor_h handle)
661 {
662     return _sensor_unset_calibration_cb(handle, SENSOR_MAGNETIC);
663 }
664
665 int sensor_orientation_set_cb (sensor_h handle, 
666                 int rate, sensor_orientation_event_cb callback, void *user_data)
667 {
668         return _sensor_set_data_cb(handle, SENSOR_ORIENTATION, rate, (void*) callback, user_data);
669 }
670
671 int sensor_orientation_unset_cb                (sensor_h handle)
672 {
673     return _sensor_unset_data_cb(handle, SENSOR_ORIENTATION);
674 }
675 int sensor_orientation_set_calibration_cb      (sensor_h handle, sensor_calibration_cb callback, void *user_data)
676 {
677     return _sensor_set_calibration_cb(handle, SENSOR_ORIENTATION, callback, user_data);
678 }
679 int sensor_orientation_unset_calibration_cb    (sensor_h handle)
680 {
681     return _sensor_unset_calibration_cb(handle, SENSOR_ORIENTATION);
682 }
683
684 int sensor_gyroscope_set_cb (sensor_h handle, 
685                 int rate, sensor_gyroscope_event_cb callback, void *user_data)
686 {
687         return _sensor_set_data_cb(handle, SENSOR_GYROSCOPE, rate, (void*) callback, user_data);
688 }
689
690 int sensor_gyroscope_unset_cb                  (sensor_h handle)
691 {
692     return _sensor_unset_data_cb(handle, SENSOR_GYROSCOPE);
693 }
694
695 int sensor_light_set_cb (sensor_h handle, 
696                 int rate, sensor_light_event_cb callback, void *user_data)
697 {
698         return _sensor_set_data_cb(handle, SENSOR_LIGHT, rate, (void*) callback, user_data);
699 }
700
701 int sensor_light_unset_cb                      (sensor_h handle)
702 {
703     return _sensor_unset_data_cb(handle, SENSOR_LIGHT);
704 }
705
706 int sensor_proximity_set_cb (sensor_h handle, sensor_proximity_event_cb callback, void *user_data)
707 {
708         return _sensor_set_data_cb(handle, SENSOR_PROXIMITY, 0, (void*) callback, user_data);
709 }
710
711 int sensor_proximity_unset_cb                  (sensor_h handle)
712 {
713     return _sensor_unset_data_cb(handle, SENSOR_PROXIMITY);
714 }
715
716 static int _sensor_read_data(sensor_h handle, sensor_type_e type, 
717                 sensor_data_accuracy_e* accuracy, float* values, int values_size)
718 {
719     int err = 0;
720         sensor_data_t data;
721
722         RETURN_IF_NOT_HANDLE(handle);
723     if(type > SENSOR_PROXIMITY && type <= SENSOR_MOTION_DOUBLETAP)
724         RETURN_ERROR(SENSOR_ERROR_INVALID_PARAMETER);
725         RETURN_IF_NOT_TYPE(type);
726
727     DEBUG_PRINTF("sensor read data %s", TYPE_NAME(type));
728
729     if( (err = _sensor_connect(handle, type)) != SENSOR_ERROR_NONE)
730         return err;
731         if ( sf_get_data(handle->ids[_SID(type)], _DTYPE[type], &data) < 0 )
732     {
733         RETURN_ERROR(SENSOR_ERROR_IO_ERROR);
734     }
735
736         // this error will never happen. but it exist for more safe code.. 
737         if(values_size > 12 || values_size < 0)
738         RETURN_ERROR(SENSOR_ERROR_INVALID_PARAMETER);
739         
740     if(accuracy != NULL)
741         *accuracy = _ACCU(data.data_accuracy);
742         memcpy(values, data.values, values_size * sizeof(float));
743
744         return SENSOR_ERROR_NONE;
745 }
746
747 int sensor_accelerometer_read_data (sensor_h handle, 
748                 sensor_data_accuracy_e* accuracy, float* x, float* y, float* z)
749 {
750         float values[3] = {0,0,0};
751         int err = _sensor_read_data(handle, SENSOR_ACCELEROMETER, accuracy, values, 3);
752     if(err < 0) return err;
753
754     if(x == NULL || y == NULL || z == NULL)
755         RETURN_ERROR(SENSOR_ERROR_INVALID_PARAMETER);
756
757     *x = values[0];
758     *y = values[1];
759     *z = values[2];
760
761         return SENSOR_ERROR_NONE;
762 }
763
764 int sensor_magnetic_read_data      (sensor_h handle, sensor_data_accuracy_e* accuracy, float* x, float* y, float* z)
765 {
766         float values[3] = {0,0,0};
767         int err = _sensor_read_data(handle, SENSOR_MAGNETIC, accuracy, values, 3);
768     if(err < 0) return err;
769
770     if(x == NULL || y == NULL || z == NULL)
771         RETURN_ERROR(SENSOR_ERROR_INVALID_PARAMETER);
772
773     *x = values[0];
774     *y = values[1];
775     *z = values[2];
776
777         return SENSOR_ERROR_NONE;
778 }
779
780 int sensor_orientation_read_data   (sensor_h handle, sensor_data_accuracy_e* accuracy, float* azimuth, float* pitch, float* roll)
781 {
782         float values[3] = {0,0,0};
783         int err = _sensor_read_data(handle, SENSOR_ORIENTATION, accuracy, values, 3);
784     if(err < 0) return err;
785
786     if(azimuth == NULL || pitch == NULL || roll == NULL)
787         RETURN_ERROR(SENSOR_ERROR_INVALID_PARAMETER);
788
789     *azimuth = values[0];
790     *pitch = values[1];
791     *roll = values[2];
792
793         return SENSOR_ERROR_NONE;
794 }
795
796 int sensor_gyroscope_read_data     (sensor_h handle, sensor_data_accuracy_e* accuracy, float* x, float* y, float* z)
797 {
798         float values[3] = {0,0,0};
799         int err = _sensor_read_data(handle, SENSOR_GYROSCOPE, accuracy, values, 3);
800     if(err < 0) return err;
801
802     if(x == NULL || y == NULL || z == NULL)
803         RETURN_ERROR(SENSOR_ERROR_INVALID_PARAMETER);
804
805     *x = values[0];
806     *y = values[1];
807     *z = values[2];
808
809         return SENSOR_ERROR_NONE;
810 }
811
812 int sensor_light_read_data         (sensor_h handle, sensor_data_accuracy_e* accuracy, int* level)
813 {
814         float values[1] = {0};
815         int err = _sensor_read_data(handle, SENSOR_LIGHT, accuracy, values, 1);
816     if(err < 0) return err;
817
818     if(level == NULL)
819         RETURN_ERROR(SENSOR_ERROR_INVALID_PARAMETER);
820
821     *level = (int)values[0];
822
823         return SENSOR_ERROR_NONE;
824 }
825
826 int sensor_proximity_read_data     (sensor_h handle, sensor_data_accuracy_e* accuracy, bool* is_near)
827 {
828         float values[1] = {0};
829     int err = _sensor_read_data(handle, SENSOR_PROXIMITY, accuracy, values, 1);
830     if(err < 0) return err;
831
832     if(is_near == NULL)
833         RETURN_ERROR(SENSOR_ERROR_INVALID_PARAMETER);
834
835     *is_near = (bool)values[0];
836
837         return SENSOR_ERROR_NONE;
838 }
839
840
841 int sensor_motion_snap_set_cb    (sensor_h handle, sensor_motion_snap_event_cb callback, void *user_data)
842 {
843         return _sensor_set_data_cb(handle, SENSOR_MOTION_SNAP, 0, (void*) callback, user_data);
844 }
845
846 int sensor_motion_snap_unset_cb                (sensor_h handle)
847 {
848     return _sensor_unset_data_cb(handle, SENSOR_MOTION_SNAP);
849 }
850
851 int sensor_motion_shake_set_cb   (sensor_h handle, sensor_motion_shake_event_cb callback, void *user_data)
852 {
853         return _sensor_set_data_cb(handle, SENSOR_MOTION_SHAKE, 0, (void*) callback, user_data);
854 }
855
856 int sensor_motion_shake_unset_cb (sensor_h handle)
857 {
858     return _sensor_unset_data_cb(handle, SENSOR_MOTION_SHAKE);
859 }
860
861 int sensor_motion_doubletap_set_cb    (sensor_h handle, sensor_motion_doubletap_event_cb callback, void *user_data)
862 {
863         return _sensor_set_data_cb(handle, SENSOR_MOTION_DOUBLETAP, 0, (void*) callback, user_data);
864 }
865
866 int sensor_motion_doubletap_unset_cb (sensor_h handle)
867 {
868     return _sensor_unset_data_cb(handle, SENSOR_MOTION_DOUBLETAP);
869 }
870
871 int sensor_motion_panning_set_cb    (sensor_h handle, sensor_motion_panning_event_cb callback, void *user_data)
872 {
873         return _sensor_set_data_cb(handle, SENSOR_MOTION_PANNING, 0, (void*) callback, user_data);
874 }
875
876 int sensor_motion_panning_unset_cb (sensor_h handle)
877 {
878     return _sensor_unset_data_cb(handle, SENSOR_MOTION_PANNING);
879 }
880
881 int sensor_motion_facedown_set_cb    (sensor_h handle, sensor_motion_facedown_event_cb callback, void *user_data)
882 {
883         return _sensor_set_data_cb(handle, SENSOR_MOTION_FACEDOWN, 0, (void*) callback, user_data);
884 }
885
886 int sensor_motion_facedown_unset_cb (sensor_h handle)
887 {
888     return _sensor_unset_data_cb(handle, SENSOR_MOTION_FACEDOWN);
889 }