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