tizen 2.4 release
[framework/context/place-context-provider.git] / src / geofence / place_geofence.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <geofence_manager.h>
18
19 #include <types_internal.h>
20 #include <json.h>
21 #include <context_mgr.h>
22 #include "place_geofence.h"
23
24 ctx::place_geofence_provider *ctx::place_geofence_provider::__instance = NULL;
25
26 ctx::place_geofence_provider::place_geofence_provider()
27 {
28 }
29
30 ctx::place_geofence_provider::~place_geofence_provider()
31 {
32         for (handle_map_t::iterator it = __handle_map.begin(); it != __handle_map.end(); ++it) {
33                 delete it->second;
34         }
35
36         __handle_map.clear();
37 }
38
39 ctx::context_provider_iface *ctx::place_geofence_provider::create(void *data)
40 {
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");
44         _I(BLUE("Created"));
45         return __instance;
46 }
47
48 void ctx::place_geofence_provider::destroy(void *data)
49 {
50         IF_FAIL_VOID(__instance);
51         delete __instance;
52         __instance = NULL;
53         _I(BLUE("Destroyed"));
54 }
55
56 bool ctx::place_geofence_provider::is_supported()
57 {
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");
61         return supported;
62 }
63
64 void ctx::place_geofence_provider::submit_trigger_item()
65 {
66         context_manager::register_trigger_item(PLACE_SUBJ_GEOFENCE, OPS_SUBSCRIBE,
67                         "{"
68                                 "\"Event\":{\"type\":\"string\",\"values\":[\"In\",\"Out\"]}"
69                         "}",
70                         "{"
71                                 "\"PlaceId\":{\"type\":\"integer\",\"min\":1}"
72                         "}");
73 }
74
75 void ctx::place_geofence_provider::__destroy_if_unused()
76 {
77         IF_FAIL_VOID(__handle_map.empty());
78         destroy(NULL);
79 }
80
81
82 int ctx::place_geofence_provider::subscribe(const char *subject, ctx::json option, ctx::json *request_result)
83 {
84         int ret = __subscribe(option);
85         __destroy_if_unused();
86         return ret;
87 }
88
89 int ctx::place_geofence_provider::unsubscribe(const char *subject, ctx::json option)
90 {
91         int ret = __unsubscribe(option);
92         __destroy_if_unused();
93         return ret;
94 }
95
96 int ctx::place_geofence_provider::read(const char *subject, ctx::json option, ctx::json *request_result)
97 {
98         __destroy_if_unused();
99         return ERR_NOT_SUPPORTED;
100 }
101
102 int ctx::place_geofence_provider::write(const char *subject, ctx::json data, ctx::json *request_result)
103 {
104         __destroy_if_unused();
105         return ERR_NOT_SUPPORTED;
106 }
107
108 int ctx::place_geofence_provider::__subscribe(ctx::json option)
109 {
110         int pid = -1;
111         option.get(NULL, PLACE_STATUS_OPT_MYPLACE_ID, &pid);
112         IF_FAIL_RETURN_TAG(pid != -1, ERR_INVALID_PARAMETER, _E, "Getting PlaceID failed");
113
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);
117                 return ERR_NONE;
118         }
119
120         myplace_handle *handle = new(std::nothrow) myplace_handle();
121         ASSERT_ALLOC(handle);
122
123         bool ret = handle->start_monitor(pid);
124         if (!ret) {
125                 _E("Monitoring Place ID %d failed", pid);
126                 delete handle;
127                 return ERR_OPERATION_FAILED;
128         }
129
130         __handle_map[pid] = handle;
131
132         return ERR_NONE;
133 }
134
135 int ctx::place_geofence_provider::__unsubscribe(ctx::json option)
136 {
137         int pid = -1;
138         option.get(NULL, PLACE_STATUS_OPT_MYPLACE_ID, &pid);
139         IF_FAIL_RETURN_TAG(pid != -1, ERR_INVALID_PARAMETER, _E, "Getting PlaceID failed");
140
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);
144                 return ERR_NONE;
145         }
146
147         delete it->second;
148         __handle_map.erase(it);
149
150         return ERR_NONE;
151 }