Refactor to use a single listener instance for all activities 31/83131/1
authorMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 9 Aug 2016 04:30:44 +0000 (13:30 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 9 Aug 2016 08:23:01 +0000 (17:23 +0900)
Change-Id: If6543085a902afe619469024c2d6d6e9498a326e
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
src/Activity.cpp
src/ActivityProxy.cpp [deleted file]
src/ActivitySensor.cpp [new file with mode: 0644]
src/ActivitySensor.h [moved from src/ActivityProxy.h with 64% similarity]
src/Gesture.cpp
src/GestureSensor.cpp [moved from src/GestureProxy.cpp with 55% similarity]
src/GestureSensor.h [moved from src/GestureProxy.h with 67% similarity]
src/ISensorListener.h [new file with mode: 0644]
src/SensorAdapter.cpp [new file with mode: 0644]
src/SensorAdapter.h [moved from src/SensorProxy.h with 64% similarity]
src/SensorProxy.cpp [deleted file]

index 9f744d3..a7cf667 100644 (file)
@@ -16,7 +16,7 @@
  */
 
 #include <stdlib.h>
-#include "ActivityProxy.h"
+#include "ActivitySensor.h"
 
 #define FEATURE_ACTIVITY "tizen.org/feature/sensor.activity_recognition"
 
@@ -27,7 +27,7 @@
 using namespace motion;
 
 struct _activity_handle_s {
-       ActivityProxy *proxy;
+       ActivitySensor *sensor;
 };
 
 EXTAPI int activity_is_supported(activity_type_e activity, bool* supported)
@@ -39,7 +39,7 @@ EXTAPI int activity_is_supported(activity_type_e activity, bool* supported)
        ASSERT_NOT_NULL(supported);
        IF_FAIL_RETURN(IS_VALID_ACTIVITY(activity), ERR_INVALID_PARAMETER);
 
-       *supported = ActivityProxy::isSupported(activity);
+       *supported = ActivitySensor::isSupported(activity);
        return ERR_NONE;
 }
 
@@ -51,8 +51,8 @@ EXTAPI int activity_create(activity_h *handle)
        _activity_handle_s *hdl = static_cast<activity_h>(malloc(sizeof(_activity_handle_s)));
        IF_FAIL_RETURN_TAG(hdl, ERR_OPERATION_FAILED, _E, "Memory allocation failed");
 
