Modify flora license version.
[platform/core/messaging/msg-service.git] / msg_helper / MsgSensorWrapper.cpp
1 /*
2 * Copyright 2012-2013  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.1 (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 #include "MsgDebug.h"
18 #include "MsgCppTypes.h"
19 #include "MsgGconfWrapper.h"
20 #include "MsgHelper.h"
21
22 #include <sensor.h>
23
24 /*==================================================================================================
25                                      VARIABLES
26 ==================================================================================================*/
27 int sensorHandler = -1;
28
29 msg_sensor_cb cbFunction = NULL;
30
31 /*==================================================================================================
32                                      FUNCTION IMPLEMENTATION
33 ==================================================================================================*/
34
35 void MsgSensorCB(unsigned int event_type, sensor_event_data_t *event_data , void *data)
36 {
37         int *my_event_data;
38
39         my_event_data = (int *)(event_data->event_data);
40
41         if (event_type == MOTION_ENGINE_EVENT_TOP_TO_BOTTOM)
42                 if(*my_event_data == MOTION_ENGIEN_TOP_TO_BOTTOM_DETECTION) {
43                         MSG_DEBUG("top to bottom event detected.");
44                         if(MsgSettingGetInt(VCONFKEY_SETAPPL_MOTION_ACTIVATION))
45                                 if(MsgSettingGetInt(VCONFKEY_SETAPPL_USE_TURN_OVER))
46                                         cbFunction();
47                 }
48 }
49
50
51 msg_error_t MsgSensorConnect()
52 {
53         sensorHandler = sf_connect(MOTION_SENSOR);
54         if (sensorHandler < 0) {
55                 MSG_DEBUG("sensor attach fail.");
56                 return MSG_ERR_UNKNOWN;
57         }
58
59         return MSG_SUCCESS;
60 }
61
62
63 void MsgSensorDisconnect()
64 {
65         if(cbFunction != NULL)
66                 cbFunction = NULL;
67
68         if (sensorHandler < 0)
69                 return;
70
71         try
72         {
73                 sf_stop(sensorHandler);
74         }
75         catch(int exception)
76         {
77                 MSG_FATAL("sf_stop error[%d]", exception);
78         }
79         sf_disconnect(sensorHandler);
80 }
81
82
83 msg_error_t MsgRegSensorCB(msg_sensor_cb cb)
84 {
85         int resultCondition = -1;
86
87         if (sensorHandler < 0) {
88                 MSG_DEBUG("Not connected to sensor FW.");
89                 return MSG_ERR_UNKNOWN;
90         }
91
92         if(cb != NULL) {
93                 // regist cb.
94                 cbFunction = cb;
95         } else {
96                 MSG_DEBUG("cb is NULL.");
97                 return MSG_ERR_UNKNOWN;
98         }
99
100         resultCondition = sf_register_event(sensorHandler, MOTION_ENGINE_EVENT_TOP_TO_BOTTOM , NULL , MsgSensorCB,NULL);
101         if (resultCondition < 0) {
102                 MSG_DEBUG("SLP_sensor_register_cb fail to gather data.");
103                 return MSG_ERR_UNKNOWN;
104         }
105
106         MSG_DEBUG("Start SF.");
107         resultCondition = sf_start(sensorHandler, 0);
108         if (resultCondition < 0) {
109                 MSG_DEBUG("SLP_sensor_start fail.");
110                 return MSG_ERR_UNKNOWN;
111         }
112
113         return MSG_SUCCESS;
114 }