change tab to space
[platform/core/location/geofence-server.git] / geofence-server / src / geofence_server.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 <stdio.h>
17 #include <string.h>
18 #include <time.h>
19 #include <stdlib.h>
20 #include <glib.h>
21 #include <glib-object.h>
22 #include <geofence_client.h>
23 #include <dd-display.h>
24 #include <vconf.h>
25 #include <vconf-internal-wifi-keys.h>
26 #include <vconf-internal-dnet-keys.h>
27 #include <vconf-internal-location-keys.h>
28 #include "geofence_server_data_types.h"
29 #include "geofence_server.h"
30 #include "server.h"
31 #include "debug_util.h"
32 #include "geofence_server_db.h"
33 #include "geofence_server_private.h"
34 #include "geofence_server_wifi.h"
35 #include "geofence_server_alarm.h"
36 #include "geofence_server_internal.h"
37 #include "geofence_server_bluetooth.h"
38 #include <wifi.h>
39 #include <network-wifi-intf.h>
40 #include <system_info.h>
41
42 #define TIZEN_ENGINEER_MODE
43 #ifdef TIZEN_ENGINEER_MODE
44 #include "geofence_server_log.h"
45 #endif
46
47 #define WPS_ACCURACY_TOLERANCE  100
48 #define GPS_TRIGGER_BOUNDARY 1000
49 #define SMART_ASSIST_HOME       1
50 #define SMART_ASSIST_TIMEOUT                    60      /* Refer to LPP */
51 #define GEOFENCE_DEFAULT_RADIUS                 200     /* Default 200 m */
52 #define GPS_TIMEOUT                             60
53 #define WPS_TIMEOUT                             60
54
55 #define MYPLACES_APPID  "org.tizen.myplace"
56 #define DEFAULT_PLACE_HOME 1
57 #define DEFAULT_PLACE_OFFICE 2
58 #define DEFAULT_PLACE_CAR 3
59
60 static int __emit_fence_proximity(GeofenceServer *geofence_server, int fence_id, geofence_proximity_state_e state);
61 static int __gps_alarm_cb(alarm_id_t alarm_id, void *user_data);
62 static int __wps_alarm_cb(alarm_id_t alarm_id, void *user_data);
63 static int __gps_timeout_cb(alarm_id_t alarm_id, void *user_data);
64 static int __wps_timeout_cb(alarm_id_t alarm_id, void *user_data);
65 static void __add_left_fences(gpointer user_data);
66 static void __start_activity_service(GeofenceServer *geofence_server);
67 static void __stop_activity_service(GeofenceServer *geofence_server);
68 static void __set_interval_for_gps(double min_distance, int min_fence_id, void *user_data);
69 static void __activity_cb(activity_type_e type, const activity_data_h data, double timestamp, activity_error_e error, void *user_data);
70 static bool __isWifiOn(void);
71 static bool __isDataConnected(void);
72
73 static bool __is_support_wps()
74 {
75         const char *wps_feature = "http://tizen.org/feature/location.wps";
76         bool is_wps_supported = false;
77         system_info_get_platform_bool(wps_feature, &is_wps_supported);
78         if (is_wps_supported == true)
79                 location_manager_is_enabled_method(LOCATIONS_METHOD_WPS, &is_wps_supported);
80
81         return is_wps_supported;
82 }
83
84 static const char *__convert_wifi_error_to_string(wifi_error_e err_type)
85 {
86         switch (err_type) {
87         case WIFI_ERROR_NONE:
88                 return "NONE";
89         case WIFI_ERROR_INVALID_PARAMETER:
90                 return "INVALID_PARAMETER";
91         case WIFI_ERROR_OUT_OF_MEMORY:
92                 return "OUT_OF_MEMORY";
93         case WIFI_ERROR_INVALID_OPERATION:
94                 return "INVALID_OPERATION";
95         case WIFI_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
96                 return "ADDRESS_FAMILY_NOT_SUPPORTED";
97         case WIFI_ERROR_OPERATION_FAILED:
98                 return "OPERATION_FAILED";
99         case WIFI_ERROR_NO_CONNECTION:
100                 return "NO_CONNECTION";
101         case WIFI_ERROR_NOW_IN_PROGRESS:
102                 return "NOW_IN_PROGRESS";
103         case WIFI_ERROR_ALREADY_EXISTS:
104                 return "ALREADY_EXISTS";
105         case WIFI_ERROR_OPERATION_ABORTED:
106                 return "OPERATION_ABORTED";
107         case WIFI_ERROR_DHCP_FAILED:
108                 return "DHCP_FAILED";
109         case WIFI_ERROR_INVALID_KEY:
110                 return "INVALID_KEY";
111         case WIFI_ERROR_NO_REPLY:
112                 return "NO_REPLY";
113         case WIFI_ERROR_SECURITY_RESTRICTED:
114                 return "SECURITY_RESTRICTED";
115         case WIFI_ERROR_PERMISSION_DENIED:
116                 return "PERMISSION_DENIED";
117         default:
118                 return "NOT Defined";
119         }
120 }
121
122 void emit_proximity_using_ble(GeofenceServer *geofence_server, int fence_id, geofence_proximity_state_e state)
123 {
124         FUNC_ENTRANCE_SERVER;
125         g_return_if_fail(geofence_server);
126         GeofenceItemData *item_data = __get_item_by_fence_id(fence_id, geofence_server);
127         if (item_data) {
128                 if (item_data->common_info.proximity_status != state) {
129                         LOGI_GEOFENCE("Emitting proximity status(fence: %d): %d", fence_id, state);
130                         geofence_dbus_server_send_geofence_proximity_changed(geofence_server->geofence_dbus_server, item_data->common_info.appid, fence_id, item_data->common_info.access_type, state, GEOFENCE_PROXIMITY_PROVIDER_BLE);
131                         item_data->common_info.proximity_status = state;
132                 }
133         } else {
134                 LOGD_GEOFENCE("Invalid item_data");
135         }
136 }
137
138 static bool __check_for_match(char *str1, char *str2)
139 {
140         if (g_strrstr(str1, str2) == NULL)
141                 return false;
142         return true;
143 }
144
145 static void bt_le_scan_result_cb(int result, bt_adapter_le_device_scan_result_info_s *info, void *user_data)
146 {
147         int ret = BT_ERROR_NONE;
148         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
149         LOGI_GEOFENCE("Current addresses: %s", geofence_server->ble_info);
150
151         if (info == NULL) {
152                 LOGI_GEOFENCE("Stopping scan as there is no BLE addresses found");
153                 ret = bt_adapter_le_stop_scan();
154                 if (ret != BT_ERROR_NONE)
155                         LOGE_GEOFENCE("Unable to stop the BLE scan, error: %d", ret);
156                 return;
157         }
158         LOGI_GEOFENCE("Received address: %s", info->remote_address);
159
160         /* Retrieve the information about the AP */
161         if (!g_ascii_strcasecmp(geofence_server->ble_info, "")) {
162                 g_stpcpy(geofence_server->ble_info, info->remote_address);
163         } else if (!__check_for_match(geofence_server->ble_info, info->remote_address)) { /* If duplicate does not exist */
164                 char *p = g_strjoin(";", geofence_server->ble_info, info->remote_address, NULL);
165                 g_stpcpy(geofence_server->ble_info, p);
166                 g_free(p);
167         } else {
168                 LOGI_GEOFENCE("Stopping scan. Address: %s already exist in the string %s", info->remote_address, geofence_server->ble_info);
169                 ret = bt_adapter_le_stop_scan();
170                 if (ret != BT_ERROR_NONE)
171                         LOGE_GEOFENCE("Unable to stop the BLE scan, error: %d", ret);
172                 /* Add the string to the database. */
173                 LOGI_GEOFENCE("Writing address: %s to DB for fence: %d", geofence_server->ble_info, geofence_server->nearestTrackingFence);
174                 geofence_manager_set_ble_info_to_geofence(geofence_server->nearestTrackingFence, geofence_server->ble_info);
175         }
176 }
177
178 void bt_le_scan_result_display_cb(int result, bt_adapter_le_device_scan_result_info_s *info, void *user_data)
179 {
180         FUNC_ENTRANCE_SERVER;
181         int ret = BT_ERROR_NONE;
182         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
183         int wps_state = 0;
184         int gps_state = 0;
185         int fence_id = -1;
186         char *ble_info = NULL;
187         ble_mode_e ble_proximity_mode = BLE_INFO_NONE;
188         GList *tracking_list = g_list_first(geofence_server->tracking_list);
189         if (info == NULL) {
190                 LOGI_GEOFENCE("Stopping scan as there is no BLE addresses found");
191                 ret = bt_adapter_le_stop_scan();
192                 if (ret != BT_ERROR_NONE)
193                         LOGE_GEOFENCE("Unable to stop the BLE scan, error: %d", ret);
194                 return;
195         }
196         if (!g_ascii_strcasecmp(geofence_server->ble_info, "")) {
197                 g_stpcpy(geofence_server->ble_info, info->remote_address);
198         } else if (!__check_for_match(geofence_server->ble_info, info->remote_address)) { /* If duplicate does not exist */
199                 char *p = g_strjoin(";", geofence_server->ble_info, info->remote_address, NULL);
200                 g_stpcpy(geofence_server->ble_info, p);
201                 g_free(p);
202         } else {
203                 LOGI_GEOFENCE("Stopping scan. Address: %s already exist in the string %s", info->remote_address, geofence_server->ble_info);
204                 ret = bt_adapter_le_stop_scan();
205                 if (ret != BT_ERROR_NONE)
206                         LOGE_GEOFENCE("Unable to stop the BLE scan, error: %d", ret);
207                 return;
208         }
209         if (tracking_list) {
210                 while (tracking_list) {
211                         ble_proximity_mode = BLE_INFO_NONE;
212                         fence_id = GPOINTER_TO_INT(tracking_list->data);
213                         GeofenceItemData *item_data = __get_item_by_fence_id(fence_id, geofence_server);
214                         if (item_data == NULL) {
215                                 LOGD_GEOFENCE("Invalid item_data");
216                                 return;
217                         }
218                         if (item_data->common_info.type == GEOFENCE_TYPE_GEOPOINT) {
219                                 vconf_get_int(VCONFKEY_LOCATION_NETWORK_ENABLED, &wps_state);
220                                 vconf_get_int(VCONFKEY_LOCATION_ENABLED, &gps_state);
221                                 if (wps_state == 0 && gps_state == 0)
222                                         ble_proximity_mode = BLE_INFO_READ;
223                         } else if (item_data->common_info.type == GEOFENCE_TYPE_WIFI) {
224                                 if (__isWifiOn() == false)
225                                         ble_proximity_mode = BLE_INFO_READ;
226                         } else if (item_data->common_info.type == GEOFENCE_TYPE_BT) {
227                                 bssid_info_s *bt_info = NULL;
228                                 bt_info = (bssid_info_s *) item_data->priv;
229                                 bt_device_info_s *bt_device_info = NULL;
230                                 if (bt_info != NULL) {
231                                         ret = bt_adapter_get_bonded_device_info(bt_info->bssid, &bt_device_info);
232                                         if (ret == BT_ERROR_NONE) {
233                                                 if (bt_device_info->is_connected == false)
234                                                         ble_proximity_mode = BLE_INFO_READ;
235                                         } else if (ret == BT_ERROR_REMOTE_DEVICE_NOT_BONDED) {
236                                                 ble_proximity_mode = BLE_INFO_READ; /*Its not bonded*/
237                                         }
238                                 }
239                         }
240                         if (ble_proximity_mode == BLE_INFO_READ) {
241                                 geofence_manager_get_ble_info_from_geofence(fence_id, &ble_info);
242                                 LOGI_GEOFENCE("Ble info read from DB: %s", ble_info);
243                                 if (__check_for_match(ble_info, info->remote_address)) {
244                                         LOGI_GEOFENCE("Matched for ble address: %s for the fence: %d", info->remote_address, fence_id);
245                                         emit_proximity_using_ble(geofence_server, fence_id, GEOFENCE_PROXIMITY_IMMEDIATE);
246                                 }
247                         }
248                         tracking_list = g_list_next(tracking_list);
249                 }
250         }
251 }
252
253 void device_display_changed_cb(device_callback_e type, void *value, void *user_data)
254 {
255         FUNC_ENTRANCE_SERVER;
256         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
257         int ret = BT_ERROR_NONE;
258         GList *tracking_list = g_list_first(geofence_server->tracking_list);
259         if (tracking_list) {
260                 if (type == DEVICE_CALLBACK_DISPLAY_STATE) {
261                         display_state_e state = (display_state_e)value;
262                         if (state == DISPLAY_STATE_NORMAL) {
263                                 LOGI_GEOFENCE("State: NORMAL");
264                                 if (tracking_list) {
265                                         LOGD_GEOFENCE("Scanning for BLE and read DB");
266                                         g_stpcpy(geofence_server->ble_info, "");
267                                         ret = bt_adapter_le_start_scan(bt_le_scan_result_display_cb, geofence_server);
268                                         if (ret != BT_ERROR_NONE)
269                                                 LOGE_GEOFENCE("Fail to start ble scan. %d", ret);
270
271                                 }
272                         } else if (state == DISPLAY_STATE_SCREEN_DIM)
273                                 LOGI_GEOFENCE("State: DIM");
274                         else
275                                 LOGI_GEOFENCE("State: OFF");
276                 }
277         }
278 }
279
280 static int __emit_fence_event(GeofenceServer *geofence_server, int place_id, int fence_id, access_type_e access_type, const gchar *app_id, geofence_server_error_e error, geofence_manage_e state)
281 {
282         FUNC_ENTRANCE_SERVER;
283         g_return_val_if_fail(geofence_server, -1);
284
285         LOGD_GEOFENCE("place_id: %d, fence_id: %d, access_type: %d, error: %d, state: %d", place_id, fence_id, access_type, error, state);
286
287         geofence_dbus_server_send_geofence_event_changed(geofence_server->geofence_dbus_server, place_id, fence_id, access_type, app_id, error, state);
288         return 0;
289 }
290
291 static int __emit_fence_inout(GeofenceServer *geofence_server, int fence_id, geofence_fence_state_e state)
292 {
293         FUNC_ENTRANCE_SERVER;
294         g_return_val_if_fail(geofence_server, -1);
295         int ret = 0;
296
297         LOGD_GEOFENCE("FenceId[%d], state[%d]", fence_id, state);
298         GeofenceItemData *item_data = __get_item_by_fence_id(fence_id, geofence_server);
299         if (item_data == NULL) {
300                 LOGD_GEOFENCE("Invalid item_data");
301                 return -1;
302         }
303
304         if (state == GEOFENCE_FENCE_STATE_IN) {
305                 /*LOGD_GEOFENCE("FENCE_IN to be set, current state: %d", item_data->common_info.status);*/
306                 if (item_data->common_info.status != GEOFENCE_FENCE_STATE_IN) {
307                         geofence_dbus_server_send_geofence_inout_changed(geofence_server->geofence_dbus_server, item_data->common_info.appid, fence_id, item_data->common_info.access_type, GEOFENCE_EMIT_STATE_IN);
308                         if (item_data->client_status == GEOFENCE_CLIENT_STATUS_START)
309                                 item_data->client_status = GEOFENCE_CLIENT_STATUS_RUNNING;
310
311                         LOGD_GEOFENCE("%d : FENCE_IN", fence_id);
312 #ifdef TIZEN_ENGINEER_MODE
313                         GEOFENCE_PRINT_LOG("FENCE_IN");
314 #endif
315                 }
316
317                 item_data->common_info.status = GEOFENCE_FENCE_STATE_IN;
318
319         } else if (state == GEOFENCE_FENCE_STATE_OUT) {
320                 /*LOGD_GEOFENCE("FENCE_OUT to be set, current state: %d", item_data->common_info.status);*/
321                 if (item_data->common_info.status != GEOFENCE_FENCE_STATE_OUT) {
322                         geofence_dbus_server_send_geofence_inout_changed(geofence_server->geofence_dbus_server, item_data->common_info.appid, fence_id, item_data->common_info.access_type, GEOFENCE_EMIT_STATE_OUT);
323                         __emit_fence_proximity(geofence_server, fence_id, GEOFENCE_PROXIMITY_UNCERTAIN);
324                         if (item_data->client_status == GEOFENCE_CLIENT_STATUS_START)
325                                 item_data->client_status = GEOFENCE_CLIENT_STATUS_RUNNING;
326
327                         LOGD_GEOFENCE("%d : FENCE_OUT", fence_id);
328 #ifdef TIZEN_ENGINEER_MODE
329                         GEOFENCE_PRINT_LOG("FENCE_OUT");
330 #endif
331                 } else {
332                         LOGD_GEOFENCE("Fence status [%d]", item_data->common_info.status);
333                 }
334                 item_data->common_info.status = GEOFENCE_FENCE_STATE_OUT;
335         } else {
336                 LOGD_GEOFENCE("Not emit, Prev[%d], Curr[%d]", item_data->common_info.status, state);
337         }
338
339         return ret;
340 }
341
342 static int __emit_fence_proximity(GeofenceServer *geofence_server, int fence_id, geofence_proximity_state_e state)
343 {
344         FUNC_ENTRANCE_SERVER;
345         g_return_val_if_fail(geofence_server, -1);
346         geofence_proximity_provider_e provider = GEOFENCE_PROXIMITY_PROVIDER_LOCATION;
347         GeofenceItemData *item_data = __get_item_by_fence_id(fence_id, geofence_server);
348         if (item_data) {
349                 if (item_data->common_info.type == GEOFENCE_TYPE_WIFI)
350                         provider = GEOFENCE_PROXIMITY_PROVIDER_WIFI;
351                 else if (item_data->common_info.type == GEOFENCE_TYPE_BT)
352                         provider = GEOFENCE_PROXIMITY_PROVIDER_BLUETOOTH;
353
354                 geofence_dbus_server_send_geofence_proximity_changed(geofence_server->geofence_dbus_server, item_data->common_info.appid, fence_id, item_data->common_info.access_type, state, provider);
355                 item_data->common_info.proximity_status = state;
356         } else {
357                 LOGD_GEOFENCE("Invalid item_data");
358                 return -1;
359         }
360         return 0;
361 }
362
363 static void __check_proximity_for_fence(double distance, int fence_id, int radius, geofence_proximity_state_e current_state, void *user_data)
364 {
365         FUNC_ENTRANCE_SERVER;
366         int ret = BT_ERROR_NONE;
367         geofence_proximity_state_e state = GEOFENCE_PROXIMITY_UNCERTAIN;
368         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
369         if (distance <= 50.0)
370                 state = GEOFENCE_PROXIMITY_IMMEDIATE;
371         else if (distance > 50.0 && distance <= 100.0)
372                 state = GEOFENCE_PROXIMITY_NEAR;
373         else if (distance > 100.0 && distance <= radius)
374                 state = GEOFENCE_PROXIMITY_FAR;
375
376         if (current_state != state) {
377                 LOGD_GEOFENCE("PROXIMITY ALERTING for fence: %d, alert: %d, distance: %f", fence_id, state, distance);
378                 __emit_fence_proximity(geofence_server, fence_id, state);
379                 if (geofence_server->nearestTrackingFence == fence_id) {
380                         if (state == GEOFENCE_PROXIMITY_IMMEDIATE) {
381                                 LOGD_GEOFENCE("Scanning for BLE and storing in DB");
382                                 g_stpcpy(geofence_server->ble_info, "");
383                                 ret = bt_adapter_le_start_scan(bt_le_scan_result_cb, geofence_server);
384                                 if (ret != BT_ERROR_NONE)
385                                         LOGE_GEOFENCE("Fail to start ble scan. %d", ret);
386                         } else if (current_state == GEOFENCE_PROXIMITY_IMMEDIATE) { /* Stopping the scan if state changes */
387                                 ret = bt_adapter_le_stop_scan();
388                                 if (ret != BT_ERROR_NONE)
389                                         LOGE_GEOFENCE("Unable to stop the BLE scan/ Stopped already, error: %d", ret);
390                         }
391                 }
392         }
393 }
394
395 static void __check_inout_by_gps(double latitude, double longitude, int fence_id, void *user_data)
396 {
397         FUNC_ENTRANCE_SERVER;
398         double distance = 0.0;
399         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
400         GeofenceItemData *item_data = __get_item_by_fence_id(fence_id, geofence_server);
401         if (!item_data || item_data->client_status == GEOFENCE_CLIENT_STATUS_NONE)
402                 return;
403
404         geofence_fence_state_e status = GEOFENCE_FENCE_STATE_OUT;
405         geocoordinate_info_s *geocoordinate_info = (geocoordinate_info_s *)item_data->priv;
406         /* get_current_position/ check_fence_in/out  for geoPoint */
407         location_manager_get_distance(latitude, longitude, geocoordinate_info->latitude, geocoordinate_info->longitude, &distance);
408
409         if (distance >= geocoordinate_info->radius) {
410                 LOGD_GEOFENCE("FENCE_OUT : based on distance. Distance[%f] for fence id: %d at (%f, %f)", distance, fence_id, latitude, longitude);
411                 status = GEOFENCE_FENCE_STATE_OUT;
412         } else {
413                 LOGD_GEOFENCE("FENCE_IN : based on distance. Distance[%f] for fence id: %d at (%f, %f)", distance, fence_id, latitude, longitude);
414                 status = GEOFENCE_FENCE_STATE_IN;
415         }
416
417         /* Alert for the proximity */
418         /*if (status == GEOFENCE_FENCE_STATE_IN) {*/
419                 __check_proximity_for_fence(distance, item_data->common_info.fence_id, geocoordinate_info->radius, item_data->common_info.proximity_status, geofence_server);
420         /*}*/
421
422         if (__emit_fence_inout(geofence_server, item_data->common_info.fence_id, status) == 0 && status == GEOFENCE_FENCE_STATE_IN)
423                 LOGD_GEOFENCE("Disable timer");
424 }
425
426 static void __stop_gps_alarms(void *user_data)
427 {
428         FUNC_ENTRANCE_SERVER;
429         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
430         /*Stop the gps interval alarm if it is running...*/
431         if (geofence_server->gps_alarm_id != -1) {
432                 /*LOGI_GEOFENCE("GPS interval timer removed. ID[%d]", geofence_server->gps_alarm_id);*/
433                 geofence_server->gps_alarm_id = _geofence_remove_alarm(geofence_server->gps_alarm_id);
434         }
435         /*Stop the timeout alarm if it is running...*/
436         if (geofence_server->gps_timeout_alarm_id != -1) {
437                 /*LOGI_GEOFENCE("Timeout timer removed for gps. ID[%d]", geofence_server->gps_timeout_alarm_id);*/
438                 geofence_server->gps_timeout_alarm_id = _geofence_remove_alarm(geofence_server->gps_timeout_alarm_id);
439         }
440 }
441
442 static void __stop_wps_alarms(void *user_data)
443 {
444         FUNC_ENTRANCE_SERVER;
445         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
446         /*Stop the wps interval alarm if it is running...*/
447         if (geofence_server->wps_alarm_id != -1) {
448                 /*LOGI_GEOFENCE("WPS interval timer removed. ID[%d]", geofence_server->wps_alarm_id);*/
449                 geofence_server->wps_alarm_id = _geofence_remove_alarm(geofence_server->wps_alarm_id);
450         }
451         /*Stop the timeout alarm if it is running...*/
452         if (geofence_server->wps_timeout_alarm_id != -1) {
453                 /*LOGI_GEOFENCE("Timeout timer removed for wps. ID[%d]", geofence_server->wps_timeout_alarm_id);*/
454                 geofence_server->wps_timeout_alarm_id = _geofence_remove_alarm(geofence_server->wps_timeout_alarm_id);
455         }
456 }
457
458 static void __check_current_location_cb(double latitude, double longitude, double altitude, time_t timestamp, void *user_data)
459 {
460         FUNC_ENTRANCE_SERVER;
461         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
462         int fence_id = 0;
463         GList *tracking_list = NULL;
464         GeofenceItemData *item_data = NULL;
465
466         LOGD_GEOFENCE("Traversing the tracking list");
467         tracking_list = g_list_first(geofence_server->tracking_list);
468         LOGD_GEOFENCE("Got the first element in tracking list");
469
470         while (tracking_list) {
471                 fence_id = GPOINTER_TO_INT(tracking_list->data);
472                 item_data = __get_item_by_fence_id(fence_id, geofence_server);
473
474                 if (item_data != NULL) {
475                         if (item_data->common_info.type == GEOFENCE_TYPE_GEOPOINT) {
476                                 LOGD_GEOFENCE("TRACKING FENCE ID :: %d", fence_id);
477                                 __check_inout_by_gps(latitude, longitude, fence_id, geofence_server);
478                         }
479                 }
480                 tracking_list = g_list_next(tracking_list);
481         }
482 }
483
484 static int __start_wps_positioning(GeofenceServer *geofence_server, location_position_updated_cb callback)
485 {
486         FUNC_ENTRANCE_SERVER;
487         g_return_val_if_fail(geofence_server, -1);
488         int ret = FENCE_ERR_NONE;
489
490         if (geofence_server->loc_wps_manager == NULL) {
491                 ret = location_manager_create(LOCATIONS_METHOD_WPS, &geofence_server->loc_wps_manager);
492                 if (ret != LOCATIONS_ERROR_NONE) {
493                         LOGD_GEOFENCE("Fail to create location_manager_h for wps: %d", ret);
494                         return FENCE_ERR_UNKNOWN;
495                 }
496         }
497         if (geofence_server->loc_wps_started == FALSE) {
498                 ret = location_manager_set_position_updated_cb(geofence_server->loc_wps_manager, callback, 1, (void *) geofence_server);
499                 if (ret != LOCATIONS_ERROR_NONE) {
500                         LOGD_GEOFENCE("Fail to set callback for wps. %d", ret);
501                         return FENCE_ERR_UNKNOWN;
502                 }
503                 ret = location_manager_start(geofence_server->loc_wps_manager);
504                 if (ret != LOCATIONS_ERROR_NONE) {
505                         LOGD_GEOFENCE("Fail to start. %d", ret);
506                         location_manager_unset_position_updated_cb(geofence_server->loc_wps_manager);
507                         location_manager_destroy(geofence_server->loc_wps_manager);
508                         geofence_server->loc_wps_manager = NULL;
509                         return FENCE_ERR_UNKNOWN;
510                 }
511                 if (geofence_server->wps_timeout_alarm_id == -1)
512                         geofence_server->wps_timeout_alarm_id = _geofence_add_alarm(WPS_TIMEOUT, __wps_timeout_cb, geofence_server);
513
514                 geofence_server->loc_wps_started = TRUE;
515         } else {
516                 LOGD_GEOFENCE("loc_wps_started TRUE");
517         }
518
519         return ret;
520 }
521
522 static int __start_gps_positioning(GeofenceServer *geofence_server, location_position_updated_cb callback)
523 {
524         FUNC_ENTRANCE_SERVER;
525         g_return_val_if_fail(geofence_server, -1);
526         int ret = FENCE_ERR_NONE;
527
528         if (geofence_server->loc_gps_manager == NULL) {
529                 ret = location_manager_create(LOCATIONS_METHOD_GPS, &geofence_server->loc_gps_manager);
530                 if (ret != LOCATIONS_ERROR_NONE) {
531                         LOGD_GEOFENCE("Fail to create location_manager_h: %d", ret);
532                         return FENCE_ERR_UNKNOWN;
533                 }
534         }
535
536         if (geofence_server->loc_gps_started == FALSE) {
537                 ret = location_manager_set_position_updated_cb(geofence_server->loc_gps_manager, callback, geofence_server->gps_trigger_interval, (void *) geofence_server);
538                 if (ret != LOCATIONS_ERROR_NONE) {
539                         LOGD_GEOFENCE("Fail to set callback. %d", ret);
540                         return FENCE_ERR_UNKNOWN;
541                 }
542
543                 ret = location_manager_start(geofence_server->loc_gps_manager);
544                 if (ret != LOCATIONS_ERROR_NONE) {
545                         LOGD_GEOFENCE("Fail to start. %d", ret);
546                         location_manager_unset_position_updated_cb(geofence_server->loc_gps_manager);
547                         location_manager_destroy(geofence_server->loc_gps_manager);
548                         geofence_server->loc_gps_manager = NULL;
549                         return FENCE_ERR_UNKNOWN;
550                 }
551                 if (geofence_server->gps_timeout_alarm_id == -1)
552                         geofence_server->gps_timeout_alarm_id = _geofence_add_alarm(GPS_TIMEOUT, __gps_timeout_cb, geofence_server);
553
554                 geofence_server->loc_gps_started = TRUE;
555         } else {
556                 LOGD_GEOFENCE("loc_gps_started TRUE");
557         }
558
559         return ret;
560 }
561
562 static void __stop_gps_positioning(gpointer userdata)
563 {
564         FUNC_ENTRANCE_SERVER;
565         g_return_if_fail(userdata);
566         GeofenceServer *geofence_server = (GeofenceServer *) userdata;
567         int ret = 0;
568         if (geofence_server->loc_gps_started == TRUE) {
569                 ret = location_manager_stop(geofence_server->loc_gps_manager);
570                 if (ret != LOCATIONS_ERROR_NONE)
571                         return;
572                 geofence_server->loc_gps_started = FALSE;
573                 ret = location_manager_unset_position_updated_cb(geofence_server->loc_gps_manager);
574                 if (ret != LOCATIONS_ERROR_NONE)
575                         return;
576         }
577
578         if (geofence_server->loc_gps_manager != NULL) {
579                 ret = location_manager_destroy(geofence_server->loc_gps_manager);
580                 if (ret != LOCATIONS_ERROR_NONE)
581                         return;
582                 geofence_server->loc_gps_manager = NULL;
583         }
584 }
585
586 static void __stop_wps_positioning(gpointer userdata)
587 {
588         FUNC_ENTRANCE_SERVER;
589         g_return_if_fail(userdata);
590         GeofenceServer *geofence_server = (GeofenceServer *) userdata;
591         int ret = 0;
592         if (geofence_server->loc_wps_started == TRUE) {
593                 ret = location_manager_stop(geofence_server->loc_wps_manager);
594                 if (ret != LOCATIONS_ERROR_NONE) {
595                         LOGI_GEOFENCE("Unable to stop the wps");
596                         return;
597                 }
598                 geofence_server->loc_wps_started = FALSE;
599                 ret = location_manager_unset_position_updated_cb(geofence_server->loc_wps_manager);
600                 if (ret != LOCATIONS_ERROR_NONE) {
601                         LOGI_GEOFENCE("Unable to unset the callback");
602                         return;
603                 }
604         }
605
606         if (geofence_server->loc_wps_manager != NULL) {
607                 ret = location_manager_destroy(geofence_server->loc_wps_manager);
608                 if (ret != LOCATIONS_ERROR_NONE) {
609                         LOGI_GEOFENCE("Unable to destroy the wps manager");
610                         return;
611                 }
612                 geofence_server->loc_wps_manager = NULL;
613         }
614 }
615
616 static int __get_time_diff(int timestamp)
617 {
618         int current_time = 0;
619         int timediff = 0;
620         current_time = (g_get_real_time()/1000000);
621         timediff = current_time - timestamp;
622         return timediff;
623 }
624
625 static void __process_best_location(GeofenceServer *geofence_server)
626 {
627         FUNC_ENTRANCE_SERVER;
628
629         int gpsdiff = 0;
630         int wpsdiff = 0;
631
632         /* Check if any of the fix is null just return. It doesn't make sense to compare if only one fix is available*/
633         if (geofence_server->gps_fix_info == NULL || geofence_server->wps_fix_info == NULL)
634                 return;
635
636         /*Calculate the time difference*/
637         gpsdiff = __get_time_diff(geofence_server->gps_fix_info->timestamp);
638         wpsdiff = __get_time_diff(geofence_server->wps_fix_info->timestamp);
639
640         if (gpsdiff < wpsdiff) {
641                 if ((geofence_server->gps_fix_info->timestamp - geofence_server->wps_fix_info->timestamp) <= 20) {
642                         if (geofence_server->gps_fix_info->accuracy <= geofence_server->wps_fix_info->accuracy) {
643                                 LOGI_GEOFENCE("Using GPS fix");
644                                 __check_current_location_cb(geofence_server->gps_fix_info->latitude, geofence_server->gps_fix_info->longitude, 0.0, geofence_server->gps_fix_info->timestamp, geofence_server);
645                         } else {
646                                 LOGI_GEOFENCE("Using WPS fix");
647                                 __check_current_location_cb(geofence_server->wps_fix_info->latitude, geofence_server->wps_fix_info->longitude, 0.0, geofence_server->wps_fix_info->timestamp, geofence_server);
648                         }
649                 } else {
650                         LOGI_GEOFENCE("Time diff is more. So using latest GPS fix");
651                         __check_current_location_cb(geofence_server->gps_fix_info->latitude, geofence_server->gps_fix_info->longitude, 0.0, geofence_server->gps_fix_info->timestamp, geofence_server);
652                 }
653         } else {
654                 if ((geofence_server->wps_fix_info->timestamp - geofence_server->gps_fix_info->timestamp) <= 20) {
655                         if (geofence_server->wps_fix_info->accuracy <= geofence_server->gps_fix_info->accuracy) {
656                                 LOGI_GEOFENCE("Using WPS fix");
657                                 __check_current_location_cb(geofence_server->wps_fix_info->latitude, geofence_server->wps_fix_info->longitude, 0.0, geofence_server->wps_fix_info->timestamp, geofence_server);
658                         } else {
659                                 LOGI_GEOFENCE("Using GPS fix");
660                                 __check_current_location_cb(geofence_server->gps_fix_info->latitude, geofence_server->gps_fix_info->longitude, 0.0, geofence_server->gps_fix_info->timestamp, geofence_server);
661                         }
662                 } else {
663                         LOGI_GEOFENCE("Time diff is more. So using latest WPS fix");
664                         __check_current_location_cb(geofence_server->wps_fix_info->latitude, geofence_server->wps_fix_info->longitude, 0.0, geofence_server->wps_fix_info->timestamp, geofence_server);
665                 }
666         }
667 }
668
669 static void __geofence_standalone_gps_position_changed_cb(double latitude, double longitude, double altitude, time_t timestamp, void *user_data)
670 {
671         FUNC_ENTRANCE_SERVER;
672         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
673         double distance = 0;
674         int interval = 0;
675         int min_fence_id = 0;
676         double hor_acc = 0.0;
677         double ver_acc = 0.0;
678         location_accuracy_level_e level;
679         int ret = LOCATIONS_ERROR_NONE;
680
681         /* Allocate memory for the location_info structure */
682         if (geofence_server->gps_fix_info == NULL)
683                 geofence_server->gps_fix_info = (location_fix_info_s *)g_malloc(sizeof(location_fix_info_s));
684
685         /* Store the location information in the structure for future use*/
686         if (geofence_server->gps_fix_info != NULL) {
687                 geofence_server->gps_fix_info->latitude = latitude;
688                 geofence_server->gps_fix_info->longitude = longitude;
689                 ret = location_manager_get_accuracy(geofence_server->loc_gps_manager, &level, &hor_acc, &ver_acc);
690                 if (ret == LOCATIONS_ERROR_NONE) {
691                         LOGI_GEOFENCE("hor_acc:%f, ver_acc:%f", hor_acc, ver_acc);
692                         LOGD_GEOFENCE("*****%f, %f********", latitude, longitude);
693                         geofence_server->gps_fix_info->accuracy = hor_acc;
694                 }
695         }
696         /*Remove the timeout callback that might be running when requesting for fix.*/
697         if (geofence_server->gps_timeout_alarm_id != -1) {
698                 /*LOGI_GEOFENCE("Removing the timeout alarm from restart gps");*/
699                 geofence_server->gps_timeout_alarm_id = _geofence_remove_alarm(geofence_server->gps_timeout_alarm_id);
700         }
701         geofence_server->last_loc_time = timestamp;
702         __check_current_location_cb(latitude, longitude, altitude, timestamp, user_data);
703
704         /* Distance based alarm */
705         distance = _get_min_distance(latitude, longitude, &min_fence_id, geofence_server);
706         geofence_server->nearestTrackingFence = min_fence_id;
707
708         if (distance < 200) {
709                 LOGD_GEOFENCE("interval: 1 secs");
710                 interval = 1;
711         } else if (distance < 500) {
712                 LOGD_GEOFENCE("interval: 3 secs");
713                 interval = 3;
714         } else if (distance < 1000) {
715                 LOGD_GEOFENCE("interval: 6 secs");
716                 interval = 6;
717         } else if (distance < 2000) {
718                 LOGD_GEOFENCE("interval: 20 secs");
719                 interval = 20;
720         } else if (distance < 3000) {
721                 LOGD_GEOFENCE("interval : 1 min");
722                 interval = 1 * 60;
723         } else if (distance < 5000) {
724                 LOGD_GEOFENCE("interval: 2 mins");
725                 interval = 2 * 60;
726         } else if (distance < 10000) {
727                 LOGD_GEOFENCE("interval: 5 mins");
728                 interval = 5 * 60;
729         } else if (distance < 20000) {
730                 LOGD_GEOFENCE("interval : 10 mins");
731                 interval = 10 * 60;
732         } else if (distance < 100000) {
733                 LOGD_GEOFENCE("interval : 20 mins");
734                 interval = 20 * 60;
735         } else {
736                 LOGD_GEOFENCE("interval : 60 mins");
737                 interval = 60 * 60;
738         }
739
740         /* remove the activity value when 10 hours later */
741         if (geofence_server->last_loc_time - geofence_server->activity_timestamp > 10 * 60 * 60)
742                 geofence_server->activity_type = ACTIVITY_IN_VEHICLE;
743
744         if (geofence_server->activity_type == ACTIVITY_STATIONARY)
745                 interval = interval * 10;
746         else if (geofence_server->activity_type == ACTIVITY_WALK)
747                 interval = interval * 5;
748         else if (geofence_server->activity_type == ACTIVITY_RUN)
749                 interval = interval * 3;
750         LOGD_GEOFENCE("Unsetting the position_updated_cb");
751         location_manager_unset_position_updated_cb(geofence_server->loc_gps_manager);
752         location_manager_stop(geofence_server->loc_gps_manager);
753         geofence_server->loc_gps_started = FALSE;
754
755         LOGI_GEOFENCE("Setting the gps interval of alrm %d s", interval);
756         if (geofence_server->gps_alarm_id == -1) {
757                 LOGI_GEOFENCE("Setting the gps alarm from the callback");
758                 geofence_server->gps_alarm_id = _geofence_add_alarm(interval, __gps_alarm_cb, geofence_server);
759         }
760 }
761
762 static void __geofence_gps_position_changed_cb(double latitude, double longitude, double altitude, time_t timestamp, void *user_data)
763 {
764         FUNC_ENTRANCE_SERVER;
765         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
766         double hor_acc = 0.0;
767         double ver_acc = 0.0;
768         GeofenceItemData *item_data = NULL;
769         int min_fence_id = -1;
770         int min_distance = 0;
771         location_accuracy_level_e level;
772         int ret = LOCATIONS_ERROR_NONE;
773
774         /*Remove the timeout callback that might be running when requesting for fix.*/
775         if (geofence_server->gps_timeout_alarm_id != -1) {
776                 /*LOGI_GEOFENCE("Removing the timeout alarm from restart gps");*/
777                 geofence_server->gps_timeout_alarm_id = _geofence_remove_alarm(geofence_server->gps_timeout_alarm_id);
778         }
779
780         /* Allocate memory for the location_info structure */
781         if (geofence_server->gps_fix_info == NULL)
782                 geofence_server->gps_fix_info = (location_fix_info_s *)g_malloc(sizeof(location_fix_info_s));
783
784         /* Store the location information in the structure for future use*/
785         if (geofence_server->gps_fix_info != NULL) {
786                 geofence_server->gps_fix_info->latitude = latitude;
787                 geofence_server->gps_fix_info->longitude = longitude;
788                 geofence_server->gps_fix_info->timestamp = (g_get_real_time()/1000000); /* microsecs->millisecs->secs */
789                 ret = location_manager_get_accuracy(geofence_server->loc_gps_manager, &level, &hor_acc, &ver_acc);
790                 if (ret == LOCATIONS_ERROR_NONE) {
791                         LOGD_GEOFENCE("hor_acc:%f, ver_acc:%f", hor_acc, ver_acc);
792                         LOGD_GEOFENCE("*****%f, %f********", latitude, longitude);
793                         geofence_server->gps_fix_info->accuracy = hor_acc;
794                 }
795         } else {
796                 LOGD_GEOFENCE("Invalid GPS fix data");
797                 return;
798         }
799         geofence_server->last_loc_time = timestamp;
800
801         if (geofence_server->wps_fix_info && __get_time_diff(geofence_server->wps_fix_info->timestamp) <= 20 && geofence_server->gps_fix_info->accuracy <= 50.0) {
802                 LOGI_GEOFENCE("Going for fix comparison from gps fix");
803                 __process_best_location(geofence_server);
804                 /* Using GPS fix from this point. So stop WPS alarms which trigger next WPS request session */
805                 __stop_wps_alarms(geofence_server);
806         } else if (geofence_server->gps_fix_info->accuracy <= 50.0) {
807                 LOGI_GEOFENCE("Emitting from GPS fix directly");
808                 __check_current_location_cb(latitude, longitude, altitude, timestamp, user_data);
809                 /* Using GPS fix from point. So stop WPS alarms which trigger next WPS request session */
810                 __stop_wps_alarms(geofence_server);
811         }
812
813         location_manager_unset_position_updated_cb(geofence_server->loc_gps_manager);
814         location_manager_stop(geofence_server->loc_gps_manager);
815         geofence_server->loc_gps_started = FALSE;
816
817         /*Get minimum distance and fence_id of the nearest tracking fence*/
818         if (geofence_server->gps_fix_info) {
819                 min_distance = _get_min_distance(geofence_server->gps_fix_info->latitude, geofence_server->gps_fix_info->longitude, &min_fence_id, geofence_server);
820                 geofence_server->nearestTrackingFence = min_fence_id; /*This has to be updated frequently*/
821                 item_data = __get_item_by_fence_id(min_fence_id, geofence_server);
822                 if (item_data && geofence_server->loc_gps_started_by_wps == TRUE) {
823                         LOGI_GEOFENCE("******Setting the GPS interval******");
824                         __set_interval_for_gps(min_distance, min_fence_id, user_data);
825                 }
826         }
827 }
828
829 static void __geofence_wps_position_changed_cb(double latitude, double longitude, double altitude, time_t timestamp, void *user_data)
830 {
831         FUNC_ENTRANCE_SERVER;
832         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
833         GeofenceItemData *item_data = NULL;
834         double min_distance = 0.0;
835         int min_fence_id = 0;
836         double hor_acc = 0.0;
837         double ver_acc = 0.0;
838         int gps_state = 0;
839         location_accuracy_level_e level;
840         int ret = LOCATIONS_ERROR_NONE;
841         int interval = 0;
842
843         /* Allocate memory for the location_info structure */
844         if (geofence_server->wps_fix_info == NULL)
845                 geofence_server->wps_fix_info = (location_fix_info_s *)g_malloc0(sizeof(location_fix_info_s));
846          /*Remove the timeout callback that might be running when requesting for fix.*/
847         if (geofence_server->wps_timeout_alarm_id != -1) {
848                 /*LOGI_GEOFENCE("Removing the timeout alarm from restart gps");*/
849                 geofence_server->wps_timeout_alarm_id = _geofence_remove_alarm(geofence_server->wps_timeout_alarm_id);
850         }
851         /* Store the location information in the structure for future use*/
852         if (geofence_server->wps_fix_info != NULL) {
853                 geofence_server->wps_fix_info->latitude = latitude;
854                 geofence_server->wps_fix_info->longitude = longitude;
855                 geofence_server->wps_fix_info->timestamp = (g_get_real_time()/1000000); /* microsecs->millisecs->secs */
856                 ret = location_manager_get_accuracy(geofence_server->loc_wps_manager, &level, &hor_acc, &ver_acc);
857                 if (ret == LOCATIONS_ERROR_NONE) {
858                         LOGD_GEOFENCE("hor_acc:%f, ver_acc:%f", hor_acc, ver_acc);
859                         LOGD_GEOFENCE("*****%f, %f********", latitude, longitude);
860                         geofence_server->wps_fix_info->accuracy = hor_acc;
861                 }
862         } else {
863                 LOGD_GEOFENCE("Invalid WPS fix data");
864                 return;
865         }
866
867         /*Get minimum distance and fence_id of the nearest tracking fence*/
868         min_distance = _get_min_distance(latitude, longitude, &min_fence_id, geofence_server);
869         LOGI_GEOFENCE("Nearest fence id: %d, distance: %f", min_fence_id, min_distance);
870         geofence_server->nearestTrackingFence = min_fence_id;/* This has to be updated frequently*/
871
872         item_data = __get_item_by_fence_id(min_fence_id, geofence_server);
873
874         if (!item_data)
875                 return;/* There is no valid fence with this fence id. So return*/
876
877         geocoordinate_info_s *geocoordinate_info = (geocoordinate_info_s *)item_data->priv;
878
879         double interval_dist = (min_distance - geocoordinate_info->radius) - geofence_server->wps_fix_info->accuracy;
880         LOGI_GEOFENCE("Distance for interval: %f", interval_dist);
881         if (interval_dist < 15000)
882                 interval = interval_dist/25; /*secs*/ /*Assuming 90 km/hr of speed - So 25 mtrs covered in 1 sec*/
883         else if (interval_dist >= 15000 && interval_dist < 18000)
884                 interval = 10 * 60; /* 10 mins */
885         else if (interval_dist >= 18000 && interval_dist < 20000)
886                 interval = 12 * 60; /* 12 mins */
887         else if (interval_dist >= 20000)
888                 interval = 15 * 60; /*15 mins*/
889         if (interval < 15)
890                 interval = 15; /*15 sec */
891
892         location_manager_unset_position_updated_cb(geofence_server->loc_wps_manager);
893         location_manager_stop(geofence_server->loc_wps_manager);
894         geofence_server->loc_wps_started = FALSE;
895
896         LOGI_GEOFENCE("Setting the wps interval of %d secs", interval);
897         if (geofence_server->wps_alarm_id == -1) {
898                 LOGI_GEOFENCE("Setting the wps alarm from the callback");
899                 geofence_server->wps_alarm_id = _geofence_add_alarm(interval, __wps_alarm_cb, geofence_server);
900         }
901
902         /* Get the GPS state here */
903         vconf_get_int(VCONFKEY_LOCATION_ENABLED, &gps_state);
904         if (gps_state == 1) {
905                 if (geofence_server->wps_fix_info->accuracy <= 100.0 && geofence_server->loc_gps_started_by_wps == false) {/*This works when GPS is not running or GPS timeout happens*/
906                         __check_current_location_cb(latitude, longitude, altitude, timestamp, user_data);
907                 } else if (item_data->common_info.status == GEOFENCE_FENCE_STATE_UNCERTAIN) {
908                         __check_current_location_cb(latitude, longitude, altitude, timestamp, user_data);
909                 }
910                 if (geofence_server->loc_gps_started_by_wps == FALSE && geofence_server->loc_gps_started == FALSE) {
911                         if (min_distance <= (geocoordinate_info->radius + GPS_TRIGGER_BOUNDARY)) {
912                                 LOGD_GEOFENCE("Triggering GPS");
913                                 /*LOGD_GEOFENCE("(GPS TRIGGER) GPS started at lat:%f, lon:%f for fence_id:%d at distance:%f", latitude, longitude, min_fence_id, min_distance);*/
914                                 if (FENCE_ERR_NONE == __start_gps_positioning(geofence_server, __geofence_gps_position_changed_cb))
915                                         geofence_server->loc_gps_started_by_wps = true;
916                                 else
917                                         LOGI_GEOFENCE("Error starting GPS/ GPS is off");
918                         }
919                 }
920         } else
921                 __check_current_location_cb(latitude, longitude, altitude, timestamp, user_data); /* Its WPS only mode so no need to worry abt accuracy */
922 }
923
924 static void __set_interval_for_gps(double min_distance, int min_fence_id, void *user_data)
925 {
926         FUNC_ENTRANCE_SERVER;
927         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
928         GeofenceItemData *item_data = NULL;
929         bool isSwitched = false;
930         item_data = __get_item_by_fence_id(min_fence_id, geofence_server);
931         if (item_data && geofence_server->gps_fix_info) {
932                 geocoordinate_info_s *geocoordinate_info = (geocoordinate_info_s *)item_data->priv;
933                 if (geofence_server->gps_trigger_interval == 1 && (min_distance > (geocoordinate_info->radius + 100 + geofence_server->gps_fix_info->accuracy) && min_distance <= (geocoordinate_info->radius + 1000))) {
934                         isSwitched = true;
935                         LOGI_GEOFENCE("Setting the GPS interval as 5 secs");
936                         geofence_server->gps_trigger_interval = 5;
937                         /*LOGI_GEOFENCE("(GPS SWITCH) GPS changed from 1 to 5 sec at lat:%f, lon:%f for fence_id:%d at distance:%f", geofence_server->gps_fix_info->latitude, geofence_server->gps_fix_info->longitude, min_fence_id, min_distance);*/
938                 } else if (geofence_server->gps_trigger_interval == 5 && min_distance <= (geocoordinate_info->radius + 100 + geofence_server->gps_fix_info->accuracy)) {
939                         isSwitched = true;
940                         LOGI_GEOFENCE("Setting the GPS interval as 1 secs");
941                         geofence_server->gps_trigger_interval = 1;
942                         /*LOGI_GEOFENCE("(GPS SWITCH) GPS changed from 5 to 1 sec at lat:%f, lon:%f for fence_id:%d at distance:%f", geofence_server->gps_fix_info->latitude, geofence_server->gps_fix_info->longitude, min_fence_id, min_distance);*/
943                 } else if (min_distance > (geocoordinate_info->radius + 1000)) {
944                         /* Already stopped. Just that GPS trigger alarm wont be scheduled again */
945                         /*LOGI_GEOFENCE("(GPS TRIGGER) GPS stopped at lat:%f, lon:%f for fence:%d at distance:%f", geofence_server->gps_fix_info->latitude, geofence_server->gps_fix_info->longitude, min_fence_id, min_distance);*/
946                         geofence_server->loc_gps_started_by_wps = false;
947                         /*No need of GPS. So stop GPS and start the WPS from here*/
948                         location_manager_unset_position_updated_cb(geofence_server->loc_gps_manager);
949                         location_manager_stop(geofence_server->loc_gps_manager);
950                         geofence_server->loc_gps_started = FALSE;
951                         __start_wps_positioning(geofence_server, __geofence_wps_position_changed_cb);/* Stopping the GPS here. So start using wps */
952                 }
953                 if ((geofence_server->loc_gps_started_by_wps == true && isSwitched == true) || geofence_server->gps_trigger_interval > 1) {
954                         LOGI_GEOFENCE("Setting the gps interval of %d secs during wps session", geofence_server->gps_trigger_interval);
955                         if (geofence_server->gps_alarm_id == -1) {
956                                 /*Switching the interval for GPS. So stop and start using alarm*/
957                                 location_manager_unset_position_updated_cb(geofence_server->loc_gps_manager);
958                                 location_manager_stop(geofence_server->loc_gps_manager);
959                                 geofence_server->loc_gps_started = FALSE;
960                                 LOGI_GEOFENCE("Setting the gps alarm from the callback");
961                                 geofence_server->gps_alarm_id = _geofence_add_alarm(geofence_server->gps_trigger_interval, __gps_alarm_cb, geofence_server);
962                         }
963                 }
964         }
965 }
966
967 void bt_adapter_device_discovery_state_cb(int result, bt_adapter_device_discovery_state_e discovery_state, bt_adapter_device_discovery_info_s *discovery_info, void *user_data)
968 {
969 #if 0
970         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
971         GeofenceItemData *item_data = NULL;
972         int i;
973         int tracking_fence_id = 0;
974         GList *tracking_fences = g_list_first(geofence_server->tracking_list);
975
976         if (discovery_state != BT_ADAPTER_DEVICE_DISCOVERY_FOUND) {
977                 LOGI_GEOFENCE("BREDR discovery %s", discovery_state == BT_ADAPTER_DEVICE_DISCOVERY_STARTED ? "Started" : "Finished");
978                 /* Check only if some BT fence is running */
979                 if (discovery_state == BT_ADAPTER_DEVICE_DISCOVERY_FINISHED && geofence_server->running_bt_cnt > 0) {
980                         LOGI_GEOFENCE("Comparison for BT is done. Now emit the status...");
981                         while (tracking_fences) {
982                                 tracking_fence_id = GPOINTER_TO_INT(tracking_fences->data);
983                                 tracking_fences = g_list_next(tracking_fences);
984                                 item_data = __get_item_by_fence_id(tracking_fence_id, geofence_server);
985                                 if (item_data && item_data->common_info.type == GEOFENCE_TYPE_BT) {
986                                         if (item_data->is_bt_status_in == true)
987                                                 __emit_fence_inout(geofence_server, item_data->common_info.fence_id, GEOFENCE_FENCE_STATE_IN);
988                                         else
989                                                 __emit_fence_inout(geofence_server, item_data->common_info.fence_id, GEOFENCE_FENCE_STATE_OUT);
990                                         item_data->is_bt_status_in = false;
991                                 }
992                         }
993                 }
994         } else {
995                 LOGI_GEOFENCE("%s, %s", discovery_info->remote_address, discovery_info->remote_name);
996                 LOGI_GEOFENCE("rssi: %d is_bonded: %d", discovery_info->rssi, discovery_info->is_bonded);
997
998                 if (geofence_server->running_bt_cnt > 0) {
999                         for (i = 0; i < discovery_info->service_count; i++)
1000                                 LOGI_GEOFENCE("uuid: %s", discovery_info->service_uuid[i]);
1001                         LOGI_GEOFENCE("Tracking list is being checked for the BT geofence");
1002                         __check_tracking_list(discovery_info->remote_address, geofence_server, GEOFENCE_TYPE_BT);
1003                 }
1004         }
1005 #endif
1006 }
1007
1008 static void geofence_network_evt_cb(net_event_info_t *event_cb, void *user_data)
1009 {
1010         FUNC_ENTRANCE_SERVER;
1011         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
1012         g_return_if_fail(geofence_server);
1013         int ret = -1;
1014         int wps_state = 0;
1015         int gps_state = 0;
1016
1017         switch (event_cb->Event) {
1018         case NET_EVENT_WIFI_POWER_IND:
1019                 LOGI_GEOFENCE("WIFI ON/OFF indication");
1020                 vconf_get_int(VCONFKEY_LOCATION_NETWORK_ENABLED, &wps_state);
1021                 vconf_get_int(VCONFKEY_LOCATION_ENABLED, &gps_state);
1022                 if (__is_support_wps() == true && geofence_server->running_geopoint_cnt > 0) {
1023                         if (__isWifiOn() == false) {
1024                                 LOGI_GEOFENCE("WIFI is OFF");
1025                                 /* In Tizen device(Kiran) WPS is not supported if WIFI is switched off */
1026                                 __stop_wps_positioning(geofence_server);
1027                                 __stop_wps_alarms(geofence_server);
1028                                 if (geofence_server->loc_gps_started_by_wps == true) {
1029                                         __stop_gps_positioning(geofence_server); /*Stop the gps if it was started by wps*/
1030                                         __stop_gps_alarms(geofence_server);
1031                                         geofence_server->loc_gps_started_by_wps = false;
1032                                 }
1033                                 if (gps_state == 1) {
1034                                         ret = __start_gps_positioning(geofence_server, __geofence_standalone_gps_position_changed_cb);
1035                                         if (ret != FENCE_ERR_NONE)
1036                                                 LOGE_GEOFENCE("Fail to start standalone gps positioning. Error[%d]", ret);
1037                                 }
1038                         } else {
1039                                 if (__isDataConnected() == true) {/*&& wps_state == 1) {*/
1040                                         LOGI_GEOFENCE("DATA CONNECTION IS TRUE");
1041                                         if (wps_state == 1) {
1042                                                 LOGI_GEOFENCE("WPS STATE IS 1");
1043                                                 __stop_gps_positioning(geofence_server); /* Stop the gps which is running as wps can be used*/
1044                                                 __stop_gps_alarms(geofence_server);
1045                                                 /**** Start the WPS as mobile data is connected and wifi and wps are on *******/
1046                                                 ret = __start_wps_positioning(geofence_server, __geofence_wps_position_changed_cb);
1047                                                 if (ret != FENCE_ERR_NONE)
1048                                                         LOGE_GEOFENCE("Fail to start wps positioning. Error[%d]", ret);
1049                                         }
1050                                 }
1051                         }
1052                 } else {
1053                         LOGE_GEOFENCE("WPS is not supported");
1054                 }
1055                 break;
1056         case NET_EVENT_OPEN_IND:
1057                 LOGI_GEOFENCE("Mobile internet connected");
1058                 vconf_get_int(VCONFKEY_LOCATION_NETWORK_ENABLED, &wps_state);
1059                 if (__is_support_wps() == true && geofence_server->running_geopoint_cnt > 0 && wps_state == 1 && __isWifiOn() == true && __isDataConnected() == true) {
1060                         /**** Start the WPS as mobile data is connected and wifi is on *******/
1061                         if (geofence_server->loc_gps_started_by_wps == false && geofence_server->loc_gps_started == true) {
1062                                 __stop_gps_positioning(geofence_server); /*GPS should be stopped only if it is running standalone*/
1063                                 __stop_gps_alarms(geofence_server);
1064                         }
1065                         ret = __start_wps_positioning(geofence_server, __geofence_wps_position_changed_cb);
1066                         if (ret != FENCE_ERR_NONE)
1067                                 LOGE_GEOFENCE("Fail to start wps positioning. Error[%d]", ret);
1068                 }
1069                 break;
1070         case NET_EVENT_CLOSE_IND:
1071                 LOGI_GEOFENCE("Mobile internet disconnected");
1072                 if (__is_support_wps() == true && geofence_server->running_geopoint_cnt > 0 && geofence_server->loc_wps_started == true) {
1073                         /***** Start standalone gps as mobile data is disconnected *****/
1074                         __stop_wps_positioning(geofence_server);
1075                         __stop_wps_alarms(geofence_server);
1076                         if (geofence_server->loc_gps_started_by_wps == true) {
1077                                 __stop_gps_positioning(geofence_server); /*Stop the gps if it was started by wps*/
1078                                 __stop_gps_alarms(geofence_server);
1079                                 geofence_server->loc_gps_started_by_wps = false;
1080                         }
1081                         ret = __start_gps_positioning(geofence_server, __geofence_standalone_gps_position_changed_cb);
1082                         if (ret != FENCE_ERR_NONE)
1083                                 LOGE_GEOFENCE("Fail to start standalone gps positioning. Error[%d]", ret);
1084                 }
1085                 break;
1086         default:
1087                 break;
1088         }
1089 }
1090
1091 static int __gps_timeout_cb(alarm_id_t alarm_id, void *user_data)
1092 {
1093         LOGI_GEOFENCE("__gps_timeout_cb");
1094         g_return_val_if_fail(user_data, -1);
1095         LOGD_GEOFENCE("alarm_id : %d", alarm_id);
1096         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
1097         geofence_server->gps_timeout_alarm_id = -1;     /*resetting the alarm id*/
1098         /*Stop the gps for sometime when there is no fix*/
1099         __stop_gps_positioning(geofence_server);
1100         if (geofence_server->loc_gps_started_by_wps == FALSE)
1101                 geofence_server->gps_alarm_id = _geofence_add_alarm(1 * 60, __gps_alarm_cb, geofence_server);
1102         else
1103                 geofence_server->loc_gps_started_by_wps = FALSE;
1104         return 0;
1105 }
1106
1107 static int __gps_alarm_cb(alarm_id_t alarm_id, void *user_data)
1108 {
1109         LOGI_GEOFENCE("__gps_alarm_cb");
1110         g_return_val_if_fail(user_data, -1);
1111         LOGD_GEOFENCE("gps alarm_id : %d", alarm_id);
1112         int ret = FENCE_ERR_NONE;
1113         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
1114         if (geofence_server->gps_alarm_id != -1) {
1115                 /*LOGI_GEOFENCE("GPS interval timer removed. ID[%d]", geofence_server->gps_alarm_id);*/
1116                 geofence_server->gps_alarm_id = _geofence_remove_alarm(geofence_server->gps_alarm_id);
1117                 geofence_server->gps_alarm_id = -1;
1118         }
1119         if (geofence_server->loc_gps_started_by_wps == true) {
1120                 ret = __start_gps_positioning(geofence_server, __geofence_gps_position_changed_cb);
1121                 if (ret != FENCE_ERR_NONE)
1122                         LOGE_GEOFENCE("Fail to start gps positioning. Error[%d]", ret);
1123         } else {
1124                 ret = __start_gps_positioning(geofence_server, __geofence_standalone_gps_position_changed_cb);
1125                 if (ret != FENCE_ERR_NONE)
1126                         LOGE_GEOFENCE("Fail to start standalone gps positioning. Error[%d]", ret);
1127         }
1128         return 0;
1129 }
1130
1131 static int __wps_timeout_cb(alarm_id_t alarm_id, void *user_data)
1132 {
1133         LOGI_GEOFENCE("__wps_timeout_cb");
1134         g_return_val_if_fail(user_data, -1);
1135         LOGD_GEOFENCE("alarm_id : %d", alarm_id);
1136         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
1137         if (geofence_server->wps_timeout_alarm_id != -1) {
1138                 /*LOGI_GEOFENCE("WPS timeout timer removed. ID[%d]", geofence_server->wps_timeout_alarm_id);*/
1139                 geofence_server->wps_timeout_alarm_id = _geofence_remove_alarm(geofence_server->wps_timeout_alarm_id);
1140                 geofence_server->wps_timeout_alarm_id = -1;     /*resetting the alarm id*/
1141         }
1142         /*Stop the wps for sometime when there is no fix*/
1143         __stop_wps_positioning(geofence_server);
1144         geofence_server->wps_alarm_id = _geofence_add_alarm(10, __wps_alarm_cb, geofence_server);
1145         /*display_unlock_state(LCD_OFF, PM_RESET_TIMER);*/
1146         return 0;
1147 }
1148
1149 static int __wps_alarm_cb(alarm_id_t alarm_id, void *user_data)
1150 {
1151         LOGI_GEOFENCE("__wps_alarm_cb");
1152         g_return_val_if_fail(user_data, -1);
1153         LOGD_GEOFENCE("wps alarm_id : %d", alarm_id);
1154         int ret = FENCE_ERR_NONE;
1155         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
1156         if (geofence_server->wps_alarm_id != -1) {
1157                 /*LOGI_GEOFENCE("WPS interval timer removed. ID[%d]", geofence_server->wps_alarm_id);*/
1158                 geofence_server->wps_alarm_id = _geofence_remove_alarm(geofence_server->wps_alarm_id);
1159                 geofence_server->wps_alarm_id = -1;
1160         }
1161         if (__is_support_wps() == true && __isWifiOn() == true && __isDataConnected() == true) {
1162                 ret = __start_wps_positioning(geofence_server, __geofence_wps_position_changed_cb);
1163                 if (ret != FENCE_ERR_NONE)
1164                         LOGE_GEOFENCE("Fail to start wps positioning. Error[%d]", ret);
1165         } else {
1166                 ret = __start_gps_positioning(geofence_server, __geofence_standalone_gps_position_changed_cb);
1167                 if (ret != FENCE_ERR_NONE)
1168                         LOGE_GEOFENCE("Fail to start standalone gps positioning. Error[%d]", ret);
1169         }
1170         return 0;
1171 }
1172
1173 static void gps_setting_changed_cb(location_method_e method, bool enable, void *user_data)
1174 {
1175         FUNC_ENTRANCE_SERVER;
1176         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
1177         g_return_if_fail(geofence_server);
1178         GList *tracking_fences = g_list_first(geofence_server->tracking_list);
1179         GeofenceItemData *item_data = NULL;
1180         int tracking_fence_id = 0;
1181         int ret = FENCE_ERR_NONE;
1182         int wps_state = 0;
1183         int gps_state = 0;
1184         /* Get the wps status */
1185         vconf_get_int(VCONFKEY_LOCATION_NETWORK_ENABLED, &wps_state);
1186         vconf_get_int(VCONFKEY_LOCATION_ENABLED, &gps_state);
1187
1188         if (enable == false && geofence_server->running_geopoint_cnt > 0) {
1189                 if (method == LOCATIONS_METHOD_GPS) {
1190                         LOGI_GEOFENCE("Stopping the GPS from settings callback");
1191                         __stop_gps_positioning(geofence_server);
1192                         __stop_gps_alarms(geofence_server);
1193
1194                         if (wps_state == 0) { /* If data is connected then WPS will be running and alerts will be given through WPS*/
1195                                 while (tracking_fences) {
1196                                         tracking_fence_id = GPOINTER_TO_INT(tracking_fences->data);
1197                                         tracking_fences = g_list_next(tracking_fences);
1198                                         item_data = __get_item_by_fence_id(tracking_fence_id, geofence_server);
1199                                         if (item_data && item_data->common_info.type == GEOFENCE_TYPE_GEOPOINT) {
1200                                                 __emit_fence_inout(geofence_server, item_data->common_info.fence_id, GEOFENCE_FENCE_STATE_OUT);
1201                                                 item_data->common_info.proximity_status = GEOFENCE_PROXIMITY_UNCERTAIN;
1202                                         }
1203                                 }
1204                         }
1205                 } else if (method == LOCATIONS_METHOD_WPS) {
1206                         LOGI_GEOFENCE("Stopping the WPS from settings callback");
1207                         __stop_wps_positioning(geofence_server);
1208                         __stop_wps_alarms(geofence_server);
1209
1210                         if (gps_state == 0) { /* If data is connected then WPS will be running and alerts will be given through WPS*/
1211                                 while (tracking_fences) {
1212                                         tracking_fence_id = GPOINTER_TO_INT(tracking_fences->data);
1213                                         tracking_fences = g_list_next(tracking_fences);
1214                                         item_data = __get_item_by_fence_id(tracking_fence_id, geofence_server);
1215                                         if (item_data && item_data->common_info.type == GEOFENCE_TYPE_GEOPOINT) {
1216                                                 __emit_fence_inout(geofence_server, item_data->common_info.fence_id, GEOFENCE_FENCE_STATE_OUT);
1217                                                 item_data->common_info.proximity_status = GEOFENCE_PROXIMITY_UNCERTAIN;
1218                                         }
1219                                 }
1220                                 return;
1221                         }
1222                         /* stop the gps if it was started by WPS */
1223                         if (geofence_server->loc_gps_started_by_wps == true) {
1224                                 __stop_gps_positioning(geofence_server);
1225                                 __stop_gps_alarms(geofence_server);
1226                                 geofence_server->loc_gps_started_by_wps = false; /*So that WPS will use GPS if needed in its next fix(wps fix)*/
1227                         }
1228                         if (geofence_server->loc_gps_started == false && gps_state == 1) {/*As WPS is turned off standalone GPS should be used for tracking the fence*/
1229                                 ret = __start_gps_positioning(geofence_server, __geofence_standalone_gps_position_changed_cb);
1230                                 if (ret != FENCE_ERR_NONE) {
1231                                         LOGE_GEOFENCE("Fail to start gps positioning. Error[%d]", ret);
1232                                         return;
1233                                 }
1234                         }
1235                 }
1236                 if (geofence_server->loc_gps_started_by_wps == true)
1237                         geofence_server->loc_gps_started_by_wps = false; /*So that WPS will use GPS if needed in its next fix(wps fix)*/
1238         } else if (enable == true && geofence_server->running_geopoint_cnt > 0) {
1239                 if (method == LOCATIONS_METHOD_GPS) {
1240                         geofence_server->loc_gps_started_by_wps = false; /* So that WPS will use GPS if needed in its next fix(wps fix) */
1241                         if (wps_state == 0) { /*If wps is on then WPS would be already running. So no need to start GPS*/
1242                                 ret = __start_gps_positioning(geofence_server, __geofence_standalone_gps_position_changed_cb);
1243                                 if (ret != FENCE_ERR_NONE) {
1244                                         LOGE_GEOFENCE("Fail to start gps positioning. Error[%d]", ret);
1245                                         return;
1246                                 }
1247                         }
1248                 } else if (method == LOCATIONS_METHOD_WPS) {
1249                         if (__isWifiOn() == true && __isDataConnected() == true) {/* Start WPS positioning */
1250                                 ret = __start_wps_positioning(geofence_server, __geofence_wps_position_changed_cb);
1251                                 if (ret != FENCE_ERR_NONE) {
1252                                         LOGE_GEOFENCE("Fail to start wps positioning. Error[%d]", ret);
1253                                         return;
1254                                 }
1255                         }
1256                         if (geofence_server->loc_wps_started == true) {/* If WPS is successfully started, switch off gps*/
1257                                 __stop_gps_positioning(geofence_server);
1258                                 __stop_gps_alarms(geofence_server);
1259                         }
1260                 }
1261         }
1262 }
1263
1264 /*********************************THIS HAS TO BE USED ONLY FOR TESTING*********************************************/
1265 #ifdef __LOCAL_TEST__
1266 static void __free_geofence_list(gpointer userdata)
1267 {
1268         GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1269
1270         GList *tmp_fence_list = g_list_first(geofence_server->geofence_list);
1271         while (tmp_fence_list) {
1272                 GeofenceItemData *tmp_data = (GeofenceItemData *)tmp_fence_list->data;
1273                 if (tmp_data)
1274                         g_free(tmp_data);
1275                 tmp_fence_list = g_list_next(tmp_fence_list);
1276         }
1277         geofence_server->geofence_list = NULL;
1278 }
1279 #endif
1280
1281 static int __check_fence_permission(int fence_id, const char *app_id)
1282 {
1283         access_type_e access_type = ACCESS_TYPE_PUBLIC;
1284         char *appid;
1285         int ret = FENCE_ERR_NONE;
1286         ret = geofence_manager_get_access_type(fence_id, -1, &access_type);
1287         if (ret != FENCE_ERR_NONE) {
1288                 LOGE("Error getting the access_type");
1289                 return -1;
1290         }
1291         if (access_type == ACCESS_TYPE_PRIVATE) {
1292                 ret = geofence_manager_get_appid_from_geofence(fence_id, &appid);
1293                 if (ret != FENCE_ERR_NONE) {
1294                         LOGE("Error getting the app_id for fence_id[%d]", fence_id);
1295                         return -1;
1296                 }
1297                 if (g_strcmp0(appid, app_id)) {
1298                         LOGE("Not authorized to access this private fence[%d]", fence_id);
1299                         return 0;
1300                 }
1301         }
1302         return 1;
1303 }
1304
1305 static int __check_place_permission(int place_id, const char *app_id)
1306 {
1307         access_type_e access_type = ACCESS_TYPE_PUBLIC;
1308         char *appid;
1309         int ret = FENCE_ERR_NONE;
1310         ret = geofence_manager_get_access_type(-1, place_id, &access_type);
1311         if (ret != FENCE_ERR_NONE) {
1312                 LOGE("Error getting the access_type");
1313                 return -1;
1314         }
1315         if (access_type == ACCESS_TYPE_PRIVATE) {
1316                 ret = geofence_manager_get_appid_from_places(place_id, &appid);
1317                 if (ret != FENCE_ERR_NONE) {
1318                         LOGE("Error getting the place_id for place_id[%d]", place_id);
1319                         return -1;
1320                 }
1321                 if (g_strcmp0(appid, app_id)) {
1322                         LOGE("Not authorized to access this private place[%d]", place_id);
1323                         return 0;
1324                 }
1325         }
1326         return 1;
1327 }
1328
1329 static void __stop_geofence_service(gint fence_id, const gchar *app_id, gpointer userdata)
1330 {
1331         FUNC_ENTRANCE_SERVER;
1332         g_return_if_fail(userdata);
1333
1334         GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1335         GeofenceItemData *item_data = NULL;
1336         int tracking_status = -1;
1337         int ret = FENCE_ERR_NONE;
1338         int place_id = -1;
1339         access_type_e access_type = ACCESS_TYPE_UNKNOWN;
1340
1341         item_data = __get_item_by_fence_id(fence_id, geofence_server);  /*Fetch the fence details from add_list*/
1342         if (item_data == NULL) {
1343                 LOGI_GEOFENCE("Invalid fence id - no fence exists with this fence id");
1344                 __emit_fence_event(geofence_server, -1, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_ID_NOT_EXIST, GEOFENCE_MANAGE_FENCE_STOPPED);
1345                 return;         /*Invalid fence id - no fence exists with this fence id*/
1346         }
1347         ret = geofence_manager_get_place_id(fence_id, &place_id);
1348         if (ret != FENCE_ERR_NONE) {
1349                 LOGI_GEOFENCE("Error fetching the place_id from the DB for fence: %d", fence_id);
1350                 __emit_fence_event(geofence_server, -1, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STOPPED);
1351                 return;
1352         }
1353         ret = geofence_manager_get_access_type(fence_id, -1, &access_type);
1354         if (ret != FENCE_ERR_NONE) {
1355                 LOGI_GEOFENCE("Error fetching the access type from the DB for fence: %d", fence_id);
1356                 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STOPPED);
1357                 return;
1358         }
1359         ret = __check_fence_permission(fence_id, app_id);
1360         if (ret != 1) {
1361                 LOGE("Permission denied or DB error occured while accessing the fence[%d]", fence_id);
1362                 if (ret == 0)
1363                         __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_GEOFENCE_ACCESS_DENIED, GEOFENCE_MANAGE_FENCE_STOPPED);
1364                 else
1365                         __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STOPPED);
1366                 return;
1367         }
1368         ret = geofence_manager_get_running_status(fence_id, &tracking_status);
1369         if (ret != FENCE_ERR_NONE) {
1370                 LOGI_GEOFENCE("Error fetching the running status from the DB for fence: %d", fence_id);
1371                 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STOPPED);
1372                 return;
1373         }
1374
1375         if (tracking_status == 0) {
1376                 /*This fence is not in the tracking mode currently - nothing to do, just return saying the error*/
1377                 LOGI_GEOFENCE("Fence ID: %d, is not in the tracking mode", fence_id);
1378                 __emit_fence_event(geofence_server, place_id, fence_id, access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_FENCE_STOPPED);
1379                 return;
1380         }
1381
1382         if (tracking_status > 0) {
1383                 LOGI_GEOFENCE("Remove from tracklist: Fence id: %d", fence_id);
1384                 item_data = __get_item_by_fence_id(fence_id, geofence_server);
1385
1386                 /*Item needs to be removed from the fence list*/
1387                 if (item_data != NULL) {
1388                         /*Main DB table should be updated here with the unsetting of running status flag*/
1389                         tracking_status = tracking_status - 1;
1390                         ret = geofence_manager_set_running_status(fence_id, tracking_status);
1391                         if (ret != FENCE_ERR_NONE) {
1392                                 LOGI_GEOFENCE("Error resetting the running status in DB for fence: %d", fence_id);
1393                                 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STOPPED);
1394                                 return;
1395                         }
1396                         /*Update the geofence count according to the type of geofence*/
1397                         if (item_data->common_info.type == GEOFENCE_TYPE_GEOPOINT) {
1398                                 geofence_server->running_geopoint_cnt--;
1399                                 LOGI_GEOFENCE("Removed geopoint fence: %d from tracking list", fence_id);
1400
1401                                 if (geofence_server->running_geopoint_cnt <= 0) {
1402                                         /*Stopping GPS...WPS*/
1403                                         __stop_gps_positioning(geofence_server);
1404                                         if (geofence_server->gps_fix_info != NULL) {
1405                                                 g_free(geofence_server->gps_fix_info);
1406                                                 geofence_server->gps_fix_info = NULL;
1407                                         }
1408                                         geofence_server->loc_gps_started_by_wps = false;
1409                                         __stop_wps_positioning(geofence_server);
1410                                         if (geofence_server->wps_fix_info != NULL) {
1411                                                 g_free(geofence_server->wps_fix_info);
1412                                                 geofence_server->wps_fix_info = NULL;
1413                                         }
1414                                         __stop_gps_alarms(geofence_server);
1415                                         __stop_wps_alarms(geofence_server);
1416                                         __stop_activity_service(geofence_server);
1417                                 }
1418                         } else if (item_data->common_info.type == GEOFENCE_TYPE_BT) {
1419                                 geofence_server->running_bt_cnt--;
1420                                 LOGI_GEOFENCE("Removed bt fence: %d from tracking list", fence_id);
1421                         } else if (item_data->common_info.type == GEOFENCE_TYPE_WIFI) {
1422                                 /*NOTHING NEED TO BE DONE HERE EXCEPT DECREMENTING THE COUNT*/
1423                                 geofence_server->running_wifi_cnt--;
1424                                 if (geofence_server->connectedTrackingWifiFenceId == fence_id) /*It means this fence is connected and it is stopped now*/
1425                                         geofence_server->connectedTrackingWifiFenceId = -1;
1426                         }
1427
1428                         if (tracking_status == 0) {
1429                                 /*Remove the fence from the tracklist*/
1430                                 LOGD_GEOFENCE("Setting the fence status as uncertain here...");
1431                                 if (fence_id == geofence_server->nearestTrackingFence) {
1432                                         ret = bt_adapter_le_stop_scan();
1433                                         if (ret != BT_ERROR_NONE)
1434                                                 LOGE_GEOFENCE("Unable to stop the BLE scan/ Stopped already, error: %d", ret);
1435                                 }
1436                                 item_data->common_info.status = GEOFENCE_FENCE_STATE_UNCERTAIN;
1437                                 item_data->common_info.proximity_status = GEOFENCE_PROXIMITY_UNCERTAIN;
1438                                 geofence_server->tracking_list = g_list_remove(geofence_server->tracking_list, GINT_TO_POINTER(fence_id));
1439                                 if (g_list_length(geofence_server->tracking_list) == 0) {
1440                                         g_list_free(geofence_server->tracking_list);
1441                                         geofence_server->tracking_list = NULL;
1442                                 }
1443                         }
1444                 } else {
1445                         LOGI_GEOFENCE("Geofence service is not running for this fence");
1446                 }
1447
1448         }
1449         /* Emit the error code */
1450         __emit_fence_event(geofence_server, place_id, fence_id, access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_FENCE_STOPPED);
1451 }
1452
1453
1454 static bool __isWifiOn(void)
1455 {
1456         int network_state = -1;
1457         vconf_get_int(VCONFKEY_WIFI_STATE, &network_state);
1458         if (network_state == 0)
1459                         return false;
1460         return true;
1461 }
1462
1463 static bool __isDataConnected(void)
1464 {
1465         bool isDataConnected = false;
1466         int network_state = -1;
1467         int data_state = -1;
1468
1469         int rv = vconf_get_int(VCONFKEY_NETWORK_WIFI_STATE, &network_state);
1470         if (rv == 0) {
1471                 if (network_state == VCONFKEY_NETWORK_WIFI_CONNECTED) {
1472                         LOGI_GEOFENCE("USING WIFI DATA");
1473                         isDataConnected = true;
1474                 }
1475         }
1476         if (isDataConnected == false) {
1477                 rv = vconf_get_int(VCONFKEY_NETWORK_CELLULAR_STATE, &network_state);
1478                 if (rv == 0) {
1479                         if (network_state == VCONFKEY_NETWORK_CELLULAR_ON) {
1480                                 rv = vconf_get_int(VCONFKEY_DNET_STATE, &data_state);
1481                                 if (data_state == VCONFKEY_DNET_NORMAL_CONNECTED) {
1482                                         LOGI_GEOFENCE("USING MOBILE DATA");
1483                                         isDataConnected = true;
1484                                 }
1485                         }
1486                 }
1487         }
1488         return isDataConnected;
1489 }
1490
1491 static int dbus_add_fence_cb(const gchar *app_id,
1492                                                         gint place_id,
1493                                                         gint geofence_type,
1494                                                         gdouble latitude,
1495                                                         gdouble longitude,
1496                                                         gint radius,
1497                                                         const gchar *address,
1498                                                         const gchar *bssid, const gchar *ssid, gpointer userdata)
1499 {
1500         FUNC_ENTRANCE_SERVER;
1501         GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1502
1503         /* create fence id*/
1504         int fence_id = -1;
1505         int ret = FENCE_ERR_NONE;
1506         void *next_item_ptr = NULL;
1507         time_t cur_time;
1508         time(&cur_time);
1509         access_type_e access_type;
1510
1511         ret = geofence_manager_get_access_type(-1, place_id, &access_type);
1512         if (ret != FENCE_ERR_NONE) {
1513                 LOGI_GEOFENCE("Error fetching the access type from the DB for place: %d or place-id does not exist.", place_id);
1514                 __emit_fence_event(geofence_server, -1, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_ID_NOT_EXIST, GEOFENCE_MANAGE_FENCE_ADDED);
1515                 return -1;
1516         }
1517
1518         ret = __check_place_permission(place_id, app_id);
1519         if (ret != 1) {
1520                 LOGE("Unable to add the fence. Permission denied or DB error occured while accessing the place[%d]", place_id);
1521                 if (ret == 0)
1522                         __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_GEOFENCE_ACCESS_DENIED, GEOFENCE_MANAGE_FENCE_ADDED);
1523                 else
1524                         __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_ADDED);
1525                 return -1;
1526         }
1527         /* create GeofenceItemData item, and append it into geofence_list*/
1528         GeofenceItemData *item_data = (GeofenceItemData *)g_malloc0(sizeof(GeofenceItemData));
1529         if (item_data == NULL) {
1530                 LOGI_GEOFENCE("Unable to add the fence because of malloc fail");
1531                 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_OUT_OF_MEMORY, GEOFENCE_MANAGE_FENCE_ADDED);
1532                 return -1;
1533         }
1534
1535         item_data->distance = -1;
1536         item_data->client_status = GEOFENCE_CLIENT_STATUS_NONE;
1537         item_data->common_info.type = geofence_type;
1538         /*fences added by myplaces application are public fences by default*/
1539         if (!g_strcmp0(app_id, MYPLACES_APPID))
1540                 item_data->common_info.access_type = ACCESS_TYPE_PUBLIC;
1541         else
1542                 item_data->common_info.access_type = ACCESS_TYPE_PRIVATE;
1543         item_data->common_info.enable = 1;
1544         item_data->common_info.status = GEOFENCE_FENCE_STATE_UNCERTAIN;
1545         item_data->common_info.proximity_status = GEOFENCE_PROXIMITY_UNCERTAIN;
1546         item_data->is_wifi_status_in = false;
1547         item_data->is_bt_status_in = false;
1548         g_strlcpy(item_data->common_info.appid, app_id, APP_ID_LEN);
1549         item_data->common_info.running_status = 0;
1550         item_data->common_info.place_id = place_id;
1551
1552         /*DB is called and fence-id is retrieved from there(by auto increment mechanism)*/
1553         geofence_manager_set_common_info(&(item_data->common_info), &fence_id);
1554         item_data->common_info.fence_id = fence_id;
1555         LOGD_GEOFENCE("fence id : %d", item_data->common_info.fence_id);
1556
1557         if (geofence_type == GEOFENCE_TYPE_GEOPOINT) {
1558                 LOGD_GEOFENCE("Add geofence with GeoPoint");
1559                 geocoordinate_info_s *geocoordinate_info = (geocoordinate_info_s *)g_malloc0(sizeof(geocoordinate_info_s));
1560                 if (geocoordinate_info == NULL) {
1561                         LOGI_GEOFENCE("Fail to set geocoordinate_info for GPS because of malloc fail");
1562                         __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_OUT_OF_MEMORY, GEOFENCE_MANAGE_FENCE_ADDED);
1563                         g_free(item_data);
1564                         return -1;
1565                 }
1566                 geocoordinate_info->latitude = latitude;
1567                 geocoordinate_info->longitude = longitude;
1568                 if (radius < GEOFENCE_DEFAULT_RADIUS)
1569                         geocoordinate_info->radius = GEOFENCE_DEFAULT_RADIUS;
1570                 else
1571                         geocoordinate_info->radius = radius;
1572                 g_strlcpy(geocoordinate_info->address, address, ADDRESS_LEN);
1573
1574                 /*Geopoint information is saved in the DB*/
1575                 ret = geofence_manager_set_geocoordinate_info(fence_id, geocoordinate_info);
1576                 if (ret != FENCE_ERR_NONE) {
1577                         LOGI_GEOFENCE("Fail to set geocoordinate_info");
1578                         ret = geofence_manager_delete_fence_info(fence_id);
1579                         if (ret != FENCE_ERR_NONE)
1580                                 LOGI_GEOFENCE("Fail to delete fence_id[%d] from common table", fence_id);
1581                         __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_ADDED);
1582                         g_free(geocoordinate_info);
1583                         g_free(item_data);
1584                         return -1;
1585                 }
1586                 item_data->priv = (void *) geocoordinate_info;
1587
1588         } else if (geofence_type == GEOFENCE_TYPE_WIFI) {       /* Specific AP */
1589                 LOGD_GEOFENCE("Add geofence with specific AP");
1590
1591                 bssid_info_s *wifi_info = NULL;
1592                 wifi_info = (bssid_info_s *) g_malloc0(sizeof(bssid_info_s));
1593                 if (wifi_info == NULL) {
1594                         LOGI_GEOFENCE("Fail to set bssid_info for wifi because of malloc fail");
1595                         __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_OUT_OF_MEMORY, GEOFENCE_MANAGE_FENCE_ADDED);
1596                         g_free(item_data);
1597                         return -1;
1598                 }
1599                 g_strlcpy(wifi_info->bssid, bssid, WLAN_BSSID_LEN);
1600                 g_strlcpy(wifi_info->ssid, ssid, WLAN_BSSID_LEN);
1601
1602                 /*Wifi information is saved in the DB(both wifi and BT share the same bssid table here)*/
1603                 ret = geofence_manager_set_bssid_info(fence_id, wifi_info);
1604                 if (ret != FENCE_ERR_NONE) {
1605                         LOGI_GEOFENCE("Fail to set bssid_info for wifi");
1606                         ret = geofence_manager_delete_fence_info(fence_id);
1607                         if (ret != FENCE_ERR_NONE)
1608                                 LOGI_GEOFENCE("Fail to delete fence_id[%d] from common table", fence_id);
1609                         __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_ADDED);
1610                         g_free(wifi_info);
1611                         g_free(item_data);
1612                         return -1;
1613                 }
1614                 item_data->priv = (void *) wifi_info;
1615         } else if (geofence_type == GEOFENCE_TYPE_BT) {
1616                 LOGD_GEOFENCE("Add geofence with bluetooth bssid");
1617
1618                 bssid_info_s *bt_info = NULL;
1619                 bt_info = (bssid_info_s *) g_malloc0(sizeof(bssid_info_s));
1620                 if (bt_info == NULL) {
1621                         LOGI_GEOFENCE("Fail to set bssid_info for BT because of malloc fail");
1622                         __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_OUT_OF_MEMORY, GEOFENCE_MANAGE_FENCE_ADDED);
1623                         g_free(item_data);
1624                         return -1;
1625                 }
1626                 bt_info->enabled = TRUE;
1627                 g_strlcpy(bt_info->bssid, bssid, WLAN_BSSID_LEN);
1628                 g_strlcpy(bt_info->ssid, ssid, WLAN_BSSID_LEN);
1629
1630                 /*BT info is saved in the DB(both wifi and BT share the same bssid table here)*/
1631                 ret = geofence_manager_set_bssid_info(fence_id, bt_info);
1632                 if (ret != FENCE_ERR_NONE) {
1633                         LOGI_GEOFENCE("Fail to set bssid_info for BT");
1634                         ret = geofence_manager_delete_fence_info(fence_id);
1635                         if (ret != FENCE_ERR_NONE)
1636                                 LOGI_GEOFENCE("Fail to delete fence_id[%d] from common table", fence_id);
1637                         __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_ADDED);
1638                         g_free(bt_info);
1639                         g_free(item_data);
1640                         return -1;
1641                 }
1642                 item_data->priv = (void *) bt_info;
1643         }
1644         /*Adding the data to the geofence_list which contains the added geofences list information*/
1645         if (geofence_server->geofence_list == NULL)
1646                 geofence_server->geofence_list = g_list_append(geofence_server->geofence_list, item_data);
1647         else
1648                 geofence_server->geofence_list = g_list_insert_before(geofence_server->geofence_list, next_item_ptr, item_data);
1649         /*This code is just for testing purpose. It will be removed after the development phase - Karthik*/
1650         int temp_cnt = 0;
1651         GList *temp_list = g_list_first(geofence_server->geofence_list);
1652         temp_list = g_list_first(geofence_server->geofence_list);
1653         while (temp_list) {
1654                 temp_cnt++;
1655                 temp_list = g_list_next(temp_list);
1656         }
1657         LOGI_GEOFENCE("Fences in local list: %d", temp_cnt);
1658         geofence_manager_get_count_of_fences(&temp_cnt);
1659         LOGI_GEOFENCE("Fence count in DB: %d", temp_cnt);
1660
1661         /*Emit the error code*/
1662         __emit_fence_event(geofence_server, place_id, fence_id, item_data->common_info.access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_FENCE_ADDED);
1663         return fence_id;
1664 }
1665
1666 static int dbus_add_place_cb(const gchar *app_id, const gchar *place_name, gpointer userdata)
1667 {
1668         FUNC_ENTRANCE_SERVER;
1669         int place_id = -1;
1670         int ret = FENCE_ERR_NONE;
1671         GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1672         place_info_s *place_info = (place_info_s *)g_malloc0(sizeof(place_info_s));
1673
1674         if (place_info == NULL) {
1675                 LOGI_GEOFENCE("Unable to add the place due to malloc fail");
1676                 __emit_fence_event(geofence_server, -1, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_OUT_OF_MEMORY, GEOFENCE_MANAGE_PLACE_ADDED);
1677                 return -1;
1678         }
1679         /*fences added by myplaces application are public fences by default*/
1680         if (!g_strcmp0(app_id, MYPLACES_APPID))
1681                 place_info->access_type = ACCESS_TYPE_PUBLIC;
1682         else
1683                 place_info->access_type = ACCESS_TYPE_PRIVATE;
1684
1685         g_strlcpy(place_info->place_name, place_name, PLACE_NAME_LEN);
1686         g_strlcpy(place_info->appid, app_id, APP_ID_LEN);
1687         /*Add the place details to db*/
1688         ret = geofence_manager_set_place_info(place_info, &place_id);
1689         if (ret != FENCE_ERR_NONE) {
1690                 LOGI_GEOFENCE("Unable to add the place due to DB error");
1691                 __emit_fence_event(geofence_server, -1, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_PLACE_ADDED);
1692                 g_free(place_info);
1693                 return -1;
1694         }
1695         __emit_fence_event(geofence_server, place_id, -1, place_info->access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_PLACE_ADDED);
1696         g_free(place_info);
1697         return place_id;
1698 }
1699
1700 static void dbus_enable_geofence_cb(gint fence_id, const gchar *app_id, gboolean enable, gpointer userdata)
1701 {
1702         FUNC_ENTRANCE_SERVER;
1703         g_return_if_fail(userdata);
1704
1705         GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1706         int ret = FENCE_ERR_NONE;
1707         access_type_e access_type = ACCESS_TYPE_UNKNOWN;
1708         int place_id = -1;
1709         int enable_status = 0;
1710         geofence_manage_e manage_enum = GEOFENCE_MANAGE_SETTING_ENABLED;
1711
1712         if (enable == 0)
1713                 manage_enum = GEOFENCE_MANAGE_SETTING_DISABLED;
1714
1715         ret = geofence_manager_get_access_type(fence_id, -1, &access_type);
1716         if (ret != FENCE_ERR_NONE) {
1717                 LOGI_GEOFENCE("Error fetching the access type from the DB for fence: %d or fence-id does not exist.", fence_id);
1718                 __emit_fence_event(geofence_server, -1, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_ID_NOT_EXIST, manage_enum);
1719                 return;
1720         }
1721         ret = geofence_manager_get_place_id(fence_id, &place_id);
1722         if (ret != FENCE_ERR_NONE) {
1723                 LOGI_GEOFENCE("Error fetching the place_id from the DB for fence: %d", fence_id);
1724                 __emit_fence_event(geofence_server, -1, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, manage_enum);
1725                 return;
1726         }
1727         if (access_type == ACCESS_TYPE_PUBLIC) {
1728                 if (g_strcmp0(app_id, MYPLACES_APPID) != 0) {
1729                         LOGI_GEOFENCE("Received: %s", app_id);
1730                         LOGI_GEOFENCE("Not authorized to enable/disable this fence[%d] service.", fence_id);
1731                         __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_GEOFENCE_ACCESS_DENIED, manage_enum);
1732                         return;
1733                 }
1734                 if (enable == true)
1735                         enable_status = 1;
1736                 ret = geofence_manager_set_enable_status(fence_id, enable_status);
1737                 if (ret != FENCE_ERR_NONE) {
1738                         LOGI_GEOFENCE("DB error in enabling/disabling the fence[%d].", fence_id);
1739                         __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, manage_enum);
1740                         return;
1741                 }
1742         } else {
1743                 LOGI_GEOFENCE("Currently, only public fences can be enabled/disabled. It can be done by MyPlaces app only.");
1744         }
1745         /*Emit the error code*/
1746         __emit_fence_event(geofence_server, place_id, fence_id, access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, manage_enum);
1747 }
1748
1749 static void dbus_update_place_cb(gint place_id, const gchar *app_id, const gchar *place_name, gpointer userdata)
1750 {
1751         FUNC_ENTRANCE_SERVER;
1752         int ret = FENCE_ERR_NONE;
1753         GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1754
1755         if (place_id == DEFAULT_PLACE_HOME || place_id == DEFAULT_PLACE_OFFICE || place_id == DEFAULT_PLACE_CAR) {
1756                 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_PLACE_ACCESS_DENIED, GEOFENCE_MANAGE_PLACE_UPDATED);
1757                 return;
1758         }
1759
1760         place_info_s *place_info = NULL;
1761         ret = geofence_manager_get_place_info(place_id, &place_info);
1762         if (ret != FENCE_ERR_NONE || place_info == NULL) {
1763                 if (ret == FENCE_ERR_INTERNAL) {
1764                         LOGI_GEOFENCE("malloc fail for place id[%d]", place_id);
1765                         __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_OUT_OF_MEMORY, GEOFENCE_MANAGE_PLACE_UPDATED);
1766                 } else {
1767                         LOGI_GEOFENCE("Place_id does not exist or DB error in getting the place info for place_id[%d].", place_id);
1768                         __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_ID_NOT_EXIST, GEOFENCE_MANAGE_PLACE_UPDATED);
1769                 }
1770                 g_free(place_info);
1771                 return;
1772         }
1773         if (g_strcmp0(app_id, place_info->appid) != 0) {
1774                 LOGI_GEOFENCE("Not authorized to update the place");
1775                 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_PLACE_ACCESS_DENIED, GEOFENCE_MANAGE_PLACE_UPDATED);
1776                 g_free(place_info);
1777                 return;
1778         }
1779
1780         /*Update the place details to db*/
1781         ret = geofence_manager_update_place_info(place_id, place_name);
1782         if (ret != FENCE_ERR_NONE) {
1783                 LOGI_GEOFENCE("Unable to update the place");
1784                 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_PLACE_UPDATED);
1785                 g_free(place_info);
1786                 return;
1787         }
1788         __emit_fence_event(geofence_server, place_id, -1, place_info->access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_PLACE_UPDATED);
1789         g_free(place_info);
1790 }
1791
1792 static void dbus_remove_fence_cb(gint fence_id, const gchar *app_id, gpointer userdata)
1793 {
1794         FUNC_ENTRANCE_SERVER;
1795         GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1796         g_return_if_fail(geofence_server);
1797         int ret = FENCE_ERR_NONE;
1798         int place_id = -1;
1799         char *app_id_db;
1800         access_type_e access_type = ACCESS_TYPE_UNKNOWN;
1801
1802         /*//////////Required to be sent in the event callback////////////////--*/
1803         ret = geofence_manager_get_place_id(fence_id, &place_id);
1804         if (ret != FENCE_ERR_NONE) {
1805                 LOGI_GEOFENCE("Error fetching the place_id from the DB for fence: %d or fence-id does not exist", fence_id);
1806                 __emit_fence_event(geofence_server, -1, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_ID_NOT_EXIST, GEOFENCE_MANAGE_FENCE_REMOVED);
1807                 return;
1808         }
1809         /*//////////////////////////////////////////////////////////////////--*/
1810         ret = geofence_manager_get_appid_from_geofence(fence_id, &app_id_db);
1811         if (ret != FENCE_ERR_NONE) {
1812                 LOGI_GEOFENCE("Failed to get the appid, Error - %d", ret);
1813                 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_REMOVED);
1814                 return;
1815         }
1816         if (g_strcmp0(app_id_db, app_id) != 0) {
1817                 LOGI_GEOFENCE("Not authorized to remove the fence");
1818                 g_free(app_id_db);
1819                 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_GEOFENCE_ACCESS_DENIED, GEOFENCE_MANAGE_FENCE_REMOVED);
1820                 return;
1821         }
1822         g_free(app_id_db);
1823         /*/////////required to be sent in the event callback///////////////--*/
1824         ret = geofence_manager_get_access_type(fence_id, -1, &access_type);
1825         if (ret != FENCE_ERR_NONE) {
1826                 LOGI_GEOFENCE("Error fetching the access type from the DB for fence: %d", fence_id);
1827                 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_REMOVED);
1828                 return;
1829         }
1830         /*///////////////////////////////////////////////////////////////////////--*/
1831         GeofenceItemData *item_data = __get_item_by_fence_id(fence_id, geofence_server);
1832         if (item_data == NULL) {
1833                 LOGI_GEOFENCE("Invalid fence_id[%d]", fence_id);
1834                 return;
1835         }
1836
1837         /*Stop the geofence service for the fence first if it is running*/
1838         int tracking_status = -1;
1839         ret = geofence_manager_get_running_status(fence_id, &tracking_status);
1840         if (ret != FENCE_ERR_NONE) {
1841                 LOGI_GEOFENCE("Error fetching the running status from the DB for fence: %d or fence-id does not exist", fence_id);
1842                 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_REMOVED);
1843                 return;
1844         }
1845         if (tracking_status == 1) {
1846                 __stop_geofence_service(fence_id, app_id, userdata);
1847         } else if (tracking_status > 1) {/*its a public fence*/
1848                 tracking_status = 1;    /*resetting the running status to 1 for forcefull stop from MYPlacesApp*/
1849                 ret = geofence_manager_set_running_status(fence_id, tracking_status);
1850                 if (ret != FENCE_ERR_NONE) {
1851                         LOGI_GEOFENCE("Error resetting the running status in the DB for fence: %d or fence-id does not exist", fence_id);
1852                         __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_REMOVED);
1853                         return;
1854                 }
1855                 __stop_geofence_service(fence_id, app_id, userdata);
1856         }
1857         /*Removing the fence id from the DB*/
1858         ret = geofence_manager_delete_fence_info(fence_id);
1859         if (ret != FENCE_ERR_NONE) {
1860                 LOGI_GEOFENCE("Fail to delete fence_id[%d]", fence_id);
1861                 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_REMOVED);
1862                 return;
1863         }
1864
1865         /*Removing the fence id from the geofence_list which contains the added fence list details*/
1866         geofence_server->geofence_list = g_list_remove(geofence_server->geofence_list, item_data);
1867         LOGI_GEOFENCE("Removed fence_id[%d]", fence_id);
1868         g_free(item_data);      /*freeing the memory*/
1869
1870         /*Check if the length of the geofence_list is 0 then free and make it null*/
1871         if (g_list_length(geofence_server->geofence_list) == 0) {
1872                 g_list_free(geofence_server->geofence_list);
1873                 geofence_server->geofence_list = NULL;
1874         }
1875         /*Emit the error code*/
1876         __emit_fence_event(geofence_server, place_id, fence_id, access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_FENCE_REMOVED);
1877 }
1878
1879 static void dbus_get_place_name_cb(gint place_id, const gchar *app_id, char **place_name, int *error_code, gpointer userdata)
1880 {
1881         FUNC_ENTRANCE_SERVER;
1882         access_type_e access_type = ACCESS_TYPE_UNKNOWN;
1883
1884         int ret = geofence_manager_get_access_type(-1, place_id, &access_type);
1885         if (ret != FENCE_ERR_NONE) {
1886                 LOGI_GEOFENCE("Error fetching the access type from the DB for place: %d or place-id does not exist.", place_id);
1887                 *error_code = GEOFENCE_SERVER_ERROR_ID_NOT_EXIST;
1888                 return;
1889         }
1890
1891         ret = __check_place_permission(place_id, app_id);
1892         if (ret != 1) {
1893                 LOGE("Unable to get the place name. Permission denied or DB error occured while accessing the place[%d]", place_id);
1894                 if (ret == 0)
1895                         *error_code = GEOFENCE_SERVER_ERROR_PLACE_ACCESS_DENIED;
1896                 else
1897                         *error_code = GEOFENCE_SERVER_ERROR_DATABASE;
1898                 return;
1899         }
1900         ret = geofence_manager_get_place_name(place_id, place_name);
1901         if (ret != FENCE_ERR_NONE) {
1902                 *error_code = GEOFENCE_SERVER_ERROR_DATABASE;
1903                 return;
1904         }
1905         *error_code = GEOFENCE_SERVER_ERROR_NONE;
1906 }
1907
1908 static void dbus_remove_place_cb(gint place_id, const gchar *app_id, gpointer userdata)
1909 {
1910         FUNC_ENTRANCE_SERVER;
1911         g_return_if_fail(userdata);
1912         GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1913         GList *fence_list = NULL, *list = NULL;
1914         int fence_cnt = 0;
1915         int tracking_status = 0;
1916         int ret = FENCE_ERR_NONE;
1917         GeofenceItemData *item_data = NULL;
1918         access_type_e access_type = ACCESS_TYPE_UNKNOWN;
1919
1920         /* Default places */
1921         if (place_id == DEFAULT_PLACE_HOME || place_id == DEFAULT_PLACE_OFFICE || place_id == DEFAULT_PLACE_CAR) {
1922                 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_PLACE_ACCESS_DENIED, GEOFENCE_MANAGE_PLACE_REMOVED);
1923                 return;
1924         }
1925         ret = geofence_manager_get_access_type(-1, place_id, &access_type);
1926         if (ret != FENCE_ERR_NONE) {
1927                 LOGE("Unable to fetch the access type for place_id[%d]", place_id);
1928                 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_ID_NOT_EXIST, GEOFENCE_MANAGE_PLACE_REMOVED);
1929                 return;
1930         }
1931
1932         place_info_s *place_info = NULL;
1933         ret = geofence_manager_get_place_info(place_id, &place_info);
1934         if (ret != FENCE_ERR_NONE || place_info == NULL) {
1935                 if (ret == FENCE_ERR_INTERNAL) {
1936                         LOGI_GEOFENCE("malloc fail for place id[%d]", place_id);
1937                         __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_OUT_OF_MEMORY, GEOFENCE_MANAGE_PLACE_REMOVED);
1938                 } else {
1939                         LOGI_GEOFENCE("Place_id does not exist or DB error in getting the place info for place_id[%d].", place_id);
1940                         __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_ID_NOT_EXIST, GEOFENCE_MANAGE_PLACE_REMOVED);
1941                 }
1942                 g_free(place_info);
1943                 return;
1944         }
1945         if (g_strcmp0(app_id, place_info->appid) != 0) {
1946                 LOGI_GEOFENCE("Not authorized to remove the place");
1947                 g_free(place_info);
1948                 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_PLACE_ACCESS_DENIED, GEOFENCE_MANAGE_PLACE_REMOVED);
1949                 return;
1950         }
1951         g_free(place_info);
1952
1953         ret = geofence_manager_get_fenceid_list_from_db(&fence_cnt, &fence_list, place_id);
1954         if (ret != FENCE_ERR_NONE) {
1955                 LOGE("Unable to fetch the fence list from the DB");
1956                 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_PLACE_REMOVED);
1957                 return;
1958         }
1959         int fence_id = 0;
1960         list = g_list_first(fence_list);
1961         while (list) {
1962                 fence_id = GPOINTER_TO_INT(list->data);
1963                 item_data = __get_item_by_fence_id(fence_id, geofence_server);
1964                 ret = geofence_manager_get_running_status(fence_id, &tracking_status);
1965                 if (ret != FENCE_ERR_NONE) {
1966                         LOGE("Unable to fetch the running status before removing the fence while removing a place");
1967                         __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_PLACE_REMOVED);
1968                         return;
1969                 }
1970                 if (tracking_status == 1) {
1971                         __stop_geofence_service(fence_id, app_id, userdata);
1972                 } else if (tracking_status > 1) {
1973                         tracking_status = 1;    /*resetting the running status as it is a forcefull stop from MYPlacesApp*/
1974                         ret = geofence_manager_set_running_status(fence_id, tracking_status);
1975                         if (ret != FENCE_ERR_NONE) {
1976                                 LOGI_GEOFENCE("Error setting the running status from the DB for fence: %d or fence-id does not exist", fence_id);
1977                                 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_PLACE_REMOVED);
1978                                 return;
1979                         }
1980                         __stop_geofence_service(fence_id, app_id, userdata);
1981                 }
1982
1983                 /*Removing the fence id from the geofence_list which contains the added fence list details*/
1984                 geofence_server->geofence_list = g_list_remove(geofence_server->geofence_list, item_data);
1985                 LOGI_GEOFENCE("Removed fence_id[%d]", fence_id);
1986                 g_free(item_data);
1987                 /*Check if the length of the geofence_list is 0 then free and make it null*/
1988                 if (g_list_length(geofence_server->geofence_list) == 0) {
1989                         g_list_free(geofence_server->geofence_list);
1990                         geofence_server->geofence_list = NULL;
1991                 }
1992                 list = g_list_next(list);
1993         }
1994         ret = geofence_manager_delete_place_info(place_id);
1995         if (ret != FENCE_ERR_NONE) {
1996                 LOGI_GEOFENCE("DB error occured while removing the place from DB");
1997                 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_PLACE_REMOVED);
1998                 return;
1999         }
2000         __emit_fence_event(geofence_server, place_id, -1, access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_PLACE_REMOVED);
2001 }
2002
2003 static void dbus_start_geofence_cb(gint fence_id, const gchar *app_id, gpointer userdata)
2004 {
2005         FUNC_ENTRANCE_SERVER;
2006         g_return_if_fail(userdata);
2007
2008         GeofenceServer *geofence_server = (GeofenceServer *) userdata;
2009         int tracking_fence_id = -1;
2010         void *next_item_ptr = NULL;
2011         GList *track_list = g_list_first(geofence_server->tracking_list);
2012         GeofenceItemData *item_data = NULL;
2013
2014         int ret = FENCE_ERR_NONE;
2015         int tracking_status = -1;
2016         int place_id = -1;
2017         access_type_e access_type = ACCESS_TYPE_UNKNOWN;
2018         char *app_id_db = NULL;
2019         geofence_fence_state_e status_to_be_emitted = GEOFENCE_FENCE_STATE_UNCERTAIN;
2020
2021         item_data = __get_item_by_fence_id(fence_id, geofence_server);  /*Fetch the fence details from add_list*/
2022         if (item_data == NULL) {
2023                 LOGI_GEOFENCE("Invalid fence id - no fence exists with this fence id");
2024                 __emit_fence_event(geofence_server, -1, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_ID_NOT_EXIST, GEOFENCE_MANAGE_FENCE_STARTED);
2025                 return;         /*Invalid fence id - no fence exists with this fence id*/
2026         }
2027         if (!g_strcmp0(app_id, MYPLACES_APPID)) {
2028                 LOGI_GEOFENCE("My Places cannot start a fence");
2029                 __emit_fence_event(geofence_server, -1, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_GEOFENCE_ACCESS_DENIED, GEOFENCE_MANAGE_FENCE_STARTED);
2030                 return;
2031         }
2032         ret = geofence_manager_get_place_id(fence_id, &place_id);
2033         if (ret != FENCE_ERR_NONE) {
2034                 LOGI_GEOFENCE("Error fetching the place_id from the DB for fence: %d", fence_id);
2035                 __emit_fence_event(geofence_server, -1, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STARTED);
2036                 return;
2037         }
2038
2039         ret = geofence_manager_get_running_status(fence_id, &tracking_status);
2040         if (ret != FENCE_ERR_NONE) {
2041                 LOGI_GEOFENCE("Error fetching the running status from the DB for fence: %d or fence-id does not exist.", fence_id);
2042                 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STARTED);
2043                 return;
2044         }
2045
2046         ret = geofence_manager_get_access_type(fence_id, -1, &access_type);
2047         if (ret != FENCE_ERR_NONE) {
2048                 LOGE("Error getting the access_type");
2049                 return;
2050         }
2051         if (access_type == ACCESS_TYPE_PRIVATE) {
2052                 ret = geofence_manager_get_appid_from_geofence(fence_id, &app_id_db);
2053                 if (ret != FENCE_ERR_NONE) {
2054                         LOGE("Error getting the app_id for fence_id[%d]", fence_id);
2055                         __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STARTED);
2056                         return;
2057                 }
2058                 if (g_strcmp0(app_id_db, app_id)) {
2059                         LOGE("Not authorized to access this private fence[%d]", fence_id);
2060                         __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_GEOFENCE_ACCESS_DENIED, GEOFENCE_MANAGE_FENCE_STARTED);
2061                         g_free(app_id_db);
2062                         return;
2063                 }
2064                 g_free(app_id_db);
2065                 if (tracking_status == 1) {
2066                         LOGI_GEOFENCE("Private fence ID: %d, already exists in the tracking list", fence_id);
2067                         __emit_fence_event(geofence_server, place_id, fence_id, access_type, app_id, GEOFENCE_SERVER_ERROR_ALREADY_STARTED, GEOFENCE_MANAGE_FENCE_STARTED);
2068                         return;
2069                 } else {
2070                         ret = geofence_manager_set_running_status(fence_id, 1);
2071                         if (ret != FENCE_ERR_NONE) {
2072                                 LOGI_GEOFENCE("Error setting the fence status");
2073                                 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STARTED);
2074                                 return;
2075                         }
2076                         tracking_status = 1;
2077                 }
2078         } else if (access_type == ACCESS_TYPE_PUBLIC) {
2079                 int enable = -1;
2080                 ret = geofence_manager_get_enable_status(fence_id, &enable);
2081                 if (ret != FENCE_ERR_NONE) {
2082                         LOGI_GEOFENCE("Error fetching the enable status from the DB for fence: %d or fence-id does not exist.", fence_id);
2083                         __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STARTED);
2084                         return;
2085                 }
2086                 if (enable == 0) {
2087                         LOGI_GEOFENCE("Error - Fence[%d] is not enabled", fence_id);
2088                         __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_GEOFENCE_ACCESS_DENIED, GEOFENCE_MANAGE_FENCE_STARTED);
2089                         return;
2090                 }
2091                 if (tracking_status > 0) {
2092                         LOGI_GEOFENCE("Public fence ID: %d, already exists in the tracking list, incrementing the counter for fence", fence_id);
2093                         ret = geofence_manager_set_running_status(fence_id, (tracking_status + 1));
2094                         if (ret != FENCE_ERR_NONE) {
2095                                 LOGI_GEOFENCE("Error setting the fence status");
2096                                 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STARTED);
2097                                 return;
2098                         }
2099                         __emit_fence_event(geofence_server, place_id, fence_id, access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_FENCE_STARTED);
2100                         return;
2101                 } else {
2102                         ret = geofence_manager_set_running_status(fence_id, (tracking_status + 1));
2103                         if (ret != FENCE_ERR_NONE) {
2104                                 LOGI_GEOFENCE("Error setting the fence status");
2105                                 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STARTED);
2106                                 return;
2107                         }
2108                         tracking_status++;
2109                 }
2110         }
2111
2112         item_data->client_status = GEOFENCE_CLIENT_STATUS_START;
2113
2114         if (item_data->common_info.type == GEOFENCE_TYPE_GEOPOINT) {
2115                 status_to_be_emitted = GEOFENCE_FENCE_STATE_OUT;
2116                 if (__is_support_wps() == true && __isDataConnected() == true && __isWifiOn() == true) {
2117                         ret = __start_wps_positioning(geofence_server, __geofence_wps_position_changed_cb);
2118                         if (ret != FENCE_ERR_NONE) {
2119                                 LOGE_GEOFENCE("Fail to start wps positioning. Error[%d]", ret);
2120                                 geofence_manager_set_running_status(fence_id, (tracking_status - 1));
2121                                 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_IPC, GEOFENCE_MANAGE_FENCE_STARTED);
2122                                 return;
2123                         }
2124                 } else {
2125                         ret = __start_gps_positioning(geofence_server, __geofence_standalone_gps_position_changed_cb);
2126                         if (ret != FENCE_ERR_NONE) {
2127                                 LOGE_GEOFENCE("Fail to start gps positioning. Error[%d]", ret);
2128                                 geofence_manager_set_running_status(fence_id, (tracking_status - 1));
2129                                 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_IPC, GEOFENCE_MANAGE_FENCE_STARTED);
2130                                 return;
2131                         }
2132                 }
2133                 geofence_server->running_geopoint_cnt++;
2134                 __start_activity_service(geofence_server);
2135         } else if (item_data->common_info.type == GEOFENCE_TYPE_BT) {
2136                 LOGI_GEOFENCE("fence_type [GEOFENCE_TYPE_BT]");
2137
2138                 bssid_info_s *bt_info = NULL;
2139                 if (item_data->priv != NULL) {
2140                         bt_info = (bssid_info_s *) item_data->priv;
2141                         if (!bt_info->enabled)
2142                                 bt_info->enabled = TRUE;
2143                 }
2144                 bt_adapter_state_e adapter_state = BT_ADAPTER_DISABLED;
2145                 bt_error_e error = BT_ERROR_NONE;
2146                 error = bt_adapter_get_state(&adapter_state);
2147                 if (error == BT_ERROR_NONE) {
2148                         geofence_server->running_bt_cnt++;
2149                         if (adapter_state == BT_ADAPTER_DISABLED) {
2150                                 LOGE_GEOFENCE("BT Adapter is DISABLED");
2151                                 status_to_be_emitted = GEOFENCE_FENCE_STATE_OUT;
2152                         } else if (adapter_state == BT_ADAPTER_ENABLED) {
2153                                 bt_device_info_s *bt_device_info = NULL;
2154                                 if (bt_info != NULL) {
2155                                         ret = bt_adapter_get_bonded_device_info(bt_info->bssid, &bt_device_info);
2156                                         if (ret != BT_ERROR_NONE) {
2157                                                 LOGE_GEOFENCE("Fail to get the bonded device info/ Not bonded with any device. Error[%d]", ret);
2158                                                 /*NEED TO BE DECIDED WHETHER TO REQUEST FOR A SCAN HERE OR JUST EMIT OUT AS STATUS*/
2159                                                 if (ret == BT_ERROR_REMOTE_DEVICE_NOT_BONDED)
2160                                                         status_to_be_emitted = GEOFENCE_FENCE_STATE_OUT;
2161                                         } else {
2162                                                 if (bt_device_info == NULL) {
2163                                                         LOGI_GEOFENCE("bt_adapter_get_bonded_device_info [%s] failed.", bt_info->bssid);
2164                                                         status_to_be_emitted = GEOFENCE_FENCE_STATE_OUT;
2165                                                 } else {
2166                                                         if (bt_device_info->is_bonded == TRUE && bt_device_info->is_connected == TRUE) {
2167                                                                 LOGI_GEOFENCE("[%s] bonded TRUE, connected TRUE", bt_info->bssid);
2168                                                                 status_to_be_emitted = GEOFENCE_FENCE_STATE_IN;
2169                                                         } else {
2170                                                                 status_to_be_emitted = GEOFENCE_FENCE_STATE_OUT;
2171                                                         }
2172
2173                                                         ret = bt_adapter_free_device_info(bt_device_info);
2174                                                         if (ret != BT_ERROR_NONE)
2175                                                                 LOGE_GEOFENCE("bt_adapter_free_device_info fail[%d]", ret);
2176                                                 }
2177                                         }
2178                                 }
2179                         }
2180                 } else {
2181                         if (error != BT_ERROR_NONE) {
2182                                 LOGI_GEOFENCE("Unable to get the BT adapter state. Not added to track list: %d", error);
2183                                 geofence_manager_set_running_status(fence_id, (tracking_status - 1));
2184                                 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_IPC, GEOFENCE_MANAGE_FENCE_STARTED);
2185                                 return;
2186                         }
2187                 }
2188         } else if (item_data->common_info.type == GEOFENCE_TYPE_WIFI) {
2189                 LOGI_GEOFENCE("fence_type [GEOFENCE_TYPE_WIFI]");
2190                 wifi_error_e rv = WIFI_ERROR_NONE;
2191                 int nWifiState;
2192                 wifi_ap_h ap_h;
2193                 char *ap_bssid = NULL;
2194                 int bssidlen;
2195                 bssid_info_s *wifi_info = NULL;
2196                 vconf_get_int(VCONFKEY_WIFI_STATE, &nWifiState);
2197
2198                 if (nWifiState != 0) {
2199                         LOGI_GEOFENCE("Wifi is on...");
2200                         geofence_server->running_wifi_cnt++;    /*Incrementing the counter for wifi fence*/
2201
2202                         if (item_data->priv != NULL)
2203                                 wifi_info = (bssid_info_s *) item_data->priv;
2204                         rv = wifi_get_connected_ap(&ap_h);
2205                         if (rv != WIFI_ERROR_NONE) {
2206                                 LOGI_GEOFENCE("Fail/not connected to get the connected AP: [%s] , geofence will be added to the fence list", __convert_wifi_error_to_string(rv));
2207                                 if (rv == WIFI_ERROR_NO_CONNECTION) {
2208                                         LOGI_GEOFENCE("Not connected to any AP");
2209                                         status_to_be_emitted = GEOFENCE_FENCE_STATE_OUT;
2210                                 }
2211                         } else {
2212                                 rv = wifi_ap_get_bssid(ap_h, &ap_bssid);
2213                                 if (rv != WIFI_ERROR_NONE) {
2214                                         LOGI_GEOFENCE("Fail to get the bssid: [%d]\n", __convert_wifi_error_to_string(rv));
2215                                 } else {
2216                                         bssidlen = strlen(ap_bssid);
2217                                         LOGI_GEOFENCE("Connected AP: %s, %d\n", ap_bssid, bssidlen);
2218                                         if (g_strcmp0(wifi_info->bssid, ap_bssid) == 0) {
2219                                                 status_to_be_emitted = GEOFENCE_FENCE_STATE_IN;
2220                                                 geofence_server->connectedTrackingWifiFenceId = fence_id;
2221                                         } else {
2222                                                 status_to_be_emitted = GEOFENCE_FENCE_STATE_OUT;
2223                                         }
2224
2225                                 }
2226                                 if (ap_bssid != NULL)
2227                                         free(ap_bssid);
2228                                 ap_bssid = NULL;
2229                         }
2230                 } else {
2231                         LOGI_GEOFENCE("Wifi is not switched on...");
2232                         geofence_server->running_wifi_cnt++;    /*Incrementing the counter for wifi fence*/
2233                         /*Emit the fence status as out as wifi is not switched on here*/
2234                         status_to_be_emitted = GEOFENCE_FENCE_STATE_OUT;
2235                 }
2236         } else {
2237                 LOGI_GEOFENCE("Invalid fence_type[%d]", item_data->common_info.type);
2238                 return;
2239         }
2240         /*Adding the fence to the tracking list*/
2241         LOGI_GEOFENCE("Add to tracklist: Fence id: %d", fence_id);
2242         if (geofence_server->tracking_list == NULL)
2243                 geofence_server->tracking_list = g_list_append(geofence_server->tracking_list, GINT_TO_POINTER(fence_id));
2244         else
2245                 geofence_server->tracking_list = g_list_insert_before(geofence_server->tracking_list, next_item_ptr, GINT_TO_POINTER(fence_id));
2246         LOGI_GEOFENCE("Added fence id: Fence id: %d", fence_id);
2247
2248         __emit_fence_event(geofence_server, place_id, fence_id, access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_FENCE_STARTED);
2249
2250         __emit_fence_inout(geofence_server, item_data->common_info.fence_id, status_to_be_emitted);
2251
2252         track_list = g_list_first(geofence_server->tracking_list);
2253         while (track_list) {
2254                 tracking_fence_id = GPOINTER_TO_INT(track_list->data);
2255                 LOGI_GEOFENCE("%d", tracking_fence_id);
2256                 track_list = g_list_next(track_list);
2257         }
2258 }
2259
2260 static void dbus_stop_geofence_cb(gint fence_id, const gchar *app_id, gpointer userdata)
2261 {
2262         __stop_geofence_service(fence_id, app_id, userdata);
2263 }
2264
2265 static void __start_activity_service(GeofenceServer *geofence_server)
2266 {
2267         FUNC_ENTRANCE_SERVER;
2268         bool activity_supported = TRUE;
2269         int ret = ACTIVITY_ERROR_NONE;
2270
2271         if (geofence_server->activity_stationary_h == NULL) {
2272                 activity_is_supported(ACTIVITY_STATIONARY, &activity_supported);
2273                 if (activity_supported == TRUE) {
2274                         ret = activity_create(&(geofence_server->activity_stationary_h));
2275                         if (ret != ACTIVITY_ERROR_NONE) {
2276                                 LOGD_GEOFENCE("Fail to create stationary activity %d", ret);
2277                         } else {
2278                                 ret = activity_start_recognition(geofence_server->activity_stationary_h, ACTIVITY_STATIONARY, __activity_cb, geofence_server);
2279                                 if (ret != ACTIVITY_ERROR_NONE)
2280                                         LOGD_GEOFENCE("Fail to start stationary activity %d", ret);
2281                                 else
2282                                         LOGD_GEOFENCE("Success to start stationary activity");
2283                         }
2284                 } else
2285                         LOGD_GEOFENCE("Not support stationary activity");
2286         }
2287
2288         if (geofence_server->activity_walk_h == NULL) {
2289                 activity_is_supported(ACTIVITY_WALK, &activity_supported);
2290                 if (activity_supported == TRUE) {
2291                         ret = activity_create(&(geofence_server->activity_walk_h));
2292                         if (ret != ACTIVITY_ERROR_NONE) {
2293                                 LOGD_GEOFENCE("Fail to create walk activity %d", ret);
2294                         } else {
2295                                 ret = activity_start_recognition(geofence_server->activity_walk_h, ACTIVITY_WALK, __activity_cb, geofence_server);
2296                                 if (ret != ACTIVITY_ERROR_NONE)
2297                                         LOGD_GEOFENCE("Fail to start walk activity %d", ret);
2298                                 else
2299                                         LOGD_GEOFENCE("Success to start walk activity");
2300                         }
2301                 } else
2302                         LOGD_GEOFENCE("Not support walk activity");
2303         }
2304
2305         if (geofence_server->activity_run_h == NULL) {
2306                 activity_is_supported(ACTIVITY_RUN, &activity_supported);
2307                 if (activity_supported == TRUE) {
2308                         ret = activity_create(&(geofence_server->activity_run_h));
2309                         if (ret != ACTIVITY_ERROR_NONE) {
2310                                 LOGD_GEOFENCE("Fail to create run activity %d", ret);
2311                         } else {
2312                                 ret = activity_start_recognition(geofence_server->activity_run_h, ACTIVITY_RUN, __activity_cb, geofence_server);
2313                                 if (ret != ACTIVITY_ERROR_NONE)
2314                                         LOGD_GEOFENCE("Fail to start run activity %d", ret);
2315                                 else
2316                                         LOGD_GEOFENCE("Success to start run activity");
2317                         }
2318                 } else
2319                         LOGD_GEOFENCE("Not support run activity");
2320         }
2321
2322         if (geofence_server->activity_in_vehicle_h == NULL) {
2323                 activity_is_supported(ACTIVITY_IN_VEHICLE, &activity_supported);
2324                 if (activity_supported == TRUE) {
2325                         ret = activity_create(&(geofence_server->activity_in_vehicle_h));
2326                         if (ret != ACTIVITY_ERROR_NONE) {
2327                                 LOGD_GEOFENCE("Fail to create in_vehicle activity %d", ret);
2328                         } else {
2329                                 ret = activity_start_recognition(geofence_server->activity_in_vehicle_h, ACTIVITY_IN_VEHICLE, __activity_cb, geofence_server);
2330                                 if (ret != ACTIVITY_ERROR_NONE)
2331                                         LOGD_GEOFENCE("Fail to start in_vehicle activity %d", ret);
2332                                 else
2333                                         LOGD_GEOFENCE("Success to start in_vehicle activity");
2334                         }
2335                 } else
2336                         LOGD_GEOFENCE("Not support in_vehicle activity");
2337         }
2338 }
2339
2340 static void __stop_activity_service(GeofenceServer *geofence_server)
2341 {
2342         FUNC_ENTRANCE_SERVER;
2343         int ret = ACTIVITY_ERROR_NONE;
2344
2345         if (geofence_server->activity_stationary_h != NULL) {
2346                 ret = activity_stop_recognition(geofence_server->activity_stationary_h);
2347                 if (ret != ACTIVITY_ERROR_NONE)
2348                         LOGD_GEOFENCE("Fail to stop stationary activity %d", ret);
2349                 else
2350                         LOGD_GEOFENCE("Success to stop stationary activity");
2351
2352                 ret = activity_release(geofence_server->activity_stationary_h);
2353                 if (ret != ACTIVITY_ERROR_NONE)
2354                         LOGD_GEOFENCE("Fail to release stationary activity %d", ret);
2355                 else
2356                         geofence_server->activity_stationary_h = NULL;
2357         }
2358
2359         if (geofence_server->activity_walk_h != NULL) {
2360                 ret = activity_stop_recognition(geofence_server->activity_walk_h);
2361                 if (ret != ACTIVITY_ERROR_NONE)
2362                         LOGD_GEOFENCE("Fail to stop walk activity %d", ret);
2363                 else
2364                         LOGD_GEOFENCE("Success to stop walk activity");
2365
2366                 ret = activity_release(geofence_server->activity_walk_h);
2367                 if (ret != ACTIVITY_ERROR_NONE)
2368                         LOGD_GEOFENCE("Fail to release walk activity %d", ret);
2369                 else
2370                         geofence_server->activity_walk_h = NULL;
2371         }
2372
2373         if (geofence_server->activity_run_h != NULL) {
2374                 ret = activity_stop_recognition(geofence_server->activity_run_h);
2375                 if (ret != ACTIVITY_ERROR_NONE)
2376                         LOGD_GEOFENCE("Fail to stop run activity %d", ret);
2377                 else
2378                         LOGD_GEOFENCE("Success to stop run activity");
2379
2380                 ret = activity_release(geofence_server->activity_run_h);
2381                 if (ret != ACTIVITY_ERROR_NONE)
2382                         LOGD_GEOFENCE("Fail to release run activity %d", ret);
2383                 else
2384                         geofence_server->activity_run_h = NULL;
2385         }
2386
2387         if (geofence_server->activity_in_vehicle_h != NULL) {
2388                 ret = activity_stop_recognition(geofence_server->activity_in_vehicle_h);
2389                 if (ret != ACTIVITY_ERROR_NONE)
2390                         LOGD_GEOFENCE("Fail to stop in_vehicle activity %d", ret);
2391                 else
2392                         LOGD_GEOFENCE("Success to stop in_vehicle activity");
2393
2394                 ret = activity_release(geofence_server->activity_in_vehicle_h);
2395                 if (ret != ACTIVITY_ERROR_NONE)
2396                         LOGD_GEOFENCE("Fail to release in_vehicle activity %d", ret);
2397                 else
2398                         geofence_server->activity_in_vehicle_h = NULL;
2399         }
2400 }
2401
2402 static void __activity_cb(activity_type_e type, const activity_data_h data, double timestamp, activity_error_e error, void *user_data)
2403 {
2404         FUNC_ENTRANCE_SERVER
2405         GeofenceServer *geofence_server = (GeofenceServer *)user_data;
2406         activity_accuracy_e accuracy = ACTIVITY_ACCURACY_LOW;
2407         int result = ACTIVITY_ERROR_NONE;
2408
2409         if (error != ACTIVITY_ERROR_NONE) {
2410                 LOGD_GEOFENCE("Error in activity callback %d", error);
2411                 return;
2412         }
2413
2414         result = activity_get_accuracy(data, &accuracy);
2415         if (result != ACTIVITY_ERROR_NONE) {
2416                 LOGD_GEOFENCE("Fail to get accuracy of activity %d", error);
2417                 return;
2418         }
2419
2420         if (accuracy >= ACTIVITY_ACCURACY_MID) {
2421                 geofence_server->activity_type = type;
2422                 geofence_server->activity_timestamp = timestamp;
2423
2424                 LOGD_GEOFENCE("Activity type = %d, timestamp = %lf", type, timestamp);
2425         }
2426 }
2427
2428 static GVariant *dbus_get_geofences_cb(int place_id, const gchar *app_id, int *fenceCnt, int *errorCode, gpointer userdata)
2429 {
2430         geofence_info_s *item;
2431         GVariantBuilder b;
2432         GList *fence_list = NULL, *list = NULL;
2433         place_info_s *place_info = NULL;
2434         int count = 0, fence_cnt = 0;
2435         int ret = 0;
2436
2437         LOGI_GEOFENCE(">>> Enter");
2438         /*As same API is used to get the fence list, whenever complete fence list is requested, place_id is passed as -1 from the module.*/
2439         /*Whenever fence_list in a particular place is requested place_id will not be -1. This is jusr maintained internally*/
2440         if (place_id == -1) {
2441                 ret = geofence_manager_get_fence_list_from_db(&count, &fence_list, -1);
2442         } else {
2443                 ret = geofence_manager_get_place_info(place_id, &place_info);
2444                 if (ret != FENCE_ERR_NONE || place_info == NULL) {
2445                         LOGE("Error getting the place info for place_id[%d]", place_id);
2446                         /* Send ZERO data gvariant*/
2447                         if (ret == FENCE_ERR_INTERNAL)
2448                                 *errorCode = GEOFENCE_SERVER_ERROR_OUT_OF_MEMORY;
2449                         else
2450                                 *errorCode = GEOFENCE_SERVER_ERROR_DATABASE;
2451                         *fenceCnt = fence_cnt;
2452                         g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2453                         g_free(place_info);
2454                         return g_variant_builder_end(&b);
2455                 }
2456                 if ((place_info != NULL) && (place_info->access_type == ACCESS_TYPE_PRIVATE)) {
2457                         if (g_strcmp0(app_id, place_info->appid) != 0) {
2458                                 LOGI_GEOFENCE("Not authorized to access this private place[%d]", place_id);
2459                                 if (place_info)
2460                                         g_free(place_info);
2461                                 /* Send ZERO data gvariant*/
2462                                 *errorCode = GEOFENCE_SERVER_ERROR_PLACE_ACCESS_DENIED;
2463                                 *fenceCnt = fence_cnt;
2464                                 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2465                                 return g_variant_builder_end(&b);
2466                         }
2467                         if (place_info)
2468                                 g_free(place_info);
2469                 }
2470                 ret = geofence_manager_get_fence_list_from_db(&count, &fence_list, place_id);
2471         }
2472         LOGI_GEOFENCE("count = %d", count);
2473
2474         if (ret != FENCE_ERR_NONE) {
2475                 LOGI_GEOFENCE("get list failed");
2476                 /* Send ZERO data gvariant*/
2477                 *errorCode = GEOFENCE_SERVER_ERROR_DATABASE;
2478                 *fenceCnt = fence_cnt;
2479                 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2480                 return g_variant_builder_end(&b);
2481         } else if (count == 0) {
2482                 LOGI_GEOFENCE("List is empty");
2483                 /* Send ZERO data gvariant*/
2484                 *errorCode = GEOFENCE_SERVER_ERROR_NONE;
2485                 *fenceCnt = fence_cnt;
2486                 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2487                 return g_variant_builder_end(&b);
2488         }
2489
2490         /* Initialize for the container*/
2491         g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2492
2493         list = g_list_first(fence_list);
2494         while (list) {
2495                 item = (geofence_info_s *) list->data;
2496
2497                 if (item && ((item->access_type == ACCESS_TYPE_PUBLIC) || !(g_strcmp0(app_id, item->app_id)))) {
2498                         /* Open container*/
2499                         g_variant_builder_open(&b, G_VARIANT_TYPE("a{sv}"));
2500
2501                         /* Add parameters to dictionary*/
2502                         g_variant_builder_add(&b, "{sv}", "fence_id", g_variant_new_int32(item->fence_id));
2503
2504                         LOGI_GEOFENCE("fence_id: %d, place_id: %d, latitude: %f, longitude: %f, radius: %d", item->fence_id, item->param.place_id, item->param.latitude, item->param.longitude, item->param.radius);
2505
2506                         switch (item->param.type) {
2507                         case GEOFENCE_TYPE_GEOPOINT: {
2508                                         g_variant_builder_add(&b, "{sv}", "place_id", g_variant_new_int32(item->param.place_id));
2509                                         g_variant_builder_add(&b, "{sv}", "geofence_type", g_variant_new_int32(item->param.type));
2510                                         g_variant_builder_add(&b, "{sv}", "latitude", g_variant_new_double(item->param.latitude));
2511                                         g_variant_builder_add(&b, "{sv}", "longitude", g_variant_new_double(item->param.longitude));
2512                                         g_variant_builder_add(&b, "{sv}", "radius", g_variant_new_int32(item->param.radius));
2513                                         g_variant_builder_add(&b, "{sv}", "address", g_variant_new_string(item->param.address));
2514                                         g_variant_builder_add(&b, "{sv}", "bssid", g_variant_new_string("NA"));
2515                                         g_variant_builder_add(&b, "{sv}", "ssid", g_variant_new_string("NA"));
2516                                 }
2517                                 break;
2518                         case GEOFENCE_TYPE_WIFI:
2519                         case GEOFENCE_TYPE_BT: {
2520                                         g_variant_builder_add(&b, "{sv}", "place_id", g_variant_new_int32(item->param.place_id));
2521                                         g_variant_builder_add(&b, "{sv}", "geofence_type", g_variant_new_int32(item->param.type));
2522                                         g_variant_builder_add(&b, "{sv}", "latitude", g_variant_new_double(0.0));
2523                                         g_variant_builder_add(&b, "{sv}", "longitude", g_variant_new_double(0.0));
2524                                         g_variant_builder_add(&b, "{sv}", "radius", g_variant_new_int32(0.0));
2525                                         g_variant_builder_add(&b, "{sv}", "address", g_variant_new_string("NA"));
2526                                         g_variant_builder_add(&b, "{sv}", "bssid", g_variant_new_string(item->param.bssid));
2527                                         g_variant_builder_add(&b, "{sv}", "ssid", g_variant_new_string(item->param.ssid));
2528                                 }
2529                                 break;
2530                         default:
2531                                 LOGI_GEOFENCE("Unsupported type: [%d]", item->param.type);
2532                                 break;
2533                         }
2534
2535                         /* Close container*/
2536                         g_variant_builder_close(&b);
2537                         fence_cnt++;
2538                 } else {
2539                         if (item != NULL)
2540                                 LOGI_GEOFENCE("This fence id: %d is private. Not authorized to access by this app", item->fence_id);
2541                 }
2542
2543                 /* Move to next node*/
2544                 list = g_list_next(list);
2545         }
2546         *errorCode = GEOFENCE_SERVER_ERROR_NONE;
2547         *fenceCnt = fence_cnt;
2548         return g_variant_builder_end(&b);
2549 }
2550
2551 static GVariant *dbus_get_places_cb(const gchar *app_id, int *placeCnt, int *errorCode, gpointer userdata)
2552 {
2553         place_info_s *item;
2554         GVariantBuilder b;
2555         GList *place_list = NULL, *list = NULL;
2556         int count = 0, place_cnt = 0;
2557         int ret = 0;
2558
2559         LOGI_GEOFENCE(">>> Enter");
2560
2561         ret = geofence_manager_get_place_list_from_db(&count, &place_list);
2562
2563         LOGI_GEOFENCE("count = %d", count);
2564
2565         if (ret != FENCE_ERR_NONE) {
2566                 LOGI_GEOFENCE("get list failed");
2567                 /* Send ZERO data gvariant*/
2568                 *errorCode = GEOFENCE_SERVER_ERROR_DATABASE;
2569                 *placeCnt = place_cnt;
2570                 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2571                 return g_variant_builder_end(&b);
2572         } else if (count == 0) {
2573                 LOGI_GEOFENCE("List is empty");
2574                 /* Send ZERO data gvariant*/
2575                 *errorCode = GEOFENCE_SERVER_ERROR_NONE;
2576                 *placeCnt = place_cnt;
2577                 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2578                 return g_variant_builder_end(&b);
2579         }
2580
2581         /* Initialize for the container*/
2582         g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2583
2584         list = g_list_first(place_list);
2585         while (list) {
2586                 item = (place_info_s *) list->data;
2587
2588                 if (item && ((item->access_type == ACCESS_TYPE_PUBLIC) || !(g_strcmp0(app_id, item->appid)))) {
2589                         /* Open container*/
2590                         g_variant_builder_open(&b, G_VARIANT_TYPE("a{sv}"));
2591
2592                         LOGI_GEOFENCE("place_id: %d, access_type: %d, place_name: %s, app_id: %s", item->place_id, item->access_type, item->place_name, item->appid);
2593                         /* Add data to dictionary*/
2594                         g_variant_builder_add(&b, "{sv}", "place_id", g_variant_new_int32(item->place_id));
2595                         g_variant_builder_add(&b, "{sv}", "place_name", g_variant_new_string(item->place_name));
2596                         /* Close container*/
2597                         g_variant_builder_close(&b);
2598                         place_cnt++;
2599                 } else {
2600                         if (item != NULL)
2601                                 LOGI_GEOFENCE("This place id: %d is private. Not authorized to access by this app", item->place_id);
2602                 }
2603                 list = g_list_next(list);
2604         }
2605         *errorCode = GEOFENCE_SERVER_ERROR_NONE;
2606         *placeCnt = place_cnt;
2607         return g_variant_builder_end(&b);
2608 }
2609
2610 static void __add_default_place(char *place_name)
2611 {
2612         int place_id;
2613         place_info_s *place_info = (place_info_s *) g_malloc0(sizeof(place_info_s));
2614         g_return_if_fail(place_info);
2615
2616         place_info->access_type = ACCESS_TYPE_PUBLIC;
2617         g_strlcpy(place_info->place_name, place_name, PLACE_NAME_LEN);
2618         g_strlcpy(place_info->appid, "dummy_app_id", APP_ID_LEN);
2619         /*Add the place details to db*/
2620         int ret = geofence_manager_set_place_info(place_info, &place_id);
2621         if (ret != FENCE_ERR_NONE)
2622                 LOGI_GEOFENCE("Unable to add the default places due to DB error");
2623         g_free(place_info);
2624 }
2625
2626 static void __init_geofencemanager(GeofenceServer *geofence_server)
2627 {
2628         FUNC_ENTRANCE_SERVER;
2629
2630         geofence_server->loc_gps_started_by_wps = false;
2631         geofence_server->loc_gps_started = false;
2632         geofence_server->loc_wps_started = false;
2633         geofence_server->nearestTrackingFence = 0;
2634         geofence_server->connectedTrackingWifiFenceId = -1;
2635         geofence_server->gps_fix_info = NULL;
2636         geofence_server->wps_fix_info = NULL;
2637         g_stpcpy(geofence_server->ble_info, "");
2638         geofence_server->gps_trigger_interval = 1; /* 1 sec by default*/
2639         geofence_server->timer_id = -1;
2640         geofence_server->gps_alarm_id = -1;
2641         geofence_server->wps_alarm_id = -1;
2642         geofence_server->gps_timeout_alarm_id = -1;
2643         geofence_server->wps_timeout_alarm_id = -1;
2644         geofence_server->geofence_list = NULL;
2645         geofence_server->tracking_list = NULL;
2646         geofence_server->running_geopoint_cnt = 0;
2647         geofence_server->running_bt_cnt = 0;
2648         geofence_server->running_wifi_cnt = 0;
2649         geofence_server->activity_type = ACTIVITY_IN_VEHICLE;
2650         geofence_server->activity_timestamp = 0;
2651
2652         geofence_server->activity_stationary_h = NULL;
2653         geofence_server->activity_walk_h = NULL;
2654         geofence_server->activity_run_h = NULL;
2655         geofence_server->activity_in_vehicle_h = NULL;
2656
2657         /*Initializing the DB to store the fence informations*/
2658         if (geofence_manager_db_init() != FENCE_ERR_NONE)
2659                 LOGI_GEOFENCE("Error initalizing the DB");
2660         /*Adding default places in the DB*/
2661         int place_id = DEFAULT_PLACE_HOME;
2662         int count = -1;
2663
2664         while (place_id <= DEFAULT_PLACE_CAR) {
2665                 if (geofence_manager_get_place_count_by_placeid(place_id, &count) == FENCE_ERR_NONE) {
2666                         if (count == 0) {
2667                                 if (place_id == DEFAULT_PLACE_HOME)
2668                                         __add_default_place("Home");
2669                                 else if (place_id == DEFAULT_PLACE_OFFICE)
2670                                         __add_default_place("Office");
2671                                 else if (place_id == DEFAULT_PLACE_CAR)
2672                                         __add_default_place("Car");
2673                         }
2674                 } else {
2675                         LOGI_GEOFENCE("Error adding the default place: %d", place_id);
2676                 }
2677                 place_id++;
2678                 count = -1;
2679         }
2680
2681         /*delete all fences at rebooting for a test. TODO: will be replaced by updating previous fences*/
2682         /*geofence_manager_db_reset();*/
2683 }
2684
2685
2686 #if USE_HW_GEOFENCE
2687 static void __start_gps_geofence_client(void *handle, GeofenceModCB geofence_cb, void *userdata)
2688 {
2689         FUNC_ENTRANCE_SERVER;
2690         /*Previously code to start the HW geofence client was there. It has been removed now*/
2691         return;
2692 }
2693
2694 static void __stop_gps_geofence_client(void *handle)
2695 {
2696         FUNC_ENTRANCE_SERVER;
2697         /*Stop tracking the geofences*/
2698         return;
2699 }
2700 #endif
2701
2702 int __copy_geofence_to_item_data(int fence_id, GeofenceItemData *item_data)
2703 {
2704         FUNC_ENTRANCE_SERVER;
2705         g_return_val_if_fail(item_data, FENCE_ERR_INVALID_PARAMETER);
2706         char *app_id = NULL;
2707
2708         item_data->common_info.fence_id = fence_id;
2709         item_data->common_info.status = GEOFENCE_FENCE_STATE_UNCERTAIN;
2710         if (FENCE_ERR_NONE != geofence_manager_get_geofence_type(fence_id, &item_data->common_info.type))
2711                 return FENCE_ERR_SQLITE_FAIL;
2712
2713         if (FENCE_ERR_NONE != geofence_manager_get_access_type(fence_id, -1, &item_data->common_info.access_type))
2714                 return FENCE_ERR_SQLITE_FAIL;
2715
2716         if (FENCE_ERR_NONE != geofence_manager_get_enable_status(fence_id, &item_data->common_info.enable))
2717                 return FENCE_ERR_SQLITE_FAIL;
2718
2719         if (FENCE_ERR_NONE != geofence_manager_get_appid_from_geofence(fence_id, &app_id)) {
2720                 g_free(app_id);
2721                 return FENCE_ERR_SQLITE_FAIL;
2722         } else {
2723                 g_strlcpy(item_data->common_info.appid, app_id, APP_ID_LEN);
2724                 g_free(app_id);
2725         }
2726         if (FENCE_ERR_NONE != geofence_manager_get_placeid_from_geofence(fence_id, &item_data->common_info.place_id))
2727                 return FENCE_ERR_SQLITE_FAIL;
2728
2729         if (FENCE_ERR_NONE != geofence_manager_get_running_status(fence_id, &item_data->common_info.running_status))
2730                 return FENCE_ERR_SQLITE_FAIL;
2731
2732         if (item_data->common_info.type == GEOFENCE_TYPE_GEOPOINT) {
2733                 geocoordinate_info_s *geocoordinate_info = NULL;
2734                 int ret = FENCE_ERR_NONE;
2735
2736                 ret = geofence_manager_get_geocoordinate_info(fence_id, &geocoordinate_info);
2737                 if (ret != FENCE_ERR_NONE || geocoordinate_info == NULL) {
2738                         LOGI_GEOFENCE("can not get geocoordinate_info");
2739                         return FENCE_ERR_SQLITE_FAIL;
2740                 }
2741                 item_data->priv = (void *) geocoordinate_info;
2742         } else {
2743                 bssid_info_s *bssid_info = NULL;
2744                 int ret = FENCE_ERR_NONE;
2745
2746                 ret = geofence_manager_get_bssid_info(fence_id, &bssid_info);
2747                 if (ret != FENCE_ERR_NONE || bssid_info == NULL) {
2748                         LOGI_GEOFENCE("can not get bssid_info");
2749                         return FENCE_ERR_SQLITE_FAIL;
2750                 }
2751                 item_data->priv = (void *) bssid_info;
2752         }
2753         return FENCE_ERR_NONE;
2754 }
2755
2756 static void __add_left_fences(gpointer user_data)
2757 {
2758         g_return_if_fail(user_data);
2759         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
2760         if (geofence_server->geofence_list != NULL)
2761                 return;
2762         GList *fence_list = NULL;
2763         int fence_id = 0;
2764         int count = 0;
2765
2766         /*Get the number of fences count*/
2767         geofence_manager_get_count_of_fences(&count);
2768         if (count <= 0)
2769                 return;
2770         /*Fetch the fences from the DB and populate it in the list*/
2771         geofence_manager_get_fences(NULL, 0, &fence_list);
2772         fence_list = g_list_first(fence_list);
2773         while (fence_list) {
2774                 fence_id = GPOINTER_TO_INT(fence_list->data);
2775
2776                 /*if(geofence_server is not restarted by dbus auto activation method[It means phone is rebooted so app should start the fences again. If we start the service by ourself it is waste of power. So we should not do that])
2777                  * {
2778                  * geofence_manager_set_running_status(fence_id, 0); // resetting the running-status flag since it is a device reboot
2779                  * } */
2780
2781                 GeofenceItemData *item_data = (GeofenceItemData *)g_malloc0(sizeof(GeofenceItemData));
2782                 if (FENCE_ERR_NONE != __copy_geofence_to_item_data(fence_id, item_data)) {
2783                         g_free(item_data);
2784                         return;
2785                 }
2786                 LOGI_GEOFENCE("adding fence_id = %d to fence_list", item_data->common_info.fence_id);
2787
2788                 /*Here fences from DB will be added to the list but tracking list is not populated here*/
2789                 geofence_server->geofence_list = g_list_append(geofence_server->geofence_list, item_data);
2790
2791                 fence_list = g_list_next(fence_list);
2792         }
2793 }
2794
2795 static void _glib_log(const gchar *log_domain, GLogLevelFlags log_level, const gchar *msg, gpointer user_data)
2796 {
2797         LOGI_GEOFENCE("GLIB[%d] : %s", log_level, msg);
2798 }
2799
2800 int main(int argc, char **argv)
2801 {
2802         GeofenceServer *geofenceserver = NULL;
2803         LOGI_GEOFENCE("----------------Starting Server -----------------------------");
2804         /*Callback registrations*/
2805         geofence_callbacks cb;
2806         cb.bt_conn_state_changed_cb = bt_conn_state_changed;
2807         cb.bt_apater_disable_cb = bt_adp_disable;
2808         cb.device_display_changed_cb = device_display_changed_cb;
2809         cb.wifi_conn_state_changed_cb = wifi_conn_state_changed;
2810         cb.wifi_device_state_changed_cb = wifi_device_state_changed;
2811         cb.network_evt_cb = geofence_network_evt_cb;
2812         cb.bt_discovery_cb = bt_adapter_device_discovery_state_cb;
2813         cb.gps_setting_changed_cb = gps_setting_changed_cb;
2814         cb.wifi_rssi_level_changed_cb = wifi_rssi_level_changed;
2815 #if !GLIB_CHECK_VERSION(2, 35, 0)
2816         g_type_init();
2817 #endif
2818         geofenceserver = g_new0(GeofenceServer, 1);
2819         if (!geofenceserver) {
2820                 LOGI_GEOFENCE("GeofenceServer create fail");
2821                 return 1;
2822         }
2823
2824         if (alarmmgr_init(PACKAGE_NAME) != ALARMMGR_RESULT_SUCCESS) {
2825                 LOGI_GEOFENCE("alarmmgr_init fail");
2826                 return 1;
2827         }
2828
2829         g_log_set_default_handler(_glib_log, geofenceserver);
2830
2831         /*Initialize the geofence manager where DB related activities exists*/
2832         __init_geofencemanager(geofenceserver);
2833
2834         /*This will read the DB and populate the list*/
2835         __add_left_fences(geofenceserver);
2836
2837         /*This call goes and registers the cb with Server.c where all the wifi,bt event callbacks are triggered*/
2838         _geofence_register_update_callbacks(&cb, geofenceserver);
2839
2840         /*This call goes and make initializations of bt and wifi stacks and then register the callbacks with them*/
2841         _geofence_initialize_geofence_server(geofenceserver);
2842
2843 #ifdef TIZEN_ENGINEER_MODE
2844         _init_log();
2845 #endif
2846
2847         /* This call goes to Geofence_dbus_server.c and creates the actual server dbus connection who will interact with the client*/
2848         geofence_dbus_callback_s *dbus_callback;
2849         dbus_callback = g_new0(geofence_dbus_callback_s, 1);
2850         g_return_val_if_fail(dbus_callback, GEOFENCE_DBUS_SERVER_ERROR_MEMORY);
2851
2852         dbus_callback->add_geofence_cb = dbus_add_fence_cb;
2853         dbus_callback->delete_geofence_cb = dbus_remove_fence_cb;
2854         dbus_callback->get_geofences_cb = dbus_get_geofences_cb;
2855         dbus_callback->enable_geofence_cb = dbus_enable_geofence_cb;
2856         dbus_callback->start_geofence_cb = dbus_start_geofence_cb;
2857         dbus_callback->stop_geofence_cb = dbus_stop_geofence_cb;
2858         dbus_callback->add_place_cb = dbus_add_place_cb;
2859         dbus_callback->update_place_cb = dbus_update_place_cb;
2860         dbus_callback->delete_place_cb = dbus_remove_place_cb;
2861         dbus_callback->get_place_name_cb = dbus_get_place_name_cb;
2862         dbus_callback->get_places_cb = dbus_get_places_cb;
2863
2864         geofence_dbus_server_create(&(geofenceserver->geofence_dbus_server), dbus_callback, (void *) geofenceserver);
2865
2866         LOGD_GEOFENCE("lbs_geofence_server_creation done");
2867
2868         geofenceserver->loop = g_main_loop_new(NULL, TRUE);
2869         g_main_loop_run(geofenceserver->loop);
2870
2871         LOGD_GEOFENCE("GEOFENCE_manager deamon Stop....");
2872
2873         /*This call goes to server.c and deregisters all the callbacks w.r.t bt and wifi*/
2874         _geofence_deinitialize_geofence_server();
2875
2876 #ifdef TIZEN_ENGINEER_MODE
2877         _deinit_log();
2878 #endif
2879         /*This call goes to Geofence_dbus_server.c and deletes the memory allocated to the server, hence destroys it*/
2880         geofence_dbus_server_destroy(geofenceserver->geofence_dbus_server);
2881         LOGD_GEOFENCE("lbs_server_destroy called");
2882
2883         g_free(dbus_callback);
2884         g_main_loop_unref(geofenceserver->loop);
2885         g_free(geofenceserver);
2886
2887         /*Closing the DB and the handle is aquired again when geofence server comes up.*/
2888         geofence_manager_close_db();
2889
2890         return 0;
2891 }