-       hdl->proxy = new(std::nothrow) ActivityProxy();
-       if (hdl->proxy == NULL) {
+       hdl->sensor = new(std::nothrow) ActivitySensor();
+       if (hdl->sensor == NULL) {
                _E("Memory allocation failed");
                free(hdl);
                return ERR_OPERATION_FAILED;
@@ -67,7 +67,7 @@ EXTAPI int activity_release(activity_h handle)
        ASSERT_SUPPORTED(FEATURE_ACTIVITY);
        ASSERT_NOT_NULL(handle);
 
-       delete handle->proxy;
+       delete handle->sensor;
        free(handle);
 
        return ERR_NONE;
@@ -80,15 +80,14 @@ EXTAPI int activity_start_recognition(activity_h handle, activity_type_e activit
        ASSERT_NOT_NULL(callback);
        IF_FAIL_RETURN(IS_VALID_ACTIVITY(activity), ERR_INVALID_PARAMETER);
 
-       if (!handle->proxy->setActivity(activity)) {
+       if (!handle->sensor->setActivity(activity)) {
                return ERR_INVALID_PARAMETER;
        }
 
-       handle->proxy->setPowerSave(false);
-       handle->proxy->setCb(callback);
-       handle->proxy->setUserData(user_data);
+       handle->sensor->setCallback(callback);
+       handle->sensor->setUserData(user_data);
 
-       if (!handle->proxy->start()) {
+       if (!handle->sensor->start()) {
                return ERR_OPERATION_FAILED;
        }
 
@@ -100,7 +99,7 @@ EXTAPI int activity_stop_recognition(activity_h handle)
        ASSERT_SUPPORTED(FEATURE_ACTIVITY);
        ASSERT_NOT_NULL(handle);
 
-       IF_FAIL_RETURN(handle->proxy->stop(), ACTIVITY_ERROR_NOT_STARTED);
+       IF_FAIL_RETURN(handle->sensor->stop(), ACTIVITY_ERROR_NOT_STARTED);
 
        return ERR_NONE;
 }
diff --git a/src/ActivityProxy.cpp b/src/ActivityProxy.cpp
deleted file mode 100644 (file)
index fa8275d..0000000
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-#include <math.h>
-#include "ActivityProxy.h"
-
-#define ACTIVITY_BIT(X) (0x1 << (int)(X))
-#define ACTIVITY_ENUM(X) ((int)log2f((X)))
-
-using namespace motion;
-
-ActivityProxy::ActivityProxy()
-       : __activityType(static_cast<activity_type_e>(UNDEFINED))
-       , __callback(NULL)
-{
-}
-
-ActivityProxy::~ActivityProxy()
-{
-}
-
-bool ActivityProxy::setActivity(activity_type_e type)
-{
-       __activityType = type;
-       setSensor(ACTIVITY_TRACKER_SENSOR);
-       return true;
-}
-
-void ActivityProxy::setCb(activity_recognition_cb cb)
-{
-       __callback = cb;
-}
-
-bool ActivityProxy::start()
-{
-       IF_FAIL_RETURN(SensorProxy::start(), false);
-       sensord_set_attribute_int(sensorHandle, SENSOR_ATTR_ACTIVITY, ACTIVITY_BIT(__activityType));
-       return true;
-}
-
-bool ActivityProxy::isSupported(activity_type_e type)
-{
-       return SensorProxy::isSupported(ACTIVITY_TRACKER_SENSOR);
-}
-
-void ActivityProxy::onEvent(sensor_data_t *eventData)
-{
-       _activity_data_s data;
-
-       IF_FAIL_VOID(ACTIVITY_ENUM(eventData->values[0]) == static_cast<int>(__activityType));
-
-       data.activity = __activityType;
-       data.accuracy = eventData->accuracy;
-
-       __callback(__activityType, &data, getTime(eventData->timestamp),
-                       static_cast<activity_error_e>(ERR_NONE), userData);
-}
diff --git a/src/ActivitySensor.cpp b/src/ActivitySensor.cpp
new file mode 100644 (file)
index 0000000..d275d5d
--- /dev/null
@@ -0,0 +1,142 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <cmath>
+#include "ActivitySensor.h"
+
+#define ACTIVITY_BIT(X) (0x1 << (int)(X))
+#define ACTIVITY_ENUM(X) ((int)log2f((X)))
+
+using namespace motion;
+
+class ActivityListener : public ISensorListener {
+public:
+       ActivityListener()
+       {
+       }
+
+       ~ActivityListener()
+       {
+       }
+
+       void onEvent(double timestamp, float* values, int accuracy)
+       {
+               ActivitySensor::onEvent(timestamp, values, accuracy);
+       }
+};
+
+static ActivityListener __activityListener;
+
+std::set<ActivitySensor*> ActivitySensor::__sensorSet;
+SensorAdapter ActivitySensor::__sensorAdapter(&__activityListener);
+
+ActivitySensor::ActivitySensor() :
+       __activityType(static_cast<activity_type_e>(UNDEFINED)),
+       __callback(NULL),
+       __userData(NULL),
+       __started(false)
+{
+       __sensorAdapter.setSensor(ACTIVITY_TRACKER_SENSOR);
+       __sensorAdapter.setPowerSave(false);
+       __sensorSet.insert(this);
+}
+
+ActivitySensor::~ActivitySensor()
+{
+       stop();
+       __sensorSet.erase(this);
+}
+
+bool ActivitySensor::setActivity(activity_type_e type)
+{
+       __activityType = type;
+       return true;
+}
+
+void ActivitySensor::setCallback(activity_recognition_cb cb)
+{
+       __callback = cb;
+}
+
+void ActivitySensor::setUserData(void* data)
+{
+       __userData = data;
+}
+
+bool ActivitySensor::start()
+{
+       bool started = false;
+       int activityFilter = 0;
+
+       for (std::set<ActivitySensor*>::iterator iter = __sensorSet.begin(); iter != __sensorSet.end(); ++iter) {
+               started = started | (*iter)->__started;
+               if ((*iter)->__started || (*iter) == this)
+                       activityFilter |= ACTIVITY_BIT((*iter)->__activityType);
+       }
+
+       if (!started)
+               IF_FAIL_RETURN(__sensorAdapter.start(), false);
+
+       __sensorAdapter.setAttribute(SENSOR_ATTR_ACTIVITY, activityFilter);
+
+       __started = true;
+       return true;
+}
+
+bool ActivitySensor::stop()
+{
+       IF_FAIL_RETURN(__started, false);
+
+       bool started = false;
+       int activityFilter = 0;
+
+       __started = false;
+
+       for (std::set<ActivitySensor*>::iterator iter = __sensorSet.begin(); iter != __sensorSet.end(); ++iter) {
+               started = started | (*iter)->__started;
+               if ((*iter)->__started)
+                       activityFilter |= ACTIVITY_BIT((*iter)->__activityType);
+       }
+
+       if (started)
+               __sensorAdapter.setAttribute(SENSOR_ATTR_ACTIVITY, activityFilter);
+       else
+               __sensorAdapter.stop();
+
+       return true;
+}
+
+bool ActivitySensor::isSupported(activity_type_e type)
+{
+       return SensorAdapter::isSupported(ACTIVITY_TRACKER_SENSOR);
+}
+
+void ActivitySensor::onEvent(double timestamp, float* values, int accuracy)
+{
+       _activity_data_s data;
+       data.activity = ACTIVITY_ENUM(values[0]);
+       data.accuracy = accuracy;
+
+       for (std::set<ActivitySensor*>::iterator iter = __sensorSet.begin(); iter != __sensorSet.end(); ++iter) {
+               if (!(*iter)->__started)
+                       continue;
+               if (static_cast<int>((*iter)->__activityType) == data.activity && (*iter)->__callback) {
+                       activity_recognition_cb cb = (*iter)->__callback;
+                       cb((*iter)->__activityType, &data, timestamp, ACTIVITY_ERROR_NONE, (*iter)->__userData);
+               }
+       }
+}
similarity index 64%
rename from src/ActivityProxy.h
rename to src/ActivitySensor.h
index 402bd27..44f1ec6 100644 (file)
  *
  */
 
-#ifndef _MOTION_ACTIVITY_PROXY_H_
-#define _MOTION_ACTIVITY_PROXY_H_
+#ifndef __MOTION_ACTIVITY_SENSOR_H__
+#define __MOTION_ACTIVITY_SENSOR_H__
 
+#include <set>
 #include <activity_recognition.h>
-#include "SensorProxy.h"
+#include "SensorAdapter.h"
 
 struct _activity_data_s {
        int activity;
@@ -28,26 +29,31 @@ struct _activity_data_s {
 
 namespace motion {
 
-       class ActivityProxy : public SensorProxy {
+       class ActivitySensor {
        public:
-               ActivityProxy();
-               ~ActivityProxy();
+               ActivitySensor();
+               ~ActivitySensor();
 
                bool setActivity(activity_type_e type);
-               void setCb(activity_recognition_cb cb);
+               void setCallback(activity_recognition_cb cb);
+               void setUserData(void* data);
 
                bool start();
+               bool stop();
 
                static bool isSupported(activity_type_e type);
-
-       protected:
-               void onEvent(sensor_data_t *eventData);
+               static void onEvent(double timestamp, float* values, int accuracy);
 
        private:
                activity_type_e __activityType;
                activity_recognition_cb __callback;
+               void *__userData;
+               bool __started;
+
+               static std::set<ActivitySensor*> __sensorSet;
+               static SensorAdapter __sensorAdapter;
        };
 
 }
 
-#endif /* _MOTION_ACTIVITY_PROXY_PRIVATE_H_ */
+#endif /* __MOTION_ACTIVITY_PROXY_PRIVATE_H__ */
index f699ec6..bdb4306 100644 (file)
@@ -16,7 +16,7 @@
  */
 
 #include <stdlib.h>
-#include "GestureProxy.h"
+#include "GestureSensor.h"
 
 #define FEATURE_GESTURE "tizen.org/feature/sensor.gesture_recognition"
 
@@ -27,7 +27,7 @@
 using namespace motion;
 
 struct _gesture_handle_s {
-       GestureProxy *proxy;
+       GestureSensor *sensor;
 };
 
 EXTAPI int gesture_is_supported(gesture_type_e gesture, bool* supported)
@@ -39,7 +39,7 @@ EXTAPI int gesture_is_supported(gesture_type_e gesture, bool* supported)
        ASSERT_NOT_NULL(supported);
        IF_FAIL_RETURN(IS_VALID_GESTURE(gesture), ERR_INVALID_PARAMETER);
 
-       *supported = GestureProxy::isSupported(gesture);
+       *supported = GestureSensor::isSupported(gesture);
        return ERR_NONE;
 }
 
@@ -51,8 +51,8 @@ EXTAPI int gesture_create(gesture_h *handle)
        _gesture_handle_s *hdl = static_cast<gesture_h>(malloc(sizeof(_gesture_handle_s)));
        IF_FAIL_RETURN_TAG(hdl, ERR_OPERATION_FAILED, _E, "Memory allocation failed");
 
-       hdl->proxy = new(std::nothrow) GestureProxy();
-       if (hdl->proxy == NULL) {
+       hdl->sensor = new(std::nothrow) GestureSensor();
+       if (hdl->sensor == NULL) {
                _E("Memory allocation failed");
                free(hdl);
                return ERR_OPERATION_FAILED;
@@ -67,7 +67,7 @@ EXTAPI int gesture_release(gesture_h handle)
        ASSERT_SUPPORTED(FEATURE_GESTURE);
        ASSERT_NOT_NULL(handle);
 
-       delete handle->proxy;
+       delete handle->sensor;
        free(handle);
 
        return ERR_NONE;
@@ -81,21 +81,21 @@ EXTAPI int gesture_start_recognition(gesture_h handle, gesture_type_e gesture, g
        IF_FAIL_RETURN(IS_VALID_GESTURE(gesture), ERR_INVALID_PARAMETER);
 
        if (option == GESTURE_OPTION_DEFAULT) {
-               handle->proxy->setPowerSave(true);
+               handle->sensor->setPowerSave(true);
        } else if (option == GESTURE_OPTION_ALWAYS_ON) {
-               handle->proxy->setPowerSave(false);
+               handle->sensor->setPowerSave(false);
        } else {
                return ERR_INVALID_PARAMETER;
        }
 
-       if (!handle->proxy->setGesture(gesture)) {
+       if (!handle->sensor->setGesture(gesture)) {
                return GESTURE_ERROR_NOT_SUPPORTED;
        }
 
-       handle->proxy->setCb(callback);
-       handle->proxy->setUserData(user_data);
+       handle->sensor->setCallback(callback);
+       handle->sensor->setUserData(user_data);
 
-       if (!handle->proxy->start()) {
+       if (!handle->sensor->start()) {
                return ERR_OPERATION_FAILED;
        }
 
@@ -107,7 +107,7 @@ EXTAPI int gesture_stop_recognition(gesture_h handle)
        ASSERT_SUPPORTED(FEATURE_GESTURE);
        ASSERT_NOT_NULL(handle);
 
-       IF_FAIL_RETURN(handle->proxy->stop(), GESTURE_ERROR_NOT_STARTED);
+       IF_FAIL_RETURN(handle->sensor->stop(), GESTURE_ERROR_NOT_STARTED);
 
        return ERR_NONE;
 }
similarity index 55%
rename from src/GestureProxy.cpp
rename to src/GestureSensor.cpp
index 474e968..306b059 100644 (file)
  *
  */
 
-#include "GestureProxy.h"
+#include "GestureSensor.h"
 
 using namespace motion;
 
-GestureProxy::GestureProxy()
-       : __gestureType(static_cast<gesture_type_e>(UNDEFINED))
-       , __callback(NULL)
+GestureSensor::GestureSensor() :
+       __gestureType(static_cast<gesture_type_e>(UNDEFINED)),
+       __callback(NULL),
+       __userData(NULL),
+       __sensorAdapter(this)
 {
 }
 
-GestureProxy::~GestureProxy()
+GestureSensor::~GestureSensor()
 {
 }
 
-bool GestureProxy::setGesture(gesture_type_e type)
+bool GestureSensor::setGesture(gesture_type_e type)
 {
        sensor_type_t sensor = __toSensor(type);
        IF_FAIL_RETURN(sensor != UNKNOWN_SENSOR, false);
 
        __gestureType = type;
-       setSensor(sensor);
+       __sensorAdapter.setSensor(sensor);
        return true;
 }
 
-void GestureProxy::setCb(gesture_recognition_cb cb)
+void GestureSensor::setPowerSave(bool ps)
+{
+       __sensorAdapter.setPowerSave(ps);
+}
+
+void GestureSensor::setCallback(gesture_recognition_cb cb)
 {
        __callback = cb;
 }
 
-bool GestureProxy::isSupported(gesture_type_e type)
+void GestureSensor::setUserData(void* data)
+{
+       __userData = data;
+}
+
+bool GestureSensor::start()
+{
+       return __sensorAdapter.start();
+}
+
+bool GestureSensor::stop()
+{
+       return __sensorAdapter.stop();
+}
+
+bool GestureSensor::isSupported(gesture_type_e type)
 {
        sensor_type_t sensor = __toSensor(type);
        IF_FAIL_RETURN(sensor != UNKNOWN_SENSOR, false);
 
-       return SensorProxy::isSupported(sensor);
+       return SensorAdapter::isSupported(sensor);
 }
 
-void GestureProxy::onEvent(sensor_data_t *eventData)
+void GestureSensor::onEvent(double timestamp, float* values, int accuracy)
 {
        _gesture_data_s data;
        data.gesture = __gestureType;
 
        /* TODO: This is the default form.
           For each sensor, this part needs to adapt accordingly */
-       data.event = static_cast<int>(eventData->values[0]);
+       data.event = static_cast<int>(values[0]);
 
-       __callback(__gestureType, &data, getTime(eventData->timestamp),
-                       static_cast<gesture_error_e>(ERR_NONE), userData);
+       __callback(__gestureType, &data, timestamp, static_cast<gesture_error_e>(ERR_NONE), __userData);
 }
 
-sensor_type_t GestureProxy::__toSensor(gesture_type_e type)
+sensor_type_t GestureSensor::__toSensor(gesture_type_e type)
 {
        switch (type) {
        case GESTURE_PICK_UP:
similarity index 67%
rename from src/GestureProxy.h
rename to src/GestureSensor.h
index 9f1951e..2170575 100644 (file)
  *
  */
 
-#ifndef _MOTION_GESTURE_PROXY_H_
-#define _MOTION_GESTURE_PROXY_H_
+#ifndef __MOTION_GESTURE_SENSOR_H__
+#define __MOTION_GESTURE_SENSOR_H__
 
 #include <gesture_recognition.h>
-#include "SensorProxy.h"
+#include "SensorAdapter.h"
 
 struct _gesture_data_s {
        int gesture;
@@ -30,27 +30,32 @@ struct _gesture_data_s {
 
 namespace motion {
 
-       class GestureProxy : public SensorProxy {
+       class GestureSensor : public ISensorListener {
        public:
-               GestureProxy();
-               ~GestureProxy();
+               GestureSensor();
+               ~GestureSensor();
 
                bool setGesture(gesture_type_e type);
-               void setCb(gesture_recognition_cb cb);
+               void setPowerSave(bool ps);
+               void setCallback(gesture_recognition_cb cb);
+               void setUserData(void* data);
 
-               static bool isSupported(gesture_type_e type);
+               bool start();
+               bool stop();
 
-       protected:
-               void onEvent(sensor_data_t *eventData);
+               static bool isSupported(gesture_type_e type);
 
        private:
-               gesture_type_e __gestureType;
-               gesture_recognition_cb __callback;
+               void onEvent(double timestamp, float* values, int accuracy);
 
                static sensor_type_t __toSensor(gesture_type_e type);
+
+               gesture_type_e __gestureType;
+               gesture_recognition_cb __callback;
+               void *__userData;
+               SensorAdapter __sensorAdapter;
        };
 
 }
 
-#endif /* _MOTION_GESTURE_PROXY_H_ */
-
+#endif /* __MOTION_GESTURE_SENSOR_H__ */
diff --git a/src/ISensorListener.h b/src/ISensorListener.h
new file mode 100644 (file)
index 0000000..b6d657e
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef __MOTION_I_SENSOR_LISTENER_H__
+#define __MOTION_I_SENSOR_LISTENER_H__
+
+namespace motion {
+
+       class ISensorListener {
+       public:
+               virtual ~ISensorListener() {}
+               virtual void onEvent(double timestamp, float* values, int accuracy) = 0;
+       };
+
+}
+
+#endif /* __MOTION_I_SENSOR_LISTENER_H__ */
diff --git a/src/SensorAdapter.cpp b/src/SensorAdapter.cpp
new file mode 100644 (file)
index 0000000..90cc77b
--- /dev/null
@@ -0,0 +1,133 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <time.h>
+#include <sys/time.h>
+#include "SensorAdapter.h"
+
+#define SENSOR_EVENT(X) (((int)(X) << 16) | 0x01)
+
+using namespace motion;
+
+SensorAdapter::SensorAdapter(ISensorListener* listener) :
+       __sensorHandle(-1),
+       __sensorType(UNKNOWN_SENSOR),
+       __powerSave(true),
+       __listener(listener)
+{
+}
+
+SensorAdapter::~SensorAdapter()
+{
+       stop();
+}
+
+void SensorAdapter::setSensor(sensor_type_t type)
+{
+       __sensorType = type;
+}
+
+void SensorAdapter::setPowerSave(bool ps)
+{
+       __powerSave = ps;
+
+       if (__sensorHandle < 0)
+               return;
+
+       sensord_set_attribute_int(__sensorHandle, SENSORD_ATTRIBUTE_PAUSE_POLICY,
+                       __powerSave ? SENSORD_PAUSE_ALL : SENSORD_PAUSE_NONE);
+}
+
+void SensorAdapter::setAttribute(int key, int value)
+{
+       IF_FAIL_VOID_TAG(__sensorHandle >= 0, _W, "Sensor not started");
+       sensord_set_attribute_int(__sensorHandle, key, value);
+}
+
+bool SensorAdapter::start()
+{
+       int err;
+       sensor_t sensor;
+
+       err = sensord_get_default_sensor(__sensorType, &sensor);
+       IF_FAIL_RETURN_TAG(err == 0, false, _E, "Getting sensor failed (%d)", err);
+
+       __sensorHandle = sensord_connect(sensor);
+       IF_FAIL_RETURN_TAG(__sensorHandle >= 0, false, _E, "Connection failed");
+
+       if (!sensord_register_event(__sensorHandle, SENSOR_EVENT(__sensorType), 0, 0, __eventCb, this)) {
+               _E("Event registration failed");
+               sensord_disconnect(__sensorHandle);
+               __sensorHandle = -1;
+               return false;
+       }
+
+       if (!sensord_start(__sensorHandle, __powerSave ? SENSOR_OPTION_DEFAULT : SENSOR_OPTION_ALWAYS_ON)) {
+               _E("Starting failed");
+               sensord_unregister_event(__sensorHandle, SENSOR_EVENT(__sensorType));
+               sensord_disconnect(__sensorHandle);
+               __sensorHandle = -1;
+               return false;
+       }
+
+       return true;
+}
+
+bool SensorAdapter::stop()
+{
+       IF_FAIL_RETURN(__sensorHandle >= 0, false);
+
+       sensord_stop(__sensorHandle);
+       sensord_unregister_event(__sensorHandle, SENSOR_EVENT(__sensorType));
+       sensord_disconnect(__sensorHandle);
+       __sensorHandle = -1;
+
+       return true;
+}
+
+bool SensorAdapter::isSupported(sensor_type_t type)
+{
+       sensor_t sensor = sensord_get_sensor(type);
+       return (sensor != NULL);
+}
+
+double SensorAdapter::__getEpoch(unsigned long long monotonic)
+{
+       struct timespec ts;
+       struct timeval tv;
+       double timeDiff;
+       double timestamp;
+
+       clock_gettime(CLOCK_MONOTONIC, &ts);
+       timeDiff = (ts.tv_sec * 1000000000.0 + ts.tv_nsec) / 1000000.0 - monotonic / 1000.0;
+
+       gettimeofday(&tv, NULL);
+       timestamp = tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0 - timeDiff;
+       return timestamp;
+}
+
+void SensorAdapter::__onEvent(sensor_data_t *eventData)
+{
+       double timestamp = __getEpoch(eventData->timestamp);
+       __listener->onEvent(timestamp, eventData->values, eventData->accuracy);
+}
+
+void SensorAdapter::__eventCb(sensor_t sensor, unsigned int eventType, sensor_data_t *eventData, void *cbData)
+{
+       SensorAdapter *instance = static_cast<SensorAdapter*>(cbData);
+       instance->__onEvent(eventData);
+}
similarity index 64%
rename from src/SensorProxy.h
rename to src/SensorAdapter.h
index 9f74cb5..66ac04d 100644 (file)
  *
  */
 
-#ifndef _MOTION_SENSOR_PROXY_H_
-#define _MOTION_SENSOR_PROXY_H_
+#ifndef __MOTION_SENSOR_ADAPTER_H__
+#define __MOTION_SENSOR_ADAPTER_H__
 
 #include <sensor_internal.h>
 #include "TypesInternal.h"
+#include "ISensorListener.h"
 
 namespace motion {
 
-       class SensorProxy {
+       class SensorAdapter {
        public:
-               SensorProxy();
-               virtual ~SensorProxy();
+               SensorAdapter(ISensorListener* listener);
+               ~SensorAdapter();
 
                void setSensor(sensor_type_t type);
                void setPowerSave(bool ps);
-               void setUserData(void *data);
+               void setAttribute(int key, int value);
 
-               bool stop(bool force = false);
-
-               virtual bool start();
-
-       protected:
-               int sensorHandle;
-               bool running;
-               sensor_type_t sensorType;
-               bool powerSave;
-               void *userData;
-
-               double getTime(unsigned long long monotonic);
+               bool start();
+               bool stop();
 
                static bool isSupported(sensor_type_t type);
 
-               virtual void onEvent(sensor_data_t *eventData) = 0;
-
        private:
+               double __getEpoch(unsigned long long monotonic);
+               void __onEvent(sensor_data_t* eventData);
+
                static void __eventCb(sensor_t sensor, unsigned int eventType, sensor_data_t *eventData, void *cbData);
+
+               int __sensorHandle;
+               sensor_type_t __sensorType;
+               bool __powerSave;
+               void *__userData;
+               ISensorListener *__listener;
        };
 
 }
 
-#endif /* _MOTION_SENSOR_PROXY_H_ */
+#endif /* __MOTION_SENSOR_ADAPTER_H__ */
diff --git a/src/SensorProxy.cpp b/src/SensorProxy.cpp
deleted file mode 100644 (file)
index a120048..0000000
+++ /dev/null
@@ -1,120 +0,0 @@
-/*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-#include <time.h>
-#include <sys/time.h>
-#include "SensorProxy.h"
-
-#define SENSOR_EVENT(X) (((int)(X) << 16) | 0x01)
-
-using namespace motion;
-
-SensorProxy::SensorProxy()
-       : sensorHandle(-1)
-       , running(false)
-       , sensorType(UNKNOWN_SENSOR)
-       , powerSave(true)
-       , userData(NULL)
-{
-}
-
-SensorProxy::~SensorProxy()
-{
-       stop(true);
-}
-
-void SensorProxy::setSensor(sensor_type_t type)
-{
-       sensorType = type;
-}
-
-void SensorProxy::setPowerSave(bool ps)
-{
-       powerSave = ps;
-}
-
-void SensorProxy::setUserData(void *data)
-{
-       userData = data;
-}
-
-bool SensorProxy::start()
-{
-       sensor_t sensor = sensord_get_sensor(sensorType);
-       IF_FAIL_RETURN_TAG(sensor, false, _E, "Getting sensor failed");
-
-       sensorHandle = sensord_connect(sensor);
-       IF_FAIL_RETURN_TAG(sensorHandle >= 0, false, _E, "Connection failed");
-
-       if (!sensord_register_event(sensorHandle, SENSOR_EVENT(sensorType), 0, 0, __eventCb, this)) {
-               _E("Event registration failed");
-               sensord_disconnect(sensorHandle);
-               sensorHandle = -1;
-               return false;
-       }
-
-       if (!sensord_start(sensorHandle, powerSave ? SENSOR_OPTION_DEFAULT : SENSOR_OPTION_ALWAYS_ON)) {
-               _E("Starting failed");
-               sensord_unregister_event(sensorHandle, SENSOR_EVENT(sensorType));
-               sensord_disconnect(sensorHandle);
-               sensorHandle = -1;
-               return false;
-       }
-
-       return true;
-}
-
-bool SensorProxy::stop(bool force)
-{
-       if (!force) {
-               IF_FAIL_RETURN(sensorHandle >= 0, false);
-       }
-
-       sensord_stop(sensorHandle);
-       sensord_unregister_event(sensorHandle, SENSOR_EVENT(sensorType));
-       sensord_disconnect(sensorHandle);
-       sensorHandle = -1;
-
-       return true;
-}
-
-double SensorProxy::getTime(unsigned long long monotonic)
-{
-       struct timespec ts;
-       struct timeval tv;
-       double timeDiff;
-       double timestamp;
-
-       clock_gettime(CLOCK_MONOTONIC, &ts);
-       timeDiff = (ts.tv_sec * 1000000000.0 + ts.tv_nsec) / 1000000.0 - monotonic / 1000.0;
-
-       gettimeofday(&tv, NULL);
-       timestamp = tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0 - timeDiff;
-       return timestamp;
-}
-
-bool SensorProxy::isSupported(sensor_type_t type)
-{
-       sensor_t sensor = sensord_get_sensor(type);
-       return (sensor != NULL);
-}
-
-void SensorProxy::__eventCb(sensor_t sensor, unsigned int eventType, sensor_data_t *eventData, void *cbData)
-{
-       SensorProxy *instance = static_cast<SensorProxy*>(cbData);
-       instance->onEvent(eventData);
-}