Add sensor recorder provides (pedometer & pressure supported)
[platform/core/context/context-provider.git] / src / sensor / SensorProxy.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 <Types.h>
19 #include "SensorProxy.h"
20
21 #define SENSOR_EVENT(X) (((int)(X) << 16) | 0x01)
22
23 using namespace ctx;
24
25 SensorProxy::SensorProxy() :
26         sensorHandle(-1),
27         sensorType(UNKNOWN_SENSOR),
28         powerSave(true),
29         samplingInterval(0),
30         batchLatency(0),
31         userData(NULL)
32 {
33 }
34
35 SensorProxy::~SensorProxy()
36 {
37         stop();
38 }
39
40 void SensorProxy::setSensor(sensor_type_t type)
41 {
42         sensorType = type;
43 }
44
45 void SensorProxy::setPowerSave(bool ps)
46 {
47         powerSave = ps;
48 }
49
50 void SensorProxy::setSamplingInterval(unsigned int interval)
51 {
52         samplingInterval = interval;
53 }
54
55 void SensorProxy::setBatchLatency(unsigned int latency)
56 {
57         batchLatency = latency;
58 }
59
60 void SensorProxy::setUserData(void *data)
61 {
62         userData = data;
63 }
64
65 bool SensorProxy::start()
66 {
67         _D("#Sensor = %#x", sensorType);
68
69         sensor_t sensor = sensord_get_sensor(sensorType);
70         IF_FAIL_RETURN_TAG(sensor, false, _E, "Getting sensor failed");
71
72         sensorHandle = sensord_connect(sensor);
73         IF_FAIL_RETURN_TAG(sensorHandle >= 0, false, _E, "Connection failed");
74
75         if (!sensord_register_event(sensorHandle, SENSOR_EVENT(sensorType), samplingInterval, batchLatency, __eventCb, this)) {
76                 _E("Event registration failed");
77                 sensord_disconnect(sensorHandle);
78                 sensorHandle = -1;
79                 return false;
80         }
81
82         if (!sensord_start(sensorHandle, powerSave ? SENSOR_OPTION_DEFAULT : SENSOR_OPTION_ALWAYS_ON)) {
83                 _E("Starting failed");
84                 sensord_unregister_event(sensorHandle, SENSOR_EVENT(sensorType));
85                 sensord_disconnect(sensorHandle);
86                 sensorHandle = -1;
87                 return false;
88         }
89
90         return true;
91 }
92
93 void SensorProxy::stop()
94 {
95         IF_FAIL_VOID(sensorHandle >= 0);
96
97         sensord_stop(sensorHandle);
98         sensord_unregister_event(sensorHandle, SENSOR_EVENT(sensorType));
99         sensord_disconnect(sensorHandle);
100         sensorHandle = -1;
101 }
102
103 void SensorProxy::flush()
104 {
105         IF_FAIL_VOID(sensorHandle >= 0);
106         sensord_flush(sensorHandle);
107 }
108
109 bool SensorProxy::isRunning()
110 {
111         return sensorHandle >= 0;
112 }
113
114 bool SensorProxy::isSupported(sensor_type_t type)
115 {
116         sensor_t sensor = sensord_get_sensor(type);
117         return (sensor != NULL);
118 }
119
120 void SensorProxy::__eventCb(sensor_t sensor, unsigned int eventType, sensor_data_t *eventData, void *cbData)
121 {
122         SensorProxy *instance = static_cast<SensorProxy*>(cbData);
123         instance->onEvent(eventData);
124 }