Tizen 2.0 Release
[apps/core/preloaded/myfiles.git] / src / common / mf-sensor.c
1 /*
2  * Copyright 2013         Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 #include "mf-sensor.h"
19 #include "mf-dlog.h"
20 #include <sensors.h>
21
22 static sensor_h g_sensor;
23
24 int mf_sensor_init(void)
25 {
26         int res = SENSOR_ERROR_NONE;
27         bool doubletap = 0;
28
29         res = sensor_is_supported(SENSOR_MOTION_DOUBLETAP, &doubletap);
30         MF_CHECK_VAL(res == SENSOR_ERROR_NONE, res);
31
32         if(!doubletap)
33                 mf_error("sensor not supported. doubletap: %d", doubletap);
34
35         if(doubletap)
36         {
37                 res = sensor_create(&g_sensor);
38                 if(res != SENSOR_ERROR_NONE)
39                         mf_error("Fail sensor_create");
40         }
41         else
42                 mf_error("motion not supported");
43
44         return res;
45 }
46
47 int mf_sensor_finalize(void)
48 {
49         int res = SENSOR_ERROR_NONE;
50         MF_CHECK_VAL(g_sensor, -1);
51         res = sensor_destroy(g_sensor);
52         g_sensor = NULL;
53
54         return res;
55 }
56
57 int mf_sensor_start(mf_motion_type type)
58 {
59         MF_CHECK_VAL(g_sensor, -1);
60
61         int res = SENSOR_ERROR_NONE;
62         switch(type)
63         {
64         case MF_MOTION_FACEDOWN:
65                 res = sensor_start(g_sensor, SENSOR_MOTION_FACEDOWN);
66                 break;
67         case MF_MOTION_DOUBLETAP:
68                 res = sensor_start(g_sensor, SENSOR_MOTION_DOUBLETAP);
69                 break;
70         default:
71                 mf_error("Invalid motion type: 0x%x", type);
72         }
73
74         if(res != SENSOR_ERROR_NONE)
75                 mf_error("Fail sensor_start: type: %d ", type);
76
77         mf_debug("sensor started");
78         return res;
79 }
80
81 int mf_sensor_stop(mf_motion_type type)
82 {
83         MF_CHECK_VAL(g_sensor, -1);
84
85         int res = SENSOR_ERROR_NONE;
86         switch(type)
87         {
88         case MF_MOTION_FACEDOWN:
89                 res = sensor_stop(g_sensor, SENSOR_MOTION_FACEDOWN);
90                 break;
91         case MF_MOTION_DOUBLETAP:
92                 res = sensor_stop(g_sensor, SENSOR_MOTION_DOUBLETAP);
93                 break;
94         default:
95                 mf_error("Invalid motion type: 0x%x", type);
96         }
97
98         if(res != SENSOR_ERROR_NONE)
99                 mf_error("Fail sensor_stop: type: %d ", type);
100
101         mf_debug("sensor stopped");
102         return res;
103 }
104
105 int mf_sensor_set_callback(mf_motion_type type, mf_sensor_cb callback, void *user_data)
106 {
107         MF_CHECK_VAL(g_sensor, -1);
108
109         int res = SENSOR_ERROR_NONE;
110         switch(type)
111         {
112         case MF_MOTION_FACEDOWN:
113                 res = sensor_motion_facedown_set_cb(g_sensor, callback, user_data);
114                 break;
115         case MF_MOTION_DOUBLETAP:
116                 res = sensor_motion_doubletap_set_cb(g_sensor, callback, user_data);
117                 break;
118         default:
119                 mf_error("Invalid motion type: 0x%x", type);
120                 break;
121         }
122         if(res != SENSOR_ERROR_NONE)
123                 mf_error("Error: set sensor callback res = 0x%x");
124         return res;
125 }
126
127 int mf_sensor_unset_callback(mf_motion_type type)
128 {
129         MF_CHECK_VAL(g_sensor, -1);
130
131         int res = SENSOR_ERROR_NONE;
132         switch(type)
133         {
134         case MF_MOTION_FACEDOWN:
135                 res = sensor_motion_facedown_unset_cb(g_sensor);
136                 break;
137         case MF_MOTION_DOUBLETAP:
138                 res = sensor_motion_doubletap_unset_cb(g_sensor);
139                 break;
140         default:
141                 mf_error("Invalid motion type: 0x%x", type);
142                 break;
143         }
144         if(res != SENSOR_ERROR_NONE)
145                 mf_error("Error: set sensor callback res = 0x%x");
146         return res;
147 }
148