2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "SensorProxy.h"
21 #define SENSOR_EVENT(X) (((int)(X) << 16) | 0x01)
25 SensorProxy::SensorProxy() :
27 sensorType(UNKNOWN_SENSOR),
35 SensorProxy::~SensorProxy()
40 void SensorProxy::setSensor(sensor_type_t type)
45 void SensorProxy::setPowerSave(bool ps)
50 void SensorProxy::setSamplingInterval(unsigned int interval)
52 samplingInterval = interval;
55 void SensorProxy::setBatchLatency(unsigned int latency)
57 batchLatency = latency;
60 void SensorProxy::setUserData(void *data)
65 bool SensorProxy::listen()
67 _D("#Sensor = %#x", sensorType);
69 sensor_t sensor = sensord_get_sensor(sensorType);
70 IF_FAIL_RETURN_TAG(sensor, false, _E, "Getting sensor failed");
72 sensorHandle = sensord_connect(sensor);
73 IF_FAIL_RETURN_TAG(sensorHandle >= 0, false, _E, "Connection failed");
75 if (!sensord_register_event(sensorHandle, SENSOR_EVENT(sensorType), samplingInterval, batchLatency, __eventCb, this)) {
76 _E("Event registration failed");
77 sensord_disconnect(sensorHandle);
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);
93 void SensorProxy::unlisten()
95 IF_FAIL_VOID(sensorHandle >= 0);
97 sensord_stop(sensorHandle);
98 sensord_unregister_event(sensorHandle, SENSOR_EVENT(sensorType));
99 sensord_disconnect(sensorHandle);
103 void SensorProxy::flush()
105 IF_FAIL_VOID(sensorHandle >= 0);
106 sensord_flush(sensorHandle);
109 bool SensorProxy::isRunning()
111 return sensorHandle >= 0;
114 bool SensorProxy::isSupported(sensor_type_t type)
116 sensor_t sensor = sensord_get_sensor(type);
117 return (sensor != NULL);
120 void SensorProxy::__eventCb(sensor_t sensor, unsigned int eventType, sensor_data_t *eventData, void *cbData)
122 SensorProxy *instance = static_cast<SensorProxy*>(cbData);
123 instance->onEvent(eventData);