1.apply WPS for geofence
[platform/core/location/geofence-server.git] / geofence-server / src / geofence_server_internal.c
1 /* Copyright 2014 Samsung Electronics Co., Ltd All Rights Reserved
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include "geofence_server.h"
17 #include "geofence_server_private.h"
18 #include "geofence_server_log.h"
19 #include "debug_util.h"
20
21 static gint __find_custom_item_by_fence_id(gconstpointer data, gconstpointer compare_with)
22 {
23         g_return_val_if_fail(data, 1);
24         g_return_val_if_fail(compare_with, -1);
25         int ret = -1;
26
27         GeofenceItemData *item_data = (GeofenceItemData *)data;
28         int *fence_id = (int *)compare_with;
29         if (item_data->common_info.fence_id == *fence_id) {
30                 LOGD_GEOFENCE("Found[%d]", *fence_id);
31                 ret = 0;
32         }
33
34         return ret;
35 }
36
37 GeofenceItemData *__get_item_by_fence_id(gint fence_id, GeofenceServer *geofence_server)
38 {
39         g_return_val_if_fail(geofence_server, NULL);
40
41         geofence_server->geofence_list = g_list_first(geofence_server->geofence_list);
42         GList *found_item = NULL;
43         found_item = g_list_find_custom(geofence_server->geofence_list, &fence_id, __find_custom_item_by_fence_id);
44         if (found_item == NULL || found_item->data == NULL) {
45                 LOGD_GEOFENCE("item_data is not found. found_item[%d]", found_item);
46                 return NULL;
47         }
48         /*Get the item from the list and return it*/
49         return (GeofenceItemData *)found_item->data;
50 }
51
52 double _get_min_distance(double cur_lat, double cur_lon, int *min_fence_id, GeofenceServer *geofence_server)
53 {
54         GList *fence_list = NULL;
55         GList *item_list = NULL;
56         int fence_id = 0;
57         GeofenceItemData *item_data = NULL;
58         geocoordinate_info_s *geocoordinate_info = NULL;
59         double min_dist = -1.0, distance = 0.0;
60
61         fence_list = geofence_server->tracking_list;
62
63         item_list = g_list_first(fence_list);
64         while (item_list) {
65                 fence_id = GPOINTER_TO_INT(item_list->data);
66                 LOGD_GEOFENCE("FENCE Id to find distance :: %d", fence_id);
67
68                 item_data = __get_item_by_fence_id(fence_id, geofence_server);
69                 if (item_data && item_data->common_info.type == GEOFENCE_TYPE_GEOPOINT) {
70                         geocoordinate_info = (geocoordinate_info_s *)item_data->priv;
71                         /* get_current_position/ check_fence_in/out  for geoPoint*/
72                         location_manager_get_distance(cur_lat, cur_lon, geocoordinate_info->latitude, geocoordinate_info->longitude, &distance);
73                         if ((min_dist == -1.0) || (distance < min_dist)) {
74                                 min_dist = distance;
75                                 *min_fence_id = fence_id;
76                         }
77                 }
78                 item_list = g_list_next(item_list);
79         }
80         LOGD_GEOFENCE("Min fence_id: %d, Min distance : %f", *min_fence_id, min_dist);
81
82         return min_dist;
83 }