From 3bbe9bd327d239c06ce3e15d5ecc7306ff4e5b50 Mon Sep 17 00:00:00 2001 From: Somin Kim Date: Thu, 7 Apr 2016 18:28:07 +0900 Subject: [PATCH] Applying Tizen C++ coding style to place context provider(geofence) Change-Id: Ic81ae68d2e39dcea8adf4e7fbdbad6bafa9919aa Signed-off-by: Somin Kim --- ..._provider.cpp => PlaceContextProvider.cpp} | 12 +- src/place/geofence/GeofenceMonitorHandle.cpp | 218 ++++++++++++++++++ src/place/geofence/GeofenceMonitorHandle.h | 55 +++++ src/place/geofence/PlaceGeofenceProvider.cpp | 151 ++++++++++++ ...ace_geofence.h => PlaceGeofenceProvider.h} | 27 ++- ..._geofence_types.h => PlaceGeofenceTypes.h} | 26 +-- src/place/geofence/myplace_handle.cpp | 218 ------------------ src/place/geofence/myplace_handle.h | 59 ----- src/place/geofence/place_geofence.cpp | 151 ------------ src/place/recognition/place_recognition.cpp | 2 +- src/place/recognition/place_recognition.h | 2 +- 11 files changed, 457 insertions(+), 464 deletions(-) rename src/place/{place_context_provider.cpp => PlaceContextProvider.cpp} (82%) create mode 100644 src/place/geofence/GeofenceMonitorHandle.cpp create mode 100644 src/place/geofence/GeofenceMonitorHandle.h create mode 100644 src/place/geofence/PlaceGeofenceProvider.cpp rename src/place/geofence/{place_geofence.h => PlaceGeofenceProvider.h} (69%) rename src/place/geofence/{place_geofence_types.h => PlaceGeofenceTypes.h} (63%) delete mode 100644 src/place/geofence/myplace_handle.cpp delete mode 100644 src/place/geofence/myplace_handle.h delete mode 100644 src/place/geofence/place_geofence.cpp diff --git a/src/place/place_context_provider.cpp b/src/place/PlaceContextProvider.cpp similarity index 82% rename from src/place/place_context_provider.cpp rename to src/place/PlaceContextProvider.cpp index c3efaef..f168dd1 100644 --- a/src/place/place_context_provider.cpp +++ b/src/place/PlaceContextProvider.cpp @@ -20,27 +20,27 @@ #include #ifdef _MOBILE_ -#include "geofence/place_geofence.h" +#include "geofence/PlaceGeofenceProvider.h" #ifndef _DISABLE_RECOG_ENGINE_ #include "recognition/place_recognition.h" #endif /* _DISABLE_RECOG_ENGINE_ */ #endif /* _MOBILE_ */ -template +template void registerProvider(const char *subject, const char *privilege) { - if (!provider::is_supported()) + if (!Provider::isSupported()) return; - ctx::ContextProviderInfo providerInfo(provider::create, provider::destroy, NULL, privilege); + ctx::ContextProviderInfo providerInfo(Provider::create, Provider::destroy, NULL, privilege); ctx::context_manager::registerProvider(subject, providerInfo); } EXTAPI bool ctx::initPlaceContextProvider() { #ifdef _MOBILE_ - registerProvider(PLACE_SUBJ_GEOFENCE, PLACE_PRIV_GEOFENCE); - place_geofence_provider::submit_trigger_item(); + registerProvider(PLACE_SUBJ_GEOFENCE, PLACE_PRIV_GEOFENCE); + PlaceGeofenceProvider::submitTriggerItem(); #ifndef _DISABLE_RECOG_ENGINE_ place_recognition_provider::create(NULL); diff --git a/src/place/geofence/GeofenceMonitorHandle.cpp b/src/place/geofence/GeofenceMonitorHandle.cpp new file mode 100644 index 0000000..52b4b07 --- /dev/null +++ b/src/place/geofence/GeofenceMonitorHandle.cpp @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2015 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 "PlaceGeofenceTypes.h" +#include "GeofenceMonitorHandle.h" + +ctx::GeofenceMonitorHandle::GeofenceMonitorHandle() : + __placeId(-1), + __prevState(GEOFENCE_STATE_UNCERTAIN), + __geoHandle(NULL) +{ +} + +ctx::GeofenceMonitorHandle::~GeofenceMonitorHandle() +{ + __stopMonitor(); +} + +bool ctx::GeofenceMonitorHandle::startMonitor(int placeId) +{ + _D("Starts to monitor Place-%d", placeId); + + IF_FAIL_RETURN(placeId >= 0, false); + IF_FAIL_RETURN_TAG(__geoHandle == NULL, false, _E, "Re-starting MyPlace monitor"); + + geofence_manager_create(&__geoHandle); + IF_FAIL_RETURN_TAG(__geoHandle, false, _E, "Geofence initialization failed"); + + int ret; + + ret = geofence_manager_set_geofence_state_changed_cb(__geoHandle, __fenceStateCb, this); + IF_FAIL_CATCH_TAG(ret == GEOFENCE_MANAGER_ERROR_NONE, _E, "Setting state callback failed"); + + ret = geofence_manager_set_geofence_event_cb(__geoHandle, __fenceEventCb, this); + IF_FAIL_CATCH_TAG(ret == GEOFENCE_MANAGER_ERROR_NONE, _E, "Setting event callback failed"); + + ret = geofence_manager_foreach_place_geofence_list(__geoHandle, placeId, __fenceListCb, this); + IF_FAIL_CATCH_TAG(ret == GEOFENCE_MANAGER_ERROR_NONE, _E, "Getting fence list failed"); + + __placeId = placeId; + return true; + +CATCH: + __stopMonitor(); + return false; +} + +int ctx::GeofenceMonitorHandle::getPlaceId() +{ + return __placeId; +} + +void ctx::GeofenceMonitorHandle::__stopMonitor() +{ + _D("Stops monitoring Place-%d", __placeId); + + //TODO: Do we need to stop all geofences explicitly? + if (__geoHandle) { + geofence_manager_destroy(__geoHandle); + __geoHandle = NULL; + } + + __geoStateMap.clear(); + __placeId = -1; + __prevState = GEOFENCE_STATE_UNCERTAIN; +} + +bool ctx::GeofenceMonitorHandle::__startFence(int fenceId) +{ + int ret; + + ret = geofence_manager_start(__geoHandle, fenceId); + IF_FAIL_RETURN_TAG(ret == GEOFENCE_MANAGER_ERROR_NONE, true, _W, "Starting failed"); + + geofence_status_h status; + ret = geofence_status_create(fenceId, &status); + IF_FAIL_RETURN_TAG(ret == GEOFENCE_MANAGER_ERROR_NONE, true, _W, "Getting status failed"); + + geofence_state_e state = GEOFENCE_STATE_UNCERTAIN; + geofence_status_get_state(status, &state); + geofence_status_destroy(status); + + __geoStateMap[fenceId] = state; + + return true; +} + +void ctx::GeofenceMonitorHandle::__removeFence(int fenceId) +{ + geofence_manager_stop(__geoHandle, fenceId); + __geoStateMap.erase(fenceId); +} + +void ctx::GeofenceMonitorHandle::__updateFence(int fenceId, geofence_manage_e manage) +{ + switch (manage) { + case GEOFENCE_MANAGE_PLACE_REMOVED: + _W("[Place-%d] Removed", __placeId); + __stopMonitor(); + break; + case GEOFENCE_MANAGE_FENCE_ADDED: + _I("[Place %d] Fence-%d added", __placeId, fenceId); + __startFence(fenceId); + __emitStateChange(); + break; + case GEOFENCE_MANAGE_FENCE_REMOVED: + _I("[Place-%d] Fence-%d removed", __placeId, fenceId); + __removeFence(fenceId); + __emitStateChange(); + break; + case GEOFENCE_MANAGE_FENCE_STARTED: + _D("[Place-%d] Fence-%d started", __placeId, fenceId); + break; + case GEOFENCE_MANAGE_FENCE_STOPPED: + _D("[Place-%d] Fence-%d stopped", __placeId, fenceId); + //TODO: Do we need to restart this? + break; + default: + _D("[Place-%d] Ignoring the manage event %d", __placeId, manage); + break; + } +} + +void ctx::GeofenceMonitorHandle::__updateState(int fenceId, geofence_state_e state) +{ + __geoStateMap[fenceId] = state; +} + +static const char* get_state_string(geofence_state_e state) +{ + switch (state) { + case GEOFENCE_STATE_IN: + return PLACE_GEOFENCE_IN; + case GEOFENCE_STATE_OUT: + return PLACE_GEOFENCE_OUT; + case GEOFENCE_STATE_UNCERTAIN: + return PLACE_GEOFENCE_UNCERTAIN; + default: + return PLACE_GEOFENCE_UNCERTAIN; + } +} + +void ctx::GeofenceMonitorHandle::__emitStateChange() +{ + geofence_state_e current_state = GEOFENCE_STATE_UNCERTAIN; + int outCount = 0; + + for (auto it = __geoStateMap.begin(); it != __geoStateMap.end(); ++it) { + if (it->second == GEOFENCE_STATE_IN) { + current_state = GEOFENCE_STATE_IN; + break; + } else if (it->second == GEOFENCE_STATE_OUT) { + ++ outCount; + } + } + + if (current_state != GEOFENCE_STATE_IN && outCount > 0) { + current_state = GEOFENCE_STATE_OUT; + } + + if (current_state == __prevState) { + return; + } + + __prevState = current_state; + + Json option; + option.set(NULL, PLACE_GEOFENCE_PLACE_ID, __placeId); + + Json data; + data.set(NULL, PLACE_GEOFENCE_PLACE_ID, __placeId); + data.set(NULL, PLACE_GEOFENCE_EVENT, get_state_string(current_state)); + + context_manager::publish(PLACE_SUBJ_GEOFENCE, option, ERR_NONE, data); +} + +bool ctx::GeofenceMonitorHandle::__fenceListCb(int geofenceId, geofence_h fence, int fenceIndex, int fenceCount, void* userData) +{ + _D("FenceID: %d, Index: %d, Count: %d", geofenceId, fenceIndex, fenceCount); + IF_FAIL_RETURN(fenceCount > 0, false); + + GeofenceMonitorHandle *handle = reinterpret_cast(userData); + return handle->__startFence(geofenceId); +} + +void ctx::GeofenceMonitorHandle::__fenceEventCb(int placeId, int geofenceId, geofence_manager_error_e error, geofence_manage_e manage, void* userData) +{ + IF_FAIL_VOID_TAG(error == GEOFENCE_MANAGER_ERROR_NONE, _W, "Geofence error: %d", error); + + GeofenceMonitorHandle *handle = reinterpret_cast(userData); + + IF_FAIL_VOID_TAG(placeId == handle->getPlaceId(), _W, "Mismatched Place ID"); + + handle->__updateFence(geofenceId, manage); +} + +void ctx::GeofenceMonitorHandle::__fenceStateCb(int geofenceId, geofence_state_e state, void* userData) +{ + GeofenceMonitorHandle *handle = reinterpret_cast(userData); + handle->__updateState(geofenceId, state); + handle->__emitStateChange(); +} diff --git a/src/place/geofence/GeofenceMonitorHandle.h b/src/place/geofence/GeofenceMonitorHandle.h new file mode 100644 index 0000000..3f15432 --- /dev/null +++ b/src/place/geofence/GeofenceMonitorHandle.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2015 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_PLACE_GEOFENCE_MONITOR_HANDLE_H_ +#define _CONTEXT_PLACE_GEOFENCE_MONITOR_HANDLE_H_ + +#include +#include +#include + +namespace ctx { + + class GeofenceMonitorHandle { + + public: + GeofenceMonitorHandle(); + ~GeofenceMonitorHandle(); + + bool startMonitor(int placeId); + int getPlaceId(); + + private: + int __placeId; + geofence_state_e __prevState; + geofence_manager_h __geoHandle; + std::map __geoStateMap; + + void __emitStateChange(); + void __stopMonitor(); + bool __startFence(int fenceId); + void __removeFence(int fenceId); + void __updateFence(int fenceId, geofence_manage_e manage); + void __updateState(int fenceId, geofence_state_e state); + + static bool __fenceListCb(int geofenceId, geofence_h fence, int fenceIndex, int fenceCount, void* userData); + static void __fenceEventCb(int placeId, int geofenceId, geofence_manager_error_e error, geofence_manage_e manage, void* userData); + static void __fenceStateCb(int geofenceId, geofence_state_e state, void* userData); + }; + +} /* namespace ctx */ + +#endif /* End of _CONTEXT_PLACE_GEOFENCE_MONITOR_HANDLE_H_ */ diff --git a/src/place/geofence/PlaceGeofenceProvider.cpp b/src/place/geofence/PlaceGeofenceProvider.cpp new file mode 100644 index 0000000..a1a5b5f --- /dev/null +++ b/src/place/geofence/PlaceGeofenceProvider.cpp @@ -0,0 +1,151 @@ +/* + * Copyright (c) 2015 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 "PlaceGeofenceProvider.h" + +ctx::PlaceGeofenceProvider *ctx::PlaceGeofenceProvider::__instance = NULL; + +ctx::PlaceGeofenceProvider::PlaceGeofenceProvider() +{ +} + +ctx::PlaceGeofenceProvider::~PlaceGeofenceProvider() +{ + for (auto it = __handleMap.begin(); it != __handleMap.end(); ++it) { + delete it->second; + } + + __handleMap.clear(); +} + +ctx::ContextProviderBase *ctx::PlaceGeofenceProvider::create(void *data) +{ + IF_FAIL_RETURN(!__instance, __instance); + __instance = new(std::nothrow) PlaceGeofenceProvider(); + IF_FAIL_RETURN_TAG(__instance, NULL, _E, "Memory allocation failed"); + _I(BLUE("Created")); + return __instance; +} + +void ctx::PlaceGeofenceProvider::destroy(void *data) +{ + IF_FAIL_VOID(__instance); + delete __instance; + __instance = NULL; + _I(BLUE("Destroyed")); +} + +bool ctx::PlaceGeofenceProvider::isSupported() +{ + bool supported = false; + int ret = geofence_manager_is_supported(&supported); + IF_FAIL_RETURN_TAG(ret == GEOFENCE_MANAGER_ERROR_NONE, false, _E, "geofence_manager_is_supported() failed"); + return supported; +} + +void ctx::PlaceGeofenceProvider::submitTriggerItem() +{ + context_manager::registerTriggerItem(PLACE_SUBJ_GEOFENCE, OPS_SUBSCRIBE, + "{" + "\"Event\":{\"type\":\"string\",\"values\":[\"In\",\"Out\"]}" + "}", + "{" + "\"PlaceId\":{\"type\":\"integer\",\"min\":1}" + "}"); +} + +void ctx::PlaceGeofenceProvider::__destroyIfUnused() +{ + IF_FAIL_VOID(__handleMap.empty()); + destroy(NULL); +} + + +int ctx::PlaceGeofenceProvider::subscribe(const char *subject, ctx::Json option, ctx::Json *requestResult) +{ + int ret = __subscribe(option); + __destroyIfUnused(); + return ret; +} + +int ctx::PlaceGeofenceProvider::unsubscribe(const char *subject, ctx::Json option) +{ + int ret = __unsubscribe(option); + __destroyIfUnused(); + return ret; +} + +int ctx::PlaceGeofenceProvider::read(const char *subject, ctx::Json option, ctx::Json *requestResult) +{ + __destroyIfUnused(); + return ERR_NOT_SUPPORTED; +} + +int ctx::PlaceGeofenceProvider::write(const char *subject, ctx::Json data, ctx::Json *requestResult) +{ + __destroyIfUnused(); + return ERR_NOT_SUPPORTED; +} + +int ctx::PlaceGeofenceProvider::__subscribe(ctx::Json option) +{ + int placeId = -1; + option.get(NULL, PLACE_GEOFENCE_PLACE_ID, &placeId); + IF_FAIL_RETURN_TAG(placeId != -1, ERR_INVALID_PARAMETER, _E, "Getting PlaceID failed"); + + auto it = __handleMap.find(placeId); + if (it != __handleMap.end()) { + _D("Place ID %d is being monitored already", placeId); + return ERR_NONE; + } + + GeofenceMonitorHandle *handle = new(std::nothrow) GeofenceMonitorHandle(); + ASSERT_ALLOC(handle); + + bool ret = handle->startMonitor(placeId); + if (!ret) { + _E("Monitoring Place ID %d failed", placeId); + delete handle; + return ERR_OPERATION_FAILED; + } + + __handleMap[placeId] = handle; + + return ERR_NONE; +} + +int ctx::PlaceGeofenceProvider::__unsubscribe(ctx::Json option) +{ + int placeId = -1; + option.get(NULL, PLACE_GEOFENCE_PLACE_ID, &placeId); + IF_FAIL_RETURN_TAG(placeId != -1, ERR_INVALID_PARAMETER, _E, "Getting PlaceID failed"); + + auto it = __handleMap.find(placeId); + if (it == __handleMap.end()) { + _D("Place ID %d is not being monitored", placeId); + return ERR_NONE; + } + + delete it->second; + __handleMap.erase(it); + + return ERR_NONE; +} diff --git a/src/place/geofence/place_geofence.h b/src/place/geofence/PlaceGeofenceProvider.h similarity index 69% rename from src/place/geofence/place_geofence.h rename to src/place/geofence/PlaceGeofenceProvider.h index 739610f..a95f4c6 100644 --- a/src/place/geofence/place_geofence.h +++ b/src/place/geofence/PlaceGeofenceProvider.h @@ -14,24 +14,23 @@ * limitations under the License. */ -#ifndef __CONTEXT_PLACE_GEOFENCE_H__ -#define __CONTEXT_PLACE_GEOFENCE_H__ +#ifndef _CONTEXT_PLACE_GEOFENCE_PROVIDER_H_ +#define _CONTEXT_PLACE_GEOFENCE_PROVIDER_H_ #include #include -#include "myplace_handle.h" -#include "place_geofence_types.h" +#include "GeofenceMonitorHandle.h" +#include "PlaceGeofenceTypes.h" namespace ctx { - class place_geofence_provider : public ContextProviderBase { - typedef std::map handle_map_t; + class PlaceGeofenceProvider : public ContextProviderBase { public: static ContextProviderBase *create(void *data); static void destroy(void *data); - static bool is_supported(); - static void submit_trigger_item(); + static bool isSupported(); + static void submitTriggerItem(); int subscribe(const char *subject, ctx::Json option, ctx::Json *requestResult); int unsubscribe(const char *subject, ctx::Json option); @@ -39,17 +38,17 @@ namespace ctx { int write(const char *subject, ctx::Json data, ctx::Json *requestResult); private: - static place_geofence_provider *__instance; - handle_map_t __handle_map; + static PlaceGeofenceProvider *__instance; + std::map __handleMap; - place_geofence_provider(); - ~place_geofence_provider(); + PlaceGeofenceProvider(); + ~PlaceGeofenceProvider(); int __subscribe(ctx::Json option); int __unsubscribe(ctx::Json option); - void __destroy_if_unused(); + void __destroyIfUnused(); }; } /* namespace ctx */ -#endif /* __CONTEXT_PLACE_GEOFENCE_H__ */ +#endif /* End of _CONTEXT_PLACE_GEOFENCE_PROVIDER_H_ */ diff --git a/src/place/geofence/place_geofence_types.h b/src/place/geofence/PlaceGeofenceTypes.h similarity index 63% rename from src/place/geofence/place_geofence_types.h rename to src/place/geofence/PlaceGeofenceTypes.h index b9aa61a..fa66a41 100644 --- a/src/place/geofence/place_geofence_types.h +++ b/src/place/geofence/PlaceGeofenceTypes.h @@ -14,24 +14,22 @@ * limitations under the License. */ -#ifndef __CONTEXT_PLACE_GEOFENCE_TYPES__ -#define __CONTEXT_PLACE_GEOFENCE_TYPES__ +#ifndef _CONTEXT_PLACE_GEOFENCE_TYPES_H_ +#define _CONTEXT_PLACE_GEOFENCE_TYPES_H_ -// Context Items +// Subject #define PLACE_SUBJ_GEOFENCE "place/geofence" +// Privilege #define PLACE_PRIV_GEOFENCE "location" -// Option Keys -#define PLACE_STATUS_OPT_MYPLACE_ID "PlaceId" +// Option & Data Key +#define PLACE_GEOFENCE_PLACE_ID "PlaceId" +#define PLACE_GEOFENCE_EVENT "Event" -// Data Keys -#define PLACE_STATUS_DATA_MYPLACE_ID "PlaceId" -#define PLACE_STATUS_DATA_MYPLACE_EVENT "Event" +// Data Value +#define PLACE_GEOFENCE_UNCERTAIN "Uncertain" +#define PLACE_GEOFENCE_IN "In" +#define PLACE_GEOFENCE_OUT "Out" -// Data Values -#define MYPLACE_EVENT_UNCERTAIN "Uncertain" -#define MYPLACE_EVENT_IN "In" -#define MYPLACE_EVENT_OUT "Out" - -#endif +#endif /* End of _CONTEXT_PLACE_GEOFENCE_TYPES_H_ */ diff --git a/src/place/geofence/myplace_handle.cpp b/src/place/geofence/myplace_handle.cpp deleted file mode 100644 index d7b8fcc..0000000 --- a/src/place/geofence/myplace_handle.cpp +++ /dev/null @@ -1,218 +0,0 @@ -/* - * Copyright (c) 2015 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 "place_geofence_types.h" -#include "myplace_handle.h" - -ctx::myplace_handle::myplace_handle() - : _place_id(-1) - , prev_state(GEOFENCE_STATE_UNCERTAIN) - , geo_handle(NULL) -{ -} - -ctx::myplace_handle::~myplace_handle() -{ - stop_monitor(); -} - -bool ctx::myplace_handle::start_monitor(int place_id) -{ - _D("Starts to monitor Place-%d", place_id); - - IF_FAIL_RETURN(place_id >= 0, false); - IF_FAIL_RETURN_TAG(geo_handle == NULL, false, _E, "Re-starting MyPlace monitor"); - - geofence_manager_create(&geo_handle); - IF_FAIL_RETURN_TAG(geo_handle, false, _E, "Geofence initialization failed"); - - int ret; - - ret = geofence_manager_set_geofence_state_changed_cb(geo_handle, fence_state_cb, this); - IF_FAIL_CATCH_TAG(ret == GEOFENCE_MANAGER_ERROR_NONE, _E, "Setting state callback failed"); - - ret = geofence_manager_set_geofence_event_cb(geo_handle, fence_event_cb, this); - IF_FAIL_CATCH_TAG(ret == GEOFENCE_MANAGER_ERROR_NONE, _E, "Setting event callback failed"); - - ret = geofence_manager_foreach_place_geofence_list(geo_handle, place_id, fence_list_cb, this); - IF_FAIL_CATCH_TAG(ret == GEOFENCE_MANAGER_ERROR_NONE, _E, "Getting fence list failed"); - - _place_id = place_id; - return true; - -CATCH: - stop_monitor(); - return false; -} - -int ctx::myplace_handle::get_place_id() -{ - return _place_id; -} - -void ctx::myplace_handle::stop_monitor() -{ - _D("Stops monitoring Place-%d", _place_id); - - //TODO: Do we need to stop all geofences explicitly? - if (geo_handle) { - geofence_manager_destroy(geo_handle); - geo_handle = NULL; - } - - geo_state_map.clear(); - _place_id = -1; - prev_state = GEOFENCE_STATE_UNCERTAIN; -} - -bool ctx::myplace_handle::start_fence(int fence_id) -{ - int ret; - - ret = geofence_manager_start(geo_handle, fence_id); - IF_FAIL_RETURN_TAG(ret == GEOFENCE_MANAGER_ERROR_NONE, true, _W, "Starting failed"); - - geofence_status_h status; - ret = geofence_status_create(fence_id, &status); - IF_FAIL_RETURN_TAG(ret == GEOFENCE_MANAGER_ERROR_NONE, true, _W, "Getting status failed"); - - geofence_state_e state = GEOFENCE_STATE_UNCERTAIN; - geofence_status_get_state(status, &state); - geofence_status_destroy(status); - - geo_state_map[fence_id] = state; - - return true; -} - -void ctx::myplace_handle::remove_fence(int fence_id) -{ - geofence_manager_stop(geo_handle, fence_id); - geo_state_map.erase(fence_id); -} - -void ctx::myplace_handle::update_fence(int fence_id, geofence_manage_e manage) -{ - switch (manage) { - case GEOFENCE_MANAGE_PLACE_REMOVED: - _W("[Place-%d] Removed", _place_id); - stop_monitor(); - break; - case GEOFENCE_MANAGE_FENCE_ADDED: - _I("[Place %d] Fence-%d added", _place_id, fence_id); - start_fence(fence_id); - emit_state_change(); - break; - case GEOFENCE_MANAGE_FENCE_REMOVED: - _I("[Place-%d] Fence-%d removed", _place_id, fence_id); - remove_fence(fence_id); - emit_state_change(); - break; - case GEOFENCE_MANAGE_FENCE_STARTED: - _D("[Place-%d] Fence-%d started", _place_id, fence_id); - break; - case GEOFENCE_MANAGE_FENCE_STOPPED: - _D("[Place-%d] Fence-%d stopped", _place_id, fence_id); - //TODO: Do we need to restart this? - break; - default: - _D("[Place-%d] Ignoring the manage event %d", _place_id, manage); - break; - } -} - -void ctx::myplace_handle::update_state(int fence_id, geofence_state_e state) -{ - geo_state_map[fence_id] = state; -} - -static const char* get_state_string(geofence_state_e state) -{ - switch (state) { - case GEOFENCE_STATE_IN: - return MYPLACE_EVENT_IN; - case GEOFENCE_STATE_OUT: - return MYPLACE_EVENT_OUT; - case GEOFENCE_STATE_UNCERTAIN: - return MYPLACE_EVENT_UNCERTAIN; - default: - return MYPLACE_EVENT_UNCERTAIN; - } -} - -void ctx::myplace_handle::emit_state_change() -{ - geofence_state_e current_state = GEOFENCE_STATE_UNCERTAIN; - int out_count = 0; - - for (geo_state_map_t::iterator it = geo_state_map.begin(); it != geo_state_map.end(); ++it) { - if (it->second == GEOFENCE_STATE_IN) { - current_state = GEOFENCE_STATE_IN; - break; - } else if (it->second == GEOFENCE_STATE_OUT) { - ++ out_count; - } - } - - if (current_state != GEOFENCE_STATE_IN && out_count > 0) { - current_state = GEOFENCE_STATE_OUT; - } - - if (current_state == prev_state) { - return; - } - - prev_state = current_state; - - Json option; - option.set(NULL, PLACE_STATUS_DATA_MYPLACE_ID, _place_id); - - Json data; - data.set(NULL, PLACE_STATUS_DATA_MYPLACE_ID, _place_id); - data.set(NULL, PLACE_STATUS_DATA_MYPLACE_EVENT, get_state_string(current_state)); - - context_manager::publish(PLACE_SUBJ_GEOFENCE, option, ERR_NONE, data); -} - -bool ctx::myplace_handle::fence_list_cb(int geofence_id, geofence_h fence, int fence_index, int fence_cnt, void* user_data) -{ - _D("FenceID: %d, Index: %d, Count: %d", geofence_id, fence_index, fence_cnt); - IF_FAIL_RETURN(fence_cnt > 0, false); - - myplace_handle *handle = reinterpret_cast(user_data); - return handle->start_fence(geofence_id); -} - -void ctx::myplace_handle::fence_event_cb(int place_id, int geofence_id, geofence_manager_error_e error, geofence_manage_e manage, void* user_data) -{ - IF_FAIL_VOID_TAG(error == GEOFENCE_MANAGER_ERROR_NONE, _W, "Geofence error: %d", error); - - myplace_handle *handle = reinterpret_cast(user_data); - - IF_FAIL_VOID_TAG(place_id == handle->get_place_id(), _W, "Mismatched Place ID"); - - handle->update_fence(geofence_id, manage); -} - -void ctx::myplace_handle::fence_state_cb(int geofence_id, geofence_state_e state, void* user_data) -{ - myplace_handle *handle = reinterpret_cast(user_data); - handle->update_state(geofence_id, state); - handle->emit_state_change(); -} diff --git a/src/place/geofence/myplace_handle.h b/src/place/geofence/myplace_handle.h deleted file mode 100644 index c8b4913..0000000 --- a/src/place/geofence/myplace_handle.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2015 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_PLACE_MYPLACE_HANDLE_H__ -#define __CONTEXT_PLACE_MYPLACE_HANDLE_H__ - -#include -#include -#include -#include - -namespace ctx { - - class myplace_handle { - - typedef std::map geo_state_map_t; - typedef std::set string_set_t; - - public: - myplace_handle(); - ~myplace_handle(); - - bool start_monitor(int place_id); - int get_place_id(); - - private: - int _place_id; - geofence_state_e prev_state; - geofence_manager_h geo_handle; - geo_state_map_t geo_state_map; - - void emit_state_change(); - void stop_monitor(); - bool start_fence(int fence_id); - void remove_fence(int fence_id); - void update_fence(int fence_id, geofence_manage_e manage); - void update_state(int fence_id, geofence_state_e state); - - static bool fence_list_cb(int geofence_id, geofence_h fence, int fence_index, int fence_cnt, void* user_data); - static void fence_event_cb(int place_id, int geofence_id, geofence_manager_error_e error, geofence_manage_e manage, void* user_data); - static void fence_state_cb(int geofence_id, geofence_state_e state, void* user_data); - }; - -} /* namespace ctx */ - -#endif /* __CONTEXT_PLACE_MYPLACE_HANDLE_H__ */ diff --git a/src/place/geofence/place_geofence.cpp b/src/place/geofence/place_geofence.cpp deleted file mode 100644 index 5bcfb7f..0000000 --- a/src/place/geofence/place_geofence.cpp +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Copyright (c) 2015 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 "place_geofence.h" - -ctx::place_geofence_provider *ctx::place_geofence_provider::__instance = NULL; - -ctx::place_geofence_provider::place_geofence_provider() -{ -} - -ctx::place_geofence_provider::~place_geofence_provider() -{ - for (handle_map_t::iterator it = __handle_map.begin(); it != __handle_map.end(); ++it) { - delete it->second; - } - - __handle_map.clear(); -} - -ctx::ContextProviderBase *ctx::place_geofence_provider::create(void *data) -{ - IF_FAIL_RETURN(!__instance, __instance); - __instance = new(std::nothrow) place_geofence_provider(); - IF_FAIL_RETURN_TAG(__instance, NULL, _E, "Memory allocation failed"); - _I(BLUE("Created")); - return __instance; -} - -void ctx::place_geofence_provider::destroy(void *data) -{ - IF_FAIL_VOID(__instance); - delete __instance; - __instance = NULL; - _I(BLUE("Destroyed")); -} - -bool ctx::place_geofence_provider::is_supported() -{ - bool supported = false; - int ret = geofence_manager_is_supported(&supported); - IF_FAIL_RETURN_TAG(ret == GEOFENCE_MANAGER_ERROR_NONE, false, _E, "geofence_manager_is_supported() failed"); - return supported; -} - -void ctx::place_geofence_provider::submit_trigger_item() -{ - context_manager::registerTriggerItem(PLACE_SUBJ_GEOFENCE, OPS_SUBSCRIBE, - "{" - "\"Event\":{\"type\":\"string\",\"values\":[\"In\",\"Out\"]}" - "}", - "{" - "\"PlaceId\":{\"type\":\"integer\",\"min\":1}" - "}"); -} - -void ctx::place_geofence_provider::__destroy_if_unused() -{ - IF_FAIL_VOID(__handle_map.empty()); - destroy(NULL); -} - - -int ctx::place_geofence_provider::subscribe(const char *subject, ctx::Json option, ctx::Json *requestResult) -{ - int ret = __subscribe(option); - __destroy_if_unused(); - return ret; -} - -int ctx::place_geofence_provider::unsubscribe(const char *subject, ctx::Json option) -{ - int ret = __unsubscribe(option); - __destroy_if_unused(); - return ret; -} - -int ctx::place_geofence_provider::read(const char *subject, ctx::Json option, ctx::Json *requestResult) -{ - __destroy_if_unused(); - return ERR_NOT_SUPPORTED; -} - -int ctx::place_geofence_provider::write(const char *subject, ctx::Json data, ctx::Json *requestResult) -{ - __destroy_if_unused(); - return ERR_NOT_SUPPORTED; -} - -int ctx::place_geofence_provider::__subscribe(ctx::Json option) -{ - int pid = -1; - option.get(NULL, PLACE_STATUS_OPT_MYPLACE_ID, &pid); - IF_FAIL_RETURN_TAG(pid != -1, ERR_INVALID_PARAMETER, _E, "Getting PlaceID failed"); - - handle_map_t::iterator it = __handle_map.find(pid); - if (it != __handle_map.end()) { - _D("Place ID %d is being monitored already", pid); - return ERR_NONE; - } - - myplace_handle *handle = new(std::nothrow) myplace_handle(); - ASSERT_ALLOC(handle); - - bool ret = handle->start_monitor(pid); - if (!ret) { - _E("Monitoring Place ID %d failed", pid); - delete handle; - return ERR_OPERATION_FAILED; - } - - __handle_map[pid] = handle; - - return ERR_NONE; -} - -int ctx::place_geofence_provider::__unsubscribe(ctx::Json option) -{ - int pid = -1; - option.get(NULL, PLACE_STATUS_OPT_MYPLACE_ID, &pid); - IF_FAIL_RETURN_TAG(pid != -1, ERR_INVALID_PARAMETER, _E, "Getting PlaceID failed"); - - handle_map_t::iterator it = __handle_map.find(pid); - if (it == __handle_map.end()) { - _D("Place ID %d is not being monitored", pid); - return ERR_NONE; - } - - delete it->second; - __handle_map.erase(it); - - return ERR_NONE; -} diff --git a/src/place/recognition/place_recognition.cpp b/src/place/recognition/place_recognition.cpp index 0b1b790..0088b5e 100644 --- a/src/place/recognition/place_recognition.cpp +++ b/src/place/recognition/place_recognition.cpp @@ -71,7 +71,7 @@ int ctx::place_recognition_provider::write(const char *subject, ctx::Json data, return ERR_NOT_SUPPORTED; } -bool ctx::place_recognition_provider::is_supported() +bool ctx::place_recognition_provider::isSupported() { return true; } diff --git a/src/place/recognition/place_recognition.h b/src/place/recognition/place_recognition.h index 40a93ec..14d76d9 100644 --- a/src/place/recognition/place_recognition.h +++ b/src/place/recognition/place_recognition.h @@ -28,7 +28,7 @@ namespace ctx { public: static ContextProviderBase *create(void *data); static void destroy(void *data); - static bool is_supported(); + static bool isSupported(); int subscribe(const char *subject, ctx::Json option, ctx::Json *requestResult); int unsubscribe(const char *subject, ctx::Json option); -- 2.34.1