1.apply WPS for geofence
[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 gboolean __geofence_check_fence_status(int fence_status, GeofenceItemData *item_data)
28 {
29         FUNC_ENTRANCE_SERVER
30         gboolean ret = FALSE;
31
32         if (fence_status != item_data->common_info.status) {
33                 LOGD_GEOFENCE("Fence status changed. %d -> %d", item_data->common_info.status, fence_status);
34                 ret = TRUE;
35                 item_data->common_info.status = fence_status;   /*update status*/
36         }
37         return ret;
38 }
39
40 static void emit_bt_geofence_proximity_changed(GeofenceServer *geofence_server, int fence_id, int fence_proximity_status)
41 {
42         FUNC_ENTRANCE_SERVER
43         LOGD_GEOFENCE("emit_bt_geofence_proximity_changed");
44         char *app_id = NULL;
45         int ret = FENCE_ERR_NONE;
46
47         ret = geofence_manager_get_appid_from_geofence(fence_id, &app_id);
48         if (ret != FENCE_ERR_NONE) {
49                 LOGE("Error getting the app_id for fence id[%d]", fence_id);
50                 return;
51         }
52         GeofenceItemData *item_data = __get_item_by_fence_id(fence_id, geofence_server);
53         if (item_data == NULL) {
54                 LOGD_GEOFENCE("getting item data failed. fence_id [%d]", fence_id);
55                 g_free(app_id);
56                 return;
57         }
58
59         if (fence_proximity_status != item_data->common_info.proximity_status) {
60                 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);
61                 item_data->common_info.proximity_status = fence_proximity_status;
62         }
63
64         if (app_id)
65                 free(app_id);
66 }
67
68
69 static void emit_bt_geofence_inout_changed(GeofenceServer *geofence_server, GeofenceItemData *item_data, int fence_status)
70 {
71         FUNC_ENTRANCE_SERVER
72         char *app_id = (char *)g_malloc0(sizeof(char) * APP_ID_LEN);
73         g_strlcpy(app_id, item_data->common_info.appid, APP_ID_LEN);
74         if (app_id == NULL) {
75                 LOGD_GEOFENCE("get app_id failed. fence_id [%d]", item_data->common_info.fence_id);
76                 return;
77         }
78
79         if (fence_status == GEOFENCE_FENCE_STATE_IN) {
80                 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);
81         } else if (fence_status == GEOFENCE_FENCE_STATE_OUT) {
82                 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);
83         }
84
85         if (item_data->client_status == GEOFENCE_CLIENT_STATUS_START) {
86                 item_data->client_status = GEOFENCE_CLIENT_STATUS_RUNNING;
87         }
88         if (app_id)
89                 free(app_id);
90 }
91
92 static void __geofence_check_bt_fence_type(gboolean connected, const char *bssid, void *data)
93 {
94         FUNC_ENTRANCE_SERVER
95         GeofenceServer *geofence_server = (GeofenceServer *)data;
96         g_return_if_fail(geofence_server);
97         g_return_if_fail(bssid);
98
99         int fence_id = 0;
100         geofence_type_e fence_type;
101         GeofenceItemData *item_data = NULL;
102         bssid_info_s *bt_info_from_db = NULL;
103         bssid_info_s *bt_info_from_list = NULL;
104
105         GList *fence_list = g_list_first(geofence_server->tracking_list);
106
107         for (; fence_list != NULL; fence_list = g_list_next(fence_list)) {
108                 int ret = FENCE_ERR_NONE;
109                 item_data = NULL;
110                 fence_id = GPOINTER_TO_INT(fence_list->data);
111                 item_data = __get_item_by_fence_id(fence_id, geofence_server);
112
113                 if (item_data == NULL)
114                         continue;
115
116                 fence_type = item_data->common_info.type;
117
118                 if (fence_type != GEOFENCE_TYPE_BT)
119                         continue;
120
121                 bt_info_from_list = (bssid_info_s *) item_data->priv;
122
123                 if (bt_info_from_list == NULL || bt_info_from_list->enabled == FALSE)
124                         continue;
125
126                 ret = geofence_manager_get_bssid_info(fence_id, &bt_info_from_db);
127
128                 if (bt_info_from_db == NULL) {
129                         LOGD_GEOFENCE("Failed to get bt_info. Fence Id[%d], Error[%d]", fence_id, ret);
130                         continue;
131                 }
132                 LOGD_GEOFENCE("bt_info->bssid [%s]", bt_info_from_db->bssid);
133
134                 if (!g_ascii_strcasecmp(bt_info_from_db->bssid, bssid) || !g_ascii_strcasecmp(g_strdelimit(bt_info_from_db->bssid, "-", ':'), bssid)) {
135                         if (connected) {        /* connected => FENCE_IN*/
136                                 if (__geofence_check_fence_status(GEOFENCE_FENCE_STATE_IN, item_data) == TRUE) {
137                                         LOGD_GEOFENCE("Emitted to fence_id [%d] GEOFENCE_FENCE_STATE_IN", fence_id);
138                                         emit_bt_geofence_inout_changed(geofence_server, item_data, GEOFENCE_FENCE_STATE_IN);
139                                         emit_bt_geofence_proximity_changed(geofence_server, fence_id, GEOFENCE_PROXIMITY_NEAR);
140                                 }
141                         } else {        /* disconnected => FENCE_OUT*/
142                                 if (__geofence_check_fence_status(GEOFENCE_FENCE_STATE_OUT, item_data) == TRUE) {
143                                         LOGD_GEOFENCE("Emitted to fence_id [%d] GEOFENCE_FENCE_STATE_OUT", fence_id);
144                                         emit_bt_geofence_inout_changed(geofence_server, item_data, GEOFENCE_FENCE_STATE_OUT);
145                                         emit_bt_geofence_proximity_changed(geofence_server, fence_id, GEOFENCE_PROXIMITY_FAR);
146                                 }
147                         }
148                 }
149                 g_slice_free(bssid_info_s, bt_info_from_db);
150                 bt_info_from_db = NULL;
151                 bt_info_from_list = NULL;
152         }
153
154         LOGD_GEOFENCE("exit");
155 }
156
157 void bt_conn_state_changed(gboolean connected, bt_device_connection_info_s *conn_info, void *user_data)
158 {
159         FUNC_ENTRANCE_SERVER
160         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
161         g_return_if_fail(geofence_server);
162         g_return_if_fail(conn_info);
163
164         if (connected == true) {
165                 if (conn_info->remote_address == NULL) {
166                         LOGD_GEOFENCE("Bluetooth device connected, but remote_address not exist.");
167                 } else {
168                         LOGD_GEOFENCE("Bluetooth device connected [%s].", conn_info->remote_address);
169                         __geofence_check_bt_fence_type(connected, conn_info->remote_address, user_data);
170                 }
171         } else {
172                 LOGD_GEOFENCE("Bluetooth device disconnected [%s]. reason [%d]", conn_info->remote_address, conn_info->disconn_reason);
173                 __geofence_check_bt_fence_type(connected, conn_info->remote_address, user_data);
174         }
175         LOGD_GEOFENCE("exit");
176 }
177
178 void bt_adp_disable(gboolean connected, void *user_data)
179 {
180         FUNC_ENTRANCE_SERVER
181         GeofenceServer *geofence_server = (GeofenceServer *) user_data;
182         g_return_if_fail(geofence_server);
183
184         int fence_id = 0;
185
186         geofence_type_e fence_type;
187         GeofenceItemData *item_data = NULL;
188
189         GList *fence_list = g_list_first(geofence_server->tracking_list);
190
191         for (; fence_list != NULL; fence_list = g_list_next(fence_list)) {
192                 fence_id = GPOINTER_TO_INT(fence_list->data);
193                 item_data = NULL;
194                 item_data = __get_item_by_fence_id(fence_id, geofence_server);
195
196                 if (item_data == NULL)
197                         continue;
198
199                 fence_type = item_data->common_info.type;
200
201                 if (fence_type != GEOFENCE_TYPE_BT)     /* check only bluetooth type*/
202                         continue;
203
204                 if (connected == FALSE) {
205                         if (__geofence_check_fence_status(GEOFENCE_FENCE_STATE_OUT, item_data) == TRUE) {
206                                 LOGD_GEOFENCE("Emitted to fence_id [%d] GEOFENCE_FENCE_STATE_OUT", fence_id);
207                                 emit_bt_geofence_inout_changed(geofence_server, item_data, GEOFENCE_FENCE_STATE_OUT);
208                         }
209                 }
210         }
211
212         LOGD_GEOFENCE("exit");
213 }