code clean up
[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 #include <stdlib.h>
31
32 #include "location.h"
33 #include "location-common-util.h"
34 #include "location-setting.h"
35 #include "location-log.h"
36 #include <app_manager.h>
37 #include <system_info.h>
38
39
40 int location_application_get_authority(void)
41 {
42         return LOCATION_APP_ON;
43 }
44
45 int location_application_set_authority(int auth)
46 {
47         return LOCATION_ERROR_NONE;
48 }
49
50 static gint compare_position(gconstpointer a, gconstpointer b)
51 {
52         g_return_val_if_fail(a, 1);
53         g_return_val_if_fail(b, -1);
54
55         if (location_position_equal((LocationPosition *) a, (LocationPosition *)b) == TRUE)
56                 return 0;
57
58         return -1;
59 }
60
61 static int
62 boundary_compare(gconstpointer comp1, gconstpointer comp2)
63 {
64         g_return_val_if_fail(comp1, 1);
65         g_return_val_if_fail(comp2, -1);
66
67         int ret = -1;
68
69         LocationBoundaryPrivate *priv1 = (LocationBoundaryPrivate *)comp1;
70         LocationBoundaryPrivate *priv2 = (LocationBoundaryPrivate *)comp2;
71
72         if (priv1->boundary->type == priv2->boundary->type) {
73                 switch (priv1->boundary->type) {
74                         case LOCATION_BOUNDARY_CIRCLE: {
75                                         if (location_position_equal(priv1->boundary->circle.center, priv2->boundary->circle.center)
76                                                 && priv1->boundary->circle.radius == priv2->boundary->circle.radius) {
77                                                 ret = 0;
78                                         }
79                                         break;
80                                 }
81                         case LOCATION_BOUNDARY_RECT: {
82                                         if (location_position_equal(priv1->boundary->rect.left_top, priv2->boundary->rect.left_top)
83                                                 && location_position_equal(priv1->boundary->rect.right_bottom, priv2->boundary->rect.right_bottom)) {
84                                                 ret = 0;
85                                         }
86                                         break;
87                                 }
88                         case LOCATION_BOUNDARY_POLYGON: {
89                                         GList *boundary1_next = NULL;
90                                         GList *boundary2_start = NULL, *boundary2_prev = NULL, *boundary2_next = NULL;
91                                         if (g_list_length(priv1->boundary->polygon.position_list) != g_list_length(priv2->boundary->polygon.position_list))
92                                                 return -1;
93
94                                         /* Find a matching index of Boundary2 with Boundary1's 1st postion. */
95                                         boundary2_start = g_list_find_custom(priv2->boundary->polygon.position_list, g_list_nth_data(priv1->boundary->polygon.position_list, 0), (GCompareFunc) compare_position);
96                                         if (boundary2_start == NULL) return -1;
97
98                                         boundary2_prev = g_list_previous(boundary2_start);
99                                         boundary2_next = g_list_next(boundary2_start);
100                                         if (boundary2_prev == NULL)
101                                                 boundary2_prev = g_list_last(priv2->boundary->polygon.position_list);
102
103                                         if (boundary2_next == NULL)
104                                                 boundary2_next = g_list_first(priv2->boundary->polygon.position_list);
105
106                                         boundary1_next = g_list_next(priv1->boundary->polygon.position_list);
107                                         if (boundary1_next && location_position_equal((LocationPosition *)boundary1_next->data, (LocationPosition *)boundary2_prev->data) == TRUE) {
108                                                 boundary1_next = g_list_next(boundary1_next);
109                                                 while (boundary1_next) {
110                                                         boundary2_prev = g_list_previous(boundary2_prev);
111                                                         if (boundary2_prev == NULL)
112                                                                 boundary2_prev = g_list_last(priv2->boundary->polygon.position_list);
113
114                                                         if (location_position_equal((LocationPosition *)boundary1_next->data, (LocationPosition *) boundary2_prev->data) == FALSE)
115                                                                 return -1;
116
117                                                         boundary1_next = g_list_next(boundary1_next);
118                                                 }
119                                                 ret = 0;
120                                         } else if (boundary1_next && location_position_equal((LocationPosition *)boundary1_next->data, (LocationPosition *)boundary2_next->data) == TRUE) {
121                                                 boundary1_next = g_list_next(boundary1_next);
122                                                 while (boundary1_next) {
123                                                         boundary2_next = g_list_next(boundary2_next);
124                                                         if (boundary2_next == NULL)
125                                                                 boundary2_next = g_list_first(priv2->boundary->polygon.position_list);
126
127                                                         if (location_position_equal((LocationPosition *)boundary1_next->data, (LocationPosition *) boundary2_next->data) == FALSE)
128                                                                 return -1;
129
130                                                         boundary1_next = g_list_next(boundary1_next);
131                                                 }
132                                                 ret = 0;
133                                         } else {
134                                                 return -1;
135                                         }
136                                         break;
137                                 }
138                         default: {
139                                         ret = -1;
140                                         break;
141                                 }
142
143                 }
144         }
145
146         return ret;
147 }
148
149 int set_prop_boundary(GList **prev_boundary_priv_list, GList *new_boundary_priv_list)
150 {
151         LOC_FUNC_LOG
152         g_return_val_if_fail(new_boundary_priv_list, LOCATION_ERROR_PARAMETER);
153
154         int index = 0;
155         GList *check_list = NULL;
156
157         LocationBoundaryPrivate *new_priv = NULL;
158
159         while ((new_priv = (LocationBoundaryPrivate *) g_list_nth_data(new_boundary_priv_list, index)) != NULL) {
160                 *prev_boundary_priv_list = g_list_first(*prev_boundary_priv_list);
161                 check_list = g_list_find_custom(*prev_boundary_priv_list, new_priv, (GCompareFunc)boundary_compare);
162                 if (check_list == NULL) {
163                         LocationBoundaryPrivate *copy_priv = g_slice_new0(LocationBoundaryPrivate);
164                         if (!copy_priv) break;
165                         copy_priv->boundary = location_boundary_copy(new_priv->boundary);
166                         if (!copy_priv->boundary) break;
167                         copy_priv->zone_status = new_priv->zone_status;
168                         *prev_boundary_priv_list = g_list_append(*prev_boundary_priv_list, copy_priv);
169
170                         LOCATION_LOGD("copy_priv: %p, copy_priv->boundary: %p, copy_priv->boundary->type: %d", copy_priv, copy_priv->boundary, copy_priv->boundary->type);
171                 }
172                 location_boundary_free(new_priv->boundary);
173                 index++;
174         }
175
176         return LOCATION_ERROR_NONE;
177 }
178
179 int set_prop_removal_boundary(GList **prev_boundary_list, LocationBoundary *boundary)
180 {
181         LOC_FUNC_LOG
182         g_return_val_if_fail(*prev_boundary_list, LOCATION_ERROR_PARAMETER);
183
184         GList *check_list = NULL;
185         LocationBoundaryPrivate *remove_priv = g_slice_new0(LocationBoundaryPrivate);
186         g_return_val_if_fail(remove_priv, LOCATION_ERROR_PARAMETER);
187
188         remove_priv->boundary = location_boundary_copy(boundary);
189         g_return_val_if_fail(remove_priv->boundary, LOCATION_ERROR_PARAMETER);
190
191         check_list = g_list_find_custom(*prev_boundary_list, remove_priv, (GCompareFunc) boundary_compare);
192         if (check_list) {
193                 LOCATION_LOGD("Found");
194                 *prev_boundary_list = g_list_delete_link(*prev_boundary_list, check_list);
195         }
196
197         if (g_list_length(*prev_boundary_list) == 0) {
198                 LOCATION_LOGD("Boundary List is empty");
199                 g_list_free(*prev_boundary_list);
200                 *prev_boundary_list = NULL;
201         }
202
203         location_boundary_free(remove_priv->boundary);
204         g_slice_free(LocationBoundaryPrivate, remove_priv);
205
206         LOCATION_LOGD("EXIT <<<");
207         return LOCATION_ERROR_NONE;
208 }
209
210 void free_boundary_list(gpointer data)
211 {
212         LocationBoundaryPrivate *priv = (LocationBoundaryPrivate *)data;
213
214         location_boundary_free(priv->boundary);
215         g_slice_free(LocationBoundaryPrivate, priv);
216 }
217
218 int location_get_app_type(char *target_app_id)
219 {
220         int ret = 0;
221         pid_t pid = 0;
222         char *app_id = NULL;
223         app_info_h app_info;
224         char *type = NULL;
225
226         if (target_app_id == NULL) {
227                 pid = getpid();
228                 ret = app_manager_get_app_id(pid, &app_id);
229                 LOC_COND_RET(ret != APP_MANAGER_ERROR_NONE, LOCATION_ERROR_NONE, _E, "Fail to get app_id. Err[%d]", ret);
230         } else {
231                 app_id = g_strdup(target_app_id);
232         }
233
234         ret = app_info_create(app_id, &app_info);
235         if (ret != APP_MANAGER_ERROR_NONE) {
236                 LOCATION_LOGE("Fail to get app_info. Err[%d]", ret);
237                 g_free(app_id);
238                 return LOCATION_ERROR_NONE;
239         }
240
241         ret = app_info_get_type(app_info, &type);
242         if (ret != APP_MANAGER_ERROR_NONE) {
243                 LOCATION_LOGE("Fail to get type. Err[%d]", ret);
244                 g_free(app_id);
245                 app_info_destroy(app_info);
246                 return LOCATION_ERROR_NONE;
247         }
248
249         if (strcmp(type, "c++app") == 0)
250                 ret = CPPAPP;
251         else if (strcmp(type, "webapp") == 0)
252                 ret = WEBAPP;
253         else
254                 ret = CAPP;
255
256         g_free(type);
257         g_free(app_id);
258         app_info_destroy(app_info);
259
260         return ret;
261 }
262
263 const char* err_msg(int err)
264 {
265         switch (err) {
266         case LOCATION_ERROR_NONE: return "LOCATION_ERROR_NONE";
267         case LOCATION_ERROR_NOT_ALLOWED: return "LOCATION_ERROR_NOT_ALLOWED";
268         case LOCATION_ERROR_NOT_AVAILABLE: return "LOCATION_ERROR_NOT_AVAILABLE";
269         case LOCATION_ERROR_NETWORK_FAILED: return "LOCATION_ERROR_NETWORK_FAILED";
270         case LOCATION_ERROR_NETWORK_NOT_CONNECTED: return "LOCATION_ERROR_NETWORK_NOT_CONNECTED";
271         case LOCATION_ERROR_CONFIGURATION: return "LOCATION_ERROR_CONFIGURATION";
272         case LOCATION_ERROR_PARAMETER: return "LOCATION_ERROR_PARAMETER";
273         case LOCATION_ERROR_NOT_FOUND: return "LOCATION_ERROR_NOT_FOUND";
274         case LOCATION_ERROR_NOT_SUPPORTED: return "LOCATION_ERROR_NOT_SUPPORTED";
275         case LOCATION_ERROR_UNKNOWN: return "LOCATION_ERROR_UNKNOWN";
276         case LOCATION_ERROR_SETTING_OFF: return "LOCATION_ERROR_SETTING_OFF";
277         case LOCATION_ERROR_SECURITY_DENIED: return "LOCATION_ERROR_SECURITY_DENIED";
278         default: return "LOCATION_ERROR_UNKNOWN";
279         }
280 }
281
282 tizen_profile_t _get_tizen_profile()
283 {
284         static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
285         if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
286                 return profile;
287
288         char *profileName;
289         system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
290         switch (*profileName) {
291         case 'm':
292         case 'M':
293                 profile = TIZEN_PROFILE_MOBILE;
294                 break;
295         case 'w':
296         case 'W':
297                 profile = TIZEN_PROFILE_WEARABLE;
298                 break;
299         case 't':
300         case 'T':
301                 profile = TIZEN_PROFILE_TV;
302                 break;
303         case 'i':
304         case 'I':
305                 profile = TIZEN_PROFILE_IVI;
306                 break;
307         default: /* common or unknown ==> ALL ARE COMMON. */
308                 profile = TIZEN_PROFILE_COMMON;
309         }
310         free(profileName);
311
312         return profile;
313 }