From: Mu-Woong Lee Date: Wed, 6 Apr 2016 12:23:43 +0000 (+0900) Subject: Re-implement using the latest sensor internal API X-Git-Tag: submit/tizen/20160411.085651^2~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F86%2F64986%2F7;p=platform%2Fcore%2Fapi%2Fmotion.git Re-implement using the latest sensor internal API Change-Id: I46a23aeb008c886c421c155693f48431f41724f7 Signed-off-by: Mu-Woong Lee --- diff --git a/CMakeLists.txt b/CMakeLists.txt index c2049b8..d6db33d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ INCLUDE(GNUInstallDirs) SET(target "core-context-manager") # Source Lists -FILE(GLOB_RECURSE SRCS src/*.cpp) +FILE(GLOB SRCS src/*.cpp) SET(inc_subdir "context-service") SET(dependency "glib-2.0 dlog capi-base-common capi-system-info sensor") @@ -15,10 +15,9 @@ SET(dependency "glib-2.0 dlog capi-base-common capi-system-info sensor") INCLUDE(FindPkgConfig) INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include - ${CMAKE_CURRENT_SOURCE_DIR}/src/shared ) -ADD_DEFINITIONS(-O2 -Wall -fPIC -fdata-sections -ffunction-sections -fvisibility=hidden) +ADD_DEFINITIONS(-O2 -Wall -fPIC -flto -fdata-sections -ffunction-sections -fvisibility=hidden) SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIC -Wl,--as-needed -Wl,--gc-section -Wl,--print-gc-section") # Build diff --git a/packaging/motion.spec b/packaging/motion.spec index d0eb8d8..694b22e 100644 --- a/packaging/motion.spec +++ b/packaging/motion.spec @@ -36,7 +36,6 @@ export CXXFLAGS+=" -fno-strict-aliasing -fno-unroll-loops -fsigned-char -fstrict export CFLAGS+=" -fno-common" export CXXFLAGS+=" -Wnon-virtual-dtor" -export CXXFLAGS+=" -std=c++11 -Wno-c++11-compat" #export CFLAGS+=" -Wcast-qual" #export CXXFLAGS+=" -Wcast-qual" diff --git a/src/Activity.cpp b/src/Activity.cpp new file mode 100644 index 0000000..46e29a0 --- /dev/null +++ b/src/Activity.cpp @@ -0,0 +1,117 @@ +/* + * 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 +#include "ActivityProxy.h" + +#define FEATURE_ACTIVITY "tizen.org/feature/sensor.activity_recognition" + +#define ACTIVITY_FIRST ACTIVITY_STATIONARY +#define ACTIVITY_LAST ACTIVITY_IN_VEHICLE +#define IS_VALID_ACTIVITY(X) (ACTIVITY_FIRST <= (X) && (X) <= ACTIVITY_LAST) + +using namespace motion; + +struct _activity_handle_s { + ActivityProxy *proxy; +}; + +EXTAPI int activity_is_supported(activity_type_e activity, bool* supported) +{ + if (supported) + *supported = false; + + ASSERT_SUPPORTED(FEATURE_ACTIVITY); + ASSERT_NOT_NULL(supported); + IF_FAIL_RETURN(IS_VALID_ACTIVITY(activity), ERR_INVALID_PARAMETER); + + *supported = ActivityProxy::isSupported(activity); + return ERR_NONE; +} + +EXTAPI int activity_create(activity_h *handle) +{ + ASSERT_SUPPORTED(FEATURE_ACTIVITY); + ASSERT_NOT_NULL(handle); + + _activity_handle_s *hdl = static_cast(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) { + _E("Memory allocation failed"); + free(hdl); + return ERR_OPERATION_FAILED; + } + + *handle = hdl; + return ERR_NONE; +} + +EXTAPI int activity_release(activity_h handle) +{ + ASSERT_SUPPORTED(FEATURE_ACTIVITY); + ASSERT_NOT_NULL(handle); + + delete handle->proxy; + free(handle); + + return ERR_NONE; +} + +EXTAPI int activity_start_recognition(activity_h handle, activity_type_e activity, activity_recognition_cb callback, void *user_data) +{ + ASSERT_SUPPORTED(FEATURE_ACTIVITY); + ASSERT_NOT_NULL(handle); + ASSERT_NOT_NULL(callback); + IF_FAIL_RETURN(IS_VALID_ACTIVITY(activity), ERR_INVALID_PARAMETER); + + if (!handle->proxy->setActivity(activity)) { + return ERR_INVALID_PARAMETER; + } + + handle->proxy->setPowerSave(false); + handle->proxy->setCb(callback); + handle->proxy->setUserData(user_data); + + if (!handle->proxy->start()) { + return ERR_OPERATION_FAILED; + } + + return ERR_NONE; +} + +EXTAPI int activity_stop_recognition(activity_h handle) +{ + ASSERT_SUPPORTED(FEATURE_ACTIVITY); + ASSERT_NOT_NULL(handle); + + handle->proxy->stop(); + + return ERR_NONE; +} + +EXTAPI int activity_get_accuracy(const activity_data_h data, activity_accuracy_e *accuracy) +{ + ASSERT_SUPPORTED(FEATURE_ACTIVITY); + ASSERT_NOT_NULL(data); + ASSERT_NOT_NULL(accuracy); + + *accuracy = static_cast(data->accuracy); + + return ERR_NONE; +} diff --git a/src/ActivityProxy.cpp b/src/ActivityProxy.cpp new file mode 100644 index 0000000..fa8275d --- /dev/null +++ b/src/ActivityProxy.cpp @@ -0,0 +1,71 @@ +/* + * 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 +#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(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(__activityType)); + + data.activity = __activityType; + data.accuracy = eventData->accuracy; + + __callback(__activityType, &data, getTime(eventData->timestamp), + static_cast(ERR_NONE), userData); +} diff --git a/src/ActivityProxy.h b/src/ActivityProxy.h new file mode 100644 index 0000000..402bd27 --- /dev/null +++ b/src/ActivityProxy.h @@ -0,0 +1,53 @@ +/* + * 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_ACTIVITY_PROXY_H_ +#define _MOTION_ACTIVITY_PROXY_H_ + +#include +#include "SensorProxy.h" + +struct _activity_data_s { + int activity; + int accuracy; +}; + +namespace motion { + + class ActivityProxy : public SensorProxy { + public: + ActivityProxy(); + ~ActivityProxy(); + + bool setActivity(activity_type_e type); + void setCb(activity_recognition_cb cb); + + bool start(); + + static bool isSupported(activity_type_e type); + + protected: + void onEvent(sensor_data_t *eventData); + + private: + activity_type_e __activityType; + activity_recognition_cb __callback; + }; + +} + +#endif /* _MOTION_ACTIVITY_PROXY_PRIVATE_H_ */ diff --git a/src/Gesture.cpp b/src/Gesture.cpp new file mode 100644 index 0000000..f09d17a --- /dev/null +++ b/src/Gesture.cpp @@ -0,0 +1,143 @@ +/* + * 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 +#include "GestureProxy.h" + +#define FEATURE_GESTURE "tizen.org/feature/sensor.gesture_recognition" + +#define GESTURE_FIRST GESTURE_DOUBLE_TAP +#define GESTURE_LAST GESTURE_WRIST_UP +#define IS_VALID_GESTURE(X) (GESTURE_FIRST <= (X) && (X) <= GESTURE_LAST) + +using namespace motion; + +struct _gesture_handle_s { + GestureProxy *proxy; +}; + +EXTAPI int gesture_is_supported(gesture_type_e gesture, bool* supported) +{ + if (supported) + *supported = false; + + ASSERT_SUPPORTED(FEATURE_GESTURE); + ASSERT_NOT_NULL(supported); + IF_FAIL_RETURN(IS_VALID_GESTURE(gesture), ERR_INVALID_PARAMETER); + + *supported = GestureProxy::isSupported(gesture); + return ERR_NONE; +} + +EXTAPI int gesture_create(gesture_h *handle) +{ + ASSERT_SUPPORTED(FEATURE_GESTURE); + ASSERT_NOT_NULL(handle); + + _gesture_handle_s *hdl = static_cast(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) { + _E("Memory allocation failed"); + free(hdl); + return ERR_OPERATION_FAILED; + } + + *handle = hdl; + return ERR_NONE; +} + +EXTAPI int gesture_release(gesture_h handle) +{ + ASSERT_SUPPORTED(FEATURE_GESTURE); + ASSERT_NOT_NULL(handle); + + delete handle->proxy; + free(handle); + + return ERR_NONE; +} + +EXTAPI int gesture_start_recognition(gesture_h handle, gesture_type_e gesture, gesture_option_e option, gesture_recognition_cb callback, void *user_data) +{ + ASSERT_SUPPORTED(FEATURE_GESTURE); + ASSERT_NOT_NULL(handle); + ASSERT_NOT_NULL(callback); + IF_FAIL_RETURN(IS_VALID_GESTURE(gesture), ERR_INVALID_PARAMETER); + + if (!handle->proxy->setGesture(gesture)) { + return ERR_INVALID_PARAMETER; + } + + if (option == GESTURE_OPTION_DEFAULT) { + handle->proxy->setPowerSave(true); + } else if (option == GESTURE_OPTION_ALWAYS_ON) { + handle->proxy->setPowerSave(false); + } else { + return ERR_INVALID_PARAMETER; + } + + handle->proxy->setCb(callback); + handle->proxy->setUserData(user_data); + + if (!handle->proxy->start()) { + return ERR_OPERATION_FAILED; + } + + return ERR_NONE; +} + +EXTAPI int gesture_stop_recognition(gesture_h handle) +{ + ASSERT_SUPPORTED(FEATURE_GESTURE); + ASSERT_NOT_NULL(handle); + + handle->proxy->stop(); + + return ERR_NONE; +} + +EXTAPI int gesture_get_event(const gesture_data_h data, gesture_event_e *event) +{ + ASSERT_SUPPORTED(FEATURE_GESTURE); + ASSERT_NOT_NULL(data); + ASSERT_NOT_NULL(event); + + if (data->gesture == GESTURE_TILT) + return GESTURE_ERROR_NOT_SUPPORTED; + + *event = static_cast(data->event); + + return ERR_NONE; +} + +EXTAPI int gesture_get_tilt(const gesture_data_h data, int *x, int *y) +{ + ASSERT_SUPPORTED(FEATURE_GESTURE); + ASSERT_NOT_NULL(data); + ASSERT_NOT_NULL(x); + ASSERT_NOT_NULL(y); + + if (data->gesture != GESTURE_TILT) + return ERR_NOT_SUPPORTED; + + *x = data->tilt_x; + *y = data->tilt_y; + + return ERR_NONE; +} diff --git a/src/GestureProxy.cpp b/src/GestureProxy.cpp new file mode 100644 index 0000000..474e968 --- /dev/null +++ b/src/GestureProxy.cpp @@ -0,0 +1,78 @@ +/* + * 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 "GestureProxy.h" + +using namespace motion; + +GestureProxy::GestureProxy() + : __gestureType(static_cast(UNDEFINED)) + , __callback(NULL) +{ +} + +GestureProxy::~GestureProxy() +{ +} + +bool GestureProxy::setGesture(gesture_type_e type) +{ + sensor_type_t sensor = __toSensor(type); + IF_FAIL_RETURN(sensor != UNKNOWN_SENSOR, false); + + __gestureType = type; + setSensor(sensor); + return true; +} + +void GestureProxy::setCb(gesture_recognition_cb cb) +{ + __callback = cb; +} + +bool GestureProxy::isSupported(gesture_type_e type) +{ + sensor_type_t sensor = __toSensor(type); + IF_FAIL_RETURN(sensor != UNKNOWN_SENSOR, false); + + return SensorProxy::isSupported(sensor); +} + +void GestureProxy::onEvent(sensor_data_t *eventData) +{ + _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(eventData->values[0]); + + __callback(__gestureType, &data, getTime(eventData->timestamp), + static_cast(ERR_NONE), userData); +} + +sensor_type_t GestureProxy::__toSensor(gesture_type_e type) +{ + switch (type) { + case GESTURE_PICK_UP: + return GESTURE_MOVEMENT_SENSOR; + case GESTURE_WRIST_UP: + return GESTURE_WRIST_UP_SENSOR; + default: + return UNKNOWN_SENSOR; + } +} diff --git a/src/GestureProxy.h b/src/GestureProxy.h new file mode 100644 index 0000000..9f1951e --- /dev/null +++ b/src/GestureProxy.h @@ -0,0 +1,56 @@ +/* + * 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_GESTURE_PROXY_H_ +#define _MOTION_GESTURE_PROXY_H_ + +#include +#include "SensorProxy.h" + +struct _gesture_data_s { + int gesture; + int event; + int tilt_x; + int tilt_y; +}; + +namespace motion { + + class GestureProxy : public SensorProxy { + public: + GestureProxy(); + ~GestureProxy(); + + bool setGesture(gesture_type_e type); + void setCb(gesture_recognition_cb cb); + + static bool isSupported(gesture_type_e type); + + protected: + void onEvent(sensor_data_t *eventData); + + private: + gesture_type_e __gestureType; + gesture_recognition_cb __callback; + + static sensor_type_t __toSensor(gesture_type_e type); + }; + +} + +#endif /* _MOTION_GESTURE_PROXY_H_ */ + diff --git a/src/SensorProxy.cpp b/src/SensorProxy.cpp new file mode 100644 index 0000000..551c8f7 --- /dev/null +++ b/src/SensorProxy.cpp @@ -0,0 +1,116 @@ +/* + * 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 +#include +#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(); +} + +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; +} + +void SensorProxy::stop() +{ + IF_FAIL_VOID(sensorHandle >= 0); + + sensord_stop(sensorHandle); + sensord_unregister_event(sensorHandle, SENSOR_EVENT(sensorType)); + sensord_disconnect(sensorHandle); + sensorHandle = -1; +} + +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(cbData); + instance->onEvent(eventData); +} diff --git a/src/SensorProxy.h b/src/SensorProxy.h new file mode 100644 index 0000000..0f0d01e --- /dev/null +++ b/src/SensorProxy.h @@ -0,0 +1,58 @@ +/* + * 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_SENSOR_PROXY_H_ +#define _MOTION_SENSOR_PROXY_H_ + +#include +#include "TypesInternal.h" + +namespace motion { + + class SensorProxy { + public: + SensorProxy(); + virtual ~SensorProxy(); + + void setSensor(sensor_type_t type); + void setPowerSave(bool ps); + void setUserData(void *data); + + void stop(); + + virtual bool start(); + + protected: + int sensorHandle; + bool running; + sensor_type_t sensorType; + bool powerSave; + void *userData; + + double getTime(unsigned long long monotonic); + + static bool isSupported(sensor_type_t type); + + virtual void onEvent(sensor_data_t *eventData) = 0; + + private: + static void __eventCb(sensor_t sensor, unsigned int eventType, sensor_data_t *eventData, void *cbData); + }; + +} + +#endif /* _MOTION_SENSOR_PROXY_H_ */ diff --git a/src/TypesInternal.cpp b/src/TypesInternal.cpp new file mode 100644 index 0000000..2a7a755 --- /dev/null +++ b/src/TypesInternal.cpp @@ -0,0 +1,35 @@ +/* + * 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 +#include "TypesInternal.h" + +int motion::isSupported(const char *feature) +{ + bool supported = false; + int ret = system_info_get_platform_bool(feature, &supported); + + if (ret == ERR_NONE && !supported) { + _W("Not Supported: %s", feature); + return ERR_NOT_SUPPORTED; + } else if (ret != ERR_NONE) { + _E("Getting system info failed: %#x", ret); + return ERR_OPERATION_FAILED; + } + + return ERR_NONE; +} diff --git a/src/TypesInternal.h b/src/TypesInternal.h new file mode 100644 index 0000000..bde7980 --- /dev/null +++ b/src/TypesInternal.h @@ -0,0 +1,89 @@ +/* + * 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_TYPES_INTERNAL_H_ +#define _MOTION_TYPES_INTERNAL_H_ + +#include +#include + +#define EXTAPI __attribute__ ((visibility("default"))) + +#define UNDEFINED -1 + +#define ERR_NONE TIZEN_ERROR_NONE +#define ERR_INVALID_PARAMETER TIZEN_ERROR_INVALID_PARAMETER +#define ERR_INVALID_OPERATION TIZEN_ERROR_INVALID_OPERATION +#define ERR_OUT_OF_MEMORY TIZEN_ERROR_OUT_OF_MEMORY +#define ERR_PERMISSION_DENIED TIZEN_ERROR_PERMISSION_DENIED +#define ERR_NOT_SUPPORTED TIZEN_ERROR_NOT_SUPPORTED +#define ERR_NO_DATA TIZEN_ERROR_NO_DATA +#define ERR_ALREADY_STARTED (TIZEN_ERROR_CONTEXT | 0x01) +#define ERR_NOT_STARTED (TIZEN_ERROR_CONTEXT | 0x02) +#define ERR_OUT_OF_RANGE (TIZEN_ERROR_CONTEXT | 0x03) +#define ERR_OPERATION_FAILED (TIZEN_ERROR_CONTEXT | 0x04) +#define ERR_RULE_ENABLED (TIZEN_ERROR_CONTEXT | 0x05) +#define ERR_RULE_NOT_ENABLED (TIZEN_ERROR_CONTEXT | 0x06) +#define ERR_INVALID_RULE (TIZEN_ERROR_CONTEXT | 0x07) +#define ERR_RULE_NOT_EXIST (TIZEN_ERROR_CONTEXT | 0x08) +#define ERR_INVALID_DATA ERR_INVALID_RULE +#define ERR_DATA_EXIST (TIZEN_ERROR_CONTEXT | 0X09) + +/* Logging and Error Handling */ +#define _I SLOGI +#define _D SLOGD +#define _W SLOGW +#define _E SLOGE +#define _SI SECURE_SLOGI +#define _SD SECURE_SLOGD +#define _SW SECURE_LOGW +#define _SE SECURE_SLOGE + +#define IF_FAIL_RETURN_TAG(cond, ret, tag, fmt, arg...) \ + do { if (!(cond)) { tag(fmt, ##arg); return ret; } } while (0) + +#define IF_FAIL_RETURN(cond, ret) \ + do { if (!(cond)) { return ret; } } while (0) + +#define IF_FAIL_VOID_TAG(cond, tag, fmt, arg...) \ + do { if (!(cond)) { tag(fmt, ##arg); return; } } while (0) + +#define IF_FAIL_VOID(cond) \ + do { if (!(cond)) { return; } } while (0) + +#define IF_FAIL_CATCH_TAG(cond, tag, fmt, arg...) \ + do { if (!(cond)) { tag(fmt, ##arg); goto CATCH; } } while (0) + +#define IF_FAIL_CATCH(cond) \ + do { if (!(cond)) { goto CATCH; } } while (0) + +#define IS_FAILED(X) ((X) != ERR_NONE) + +#define ASSERT_ALLOC(X) IF_FAIL_RETURN_TAG(X, ERR_OUT_OF_MEMORY, _E, "Memory allocation failed") +#define ASSERT_NOT_NULL(X) IF_FAIL_RETURN_TAG(X, ERR_INVALID_PARAMETER, _E, "Parameter null") + +#define ASSERT_SUPPORTED(feature) \ + do { \ + int __result = motion::isSupported(feature); \ + if (__result != ERR_NONE) return __result; \ + } while (0) + +namespace motion { + int isSupported(const char *feature); +} + +#endif /* _MOTION_TYPES_INTERNAL_H_ */ diff --git a/src/activity/activity.cpp b/src/activity/activity.cpp deleted file mode 100644 index 020a1cf..0000000 --- a/src/activity/activity.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/* - * motion - * - * Copyright (c) 2014 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 -#include -#include "activity.h" - -EXTAPI int activity_is_supported(activity_type_e activity, bool* supported) -{ - if (supported) - *supported = false; - - return CERR_NOT_SUPPORTED; -} - -EXTAPI int activity_create(activity_h *handle) -{ - return CERR_NOT_SUPPORTED; -} - -EXTAPI int activity_release(activity_h handle) -{ - return CERR_NOT_SUPPORTED; -} - -EXTAPI int activity_start_recognition(activity_h handle, activity_type_e activity, activity_recognition_cb callback, void *user_data) -{ - return CERR_NOT_SUPPORTED; -} - -EXTAPI int activity_stop_recognition(activity_h handle) -{ - return CERR_NOT_SUPPORTED; -} - -EXTAPI int activity_get_accuracy(const activity_data_h data, activity_accuracy_e *accuracy) -{ - return CERR_NOT_SUPPORTED; -} diff --git a/src/activity/activity.h b/src/activity/activity.h deleted file mode 100644 index c802c90..0000000 --- a/src/activity/activity.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * motion - * - * Copyright (c) 2014 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 _CONTEXT_ACTIVITY_H_ -#define _CONTEXT_ACTIVITY_H_ - -#include - -#define FEATURE_KEY_ACTIVITY "tizen.org/feature/sensor.activity_recognition" - -typedef struct _activity_data_s { - int activity; - int accuracy; -} _cx_activity_data; - -typedef struct _activity_handle_s { - int req_id; - activity_recognition_cb callback; - void *user_data; -} _cx_activity_h; - -#endif /* End of _CONTEXT_ACTIVITY_PRIVATE_H_ */ diff --git a/src/gesture/gesture.cpp b/src/gesture/gesture.cpp deleted file mode 100644 index f609090..0000000 --- a/src/gesture/gesture.cpp +++ /dev/null @@ -1,176 +0,0 @@ -/* - * motion - * - * Copyright (c) 2014 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 -#include -#include -#include -#include -#include "motion_engine.h" -#include "gesture.h" - -using namespace ctx::gesture; - -static ctx::handle_map_t<_cx_gesture_h> handle_map; - -void cx_gesture_deliver_data(int req_id, _cx_gesture_data data, double timestamp, int error) -{ - _cx_gesture_h *handle = NULL; - - handle = handle_map.get_copy(req_id); - try_return_void_tag(handle, LOGD, "No matching handle found"); - - if (handle->callback) { - data.gesture = handle->gesture; - handle->callback(static_cast(data.gesture), &data, - timestamp, static_cast(error), handle->user_data); - } - - free(handle); -} - -EXTAPI int gesture_is_supported(gesture_type_e gesture, bool* supported) -{ - if (supported) - *supported = false; - - ASSERT_SUPPORTED(FEATURE_KEY_GESTURE); - ASSERT_NULL(supported); - - int ret = GESTURE_ERROR_NONE; - - if (motion_engine::check_coverage(gesture)) { - ret = motion_engine::is_supported(gesture); - *supported = (ret == GESTURE_ERROR_NONE); - } - - return ret; -} - -EXTAPI int gesture_create(gesture_h *handle) -{ - ASSERT_SUPPORTED(FEATURE_KEY_GESTURE); - ASSERT_HANDLE; - - *handle = static_cast(malloc(sizeof(_cx_gesture_h))); - ASSERT_ALLOC(*handle); - - (*handle)->req_id = CTX_VALUE_UNDEFINED; - (*handle)->gesture = CTX_VALUE_UNDEFINED; - (*handle)->callback = NULL; - (*handle)->user_data = NULL; - - (*handle)->me_handle = CTX_VALUE_UNDEFINED; - (*handle)->me_event = CTX_VALUE_UNDEFINED; - - return GESTURE_ERROR_NONE; -} - -EXTAPI int gesture_release(gesture_h handle) -{ - ASSERT_SUPPORTED(FEATURE_KEY_GESTURE); - ASSERT_HANDLE; - gesture_stop_recognition(handle); - free(handle); - return GESTURE_ERROR_NONE; -} - -EXTAPI int gesture_start_recognition(gesture_h handle, gesture_type_e gesture, gesture_option_e option, gesture_recognition_cb callback, void *user_data) -{ - ASSERT_SUPPORTED(FEATURE_KEY_GESTURE); - if (gesture == GESTURE_WRIST_UP) { - ASSERT_SUPPORTED(FEATURE_KEY_WRISTUP); - } - ASSERT_HANDLE; - ASSERT_NULL(callback); - - if (handle_map.find(handle->req_id) != NULL) { - LOGW("The handle is being used"); - return GESTURE_ERROR_ALREADY_STARTED; - } - - handle->gesture = gesture; - handle->user_data = user_data; - handle->callback = callback; - - int ret; - - if (motion_engine::check_coverage(gesture)) { - ret = motion_engine::start(handle, gesture, option); - } else { - ret = GESTURE_ERROR_INVALID_PARAMETER; - } - - if (ret == GESTURE_ERROR_NONE) - handle_map.insert(handle->req_id, handle); - - return ret; -} - -EXTAPI int gesture_stop_recognition(gesture_h handle) -{ - ASSERT_SUPPORTED(FEATURE_KEY_GESTURE); - ASSERT_HANDLE; - int ret = GESTURE_ERROR_NOT_STARTED; - - if (handle_map.find(handle->req_id) != NULL) { - if (motion_engine::check_coverage(handle->gesture)) - motion_engine::stop(handle); - - handle_map.remove(handle->req_id); - ret = GESTURE_ERROR_NONE; - } - - handle->req_id = CTX_VALUE_UNDEFINED; - handle->gesture = CTX_VALUE_UNDEFINED; - handle->callback = NULL; - handle->user_data = NULL; - - return ret; -} - -EXTAPI int gesture_get_event(const gesture_data_h data, gesture_event_e *event) -{ - ASSERT_SUPPORTED(FEATURE_KEY_GESTURE); - ASSERT_NULL(data); - ASSERT_NULL(event); - - if (data->gesture == GESTURE_TILT) - return GESTURE_ERROR_NOT_SUPPORTED; - - *event = static_cast(data->event); - - return GESTURE_ERROR_NONE; -} - -EXTAPI int gesture_get_tilt(const gesture_data_h data, int *x, int *y) -{ - ASSERT_SUPPORTED(FEATURE_KEY_GESTURE); - ASSERT_NULL(data); - ASSERT_NULL(x); - ASSERT_NULL(y); - - if (data->gesture != GESTURE_TILT) - return GESTURE_ERROR_NOT_SUPPORTED; - - *x = data->tilt_x; - *y = data->tilt_y; - - return GESTURE_ERROR_NONE; -} diff --git a/src/gesture/gesture.h b/src/gesture/gesture.h deleted file mode 100644 index d2dde88..0000000 --- a/src/gesture/gesture.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * motion - * - * Copyright (c) 2014 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 _CONTEXT_GESTURE_H_ -#define _CONTEXT_GESTURE_H_ - -#include - -#define FEATURE_KEY_GESTURE "tizen.org/feature/sensor.gesture_recognition" -#define FEATURE_KEY_WRISTUP "tizen.org/feature/sensor.wrist_up" - -typedef struct _gesture_data_s { - int gesture; - int event; - int tilt_x; - int tilt_y; -} _cx_gesture_data; - -typedef struct _gesture_handle_s { - int req_id; - int gesture; - gesture_recognition_cb callback; - void *user_data; - - int me_handle; // Handle for motion-engine in sensor-fw - int me_event; // Event enum for motion-engine in sensor-fw - -} _cx_gesture_h; - -void cx_gesture_deliver_data(int req_id, _cx_gesture_data data, double timestamp, int error); - -#endif /* End of _CONTEXT_GESTURE_PRIVATE_H_ */ diff --git a/src/gesture/motion_engine.cpp b/src/gesture/motion_engine.cpp deleted file mode 100644 index 1c01997..0000000 --- a/src/gesture/motion_engine.cpp +++ /dev/null @@ -1,219 +0,0 @@ -/* - * motion - * - * Copyright (c) 2014 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 -#include -#include -#include -#include "gesture.h" -#include "motion_engine.h" - -static int convert_shake_data(int value) -{ - switch (value) { - case MOTION_ENGIEN_SHAKE_NONE: - return GESTURE_EVENT_NONE; - - case MOTION_ENGIEN_SHAKE_DETECTION: - case MOTION_ENGIEN_SHAKE_CONTINUING: - return GESTURE_SHAKE_DETECTED; - - case MOTION_ENGIEN_SHAKE_FINISH: - case MOTION_ENGINE_SHAKE_BREAK: - return GESTURE_SHAKE_FINISHED; - - default: - return CTX_VALUE_UNDEFINED; - } -} - -static int convert_face_down_data(int value) -{ - switch (value) { - case MOTION_ENGIEN_TOP_TO_BOTTOM_NONE: - return GESTURE_EVENT_NONE; - - case MOTION_ENGIEN_TOP_TO_BOTTOM_DETECTION: - return GESTURE_EVENT_DETECTED; - - default: - return CTX_VALUE_UNDEFINED; - } -} - -static void me_sensor_event_extract_int(unsigned int event_type, sensor_data_t *event_data, _cx_gesture_data *gesture_data) -{ - gesture_data->event = event_data->values[0]; - - switch (event_type) { - case MOTION_ENGINE_EVENT_SHAKE: - case MOTION_ENGINE_EVENT_SHAKE_ALWAYS_ON: - gesture_data->event = convert_shake_data(gesture_data->event); - return; - - case MOTION_ENGINE_EVENT_TOP_TO_BOTTOM: - gesture_data->event = convert_face_down_data(gesture_data->event); - return; - - default: - return; - } -} - -static void me_sensor_event_extract_tilt(sensor_data_t *event_data, _cx_gesture_data *gesture_data) -{ - try_return_void_tag(event_data->value_count >= 2, LOGW, "Invalid data count (%d)", event_data->value_count); - gesture_data->tilt_x = event_data->values[0]; - gesture_data->tilt_y = event_data->values[1]; -} - -static bool me_sensor_extract_data(unsigned int event_type, sensor_data_t *event_data, _cx_gesture_data *gesture_data) -{ - if (event_type == MOTION_ENGINE_EVENT_TILT) { - me_sensor_event_extract_tilt(event_data, gesture_data); - - } else { - me_sensor_event_extract_int(event_type, event_data, gesture_data); - } - - return true; -} - -static void me_sensor_event_cb(sensor_t sensor, unsigned int sensor_event_type, sensor_data_t *sensor_event_data, void *user_data) -{ - try_return_void(user_data); - - int64_t ts = ctx::time::get_utc(NULL, NULL); - int req_id = *static_cast(user_data); - LOGD("Received data for ReqId %d", req_id); - - _cx_gesture_data data; - - if (!me_sensor_extract_data(sensor_event_type, sensor_event_data, &data)) { - LOGE("Failed to extract motion data from sensor event"); - return; - } - - cx_gesture_deliver_data(req_id, data, ts/1000.0L, GESTURE_ERROR_NONE); -} - -static int get_event_type(int motion) -{ - switch (motion) { - // Mobile Lite Profile - case GESTURE_SHAKE: - return MOTION_ENGINE_EVENT_SHAKE; - - // Mobile Full Profile - case GESTURE_DOUBLE_TAP: - return MOTION_ENGINE_EVENT_DOUBLETAP; - - case GESTURE_MOVE_TO_EAR: - return MOTION_ENGINE_EVENT_DIRECT_CALL; - - case GESTURE_NO_MOVE: - return MOTION_ENGINE_EVENT_NO_MOVE; - - case GESTURE_PICK_UP: - return MOTION_ENGINE_EVENT_SMART_RELAY; - - case GESTURE_SNAP: - return MOTION_ENGINE_EVENT_SNAP; - - case GESTURE_TILT: - return MOTION_ENGINE_EVENT_TILT; - - case GESTURE_TURN_FACE_DOWN: - return MOTION_ENGINE_EVENT_TOP_TO_BOTTOM; - - default: - return CTX_VALUE_UNDEFINED; - } -} - -static int get_me_option(int mode) -{ - if (mode == GESTURE_OPTION_DEFAULT) { - return SENSOR_OPTION_DEFAULT; - - } else if (mode == GESTURE_OPTION_ALWAYS_ON) { - return SENSOR_OPTION_ALWAYS_ON; - } - - return CTX_VALUE_UNDEFINED; -} - -bool ctx::gesture::motion_engine::check_coverage(int motion) -{ - switch (motion) { - case GESTURE_SHAKE: - case GESTURE_DOUBLE_TAP: - case GESTURE_MOVE_TO_EAR: - case GESTURE_NO_MOVE: - case GESTURE_PICK_UP: - case GESTURE_SNAP: - case GESTURE_TILT: - case GESTURE_TURN_FACE_DOWN: - return true; - - default: - return false; - } -} - -int ctx::gesture::motion_engine::is_supported(int motion) -{ - int handle = -1, res; - int ev_type = get_event_type(motion); - res = ctx::sensor::connect(&handle, MOTION_SENSOR, ev_type, SENSOR_OPTION_DEFAULT, me_sensor_event_cb, NULL); - ctx::sensor::disconnect(&handle, MOTION_SENSOR, ev_type); - - return res; -} - -int ctx::gesture::motion_engine::start(_cx_gesture_h *handle, int gesture, int option) -{ - int ev_type = get_event_type(gesture); - int me_opt = get_me_option(option); - - handle->req_id = ctx::generate_rid(); - - try_return_result(me_opt != CTX_VALUE_UNDEFINED, GESTURE_ERROR_INVALID_PARAMETER); - - int res = ctx::sensor::connect(&handle->me_handle, MOTION_SENSOR, ev_type, me_opt, me_sensor_event_cb, &(handle->req_id)); - try_return_result(!IS_FAILED(res), res); - - LOGI("Started MotionEngine Event %d (Gesture %d)", ev_type, gesture); - - handle->me_event = ev_type; - - return GESTURE_ERROR_NONE; -} - -int ctx::gesture::motion_engine::stop(_cx_gesture_h *handle) -{ - LOGI("Stopping MotionEngine Event %d", handle->me_event); - - ctx::sensor::disconnect(&handle->me_handle, MOTION_SENSOR, handle->me_event); - - handle->me_handle = CTX_VALUE_UNDEFINED; - handle->me_event = CTX_VALUE_UNDEFINED; - - return GESTURE_ERROR_NONE; -} diff --git a/src/gesture/motion_engine.h b/src/gesture/motion_engine.h deleted file mode 100644 index 36f3ec7..0000000 --- a/src/gesture/motion_engine.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * motion - * - * Copyright (c) 2014 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 _CX_GESTURE_MOTION_ENGINE_H_ -#define _CX_GESTURE_MOTION_ENGINE_H_ - -#include -#include "gesture.h" - -namespace ctx { namespace gesture { namespace motion_engine { - - bool check_coverage(int gesture); - int is_supported(int gesture); - int start(_cx_gesture_h *handle, int gesture, int mode); - int stop(_cx_gesture_h *handle); - -} } } - -#endif /* End of _CX_GESTURE_MOTION_ENGINE_H_ */ diff --git a/src/shared/ctx_error.h b/src/shared/ctx_error.h deleted file mode 100644 index 4fca642..0000000 --- a/src/shared/ctx_error.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * motion - * - * Copyright (c) 2014 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 __TIZEN_CONTEXT_ERROR_H__ -#define __TIZEN_CONTEXT_ERROR_H__ - -#include - -#define CERR_NONE TIZEN_ERROR_NONE /* Successful */ -#define CERR_INVALID_PARAMETER TIZEN_ERROR_INVALID_PARAMETER /* Invalid function parameter */ -#define CERR_INVALID_OPERATION TIZEN_ERROR_INVALID_OPERATION /* Function not implemented */ -#define CERR_OUT_OF_MEMORY TIZEN_ERROR_OUT_OF_MEMORY /* Out of memory */ -#define CERR_PERMISSION_DENIED TIZEN_ERROR_PERMISSION_DENIED /* Permission denied */ -#define CERR_NOT_SUPPORTED TIZEN_ERROR_NOT_SUPPORTED /* Not supported */ -#define CERR_ALREADY_STARTED (TIZEN_ERROR_CONTEXT | 0x01) /* Already started */ -#define CERR_NOT_STARTED (TIZEN_ERROR_CONTEXT | 0x02) /* Not started */ -#define CERR_OPERATION_FAILED (TIZEN_ERROR_CONTEXT | 0x04) /* Operation failed because of a system error */ - -#endif // __TIZEN_CONTEXT_ERROR_H__ diff --git a/src/shared/ctx_types_private.h b/src/shared/ctx_types_private.h deleted file mode 100644 index 570989c..0000000 --- a/src/shared/ctx_types_private.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * motion - * - * Copyright (c) 2014 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 __CONTEXT_TYPES_PRIVATE_H__ -#define __CONTEXT_TYPES_PRIVATE_H__ - -#include -#include - -#define EXTAPI __attribute__ ((visibility("default"))) - -#define ASSERT_SUPPORTED(feature) \ - do { \ - int __result = ctx::is_supported(feature); \ - if (__result != CERR_NONE) return __result; \ - } while (0) - -#define try_return_result_tag(cond, ret, tag, fmt, arg...) \ - do { if (!(cond)) { tag(fmt, ##arg); return ret; } } while (0) - -#define try_return_void_tag(cond, tag, fmt, arg...) \ - do { if (!(cond)) { tag(fmt, ##arg); return; } } while (0) - -#define try_catch_tag(cond, tag, fmt, arg...) \ - do { if (!(cond)) { tag(fmt, ##arg); goto CATCH; } } while (0) - -#define try_return_result(cond, ret) \ - do { if (!(cond)) { return ret; } } while (0) - -#define try_return_void(cond) \ - do { if (!(cond)) { return; } } while (0) - -#define try_catch(cond) \ - do { if (!(cond)) { goto CATCH; } } while (0) - -#define assert_invalid_param(cond) \ - do { if (!(cond)) { return CERR_INVALID_PARAMETER; } } while (0) - -#define IS_FAILED(X) ((X) != CERR_NONE) - -#define CTX_VALUE_UNDEFINED -1 - -#define ASSERT_HANDLE try_return_result_tag(handle, CERR_INVALID_PARAMETER, LOGE, "handle cannot be null") -#define ASSERT_ALLOC(X) try_return_result_tag(X, CERR_OPERATION_FAILED, LOGE, "Memory allocation failed") -#define ASSERT_NULL(X) try_return_result_tag(X, CERR_INVALID_PARAMETER, LOGE, "Parameter null") - -#endif /* __CONTEXT_TYPES_PRIVATE_H__ */ diff --git a/src/shared/ctx_util_misc.cpp b/src/shared/ctx_util_misc.cpp deleted file mode 100644 index affa491..0000000 --- a/src/shared/ctx_util_misc.cpp +++ /dev/null @@ -1,87 +0,0 @@ -/* - * motion - * - * Copyright (c) 2014 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 -#include -#include -#include -#include -#include -#include -#include "ctx_error.h" -#include "ctx_util_misc.h" - -int ctx::generate_rid() -{ - static GMutex rid_mutex; - static int rid = 0; - int ret = 0; - - g_mutex_lock(&rid_mutex); - - if (++rid < 0) - rid = 1; - - ret = rid; - - g_mutex_unlock(&rid_mutex); - - return ret; -} - -int ctx::is_supported(const char* feature) -{ - bool supported = false; - int ret = system_info_get_platform_bool(feature, &supported); - - if (ret == CERR_NONE && !supported) { - SLOGD("Not Supported: %s", feature); - return CERR_NOT_SUPPORTED; - } else if (ret != CERR_NONE) { - SLOGE("Getting system info failed: %#x", ret); - return CERR_OPERATION_FAILED; - } - - return CERR_NONE; -} - -int64_t ctx::time::get_utc(int64_t* ymd, int64_t* hms) -{ - struct timeval now; - struct tm tm_now; - int64_t timestamp; - int64_t hms_now; - - gettimeofday(&now, NULL); - timestamp = now.tv_sec * 1000LL + now.tv_usec / 1000; - - if (ymd || hms) { - time_t now_sec = now.tv_sec; - gmtime_r(&now_sec, &tm_now); - hms_now = ((tm_now.tm_hour * 60 + tm_now.tm_min) * 60 + tm_now.tm_sec) * 1000LL + now.tv_usec / 1000; - - if (hms) - *hms = hms_now; - - if (ymd) - *ymd = timestamp - hms_now; - } - - return timestamp; -} diff --git a/src/shared/ctx_util_misc.h b/src/shared/ctx_util_misc.h deleted file mode 100644 index e15d207..0000000 --- a/src/shared/ctx_util_misc.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * motion - * - * Copyright (c) 2014 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 __CONTEXT_UTIL_MISC_H__ -#define __CONTEXT_UTIL_MISC_H__ - -#include - -namespace ctx { - - int generate_rid(void); - int is_supported(const char* feature); - - namespace time { - - int64_t get_utc(int64_t* ymd, int64_t* hms); - - } /* namespace ctx::util */ - -} /* namespace ctx */ - -#endif /* __CONTEXT_UTIL_MISC_H__ */ diff --git a/src/shared/ctx_util_sensor.cpp b/src/shared/ctx_util_sensor.cpp deleted file mode 100644 index 4077fbb..0000000 --- a/src/shared/ctx_util_sensor.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/* - * motion - * - * Copyright (c) 2014 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 -#include "ctx_types_private.h" -#include "ctx_util_sensor.h" - -#define NUM_CONNECT_TRIAL 2 - -int ctx::sensor::connect(int *handle, sensor_type_t sensor, unsigned int event, int option, sensor_cb_t cb, void *user_data) -{ - sensor_t sen = sensord_get_sensor(sensor); - - for (int i=0; i < NUM_CONNECT_TRIAL && *handle < 0; ++i) { - LOGD("Trial #%d to connect to Sensor %d", i+1, sensor); - *handle = sensord_connect(sen); - } - - try_return_result_tag(*handle>=0, CERR_NOT_SUPPORTED, SLOGE, "Failed to connect Sensor %d", sensor); - - bool supported = false; - try_return_result_tag(sensord_is_supported_event_type(sen, event, &supported), CERR_OPERATION_FAILED, SLOGE, "sensord_is_supported_event_type() failed"); - try_return_result_tag(supported, CERR_NOT_SUPPORTED, SLOGD, "Sensor Event %d is not supported.", event); - - if (!sensord_register_event(*handle, event, 0, 0, cb, user_data)) { - LOGE("Failed to register Sensor Event %d", event); - sensord_disconnect(*handle); - *handle = -1; - return CERR_NOT_SUPPORTED; - } - - if (!sensord_start(*handle, option)) { - LOGE("Failed to start Sensor %d", sensor); - sensord_unregister_event(*handle, event); - sensord_disconnect(*handle); - *handle = -1; - return CERR_OPERATION_FAILED; - } - - LOGI("Sensor %d (Event %#x) started (Option: %d)", sensor, event, option); - return CERR_NONE; -} - -void ctx::sensor::disconnect(int *handle, sensor_type_t sensor, unsigned int event) -{ - try_return_void(*handle >= 0); - - sensord_unregister_event(*handle, event); - sensord_stop(*handle); - sensord_disconnect(*handle); - - *handle = -1; - - LOGI("Sensor %d (Event %#x) stopped", sensor, event); -} diff --git a/src/shared/ctx_util_sensor.h b/src/shared/ctx_util_sensor.h deleted file mode 100644 index 70a8ded..0000000 --- a/src/shared/ctx_util_sensor.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * motion - * - * Copyright (c) 2014 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 __CONTEXT_UTIL_SENSOR_H__ -#define __CONTEXT_UTIL_SENSOR_H__ - -#include - -namespace ctx { namespace sensor { - - int connect(int *handle, sensor_type_t sensor, unsigned int event, int option, sensor_cb_t cb, void *user_data); - void disconnect(int *handle, sensor_type_t sensor, unsigned int event); - -} } /* namespace ctx::sensor */ - -#endif /* __CONTEXT_UTIL_SENSOR_H__ */ - diff --git a/src/shared/handle_map.h b/src/shared/handle_map.h deleted file mode 100644 index 75d9b2b..0000000 --- a/src/shared/handle_map.h +++ /dev/null @@ -1,107 +0,0 @@ -/* - * motion - * - * Copyright (c) 2014 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 _CONTEXT_HANDLE_LIST_H_ -#define _CONTEXT_HANDLE_LIST_H_ - -#include -#include -#include "ctx_types_private.h" - -namespace ctx { - -template -class handle_map_t -{ -private: - std::map _map; - GMutex _mutex; - -public: - handle_map_t() {} - ~handle_map_t() {} - - void insert(int rid, _handle_t *h); - void remove(int rid); - - _handle_t* find(int rid); - _handle_t* get_copy(int rid); - -}; /* class handle_map_t */ - -} /* namesapce ctx */ - -template -void ctx::handle_map_t<_handle_t>::insert(int rid, _handle_t *h) -{ - g_mutex_lock(&_mutex); - - _map[rid] = h; - - g_mutex_unlock(&_mutex); -} - -template -void ctx::handle_map_t<_handle_t>::remove(int rid) -{ - g_mutex_lock(&_mutex); - - _map.erase(rid); - - g_mutex_unlock(&_mutex); -} - -template -_handle_t* ctx::handle_map_t<_handle_t>::find(int rid) -{ - _handle_t *h = NULL; - - g_mutex_lock(&_mutex); - - typename std::map::iterator it = _map.find(rid); - if (it != _map.end()) - h = it->second; - - g_mutex_unlock(&_mutex); - - return h; -} - -template -_handle_t* ctx::handle_map_t<_handle_t>::get_copy(int rid) -{ - _handle_t *copy = static_cast<_handle_t*>(malloc(sizeof(_handle_t))); - try_return_result_tag(copy, NULL, LOGE, "Memory allocation failed"); - - g_mutex_lock(&_mutex); - - typename std::map::iterator it = _map.find(rid); - if (it != _map.end()) { - *copy = *(it->second); - } else { - free(copy); - copy = NULL; - } - - g_mutex_unlock(&_mutex); - - return copy; -} - -#endif /* _CONTEXT_HANDLE_LIST_H_ */