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.
20 #include "SensorAdapter.h"
22 #define SENSOR_EVENT(X) (((int)(X) << 16) | 0x01)
24 using namespace motion;
26 SensorAdapter::SensorAdapter(ISensorListener* listener) :
28 __sensorType(UNKNOWN_SENSOR),
34 SensorAdapter::~SensorAdapter()
40 void SensorAdapter::setSensor(sensor_type_t type)
45 void SensorAdapter::setPowerSave(bool ps)
49 if (__sensorHandle < 0)
52 sensord_listener_set_attribute_int(__sensorHandle, SENSORD_ATTRIBUTE_PAUSE_POLICY,
53 __powerSave ? SENSORD_PAUSE_ALL : SENSORD_PAUSE_NONE);
56 void SensorAdapter::setAttribute(int key, int value)
58 IF_FAIL_VOID_TAG(__sensorHandle >= 0, _W, "Sensor not started");
59 sensord_listener_set_attribute_int(__sensorHandle, key, value);
62 bool SensorAdapter::start()
67 err = sensord_get_default_sensor(__sensorType, &sensor);
68 IF_FAIL_RETURN_TAG(err == 0, false, _E, "Getting sensor failed (%d)", err);
70 __sensorHandle = sensord_connect(sensor);
71 IF_FAIL_RETURN_TAG(__sensorHandle >= 0, false, _E, "Connection failed");
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");
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");
93 bool SensorAdapter::stop()
95 IF_FAIL_RETURN(__sensorHandle >= 0, false);
97 sensord_stop(__sensorHandle);
98 sensord_unregister_event(__sensorHandle, SENSOR_EVENT(__sensorType));
99 if (!sensord_disconnect(__sensorHandle))
100 _E("sensord_disconnect() Fail");
106 bool SensorAdapter::isSupported(sensor_type_t type)
108 sensor_t sensor = sensord_get_sensor(type);
109 return (sensor != NULL);
112 double SensorAdapter::__getEpoch(unsigned long long monotonic)
119 clock_gettime(CLOCK_MONOTONIC, &ts);
120 timeDiff = (ts.tv_sec * 1000000000.0 + ts.tv_nsec) / 1000000.0 - monotonic / 1000.0;
122 gettimeofday(&tv, NULL);
123 timestamp = tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0 - timeDiff;
127 void SensorAdapter::__onEvent(sensor_data_t *eventData)
129 double timestamp = __getEpoch(eventData->timestamp);
130 __listener->onEvent(timestamp / 1000.0, eventData->values, eventData->accuracy);
133 void SensorAdapter::__eventCb(sensor_t sensor, unsigned int eventType, sensor_data_t *eventData, void *cbData)
135 SensorAdapter *instance = static_cast<SensorAdapter*>(cbData);
136 instance->__onEvent(eventData);