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