remove unnecessary return
[platform/core/api/motion.git] / src / SensorAdapter.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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 <time.h>
19 #include <sys/time.h>
20 #include "SensorAdapter.h"
21
22 #define SENSOR_EVENT(X) (((int)(X) << 16) | 0x01)
23
24 using namespace motion;
25
26 SensorAdapter::SensorAdapter(ISensorListener* listener) :
27         __sensorHandle(-1),
28         __sensorType(UNKNOWN_SENSOR),
29         __powerSave(true),
30         __listener(listener)
31 {
32 }
33
34 SensorAdapter::~SensorAdapter()
35 {
36         stop();
37 }
38
39 //LCOV_EXCL_START
40 void SensorAdapter::setSensor(sensor_type_t type)
41 {
42         __sensorType = type;
43 }
44
45 void SensorAdapter::setPowerSave(bool ps)
46 {
47         __powerSave = ps;
48
49         if (__sensorHandle < 0)
50                 return;
51
52         sensord_listener_set_attribute_int(__sensorHandle, SENSORD_ATTRIBUTE_PAUSE_POLICY,
53                         __powerSave ? SENSORD_PAUSE_ALL : SENSORD_PAUSE_NONE);
54 }
55
56 void SensorAdapter::setAttribute(int key, int value)
57 {
58         IF_FAIL_VOID_TAG(__sensorHandle >= 0, _W, "Sensor not started");
59         sensord_listener_set_attribute_int(__sensorHandle, key, value);
60 }
61
62 bool SensorAdapter::start()
63 {
64         int err;
65         sensor_t sensor;
66
67         err = sensord_get_default_sensor(__sensorType, &sensor);
68         IF_FAIL_RETURN_TAG(err == 0, false, _E, "Getting sensor failed (%d)", err);
69
70         __sensorHandle = sensord_connect(sensor);
71         IF_FAIL_RETURN_TAG(__sensorHandle >= 0, false, _E, "Connection failed");
72
73         if (!sensord_register_event(__sensorHandle, SENSOR_EVENT(__sensorType), 0, 0, __eventCb, this)) {
74                 _E("Event registration failed");
75                 if (!sensord_disconnect(__sensorHandle))
76                         _E("sensord_disconnect() Fail");
77                 __sensorHandle = -1;
78                 return false;
79         }
80
81         if (!sensord_start(__sensorHandle, __powerSave ? SENSOR_OPTION_DEFAULT : SENSOR_OPTION_ALWAYS_ON)) {
82                 _E("Starting failed");
83                 sensord_unregister_event(__sensorHandle, SENSOR_EVENT(__sensorType));
84                 if (!sensord_disconnect(__sensorHandle))
85                         _E("sensord_disconnect() Fail");
86                 __sensorHandle = -1;
87                 return false;
88         }
89
90         return true;
91 }
92
93 bool SensorAdapter::stop()
94 {
95         IF_FAIL_RETURN(__sensorHandle >= 0, false);
96
97         sensord_stop(__sensorHandle);
98         sensord_unregister_event(__sensorHandle, SENSOR_EVENT(__sensorType));
99         if (!sensord_disconnect(__sensorHandle))
100                 _E("sensord_disconnect() Fail");
101         __sensorHandle = -1;
102
103         return true;
104 }
105
106 bool SensorAdapter::isSupported(sensor_type_t type)
107 {
108         sensor_t sensor = sensord_get_sensor(type);
109         return (sensor != NULL);
110 }
111
112 double SensorAdapter::__getEpoch(unsigned long long monotonic)
113 {
114         struct timespec ts;
115         struct timeval tv;
116         double timeDiff;
117         double timestamp;
118
119         clock_gettime(CLOCK_MONOTONIC, &ts);
120         timeDiff = (ts.tv_sec * 1000000000.0 + ts.tv_nsec) / 1000000.0 - monotonic / 1000.0;
121
122         gettimeofday(&tv, NULL);
123         timestamp = tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0 - timeDiff;
124         return timestamp;
125 }
126
127 void SensorAdapter::__onEvent(sensor_data_t *eventData)
128 {
129         double timestamp = __getEpoch(eventData->timestamp);
130         __listener->onEvent(timestamp / 1000.0, eventData->values, eventData->accuracy);
131 }
132
133 void SensorAdapter::__eventCb(sensor_t sensor, unsigned int eventType, sensor_data_t *eventData, void *cbData)
134 {
135         SensorAdapter *instance = static_cast<SensorAdapter*>(cbData);
136         instance->__onEvent(eventData);
137 }
138 //LCOV_EXCL_STOP