1 /* Copyright 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <glib-object.h>
22 #include <geofence_client.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"
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>
41 #define TIZEN_ENGINEER_MODE
42 #ifdef TIZEN_ENGINEER_MODE
43 #include "geofence_server_log.h"
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
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
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);
72 static bool __is_support_wps()
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);
80 return is_wps_supported;
83 static const char *__convert_wifi_error_to_string(wifi_manager_error_e err_type)
86 case WIFI_MANAGER_ERROR_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:
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";
121 return "NOT Defined";
125 void emit_proximity_using_ble(GeofenceServer *geofence_server, int fence_id, geofence_proximity_state_e state)
127 FUNC_ENTRANCE_SERVER;
128 g_return_if_fail(geofence_server);
129 GeofenceItemData *item_data = __get_item_by_fence_id(fence_id, geofence_server);
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;
137 LOGD_GEOFENCE("Invalid item_data");
141 static bool __check_for_match(char *str1, char *str2)
143 if (g_strrstr(str1, str2) == NULL)
148 static void bt_le_scan_result_cb(int result, bt_adapter_le_device_scan_result_info_s *info, void *user_data)
150 int ret = BT_ERROR_NONE;
151 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
152 LOGI_GEOFENCE("Current addresses: %s", geofence_server->ble_info);
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);
161 LOGI_GEOFENCE("Received address: %s", info->remote_address);
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);
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);
181 void bt_le_scan_result_display_cb(int result, bt_adapter_le_device_scan_result_info_s *info, void *user_data)
183 FUNC_ENTRANCE_SERVER;
184 int ret = BT_ERROR_NONE;
185 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
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);
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);
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);
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);
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");
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*/
241 if (bt_device_info != NULL)
242 bt_adapter_free_device_info(bt_device_info);
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);
257 tracking_list = g_list_next(tracking_list);
262 void device_display_changed_cb(device_callback_e type, void *value, void *user_data)
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);
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");
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);
281 } else if (state == DISPLAY_STATE_SCREEN_DIM)
282 LOGI_GEOFENCE("State: DIM");
284 LOGI_GEOFENCE("State: OFF");
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)
291 FUNC_ENTRANCE_SERVER;
292 g_return_val_if_fail(geofence_server, -1);
294 LOGD_GEOFENCE("place_id: %d, fence_id: %d, access_type: %d, error: %d, state: %d", place_id, fence_id, access_type, error, state);
296 geofence_dbus_server_send_geofence_event_changed(geofence_server->geofence_dbus_server, place_id, fence_id, access_type, app_id, error, state);
300 static int __emit_fence_inout(GeofenceServer *geofence_server, int fence_id, geofence_fence_state_e state)
302 FUNC_ENTRANCE_SERVER;
303 g_return_val_if_fail(geofence_server, -1);
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");
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;
320 LOGD_GEOFENCE("%d : FENCE_IN", fence_id);
321 #ifdef TIZEN_ENGINEER_MODE
322 GEOFENCE_PRINT_LOG("FENCE_IN");
326 item_data->common_info.status = GEOFENCE_FENCE_STATE_IN;
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;
336 LOGD_GEOFENCE("%d : FENCE_OUT", fence_id);
337 #ifdef TIZEN_ENGINEER_MODE
338 GEOFENCE_PRINT_LOG("FENCE_OUT");
341 LOGD_GEOFENCE("Fence status [%d]", item_data->common_info.status);
343 item_data->common_info.status = GEOFENCE_FENCE_STATE_OUT;
345 LOGD_GEOFENCE("Not emit, Prev[%d], Curr[%d]", item_data->common_info.status, state);
351 static int __emit_fence_proximity(GeofenceServer *geofence_server, int fence_id, geofence_proximity_state_e state)
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);
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;
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;
366 LOGD_GEOFENCE("Invalid item_data");
372 static void __check_proximity_for_fence(double distance, int fence_id, int radius, geofence_proximity_state_e current_state, void *user_data)
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;
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);
404 static void __check_inout_by_gps(double latitude, double longitude, int fence_id, void *user_data)
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)
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);
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;
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;
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);
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");
435 static void __stop_gps_alarms(void *user_data)
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);
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);
451 static void __stop_wps_alarms(void *user_data)
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);
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);
467 static void __check_current_location_cb(double latitude, double longitude, double altitude, time_t timestamp, void *user_data)
469 FUNC_ENTRANCE_SERVER;
470 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
472 GList *tracking_list = NULL;
473 GeofenceItemData *item_data = NULL;
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");
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);
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);
489 tracking_list = g_list_next(tracking_list);
493 static int __start_wps_positioning(GeofenceServer *geofence_server, location_position_updated_cb callback)
495 FUNC_ENTRANCE_SERVER;
496 g_return_val_if_fail(geofence_server, -1);
497 int ret = FENCE_ERR_NONE;
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;
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;
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;
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);
523 geofence_server->loc_wps_started = TRUE;
525 LOGD_GEOFENCE("loc_wps_started TRUE");
531 static int __start_gps_positioning(GeofenceServer *geofence_server, location_position_updated_cb callback)
533 FUNC_ENTRANCE_SERVER;
534 g_return_val_if_fail(geofence_server, -1);
535 int ret = FENCE_ERR_NONE;
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;
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;
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;
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);
563 geofence_server->loc_gps_started = TRUE;
565 LOGD_GEOFENCE("loc_gps_started TRUE");
571 static void __stop_gps_positioning(gpointer userdata)
573 FUNC_ENTRANCE_SERVER;
574 g_return_if_fail(userdata);
575 GeofenceServer *geofence_server = (GeofenceServer *) userdata;
577 if (geofence_server->loc_gps_started == TRUE) {
578 ret = location_manager_stop(geofence_server->loc_gps_manager);
579 if (ret != LOCATIONS_ERROR_NONE)
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)
587 if (geofence_server->loc_gps_manager != NULL) {
588 ret = location_manager_destroy(geofence_server->loc_gps_manager);
589 if (ret != LOCATIONS_ERROR_NONE)
591 geofence_server->loc_gps_manager = NULL;
595 static void __stop_wps_positioning(gpointer userdata)
597 FUNC_ENTRANCE_SERVER;
598 g_return_if_fail(userdata);
599 GeofenceServer *geofence_server = (GeofenceServer *) userdata;
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");
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");
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");
621 geofence_server->loc_wps_manager = NULL;
625 static int __get_time_diff(int timestamp)
627 int current_time = 0;
629 current_time = (g_get_real_time()/1000000);
630 timediff = current_time - timestamp;
634 static void __process_best_location(GeofenceServer *geofence_server)
636 FUNC_ENTRANCE_SERVER;
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)
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);
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);
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);
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);
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);
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);
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);
678 static void __geofence_standalone_gps_position_changed_cb(double latitude, double longitude, double altitude, time_t timestamp, void *user_data)
680 FUNC_ENTRANCE_SERVER;
681 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
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;
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));
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;
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);
710 geofence_server->last_loc_time = timestamp;
711 __check_current_location_cb(latitude, longitude, altitude, timestamp, user_data);
713 /* Distance based alarm */
714 distance = _get_min_distance(latitude, longitude, &min_fence_id, geofence_server);
715 geofence_server->nearestTrackingFence = min_fence_id;
717 if (distance < 200) {
718 LOGD_GEOFENCE("interval: 1 secs");
720 } else if (distance < 500) {
721 LOGD_GEOFENCE("interval: 3 secs");
723 } else if (distance < 1000) {
724 LOGD_GEOFENCE("interval: 6 secs");
726 } else if (distance < 2000) {
727 LOGD_GEOFENCE("interval: 20 secs");
729 } else if (distance < 3000) {
730 LOGD_GEOFENCE("interval : 1 min");
732 } else if (distance < 5000) {
733 LOGD_GEOFENCE("interval: 2 mins");
735 } else if (distance < 10000) {
736 LOGD_GEOFENCE("interval: 5 mins");
738 } else if (distance < 20000) {
739 LOGD_GEOFENCE("interval : 10 mins");
741 } else if (distance < 100000) {
742 LOGD_GEOFENCE("interval : 20 mins");
745 LOGD_GEOFENCE("interval : 60 mins");
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;
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;
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);
771 static void __geofence_gps_position_changed_cb(double latitude, double longitude, double altitude, time_t timestamp, void *user_data)
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;
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);
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));
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;
805 LOGD_GEOFENCE("Invalid GPS fix data");
808 geofence_server->last_loc_time = timestamp;
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);
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;
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);
838 static void __geofence_wps_position_changed_cb(double latitude, double longitude, double altitude, time_t timestamp, void *user_data)
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;
848 location_accuracy_level_e level;
849 int ret = LOCATIONS_ERROR_NONE;
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);
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;
872 LOGD_GEOFENCE("Invalid WPS fix data");
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*/
881 item_data = __get_item_by_fence_id(min_fence_id, geofence_server);
884 return;/* There is no valid fence with this fence id. So return*/
886 geocoordinate_info_s *geocoordinate_info = (geocoordinate_info_s *)item_data->priv;
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*/
899 interval = 15; /*15 sec */
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;
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);
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);
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;
926 LOGI_GEOFENCE("Error starting GPS/ GPS is off");
930 __check_current_location_cb(latitude, longitude, altitude, timestamp, user_data); /* Its WPS only mode so no need to worry abt accuracy */
933 static void __set_interval_for_gps(double min_distance, int min_fence_id, void *user_data)
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))) {
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)) {
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 */
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);
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)
979 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
980 GeofenceItemData *item_data = NULL;
982 int tracking_fence_id = 0;
983 GList *tracking_fences = g_list_first(geofence_server->tracking_list);
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);
998 __emit_fence_inout(geofence_server, item_data->common_info.fence_id, GEOFENCE_FENCE_STATE_OUT);
999 item_data->is_bt_status_in = false;
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);
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);
1017 static void geofence_network_evt_cb(net_event_info_t *event_cb, void *user_data)
1019 FUNC_ENTRANCE_SERVER;
1020 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
1021 g_return_if_fail(geofence_server);
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;
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);
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);
1062 LOGE_GEOFENCE("WPS is not supported");
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);
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);
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;
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);
1100 static int __gps_timeout_cb(alarm_id_t alarm_id, void *user_data)
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);
1112 geofence_server->loc_gps_started_by_wps = FALSE;
1116 static int __gps_alarm_cb(alarm_id_t alarm_id, void *user_data)
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);
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);
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);
1139 static int __wps_timeout_cb(alarm_id_t alarm_id, void *user_data)
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*/
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);
1156 static int __wps_alarm_cb(alarm_id_t alarm_id, void *user_data)
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;
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);
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);
1180 static void gps_setting_changed_cb(location_method_e method, bool enable, void *user_data)
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;
1191 /* Get the wps status */
1192 vconf_get_int(VCONFKEY_LOCATION_NETWORK_ENABLED, &wps_state);
1193 vconf_get_int(VCONFKEY_LOCATION_ENABLED, &gps_state);
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);
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;
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);
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;
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)*/
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);
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);
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);
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);
1271 /*********************************THIS HAS TO BE USED ONLY FOR TESTING*********************************************/
1272 #ifdef __LOCAL_TEST__
1273 static void __free_geofence_list(gpointer userdata)
1275 GeofenceServer *geofence_server = (GeofenceServer *) userdata;
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;
1282 tmp_fence_list = g_list_next(tmp_fence_list);
1284 geofence_server->geofence_list = NULL;
1288 static int __check_fence_permission(int fence_id, const char *app_id)
1290 access_type_e access_type = ACCESS_TYPE_PUBLIC;
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");
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);
1304 if (g_strcmp0(appid, app_id)) {
1305 LOGE("Not authorized to access this private fence[%d]", fence_id);
1314 static int __check_place_permission(int place_id, const char *app_id)
1316 access_type_e access_type = ACCESS_TYPE_PUBLIC;
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");
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);
1330 if (g_strcmp0(appid, app_id)) {
1331 LOGE("Not authorized to access this private place[%d]", place_id);
1340 static void __stop_geofence_service(gint fence_id, const gchar *app_id, gpointer userdata)
1342 FUNC_ENTRANCE_SERVER;
1343 g_return_if_fail(userdata);
1345 GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1346 GeofenceItemData *item_data = NULL;
1347 int tracking_status = -1;
1348 int ret = FENCE_ERR_NONE;
1350 access_type_e access_type = ACCESS_TYPE_UNKNOWN;
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*/
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);
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);
1370 ret = __check_fence_permission(fence_id, app_id);
1372 LOGE("Permission denied or DB error occured while accessing the fence[%d]", fence_id);
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);
1376 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STOPPED);
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);
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);
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);
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);
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);
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;
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;
1425 __stop_gps_alarms(geofence_server);
1426 __stop_wps_alarms(geofence_server);
1427 __stop_activity_service(geofence_server);
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;
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);
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;
1456 LOGI_GEOFENCE("Geofence service is not running for this fence");
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);
1465 static bool __isWifiOn(void)
1467 int network_state = -1;
1468 vconf_get_int(VCONFKEY_WIFI_STATE, &network_state);
1469 if (network_state == 0)
1474 static bool __isDataConnected(void)
1476 bool isDataConnected = false;
1477 int network_state = -1;
1478 int data_state = -1;
1480 int rv = vconf_get_int(VCONFKEY_NETWORK_WIFI_STATE, &network_state);
1482 if (network_state == VCONFKEY_NETWORK_WIFI_CONNECTED) {
1483 LOGI_GEOFENCE("USING WIFI DATA");
1484 isDataConnected = true;
1487 if (isDataConnected == false) {
1488 rv = vconf_get_int(VCONFKEY_NETWORK_CELLULAR_STATE, &network_state);
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;
1499 return isDataConnected;
1502 static int dbus_add_fence_cb(const gchar *app_id,
1508 const gchar *address,
1509 const gchar *bssid, const gchar *ssid, gpointer userdata)
1511 FUNC_ENTRANCE_SERVER;
1512 GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1514 /* create fence id*/
1516 int ret = FENCE_ERR_NONE;
1517 void *next_item_ptr = NULL;
1520 access_type_e access_type;
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);
1529 ret = __check_place_permission(place_id, app_id);
1531 LOGE("Unable to add the fence. Permission denied or DB error occured while accessing the place[%d]", place_id);
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);
1535 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_ADDED);
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);
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;
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;
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);
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);
1577 geocoordinate_info->latitude = latitude;
1578 geocoordinate_info->longitude = longitude;
1579 if (radius < GEOFENCE_DEFAULT_RADIUS)
1580 geocoordinate_info->radius = GEOFENCE_DEFAULT_RADIUS;
1582 geocoordinate_info->radius = radius;
1583 g_strlcpy(geocoordinate_info->address, address, ADDRESS_LEN);
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);
1597 item_data->priv = (void *) geocoordinate_info;
1599 } else if (geofence_type == GEOFENCE_TYPE_WIFI) { /* Specific AP */
1600 LOGD_GEOFENCE("Add geofence with specific AP");
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);
1610 g_strlcpy(wifi_info->bssid, bssid, WLAN_BSSID_LEN);
1611 g_strlcpy(wifi_info->ssid, ssid, WLAN_BSSID_LEN);
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);
1625 item_data->priv = (void *) wifi_info;
1626 } else if (geofence_type == GEOFENCE_TYPE_BT) {
1627 LOGD_GEOFENCE("Add geofence with bluetooth bssid");
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);
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);
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);
1653 item_data->priv = (void *) bt_info;
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);
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*/
1662 GList *temp_list = g_list_first(geofence_server->geofence_list);
1663 temp_list = g_list_first(geofence_server->geofence_list);
1666 temp_list = g_list_next(temp_list);
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);
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);
1677 static int dbus_add_place_cb(const gchar *app_id, const gchar *place_name, gpointer userdata)
1679 FUNC_ENTRANCE_SERVER;
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));
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);
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;
1694 place_info->access_type = ACCESS_TYPE_PRIVATE;
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);
1706 __emit_fence_event(geofence_server, place_id, -1, place_info->access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_PLACE_ADDED);
1711 static void dbus_enable_geofence_cb(gint fence_id, const gchar *app_id, gboolean enable, gpointer userdata)
1713 FUNC_ENTRANCE_SERVER;
1714 g_return_if_fail(userdata);
1716 GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1717 int ret = FENCE_ERR_NONE;
1718 access_type_e access_type = ACCESS_TYPE_UNKNOWN;
1720 int enable_status = 0;
1721 geofence_manage_e manage_enum = GEOFENCE_MANAGE_SETTING_ENABLED;
1724 manage_enum = GEOFENCE_MANAGE_SETTING_DISABLED;
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);
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);
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);
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);
1754 LOGI_GEOFENCE("Currently, only public fences can be enabled/disabled. It can be done by MyPlaces app only.");
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);
1760 static void dbus_update_place_cb(gint place_id, const gchar *app_id, const gchar *place_name, gpointer userdata)
1762 FUNC_ENTRANCE_SERVER;
1763 int ret = FENCE_ERR_NONE;
1764 GeofenceServer *geofence_server = (GeofenceServer *) userdata;
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);
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);
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);
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);
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);
1799 __emit_fence_event(geofence_server, place_id, -1, place_info->access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_PLACE_UPDATED);
1803 static void dbus_remove_fence_cb(gint fence_id, const gchar *app_id, gpointer userdata)
1805 FUNC_ENTRANCE_SERVER;
1806 GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1807 g_return_if_fail(geofence_server);
1808 int ret = FENCE_ERR_NONE;
1810 char *app_id_db = NULL;
1811 access_type_e access_type = ACCESS_TYPE_UNKNOWN;
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);
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);
1827 if (g_strcmp0(app_id_db, app_id) != 0) {
1828 LOGI_GEOFENCE("Not authorized to remove the fence");
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);
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);
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);
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);
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);
1866 __stop_geofence_service(fence_id, app_id, userdata);
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);
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*/
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;
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);
1890 static void dbus_get_place_name_cb(gint place_id, const gchar *app_id, char **place_name, int *error_code, gpointer userdata)
1892 FUNC_ENTRANCE_SERVER;
1893 access_type_e access_type = ACCESS_TYPE_UNKNOWN;
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;
1902 ret = __check_place_permission(place_id, app_id);
1904 LOGE("Unable to get the place name. Permission denied or DB error occured while accessing the place[%d]", place_id);
1906 *error_code = GEOFENCE_SERVER_ERROR_PLACE_ACCESS_DENIED;
1908 *error_code = GEOFENCE_SERVER_ERROR_DATABASE;
1911 ret = geofence_manager_get_place_name(place_id, place_name);
1912 if (ret != FENCE_ERR_NONE) {
1913 *error_code = GEOFENCE_SERVER_ERROR_DATABASE;
1916 *error_code = GEOFENCE_SERVER_ERROR_NONE;
1919 static void dbus_remove_place_cb(gint place_id, const gchar *app_id, gpointer userdata)
1921 FUNC_ENTRANCE_SERVER;
1922 g_return_if_fail(userdata);
1923 GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1924 GList *fence_list = NULL, *list = NULL;
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;
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);
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);
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);
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);
1956 if (g_strcmp0(app_id, place_info->appid) != 0) {
1957 LOGI_GEOFENCE("Not authorized to remove the place");
1959 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_PLACE_ACCESS_DENIED, GEOFENCE_MANAGE_PLACE_REMOVED);
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);
1971 list = g_list_first(fence_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);
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);
1991 __stop_geofence_service(fence_id, app_id, userdata);
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);
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;
2003 list = g_list_next(list);
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);
2011 __emit_fence_event(geofence_server, place_id, -1, access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_PLACE_REMOVED);
2014 static void dbus_start_geofence_cb(gint fence_id, const gchar *app_id, gpointer userdata)
2016 FUNC_ENTRANCE_SERVER;
2017 g_return_if_fail(userdata);
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;
2025 int ret = FENCE_ERR_NONE;
2026 int tracking_status = -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;
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*/
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);
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);
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);
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");
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);
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);
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);
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);
2087 tracking_status = 1;
2089 } else if (access_type == ACCESS_TYPE_PUBLIC) {
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);
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);
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);
2110 __emit_fence_event(geofence_server, place_id, fence_id, access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_FENCE_STARTED);
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);
2123 item_data->client_status = GEOFENCE_CLIENT_STATUS_START;
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);
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);
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]");
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;
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;
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;
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;
2181 status_to_be_emitted = GEOFENCE_FENCE_STATE_OUT;
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);
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);
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;
2203 wifi_manager_ap_h ap_h;
2204 char *ap_bssid = NULL;
2206 bssid_info_s *wifi_info = NULL;
2207 vconf_get_int(VCONFKEY_WIFI_STATE, &nWifiState);
2209 if (nWifiState != 0) {
2210 LOGI_GEOFENCE("Wifi is on...");
2211 geofence_server->running_wifi_cnt++; /*Incrementing the counter for wifi fence*/
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;
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));
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;
2233 status_to_be_emitted = GEOFENCE_FENCE_STATE_OUT;
2237 if (ap_bssid != NULL)
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;
2248 LOGI_GEOFENCE("Invalid fence_type[%d]", item_data->common_info.type);
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));
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);
2259 __emit_fence_event(geofence_server, place_id, fence_id, access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_FENCE_STARTED);
2261 __emit_fence_inout(geofence_server, item_data->common_info.fence_id, status_to_be_emitted);
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);
2271 static void dbus_stop_geofence_cb(gint fence_id, const gchar *app_id, gpointer userdata)
2273 __stop_geofence_service(fence_id, app_id, userdata);
2276 static void __start_activity_service(GeofenceServer *geofence_server)
2278 FUNC_ENTRANCE_SERVER;
2279 bool activity_supported = TRUE;
2280 int ret = ACTIVITY_ERROR_NONE;
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);
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);
2293 LOGD_GEOFENCE("Success to start stationary activity");
2296 LOGD_GEOFENCE("Not support stationary activity");
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);
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);
2310 LOGD_GEOFENCE("Success to start walk activity");
2313 LOGD_GEOFENCE("Not support walk activity");
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);
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);
2327 LOGD_GEOFENCE("Success to start run activity");
2330 LOGD_GEOFENCE("Not support run activity");
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);
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);
2344 LOGD_GEOFENCE("Success to start in_vehicle activity");
2347 LOGD_GEOFENCE("Not support in_vehicle activity");
2351 static void __stop_activity_service(GeofenceServer *geofence_server)
2353 FUNC_ENTRANCE_SERVER;
2354 int ret = ACTIVITY_ERROR_NONE;
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);
2361 LOGD_GEOFENCE("Success to stop stationary activity");
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);
2367 geofence_server->activity_stationary_h = NULL;
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);
2375 LOGD_GEOFENCE("Success to stop walk activity");
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);
2381 geofence_server->activity_walk_h = NULL;
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);
2389 LOGD_GEOFENCE("Success to stop run activity");
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);
2395 geofence_server->activity_run_h = NULL;
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);
2403 LOGD_GEOFENCE("Success to stop in_vehicle activity");
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);
2409 geofence_server->activity_in_vehicle_h = NULL;
2413 static void __activity_cb(activity_type_e type, const activity_data_h data, double timestamp, activity_error_e error, void *user_data)
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;
2420 if (error != ACTIVITY_ERROR_NONE) {
2421 LOGD_GEOFENCE("Error in activity callback %d", error);
2425 result = activity_get_accuracy(data, &accuracy);
2426 if (result != ACTIVITY_ERROR_NONE) {
2427 LOGD_GEOFENCE("Fail to get accuracy of activity %d", error);
2431 if (accuracy >= ACTIVITY_ACCURACY_MID) {
2432 geofence_server->activity_type = type;
2433 geofence_server->activity_timestamp = timestamp;
2435 LOGD_GEOFENCE("Activity type = %d, timestamp = %lf", type, timestamp);
2439 static GVariant *dbus_get_geofences_cb(int place_id, const gchar *app_id, int *fenceCnt, int *errorCode, gpointer userdata)
2441 geofence_info_s *item;
2443 GList *fence_list = NULL, *list = NULL;
2444 place_info_s *place_info = NULL;
2445 int count = 0, fence_cnt = 0;
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);
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;
2461 *errorCode = GEOFENCE_SERVER_ERROR_DATABASE;
2462 *fenceCnt = fence_cnt;
2463 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2465 return g_variant_builder_end(&b);
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}"));
2475 return g_variant_builder_end(&b);
2478 ret = geofence_manager_get_fence_list_from_db(&count, &fence_list, place_id);
2480 LOGI_GEOFENCE("count = %d", count);
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);
2499 /* Initialize for the container*/
2500 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2502 list = g_list_first(fence_list);
2504 item = (geofence_info_s *) list->data;
2506 if (item && ((item->access_type == ACCESS_TYPE_PUBLIC) || !(g_strcmp0(app_id, item->app_id)))) {
2508 g_variant_builder_open(&b, G_VARIANT_TYPE("a{sv}"));
2510 /* Add parameters to dictionary*/
2511 g_variant_builder_add(&b, "{sv}", "fence_id", g_variant_new_int32(item->fence_id));
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);
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"));
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));
2540 LOGI_GEOFENCE("Unsupported type: [%d]", item->param.type);
2544 /* Close container*/
2545 g_variant_builder_close(&b);
2549 LOGI_GEOFENCE("This fence id: %d is private. Not authorized to access by this app", item->fence_id);
2552 /* Move to next node*/
2553 list = g_list_next(list);
2555 *errorCode = GEOFENCE_SERVER_ERROR_NONE;
2556 *fenceCnt = fence_cnt;
2557 return g_variant_builder_end(&b);
2560 static GVariant *dbus_get_places_cb(const gchar *app_id, int *placeCnt, int *errorCode, gpointer userdata)
2564 GList *place_list = NULL, *list = NULL;
2565 int count = 0, place_cnt = 0;
2568 LOGI_GEOFENCE(">>> Enter");
2570 ret = geofence_manager_get_place_list_from_db(&count, &place_list);
2572 LOGI_GEOFENCE("count = %d", count);
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);
2590 /* Initialize for the container*/
2591 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2593 list = g_list_first(place_list);
2595 item = (place_info_s *) list->data;
2597 if (item && ((item->access_type == ACCESS_TYPE_PUBLIC) || !(g_strcmp0(app_id, item->appid)))) {
2599 g_variant_builder_open(&b, G_VARIANT_TYPE("a{sv}"));
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);
2610 LOGI_GEOFENCE("This place id: %d is private. Not authorized to access by this app", item->place_id);
2612 list = g_list_next(list);
2614 *errorCode = GEOFENCE_SERVER_ERROR_NONE;
2615 *placeCnt = place_cnt;
2616 return g_variant_builder_end(&b);
2619 static void __add_default_place(char *place_name)
2622 place_info_s *place_info = (place_info_s *) g_malloc0(sizeof(place_info_s));
2623 g_return_if_fail(place_info);
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");
2635 static void __init_geofencemanager(GeofenceServer *geofence_server)
2637 FUNC_ENTRANCE_SERVER;
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;
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;
2666 geofence_server->wifi_h = NULL;
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;
2675 while (place_id <= DEFAULT_PLACE_CAR) {
2676 if (geofence_manager_get_place_count_by_placeid(place_id, &count) == FENCE_ERR_NONE) {
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");
2686 LOGI_GEOFENCE("Error adding the default place: %d", place_id);
2692 /*delete all fences at rebooting for a test. TODO: will be replaced by updating previous fences*/
2693 /*geofence_manager_db_reset();*/
2698 static void __start_gps_geofence_client(void *handle, GeofenceModCB geofence_cb, void *userdata)
2700 FUNC_ENTRANCE_SERVER;
2701 /*Previously code to start the HW geofence client was there. It has been removed now*/
2705 static void __stop_gps_geofence_client(void *handle)
2707 FUNC_ENTRANCE_SERVER;
2708 /*Stop tracking the geofences*/
2713 int __copy_geofence_to_item_data(int fence_id, GeofenceItemData *item_data)
2715 FUNC_ENTRANCE_SERVER;
2716 g_return_val_if_fail(item_data, FENCE_ERR_INVALID_PARAMETER);
2717 char *app_id = NULL;
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;
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;
2727 if (FENCE_ERR_NONE != geofence_manager_get_enable_status(fence_id, &item_data->common_info.enable))
2728 return FENCE_ERR_SQLITE_FAIL;
2730 if (FENCE_ERR_NONE != geofence_manager_get_appid_from_geofence(fence_id, &app_id))
2731 return FENCE_ERR_SQLITE_FAIL;
2733 g_strlcpy(item_data->common_info.appid, app_id, APP_ID_LEN);
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;
2739 if (FENCE_ERR_NONE != geofence_manager_get_running_status(fence_id, &item_data->common_info.running_status))
2740 return FENCE_ERR_SQLITE_FAIL;
2742 if (item_data->common_info.type == GEOFENCE_TYPE_GEOPOINT) {
2743 geocoordinate_info_s *geocoordinate_info = NULL;
2744 int ret = FENCE_ERR_NONE;
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;
2751 item_data->priv = (void *) geocoordinate_info;
2753 bssid_info_s *bssid_info = NULL;
2754 int ret = FENCE_ERR_NONE;
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;
2761 item_data->priv = (void *) bssid_info;
2763 return FENCE_ERR_NONE;
2766 static void __add_left_fences(gpointer user_data)
2768 g_return_if_fail(user_data);
2769 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
2770 if (geofence_server->geofence_list != NULL)
2772 GList *fence_list = NULL;
2776 /*Get the number of fences count*/
2777 geofence_manager_get_count_of_fences(&count);
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);
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])
2788 * geofence_manager_set_running_status(fence_id, 0); // resetting the running-status flag since it is a device reboot
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");
2796 if (FENCE_ERR_NONE != __copy_geofence_to_item_data(fence_id, item_data)) {
2800 LOGI_GEOFENCE("adding fence_id = %d to fence_list", item_data->common_info.fence_id);
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);
2805 fence_list = g_list_next(fence_list);
2809 static void _glib_log(const gchar *log_domain, GLogLevelFlags log_level, const gchar *msg, gpointer user_data)
2811 LOGI_GEOFENCE("GLIB[%d] : %s", log_level, msg);
2814 int main(int argc, char **argv)
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)
2832 geofenceserver = g_new0(GeofenceServer, 1);
2833 if (!geofenceserver) {
2834 LOGI_GEOFENCE("GeofenceServer create fail");
2838 if (alarmmgr_init(PACKAGE_NAME) != ALARMMGR_RESULT_SUCCESS) {
2839 LOGI_GEOFENCE("alarmmgr_init fail");
2843 g_log_set_default_handler(_glib_log, geofenceserver);
2845 /*Initialize the geofence manager where DB related activities exists*/
2846 __init_geofencemanager(geofenceserver);
2848 /*This will read the DB and populate the list*/
2849 __add_left_fences(geofenceserver);
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);
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);
2857 #ifdef TIZEN_ENGINEER_MODE
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);
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;
2878 geofence_dbus_server_create(&(geofenceserver->geofence_dbus_server), dbus_callback, (void *) geofenceserver);
2880 LOGD_GEOFENCE("lbs_geofence_server_creation done");
2882 geofenceserver->loop = g_main_loop_new(NULL, TRUE);
2883 g_main_loop_run(geofenceserver->loop);
2885 LOGD_GEOFENCE("GEOFENCE_manager deamon Stop....");
2887 /*This call goes to server.c and deregisters all the callbacks w.r.t bt and wifi*/
2888 _geofence_deinitialize_geofence_server(geofenceserver);
2890 #ifdef TIZEN_ENGINEER_MODE
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");
2897 g_free(dbus_callback);
2898 g_main_loop_unref(geofenceserver->loop);
2899 g_free(geofenceserver);
2901 /*Closing the DB and the handle is aquired again when geofence server comes up.*/
2902 geofence_manager_close_db();