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>
23 #include <dd-display.h>
25 #include <vconf-internal-wifi-keys.h>
26 #include <vconf-internal-dnet-keys.h>
27 #include <vconf-internal-location-keys.h>
28 #include "geofence_server_data_types.h"
29 #include "geofence_server.h"
31 #include "debug_util.h"
32 #include "geofence_server_db.h"
33 #include "geofence_server_private.h"
34 #include "geofence_server_wifi.h"
35 #include "geofence_server_alarm.h"
36 #include "geofence_server_internal.h"
37 #include "geofence_server_bluetooth.h"
39 #include <network-wifi-intf.h>
40 #include <system_info.h>
42 #define TIZEN_ENGINEER_MODE
43 #ifdef TIZEN_ENGINEER_MODE
44 #include "geofence_server_log.h"
47 #define WPS_ACCURACY_TOLERANCE 100
48 #define GPS_TRIGGER_BOUNDARY 1000
49 #define SMART_ASSIST_HOME 1
50 #define SMART_ASSIST_TIMEOUT 60 /* Refer to LPP */
51 #define GEOFENCE_DEFAULT_RADIUS 200 /* Default 200 m */
52 #define GPS_TIMEOUT 60
53 #define WPS_TIMEOUT 60
55 #define MYPLACES_APPID "org.tizen.myplace"
56 #define DEFAULT_PLACE_HOME 1
57 #define DEFAULT_PLACE_OFFICE 2
58 #define DEFAULT_PLACE_CAR 3
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);
81 return is_wps_supported;
84 static const char *__convert_wifi_error_to_string(wifi_error_e err_type)
89 case WIFI_ERROR_INVALID_PARAMETER:
90 return "INVALID_PARAMETER";
91 case WIFI_ERROR_OUT_OF_MEMORY:
92 return "OUT_OF_MEMORY";
93 case WIFI_ERROR_INVALID_OPERATION:
94 return "INVALID_OPERATION";
95 case WIFI_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED:
96 return "ADDRESS_FAMILY_NOT_SUPPORTED";
97 case WIFI_ERROR_OPERATION_FAILED:
98 return "OPERATION_FAILED";
99 case WIFI_ERROR_NO_CONNECTION:
100 return "NO_CONNECTION";
101 case WIFI_ERROR_NOW_IN_PROGRESS:
102 return "NOW_IN_PROGRESS";
103 case WIFI_ERROR_ALREADY_EXISTS:
104 return "ALREADY_EXISTS";
105 case WIFI_ERROR_OPERATION_ABORTED:
106 return "OPERATION_ABORTED";
107 case WIFI_ERROR_DHCP_FAILED:
108 return "DHCP_FAILED";
109 case WIFI_ERROR_INVALID_KEY:
110 return "INVALID_KEY";
111 case WIFI_ERROR_NO_REPLY:
113 case WIFI_ERROR_SECURITY_RESTRICTED:
114 return "SECURITY_RESTRICTED";
115 case WIFI_ERROR_PERMISSION_DENIED:
116 return "PERMISSION_DENIED";
118 return "NOT Defined";
123 static const char *__bt_get_error_message(bt_error_e err)
127 return "BT_ERROR_NONE";
128 case BT_ERROR_CANCELLED:
129 return "BT_ERROR_CANCELLED";
130 case BT_ERROR_INVALID_PARAMETER:
131 return "BT_ERROR_INVALID_PARAMETER";
132 case BT_ERROR_OUT_OF_MEMORY:
133 return "BT_ERROR_OUT_OF_MEMORY";
134 case BT_ERROR_RESOURCE_BUSY:
135 return "BT_ERROR_RESOURCE_BUSY";
136 case BT_ERROR_TIMED_OUT:
137 return "BT_ERROR_TIMED_OUT";
138 case BT_ERROR_NOW_IN_PROGRESS:
139 return "BT_ERROR_NOW_IN_PROGRESS";
140 case BT_ERROR_NOT_INITIALIZED:
141 return "BT_ERROR_NOT_INITIALIZED";
142 case BT_ERROR_NOT_ENABLED:
143 return "BT_ERROR_NOT_ENABLED";
144 case BT_ERROR_ALREADY_DONE:
145 return "BT_ERROR_ALREADY_DONE";
146 case BT_ERROR_OPERATION_FAILED:
147 return "BT_ERROR_OPERATION_FAILED";
148 case BT_ERROR_NOT_IN_PROGRESS:
149 return "BT_ERROR_NOT_IN_PROGRESS";
150 case BT_ERROR_REMOTE_DEVICE_NOT_BONDED:
151 return "BT_ERROR_REMOTE_DEVICE_NOT_BONDED";
152 case BT_ERROR_AUTH_REJECTED:
153 return "BT_ERROR_AUTH_REJECTED";
154 case BT_ERROR_AUTH_FAILED:
155 return "BT_ERROR_AUTH_FAILED";
156 case BT_ERROR_REMOTE_DEVICE_NOT_FOUND:
157 return "BT_ERROR_REMOTE_DEVICE_NOT_FOUND";
158 case BT_ERROR_SERVICE_SEARCH_FAILED:
159 return "BT_ERROR_SERVICE_SEARCH_FAILED";
160 case BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED:
161 return "BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED";
163 case BT_ERROR_PERMISSION_DENIED:
164 return "BT_ERROR_PERMISSION_DENIED";
165 case BT_ERROR_SERVICE_NOT_FOUND:
166 return "BT_ERROR_SERVICE_NOT_FOUND";
169 return "NOT Defined";
174 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)
176 FUNC_ENTRANCE_SERVER;
177 g_return_val_if_fail(geofence_server, -1);
179 LOGD_GEOFENCE("place_id: %d, fence_id: %d, access_type: %d, error: %d, state: %d", place_id, fence_id, access_type, error, state);
181 geofence_dbus_server_send_geofence_event_changed(geofence_server->geofence_dbus_server, place_id, fence_id, access_type, app_id, error, state);
185 static int __emit_fence_inout(GeofenceServer *geofence_server, int fence_id, geofence_fence_state_e state)
187 FUNC_ENTRANCE_SERVER;
188 g_return_val_if_fail(geofence_server, -1);
191 LOGD_GEOFENCE("FenceId[%d], state[%d]", fence_id, state);
192 GeofenceItemData *item_data = __get_item_by_fence_id(fence_id, geofence_server);
193 if (item_data == NULL) {
194 LOGD_GEOFENCE("Invalid item_data");
198 if (state == GEOFENCE_FENCE_STATE_IN) {
199 /*LOGD_GEOFENCE("FENCE_IN to be set, current state: %d", item_data->common_info.status);*/
200 if (item_data->common_info.status != GEOFENCE_FENCE_STATE_IN) {
201 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);
202 if (item_data->client_status == GEOFENCE_CLIENT_STATUS_START) {
203 item_data->client_status = GEOFENCE_CLIENT_STATUS_RUNNING;
205 LOGD_GEOFENCE("%d : FENCE_IN", fence_id);
206 #ifdef TIZEN_ENGINEER_MODE
207 GEOFENCE_PRINT_LOG("FENCE_IN");
211 item_data->common_info.status = GEOFENCE_FENCE_STATE_IN;
213 } else if (state == GEOFENCE_FENCE_STATE_OUT) {
214 /*LOGD_GEOFENCE("FENCE_OUT to be set, current state: %d", item_data->common_info.status);*/
215 if (item_data->common_info.status != GEOFENCE_FENCE_STATE_OUT) {
216 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);
217 if (item_data->client_status == GEOFENCE_CLIENT_STATUS_START) {
218 item_data->client_status = GEOFENCE_CLIENT_STATUS_RUNNING;
220 LOGD_GEOFENCE("%d : FENCE_OUT", fence_id);
221 #ifdef TIZEN_ENGINEER_MODE
222 GEOFENCE_PRINT_LOG("FENCE_OUT");
225 LOGD_GEOFENCE("Fence status [%d]", item_data->common_info.status);
227 item_data->common_info.status = GEOFENCE_FENCE_STATE_OUT;
229 LOGD_GEOFENCE("Not emit, Prev[%d], Curr[%d]", item_data->common_info.status, state);
235 static int __emit_fence_proximity(GeofenceServer *geofence_server, int fence_id, geofence_proximity_state_e state)
237 FUNC_ENTRANCE_SERVER;
238 g_return_val_if_fail(geofence_server, -1);
239 GeofenceItemData *item_data = __get_item_by_fence_id(fence_id, geofence_server);
241 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_LOCATION);
242 item_data->common_info.proximity_status = state;
244 LOGD_GEOFENCE("Invalid item_data");
250 static void __check_proximity_for_fence(double distance, int fence_id, int radius, geofence_proximity_state_e current_state, void *user_data)
252 FUNC_ENTRANCE_SERVER;
253 geofence_proximity_state_e state = GEOFENCE_PROXIMITY_UNCERTAIN;
254 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
255 if (distance <= 50.0) {
256 state = GEOFENCE_PROXIMITY_IMMEDIATE;
257 } else if (distance > 50.0 && distance <= 100.0) {
258 state = GEOFENCE_PROXIMITY_NEAR;
259 } else if (distance > 100.0 && distance <= radius) {
260 state = GEOFENCE_PROXIMITY_FAR;
262 if (current_state != state)
263 LOGD_GEOFENCE("PROXIMITY ALERTING for fence: %d, alert: %d, distance: %f", fence_id, state, distance);
264 __emit_fence_proximity(geofence_server, fence_id, state);
267 static void __check_inout_by_gps(double latitude, double longitude, int fence_id, void *user_data)
269 FUNC_ENTRANCE_SERVER;
270 double distance = 0.0;
271 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
272 GeofenceItemData *item_data = __get_item_by_fence_id(fence_id, geofence_server);
273 if (!item_data || item_data->client_status == GEOFENCE_CLIENT_STATUS_NONE)
276 geofence_fence_state_e status = GEOFENCE_FENCE_STATE_OUT;
277 geocoordinate_info_s *geocoordinate_info = (geocoordinate_info_s *)item_data->priv;
278 /* get_current_position/ check_fence_in/out for geoPoint */
279 location_manager_get_distance(latitude, longitude, geocoordinate_info->latitude, geocoordinate_info->longitude, &distance);
281 if (distance >= geocoordinate_info->radius) {
282 LOGD_GEOFENCE("FENCE_OUT : based on distance. Distance[%f] for fence id: %d at (%f, %f)", distance, fence_id, latitude, longitude);
283 status = GEOFENCE_FENCE_STATE_OUT;
285 LOGD_GEOFENCE("FENCE_IN : based on distance. Distance[%f] for fence id: %d at (%f, %f)", distance, fence_id, latitude, longitude);
286 status = GEOFENCE_FENCE_STATE_IN;
289 /* Alert for the proximity */
290 /*if (status == GEOFENCE_FENCE_STATE_IN) {*/
291 __check_proximity_for_fence(distance, item_data->common_info.fence_id, geocoordinate_info->radius, item_data->common_info.proximity_status, geofence_server);
294 if (__emit_fence_inout(geofence_server, item_data->common_info.fence_id, status) == 0 && status == GEOFENCE_FENCE_STATE_IN) {
295 LOGD_GEOFENCE("Disable timer");
299 static void __stop_gps_alarms(void *user_data)
301 FUNC_ENTRANCE_SERVER;
302 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
303 /*Stop the gps interval alarm if it is running...*/
304 if (geofence_server->gps_alarm_id != -1) {
305 /*LOGI_GEOFENCE("GPS interval timer removed. ID[%d]", geofence_server->gps_alarm_id);*/
306 geofence_server->gps_alarm_id = _geofence_remove_alarm(geofence_server->gps_alarm_id);
308 /*Stop the timeout alarm if it is running...*/
309 if (geofence_server->gps_timeout_alarm_id != -1) {
310 /*LOGI_GEOFENCE("Timeout timer removed for gps. ID[%d]", geofence_server->gps_timeout_alarm_id);*/
311 geofence_server->gps_timeout_alarm_id = _geofence_remove_alarm(geofence_server->gps_timeout_alarm_id);
315 static void __stop_wps_alarms(void *user_data)
317 FUNC_ENTRANCE_SERVER;
318 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
319 /*Stop the wps interval alarm if it is running...*/
320 if (geofence_server->wps_alarm_id != -1) {
321 /*LOGI_GEOFENCE("WPS interval timer removed. ID[%d]", geofence_server->wps_alarm_id);*/
322 geofence_server->wps_alarm_id = _geofence_remove_alarm(geofence_server->wps_alarm_id);
324 /*Stop the timeout alarm if it is running...*/
325 if (geofence_server->wps_timeout_alarm_id != -1) {
326 /*LOGI_GEOFENCE("Timeout timer removed for wps. ID[%d]", geofence_server->wps_timeout_alarm_id);*/
327 geofence_server->wps_timeout_alarm_id = _geofence_remove_alarm(geofence_server->wps_timeout_alarm_id);
331 static void __check_current_location_cb(double latitude, double longitude, double altitude, time_t timestamp, void *user_data)
333 FUNC_ENTRANCE_SERVER;
334 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
336 GList *tracking_list = NULL;
337 GeofenceItemData *item_data = NULL;
339 LOGD_GEOFENCE("Traversing the tracking list");
340 tracking_list = g_list_first(geofence_server->tracking_list);
341 LOGD_GEOFENCE("Got the first element in tracking list");
343 while (tracking_list) {
344 fence_id = GPOINTER_TO_INT(tracking_list->data);
345 item_data = __get_item_by_fence_id(fence_id, geofence_server);
347 if (item_data != NULL) {
348 if (item_data->common_info.type == GEOFENCE_TYPE_GEOPOINT) {
349 LOGD_GEOFENCE("TRACKING FENCE ID :: %d", fence_id);
350 __check_inout_by_gps(latitude, longitude, fence_id, geofence_server);
353 tracking_list = g_list_next(tracking_list);
357 static int __start_wps_positioning(GeofenceServer *geofence_server, location_position_updated_cb callback)
359 FUNC_ENTRANCE_SERVER;
360 g_return_val_if_fail(geofence_server, -1);
361 int ret = FENCE_ERR_NONE;
363 if (geofence_server->loc_wps_manager == NULL) {
364 ret = location_manager_create(LOCATIONS_METHOD_WPS, &geofence_server->loc_wps_manager);
365 if (ret != LOCATIONS_ERROR_NONE) {
366 LOGD_GEOFENCE("Fail to create location_manager_h for wps: %d", ret);
367 return FENCE_ERR_UNKNOWN;
370 if (geofence_server->loc_wps_started == FALSE) {
371 ret = location_manager_set_position_updated_cb(geofence_server->loc_wps_manager, callback, 1, (void *) geofence_server);
372 if (ret != LOCATIONS_ERROR_NONE) {
373 LOGD_GEOFENCE("Fail to set callback for wps. %d", ret);
374 return FENCE_ERR_UNKNOWN;
376 ret = location_manager_start(geofence_server->loc_wps_manager);
377 if (ret != LOCATIONS_ERROR_NONE) {
378 LOGD_GEOFENCE("Fail to start. %d", ret);
379 location_manager_unset_position_updated_cb(geofence_server->loc_wps_manager);
380 location_manager_destroy(geofence_server->loc_wps_manager);
381 geofence_server->loc_wps_manager = NULL;
382 return FENCE_ERR_UNKNOWN;
384 if (geofence_server->wps_timeout_alarm_id == -1)
385 geofence_server->wps_timeout_alarm_id = _geofence_add_alarm(WPS_TIMEOUT, __wps_timeout_cb, geofence_server);
387 geofence_server->loc_wps_started = TRUE;
389 LOGD_GEOFENCE("loc_wps_started TRUE");
395 static int __start_gps_positioning(GeofenceServer *geofence_server, location_position_updated_cb callback)
397 FUNC_ENTRANCE_SERVER;
398 g_return_val_if_fail(geofence_server, -1);
399 int ret = FENCE_ERR_NONE;
401 if (geofence_server->loc_gps_manager == NULL) {
402 ret = location_manager_create(LOCATIONS_METHOD_GPS, &geofence_server->loc_gps_manager);
403 if (ret != LOCATIONS_ERROR_NONE) {
404 LOGD_GEOFENCE("Fail to create location_manager_h: %d", ret);
405 return FENCE_ERR_UNKNOWN;
409 if (geofence_server->loc_gps_started == FALSE) {
410 ret = location_manager_set_position_updated_cb(geofence_server->loc_gps_manager, callback, geofence_server->gps_trigger_interval, (void *) geofence_server);
411 if (ret != LOCATIONS_ERROR_NONE) {
412 LOGD_GEOFENCE("Fail to set callback. %d", ret);
413 return FENCE_ERR_UNKNOWN;
416 ret = location_manager_start(geofence_server->loc_gps_manager);
417 if (ret != LOCATIONS_ERROR_NONE) {
418 LOGD_GEOFENCE("Fail to start. %d", ret);
419 location_manager_unset_position_updated_cb(geofence_server->loc_gps_manager);
420 location_manager_destroy(geofence_server->loc_gps_manager);
421 geofence_server->loc_gps_manager = NULL;
422 return FENCE_ERR_UNKNOWN;
424 if (geofence_server->gps_timeout_alarm_id == -1)
425 geofence_server->gps_timeout_alarm_id = _geofence_add_alarm(GPS_TIMEOUT, __gps_timeout_cb, geofence_server);
427 geofence_server->loc_gps_started = TRUE;
429 LOGD_GEOFENCE("loc_gps_started TRUE");
435 static void __stop_gps_positioning(gpointer userdata)
437 FUNC_ENTRANCE_SERVER;
438 g_return_if_fail(userdata);
439 GeofenceServer *geofence_server = (GeofenceServer *) userdata;
441 if (geofence_server->loc_gps_started == TRUE) {
442 ret = location_manager_stop(geofence_server->loc_gps_manager);
443 if (ret != LOCATIONS_ERROR_NONE) {
446 geofence_server->loc_gps_started = FALSE;
447 ret = location_manager_unset_position_updated_cb(geofence_server->loc_gps_manager);
448 if (ret != LOCATIONS_ERROR_NONE) {
453 if (geofence_server->loc_gps_manager != NULL) {
454 ret = location_manager_destroy(geofence_server->loc_gps_manager);
455 if (ret != LOCATIONS_ERROR_NONE) {
458 geofence_server->loc_gps_manager = NULL;
462 static void __stop_wps_positioning(gpointer userdata)
464 FUNC_ENTRANCE_SERVER;
465 g_return_if_fail(userdata);
466 GeofenceServer *geofence_server = (GeofenceServer *) userdata;
468 if (geofence_server->loc_wps_started == TRUE) {
469 ret = location_manager_stop(geofence_server->loc_wps_manager);
470 if (ret != LOCATIONS_ERROR_NONE) {
471 LOGI_GEOFENCE("Unable to stop the wps");
474 geofence_server->loc_wps_started = FALSE;
475 ret = location_manager_unset_position_updated_cb(geofence_server->loc_wps_manager);
476 if (ret != LOCATIONS_ERROR_NONE) {
477 LOGI_GEOFENCE("Unable to unset the callback");
482 if (geofence_server->loc_wps_manager != NULL) {
483 ret = location_manager_destroy(geofence_server->loc_wps_manager);
484 if (ret != LOCATIONS_ERROR_NONE) {
485 LOGI_GEOFENCE("Unable to destroy the wps manager");
488 geofence_server->loc_wps_manager = NULL;
492 static int __get_time_diff(int timestamp)
494 int current_time = 0;
496 current_time = (g_get_real_time()/1000000);
497 timediff = current_time - timestamp;
501 static void __process_best_location(GeofenceServer *geofence_server)
503 FUNC_ENTRANCE_SERVER;
508 /* Check if any of the fix is null just return. It doesn't make sense to compare if only one fix is available*/
509 if (geofence_server->gps_fix_info == NULL || geofence_server->wps_fix_info == NULL)
512 /*Calculate the time difference*/
513 gpsdiff = __get_time_diff(geofence_server->gps_fix_info->timestamp);
514 wpsdiff = __get_time_diff(geofence_server->wps_fix_info->timestamp);
516 if (gpsdiff < wpsdiff) {
517 if ((geofence_server->gps_fix_info->timestamp - geofence_server->wps_fix_info->timestamp) <= 20) {
518 if (geofence_server->gps_fix_info->accuracy <= geofence_server->wps_fix_info->accuracy) {
519 LOGI_GEOFENCE("Using GPS fix");
520 __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);
522 LOGI_GEOFENCE("Using WPS fix");
523 __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);
526 LOGI_GEOFENCE("Time diff is more. So using latest GPS fix");
527 __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);
530 if ((geofence_server->wps_fix_info->timestamp - geofence_server->gps_fix_info->timestamp) <= 20) {
531 if (geofence_server->wps_fix_info->accuracy <= geofence_server->gps_fix_info->accuracy) {
532 LOGI_GEOFENCE("Using WPS fix");
533 __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);
535 LOGI_GEOFENCE("Using GPS fix");
536 __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);
539 LOGI_GEOFENCE("Time diff is more. So using latest WPS fix");
540 __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);
545 static void __geofence_standalone_gps_position_changed_cb(double latitude, double longitude, double altitude, time_t timestamp, void *user_data)
547 FUNC_ENTRANCE_SERVER;
548 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
551 int min_fence_id = 0;
552 double hor_acc = 0.0;
553 double ver_acc = 0.0;
554 location_accuracy_level_e level;
555 int ret = LOCATIONS_ERROR_NONE;
557 /* Allocate memory for the location_info structure */
558 if (geofence_server->gps_fix_info == NULL) {
559 geofence_server->gps_fix_info = (location_fix_info_s *)g_malloc(sizeof(location_fix_info_s));
561 /* Store the location information in the structure for future use*/
562 if (geofence_server->gps_fix_info != NULL) {
563 geofence_server->gps_fix_info->latitude = latitude;
564 geofence_server->gps_fix_info->longitude = longitude;
565 ret = location_manager_get_accuracy(geofence_server->loc_gps_manager, &level, &hor_acc, &ver_acc);
566 if (ret == LOCATIONS_ERROR_NONE) {
567 LOGI_GEOFENCE("hor_acc:%f, ver_acc:%f", hor_acc, ver_acc);
568 LOGD_GEOFENCE("*****%f, %f********", latitude, longitude);
569 geofence_server->gps_fix_info->accuracy = hor_acc;
572 /*Remove the timeout callback that might be running when requesting for fix.*/
573 if (geofence_server->gps_timeout_alarm_id != -1) {
574 /*LOGI_GEOFENCE("Removing the timeout alarm from restart gps");*/
575 geofence_server->gps_timeout_alarm_id = _geofence_remove_alarm(geofence_server->gps_timeout_alarm_id);
577 geofence_server->last_loc_time = timestamp;
578 __check_current_location_cb(latitude, longitude, altitude, timestamp, user_data);
580 /* Distance based alarm */
581 distance = _get_min_distance(latitude, longitude, &min_fence_id, geofence_server);
583 if (distance < 200) {
584 LOGD_GEOFENCE("interval: 1 secs");
586 } else if (distance < 500) {
587 LOGD_GEOFENCE("interval: 3 secs");
589 } else if (distance < 1000) {
590 LOGD_GEOFENCE("interval: 6 secs");
592 } else if (distance < 2000) {
593 LOGD_GEOFENCE("interval: 20 secs");
595 } else if (distance < 3000) {
596 LOGD_GEOFENCE("interval : 1 min");
598 } else if (distance < 5000) {
599 LOGD_GEOFENCE("interval: 2 mins");
601 } else if (distance < 10000) {
602 LOGD_GEOFENCE("interval: 5 mins");
604 } else if (distance < 20000) {
605 LOGD_GEOFENCE("interval : 10 mins");
607 } else if (distance < 100000) {
608 LOGD_GEOFENCE("interval : 20 mins");
611 LOGD_GEOFENCE("interval : 60 mins");
615 /* remove the activity value when 10 hours later */
616 if (geofence_server->last_loc_time - geofence_server->activity_timestamp > 10 * 60 * 60)
617 geofence_server->activity_type = ACTIVITY_IN_VEHICLE;
619 if (geofence_server->activity_type == ACTIVITY_STATIONARY)
620 interval = interval * 10;
621 else if (geofence_server->activity_type == ACTIVITY_WALK)
622 interval = interval * 5;
623 else if (geofence_server->activity_type == ACTIVITY_RUN)
624 interval = interval * 3;
625 LOGD_GEOFENCE("Unsetting the position_updated_cb");
626 location_manager_unset_position_updated_cb(geofence_server->loc_gps_manager);
627 location_manager_stop(geofence_server->loc_gps_manager);
628 geofence_server->loc_gps_started = FALSE;
630 LOGI_GEOFENCE("Setting the gps interval of alrm %d s", interval);
631 if (geofence_server->gps_alarm_id == -1) {
632 LOGI_GEOFENCE("Setting the gps alarm from the callback");
633 geofence_server->gps_alarm_id = _geofence_add_alarm(interval, __gps_alarm_cb, geofence_server);
637 static void __geofence_gps_position_changed_cb(double latitude, double longitude, double altitude, time_t timestamp, void *user_data)
639 FUNC_ENTRANCE_SERVER;
640 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
641 double hor_acc = 0.0;
642 double ver_acc = 0.0;
643 GeofenceItemData *item_data = NULL;
644 int min_fence_id = -1;
645 int min_distance = 0;
646 location_accuracy_level_e level;
647 int ret = LOCATIONS_ERROR_NONE;
649 /*Remove the timeout callback that might be running when requesting for fix.*/
650 if (geofence_server->gps_timeout_alarm_id != -1) {
651 /*LOGI_GEOFENCE("Removing the timeout alarm from restart gps");*/
652 geofence_server->gps_timeout_alarm_id = _geofence_remove_alarm(geofence_server->gps_timeout_alarm_id);
655 /* Allocate memory for the location_info structure */
656 if (geofence_server->gps_fix_info == NULL) {
657 geofence_server->gps_fix_info = (location_fix_info_s *)g_malloc(sizeof(location_fix_info_s));
660 /* Store the location information in the structure for future use*/
661 if (geofence_server->gps_fix_info != NULL) {
662 geofence_server->gps_fix_info->latitude = latitude;
663 geofence_server->gps_fix_info->longitude = longitude;
664 geofence_server->gps_fix_info->timestamp = (g_get_real_time()/1000000); /* microsecs->millisecs->secs */
665 ret = location_manager_get_accuracy(geofence_server->loc_gps_manager, &level, &hor_acc, &ver_acc);
666 if (ret == LOCATIONS_ERROR_NONE) {
667 LOGD_GEOFENCE("hor_acc:%f, ver_acc:%f", hor_acc, ver_acc);
668 LOGD_GEOFENCE("*****%f, %f********", latitude, longitude);
669 geofence_server->gps_fix_info->accuracy = hor_acc;
672 LOGD_GEOFENCE("Invalid GPS fix data");
675 geofence_server->last_loc_time = timestamp;
677 if (geofence_server->wps_fix_info && __get_time_diff(geofence_server->wps_fix_info->timestamp) <= 20 && geofence_server->gps_fix_info->accuracy <= 50.0) {
678 LOGI_GEOFENCE("Going for fix comparison from gps fix");
679 __process_best_location(geofence_server);
680 /* Using GPS fix from this point. So stop WPS alarms which trigger next WPS request session */
681 __stop_wps_alarms(geofence_server);
682 } else if (geofence_server->gps_fix_info->accuracy <= 50.0) {
683 LOGI_GEOFENCE("Emitting from GPS fix directly");
684 __check_current_location_cb(latitude, longitude, altitude, timestamp, user_data);
685 /* Using GPS fix from point. So stop WPS alarms which trigger next WPS request session */
686 __stop_wps_alarms(geofence_server);
689 location_manager_unset_position_updated_cb(geofence_server->loc_gps_manager);
690 location_manager_stop(geofence_server->loc_gps_manager);
691 geofence_server->loc_gps_started = FALSE;
693 /*Get minimum distance and fence_id of the nearest tracking fence*/
694 if (geofence_server->gps_fix_info) {
695 min_distance = _get_min_distance(geofence_server->gps_fix_info->latitude, geofence_server->gps_fix_info->longitude, &min_fence_id, geofence_server);
696 item_data = __get_item_by_fence_id(min_fence_id, geofence_server);
697 if (item_data && geofence_server->loc_gps_started_by_wps == TRUE) {
698 LOGI_GEOFENCE("******Setting the GPS interval******");
699 __set_interval_for_gps(min_distance, min_fence_id, user_data);
704 static void __geofence_wps_position_changed_cb(double latitude, double longitude, double altitude, time_t timestamp, void *user_data)
706 FUNC_ENTRANCE_SERVER;
707 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
708 GeofenceItemData *item_data = NULL;
709 double min_distance = 0.0;
710 int min_fence_id = 0;
711 double hor_acc = 0.0;
712 double ver_acc = 0.0;
714 location_accuracy_level_e level;
715 int ret = LOCATIONS_ERROR_NONE;
718 /* Allocate memory for the location_info structure */
719 if (geofence_server->wps_fix_info == NULL) {
720 geofence_server->wps_fix_info = (location_fix_info_s *)g_malloc(sizeof(location_fix_info_s));
722 /*Remove the timeout callback that might be running when requesting for fix.*/
723 if (geofence_server->wps_timeout_alarm_id != -1) {
724 /*LOGI_GEOFENCE("Removing the timeout alarm from restart gps");*/
725 geofence_server->wps_timeout_alarm_id = _geofence_remove_alarm(geofence_server->wps_timeout_alarm_id);
727 /* Store the location information in the structure for future use*/
728 if (geofence_server->wps_fix_info != NULL) {
729 geofence_server->wps_fix_info->latitude = latitude;
730 geofence_server->wps_fix_info->longitude = longitude;
731 geofence_server->wps_fix_info->timestamp = (g_get_real_time()/1000000); /* microsecs->millisecs->secs */
732 ret = location_manager_get_accuracy(geofence_server->loc_wps_manager, &level, &hor_acc, &ver_acc);
733 if (ret == LOCATIONS_ERROR_NONE) {
734 LOGD_GEOFENCE("hor_acc:%f, ver_acc:%f", hor_acc, ver_acc);
735 LOGD_GEOFENCE("*****%f, %f********", latitude, longitude);
736 geofence_server->wps_fix_info->accuracy = hor_acc;
739 LOGD_GEOFENCE("Invalid WPS fix data");
743 /*Get minimum distance and fence_id of the nearest tracking fence*/
744 min_distance = _get_min_distance(latitude, longitude, &min_fence_id, geofence_server);
745 LOGI_GEOFENCE("Nearest fence id: %d, distance: %f", min_fence_id, min_distance);
747 item_data = __get_item_by_fence_id(min_fence_id, geofence_server);
750 return;/* There is no valid fence with this fence id. So return*/
752 geocoordinate_info_s *geocoordinate_info = (geocoordinate_info_s *)item_data->priv;
754 double interval_dist = (min_distance - geocoordinate_info->radius) - geofence_server->wps_fix_info->accuracy;
755 LOGI_GEOFENCE("Distance for interval: %f", interval_dist);
756 if (interval_dist < 15000) {
757 interval = interval_dist/25; /*secs*/ /*Assuming 90 km/hr of speed - So 25 mtrs covered in 1 sec*/
758 } else if (interval_dist >= 15000 && interval_dist < 18000) {
759 interval = 10 * 60; /* 10 mins */
760 } else if (interval_dist >= 18000 && interval_dist < 20000) {
761 interval = 12 * 60; /* 12 mins */
762 } else if (interval_dist >= 20000) {
763 interval = 15 * 60; /*15 mins*/
766 interval = 15; /*15 sec */
768 location_manager_unset_position_updated_cb(geofence_server->loc_wps_manager);
769 location_manager_stop(geofence_server->loc_wps_manager);
770 geofence_server->loc_wps_started = FALSE;
772 LOGI_GEOFENCE("Setting the wps interval of %d secs", interval);
773 if (geofence_server->wps_alarm_id == -1) {
774 LOGI_GEOFENCE("Setting the wps alarm from the callback");
775 geofence_server->wps_alarm_id = _geofence_add_alarm(interval, __wps_alarm_cb, geofence_server);
778 /* Get the GPS state here */
779 vconf_get_int(VCONFKEY_LOCATION_ENABLED, &gps_state);
780 if (gps_state == 1) {
781 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*/
782 __check_current_location_cb(latitude, longitude, altitude, timestamp, user_data);
783 } else if (item_data->common_info.status == GEOFENCE_FENCE_STATE_UNCERTAIN) {
784 __check_current_location_cb(latitude, longitude, altitude, timestamp, user_data);
786 if (geofence_server->loc_gps_started_by_wps == FALSE && geofence_server->loc_gps_started == FALSE) {
787 if (min_distance <= (geocoordinate_info->radius + GPS_TRIGGER_BOUNDARY)) {
788 LOGD_GEOFENCE("Triggering GPS");
789 /*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);*/
790 if (FENCE_ERR_NONE == __start_gps_positioning(geofence_server, __geofence_gps_position_changed_cb))
791 geofence_server->loc_gps_started_by_wps = true;
793 LOGI_GEOFENCE("Error starting GPS/ GPS is off");
797 __check_current_location_cb(latitude, longitude, altitude, timestamp, user_data); /* Its WPS only mode so no need to worry abt accuracy */
800 static void __set_interval_for_gps(double min_distance, int min_fence_id, void *user_data)
802 FUNC_ENTRANCE_SERVER;
803 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
804 GeofenceItemData *item_data = NULL;
805 bool isSwitched = false;
806 item_data = __get_item_by_fence_id(min_fence_id, geofence_server);
807 if (item_data && geofence_server->gps_fix_info) {
808 geocoordinate_info_s *geocoordinate_info = (geocoordinate_info_s *)item_data->priv;
809 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))) {
811 LOGI_GEOFENCE("Setting the GPS interval as 5 secs");
812 geofence_server->gps_trigger_interval = 5;
813 /*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);*/
814 } else if (geofence_server->gps_trigger_interval == 5 && min_distance <= (geocoordinate_info->radius + 100 + geofence_server->gps_fix_info->accuracy)) {
816 LOGI_GEOFENCE("Setting the GPS interval as 1 secs");
817 geofence_server->gps_trigger_interval = 1;
818 /*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);*/
819 } else if (min_distance > (geocoordinate_info->radius + 1000)) {
820 /* Already stopped. Just that GPS trigger alarm wont be scheduled again */
821 /*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);*/
822 geofence_server->loc_gps_started_by_wps = false;
823 /*No need of GPS. So stop GPS and start the WPS from here*/
824 location_manager_unset_position_updated_cb(geofence_server->loc_gps_manager);
825 location_manager_stop(geofence_server->loc_gps_manager);
826 geofence_server->loc_gps_started = FALSE;
827 __start_wps_positioning(geofence_server, __geofence_wps_position_changed_cb);/* Stopping the GPS here. So start using wps */
829 if ((geofence_server->loc_gps_started_by_wps == true && isSwitched == true) || geofence_server->gps_trigger_interval > 1) {
830 LOGI_GEOFENCE("Setting the gps interval of %d secs during wps session", geofence_server->gps_trigger_interval);
831 if (geofence_server->gps_alarm_id == -1) {
832 /*Switching the interval for GPS. So stop and start using alarm*/
833 location_manager_unset_position_updated_cb(geofence_server->loc_gps_manager);
834 location_manager_stop(geofence_server->loc_gps_manager);
835 geofence_server->loc_gps_started = FALSE;
836 LOGI_GEOFENCE("Setting the gps alarm from the callback");
837 geofence_server->gps_alarm_id = _geofence_add_alarm(geofence_server->gps_trigger_interval, __gps_alarm_cb, geofence_server);
844 static void __check_tracking_list(const char *bssid, void *user_data, geofence_type_e type)
846 FUNC_ENTRANCE_SERVER;
847 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
848 g_return_if_fail(geofence_server);
849 int tracking_fence_id = 0;
850 GeofenceItemData *item_data = NULL;
851 GList *tracking_fences = g_list_first(geofence_server->tracking_list);
853 while (tracking_fences) {
854 tracking_fence_id = GPOINTER_TO_INT(tracking_fences->data);
855 tracking_fences = g_list_next(tracking_fences);
856 item_data = __get_item_by_fence_id(tracking_fence_id, geofence_server);
857 if (item_data != NULL) {
858 if (item_data->common_info.type == type) {
859 if (type == GEOFENCE_TYPE_WIFI) {
860 bssid_info_s *wifi_info = (bssid_info_s *)item_data->priv;
862 if ((!g_ascii_strcasecmp(wifi_info->bssid, bssid) || !g_ascii_strcasecmp(g_strdelimit(wifi_info->bssid, "-", ':'), bssid)) && item_data->is_wifi_status_in == false) {
863 LOGI_GEOFENCE("Matched wifi fence: fence_id = %d, bssid = %s", item_data->common_info.fence_id, wifi_info->bssid);
864 item_data->is_wifi_status_in = true;
866 } else if (type == GEOFENCE_TYPE_BT) {
867 bssid_info_s *bt_info = (bssid_info_s *)item_data->priv;
869 if ((!g_ascii_strcasecmp(bt_info->bssid, bssid) || !g_ascii_strcasecmp(g_strdelimit(bt_info->bssid, "-", ':'), bssid)) && item_data->is_bt_status_in == false) {
870 LOGI_GEOFENCE("Matched bt fence: fence_id = %d, bssid received = %s, bssid = %s", item_data->common_info.fence_id, bt_info->bssid, bssid);
871 item_data->is_bt_status_in = true;
876 LOGI_GEOFENCE("No data present for the fence: %d", tracking_fence_id);
882 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)
885 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
886 GeofenceItemData *item_data = NULL;
888 int tracking_fence_id = 0;
889 GList *tracking_fences = g_list_first(geofence_server->tracking_list);
891 if (discovery_state != BT_ADAPTER_DEVICE_DISCOVERY_FOUND) {
892 LOGI_GEOFENCE("BREDR discovery %s", discovery_state == BT_ADAPTER_DEVICE_DISCOVERY_STARTED ? "Started" : "Finished");
893 /* Check only if some BT fence is running */
894 if (discovery_state == BT_ADAPTER_DEVICE_DISCOVERY_FINISHED && geofence_server->running_bt_cnt > 0) {
895 LOGI_GEOFENCE("Comparison for BT is done. Now emit the status...");
896 while (tracking_fences) {
897 tracking_fence_id = GPOINTER_TO_INT(tracking_fences->data);
898 tracking_fences = g_list_next(tracking_fences);
899 item_data = __get_item_by_fence_id(tracking_fence_id, geofence_server);
900 if (item_data && item_data->common_info.type == GEOFENCE_TYPE_BT) {
901 if (item_data->is_bt_status_in == true) {
902 __emit_fence_inout(geofence_server, item_data->common_info.fence_id, GEOFENCE_FENCE_STATE_IN);
904 __emit_fence_inout(geofence_server, item_data->common_info.fence_id, GEOFENCE_FENCE_STATE_OUT);
906 item_data->is_bt_status_in = false;
911 LOGI_GEOFENCE("%s, %s", discovery_info->remote_address, discovery_info->remote_name);
912 LOGI_GEOFENCE("rssi: %d is_bonded: %d", discovery_info->rssi, discovery_info->is_bonded);
914 if (geofence_server->running_bt_cnt > 0) {
915 for (i = 0; i < discovery_info->service_count; i++) {
916 LOGI_GEOFENCE("uuid: %s", discovery_info->service_uuid[i]);
918 LOGI_GEOFENCE("Tracking list is being checked for the BT geofence");
919 __check_tracking_list(discovery_info->remote_address, geofence_server, GEOFENCE_TYPE_BT);
925 /*static double __getProximityDistance(double rssi_rec)
927 FUNC_ENTRANCE_SERVER;
930 double rssi = rssi_rec;
931 double rssi0 = -40.9117;
934 inter = (rssi0 - rssi)/(10 * ETA);
935 d = d0 * math.pow(10, inter);
939 static void geofence_network_evt_cb(net_event_info_t *event_cb, void *user_data)
941 FUNC_ENTRANCE_SERVER;
942 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
943 g_return_if_fail(geofence_server);
944 /*GList *tracking_fences = g_list_first(geofence_server->tracking_list);
945 GeofenceItemData *item_data = NULL;
946 int tracking_fence_id = 0;*/
947 net_wifi_state_t network_state = WIFI_UNKNOWN;
952 switch (event_cb->Event) {
954 case NET_EVENT_WIFI_SCAN_IND:
956 LOGD_GEOFENCE("Got WIFI scan Ind : %d\n", event_cb->Error);
958 net_profile_info_t *profiles = NULL;
959 int num_of_profile = 0;
961 if (geofence_server->running_wifi_cnt > 0) { /*Check only if some wifi fence is running*/
962 if (NET_ERR_NONE != net_get_profile_list(NET_DEVICE_WIFI, &profiles, &num_of_profile)) {
963 LOGD_GEOFENCE("Failed to get the scanned list");
965 LOGD_GEOFENCE("Scan results retrieved successfully. No.of profiles: %d", num_of_profile);
966 if (num_of_profile > 0 && profiles != NULL) {
968 for (cnt = 0; cnt < num_of_profile; cnt++) {
969 net_wifi_profile_info_t *ap_info = &profiles[cnt].ProfileInfo.Wlan;
970 LOGD_GEOFENCE("BSSID %s", ap_info->bssid);
971 __check_tracking_list(ap_info->bssid, geofence_server, GEOFENCE_TYPE_WIFI);
973 LOGD_GEOFENCE("Comparing fences with scan results is done.Now emit the status to the application");
974 while (tracking_fences) {
975 tracking_fence_id = GPOINTER_TO_INT(tracking_fences->data);
976 tracking_fences = g_list_next(tracking_fences);
977 item_data = __get_item_by_fence_id(tracking_fence_id, geofence_server);
978 if (item_data && item_data->common_info.type == GEOFENCE_TYPE_WIFI) {
979 if (item_data->is_wifi_status_in == true) {
980 __emit_fence_inout(geofence_server, item_data->common_info.fence_id, GEOFENCE_FENCE_STATE_IN);
982 __emit_fence_inout(geofence_server, item_data->common_info.fence_id, GEOFENCE_FENCE_STATE_OUT);
984 item_data->is_wifi_status_in = false;
993 case NET_EVENT_WIFI_POWER_IND:
994 LOGI_GEOFENCE("WIFI ON/OFF indication");
995 vconf_get_int(VCONFKEY_LOCATION_NETWORK_ENABLED, &wps_state);
996 vconf_get_int(VCONFKEY_LOCATION_ENABLED, &gps_state);
997 if (__is_support_wps() == true && geofence_server->running_geopoint_cnt > 0) {
998 ret = net_get_wifi_state(&network_state, (net_profile_name_t*)event_cb->ProfileName);
1000 if (network_state == WIFI_OFF) {
1001 LOGI_GEOFENCE("WIFI is OFF");
1002 /* In Tizen device(Kiran) WPS is not supported if WIFI is switched off */
1003 __stop_wps_positioning(geofence_server);
1004 __stop_wps_alarms(geofence_server);
1005 if (geofence_server->loc_gps_started_by_wps == true) {
1006 __stop_gps_positioning(geofence_server); /*Stop the gps if it was started by wps*/
1007 __stop_gps_alarms(geofence_server);
1008 geofence_server->loc_gps_started_by_wps = false;
1010 if (gps_state == 1) {
1011 ret = __start_gps_positioning(geofence_server, __geofence_standalone_gps_position_changed_cb);
1012 if (ret != FENCE_ERR_NONE) {
1013 LOGE_GEOFENCE("Fail to start standalone gps positioning. Error[%d]", ret);
1017 if (__isDataConnected() == true) {/*&& wps_state == 1) {*/
1018 LOGI_GEOFENCE("DATA CONNECTION IS TRUE");
1019 if (wps_state == 1) {
1020 LOGI_GEOFENCE("WPS STATE IS 1");
1021 __stop_gps_positioning(geofence_server); /* Stop the gps which is running as wps can be used*/
1022 __stop_gps_alarms(geofence_server);
1023 /**** Start the WPS as mobile data is connected and wifi and wps are on *******/
1024 ret = __start_wps_positioning(geofence_server, __geofence_wps_position_changed_cb);
1025 if (ret != FENCE_ERR_NONE) {
1026 LOGE_GEOFENCE("Fail to start wps positioning. Error[%d]", ret);
1033 LOGE_GEOFENCE("WPS is not supported");
1036 case NET_EVENT_OPEN_IND:
1037 LOGI_GEOFENCE("Mobile internet connected");
1038 vconf_get_int(VCONFKEY_LOCATION_NETWORK_ENABLED, &wps_state);
1039 if (__is_support_wps() == true && geofence_server->running_geopoint_cnt > 0 && wps_state == 1 && __isWifiOn() == true && __isDataConnected() == true) {
1040 /**** Start the WPS as mobile data is connected and wifi is on *******/
1041 if (geofence_server->loc_gps_started_by_wps == false && geofence_server->loc_gps_started == true) {
1042 __stop_gps_positioning(geofence_server); /*GPS should be stopped only if it is running standalone*/
1043 __stop_gps_alarms(geofence_server);
1045 ret = __start_wps_positioning(geofence_server, __geofence_wps_position_changed_cb);
1046 if (ret != FENCE_ERR_NONE) {
1047 LOGE_GEOFENCE("Fail to start wps positioning. Error[%d]", ret);
1051 case NET_EVENT_CLOSE_IND:
1052 LOGI_GEOFENCE("Mobile internet disconnected");
1053 if (__is_support_wps() == true && geofence_server->running_geopoint_cnt > 0 && geofence_server->loc_wps_started == true) {
1054 /***** Start standalone gps as mobile data is disconnected *****/
1055 __stop_wps_positioning(geofence_server);
1056 __stop_wps_alarms(geofence_server);
1057 if (geofence_server->loc_gps_started_by_wps == true) {
1058 __stop_gps_positioning(geofence_server); /*Stop the gps if it was started by wps*/
1059 __stop_gps_alarms(geofence_server);
1060 geofence_server->loc_gps_started_by_wps = false;
1062 ret = __start_gps_positioning(geofence_server, __geofence_standalone_gps_position_changed_cb);
1063 if (ret != FENCE_ERR_NONE) {
1064 LOGE_GEOFENCE("Fail to start standalone gps positioning. Error[%d]", ret);
1073 static int __gps_timeout_cb(alarm_id_t alarm_id, void *user_data)
1075 LOGI_GEOFENCE("__gps_timeout_cb");
1076 g_return_val_if_fail(user_data, -1);
1077 LOGD_GEOFENCE("alarm_id : %d", alarm_id);
1078 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
1079 geofence_server->gps_timeout_alarm_id = -1; /*resetting the alarm id*/
1080 /*Stop the gps for sometime when there is no fix*/
1081 __stop_gps_positioning(geofence_server);
1082 if (geofence_server->loc_gps_started_by_wps == FALSE) {
1083 geofence_server->gps_alarm_id = _geofence_add_alarm(1 * 60, __gps_alarm_cb, geofence_server);
1085 geofence_server->loc_gps_started_by_wps = FALSE;
1090 static int __gps_alarm_cb(alarm_id_t alarm_id, void *user_data)
1092 LOGI_GEOFENCE("__gps_alarm_cb");
1093 g_return_val_if_fail(user_data, -1);
1094 LOGD_GEOFENCE("gps alarm_id : %d", alarm_id);
1095 int ret = FENCE_ERR_NONE;
1096 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
1097 if (geofence_server->gps_alarm_id != -1) {
1098 /*LOGI_GEOFENCE("GPS interval timer removed. ID[%d]", geofence_server->gps_alarm_id);*/
1099 geofence_server->gps_alarm_id = _geofence_remove_alarm(geofence_server->gps_alarm_id);
1100 geofence_server->gps_alarm_id = -1;
1102 if (geofence_server->loc_gps_started_by_wps == true) {
1103 ret = __start_gps_positioning(geofence_server, __geofence_gps_position_changed_cb);
1104 if (ret != FENCE_ERR_NONE) {
1105 LOGE_GEOFENCE("Fail to start gps positioning. Error[%d]", ret);
1108 ret = __start_gps_positioning(geofence_server, __geofence_standalone_gps_position_changed_cb);
1109 if (ret != FENCE_ERR_NONE) {
1110 LOGE_GEOFENCE("Fail to start standalone gps positioning. Error[%d]", ret);
1116 static int __wps_timeout_cb(alarm_id_t alarm_id, void *user_data)
1118 LOGI_GEOFENCE("__wps_timeout_cb");
1119 g_return_val_if_fail(user_data, -1);
1120 LOGD_GEOFENCE("alarm_id : %d", alarm_id);
1121 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
1122 if (geofence_server->wps_timeout_alarm_id != -1) {
1123 /*LOGI_GEOFENCE("WPS timeout timer removed. ID[%d]", geofence_server->wps_timeout_alarm_id);*/
1124 geofence_server->wps_timeout_alarm_id = _geofence_remove_alarm(geofence_server->wps_timeout_alarm_id);
1125 geofence_server->wps_timeout_alarm_id = -1; /*resetting the alarm id*/
1127 /*Stop the wps for sometime when there is no fix*/
1128 __stop_wps_positioning(geofence_server);
1129 geofence_server->wps_alarm_id = _geofence_add_alarm(10, __wps_alarm_cb, geofence_server);
1130 /*display_unlock_state(LCD_OFF, PM_RESET_TIMER);*/
1134 static int __wps_alarm_cb(alarm_id_t alarm_id, void *user_data)
1136 LOGI_GEOFENCE("__wps_alarm_cb");
1137 g_return_val_if_fail(user_data, -1);
1138 LOGD_GEOFENCE("wps alarm_id : %d", alarm_id);
1139 int ret = FENCE_ERR_NONE;
1140 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
1141 if (geofence_server->wps_alarm_id != -1) {
1142 /*LOGI_GEOFENCE("WPS interval timer removed. ID[%d]", geofence_server->wps_alarm_id);*/
1143 geofence_server->wps_alarm_id = _geofence_remove_alarm(geofence_server->wps_alarm_id);
1144 geofence_server->wps_alarm_id = -1;
1146 if (__is_support_wps() == true && __isWifiOn() == true && __isDataConnected() == true) {
1147 ret = __start_wps_positioning(geofence_server, __geofence_wps_position_changed_cb);
1148 if (ret != FENCE_ERR_NONE) {
1149 LOGE_GEOFENCE("Fail to start wps positioning. Error[%d]", ret);
1152 ret = __start_gps_positioning(geofence_server, __geofence_standalone_gps_position_changed_cb);
1153 if (ret != FENCE_ERR_NONE) {
1154 LOGE_GEOFENCE("Fail to start standalone gps positioning. Error[%d]", ret);
1160 static void gps_setting_changed_cb(location_method_e method, bool enable,
1163 FUNC_ENTRANCE_SERVER;
1164 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
1165 g_return_if_fail(geofence_server);
1166 GList *tracking_fences = g_list_first(geofence_server->tracking_list);
1167 GeofenceItemData *item_data = NULL;
1168 int tracking_fence_id = 0;
1169 int ret = FENCE_ERR_NONE;
1172 /* Get the wps status */
1173 vconf_get_int(VCONFKEY_LOCATION_NETWORK_ENABLED, &wps_state);
1174 vconf_get_int(VCONFKEY_LOCATION_ENABLED, &gps_state);
1176 if (enable == false && geofence_server->running_geopoint_cnt > 0) {
1177 if (method == LOCATIONS_METHOD_GPS) {
1178 LOGI_GEOFENCE("Stopping the GPS from settings callback");
1179 __stop_gps_positioning(geofence_server);
1180 __stop_gps_alarms(geofence_server);
1182 if (wps_state == 0) { /* If data is connected then WPS will be running and alerts will be given through WPS*/
1183 while (tracking_fences) {
1184 tracking_fence_id = GPOINTER_TO_INT(tracking_fences->data);
1185 tracking_fences = g_list_next(tracking_fences);
1186 item_data = __get_item_by_fence_id(tracking_fence_id, geofence_server);
1187 if (item_data && item_data->common_info.type == GEOFENCE_TYPE_GEOPOINT) {
1188 __emit_fence_inout(geofence_server, item_data->common_info.fence_id, GEOFENCE_FENCE_STATE_OUT);
1192 } else if (method == LOCATIONS_METHOD_WPS) {
1193 LOGI_GEOFENCE("Stopping the WPS from settings callback");
1194 __stop_wps_positioning(geofence_server);
1195 __stop_wps_alarms(geofence_server);
1197 if (gps_state == 0) { /* If data is connected then WPS will be running and alerts will be given through WPS*/
1198 while (tracking_fences) {
1199 tracking_fence_id = GPOINTER_TO_INT(tracking_fences->data);
1200 tracking_fences = g_list_next(tracking_fences);
1201 item_data = __get_item_by_fence_id(tracking_fence_id, geofence_server);
1202 if (item_data && item_data->common_info.type == GEOFENCE_TYPE_GEOPOINT) {
1203 __emit_fence_inout(geofence_server, item_data->common_info.fence_id, GEOFENCE_FENCE_STATE_OUT);
1208 /* stop the gps if it was started by WPS */
1209 if (geofence_server->loc_gps_started_by_wps == true) {
1210 __stop_gps_positioning(geofence_server);
1211 __stop_gps_alarms(geofence_server);
1212 geofence_server->loc_gps_started_by_wps = false; /*So that WPS will use GPS if needed in its next fix(wps fix)*/
1214 if (geofence_server->loc_gps_started == false && gps_state == 1) {/*As WPS is turned off standalone GPS should be used for tracking the fence*/
1215 ret = __start_gps_positioning(geofence_server, __geofence_standalone_gps_position_changed_cb);
1216 if (ret != FENCE_ERR_NONE) {
1217 LOGE_GEOFENCE("Fail to start gps positioning. Error[%d]", ret);
1222 if (geofence_server->loc_gps_started_by_wps == true) {
1223 geofence_server->loc_gps_started_by_wps = false; /*So that WPS will use GPS if needed in its next fix(wps fix)*/
1225 } else if (enable == true && geofence_server->running_geopoint_cnt > 0) {
1226 if (method == LOCATIONS_METHOD_GPS) {
1227 geofence_server->loc_gps_started_by_wps = false; /* So that WPS will use GPS if needed in its next fix(wps fix) */
1228 if (wps_state == 0) { /*If wps is on then WPS would be already running. So no need to start GPS*/
1229 ret = __start_gps_positioning(geofence_server, __geofence_standalone_gps_position_changed_cb);
1230 if (ret != FENCE_ERR_NONE) {
1231 LOGE_GEOFENCE("Fail to start gps positioning. Error[%d]", ret);
1235 } else if (method == LOCATIONS_METHOD_WPS) {
1236 if (__isWifiOn() == true && __isDataConnected() == true) {/* Start WPS positioning */
1237 ret = __start_wps_positioning(geofence_server, __geofence_wps_position_changed_cb);
1238 if (ret != FENCE_ERR_NONE) {
1239 LOGE_GEOFENCE("Fail to start wps positioning. Error[%d]", ret);
1243 if (geofence_server->loc_wps_started == true) {/* If WPS is successfully started, switch off gps*/
1244 __stop_gps_positioning(geofence_server);
1245 __stop_gps_alarms(geofence_server);
1251 /*********************************THIS HAS TO BE USED ONLY FOR TESTING*********************************************/
1252 #ifdef __LOCAL_TEST__
1253 static void __free_geofence_list(gpointer userdata)
1255 GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1257 GList *tmp_fence_list = g_list_first(geofence_server->geofence_list);
1258 while (tmp_fence_list) {
1259 GeofenceItemData *tmp_data = (GeofenceItemData *)tmp_fence_list->data;
1263 tmp_fence_list = g_list_next(tmp_fence_list);
1265 geofence_server->geofence_list = NULL;
1269 static int __check_fence_permission(int fence_id, const char *app_id)
1271 access_type_e access_type = ACCESS_TYPE_PUBLIC;
1273 int ret = FENCE_ERR_NONE;
1274 ret = geofence_manager_get_access_type(fence_id, -1, &access_type);
1275 if (ret != FENCE_ERR_NONE) {
1276 LOGE("Error getting the access_type");
1279 if (access_type == ACCESS_TYPE_PRIVATE) {
1280 ret = geofence_manager_get_appid_from_geofence(fence_id, &appid);
1281 if (ret != FENCE_ERR_NONE) {
1282 LOGE("Error getting the app_id for fence_id[%d]", fence_id);
1285 if (g_strcmp0(appid, app_id)) {
1286 LOGE("Not authorized to access this private fence[%d]", fence_id);
1293 static int __check_place_permission(int place_id, const char *app_id)
1295 access_type_e access_type = ACCESS_TYPE_PUBLIC;
1297 int ret = FENCE_ERR_NONE;
1298 ret = geofence_manager_get_access_type(-1, place_id, &access_type);
1299 if (ret != FENCE_ERR_NONE) {
1300 LOGE("Error getting the access_type");
1303 if (access_type == ACCESS_TYPE_PRIVATE) {
1304 ret = geofence_manager_get_appid_from_places(place_id, &appid);
1305 if (ret != FENCE_ERR_NONE) {
1306 LOGE("Error getting the place_id for place_id[%d]", place_id);
1309 if (g_strcmp0(appid, app_id)) {
1310 LOGE("Not authorized to access this private place[%d]", place_id);
1317 static void __stop_geofence_service(gint fence_id, const gchar *app_id, gpointer userdata)
1319 FUNC_ENTRANCE_SERVER;
1320 g_return_if_fail(userdata);
1322 GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1323 GeofenceItemData *item_data = NULL;
1324 int tracking_status = -1;
1325 int ret = FENCE_ERR_NONE;
1327 access_type_e access_type = ACCESS_TYPE_UNKNOWN;
1329 item_data = __get_item_by_fence_id(fence_id, geofence_server); /*Fetch the fence details from add_list*/
1330 if (item_data == NULL) {
1331 LOGI_GEOFENCE("Invalid fence id - no fence exists with this fence id");
1332 __emit_fence_event(geofence_server, -1, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_ID_NOT_EXIST, GEOFENCE_MANAGE_FENCE_STOPPED);
1333 return; /*Invalid fence id - no fence exists with this fence id*/
1335 ret = geofence_manager_get_place_id(fence_id, &place_id);
1336 if (ret != FENCE_ERR_NONE) {
1337 LOGI_GEOFENCE("Error fetching the place_id from the DB for fence: %d", fence_id);
1338 __emit_fence_event(geofence_server, -1, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STOPPED);
1341 ret = geofence_manager_get_access_type(fence_id, -1, &access_type);
1342 if (ret != FENCE_ERR_NONE) {
1343 LOGI_GEOFENCE("Error fetching the access type from the DB for fence: %d", fence_id);
1344 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STOPPED);
1347 ret = __check_fence_permission(fence_id, app_id);
1349 LOGE("Permission denied or DB error occured while accessing the fence[%d]", fence_id);
1351 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_GEOFENCE_ACCESS_DENIED, GEOFENCE_MANAGE_FENCE_STOPPED);
1353 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STOPPED);
1357 ret = geofence_manager_get_running_status(fence_id, &tracking_status);
1358 if (ret != FENCE_ERR_NONE) {
1359 LOGI_GEOFENCE("Error fetching the running status from the DB for fence: %d", fence_id);
1360 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STOPPED);
1364 if (tracking_status == 0) {
1365 /*This fence is not in the tracking mode currently - nothing to do, just return saying the error*/
1366 LOGI_GEOFENCE("Fence ID: %d, is not in the tracking mode", fence_id);
1367 __emit_fence_event(geofence_server, place_id, fence_id, access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_FENCE_STOPPED);
1371 if (tracking_status > 0) {
1372 LOGI_GEOFENCE("Remove from tracklist: Fence id: %d", fence_id);
1373 item_data = __get_item_by_fence_id(fence_id, geofence_server);
1375 /*Item needs to be removed from the fence list*/
1376 if (item_data != NULL) {
1377 /*Main DB table should be updated here with the unsetting of running status flag*/
1378 tracking_status = tracking_status - 1;
1379 ret = geofence_manager_set_running_status(fence_id, tracking_status);
1380 if (ret != FENCE_ERR_NONE) {
1381 LOGI_GEOFENCE("Error resetting the running status in 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);
1385 /*Update the geofence count according to the type of geofence*/
1386 if (item_data->common_info.type == GEOFENCE_TYPE_GEOPOINT) {
1387 geofence_server->running_geopoint_cnt--;
1388 LOGI_GEOFENCE("Removed geopoint fence: %d from tracking list", fence_id);
1390 if (geofence_server->running_geopoint_cnt <= 0) {
1391 /*Stopping GPS...WPS*/
1392 __stop_gps_positioning(geofence_server);
1393 if (geofence_server->gps_fix_info != NULL) {
1394 g_free(geofence_server->gps_fix_info);
1395 geofence_server->gps_fix_info = NULL;
1397 geofence_server->loc_gps_started_by_wps = false;
1398 __stop_wps_positioning(geofence_server);
1399 if (geofence_server->wps_fix_info != NULL) {
1400 g_free(geofence_server->wps_fix_info);
1401 geofence_server->wps_fix_info = NULL;
1403 __stop_gps_alarms(geofence_server);
1404 __stop_wps_alarms(geofence_server);
1405 __stop_activity_service(geofence_server);
1407 } else if (item_data->common_info.type == GEOFENCE_TYPE_BT) {
1408 geofence_server->running_bt_cnt--;
1409 LOGI_GEOFENCE("Removed bt fence: %d from tracking list", fence_id);
1411 if (geofence_server->running_bt_cnt <= 0) {
1412 /*May be unsetting the cb for bt discovery can be done here*/
1414 } else if (item_data->common_info.type == GEOFENCE_TYPE_WIFI) {
1415 /*NOTHING NEED TO BE DONE HERE EXCEPT DECREMENTING THE COUNT*/
1416 geofence_server->running_wifi_cnt--;
1417 if (geofence_server->connectedTrackingWifiFenceId == fence_id) /*It means this fence is connected and it is stopped now*/
1418 geofence_server->connectedTrackingWifiFenceId = -1;
1421 if (tracking_status == 0) {
1422 /*Remove the fence from the tracklist*/
1423 LOGD_GEOFENCE("Setting the fence status as uncertain here...");
1424 item_data->common_info.status = GEOFENCE_FENCE_STATE_UNCERTAIN;
1425 item_data->common_info.proximity_status = GEOFENCE_PROXIMITY_UNCERTAIN;
1426 geofence_server->tracking_list = g_list_remove(geofence_server->tracking_list, GINT_TO_POINTER(fence_id));
1427 if (g_list_length(geofence_server->tracking_list) == 0) {
1428 g_list_free(geofence_server->tracking_list);
1429 geofence_server->tracking_list = NULL;
1433 LOGI_GEOFENCE("Geofence service is not running for this fence");
1437 /* Emit the error code */
1438 __emit_fence_event(geofence_server, place_id, fence_id, access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_FENCE_STOPPED);
1442 static bool __isWifiOn(void)
1444 int network_state = -1;
1445 vconf_get_int(VCONFKEY_WIFI_STATE, &network_state);
1446 if (network_state == 0)
1451 static bool __isDataConnected(void)
1453 bool isDataConnected = false;
1454 int network_state = -1;
1455 int data_state = -1;
1457 int rv = vconf_get_int(VCONFKEY_NETWORK_WIFI_STATE, &network_state);
1459 if (network_state == VCONFKEY_NETWORK_WIFI_CONNECTED) {
1460 LOGI_GEOFENCE("USING WIFI DATA");
1461 isDataConnected = true;
1464 if (isDataConnected == false) {
1465 rv = vconf_get_int(VCONFKEY_NETWORK_CELLULAR_STATE, &network_state);
1467 if (network_state == VCONFKEY_NETWORK_CELLULAR_ON) {
1468 rv = vconf_get_int(VCONFKEY_DNET_STATE, &data_state);
1469 if (data_state == VCONFKEY_DNET_NORMAL_CONNECTED) {
1470 LOGI_GEOFENCE("USING MOBILE DATA");
1471 isDataConnected = true;
1476 return isDataConnected;
1479 static int dbus_add_fence_cb(const gchar *app_id,
1485 const gchar *address,
1486 const gchar *bssid, const gchar *ssid, gpointer userdata)
1488 FUNC_ENTRANCE_SERVER;
1489 GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1491 /* create fence id*/
1493 int ret = FENCE_ERR_NONE;
1494 void *next_item_ptr = NULL;
1497 access_type_e access_type;
1499 ret = geofence_manager_get_access_type(-1, place_id, &access_type);
1500 if (ret != FENCE_ERR_NONE) {
1501 LOGI_GEOFENCE("Error fetching the access type from the DB for place: %d or place-id does not exist.", place_id);
1502 __emit_fence_event(geofence_server, -1, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_ID_NOT_EXIST, GEOFENCE_MANAGE_FENCE_ADDED);
1506 ret = __check_place_permission(place_id, app_id);
1508 LOGE("Unable to add the fence. Permission denied or DB error occured while accessing the place[%d]", place_id);
1510 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_GEOFENCE_ACCESS_DENIED, GEOFENCE_MANAGE_FENCE_ADDED);
1512 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_ADDED);
1516 /* create GeofenceItemData item, and append it into geofence_list*/
1517 GeofenceItemData *item_data = (GeofenceItemData *)g_malloc0(sizeof(GeofenceItemData));
1518 if (item_data == NULL) {
1519 LOGI_GEOFENCE("Unable to add the fence because of malloc fail");
1520 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_OUT_OF_MEMORY, GEOFENCE_MANAGE_FENCE_ADDED);
1524 item_data->distance = -1;
1525 item_data->client_status = GEOFENCE_CLIENT_STATUS_NONE;
1526 item_data->common_info.type = geofence_type;
1527 /*fences added by myplaces application are public fences by default*/
1528 if (!g_strcmp0(app_id, MYPLACES_APPID)) {
1529 item_data->common_info.access_type = ACCESS_TYPE_PUBLIC;
1531 item_data->common_info.access_type = ACCESS_TYPE_PRIVATE;
1533 item_data->common_info.enable = 1;
1534 item_data->common_info.status = GEOFENCE_FENCE_STATE_UNCERTAIN;
1535 item_data->common_info.proximity_status = GEOFENCE_PROXIMITY_UNCERTAIN;
1536 item_data->is_wifi_status_in = false;
1537 item_data->is_bt_status_in = false;
1538 g_strlcpy(item_data->common_info.appid, app_id, APP_ID_LEN);
1539 item_data->common_info.running_status = 0;
1540 item_data->common_info.place_id = place_id;
1542 /*DB is called and fence-id is retrieved from there(by auto increment mechanism)*/
1543 geofence_manager_set_common_info(&(item_data->common_info), &fence_id);
1544 item_data->common_info.fence_id = fence_id;
1545 LOGD_GEOFENCE("fence id : %d", item_data->common_info.fence_id);
1547 if (geofence_type == GEOFENCE_TYPE_GEOPOINT) {
1548 LOGD_GEOFENCE("Add geofence with GeoPoint");
1549 geocoordinate_info_s *geocoordinate_info = (geocoordinate_info_s *)g_malloc0(sizeof(geocoordinate_info_s));
1550 if (geocoordinate_info == NULL) {
1551 LOGI_GEOFENCE("Fail to set geocoordinate_info for GPS because of malloc fail");
1552 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_OUT_OF_MEMORY, GEOFENCE_MANAGE_FENCE_ADDED);
1555 geocoordinate_info->latitude = latitude;
1556 geocoordinate_info->longitude = longitude;
1557 if (radius < GEOFENCE_DEFAULT_RADIUS) {
1558 geocoordinate_info->radius = GEOFENCE_DEFAULT_RADIUS;
1560 geocoordinate_info->radius = radius;
1562 g_strlcpy(geocoordinate_info->address, address, ADDRESS_LEN);
1564 /*Geopoint information is saved in the DB*/
1565 ret = geofence_manager_set_geocoordinate_info(fence_id, geocoordinate_info);
1566 if (ret != FENCE_ERR_NONE) {
1567 LOGI_GEOFENCE("Fail to set geocoordinate_info");
1568 ret = geofence_manager_delete_fence_info(fence_id);
1569 if (ret != FENCE_ERR_NONE)
1570 LOGI_GEOFENCE("Fail to delete fence_id[%d] from common table", fence_id);
1571 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_ADDED);
1574 item_data->priv = (void *) geocoordinate_info;
1576 } else if (geofence_type == GEOFENCE_TYPE_WIFI) { /* Specific AP */
1577 LOGD_GEOFENCE("Add geofence with specific AP");
1579 bssid_info_s *wifi_info = NULL;
1580 wifi_info = (bssid_info_s *) g_malloc0(sizeof(bssid_info_s));
1581 if (wifi_info == NULL) {
1582 LOGI_GEOFENCE("Fail to set bssid_info for wifi because of malloc fail");
1583 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_OUT_OF_MEMORY, GEOFENCE_MANAGE_FENCE_ADDED);
1586 g_strlcpy(wifi_info->bssid, bssid, WLAN_BSSID_LEN);
1587 g_strlcpy(wifi_info->ssid, ssid, WLAN_BSSID_LEN);
1589 /*Wifi information is saved in the DB(both wifi and BT share the same bssid table here)*/
1590 ret = geofence_manager_set_bssid_info(fence_id, wifi_info);
1591 if (ret != FENCE_ERR_NONE) {
1592 LOGI_GEOFENCE("Fail to set bssid_info for wifi");
1593 ret = geofence_manager_delete_fence_info(fence_id);
1594 if (ret != FENCE_ERR_NONE)
1595 LOGI_GEOFENCE("Fail to delete fence_id[%d] from common table", fence_id);
1596 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_ADDED);
1599 item_data->priv = (void *) wifi_info;
1600 } else if (geofence_type == GEOFENCE_TYPE_BT) {
1601 LOGD_GEOFENCE("Add geofence with bluetooth bssid");
1603 bssid_info_s *bt_info = NULL;
1604 bt_info = (bssid_info_s *) g_malloc0(sizeof(bssid_info_s));
1605 if (bt_info == NULL) {
1606 LOGI_GEOFENCE("Fail to set bssid_info for BT because of malloc fail");
1607 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_OUT_OF_MEMORY, GEOFENCE_MANAGE_FENCE_ADDED);
1610 bt_info->enabled = TRUE;
1611 g_strlcpy(bt_info->bssid, bssid, WLAN_BSSID_LEN);
1612 g_strlcpy(bt_info->ssid, ssid, WLAN_BSSID_LEN);
1614 /*BT info is saved in the DB(both wifi and BT share the same bssid table here)*/
1615 ret = geofence_manager_set_bssid_info(fence_id, bt_info);
1616 if (ret != FENCE_ERR_NONE) {
1617 LOGI_GEOFENCE("Fail to set bssid_info for BT");
1618 ret = geofence_manager_delete_fence_info(fence_id);
1619 if (ret != FENCE_ERR_NONE)
1620 LOGI_GEOFENCE("Fail to delete fence_id[%d] from common table", fence_id);
1621 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_ADDED);
1624 item_data->priv = (void *) bt_info;
1626 /*Adding the data to the geofence_list which contains the added geofences list information*/
1627 if (geofence_server->geofence_list == NULL) {
1628 geofence_server->geofence_list = g_list_append(geofence_server->geofence_list, item_data);
1630 geofence_server->geofence_list = g_list_insert_before(geofence_server->geofence_list, next_item_ptr, item_data);
1632 /*This code is just for testing purpose. It will be removed after the development phase - Karthik*/
1634 GList *temp_list = g_list_first(geofence_server->geofence_list);
1635 temp_list = g_list_first(geofence_server->geofence_list);
1638 temp_list = g_list_next(temp_list);
1640 LOGI_GEOFENCE("Fences in local list: %d", temp_cnt);
1641 geofence_manager_get_count_of_fences(&temp_cnt);
1642 LOGI_GEOFENCE("Fence count in DB: %d", temp_cnt);
1644 /*Emit the error code*/
1645 __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);
1649 static int dbus_add_place_cb(const gchar *app_id,
1650 const gchar *place_name, gpointer userdata)
1652 FUNC_ENTRANCE_SERVER;
1654 int ret = FENCE_ERR_NONE;
1655 GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1656 place_info_s *place_info = (place_info_s *)g_malloc0(sizeof(place_info_s));
1658 if (place_info == NULL) {
1659 LOGI_GEOFENCE("Unable to add the place due to malloc fail");
1660 __emit_fence_event(geofence_server, -1, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_OUT_OF_MEMORY, GEOFENCE_MANAGE_PLACE_ADDED);
1663 /*fences added by myplaces application are public fences by default*/
1664 if (!g_strcmp0(app_id, MYPLACES_APPID))
1665 place_info->access_type = ACCESS_TYPE_PUBLIC;
1667 place_info->access_type = ACCESS_TYPE_PRIVATE;
1669 g_strlcpy(place_info->place_name, place_name, PLACE_NAME_LEN);
1670 g_strlcpy(place_info->appid, app_id, APP_ID_LEN);
1671 /*Add the place details to db*/
1672 ret = geofence_manager_set_place_info(place_info, &place_id);
1673 if (ret != FENCE_ERR_NONE) {
1674 LOGI_GEOFENCE("Unable to add the place due to DB error");
1675 __emit_fence_event(geofence_server, -1, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_PLACE_ADDED);
1678 __emit_fence_event(geofence_server, place_id, -1, place_info->access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_PLACE_ADDED);
1683 static void dbus_enable_geofence_cb(gint fence_id, const gchar *app_id, gboolean enable, gpointer userdata)
1685 FUNC_ENTRANCE_SERVER;
1686 g_return_if_fail(userdata);
1688 GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1689 int ret = FENCE_ERR_NONE;
1690 access_type_e access_type = ACCESS_TYPE_UNKNOWN;
1692 int enable_status = 0;
1693 geofence_manage_e manage_enum = GEOFENCE_MANAGE_SETTING_ENABLED;
1696 manage_enum = GEOFENCE_MANAGE_SETTING_DISABLED;
1698 ret = geofence_manager_get_access_type(fence_id, -1, &access_type);
1699 if (ret != FENCE_ERR_NONE) {
1700 LOGI_GEOFENCE("Error fetching the access type from the DB for fence: %d or fence-id does not exist.", fence_id);
1701 __emit_fence_event(geofence_server, -1, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_ID_NOT_EXIST, manage_enum);
1704 ret = geofence_manager_get_place_id(fence_id, &place_id);
1705 if (ret != FENCE_ERR_NONE) {
1706 LOGI_GEOFENCE("Error fetching the place_id from the DB for fence: %d", fence_id);
1707 __emit_fence_event(geofence_server, -1, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, manage_enum);
1710 if (access_type == ACCESS_TYPE_PUBLIC) {
1711 if (g_strcmp0(app_id, MYPLACES_APPID) != 0) {
1712 LOGI_GEOFENCE("Received: %s", app_id);
1713 LOGI_GEOFENCE("Not authorized to enable/disable this fence[%d] service.", fence_id);
1714 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_GEOFENCE_ACCESS_DENIED, manage_enum);
1719 ret = geofence_manager_set_enable_status(fence_id, enable_status);
1720 if (ret != FENCE_ERR_NONE) {
1721 LOGI_GEOFENCE("DB error in enabling/disabling the fence[%d].", fence_id);
1722 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, manage_enum);
1726 LOGI_GEOFENCE("Currently, only public fences can be enabled/disabled. It can be done by MyPlaces app only.");
1728 /*Emit the error code*/
1729 __emit_fence_event(geofence_server, place_id, fence_id, access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, manage_enum);
1732 static void dbus_update_place_cb(gint place_id, const gchar *app_id, const gchar *place_name, gpointer userdata)
1734 FUNC_ENTRANCE_SERVER;
1735 int ret = FENCE_ERR_NONE;
1736 GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1738 if (place_id == DEFAULT_PLACE_HOME || place_id == DEFAULT_PLACE_OFFICE || place_id == DEFAULT_PLACE_CAR) {
1739 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_PLACE_ACCESS_DENIED, GEOFENCE_MANAGE_PLACE_UPDATED);
1743 place_info_s *place_info = (place_info_s *) g_malloc0(sizeof(place_info_s));
1744 if (place_info == NULL) {
1745 LOGI_GEOFENCE("malloc fail for place id[%d]", place_id);
1746 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_OUT_OF_MEMORY, GEOFENCE_MANAGE_PLACE_UPDATED);
1749 ret = geofence_manager_get_place_info(place_id, &place_info);
1750 if (ret != FENCE_ERR_NONE) {
1751 LOGI_GEOFENCE("Place_id does not exist or DB error in getting the place info for place_id[%d].", place_id);
1752 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_ID_NOT_EXIST, GEOFENCE_MANAGE_PLACE_UPDATED);
1756 if (g_strcmp0(app_id, place_info->appid) != 0) {
1757 LOGI_GEOFENCE("Not authorized to update the place");
1758 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_PLACE_ACCESS_DENIED, GEOFENCE_MANAGE_PLACE_UPDATED);
1763 /*Update the place details to db*/
1764 ret = geofence_manager_update_place_info(place_id, place_name);
1765 if (ret != FENCE_ERR_NONE) {
1766 LOGI_GEOFENCE("Unable to update the place");
1767 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_PLACE_UPDATED);
1771 __emit_fence_event(geofence_server, place_id, -1, place_info->access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_PLACE_UPDATED);
1775 static void dbus_remove_fence_cb(gint fence_id, const gchar *app_id, gpointer userdata)
1777 FUNC_ENTRANCE_SERVER;
1778 GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1779 g_return_if_fail(geofence_server);
1780 int ret = FENCE_ERR_NONE;
1783 access_type_e access_type = ACCESS_TYPE_UNKNOWN;
1785 /*//////////Required to be sent in the event callback////////////////--*/
1786 ret = geofence_manager_get_place_id(fence_id, &place_id);
1787 if (ret != FENCE_ERR_NONE) {
1788 LOGI_GEOFENCE("Error fetching the place_id from the DB for fence: %d or fence-id does not exist", fence_id);
1789 __emit_fence_event(geofence_server, -1, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_ID_NOT_EXIST, GEOFENCE_MANAGE_FENCE_REMOVED);
1792 /*//////////////////////////////////////////////////////////////////--*/
1793 ret = geofence_manager_get_appid_from_geofence(fence_id, &app_id_db);
1794 if (ret != FENCE_ERR_NONE) {
1795 LOGI_GEOFENCE("Failed to get the appid, Error - %d", ret);
1796 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_REMOVED);
1799 if (g_strcmp0(app_id_db, app_id) != 0) {
1800 LOGI_GEOFENCE("Not authorized to remove the fence");
1802 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_GEOFENCE_ACCESS_DENIED, GEOFENCE_MANAGE_FENCE_REMOVED);
1806 /*/////////required to be sent in the event callback///////////////--*/
1807 ret = geofence_manager_get_access_type(fence_id, -1, &access_type);
1808 if (ret != FENCE_ERR_NONE) {
1809 LOGI_GEOFENCE("Error fetching the access type from the DB for fence: %d", fence_id);
1810 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_REMOVED);
1813 /*///////////////////////////////////////////////////////////////////////--*/
1814 GeofenceItemData *item_data = __get_item_by_fence_id(fence_id, geofence_server);
1815 if (item_data == NULL) {
1816 LOGI_GEOFENCE("Invalid fence_id[%d]", fence_id);
1820 /*Stop the geofence service for the fence first if it is running*/
1821 int tracking_status = -1;
1822 ret = geofence_manager_get_running_status(fence_id, &tracking_status);
1823 if (ret != FENCE_ERR_NONE) {
1824 LOGI_GEOFENCE("Error fetching the running status from the DB for fence: %d or fence-id does not exist", fence_id);
1825 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_REMOVED);
1828 if (tracking_status == 1) {
1829 __stop_geofence_service(fence_id, app_id, userdata);
1830 } else if (tracking_status > 1) {/*its a public fence*/
1831 tracking_status = 1; /*resetting the running status to 1 for forcefull stop from MYPlacesApp*/
1832 ret = geofence_manager_set_running_status(fence_id, tracking_status);
1833 if (ret != FENCE_ERR_NONE) {
1834 LOGI_GEOFENCE("Error resetting the running status in the DB for fence: %d or fence-id does not exist", fence_id);
1835 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_REMOVED);
1838 __stop_geofence_service(fence_id, app_id, userdata);
1840 /*Removing the fence id from the DB*/
1841 ret = geofence_manager_delete_fence_info(fence_id);
1842 if (ret != FENCE_ERR_NONE) {
1843 LOGI_GEOFENCE("Fail to delete fence_id[%d]", fence_id);
1844 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_REMOVED);
1848 /*Removing the fence id from the geofence_list which contains the added fence list details*/
1849 geofence_server->geofence_list = g_list_remove(geofence_server->geofence_list, item_data);
1850 LOGI_GEOFENCE("Removed fence_id[%d]", fence_id);
1851 g_free(item_data); /*freeing the memory*/
1853 /*Check if the length of the geofence_list is 0 then free and make it null*/
1854 if (g_list_length(geofence_server->geofence_list) == 0) {
1855 g_list_free(geofence_server->geofence_list);
1856 geofence_server->geofence_list = NULL;
1858 /*Emit the error code*/
1859 __emit_fence_event(geofence_server, place_id, fence_id, access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_FENCE_REMOVED);
1862 static void dbus_get_place_name_cb(gint place_id, const gchar *app_id, char **place_name, int *error_code, gpointer userdata)
1864 FUNC_ENTRANCE_SERVER;
1865 access_type_e access_type = ACCESS_TYPE_UNKNOWN;
1867 int ret = geofence_manager_get_access_type(-1, place_id, &access_type);
1868 if (ret != FENCE_ERR_NONE) {
1869 LOGI_GEOFENCE("Error fetching the access type from the DB for place: %d or place-id does not exist.", place_id);
1870 *error_code = GEOFENCE_SERVER_ERROR_ID_NOT_EXIST;
1874 ret = __check_place_permission(place_id, app_id);
1876 LOGE("Unable to get the place name. Permission denied or DB error occured while accessing the place[%d]", place_id);
1878 *error_code = GEOFENCE_SERVER_ERROR_PLACE_ACCESS_DENIED;
1880 *error_code = GEOFENCE_SERVER_ERROR_DATABASE;
1884 ret = geofence_manager_get_place_name(place_id, place_name);
1885 if (ret != FENCE_ERR_NONE) {
1886 *error_code = GEOFENCE_SERVER_ERROR_DATABASE;
1889 *error_code = GEOFENCE_SERVER_ERROR_NONE;
1892 static void dbus_remove_place_cb(gint place_id, const gchar *app_id,
1895 FUNC_ENTRANCE_SERVER;
1896 g_return_if_fail(userdata);
1897 GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1898 GList *fence_list = NULL, *list = NULL;
1900 int tracking_status = 0;
1901 int ret = FENCE_ERR_NONE;
1902 GeofenceItemData *item_data = NULL;
1903 access_type_e access_type = ACCESS_TYPE_UNKNOWN;
1905 /* Default places */
1906 if (place_id == DEFAULT_PLACE_HOME || place_id == DEFAULT_PLACE_OFFICE || place_id == DEFAULT_PLACE_CAR) {
1907 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_PLACE_ACCESS_DENIED, GEOFENCE_MANAGE_PLACE_REMOVED);
1910 ret = geofence_manager_get_access_type(-1, place_id, &access_type);
1911 if (ret != FENCE_ERR_NONE) {
1912 LOGE("Unable to fetch the access type for place_id[%d]", place_id);
1913 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_ID_NOT_EXIST, GEOFENCE_MANAGE_PLACE_REMOVED);
1917 place_info_s *place_info =
1918 (place_info_s *) g_malloc0(sizeof(place_info_s));
1919 ret = geofence_manager_get_place_info(place_id, &place_info);
1920 if (ret != FENCE_ERR_NONE) {
1921 LOGI_GEOFENCE("Place_id does not exist or DB error in getting the place info for place_id[%d].", place_id);
1922 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_ID_NOT_EXIST, GEOFENCE_MANAGE_PLACE_REMOVED);
1926 if (g_strcmp0(app_id, place_info->appid) != 0) {
1927 LOGI_GEOFENCE("Not authorized to remove the place");
1929 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_PLACE_ACCESS_DENIED, GEOFENCE_MANAGE_PLACE_REMOVED);
1934 ret = geofence_manager_get_fenceid_list_from_db(&fence_cnt, &fence_list, place_id);
1935 if (ret != FENCE_ERR_NONE) {
1936 LOGE("Unable to fetch the fence list from the DB");
1937 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_PLACE_REMOVED);
1941 list = g_list_first(fence_list);
1943 fence_id = GPOINTER_TO_INT(list->data);
1944 item_data = __get_item_by_fence_id(fence_id, geofence_server);
1945 ret = geofence_manager_get_running_status(fence_id, &tracking_status);
1946 if (ret != FENCE_ERR_NONE) {
1947 LOGE("Unable to fetch the running status before removing the fence while removing a place");
1948 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_PLACE_REMOVED);
1951 if (tracking_status == 1) {
1952 __stop_geofence_service(fence_id, app_id, userdata);
1953 } else if (tracking_status > 1) {
1954 tracking_status = 1; /*resetting the running status as it is a forcefull stop from MYPlacesApp*/
1955 ret = geofence_manager_set_running_status(fence_id, tracking_status);
1956 if (ret != FENCE_ERR_NONE) {
1957 LOGI_GEOFENCE("Error setting the running status from the DB for fence: %d or fence-id does not exist", fence_id);
1958 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_PLACE_REMOVED);
1961 __stop_geofence_service(fence_id, app_id, userdata);
1964 /*Removing the fence id from the geofence_list which contains the added fence list details*/
1965 geofence_server->geofence_list = g_list_remove(geofence_server->geofence_list, item_data);
1966 LOGI_GEOFENCE("Removed fence_id[%d]", fence_id);
1968 /*Check if the length of the geofence_list is 0 then free and make it null*/
1969 if (g_list_length(geofence_server->geofence_list) == 0) {
1970 g_list_free(geofence_server->geofence_list);
1971 geofence_server->geofence_list = NULL;
1973 list = g_list_next(list);
1975 ret = geofence_manager_delete_place_info(place_id);
1976 if (ret != FENCE_ERR_NONE) {
1977 LOGI_GEOFENCE("DB error occured while removing the place from DB");
1978 __emit_fence_event(geofence_server, place_id, -1, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_PLACE_REMOVED);
1981 __emit_fence_event(geofence_server, place_id, -1, access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_PLACE_REMOVED);
1984 static void dbus_start_geofence_cb(gint fence_id, const gchar *app_id, gpointer userdata)
1986 FUNC_ENTRANCE_SERVER;
1987 g_return_if_fail(userdata);
1989 GeofenceServer *geofence_server = (GeofenceServer *) userdata;
1990 int tracking_fence_id = -1;
1991 void *next_item_ptr = NULL;
1992 GList *track_list = g_list_first(geofence_server->tracking_list);
1993 GeofenceItemData *item_data = NULL;
1995 int ret = FENCE_ERR_NONE;
1996 int tracking_status = -1;
1998 access_type_e access_type = ACCESS_TYPE_UNKNOWN;
1999 char *app_id_db = NULL;
2000 geofence_fence_state_e status_to_be_emitted = GEOFENCE_FENCE_STATE_UNCERTAIN;
2002 item_data = __get_item_by_fence_id(fence_id, geofence_server); /*Fetch the fence details from add_list*/
2003 if (item_data == NULL) {
2004 LOGI_GEOFENCE("Invalid fence id - no fence exists with this fence id");
2005 __emit_fence_event(geofence_server, -1, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_ID_NOT_EXIST, GEOFENCE_MANAGE_FENCE_STARTED);
2006 return; /*Invalid fence id - no fence exists with this fence id*/
2008 if (!g_strcmp0(app_id, MYPLACES_APPID)) {
2009 LOGI_GEOFENCE("My Places cannot start a fence");
2010 __emit_fence_event(geofence_server, -1, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_GEOFENCE_ACCESS_DENIED, GEOFENCE_MANAGE_FENCE_STARTED);
2013 ret = geofence_manager_get_place_id(fence_id, &place_id);
2014 if (ret != FENCE_ERR_NONE) {
2015 LOGI_GEOFENCE("Error fetching the place_id from the DB for fence: %d", fence_id);
2016 __emit_fence_event(geofence_server, -1, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STARTED);
2020 ret = geofence_manager_get_running_status(fence_id, &tracking_status);
2021 if (ret != FENCE_ERR_NONE) {
2022 LOGI_GEOFENCE("Error fetching the running status from the DB for fence: %d or fence-id does not exist.", fence_id);
2023 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STARTED);
2027 ret = geofence_manager_get_access_type(fence_id, -1, &access_type);
2028 if (ret != FENCE_ERR_NONE) {
2029 LOGE("Error getting the access_type");
2032 if (access_type == ACCESS_TYPE_PRIVATE) {
2033 ret = geofence_manager_get_appid_from_geofence(fence_id, &app_id_db);
2034 if (ret != FENCE_ERR_NONE) {
2035 LOGE("Error getting the app_id for fence_id[%d]", fence_id);
2036 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STARTED);
2039 if (g_strcmp0(app_id_db, app_id)) {
2040 LOGE("Not authorized to access this private fence[%d]", fence_id);
2041 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_GEOFENCE_ACCESS_DENIED, GEOFENCE_MANAGE_FENCE_STARTED);
2046 if (tracking_status == 1) {
2047 LOGI_GEOFENCE("Private fence ID: %d, already exists in the tracking list", fence_id);
2048 __emit_fence_event(geofence_server, place_id, fence_id, access_type, app_id, GEOFENCE_SERVER_ERROR_ALREADY_STARTED, GEOFENCE_MANAGE_FENCE_STARTED);
2051 ret = geofence_manager_set_running_status(fence_id, 1);
2052 if (ret != FENCE_ERR_NONE) {
2053 LOGI_GEOFENCE("Error setting the fence status");
2054 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STARTED);
2057 tracking_status = 1;
2059 } else if (access_type == ACCESS_TYPE_PUBLIC) {
2061 ret = geofence_manager_get_enable_status(fence_id, &enable);
2062 if (ret != FENCE_ERR_NONE) {
2063 LOGI_GEOFENCE("Error fetching the enable status from the DB for fence: %d or fence-id does not exist.", fence_id);
2064 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STARTED);
2068 LOGI_GEOFENCE("Error - Fence[%d] is not enabled",
2070 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_GEOFENCE_ACCESS_DENIED, GEOFENCE_MANAGE_FENCE_STARTED);
2073 if (tracking_status > 0) {
2074 LOGI_GEOFENCE("Public fence ID: %d, already exists in the tracking list, incrementing the counter for fence", fence_id);
2075 ret = geofence_manager_set_running_status(fence_id, (tracking_status + 1));
2076 if (ret != FENCE_ERR_NONE) {
2077 LOGI_GEOFENCE("Error setting the fence status");
2078 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STARTED);
2081 __emit_fence_event(geofence_server, place_id, fence_id, access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_FENCE_STARTED);
2084 ret = geofence_manager_set_running_status(fence_id, (tracking_status + 1));
2085 if (ret != FENCE_ERR_NONE) {
2086 LOGI_GEOFENCE("Error setting the fence status");
2087 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_DATABASE, GEOFENCE_MANAGE_FENCE_STARTED);
2094 item_data->client_status = GEOFENCE_CLIENT_STATUS_START;
2096 if (item_data->common_info.type == GEOFENCE_TYPE_GEOPOINT) {
2098 if (__is_support_wps() == true && __isDataConnected() == true && __isWifiOn() == true) {
2099 ret = __start_wps_positioning(geofence_server, __geofence_wps_position_changed_cb);
2100 if (ret != FENCE_ERR_NONE) {
2101 LOGE_GEOFENCE("Fail to start wps positioning. Error[%d]", ret);
2102 geofence_manager_set_running_status(fence_id, (tracking_status - 1));
2103 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_IPC, GEOFENCE_MANAGE_FENCE_STARTED);
2107 ret = __start_gps_positioning(geofence_server, __geofence_standalone_gps_position_changed_cb);
2108 if (ret != FENCE_ERR_NONE) {
2109 LOGE_GEOFENCE("Fail to start gps positioning. Error[%d]", ret);
2110 geofence_manager_set_running_status(fence_id, (tracking_status - 1));
2111 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_IPC, GEOFENCE_MANAGE_FENCE_STARTED);
2115 geofence_server->running_geopoint_cnt++;
2116 __start_activity_service(geofence_server);
2117 } else if (item_data->common_info.type == GEOFENCE_TYPE_BT) {
2118 LOGI_GEOFENCE("fence_type [GEOFENCE_TYPE_BT]");
2120 bssid_info_s *bt_info = NULL;
2121 if (item_data->priv != NULL) {
2122 bt_info = (bssid_info_s *) item_data->priv;
2123 if (!bt_info->enabled)
2124 bt_info->enabled = TRUE;
2126 bt_adapter_state_e adapter_state = BT_ADAPTER_DISABLED;
2127 bt_error_e error = BT_ERROR_NONE;
2128 error = bt_adapter_get_state(&adapter_state);
2129 if (error == BT_ERROR_NONE) {
2130 geofence_server->running_bt_cnt++;
2131 if (adapter_state == BT_ADAPTER_DISABLED) {
2132 LOGE_GEOFENCE("BT Adapter is DISABLED");
2133 status_to_be_emitted = GEOFENCE_FENCE_STATE_OUT;
2134 } else if (adapter_state == BT_ADAPTER_ENABLED) {
2135 bt_device_info_s *bt_device_info = NULL;
2136 if (bt_info != NULL) {
2137 ret = bt_adapter_get_bonded_device_info(bt_info->bssid, &bt_device_info);
2138 if (ret != BT_ERROR_NONE) {
2139 LOGE_GEOFENCE("Fail to get the bonded device info/ Not bonded with any device. Error[%d]", ret);
2140 /*NEED TO BE DECIDED WHETHER TO REQUEST FOR A SCAN HERE OR JUST EMIT OUT AS STATUS*/
2141 if (ret == BT_ERROR_REMOTE_DEVICE_NOT_BONDED)
2142 status_to_be_emitted = GEOFENCE_FENCE_STATE_OUT;
2144 if (bt_device_info == NULL) {
2145 LOGI_GEOFENCE("bt_adapter_get_bonded_device_info [%s] failed.", bt_info->bssid);
2146 status_to_be_emitted = GEOFENCE_FENCE_STATE_OUT;
2148 if (bt_device_info->is_bonded == TRUE && bt_device_info->is_connected == TRUE) {
2149 LOGI_GEOFENCE("[%s] bonded TRUE, connected TRUE", bt_info->bssid);
2150 status_to_be_emitted = GEOFENCE_FENCE_STATE_IN;
2152 status_to_be_emitted = GEOFENCE_FENCE_STATE_OUT;
2155 ret = bt_adapter_free_device_info(bt_device_info);
2156 if (ret != BT_ERROR_NONE)
2157 LOGE_GEOFENCE("bt_adapter_free_device_info fail[%d]", ret);
2163 if (error != BT_ERROR_NONE) {
2164 LOGI_GEOFENCE("Unable to get the BT adapter state. Not added to track list: %d", error);
2165 geofence_manager_set_running_status(fence_id, (tracking_status - 1));
2166 __emit_fence_event(geofence_server, place_id, fence_id, ACCESS_TYPE_UNKNOWN, app_id, GEOFENCE_SERVER_ERROR_IPC, GEOFENCE_MANAGE_FENCE_STARTED);
2170 } else if (item_data->common_info.type == GEOFENCE_TYPE_WIFI) {
2171 LOGI_GEOFENCE("fence_type [GEOFENCE_TYPE_WIFI]");
2172 wifi_error_e rv = WIFI_ERROR_NONE;
2175 char *ap_bssid = NULL;
2177 bssid_info_s *wifi_info = NULL;
2178 vconf_get_int(VCONFKEY_WIFI_STATE, &nWifiState);
2180 if (nWifiState != 0) {
2181 LOGI_GEOFENCE("Wifi is on...");
2182 geofence_server->running_wifi_cnt++; /*Incrementing the counter for wifi fence*/
2184 if (item_data->priv != NULL) {
2185 wifi_info = (bssid_info_s *) item_data->priv;
2187 rv = wifi_get_connected_ap(&ap_h);
2188 if (rv != WIFI_ERROR_NONE) {
2189 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));
2190 if (rv == WIFI_ERROR_NO_CONNECTION) {
2191 LOGI_GEOFENCE("Not connected to any AP");
2192 status_to_be_emitted = GEOFENCE_FENCE_STATE_OUT;
2195 rv = wifi_ap_get_bssid(ap_h, &ap_bssid);
2196 if (rv != WIFI_ERROR_NONE) {
2197 LOGI_GEOFENCE("Fail to get the bssid: [%d]\n", __convert_wifi_error_to_string(rv));
2199 bssidlen = strlen(ap_bssid);
2200 LOGI_GEOFENCE("Connected AP: %s, %d\n", ap_bssid, bssidlen);
2201 if (g_strcmp0(wifi_info->bssid, ap_bssid) == 0) {
2202 status_to_be_emitted = GEOFENCE_FENCE_STATE_IN;
2203 geofence_server->connectedTrackingWifiFenceId = fence_id;
2205 status_to_be_emitted = GEOFENCE_FENCE_STATE_OUT;
2211 LOGI_GEOFENCE("Wifi is not switched on...");
2212 geofence_server->running_wifi_cnt++; /*Incrementing the counter for wifi fence*/
2213 /*Emit the fence status as out as wifi is not switched on here*/
2214 status_to_be_emitted = GEOFENCE_FENCE_STATE_OUT;
2217 LOGI_GEOFENCE("Invalid fence_type[%d]",
2218 item_data->common_info.type);
2221 /*Adding the fence to the tracking list*/
2222 LOGI_GEOFENCE("Add to tracklist: Fence id: %d", fence_id);
2223 if (geofence_server->tracking_list == NULL) {
2224 geofence_server->tracking_list = g_list_append(geofence_server->tracking_list, GINT_TO_POINTER(fence_id));
2226 geofence_server->tracking_list = g_list_insert_before(geofence_server->tracking_list, next_item_ptr, GINT_TO_POINTER(fence_id));
2228 LOGI_GEOFENCE("Added fence id: Fence id: %d", fence_id);
2230 __emit_fence_event(geofence_server, place_id, fence_id, access_type, app_id, GEOFENCE_SERVER_ERROR_NONE, GEOFENCE_MANAGE_FENCE_STARTED);
2232 __emit_fence_inout(geofence_server, item_data->common_info.fence_id, status_to_be_emitted);
2234 track_list = g_list_first(geofence_server->tracking_list);
2235 while (track_list) {
2236 tracking_fence_id = GPOINTER_TO_INT(track_list->data);
2237 LOGI_GEOFENCE("%d", tracking_fence_id);
2238 track_list = g_list_next(track_list);
2242 static void dbus_stop_geofence_cb(gint fence_id, const gchar *app_id, gpointer userdata)
2244 __stop_geofence_service(fence_id, app_id, userdata);
2247 static void __start_activity_service(GeofenceServer *geofence_server)
2249 FUNC_ENTRANCE_SERVER;
2250 bool activity_supported = TRUE;
2251 int ret = ACTIVITY_ERROR_NONE;
2253 if (geofence_server->activity_stationary_h == NULL) {
2254 activity_is_supported(ACTIVITY_STATIONARY, &activity_supported);
2255 if (activity_supported == TRUE) {
2256 ret = activity_create(&(geofence_server->activity_stationary_h));
2257 if (ret != ACTIVITY_ERROR_NONE) {
2258 LOGD_GEOFENCE("Fail to create stationary activity %d", ret);
2260 ret = activity_start_recognition(geofence_server->activity_stationary_h, ACTIVITY_STATIONARY, __activity_cb, geofence_server);
2261 if (ret != ACTIVITY_ERROR_NONE)
2262 LOGD_GEOFENCE("Fail to start stationary activity %d", ret);
2264 LOGD_GEOFENCE("Success to start stationary activity");
2267 LOGD_GEOFENCE("Not support stationary activity");
2270 if (geofence_server->activity_walk_h == NULL) {
2271 activity_is_supported(ACTIVITY_WALK, &activity_supported);
2272 if (activity_supported == TRUE) {
2273 ret = activity_create(&(geofence_server->activity_walk_h));
2274 if (ret != ACTIVITY_ERROR_NONE) {
2275 LOGD_GEOFENCE("Fail to create walk activity %d", ret);
2277 ret = activity_start_recognition(geofence_server->activity_walk_h, ACTIVITY_WALK, __activity_cb, geofence_server);
2278 if (ret != ACTIVITY_ERROR_NONE)
2279 LOGD_GEOFENCE("Fail to start walk activity %d", ret);
2281 LOGD_GEOFENCE("Success to start walk activity");
2284 LOGD_GEOFENCE("Not support walk activity");
2287 if (geofence_server->activity_run_h == NULL) {
2288 activity_is_supported(ACTIVITY_RUN, &activity_supported);
2289 if (activity_supported == TRUE) {
2290 ret = activity_create(&(geofence_server->activity_run_h));
2291 if (ret != ACTIVITY_ERROR_NONE) {
2292 LOGD_GEOFENCE("Fail to create run activity %d", ret);
2294 ret = activity_start_recognition(geofence_server->activity_run_h, ACTIVITY_RUN, __activity_cb, geofence_server);
2295 if (ret != ACTIVITY_ERROR_NONE)
2296 LOGD_GEOFENCE("Fail to start run activity %d", ret);
2298 LOGD_GEOFENCE("Success to start run activity");
2301 LOGD_GEOFENCE("Not support run activity");
2304 if (geofence_server->activity_in_vehicle_h == NULL) {
2305 activity_is_supported(ACTIVITY_IN_VEHICLE, &activity_supported);
2306 if (activity_supported == TRUE) {
2307 ret = activity_create(&(geofence_server->activity_in_vehicle_h));
2308 if (ret != ACTIVITY_ERROR_NONE) {
2309 LOGD_GEOFENCE("Fail to create in_vehicle activity %d", ret);
2311 ret = activity_start_recognition(geofence_server->activity_in_vehicle_h, ACTIVITY_IN_VEHICLE, __activity_cb, geofence_server);
2312 if (ret != ACTIVITY_ERROR_NONE)
2313 LOGD_GEOFENCE("Fail to start in_vehicle activity %d", ret);
2315 LOGD_GEOFENCE("Success to start in_vehicle activity");
2318 LOGD_GEOFENCE("Not support in_vehicle activity");
2322 static void __stop_activity_service(GeofenceServer *geofence_server)
2324 FUNC_ENTRANCE_SERVER;
2325 int ret = ACTIVITY_ERROR_NONE;
2327 if (geofence_server->activity_stationary_h != NULL) {
2328 ret = activity_stop_recognition(geofence_server->activity_stationary_h);
2329 if (ret != ACTIVITY_ERROR_NONE)
2330 LOGD_GEOFENCE("Fail to stop stationary activity %d", ret);
2332 LOGD_GEOFENCE("Success to stop stationary activity");
2334 ret = activity_release(geofence_server->activity_stationary_h);
2335 if (ret != ACTIVITY_ERROR_NONE)
2336 LOGD_GEOFENCE("Fail to release stationary activity %d", ret);
2338 geofence_server->activity_stationary_h = NULL;
2341 if (geofence_server->activity_walk_h != NULL) {
2342 ret = activity_stop_recognition(geofence_server->activity_walk_h);
2343 if (ret != ACTIVITY_ERROR_NONE)
2344 LOGD_GEOFENCE("Fail to stop walk activity %d", ret);
2346 LOGD_GEOFENCE("Success to stop walk activity");
2348 ret = activity_release(geofence_server->activity_walk_h);
2349 if (ret != ACTIVITY_ERROR_NONE)
2350 LOGD_GEOFENCE("Fail to release walk activity %d", ret);
2352 geofence_server->activity_walk_h = NULL;
2355 if (geofence_server->activity_run_h != NULL) {
2356 ret = activity_stop_recognition(geofence_server->activity_run_h);
2357 if (ret != ACTIVITY_ERROR_NONE)
2358 LOGD_GEOFENCE("Fail to stop run activity %d", ret);
2360 LOGD_GEOFENCE("Success to stop run activity");
2362 ret = activity_release(geofence_server->activity_run_h);
2363 if (ret != ACTIVITY_ERROR_NONE)
2364 LOGD_GEOFENCE("Fail to release run activity %d", ret);
2366 geofence_server->activity_run_h = NULL;
2369 if (geofence_server->activity_in_vehicle_h != NULL) {
2370 ret = activity_stop_recognition(geofence_server->activity_in_vehicle_h);
2371 if (ret != ACTIVITY_ERROR_NONE)
2372 LOGD_GEOFENCE("Fail to stop in_vehicle activity %d", ret);
2374 LOGD_GEOFENCE("Success to stop in_vehicle activity");
2376 ret = activity_release(geofence_server->activity_in_vehicle_h);
2377 if (ret != ACTIVITY_ERROR_NONE)
2378 LOGD_GEOFENCE("Fail to release in_vehicle activity %d", ret);
2380 geofence_server->activity_in_vehicle_h = NULL;
2384 static void __activity_cb(activity_type_e type, const activity_data_h data, double timestamp, activity_error_e error, void *user_data)
2386 FUNC_ENTRANCE_SERVER
2387 GeofenceServer *geofence_server = (GeofenceServer *)user_data;
2388 activity_accuracy_e accuracy;
2389 int result = ACTIVITY_ERROR_NONE;
2391 if (error != ACTIVITY_ERROR_NONE) {
2392 LOGD_GEOFENCE("Error in activity callback %d", error);
2396 result = activity_get_accuracy(data, &accuracy);
2397 if (result != ACTIVITY_ERROR_NONE) {
2398 LOGD_GEOFENCE("Fail to get accuracy of activity %d", error);
2402 if (accuracy >= ACTIVITY_ACCURACY_MID) {
2403 geofence_server->activity_type = type;
2404 geofence_server->activity_timestamp = timestamp;
2406 LOGD_GEOFENCE("Activity type = %d, timestamp = %lf", type, timestamp);
2410 static GVariant *dbus_get_geofences_cb(int place_id, const gchar *app_id, int *fenceCnt, int *errorCode, gpointer userdata)
2412 geofence_info_s *item;
2414 GList *fence_list = NULL, *list = NULL;
2415 place_info_s *place_info = NULL;
2416 int count = 0, fence_cnt = 0;
2419 LOGI_GEOFENCE(">>> Enter");
2420 /*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.*/
2421 /*Whenever fence_list in a particular place is requested place_id will not be -1. This is jusr maintained internally*/
2422 if (place_id == -1) {
2423 ret = geofence_manager_get_fence_list_from_db(&count, &fence_list, -1);
2425 ret = geofence_manager_get_place_info(place_id, &place_info);
2426 if (ret != FENCE_ERR_NONE) {
2427 LOGE("Error getting the place info for place_id[%d]", place_id);
2428 /* Send ZERO data gvariant*/
2429 *errorCode = GEOFENCE_SERVER_ERROR_DATABASE;
2430 *fenceCnt = fence_cnt;
2431 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2432 return g_variant_builder_end(&b);
2434 if ((place_info != NULL) && (place_info->access_type == ACCESS_TYPE_PRIVATE)) {
2435 if (g_strcmp0(app_id, place_info->appid) != 0) {
2436 LOGI_GEOFENCE("Not authorized to access this private place[%d]", place_id);
2439 /* Send ZERO data gvariant*/
2440 *errorCode = GEOFENCE_SERVER_ERROR_PLACE_ACCESS_DENIED;
2441 *fenceCnt = fence_cnt;
2442 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2443 return g_variant_builder_end(&b);
2448 ret = geofence_manager_get_fence_list_from_db(&count, &fence_list, place_id);
2450 LOGI_GEOFENCE("count = %d", count);
2452 if (ret != FENCE_ERR_NONE) {
2453 LOGI_GEOFENCE("get list failed");
2454 /* Send ZERO data gvariant*/
2455 *errorCode = GEOFENCE_SERVER_ERROR_DATABASE;
2456 *fenceCnt = fence_cnt;
2457 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2458 return g_variant_builder_end(&b);
2459 } else if (count == 0) {
2460 LOGI_GEOFENCE("List is empty");
2461 /* Send ZERO data gvariant*/
2462 *errorCode = GEOFENCE_SERVER_ERROR_NONE;
2463 *fenceCnt = fence_cnt;
2464 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2465 return g_variant_builder_end(&b);
2468 /* Initialize for the container*/
2469 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2471 list = g_list_first(fence_list);
2473 item = (geofence_info_s *) list->data;
2475 if (item && ((item->access_type == ACCESS_TYPE_PUBLIC) || !(g_strcmp0(app_id, item->app_id)))) {
2477 g_variant_builder_open(&b, G_VARIANT_TYPE("a{sv}"));
2479 /* Add parameters to dictionary*/
2480 g_variant_builder_add(&b, "{sv}", "fence_id", g_variant_new_int32(item->fence_id));
2482 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);
2484 switch (item->param.type) {
2485 case GEOFENCE_TYPE_GEOPOINT: {
2486 g_variant_builder_add(&b, "{sv}", "place_id", g_variant_new_int32(item->param.place_id));
2487 g_variant_builder_add(&b, "{sv}", "geofence_type", g_variant_new_int32(item->param.type));
2488 g_variant_builder_add(&b, "{sv}", "latitude", g_variant_new_double(item->param.latitude));
2489 g_variant_builder_add(&b, "{sv}", "longitude", g_variant_new_double(item->param.longitude));
2490 g_variant_builder_add(&b, "{sv}", "radius", g_variant_new_int32(item->param.radius));
2491 g_variant_builder_add(&b, "{sv}", "address", g_variant_new_string(item->param.address));
2492 g_variant_builder_add(&b, "{sv}", "bssid", g_variant_new_string("NA"));
2493 g_variant_builder_add(&b, "{sv}", "ssid", g_variant_new_string("NA"));
2496 case GEOFENCE_TYPE_WIFI:
2497 case GEOFENCE_TYPE_BT: {
2498 g_variant_builder_add(&b, "{sv}", "place_id", g_variant_new_int32(item->param.place_id));
2499 g_variant_builder_add(&b, "{sv}", "geofence_type", g_variant_new_int32(item->param.type));
2500 g_variant_builder_add(&b, "{sv}", "latitude", g_variant_new_double(0.0));
2501 g_variant_builder_add(&b, "{sv}", "longitude", g_variant_new_double(0.0));
2502 g_variant_builder_add(&b, "{sv}", "radius", g_variant_new_int32(0.0));
2503 g_variant_builder_add(&b, "{sv}", "address", g_variant_new_string("NA"));
2504 g_variant_builder_add(&b, "{sv}", "bssid", g_variant_new_string(item->param.bssid));
2505 g_variant_builder_add(&b, "{sv}", "ssid", g_variant_new_string(item->param.ssid));
2509 LOGI_GEOFENCE("Unsupported type: [%d]", item->param.type);
2513 /* Close container*/
2514 g_variant_builder_close(&b);
2518 LOGI_GEOFENCE("This fence id: %d is private. Not authorized to access by this app", item->fence_id);
2521 /* Move to next node*/
2522 list = g_list_next(list);
2524 *errorCode = GEOFENCE_SERVER_ERROR_NONE;
2525 *fenceCnt = fence_cnt;
2526 return g_variant_builder_end(&b);
2529 static GVariant *dbus_get_places_cb(const gchar *app_id, int *placeCnt, int *errorCode, gpointer userdata)
2533 GList *place_list = NULL, *list = NULL;
2534 int count = 0, place_cnt = 0;
2537 LOGI_GEOFENCE(">>> Enter");
2539 ret = geofence_manager_get_place_list_from_db(&count, &place_list);
2541 LOGI_GEOFENCE("count = %d", count);
2543 if (ret != FENCE_ERR_NONE) {
2544 LOGI_GEOFENCE("get list failed");
2545 /* Send ZERO data gvariant*/
2546 *errorCode = GEOFENCE_SERVER_ERROR_DATABASE;
2547 *placeCnt = place_cnt;
2548 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2549 return g_variant_builder_end(&b);
2550 } else if (count == 0) {
2551 LOGI_GEOFENCE("List is empty");
2552 /* Send ZERO data gvariant*/
2553 *errorCode = GEOFENCE_SERVER_ERROR_NONE;
2554 *placeCnt = place_cnt;
2555 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2556 return g_variant_builder_end(&b);
2559 /* Initialize for the container*/
2560 g_variant_builder_init(&b, G_VARIANT_TYPE("aa{sv}"));
2562 list = g_list_first(place_list);
2564 item = (place_info_s *) list->data;
2566 if (item && ((item->access_type == ACCESS_TYPE_PUBLIC) || !(g_strcmp0(app_id, item->appid)))) {
2568 g_variant_builder_open(&b, G_VARIANT_TYPE("a{sv}"));
2570 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);
2571 /* Add data to dictionary*/
2572 g_variant_builder_add(&b, "{sv}", "place_id", g_variant_new_int32(item->place_id));
2573 g_variant_builder_add(&b, "{sv}", "place_name", g_variant_new_string(item->place_name));
2574 /* Close container*/
2575 g_variant_builder_close(&b);
2579 LOGI_GEOFENCE("This place id: %d is private. Not authorized to access by this app", item->place_id);
2581 list = g_list_next(list);
2583 *errorCode = GEOFENCE_SERVER_ERROR_NONE;
2584 *placeCnt = place_cnt;
2585 return g_variant_builder_end(&b);
2588 static void __add_default_place(char *place_name)
2591 place_info_s *place_info = (place_info_s *) g_malloc0(sizeof(place_info_s));
2592 g_return_if_fail(place_info);
2594 place_info->access_type = ACCESS_TYPE_PUBLIC;
2595 g_strlcpy(place_info->place_name, place_name, PLACE_NAME_LEN);
2596 g_strlcpy(place_info->appid, "dummy_app_id", APP_ID_LEN);
2597 /*Add the place details to db*/
2598 int ret = geofence_manager_set_place_info(place_info, &place_id);
2599 if (ret != FENCE_ERR_NONE)
2600 LOGI_GEOFENCE("Unable to add the default places due to DB error");
2603 static void __init_geofencemanager(GeofenceServer *geofence_server)
2605 FUNC_ENTRANCE_SERVER;
2607 geofence_server->loc_gps_started_by_wps = false;
2608 geofence_server->loc_gps_started = false;
2609 geofence_server->loc_wps_started = false;
2610 geofence_server->connectedTrackingWifiFenceId = -1;
2611 geofence_server->gps_fix_info = NULL;
2612 geofence_server->wps_fix_info = NULL;
2613 geofence_server->gps_trigger_interval = 1; /* 1 sec by default*/
2614 geofence_server->timer_id = -1;
2615 geofence_server->gps_alarm_id = -1;
2616 geofence_server->wps_alarm_id = -1;
2617 geofence_server->gps_timeout_alarm_id = -1;
2618 geofence_server->wps_timeout_alarm_id = -1;
2619 geofence_server->geofence_list = NULL;
2620 geofence_server->tracking_list = NULL;
2621 geofence_server->running_geopoint_cnt = 0;
2622 geofence_server->running_bt_cnt = 0;
2623 geofence_server->running_wifi_cnt = 0;
2624 geofence_server->activity_type = ACTIVITY_IN_VEHICLE;
2625 geofence_server->activity_timestamp = 0;
2627 geofence_server->activity_stationary_h = NULL;
2628 geofence_server->activity_walk_h = NULL;
2629 geofence_server->activity_run_h = NULL;
2630 geofence_server->activity_in_vehicle_h = NULL;
2632 /*Initializing the DB to store the fence informations*/
2633 if (geofence_manager_db_init() != FENCE_ERR_NONE) {
2634 LOGI_GEOFENCE("Error initalizing the DB");
2636 /*Adding default places in the DB*/
2637 int place_id = DEFAULT_PLACE_HOME;
2640 while (place_id <= DEFAULT_PLACE_CAR) {
2641 if (geofence_manager_get_place_count_by_placeid(place_id, &count) == FENCE_ERR_NONE) {
2643 if (place_id == DEFAULT_PLACE_HOME) {
2644 __add_default_place("Home");
2645 } else if (place_id == DEFAULT_PLACE_OFFICE) {
2646 __add_default_place("Office");
2647 } else if (place_id == DEFAULT_PLACE_CAR) {
2648 __add_default_place("Car");
2652 LOGI_GEOFENCE("Error adding the default place: %d", place_id);
2658 /*delete all fences at rebooting for a test. TODO: will be replaced by updating previous fences*/
2659 /*geofence_manager_db_reset();*/
2664 static void __start_gps_geofence_client(void *handle, GeofenceModCB geofence_cb, void *userdata)
2666 FUNC_ENTRANCE_SERVER;
2667 /*Previously code to start the HW geofence client was there. It has been removed now*/
2671 static void __stop_gps_geofence_client(void *handle)
2673 FUNC_ENTRANCE_SERVER;
2674 /*Stop tracking the geofences*/
2679 int __copy_geofence_to_item_data(int fence_id, GeofenceItemData *item_data)
2681 FUNC_ENTRANCE_SERVER;
2682 g_return_val_if_fail(item_data, FENCE_ERR_INVALID_PARAMETER);
2683 char *app_id = NULL;
2685 item_data->common_info.fence_id = fence_id;
2686 item_data->common_info.status = GEOFENCE_FENCE_STATE_UNCERTAIN;
2687 if (FENCE_ERR_NONE != geofence_manager_get_geofence_type(fence_id, &item_data->common_info.type))
2688 return FENCE_ERR_SQLITE_FAIL;
2690 if (FENCE_ERR_NONE != geofence_manager_get_access_type(fence_id, -1, &item_data->common_info.access_type))
2691 return FENCE_ERR_SQLITE_FAIL;
2693 if (FENCE_ERR_NONE != geofence_manager_get_enable_status(fence_id, &item_data->common_info.enable))
2694 return FENCE_ERR_SQLITE_FAIL;
2696 if (FENCE_ERR_NONE != geofence_manager_get_appid_from_geofence(fence_id, &app_id)) {
2698 return FENCE_ERR_SQLITE_FAIL;
2700 g_strlcpy(item_data->common_info.appid, app_id, APP_ID_LEN);
2703 if (FENCE_ERR_NONE != geofence_manager_get_placeid_from_geofence(fence_id, &item_data->common_info.place_id))
2704 return FENCE_ERR_SQLITE_FAIL;
2706 if (FENCE_ERR_NONE != geofence_manager_get_running_status(fence_id, &item_data->common_info.running_status))
2707 return FENCE_ERR_SQLITE_FAIL;
2709 if (item_data->common_info.type == GEOFENCE_TYPE_GEOPOINT) {
2710 geocoordinate_info_s *geocoordinate_info = NULL;
2711 int ret = FENCE_ERR_NONE;
2713 ret = geofence_manager_get_geocoordinate_info(fence_id, &geocoordinate_info);
2714 if (ret != FENCE_ERR_NONE || geocoordinate_info == NULL) {
2715 LOGI_GEOFENCE("can not get geocoordinate_info");
2716 return FENCE_ERR_SQLITE_FAIL;
2718 item_data->priv = (void *) geocoordinate_info;
2720 bssid_info_s *bssid_info = NULL;
2721 int ret = FENCE_ERR_NONE;
2723 ret = geofence_manager_get_bssid_info(fence_id, &bssid_info);
2724 if (ret != FENCE_ERR_NONE || bssid_info == NULL) {
2725 LOGI_GEOFENCE("can not get bssid_info");
2726 return FENCE_ERR_SQLITE_FAIL;
2728 item_data->priv = (void *) bssid_info;
2730 return FENCE_ERR_NONE;
2733 static void __add_left_fences(gpointer user_data)
2735 g_return_if_fail(user_data);
2736 GeofenceServer *geofence_server = (GeofenceServer *) user_data;
2737 if (geofence_server->geofence_list != NULL)
2739 GList *fence_list = NULL;
2743 /*Get the number of fences count*/
2744 geofence_manager_get_count_of_fences(&count);
2747 /*Fetch the fences from the DB and populate it in the list*/
2748 geofence_manager_get_fences(NULL, 0, &fence_list);
2749 fence_list = g_list_first(fence_list);
2750 while (fence_list) {
2751 fence_id = GPOINTER_TO_INT(fence_list->data);
2753 /*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])
2755 * geofence_manager_set_running_status(fence_id, 0); // resetting the running-status flag since it is a device reboot
2758 GeofenceItemData *item_data = (GeofenceItemData *)g_malloc0(sizeof(GeofenceItemData));
2759 if (FENCE_ERR_NONE != __copy_geofence_to_item_data(fence_id, item_data)) {
2763 LOGI_GEOFENCE("adding fence_id = %d to fence_list", item_data->common_info.fence_id);
2765 /*Here fences from DB will be added to the list but tracking list is not populated here*/
2766 geofence_server->geofence_list = g_list_append(geofence_server->geofence_list, item_data);
2768 fence_list = g_list_next(fence_list);
2772 static void _glib_log(const gchar *log_domain, GLogLevelFlags log_level, const gchar *msg, gpointer user_data)
2774 LOGI_GEOFENCE("GLIB[%d] : %s", log_level, msg);
2777 int main(int argc, char **argv)
2779 GeofenceServer *geofenceserver = NULL;
2780 LOGI_GEOFENCE("----------------Starting Server -----------------------------");
2781 /*Callback registrations*/
2782 geofence_callbacks cb;
2783 cb.bt_conn_state_changed_cb = bt_conn_state_changed;
2784 cb.bt_apater_disable_cb = bt_adp_disable;
2785 cb.wifi_conn_state_changed_cb = wifi_conn_state_changed;
2786 cb.wifi_device_state_changed_cb = wifi_device_state_changed;
2787 cb.network_evt_cb = geofence_network_evt_cb;
2788 cb.bt_discovery_cb = bt_adapter_device_discovery_state_cb;
2789 cb.gps_setting_changed_cb = gps_setting_changed_cb;
2790 cb.wifi_rssi_level_changed_cb = wifi_rssi_level_changed;
2791 #if !GLIB_CHECK_VERSION(2, 35, 0)
2794 geofenceserver = g_new0(GeofenceServer, 1);
2795 if (!geofenceserver) {
2796 LOGI_GEOFENCE("GeofenceServer create fail");
2800 if (alarmmgr_init(PACKAGE_NAME) != ALARMMGR_RESULT_SUCCESS) {
2801 LOGI_GEOFENCE("alarmmgr_init fail");
2805 g_log_set_default_handler(_glib_log, geofenceserver);
2807 /*Initialize the geofence manager where DB related activities exists*/
2808 __init_geofencemanager(geofenceserver);
2810 /*This will read the DB and populate the list*/
2811 __add_left_fences(geofenceserver);
2813 /*This call goes and registers the cb with Server.c where all the wifi,bt event callbacks are triggered*/
2814 _geofence_register_update_callbacks(&cb, geofenceserver);
2816 /*This call goes and make initializations of bt and wifi stacks and then register the callbacks with them*/
2817 _geofence_initialize_geofence_server(geofenceserver);
2819 #ifdef TIZEN_ENGINEER_MODE
2823 /* This call goes to Geofence_dbus_server.c and creates the actual server dbus connection who will interact with the client*/
2824 geofence_dbus_callback_s *dbus_callback;
2825 dbus_callback = g_new0(geofence_dbus_callback_s, 1);
2826 g_return_val_if_fail(dbus_callback, GEOFENCE_DBUS_SERVER_ERROR_MEMORY);
2828 dbus_callback->add_geofence_cb = dbus_add_fence_cb;
2829 dbus_callback->delete_geofence_cb = dbus_remove_fence_cb;
2830 dbus_callback->get_geofences_cb = dbus_get_geofences_cb;
2831 dbus_callback->enable_geofence_cb = dbus_enable_geofence_cb;
2832 dbus_callback->start_geofence_cb = dbus_start_geofence_cb;
2833 dbus_callback->stop_geofence_cb = dbus_stop_geofence_cb;
2834 dbus_callback->add_place_cb = dbus_add_place_cb;
2835 dbus_callback->update_place_cb = dbus_update_place_cb;
2836 dbus_callback->delete_place_cb = dbus_remove_place_cb;
2837 dbus_callback->get_place_name_cb = dbus_get_place_name_cb;
2838 dbus_callback->get_places_cb = dbus_get_places_cb;
2840 geofence_dbus_server_create(&(geofenceserver->geofence_dbus_server), dbus_callback, (void *) geofenceserver);
2842 LOGD_GEOFENCE("lbs_geofence_server_creation done");
2844 geofenceserver->loop = g_main_loop_new(NULL, TRUE);
2845 g_main_loop_run(geofenceserver->loop);
2847 LOGD_GEOFENCE("GEOFENCE_manager deamon Stop....");
2849 /*This call goes to server.c and deregisters all the callbacks w.r.t bt and wifi*/
2850 _geofence_deinitialize_geofence_server();
2852 #ifdef TIZEN_ENGINEER_MODE
2855 /*This call goes to Geofence_dbus_server.c and deletes the memory allocated to the server, hence destroys it*/
2856 geofence_dbus_server_destroy(geofenceserver->geofence_dbus_server);
2857 LOGD_GEOFENCE("lbs_server_destroy called");
2859 g_free(dbus_callback);
2860 g_main_loop_unref(geofenceserver->loop);
2861 g_free(geofenceserver);
2863 /*Closing the DB and the handle is aquired again when geofence server comes up.*/
2864 geofence_manager_close_db();