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