2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
31 #include <sensor_private.h>
38 #define LOG_TAG "TIZEN_SYSTEM_SENSOR"
41 static char* _DONT_USE_THIS_ARRAY_DIRECTLY[] = {
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"
62 #define TYPE_NAME(type) _DONT_USE_THIS_ARRAY_DIRECTLY[type]
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__)
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)
76 #define RETURN_VAL_IF(expr, err) \
84 #define RETURN_ERROR(err) \
91 #define RETURN_IF_NOT_HANDLE(handle) \
92 RETURN_VAL_IF(handle == NULL, SENSOR_ERROR_INVALID_PARAMETER)
94 #define RETURN_IF_NOT_TYPE(type) \
95 RETURN_VAL_IF(type > SENSOR_MOTION_FACEDOWN || type < 0, SENSOR_ERROR_INVALID_PARAMETER)
97 #define RETURN_IF_MOTION_TYPE(type) \
98 RETURN_VAL_IF(type > SENSOR_PROXIMITY && type <= SENSOR_MOTION_FACEDOWN, SENSOR_ERROR_INVALID_PARAMETER)
100 #define RETURN_IF_ERROR(val) \
101 RETURN_VAL_IF(val < 0, val)
103 sensor_data_accuracy_e _accu_table[] = {
104 SENSOR_ACCURACY_UNDEFINED,
106 SENSOR_ACCURACY_NORMAL,
107 SENSOR_ACCURACY_GOOD,
108 SENSOR_ACCURACY_VERYGOOD,
111 sensor_type_t _TYPE[] = {
112 ACCELEROMETER_SENSOR,
126 ACCELEROMETER_BASE_DATA_SET,
127 GEOMAGNETIC_RAW_DATA_SET, // really magnetic?
128 GEOMAGNETIC_BASE_DATA_SET, // really orientation?
131 PROXIMITY_BASE_DATA_SET,
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,
148 int _CALIBRATION[] = {
149 ACCELEROMETER_EVENT_CALIBRATION_NEEDED,
150 GEOMAGNETIC_EVENT_CALIBRATION_NEEDED,
151 GEOMAGNETIC_EVENT_CALIBRATION_NEEDED,
154 int _sensor_ids[] = {
168 #define _SID(id) (_sensor_ids[id])
169 #define _ACCU(accuracy) (_accu_table[accuracy + 1])
171 static int _sensor_connect(sensor_h handle, sensor_type_e type)
176 RETURN_IF_NOT_TYPE(type);
178 if(handle->ids[_SID(type)] < 0){
179 sensor_is_supported(type, &support);
181 return SENSOR_ERROR_NOT_SUPPORTED;
183 id = sf_connect(_TYPE[type]);
185 DEBUG_PRINTF("%s sensor connect legacy=[%d] type=[%d]", TYPE_NAME(type), type, _TYPE[type]);
187 return id == -2 ? SENSOR_ERROR_IO_ERROR : SENSOR_ERROR_OPERATION_FAILED;
189 DEBUG_PRINTF("%s sensor id created [%d]", TYPE_NAME(type), id);
190 handle->ids[_SID(type)] = id;
192 return SENSOR_ERROR_NONE;
195 static void _sensor_callback (unsigned int event_type, sensor_event_data_t* event, void* udata)
199 sensor_data_t *data = NULL;
200 sensor_panning_data_t *panning_data = NULL;
205 sensor_h sensor = (sensor_h)udata;
209 case MOTION_ENGINE_EVENT_SNAP:
210 nid = SENSOR_MOTION_SNAP;
212 case MOTION_ENGINE_EVENT_SHAKE:
213 nid = SENSOR_MOTION_SHAKE;
215 case MOTION_ENGINE_EVENT_DOUBLETAP:
216 nid = SENSOR_MOTION_DOUBLETAP;
218 case MOTION_ENGINE_EVENT_PANNING:
219 nid = SENSOR_MOTION_PANNING;
221 case MOTION_ENGINE_EVENT_TOP_TO_BOTTOM:
222 nid = SENSOR_MOTION_FACEDOWN;
224 case ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME :
225 nid = SENSOR_ACCELEROMETER;
227 case GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME :
228 nid = SENSOR_MAGNETIC;
230 case GEOMAGNETIC_EVENT_ATTITUDE_DATA_REPORT_ON_TIME :
231 nid = SENSOR_ORIENTATION;
233 case GYROSCOPE_EVENT_RAW_DATA_REPORT_ON_TIME :
234 nid = SENSOR_GYROSCOPE;
236 case LIGHT_EVENT_LEVEL_DATA_REPORT_ON_TIME :
239 case PROXIMITY_EVENT_CHANGE_STATE :
240 nid = SENSOR_PROXIMITY;
244 if(sensor->cb_func[nid] == NULL || sensor->started[nid] == 0)
249 case MOTION_ENGINE_EVENT_SNAP:
250 case MOTION_ENGINE_EVENT_SHAKE:
251 motion = *(int*)event->event_data;
253 case MOTION_ENGINE_EVENT_PANNING:
254 panning_data = (sensor_panning_data_t *)event->event_data;
256 case MOTION_ENGINE_EVENT_DOUBLETAP:
257 motion = *(int*)event->event_data;
258 if(motion != MOTION_ENGIEN_DOUBLTAP_DETECTION)
261 case MOTION_ENGINE_EVENT_TOP_TO_BOTTOM:
262 motion = *(int*)event->event_data;
263 if(motion != MOTION_ENGIEN_TOP_TO_BOTTOM_DETECTION)
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);
275 case PROXIMITY_EVENT_CHANGE_STATE :
276 proximity = *(int*)(event->event_data) == PROXIMITY_STATE_FAR ? 0 : 1;
279 DEBUG_PRINTF("unknown typed sensor happen!! event=%d\n", event_type);
286 case MOTION_ENGINE_EVENT_SNAP:
287 ((sensor_motion_snap_event_cb)sensor->cb_func[nid])(motion, sensor->cb_user_data[nid]);
289 case MOTION_ENGINE_EVENT_SHAKE:
290 ((sensor_motion_shake_event_cb)sensor->cb_func[nid])(motion, sensor->cb_user_data[nid]);
292 case MOTION_ENGINE_EVENT_DOUBLETAP:
293 ((sensor_motion_doubletap_event_cb)sensor->cb_func[nid])(sensor->cb_user_data[nid]);
295 case MOTION_ENGINE_EVENT_TOP_TO_BOTTOM:
296 ((sensor_motion_facedown_event_cb)sensor->cb_func[nid])(sensor->cb_user_data[nid]);
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]);
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]);
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]);
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]);
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]);
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]);
342 case PROXIMITY_EVENT_CHANGE_STATE :
343 ((sensor_proximity_event_cb)sensor->cb_func[nid])
344 (proximity, sensor->cb_user_data[nid]);
349 int sensor_is_supported(sensor_type_e type, bool* supported)
351 DEBUG_PRINT("sensor_is_support");
353 RETURN_IF_NOT_TYPE(type);
355 if(supported == NULL)
356 RETURN_ERROR(SENSOR_ERROR_INVALID_PARAMETER);
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);
361 return SENSOR_ERROR_NONE;
364 int sensor_get_spec(sensor_type_e type, float* max, float* min, float* resolution)
366 sensor_properties_t property;
368 DEBUG_PRINT("sensor_get_spec");
370 RETURN_IF_MOTION_TYPE(type);
372 RETURN_IF_NOT_TYPE(type);
374 if(sf_get_properties(_TYPE[type], &property) < 0)
375 RETURN_ERROR(SENSOR_ERROR_NOT_SUPPORTED);
377 *max = property.sensor_max_range;
378 *min = property.sensor_min_range;
379 *resolution = property.sensor_resolution;
381 DEBUG_PRINTF("success get %s's format max=%f, min=%f, res=%f\n", TYPE_NAME(type), *max, *min, *resolution);
383 return SENSOR_ERROR_NONE;
387 int sensor_create(sensor_h* handle)
389 struct sensor_handle_s* sensor = NULL;
391 DEBUG_PRINT("sensor_create");
394 RETURN_ERROR(SENSOR_ERROR_INVALID_PARAMETER);
396 sensor = (struct sensor_handle_s*)malloc( sizeof(struct sensor_handle_s) );
398 RETURN_ERROR(SENSOR_ERROR_OUT_OF_MEMORY);
403 *handle = (sensor_h)sensor;
405 return SENSOR_ERROR_NONE;
409 int sensor_destroy(sensor_h handle)
414 RETURN_IF_NOT_HANDLE(handle);
416 DEBUG_PRINT("sensor_destroy");
418 for(i=0; i<ID_NUMBERS; i++){
419 if( handle->ids[i] >= 0 ){
420 if(sf_disconnect(handle->ids[i]) < 0)
430 return SENSOR_ERROR_NONE;
433 int sensor_start(sensor_h handle, sensor_type_e type)
436 DEBUG_PRINT("sensor_start");
437 RETURN_IF_NOT_HANDLE(handle);
438 RETURN_IF_NOT_TYPE(type);
440 if( (err = _sensor_connect(handle, type)) != SENSOR_ERROR_NONE){
444 if (sf_start(handle->ids[_SID(type)], 0) < 0) {
445 RETURN_ERROR(SENSOR_ERROR_IO_ERROR);
447 handle->started[type] = 1;
448 return SENSOR_ERROR_NONE;
452 int sensor_stop(sensor_h handle, sensor_type_e type)
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);
460 handle->started[type] = 0;
461 return SENSOR_ERROR_NONE;
465 static void _sensor_calibration (unsigned int event_type, sensor_event_data_t* event, void* udata)
467 sensor_h sensor = (sensor_h)udata;
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]);
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]);
479 if(sensor->calib_func[SENSOR_ORIENTATION] != NULL){
480 ((sensor_calibration_cb)sensor->calib_func[SENSOR_ORIENTATION])(sensor->calib_user_data[SENSOR_ORIENTATION]);
484 DEBUG_PRINTF("not calibration event happened in calibration callback!! event=%d", event_type);
489 static int _sensor_set_calibration_cb(sensor_h handle, sensor_type_e type, sensor_calibration_cb callback, void *user_data)
493 DEBUG_PRINTF("%s sensor register calibration callback", TYPE_NAME(type));
495 RETURN_IF_NOT_HANDLE(handle);
497 case SENSOR_ACCELEROMETER:
498 case SENSOR_MAGNETIC:
499 case SENSOR_ORIENTATION:
502 RETURN_ERROR(SENSOR_ERROR_NOT_NEED_CALIBRATION);
505 ret = sf_is_sensor_event_available( _TYPE[type], _CALIBRATION[type] );
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);
511 if( (err = _sensor_connect(handle, type)) != SENSOR_ERROR_NONE){
515 handle->calib_func[type] = callback;
516 handle->calib_user_data[type] = user_data;
518 DEBUG_PRINTF("type : %s / id : %d / event : %x ", TYPE_NAME(type), handle->ids[_SID(type)], _CALIBRATION[type]);
520 ret = sf_register_event(handle->ids[_SID(type)], _CALIBRATION[type], NULL, _sensor_calibration, handle);
522 handle->calib_func[type] = NULL;
523 handle->calib_user_data[type] = NULL;
525 RETURN_ERROR(SENSOR_ERROR_IO_ERROR);
527 RETURN_ERROR(SENSOR_ERROR_OPERATION_FAILED);
530 return SENSOR_ERROR_NONE;
533 static int _sensor_unset_calibration_cb(sensor_h handle, sensor_type_e type)
537 DEBUG_PRINTF("%s sensor register calibration callback", TYPE_NAME(type));
539 RETURN_IF_NOT_HANDLE(handle);
541 case SENSOR_ACCELEROMETER:
542 case SENSOR_MAGNETIC:
543 case SENSOR_ORIENTATION:
546 RETURN_ERROR(SENSOR_ERROR_NOT_NEED_CALIBRATION);
549 if(handle->calib_func[type] == NULL)
550 return SENSOR_ERROR_NONE;
552 ret = sf_unregister_event(handle->ids[_SID(type)], _CALIBRATION[type]);
556 RETURN_ERROR(SENSOR_ERROR_IO_ERROR);
558 RETURN_ERROR(SENSOR_ERROR_OPERATION_FAILED);
561 handle->calib_func[type] = NULL;
562 handle->calib_user_data[type] = NULL;
564 return SENSOR_ERROR_NONE;
568 static int _sensor_set_data_cb (sensor_h handle, sensor_type_e type, int rate, void* cb, void* user_data)
571 event_condition_t condition;
573 RETURN_IF_NOT_HANDLE(handle);
574 RETURN_IF_NOT_TYPE(type);
576 DEBUG_PRINTF("sensor register callback %s", TYPE_NAME(type));
579 RETURN_ERROR(SENSOR_ERROR_INVALID_PARAMETER);
583 condition.cond_op = CONDITION_EQUAL;
584 condition.cond_value1 = rate;
587 handle->cb_func[type] = cb;
588 handle->cb_user_data[type] = user_data;
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);
595 err = sf_register_event(handle->ids[_SID(type)], _EVENT[type],
596 (rate > 0 ? &condition : NULL), _sensor_callback, handle);
598 DEBUG_PRINTF("%s sensor register function return [%d] event=[%d]", TYPE_NAME(type), err, _EVENT[type]);
601 handle->cb_func[type] = NULL;
602 handle->cb_user_data[type] = NULL;
604 RETURN_ERROR(SENSOR_ERROR_IO_ERROR);
606 RETURN_ERROR(SENSOR_ERROR_OPERATION_FAILED);
609 return SENSOR_ERROR_NONE;
612 static int _sensor_unset_data_cb (sensor_h handle, sensor_type_e type)
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;
620 error = sf_unregister_event(handle->ids[_SID(type)], _EVENT[type]);
624 RETURN_ERROR(SENSOR_ERROR_IO_ERROR);
626 RETURN_ERROR(SENSOR_ERROR_OPERATION_FAILED);
629 handle->cb_func[type] = NULL;
630 handle->cb_user_data[type] = NULL;
631 return SENSOR_ERROR_NONE;
634 int sensor_accelerometer_set_cb (sensor_h handle,
635 int rate, sensor_accelerometer_event_cb callback, void *user_data)
637 return _sensor_set_data_cb(handle, SENSOR_ACCELEROMETER, rate, (void*) callback, user_data);
640 int sensor_accelerometer_unset_cb (sensor_h handle)
642 return _sensor_unset_data_cb(handle, SENSOR_ACCELEROMETER);
645 int sensor_magnetic_set_cb (sensor_h handle,
646 int rate, sensor_magnetic_event_cb callback, void *user_data)
648 return _sensor_set_data_cb(handle, SENSOR_MAGNETIC, rate, (void*) callback, user_data);
651 int sensor_magnetic_unset_cb (sensor_h handle)
653 return _sensor_unset_data_cb(handle, SENSOR_MAGNETIC);
656 int sensor_magnetic_set_calibration_cb (sensor_h handle, sensor_calibration_cb callback, void *user_data)
658 return _sensor_set_calibration_cb(handle, SENSOR_MAGNETIC, callback, user_data);
660 int sensor_magnetic_unset_calibration_cb (sensor_h handle)
662 return _sensor_unset_calibration_cb(handle, SENSOR_MAGNETIC);
665 int sensor_orientation_set_cb (sensor_h handle,
666 int rate, sensor_orientation_event_cb callback, void *user_data)
668 return _sensor_set_data_cb(handle, SENSOR_ORIENTATION, rate, (void*) callback, user_data);
671 int sensor_orientation_unset_cb (sensor_h handle)
673 return _sensor_unset_data_cb(handle, SENSOR_ORIENTATION);
675 int sensor_orientation_set_calibration_cb (sensor_h handle, sensor_calibration_cb callback, void *user_data)
677 return _sensor_set_calibration_cb(handle, SENSOR_ORIENTATION, callback, user_data);
679 int sensor_orientation_unset_calibration_cb (sensor_h handle)
681 return _sensor_unset_calibration_cb(handle, SENSOR_ORIENTATION);
684 int sensor_gyroscope_set_cb (sensor_h handle,
685 int rate, sensor_gyroscope_event_cb callback, void *user_data)
687 return _sensor_set_data_cb(handle, SENSOR_GYROSCOPE, rate, (void*) callback, user_data);
690 int sensor_gyroscope_unset_cb (sensor_h handle)
692 return _sensor_unset_data_cb(handle, SENSOR_GYROSCOPE);
695 int sensor_light_set_cb (sensor_h handle,
696 int rate, sensor_light_event_cb callback, void *user_data)
698 return _sensor_set_data_cb(handle, SENSOR_LIGHT, rate, (void*) callback, user_data);
701 int sensor_light_unset_cb (sensor_h handle)
703 return _sensor_unset_data_cb(handle, SENSOR_LIGHT);
706 int sensor_proximity_set_cb (sensor_h handle, sensor_proximity_event_cb callback, void *user_data)
708 return _sensor_set_data_cb(handle, SENSOR_PROXIMITY, 0, (void*) callback, user_data);
711 int sensor_proximity_unset_cb (sensor_h handle)
713 return _sensor_unset_data_cb(handle, SENSOR_PROXIMITY);
716 static int _sensor_read_data(sensor_h handle, sensor_type_e type,
717 sensor_data_accuracy_e* accuracy, float* values, int values_size)
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);
727 DEBUG_PRINTF("sensor read data %s", TYPE_NAME(type));
729 if( (err = _sensor_connect(handle, type)) != SENSOR_ERROR_NONE)
731 if ( sf_get_data(handle->ids[_SID(type)], _DTYPE[type], &data) < 0 )
733 RETURN_ERROR(SENSOR_ERROR_IO_ERROR);
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);
741 *accuracy = _ACCU(data.data_accuracy);
742 memcpy(values, data.values, values_size * sizeof(float));
744 return SENSOR_ERROR_NONE;
747 int sensor_accelerometer_read_data (sensor_h handle,
748 sensor_data_accuracy_e* accuracy, float* x, float* y, float* z)
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;
754 if(x == NULL || y == NULL || z == NULL)
755 RETURN_ERROR(SENSOR_ERROR_INVALID_PARAMETER);
761 return SENSOR_ERROR_NONE;
764 int sensor_magnetic_read_data (sensor_h handle, sensor_data_accuracy_e* accuracy, float* x, float* y, float* z)
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;
770 if(x == NULL || y == NULL || z == NULL)
771 RETURN_ERROR(SENSOR_ERROR_INVALID_PARAMETER);
777 return SENSOR_ERROR_NONE;
780 int sensor_orientation_read_data (sensor_h handle, sensor_data_accuracy_e* accuracy, float* azimuth, float* pitch, float* roll)
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;
786 if(azimuth == NULL || pitch == NULL || roll == NULL)
787 RETURN_ERROR(SENSOR_ERROR_INVALID_PARAMETER);
789 *azimuth = values[0];
793 return SENSOR_ERROR_NONE;
796 int sensor_gyroscope_read_data (sensor_h handle, sensor_data_accuracy_e* accuracy, float* x, float* y, float* z)
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;
802 if(x == NULL || y == NULL || z == NULL)
803 RETURN_ERROR(SENSOR_ERROR_INVALID_PARAMETER);
809 return SENSOR_ERROR_NONE;
812 int sensor_light_read_data (sensor_h handle, sensor_data_accuracy_e* accuracy, int* level)
814 float values[1] = {0};
815 int err = _sensor_read_data(handle, SENSOR_LIGHT, accuracy, values, 1);
816 if(err < 0) return err;
819 RETURN_ERROR(SENSOR_ERROR_INVALID_PARAMETER);
821 *level = (int)values[0];
823 return SENSOR_ERROR_NONE;
826 int sensor_proximity_read_data (sensor_h handle, sensor_data_accuracy_e* accuracy, bool* is_near)
828 float values[1] = {0};
829 int err = _sensor_read_data(handle, SENSOR_PROXIMITY, accuracy, values, 1);
830 if(err < 0) return err;
833 RETURN_ERROR(SENSOR_ERROR_INVALID_PARAMETER);
835 *is_near = (bool)values[0];
837 return SENSOR_ERROR_NONE;
841 int sensor_motion_snap_set_cb (sensor_h handle, sensor_motion_snap_event_cb callback, void *user_data)
843 return _sensor_set_data_cb(handle, SENSOR_MOTION_SNAP, 0, (void*) callback, user_data);
846 int sensor_motion_snap_unset_cb (sensor_h handle)
848 return _sensor_unset_data_cb(handle, SENSOR_MOTION_SNAP);
851 int sensor_motion_shake_set_cb (sensor_h handle, sensor_motion_shake_event_cb callback, void *user_data)
853 return _sensor_set_data_cb(handle, SENSOR_MOTION_SHAKE, 0, (void*) callback, user_data);
856 int sensor_motion_shake_unset_cb (sensor_h handle)
858 return _sensor_unset_data_cb(handle, SENSOR_MOTION_SHAKE);
861 int sensor_motion_doubletap_set_cb (sensor_h handle, sensor_motion_doubletap_event_cb callback, void *user_data)
863 return _sensor_set_data_cb(handle, SENSOR_MOTION_DOUBLETAP, 0, (void*) callback, user_data);
866 int sensor_motion_doubletap_unset_cb (sensor_h handle)
868 return _sensor_unset_data_cb(handle, SENSOR_MOTION_DOUBLETAP);
871 int sensor_motion_panning_set_cb (sensor_h handle, sensor_motion_panning_event_cb callback, void *user_data)
873 return _sensor_set_data_cb(handle, SENSOR_MOTION_PANNING, 0, (void*) callback, user_data);
876 int sensor_motion_panning_unset_cb (sensor_h handle)
878 return _sensor_unset_data_cb(handle, SENSOR_MOTION_PANNING);
881 int sensor_motion_facedown_set_cb (sensor_h handle, sensor_motion_facedown_event_cb callback, void *user_data)
883 return _sensor_set_data_cb(handle, SENSOR_MOTION_FACEDOWN, 0, (void*) callback, user_data);
886 int sensor_motion_facedown_unset_cb (sensor_h handle)
888 return _sensor_unset_data_cb(handle, SENSOR_MOTION_FACEDOWN);