e7aa284fb916ed4cff47d53edc58e40519753ec6
[platform/core/location/lbs-location.git] / location / manager / location-common-util.c
1 /*
2  * libslp-location
3  *
4  * Copyright (c) 2010-2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 #include "location.h"
29 #include "location-common-util.h"
30 #include "location-setting.h"
31 #include "location-log.h"
32 #include <app_manager.h>
33
34
35 int location_application_get_authority(void)
36 {
37         return LOCATION_APP_ON;
38 }
39
40 int location_application_set_authority(int auth)
41 {
42         return LOCATION_ERROR_NONE;
43 }
44
45 static gint compare_position(gconstpointer a, gconstpointer b)
46 {
47         g_return_val_if_fail(a, 1);
48         g_return_val_if_fail(b, -1);
49
50         if (location_position_equal((LocationPosition *) a, (LocationPosition *)b) == TRUE)
51                 return 0;
52
53         return -1;
54 }
55
56 static int
57 boundary_compare(gconstpointer comp1, gconstpointer comp2)
58 {
59         g_return_val_if_fail(comp1, 1);
60         g_return_val_if_fail(comp2, -1);
61
62         int ret = -1;
63
64         LocationBoundaryPrivate *priv1 = (LocationBoundaryPrivate *)comp1;
65         LocationBoundaryPrivate *priv2 = (LocationBoundaryPrivate *)comp2;
66
67         if (priv1->boundary->type == priv2->boundary->type) {
68                 switch (priv1->boundary->type) {
69                         case LOCATION_BOUNDARY_CIRCLE: {
70                                         if (location_position_equal(priv1->boundary->circle.center, priv2->boundary->circle.center)
71                                                 && priv1->boundary->circle.radius == priv2->boundary->circle.radius) {
72                                                 ret = 0;
73                                         }
74                                         break;
75                                 }
76                         case LOCATION_BOUNDARY_RECT: {
77                                         if (location_position_equal(priv1->boundary->rect.left_top, priv2->boundary->rect.left_top)
78                                                 && location_position_equal(priv1->boundary->rect.right_bottom, priv2->boundary->rect.right_bottom)) {
79                                                 ret = 0;
80                                         }
81                                         break;
82                                 }
83                         case LOCATION_BOUNDARY_POLYGON: {
84                                         GList *boundary1_next = NULL;
85                                         GList *boundary2_start = NULL, *boundary2_prev = NULL, *boundary2_next = NULL;
86                                         if (g_list_length(priv1->boundary->polygon.position_list) != g_list_length(priv2->boundary->polygon.position_list))
87                                                 return -1;
88
89                                         /* Find a matching index of Boundary2 with Boundary1's 1st postion. */
90                                         boundary2_start = g_list_find_custom(priv2->boundary->polygon.position_list, g_list_nth_data(priv1->boundary->polygon.position_list, 0), (GCompareFunc) compare_position);
91                                         if (boundary2_start == NULL) return -1;
92
93                                         boundary2_prev = g_list_previous(boundary2_start);
94                                         boundary2_next = g_list_next(boundary2_start);
95                                         if (boundary2_prev == NULL)
96                                                 boundary2_prev = g_list_last(priv2->boundary->polygon.position_list);
97
98                                         if (boundary2_next == NULL)
99                                                 boundary2_next = g_list_first(priv2->boundary->polygon.position_list);
100
101                                         boundary1_next = g_list_next(priv1->boundary->polygon.position_list);
102                                         if (boundary1_next && location_position_equal((LocationPosition *)boundary1_next->data, (LocationPosition *)boundary2_prev->data) == TRUE) {
103                                                 boundary1_next = g_list_next(boundary1_next);
104                                                 while (boundary1_next) {
105                                                         boundary2_prev = g_list_previous(boundary2_prev);
106                                                         if (boundary2_prev == NULL)
107                                                                 boundary2_prev = g_list_last(priv2->boundary->polygon.position_list);
108
109                                                         if (location_position_equal((LocationPosition *)boundary1_next->data, (LocationPosition *) boundary2_prev->data) == FALSE)
110                                                                 return -1;
111
112                                                         boundary1_next = g_list_next(boundary1_next);
113                                                 }
114                                                 ret = 0;
115                                         } else if (boundary1_next && location_position_equal((LocationPosition *)boundary1_next->data, (LocationPosition *)boundary2_next->data) == TRUE) {
116                                                 boundary1_next = g_list_next(boundary1_next);
117                                                 while (boundary1_next) {
118                                                         boundary2_next = g_list_next(boundary2_next);
119                                                         if (boundary2_next == NULL)
120                                                                 boundary2_next = g_list_first(priv2->boundary->polygon.position_list);
121
122                                                         if (location_position_equal((LocationPosition *)boundary1_next->data, (LocationPosition *) boundary2_next->data) == FALSE)
123                                                                 return -1;
124
125                                                         boundary1_next = g_list_next(boundary1_next);
126                                                 }
127                                                 ret = 0;
128                                         } else {
129                                                 return -1;
130                                         }
131                                         break;
132                                 }
133                         default: {
134                                         ret = -1;
135                                         break;
136                                 }
137
138                 }
139         }
140
141         return ret;
142 }
143
144 int set_prop_boundary(GList **prev_boundary_priv_list, GList *new_boundary_priv_list)
145 {
146         LOC_FUNC_LOG
147         g_return_val_if_fail(new_boundary_priv_list, LOCATION_ERROR_PARAMETER);
148
149         int index = 0;
150         GList *check_list = NULL;
151
152         LocationBoundaryPrivate *new_priv = NULL;
153
154         while ((new_priv = (LocationBoundaryPrivate *) g_list_nth_data(new_boundary_priv_list, index)) != NULL) {
155                 *prev_boundary_priv_list = g_list_first(*prev_boundary_priv_list);
156                 check_list = g_list_find_custom(*prev_boundary_priv_list, new_priv, (GCompareFunc)boundary_compare);
157                 if (check_list == NULL) {
158                         LocationBoundaryPrivate *copy_priv = g_slice_new0(LocationBoundaryPrivate);
159                         if (!copy_priv) break;
160                         copy_priv->boundary = location_boundary_copy(new_priv->boundary);
161                         if (!copy_priv->boundary) break;
162                         copy_priv->zone_status = new_priv->zone_status;
163                         *prev_boundary_priv_list = g_list_append(*prev_boundary_priv_list, copy_priv);
164
165                         LOCATION_LOGD("copy_priv: %p, copy_priv->boundary: %p, copy_priv->boundary->type: %d", copy_priv, copy_priv->boundary, copy_priv->boundary->type);
166                 }
167                 location_boundary_free(new_priv->boundary);
168                 index++;
169         }
170
171         return LOCATION_ERROR_NONE;
172 }
173
174 int set_prop_removal_boundary(GList **prev_boundary_list, LocationBoundary *boundary)
175 {
176         LOC_FUNC_LOG
177         g_return_val_if_fail(*prev_boundary_list, LOCATION_ERROR_PARAMETER);
178
179         GList *check_list = NULL;
180         LocationBoundaryPrivate *remove_priv = g_slice_new0(LocationBoundaryPrivate);
181         g_return_val_if_fail(remove_priv, LOCATION_ERROR_PARAMETER);
182
183         remove_priv->boundary = location_boundary_copy(boundary);
184         g_return_val_if_fail(remove_priv->boundary, LOCATION_ERROR_PARAMETER);
185
186         check_list = g_list_find_custom(*prev_boundary_list, remove_priv, (GCompareFunc) boundary_compare);
187         if (check_list) {
188                 LOCATION_LOGD("Found");
189                 *prev_boundary_list = g_list_delete_link(*prev_boundary_list, check_list);
190         }
191
192         if (g_list_length(*prev_boundary_list) == 0) {
193                 LOCATION_LOGD("Boundary List is empty");
194                 g_list_free(*prev_boundary_list);
195                 *prev_boundary_list = NULL;
196         }
197
198         location_boundary_free(remove_priv->boundary);
199         g_slice_free(LocationBoundaryPrivate, remove_priv);
200
201         LOCATION_LOGD("EXIT <<<");
202         return LOCATION_ERROR_NONE;
203 }
204
205 void free_boundary_list(gpointer data)
206 {
207         LocationBoundaryPrivate *priv = (LocationBoundaryPrivate *)data;
208
209         location_boundary_free(priv->boundary);
210         g_slice_free(LocationBoundaryPrivate, priv);
211 }
212
213 int location_get_app_type(char *target_app_id)
214 {
215         int ret = 0;
216         pid_t pid = 0;
217         char *app_id = NULL;
218         app_info_h app_info;
219         char *type = NULL;
220
221         if (target_app_id == NULL) {
222                 pid = getpid();
223                 ret = app_manager_get_app_id(pid, &app_id);
224                 LOC_COND_RET(ret != APP_MANAGER_ERROR_NONE, LOCATION_ERROR_NONE, _E, "Fail to get app_id. Err[%d]", ret);
225         } else {
226                 app_id = g_strdup(target_app_id);
227         }
228
229         ret = app_info_create(app_id, &app_info);
230         if (ret != APP_MANAGER_ERROR_NONE) {
231                 LOCATION_LOGE("Fail to get app_info. Err[%d]", ret);
232                 g_free(app_id);
233                 return LOCATION_ERROR_NONE;
234         }
235
236         ret = app_info_get_type(app_info, &type);
237         if (ret != APP_MANAGER_ERROR_NONE) {
238                 LOCATION_LOGE("Fail to get type. Err[%d]", ret);
239                 g_free(app_id);
240                 app_info_destroy(app_info);
241                 return LOCATION_ERROR_NONE;
242         }
243
244         if (strcmp(type, "c++app") == 0)
245                 ret = CPPAPP;
246         else if (strcmp(type, "webapp") == 0)
247                 ret = WEBAPP;
248         else
249                 ret = CAPP;
250
251         g_free(type);
252         g_free(app_id);
253         app_info_destroy(app_info);
254
255         return ret;
256 }
257
258 const char* err_msg(int err)
259 {
260         switch (err) {
261         case LOCATION_ERROR_NONE: return "LOCATION_ERROR_NONE";
262         case LOCATION_ERROR_NOT_ALLOWED: return "LOCATION_ERROR_NOT_ALLOWED";
263         case LOCATION_ERROR_NOT_AVAILABLE: return "LOCATION_ERROR_NOT_AVAILABLE";
264         case LOCATION_ERROR_NETWORK_FAILED: return "LOCATION_ERROR_NETWORK_FAILED";
265         case LOCATION_ERROR_NETWORK_NOT_CONNECTED: return "LOCATION_ERROR_NETWORK_NOT_CONNECTED";
266         case LOCATION_ERROR_CONFIGURATION: return "LOCATION_ERROR_CONFIGURATION";
267         case LOCATION_ERROR_PARAMETER: return "LOCATION_ERROR_PARAMETER";
268         case LOCATION_ERROR_NOT_FOUND: return "LOCATION_ERROR_NOT_FOUND";
269         case LOCATION_ERROR_NOT_SUPPORTED: return "LOCATION_ERROR_NOT_SUPPORTED";
270         case LOCATION_ERROR_UNKNOWN: return "LOCATION_ERROR_UNKNOWN";
271         case LOCATION_ERROR_SETTING_OFF: return "LOCATION_ERROR_SETTING_OFF";
272         case LOCATION_ERROR_SECURITY_DENIED: return "LOCATION_ERROR_SECURITY_DENIED";
273         default: return "LOCATION_ERROR_UNKNOWN";
274         }
275 }