Migrate from 2.4 code repo
[platform/core/context/place-context-provider.git] / src / place_geofence / place_geofence.cpp
1 /*
2  * Copyright (c) 2014 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 <map>
18 #include <geofence_manager.h>
19
20 #include <types_internal.h>
21 #include "myplace_handle.h"
22 #include "place_geofence.h"
23
24 typedef std::map<int, ctx::myplace_handle*> handle_map_t;
25 static handle_map_t handle_map;
26
27 ctx::place_geofence_detector::place_geofence_detector()
28 {
29 }
30
31 ctx::place_geofence_detector::~place_geofence_detector()
32 {
33         for (handle_map_t::iterator it = handle_map.begin(); it != handle_map.end(); ++it) {
34                 delete it->second;
35         }
36
37         handle_map.clear();
38 }
39
40 int ctx::place_geofence_detector::subscribe(ctx::json option, ctx::json* request_result, const char* zone)
41 {
42         int pid = -1;
43         option.get(NULL, PLACE_STATUS_OPT_MYPLACE_ID, &pid);
44         IF_FAIL_RETURN_TAG(pid != -1, ERR_INVALID_PARAMETER, _E, "Getting PlaceID failed");
45
46         handle_map_t::iterator it = handle_map.find(pid);
47         if (it != handle_map.end()) {
48                 _D("Place ID %d is being monitored already", pid);
49                 it->second->add_zone(zone);
50                 return ERR_NONE;
51         }
52
53         myplace_handle *handle = new(std::nothrow) myplace_handle();
54         ASSERT_ALLOC(handle);
55
56         bool ret = handle->start_monitor(pid);
57         if (!ret) {
58                 _E("Monitoring Place ID %d failed", pid);
59                 delete handle;
60                 return ERR_OPERATION_FAILED;
61         }
62
63         handle->add_zone(zone);
64         handle_map[pid] = handle;
65
66         return ERR_NONE;
67 }
68
69 int ctx::place_geofence_detector::unsubscribe(ctx::json option, const char* zone)
70 {
71         int pid = -1;
72         option.get(NULL, PLACE_STATUS_OPT_MYPLACE_ID, &pid);
73         IF_FAIL_RETURN_TAG(pid != -1, ERR_INVALID_PARAMETER, _E, "Getting PlaceID failed");
74
75         handle_map_t::iterator it = handle_map.find(pid);
76         if (it == handle_map.end()) {
77                 _D("Place ID %d is not being monitored", pid);
78                 return ERR_NONE;
79         }
80
81         it->second->remove_zone(zone);
82         if (it->second->zone_empty()) {
83                 delete it->second;
84                 handle_map.erase(it);
85         }
86
87         return ERR_NONE;
88 }
89
90 bool ctx::place_geofence_detector::is_supported(const char* zone)
91 {
92         bool supported = false;
93
94         int ret = geofence_manager_is_supported(&supported);
95         IF_FAIL_RETURN_TAG(ret == GEOFENCE_MANAGER_ERROR_NONE, false, _E, "geofence_manager_is_supported() failed");
96
97         return supported;
98 }