Fix glib issue
[platform/core/messaging/msg-service.git] / externals / MsgSensorWrapper.cpp
1 /*
2  * Copyright (c) 2014 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 #include "MsgDebug.h"
19 #include "MsgCppTypes.h"
20 #include "MsgGconfWrapper.h"
21 #include "MsgSensorWrapper.h"
22 #include "MsgNotificationWrapper.h"
23
24 #ifndef MSG_WEARABLE_PROFILE
25 #include <gesture_recognition.h>
26
27 /*==================================================================================================
28                                      VARIABLES
29 ==================================================================================================*/
30
31 gesture_h gestureHandler = NULL;
32
33 msg_sensor_cb SensorCBFunction = NULL;
34
35 #endif /* MSG_WEARABLE_PROFILE */
36
37 /*==================================================================================================
38                                      FUNCTION IMPLEMENTATION
39 ==================================================================================================*/
40
41 #ifndef MSG_WEARABLE_PROFILE
42 void MsgGestureCB(gesture_type_e gesture, const gesture_data_h data, double timestamp, gesture_error_e error, void *user_data)
43 {
44         if (error != GESTURE_ERROR_NONE) {
45                 MSG_DEBUG("Gesture error = [%d]", error);
46                 return;
47         }
48
49         gesture_event_e event;
50         int ret = gesture_get_event(data, &event);
51         if (ret == GESTURE_ERROR_NONE && event == GESTURE_EVENT_DETECTED && \
52                         gesture == GESTURE_TURN_FACE_DOWN) {
53                 MSG_DEBUG("GESTURE_TURN_FACE_DOWN gesture detected.");
54                 int motion_activation = 0;
55                 int use_turn_over = 0;
56
57                 if (MsgSettingGetInt(VCONFKEY_SETAPPL_MOTION_ACTIVATION, &motion_activation) != MSG_SUCCESS) {
58                         MSG_INFO("MsgSettingGetInt() is failed");
59                 }
60
61                 if (MsgSettingGetInt(VCONFKEY_SETAPPL_USE_TURN_OVER, &use_turn_over) != MSG_SUCCESS) {
62                         MSG_INFO("MsgSettingGetInt() is failed");
63                 }
64
65                 if (motion_activation && use_turn_over) {
66                         if (SensorCBFunction)
67                                 SensorCBFunction();
68                 }
69         }
70 }
71
72 void MsgSensorCBStop()
73 {
74         MSG_BEGIN();
75
76
77         MsgDeleteNoti(MSG_NOTI_TYPE_ALL, -1);
78         MsgRefreshNotification(MSG_NOTI_TYPE_ALL, false, MSG_ACTIVE_NOTI_TYPE_NONE);
79 #ifndef MSG_NOTI_INTEGRATION
80         MsgRefreshNotification(MSG_NOTI_TYPE_SIM, false, MSG_ACTIVE_NOTI_TYPE_NONE);
81 #endif
82
83         MSG_END();
84 }
85 #endif /* MSG_WEARABLE_PROFILE */
86
87 void MsgInitSensor()
88 {
89 #ifndef MSG_WEARABLE_PROFILE
90         if (MsgSensorConnect() == MSG_SUCCESS) {
91                 if (MsgRegSensorCB(&MsgSensorCBStop) != MSG_SUCCESS) {
92                         MSG_DEBUG("Fail to MsgRegSensorCB.");
93                         MsgSensorDisconnect();
94                 }
95         } else {
96                 MSG_DEBUG("Fail to MsgSensorConnect.");
97         }
98 #endif /* MSG_WEARABLE_PROFILE */
99 }
100
101
102 msg_error_t MsgSensorConnect()
103 {
104 #ifndef MSG_WEARABLE_PROFILE
105         bool supported = false;
106         int ret = gesture_is_supported(GESTURE_TURN_FACE_DOWN, &supported);
107         if (ret != GESTURE_ERROR_NONE) {
108                 MSG_DEBUG("gesture_is_supported is failed [%d]", ret);
109                 return MSG_ERR_UNKNOWN;
110         }
111         if (!supported) {
112                 MSG_DEBUG("GESTURE_TURN_FACE_DOWN not supported in the current device.");
113                 return MSG_ERR_UNKNOWN;
114         }
115
116         ret = gesture_create(&gestureHandler);
117         if (ret != GESTURE_ERROR_NONE) {
118                 MSG_DEBUG("gesture_create is failed [%d]", ret);
119                 return MSG_ERR_UNKNOWN;
120         }
121 #endif /* MSG_WEARABLE_PROFILE */
122
123         return MSG_SUCCESS;
124 }
125
126
127 void MsgSensorDisconnect()
128 {
129 #ifndef MSG_WEARABLE_PROFILE
130         if (SensorCBFunction != NULL)
131                 SensorCBFunction = NULL;
132
133         if (gestureHandler == NULL)
134                 return;
135
136         try {
137                 if(gesture_stop_recognition(gestureHandler)!= GESTURE_ERROR_NONE)
138                         MSG_DEBUG("gesture_stop_recognition failed");
139         } catch(int exception) {
140                 MSG_FATAL("gesture_stop_recognition error [%d]", exception);
141         }
142
143         gesture_release(gestureHandler);
144         gestureHandler = NULL;
145 #endif /* MSG_WEARABLE_PROFILE */
146 }
147
148
149 msg_error_t MsgRegSensorCB(msg_sensor_cb cb)
150 {
151 #ifndef MSG_WEARABLE_PROFILE
152         if (gestureHandler == NULL) {
153                 MSG_DEBUG("Not connected to gesture FW.");
154                 return MSG_ERR_UNKNOWN;
155         }
156
157         if (cb != NULL) {
158                 /* regist cb. */
159                 SensorCBFunction = cb;
160         } else {
161                 MSG_DEBUG("cb is NULL.");
162                 return MSG_ERR_UNKNOWN;
163         }
164
165         int ret = gesture_start_recognition(gestureHandler, GESTURE_TURN_FACE_DOWN, \
166                                 GESTURE_OPTION_DEFAULT, MsgGestureCB, NULL);
167         if (ret != GESTURE_ERROR_NONE) {
168                 MSG_DEBUG("gesture_start_recognition failed");
169                 return MSG_ERR_UNKNOWN;
170         }
171 #endif /* MSG_WEARABLE_PROFILE */
172
173         return MSG_SUCCESS;
174 }