2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <geofence_manager.h>
19 #include <types_internal.h>
21 #include <context_mgr.h>
22 #include "place_geofence.h"
24 ctx::place_geofence_provider *ctx::place_geofence_provider::__instance = NULL;
26 ctx::place_geofence_provider::place_geofence_provider()
30 ctx::place_geofence_provider::~place_geofence_provider()
32 for (handle_map_t::iterator it = __handle_map.begin(); it != __handle_map.end(); ++it) {
39 ctx::context_provider_iface *ctx::place_geofence_provider::create(void *data)
41 IF_FAIL_RETURN(!__instance, __instance);
42 __instance = new(std::nothrow) place_geofence_provider();
43 IF_FAIL_RETURN_TAG(__instance, NULL, _E, "Memory allocation failed");
48 void ctx::place_geofence_provider::destroy(void *data)
50 IF_FAIL_VOID(__instance);
53 _I(BLUE("Destroyed"));
56 bool ctx::place_geofence_provider::is_supported()
58 bool supported = false;
59 int ret = geofence_manager_is_supported(&supported);
60 IF_FAIL_RETURN_TAG(ret == GEOFENCE_MANAGER_ERROR_NONE, false, _E, "geofence_manager_is_supported() failed");
64 void ctx::place_geofence_provider::submit_trigger_item()
66 context_manager::register_trigger_item(PLACE_SUBJ_GEOFENCE, OPS_SUBSCRIBE,
68 "\"Event\":{\"type\":\"string\",\"values\":[\"In\",\"Out\"]}"
71 "\"PlaceId\":{\"type\":\"integer\",\"min\":1}"
75 void ctx::place_geofence_provider::__destroy_if_unused()
77 IF_FAIL_VOID(__handle_map.empty());
82 int ctx::place_geofence_provider::subscribe(const char *subject, ctx::json option, ctx::json *request_result)
84 int ret = __subscribe(option);
85 __destroy_if_unused();
89 int ctx::place_geofence_provider::unsubscribe(const char *subject, ctx::json option)
91 int ret = __unsubscribe(option);
92 __destroy_if_unused();
96 int ctx::place_geofence_provider::read(const char *subject, ctx::json option, ctx::json *request_result)
98 __destroy_if_unused();
99 return ERR_NOT_SUPPORTED;
102 int ctx::place_geofence_provider::write(const char *subject, ctx::json data, ctx::json *request_result)
104 __destroy_if_unused();
105 return ERR_NOT_SUPPORTED;
108 int ctx::place_geofence_provider::__subscribe(ctx::json option)
111 option.get(NULL, PLACE_STATUS_OPT_MYPLACE_ID, &pid);
112 IF_FAIL_RETURN_TAG(pid != -1, ERR_INVALID_PARAMETER, _E, "Getting PlaceID failed");
114 handle_map_t::iterator it = __handle_map.find(pid);
115 if (it != __handle_map.end()) {
116 _D("Place ID %d is being monitored already", pid);
120 myplace_handle *handle = new(std::nothrow) myplace_handle();
121 ASSERT_ALLOC(handle);
123 bool ret = handle->start_monitor(pid);
125 _E("Monitoring Place ID %d failed", pid);
127 return ERR_OPERATION_FAILED;
130 __handle_map[pid] = handle;
135 int ctx::place_geofence_provider::__unsubscribe(ctx::json option)
138 option.get(NULL, PLACE_STATUS_OPT_MYPLACE_ID, &pid);
139 IF_FAIL_RETURN_TAG(pid != -1, ERR_INVALID_PARAMETER, _E, "Getting PlaceID failed");
141 handle_map_t::iterator it = __handle_map.find(pid);
142 if (it == __handle_map.end()) {
143 _D("Place ID %d is not being monitored", pid);
148 __handle_map.erase(it);