130f55cc952d6cab0f43b8b67501a1258b17ea26
[platform/core/location/geofence-server.git] / geofence-server / src / geofence_server_bluetooth.c
1 /* Copyright 2014 Samsung Electronics Co., Ltd All Rights Reserved
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <stdlib.h>
17 #include <glib.h>
18
19 #include "geofence_server.h"
20 #include "server.h"
21 #include "debug_util.h"
22 #include "geofence_server_log.h"
23 #include "geofence_server_private.h"
24 #include "geofence_server_internal.h"
25 #include "geofence_server_db.h"
26
27 static int connectedFence = -1;
28 static gboolean __geofence_check_fence_status(int fence_status, GeofenceItemData *item_data)
29 {
30         FUNC_ENTRANCE_SERVER
31         gboolean ret = FALSE;
32
33         if (fence_status != item_data->common_info.status) {
34                 LOGD_GEOFENCE("Fence status changed. %d -> %d", item_data->common_info.status, fence_status);
35                 ret = TRUE;
36                 item_data->common_info.status = fence_status;   /*update status*/
37         }
38         return ret;
39 }
40
41 static bool __check_for_match(char *str1, char *str2)
42 {
43         if (g_strrstr(str1, str2) == NULL)
44                 return false;
45         return true;
46 }
47
48 static void bt_le_scan_result_cb(int result, bt_adapter_le_device_scan_result_info_s *info, void *user_data)
49 {
50         int ret = BT_ERROR_NONE;
51         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
52         LOGI_GEOFENCE("Current addresses: %s", geofence_server->ble_info);
53
54         if (info == NULL) {
55                 LOGI_GEOFENCE("Stopping scan as there is no BLE address found");
56                 ret = bt_adapter_le_stop_scan();
57                 if (ret != BT_ERROR_NONE)
58                         LOGE_GEOFENCE("Unable to stop the BLE scan, error: %d", ret);
59                 return;
60         }
61         LOGI_GEOFENCE("Received address: %s", info->remote_address);
62
63         if (!g_ascii_strcasecmp(geofence_server->ble_info, "")) {
64                 g_stpcpy(geofence_server->ble_info, info->remote_address);
65         } else if (!__check_for_match(geofence_server->ble_info, info->remote_address)) { /* If duplicate does not exist */
66                 char *p = g_strjoin(";", geofence_server->ble_info, info->remote_address, NULL);
67                 g_stpcpy(geofence_server->ble_info, p);
68                 g_free(p);
69         } else {
70                 LOGI_GEOFENCE("Stopping scan. Address: %s already exist in the string %s", info->remote_address, geofence_server->ble_info);
71                 ret = bt_adapter_le_stop_scan();
72                 if (ret != BT_ERROR_NONE)
73                         LOGE_GEOFENCE("Unable to stop the BLE scan, error: %d", ret);
74                 /* Add the string to the database. */
75                 if (connectedFence != -1)
76                         geofence_manager_set_ble_info_to_geofence(connectedFence, geofence_server->ble_info);
77         }
78 }
79
80 static void emit_bt_geofence_proximity_changed(GeofenceServer *geofence_server, int fence_id, int fence_proximity_status)
81 {
82         FUNC_ENTRANCE_SERVER
83         LOGD_GEOFENCE("emit_bt_geofence_proximity_changed");
84         char *app_id = NULL;
85         int ret = FENCE_ERR_NONE;
86
87         ret = geofence_manager_get_appid_from_geofence(fence_id, &app_id);
88         if (ret != FENCE_ERR_NONE) {
89                 LOGE("Error getting the app_id for fence id[%d]", fence_id);
90                 return;
91         }
92         GeofenceItemData *item_data = __get_item_by_fence_id(fence_id, geofence_server);
93         if (item_data == NULL) {
94                 LOGD_GEOFENCE("getting item data failed. fence_id [%d]", fence_id);
95                 g_free(app_id);
96                 return;
97         }
98
99         if (fence_proximity_status != item_data->common_info.proximity_status) {
100                 geofence_dbus_server_send_geofence_proximity_changed(geofence_server->geofence_dbus_server, app_id, fence_id, item_data->common_info.access_type, fence_proximity_status, GEOFENCE_PROXIMITY_PROVIDER_BLUETOOTH);
101                 if (fence_proximity_status == GEOFENCE_PROXIMITY_NEAR) {
102                         LOGD_GEOFENCE("BT Fence. Scanning for BLE and storing in DB");
103                         g_stpcpy(geofence_server->ble_info, "");
104                         ret = bt_adapter_le_start_scan(bt_le_scan_result_cb, geofence_server);
105                         if (ret != BT_ERROR_NONE) {
106                                 LOGE_GEOFENCE("Fail to start ble scan. %d", ret);
107                         }
108                 }
109                 item_data->common_info.proximity_status = fence_proximity_status;
110         }
111
112         if (app_id)
113                 free(app_id);
114 }
115
116
117 static void emit_bt_geofence_inout_changed(GeofenceServer *geofence_server, GeofenceItemData *item_data, int fence_status)
118 {
119         FUNC_ENTRANCE_SERVER
120         char *app_id = (char *)g_malloc0(sizeof(char) * APP_ID_LEN);
121         g_strlcpy(app_id, item_data->common_info.appid, APP_ID_LEN);
122         if (app_id == NULL) {
123                 LOGD_GEOFENCE("get app_id failed. fence_id [%d]", item_data->common_info.fence_id);
124                 return;
125         }
126
127         if (fence_status == GEOFENCE_FENCE_STATE_IN) {
128                 geofence_dbus_server_send_geofence_inout_changed(geofence_server->geofence_dbus_server, app_id, item_data->common_info.fence_id, item_data->common_info.access_type, GEOFENCE_EMIT_STATE_IN);
129         } else if (fence_status == GEOFENCE_FENCE_STATE_OUT) {
130                 geofence_dbus_server_send_geofence_inout_changed(geofence_server->geofence_dbus_server, app_id, item_data->common_info.fence_id, item_data->common_info.access_type, GEOFENCE_EMIT_STATE_OUT);
131         }
132
133         if (item_data->client_status == GEOFENCE_CLIENT_STATUS_START) {
134                 item_data->client_status = GEOFENCE_CLIENT_STATUS_RUNNING;
135         }
136         if (app_id)
137                 free(app_id);
138 }
139
140 static void __geofence_check_bt_fence_type(gboolean connected, const char *bssid, void *data)
141 {
142         FUNC_ENTRANCE_SERVER
143         GeofenceServer *geofence_server = (GeofenceServer *)data;
144         g_return_if_fail(geofence_server);
145         g_return_if_fail(bssid);
146
147         int fence_id = 0;
148         geofence_type_e fence_type;
149         GeofenceItemData *item_data = NULL;
150         bssid_info_s *bt_info_from_db = NULL;
151         bssid_info_s *bt_info_from_list = NULL;
152
153         GList *fence_list = g_list_first(geofence_server->tracking_list);
154
155         for (; fence_list != NULL; fence_list = g_list_next(fence_list)) {
156                 int ret = FENCE_ERR_NONE;
157                 item_data = NULL;
158                 fence_id = GPOINTER_TO_INT(fence_list->data);
159                 item_data = __get_item_by_fence_id(fence_id, geofence_server);
160
161                 if (item_data == NULL)
162                         continue;
163
164                 fence_type = item_data->common_info.type;
165
166                 if (fence_type != GEOFENCE_TYPE_BT)
167                         continue;
168
169                 bt_info_from_list = (bssid_info_s *) item_data->priv;
170
171                 if (bt_info_from_list == NULL || bt_info_from_list->enabled == FALSE)
172                         continue;
173
174                 ret = geofence_manager_get_bssid_info(fence_id, &bt_info_from_db);
175
176                 if (bt_info_from_db == NULL) {
177                         LOGD_GEOFENCE("Failed to get bt_info. Fence Id[%d], Error[%d]", fence_id, ret);
178                         continue;
179                 }
180                 LOGD_GEOFENCE("bt_info->bssid [%s]", bt_info_from_db->bssid);
181
182                 if (!g_ascii_strcasecmp(bt_info_from_db->bssid, bssid) || !g_ascii_strcasecmp(g_strdelimit(bt_info_from_db->bssid, "-", ':'), bssid)) {
183                         if (connected) {        /* connected => FENCE_IN*/
184                                 if (__geofence_check_fence_status(GEOFENCE_FENCE_STATE_IN, item_data) == TRUE) {
185                                         LOGD_GEOFENCE("Emitted to fence_id [%d] GEOFENCE_FENCE_STATE_IN", fence_id);
186                                         connectedFence = fence_id;
187                                         emit_bt_geofence_inout_changed(geofence_server, item_data, GEOFENCE_FENCE_STATE_IN);
188                                         emit_bt_geofence_proximity_changed(geofence_server, fence_id, GEOFENCE_PROXIMITY_NEAR);
189                                 }
190                         } else {        /* disconnected => FENCE_OUT*/
191                                 if (__geofence_check_fence_status(GEOFENCE_FENCE_STATE_OUT, item_data) == TRUE) {
192                                         LOGD_GEOFENCE("Emitted to fence_id [%d] GEOFENCE_FENCE_STATE_OUT", fence_id);
193                                         connectedFence = -1;
194                                         emit_bt_geofence_inout_changed(geofence_server, item_data, GEOFENCE_FENCE_STATE_OUT);
195                                         emit_bt_geofence_proximity_changed(geofence_server, fence_id, GEOFENCE_PROXIMITY_UNCERTAIN);
196                                 }
197                         }
198                 }
199                 g_slice_free(bssid_info_s, bt_info_from_db);
200                 bt_info_from_db = NULL;
201                 bt_info_from_list = NULL;
202         }
203         LOGD_GEOFENCE("exit");
204 }
205
206 void bt_conn_state_changed(gboolean connected, bt_device_connection_info_s *conn_info, void *user_data)
207 {
208         FUNC_ENTRANCE_SERVER
209         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
210         g_return_if_fail(geofence_server);
211         g_return_if_fail(conn_info);
212
213         if (connected == true) {
214                 if (conn_info->remote_address == NULL) {
215                         LOGD_GEOFENCE("Bluetooth device connected, but remote_address not exist.");
216                 } else {
217                         LOGD_GEOFENCE("Bluetooth device connected [%s].", conn_info->remote_address);
218                         __geofence_check_bt_fence_type(connected, conn_info->remote_address, user_data);
219                 }
220         } else {
221                 LOGD_GEOFENCE("Bluetooth device disconnected [%s]. reason [%d]", conn_info->remote_address, conn_info->disconn_reason);
222                 __geofence_check_bt_fence_type(connected, conn_info->remote_address, user_data);
223         }
224         LOGD_GEOFENCE("exit");
225 }
226
227 void bt_adp_disable(gboolean connected, void *user_data)
228 {
229         FUNC_ENTRANCE_SERVER
230         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
231         g_return_if_fail(geofence_server);
232
233         int fence_id = 0;
234
235         geofence_type_e fence_type;
236         GeofenceItemData *item_data = NULL;
237
238         GList *fence_list = g_list_first(geofence_server->tracking_list);
239
240         for (; fence_list != NULL; fence_list = g_list_next(fence_list)) {
241                 fence_id = GPOINTER_TO_INT(fence_list->data);
242                 item_data = NULL;
243                 item_data = __get_item_by_fence_id(fence_id, geofence_server);
244
245                 if (item_data == NULL)
246                         continue;
247
248                 fence_type = item_data->common_info.type;
249
250                 if (fence_type != GEOFENCE_TYPE_BT)     /* check only bluetooth type*/
251                         continue;
252
253                 if (connected == FALSE) {
254                         if (__geofence_check_fence_status(GEOFENCE_FENCE_STATE_OUT, item_data) == TRUE) {
255                                 LOGD_GEOFENCE("Emitted to fence_id [%d] GEOFENCE_FENCE_STATE_OUT", fence_id);
256                                 emit_bt_geofence_inout_changed(geofence_server, item_data, GEOFENCE_FENCE_STATE_OUT);
257                                 emit_bt_geofence_proximity_changed(geofence_server, fence_id, GEOFENCE_PROXIMITY_UNCERTAIN);
258                         }
259                 }
260         }
261
262         LOGD_GEOFENCE("exit");
263 }