Not retry bonding even though bond fails
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / device / bt-service-core-device.c
1 /*
2  * Copyright (c) 2015 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Anupam Roy <anupam.r@samsung.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *              http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <gio/gio.h>
22 #include <glib.h>
23 #include <dlog.h>
24 #include <string.h>
25 #include <vconf.h>
26 #include <vconf-internal-keys.h>
27 #include <bundle.h>
28 #include <bundle_internal.h>
29 #include <eventsystem.h>
30
31 /*bt-service headers */
32 #include "bt-internal-types.h"
33 #include "bt-service-common.h"
34 #include "bt-service-util.h"
35 #include "bt-service-main.h"
36 #include "bt-service-core-device.h"
37 #include "bt-service-core-adapter.h"
38 #include "bt-service-event-receiver.h"
39 #include "bt-request-handler.h"
40 #include "bt-service-event.h"
41 #include "bt-service-agent-util.h"
42 #include "bt-service-a2dp-src.h"
43 #include "bt-service-a2dp-sink.h"
44 #ifdef TIZEN_FEATURE_BT_OBEX
45 #include "bt-service-obex-server.h"
46 #endif
47 #include "bt-service-device-internal.h"
48
49 #ifdef TIZEN_GATT_CLIENT
50 #include "bt-service-gatt.h"
51 #endif
52
53 /* OAL headers */
54 #include <oal-event.h>
55 #include <oal-manager.h>
56 #include <oal-adapter-mgr.h>
57 #include <oal-device-mgr.h>
58
59 #if !defined(TIZEN_PROFILE_WEARABLE) && !defined(TIZEN_PROFILE_IOT)
60 #define MAX_BOND_RETRY_COUNT 3
61 #endif
62 #define BT_PASSKEY_MAX_LENGTH 4
63
64 #define BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT    6000    /* msec */
65
66 #define BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL  30      /* msec */
67 #define BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL  50      /* msec */
68 #define BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY 0       /* event */
69
70 #define BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL       10      /* msec */
71 #define BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL       30      /* msec */
72 #define BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY      0       /* event */
73
74 #define BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL         80      /* msec */
75 #define BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL         100     /* msec */
76 #define BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY        2       /* event */
77
78 /* Bonding Info structure */
79 typedef struct {
80         int result;
81         char *addr;
82         gboolean is_autopair;
83         unsigned short conn_type;
84         gboolean is_cancelled_by_user;
85         gboolean is_device_creating;
86         bluetooth_device_address_t *dev_addr;
87         bt_remote_dev_info_t *dev_info;
88 } bt_bond_data_t;
89
90 /* Searching Info structure */
91 typedef struct {
92         int result;
93         char *addr;
94         gboolean is_cancelled_by_user;
95         bluetooth_device_address_t *dev_addr;
96         bt_remote_dev_info_t *dev_info;
97 } bt_service_search_info_data_t;
98
99 /* Pairing Info structure */
100 typedef struct {
101         char *addr;
102         gboolean is_autopair;
103         int is_ssp;
104 } bt_pairing_data_t;
105
106 typedef struct {
107         char addr[BT_ADDRESS_STRING_SIZE];
108         bt_remote_dev_info_t *dev_info;
109 } bt_incoming_bond_data_t;
110
111 /* Bonding and Pairing Informations */
112 bt_bond_data_t *trigger_bond_info;
113 bt_bond_data_t *trigger_unbond_info;
114 bt_pairing_data_t *trigger_pairing_info;
115 bt_service_search_info_data_t *service_search_info;
116
117 bt_incoming_bond_data_t *incoming_bond_info;
118
119 typedef enum {
120         BT_DEVICE_BOND_STATE_NONE,
121         BT_DEVICE_BOND_STATE_CANCEL_DISCOVERY,
122         BT_DEVICE_BOND_STATE_DISCOVERY_CANCELLED,
123         BT_DEVICE_BOND_STATE_REMOVE_BONDING,
124         BT_DEVICE_BOND_STATE_REMOVED_BONDING,
125         BT_DEVICE_BOND_STATE_STARTED,
126         BT_DEVICE_BOND_STATE_WAIT_PROP,
127         BT_DEVICE_BOND_STATE_WAIT_DID
128 } bt_bond_state_e;
129
130 typedef enum {
131         BT_DEVICE_BOND_INFO,
132         BT_DEVICE_INCOMING_BOND_INFO,
133         BT_DEVICE_UNBOND_INFO
134 } bt_bond_info_e;
135
136 /* BT device bond state variable */
137 static bt_bond_state_e bt_device_bond_state;
138 #if !defined(TIZEN_PROFILE_WEARABLE) && !defined(TIZEN_PROFILE_IOT)
139 static int bond_retry_count;
140 #endif
141
142 static char *passkey_watcher;
143 static GSList *pin_info_list = NULL;
144
145 /** DBFW+ Event Code */
146 typedef enum {
147         BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_INFO = 0x10,
148         BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_CLOCK_INFO = 0x11,
149         BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_PAGE_SLOTS = 0x12,
150         BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_LMP_TRACE = 0x13,
151         BT_DBFW_PLUS_EVENT_CODE_A2DP_INFO = 0x20,
152         BT_DBFW_PLUS_EVENT_CODE_HFP_INFO = 0x31,
153         BT_DBFW_PLUS_EVENT_CODE_HFP_SCO_PACKET_TYPE_INFO = 0x32,
154 } bt_dbfw_plus_event_code_t;
155
156 static int dbfw_rssi;
157
158 #ifdef TIZEN_GATT_CLIENT
159 typedef struct {
160         char *address;
161         float interval_min;
162         float interval_max;
163         GSList *senders;
164 } bt_connected_le_dev_t;
165
166 typedef struct {
167         char *sender;
168         float interval_min;
169         float interval_max;
170         guint16 latency;
171         guint16 time_out;
172         float key;
173 } bt_le_conn_param_t;
174
175 static GSList *le_connected_dev_list = NULL;
176
177 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
178 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
179 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
180 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
181 #define BT_LE_CONN_SLAVE_LATENCY_MAX 499
182 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
183 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
184
185 static void _bt_handle_le_connected_dev_info(const char *address, gboolean connected);
186 #endif
187
188 /* Forward declaration */
189 static void __bt_device_event_handler(int event_type, gpointer event_data);
190 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble);
191
192
193 static int __bt_device_handle_bond_state(void);
194 static void __bt_free_bond_info(uint8_t type);
195 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info);
196 static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr);
197 static void __bt_device_handle_bond_removal_event(bt_address_t *bd_addr);
198 static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_fail_event);
199 static void __bt_handle_ongoing_bond(bt_remote_dev_info_t *remote_dev_info, gboolean incoming_bond);
200 static void __bt_device_acl_state_changed_callback(event_dev_conn_status_t *acl_event,
201                 gboolean connected, unsigned char type);
202 static void __bt_free_pairing_info(bt_pairing_data_t **p_info);
203
204 static void __bt_device_ssp_consent_callback(remote_device_t* dev_info);
205 static void __bt_device_pin_request_callback(remote_device_t* pin_req_event);
206 static void __bt_device_ssp_passkey_display_callback(event_dev_passkey_t *dev_info);
207 static void __bt_device_ssp_passkey_confirmation_callback(event_dev_passkey_t *dev_info);
208 static void __bt_device_ssp_passkey_entry_callback(remote_device_t* dev_info);
209 static void __bt_device_authorization_request_callback(event_dev_authorize_req_t* auth_event);
210
211 static void __bt_device_services_callback(event_dev_services_t* uuid_list);
212 static void __bt_handle_ongoing_device_service_search(bt_remote_dev_info_t *remote_dev_info);
213
214 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info);
215
216 static int __bt_get_device_pin_code(const char *address, char *pin_code);
217 static gboolean __bt_ignore_auto_pairing_request(const char *address);
218
219 gboolean _bt_is_device_creating(void)
220 {
221         if (!trigger_bond_info)
222                 return FALSE;
223         return trigger_bond_info->is_device_creating;
224 }
225
226 void _bt_device_state_handle_callback_set_request(void)
227 {
228         _bt_service_register_event_handler_callback(
229                         BT_DEVICE_MODULE, __bt_device_event_handler);
230 }
231
232 void __bt_device_handle_pending_requests(int result, int service_function,
233                 void *user_data, unsigned int size)
234 {
235         GSList *l;
236         GArray *out_param;
237         invocation_info_t *req_info = NULL;
238
239         BT_DBG("+");
240
241         /* Get method invocation context */
242         for (l = _bt_get_invocation_list(); l != NULL; ) {
243                 req_info = l->data;
244                 l = g_slist_next(l);
245                 if (req_info == NULL || req_info->service_function != service_function)
246                         continue;
247
248                 switch (service_function) {
249                 case BT_SEARCH_SERVICE: {
250                         char *address = (char *)user_data;
251                         if (strncmp((char*)req_info->user_data, address, BT_ADDRESS_STRING_SIZE)) {
252                                 BT_ERR("Unexpected: Info request pending for a different address!!");
253                                 return;
254                         } else {
255                                 BT_DBG("Found info request addr [%s]", (char*)req_info->user_data);
256                                 bt_sdp_info_t sdp_info;
257
258                                 memset(&sdp_info, 0x00, sizeof(bt_sdp_info_t));
259                                 _bt_convert_addr_string_to_type(sdp_info.device_addr.addr, address);
260
261                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
262                                 g_array_append_vals(out_param, &sdp_info, sizeof(bt_sdp_info_t));
263                                 _bt_service_method_return(req_info->context, out_param, result);
264
265                                 g_free(req_info->user_data);
266                                 _bt_free_info_from_invocation_list(req_info);
267                                 g_array_free(out_param, TRUE);
268                         }
269                         break;
270                 }
271                 case BT_BOND_DEVICE: {
272                         char *address = (char *)user_data;
273                         if (strncmp((char*)req_info->user_data, address, BT_ADDRESS_STRING_SIZE)) {
274                                 BT_ERR("Unexpected: Info request pending for a different address!!");
275                                 return;
276                         } else {
277                                 BT_DBG("Found info request addr [%s]", (char*)req_info->user_data);
278                                 bluetooth_device_info_t dev_info;
279                                 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
280                                 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
281                                                 address);
282                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
283                                 g_array_append_vals(out_param, &dev_info,
284                                                 sizeof(bluetooth_device_info_t));
285                                 _bt_service_method_return(req_info->context, out_param, result);
286
287                                 g_free(req_info->user_data);
288                                 _bt_free_info_from_invocation_list(req_info);
289                                 g_array_free(out_param, TRUE);
290                         }
291                         break;
292                 }
293                 case BT_UNBOND_DEVICE: {
294                         char *address = (char *)user_data;
295                         if (strncmp((char*)req_info->user_data, address, BT_ADDRESS_STRING_SIZE)) {
296                                 BT_ERR("Unexpected: Info request pending for a different address!!");
297                                 return;
298                         } else {
299                                 BT_DBG("Found info request addr [%s]", (char*)req_info->user_data);
300                                 bluetooth_device_address_t dev_addr;
301                                 _bt_convert_addr_string_to_type(dev_addr.addr, address);
302                                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
303                                 g_array_append_vals(out_param, &dev_addr,
304                                                 sizeof(bluetooth_device_address_t));
305                                 _bt_service_method_return(req_info->context, out_param, result);
306
307                                 g_free(req_info->user_data);
308                                 _bt_free_info_from_invocation_list(req_info);
309                                 g_array_free(out_param, TRUE);
310                         }
311                         break;
312                 }
313                 default:
314                         BT_ERR("Unhandled case");
315                         break;
316                 }
317         }
318         BT_DBG("-");
319 }
320
321 /*
322  * Remote device properties are received on all following conditions
323  * a. When Bonding in on-going
324  * b. When device properties are updated\changed for a connected device
325  *    (due to SDP or any other reason)
326  */
327 static void __bt_device_remote_properties_callback(event_dev_properties_t *oal_dev_props)
328 {
329         bt_remote_dev_info_t *rem_info = NULL;
330
331         rem_info = g_malloc0(sizeof(bt_remote_dev_info_t));
332         _bt_copy_remote_dev(rem_info, &(oal_dev_props->device_info));
333
334         if (oal_dev_props->adv_len > 0) {
335                 int k;
336
337                 rem_info->manufacturer_data_len = oal_dev_props->adv_len;
338                 rem_info->manufacturer_data =
339                         g_memdup(oal_dev_props->adv_data,
340                                         oal_dev_props->adv_len);
341                 BT_DBG("----Advertising Data Length: %d",
342                                 rem_info->manufacturer_data_len);
343
344                 for (k = 0; k < rem_info->manufacturer_data_len; k++) {
345                         BT_INFO("Check data[%d] = [[0x%x]",
346                                         k, oal_dev_props->adv_data[k]);
347                 }
348         } else {
349                 rem_info->manufacturer_data = NULL;
350                 rem_info->manufacturer_data_len = 0;
351         }
352
353         /* a. Check if bonding is on-going, if yes, we MUST update the bonding device properties */
354         if (trigger_bond_info  && !strcmp(trigger_bond_info->addr, rem_info->address)) {
355                 BT_DBG("Bonding is ongoing, try update properties");
356                 if (!trigger_bond_info->dev_info ||
357                                 (!trigger_bond_info->dev_info->name &&
358                                  !trigger_bond_info->dev_info->alias) ||
359                                         !trigger_bond_info->dev_info->address ||
360                                                 trigger_bond_info->dev_info->uuid_count == 0) {
361                         BT_DBG("Complete data is not present, Assigning rem_info");
362                         if (!trigger_bond_info->dev_info)
363                                 trigger_bond_info->dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
364                         _bt_copy_remote_dev_info(trigger_bond_info->dev_info, rem_info);
365                 }
366
367                 BT_DBG("Bonding dev addr has matched with remote dev properties address [%s]", rem_info->address);
368                 __bt_handle_ongoing_bond(trigger_bond_info->dev_info, FALSE);
369         } else if (incoming_bond_info && !g_strcmp0(incoming_bond_info->addr, rem_info->address)) {
370                 BT_DBG("Incoming Bond is ongoing, try update properties");
371                 if (!incoming_bond_info->dev_info ||
372                                 (!incoming_bond_info->dev_info->name &&
373                                  !incoming_bond_info->dev_info->alias) ||
374                                         !incoming_bond_info->dev_info->address ||
375                                                 incoming_bond_info->dev_info->uuid_count == 0) {
376                         BT_DBG("Complete data is not present, Assigning rem_info");
377                         if (!incoming_bond_info->dev_info)
378                                 incoming_bond_info->dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
379                         _bt_copy_remote_dev_info(incoming_bond_info->dev_info, rem_info);
380                 }
381
382                 BT_DBG("Incoming Bond addr matches with remote dev properties address [%s]", rem_info->address);
383                 __bt_handle_ongoing_bond(incoming_bond_info->dev_info, TRUE);
384         }
385
386         /* Add device to bonded list */
387         _bt_service_add_device_to_bonded_list(rem_info);
388
389         /* Handle SDP Device properties update */
390         if (service_search_info && service_search_info->dev_info) {
391                 if (!strcmp(service_search_info->addr, rem_info->address)) {
392                         BT_DBG("Properties received and SDP request pending, fill device properties and send event");
393                         service_search_info->dev_info->class = rem_info->class;
394                         service_search_info->dev_info->paired = rem_info->paired;
395                         service_search_info->dev_info->connected = rem_info->connected;
396                         service_search_info->dev_info->rssi = rem_info->rssi;
397                         service_search_info->dev_info->addr_type = rem_info->addr_type;
398                         service_search_info->dev_info->trust = rem_info->trust;
399
400                         /* TODO*/
401                         service_search_info->dev_info->manufacturer_data = NULL;
402                         service_search_info->dev_info->manufacturer_data_len = 0;
403
404                         __bt_handle_ongoing_device_service_search(service_search_info->dev_info);
405                 }
406         }
407
408         _bt_free_remote_dev(rem_info);
409 }
410
411 static int __get_oal_trusted_profile(bluetooth_trusted_profile_t profile)
412 {
413         switch (profile) {
414         case TRUSTED_PROFILE_PBAP:
415                 return OAL_TRUSTED_PROFILE_PBAP;
416         case TRUSTED_PROFILE_MAP:
417                 return OAL_TRUSTED_PROFILE_MAP;
418         case TRUSTED_PROFILE_SAP:
419                 return OAL_TRUSTED_PROFILE_SAP;
420         case TRUSTED_PROFILE_HFP_HF:
421                 return OAL_TRUSTED_PROFILE_HFP_HF;
422         case TRUSTED_PROFILE_A2DP:
423                 return OAL_TRUSTED_PROFILE_A2DP;
424         case TRUSTED_PROFILE_ALL:
425                 return OAL_TRUSTED_PROFILE_ALL;
426         default:
427                 return 0;
428         }
429 }
430
431 int _bt_set_trust_profile(bluetooth_device_address_t *addr,
432                 bluetooth_trusted_profile_t profile, gboolean trust)
433 {
434         int result;
435         bt_address_t bd_addr;
436         oal_trusted_profile_e oal_profile;
437
438         BT_DBG("+");
439
440         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
441
442         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
443         oal_profile = __get_oal_trusted_profile(profile);
444         retv_if(0 == oal_profile, BLUETOOTH_ERROR_NOT_SUPPORT);
445
446         result = device_set_trust_profile(&bd_addr, oal_profile, trust);
447         if (result != OAL_STATUS_SUCCESS) {
448                 BT_ERR("device_set_trust_profile error: [%d]", result);
449                 return BLUETOOTH_ERROR_INTERNAL;
450         }
451
452         BT_DBG("-");
453         return BLUETOOTH_ERROR_NONE;
454 }
455
456 int _bt_get_trust_profile(bluetooth_device_address_t *addr,
457                 bluetooth_trusted_profile_t profile, guint *trust)
458 {
459         int result;
460         bt_address_t bd_addr;
461         oal_trusted_profile_e oal_profile;
462
463         BT_DBG("+");
464
465         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
466
467         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
468         oal_profile = __get_oal_trusted_profile(profile);
469         retv_if(0 == oal_profile, BLUETOOTH_ERROR_NOT_SUPPORT);
470
471         result = device_get_trust_profile(&bd_addr, oal_profile, trust);
472         if (result != OAL_STATUS_SUCCESS) {
473                 BT_ERR("device_set_trust_profile error: [%d]", result);
474                 return BLUETOOTH_ERROR_INTERNAL;
475         }
476
477         BT_DBG("-");
478         return BLUETOOTH_ERROR_NONE;
479 }
480
481 static void __bt_handle_ongoing_device_service_search(bt_remote_dev_info_t *remote_dev_info)
482 {
483         GVariant *param = NULL;
484         GVariant *uuids = NULL;
485         GVariantBuilder *builder = NULL;
486         GVariant *manufacturer_data;
487         unsigned int i = 0;
488         char *name = NULL;
489
490         BT_INFO("Send Service Search request event");
491
492         if (remote_dev_info->alias)
493                 name = remote_dev_info->alias;
494         else
495                 name = remote_dev_info->name;
496
497         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
498         for (i = 0; i < remote_dev_info->uuid_count; i++) {
499                 g_variant_builder_add(builder, "s",
500                                 remote_dev_info->uuids[i]);
501         }
502         uuids = g_variant_new("as", builder);
503         g_variant_builder_unref(builder);
504         manufacturer_data = g_variant_new_from_data((const GVariantType *)"ay",
505                         remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
506                         TRUE, NULL, NULL);
507
508         param = g_variant_new("(isunsbub@asn@ay)",
509                         BLUETOOTH_ERROR_NONE,
510                         remote_dev_info->address,
511                         remote_dev_info->class,
512                         remote_dev_info->rssi,
513                         name,
514                         remote_dev_info->paired,
515                         remote_dev_info->connected,
516                         remote_dev_info->trust,
517                         uuids,
518                         remote_dev_info->manufacturer_data_len,
519                         manufacturer_data);
520         /* Send the event to application */
521         _bt_send_event(BT_ADAPTER_EVENT,
522                         BLUETOOTH_EVENT_SERVICE_SEARCHED,
523                         param);
524
525         __bt_free_service_search_info(&service_search_info);
526         BT_DBG("-");
527 }
528
529 static void __bt_device_services_callback(event_dev_services_t* uuid_list)
530 {
531         bt_remote_dev_info_t *rem_info = NULL;
532         unsigned int i;
533         BT_DBG("+");
534
535         if (trigger_bond_info && _bt_compare_adddress(trigger_bond_info->dev_addr,
536                                 (bluetooth_device_address_t *)&uuid_list->address) == TRUE) {
537                 bluetooth_device_address_t *dev_addr = trigger_bond_info->dev_addr;
538
539                 BT_DBG("Bonding dev addr has matched");
540                 /* Bonding ongoing, Query device properties again */
541                 if (BLUETOOTH_ERROR_NONE ==
542                         _bt_device_get_bonded_device_info(dev_addr))
543                         BT_DBG("_bt_device_get_bonded_device_info success");
544                 else
545                         BT_ERR("_bt_device_get_bonded_device_info failed");
546         }
547
548         if (service_search_info == NULL) {
549                 /* Send reply */
550                 BT_DBG("searching_info == NULL");
551                 return;
552         }
553
554         if (_bt_compare_adddress(service_search_info->dev_addr,
555                                 (bluetooth_device_address_t *)&uuid_list->address) == FALSE) {
556                 BT_DBG("This device is not queried");
557                 return;
558         }
559
560         rem_info = g_malloc0(sizeof(bt_remote_dev_info_t));
561         memset(rem_info, 0x00, sizeof(bt_remote_dev_info_t));
562
563         rem_info->address = g_new0(char, BT_ADDRESS_STRING_SIZE);
564         _bt_convert_addr_type_to_string(rem_info->address, uuid_list->address.addr);
565
566         rem_info->uuid_count = uuid_list->num;
567
568         BT_INFO("Address [%s]", rem_info->address);
569         BT_INFO("Number of UUID's [%d]", rem_info->uuid_count);
570         if (rem_info->uuid_count > 0)
571                 rem_info->uuids = g_new0(char *, rem_info->uuid_count);
572
573         /* Fill Remote Device Service List list */
574         for (i = 0; i < rem_info->uuid_count; i++) {
575                 rem_info->uuids[i] = g_malloc0(BLUETOOTH_UUID_STRING_MAX);
576                 _bt_uuid_to_string((service_uuid_t *)&uuid_list->service_list[i].uuid, rem_info->uuids[i]);
577                 BT_DBG("UUID value=%s", rem_info->uuids[i]);
578         }
579
580         /* Update local cache */
581         _bt_update_remote_dev_property(rem_info->address, DEV_PROP_SERVICES, (void *)rem_info);
582
583         BT_DBG("DBUS return");
584         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_SEARCH_SERVICE,
585                         service_search_info->addr, BT_ADDRESS_STRING_SIZE);
586
587         /* Save UUID List of remote devices */
588         if (service_search_info->dev_info)
589                 _bt_free_remote_dev(service_search_info->dev_info);
590         service_search_info->dev_info = rem_info;
591
592         /* Query Other device properties */
593         if (_bt_device_get_bonded_device_info(service_search_info->dev_addr) == BLUETOOTH_ERROR_NONE) {
594                 BT_DBG("Bonded device info query posted to stack successfully");
595         } else {
596                 BT_DBG("Possibly internal stack error in bonded device info query, perform cleanup");
597                 __bt_free_service_search_info(&service_search_info);
598         }
599         BT_DBG("-");
600 }
601
602 static void __bt_device_authorization_request_callback(event_dev_authorize_req_t* auth_event)
603 {
604         oal_service_t service_d = auth_event->service_id;
605         gchar address[BT_ADDRESS_STR_LEN];
606         int res;
607
608         _bt_convert_addr_type_to_string(address, auth_event->address.addr);
609
610         BT_DBG("service_d: %d", service_d);
611
612         switch (service_d) {
613         case HID_SERVICE_ID:
614                 BT_DBG("Incoming HID Profile conn Req from device addr [%s]", address);
615                 break;
616         case A2DP_SERVICE_ID:
617                 BT_DBG("Incoming A2DP(Remote Sink) profile conn Req from device addr [%s]", address);
618                 _bt_a2dp_src_handle_incoming_authorization(address, service_d);
619                 return;
620         case A2DP_SRC_SERVICE_ID:
621                 BT_DBG("Incoming A2DP(Remote Source) Profile conn Req from device addr [%s]", address);
622                 _bt_a2dp_sink_handle_incoming_authorization(address, service_d);
623                 break;
624         case AVRCP_SERVICE_ID:
625                 BT_DBG("Incoming AVRCP (Remote) Profile conn Req from device addr [%s]", address);
626                 break;
627         case AVRCP_CT_SERVICE_ID:
628                 BT_DBG("Incoming AVRCP (Controller) Profile conn Req from device addr [%s]", address);
629                 break;
630 #ifdef TIZEN_FEATURE_BT_OBEX
631         case OPP_SERVICE_ID: {
632                 GVariant *param = NULL;
633                 char *name = NULL;
634
635                 if (_bt_obex_server_is_custom() == false) {
636                         /* Allow the connection for native OPP server */
637                         break;
638                 }
639
640                 name = g_strdup(address);
641
642                 BT_DBG("Incoming OPP conn Req from device addr [%s]", address);
643                 _bt_obex_server_set_pending_conn_auth_device_addr(address);
644                 param = g_variant_new("(iss)", BLUETOOTH_ERROR_NONE, address, name);
645                 _bt_send_event(BT_OPP_SERVER_EVENT,
646                                 BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE, param);
647                 g_free(name);
648                 return;
649         }
650 #endif
651         case HSP_SERVICE_ID:
652                 BT_DBG("Incoming HSP_SERVICE_ID conn Req from device addr [%s]", address);
653                 break;
654         case HFP_SERVICE_ID:
655                 BT_DBG("Incoming HFP_SERVICE_ID conn Req from device addr [%s]", address);
656                 break;
657         case SAP_SERVICE_ID:
658                 BT_DBG("Incoming SAP_SERVICE_ID conn Req from device addr [%s]", address);
659                 break;
660         case HSP_HS_SERVICE_ID:
661                 BT_DBG("Incoming HSP_HS_SERVICE_ID conn Req from device addr [%s]", address);
662                 break;
663         case HFP_HS_SERVICE_ID:
664                 BT_DBG("Incoming HFP_HS_SERVICE_ID conn Req from device addr [%s]", address);
665                 break;
666 #ifdef TIZEN_BT_HAL
667         case IOTIVITY_SERVICE_ID:
668                 BT_DBG("Incoming IOTIVITY_SERVICE_ID conn Req from device addr [%s]", address);
669                 break;
670 #endif
671         default:
672                 /* For now, reject authorization for any service apart from above switch cases */
673                 BT_DBG("Incoming Profile conn req with service ID [%d] from device addr [%s]", service_d, address);
674                 res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, FALSE, FALSE);
675                 if (res != OAL_STATUS_SUCCESS)
676                         BT_ERR("authorize_response: %d", res);
677                 return;
678         }
679
680         /* Auto accept authorization request for HID, A2DP and AVRCP profiles */
681         BT_INFO("Auto Accept authorization");
682         res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, TRUE, FALSE);
683         if (res != OAL_STATUS_SUCCESS)
684                 BT_ERR("authorize_response: %d", res);
685 }
686
687 static void __bt_handle_ongoing_bond(bt_remote_dev_info_t *remote_dev_info, gboolean incoming_bond)
688 {
689         GVariant *param = NULL;
690
691         if ((remote_dev_info->name || remote_dev_info->alias)
692                         && remote_dev_info->address
693                         && remote_dev_info->uuids) {
694                 GVariant *uuids = NULL;
695                 GVariantBuilder *builder = NULL;
696                 GVariant *manufacturer_data;
697                 unsigned int i = 0;
698                 char *name = NULL;
699
700                 if (remote_dev_info->alias)
701                         name = remote_dev_info->alias;
702                 else
703                         name = remote_dev_info->name;
704
705                 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
706                 for (i = 0; i < remote_dev_info->uuid_count; i++) {
707                         g_variant_builder_add(builder, "s",
708                                         remote_dev_info->uuids[i]);
709                 }
710                 uuids = g_variant_new("as", builder);
711                 g_variant_builder_unref(builder);
712                 manufacturer_data = g_variant_new_from_data((const GVariantType *)"ay",
713                                 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
714                                 TRUE, NULL, NULL);
715
716                 param = g_variant_new("(isunsbub@asn@ay)",
717                                 BLUETOOTH_ERROR_NONE,
718                                 remote_dev_info->address,
719                                 remote_dev_info->class,
720                                 remote_dev_info->rssi,
721                                 name,
722                                 remote_dev_info->paired,
723                                 remote_dev_info->connected,
724                                 remote_dev_info->trust,
725                                 uuids,
726                                 remote_dev_info->manufacturer_data_len,
727                                 manufacturer_data);
728                 /* Send the event to application */
729                 _bt_send_event(BT_ADAPTER_EVENT,
730                                 BLUETOOTH_EVENT_BONDING_FINISHED,
731                                 param);
732                 BT_PERMANENT_LOG("Paired %s", remote_dev_info->address + 12);
733                 BT_INFO_C("### Paired %s", remote_dev_info->address + 12);
734                 if (incoming_bond) {
735                         __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
736                 } else {
737                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
738                         __bt_free_pairing_info(&trigger_pairing_info);
739                 }
740         } else {
741                 BT_INFO("Lets wait for more remote device properties");
742         }
743 }
744
745 static void __handle_incoming_bond_created_event(bt_address_t *bd_addr)
746 {
747         char address[BT_ADDRESS_STRING_SIZE];
748         bluetooth_device_address_t dev_addr;
749
750         /*
751          * BlueZ sends paired signal for each paired device, during activation,
752          * We should ignore this, otherwise application thinks that a new device
753          * got paired
754          */
755         if (_bt_adapter_get_status() != BT_ACTIVATED) {
756                 BT_DBG("BT is not activated, so ignore this");
757                 return;
758         }
759
760         _bt_convert_addr_type_to_string(address, bd_addr->addr);
761         if (!incoming_bond_info) {
762                 incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
763         } else {
764                 if (g_strcmp0(incoming_bond_info->addr, address)) {
765                         BT_DBG("Previous Bond address: [%s] differs from new address: [%s]",
766                                         address, incoming_bond_info->addr);
767                         __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
768                         incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
769                 }
770         }
771
772         BT_INFO("Incoming bond successfully completed");
773         g_strlcpy(incoming_bond_info->addr, address, BT_ADDRESS_STRING_SIZE);
774         incoming_bond_info->dev_info = NULL;
775
776         _bt_convert_addr_string_to_type(dev_addr.addr, incoming_bond_info->addr);
777         if (BLUETOOTH_ERROR_NONE == _bt_device_get_bonded_device_info(&dev_addr)) {
778                 BT_DBG("Bonded device info query posted to stack successfully");
779         } else {
780                 BT_ERR("Bonded device info query failed");
781                 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
782         }
783
784 }
785
786 static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr)
787 {
788         gchar address[BT_ADDRESS_STR_LEN];
789         bluetooth_device_address_t dev_addr;
790
791         if (trigger_bond_info == NULL) {
792                 /* Send reply */
793                 BT_DBG("trigger_bond_info == NULL, Handle incomming bond event");
794                 __handle_incoming_bond_created_event(bd_addr);
795                 return;
796         }
797
798         _bt_convert_addr_type_to_string(address, bd_addr->addr);
799         if (g_strcmp0(trigger_bond_info->addr, address)) {
800                 BT_DBG("Bonding address= [%s] is different from requested address =[%s]",
801                                 address, trigger_bond_info->addr);
802                 __handle_incoming_bond_created_event(bd_addr);
803                 return;
804         }
805
806         /* Bonding state will be cleaned up & BONDING FINISHED EVENT
807            will be sent only when Properties are fetched from stack
808            Till that time lets not free trigger_bond_info.
809            However it is possible that while fetching device properties, internal
810            stack error can occur which can lead to no valid properties or
811            no properties at all. So in such cases, we must not wait for properties,
812            otherwise, it can lead to infinite wait  */
813         _bt_convert_addr_string_to_type(dev_addr.addr,
814                         trigger_bond_info->addr);
815
816         if (_bt_device_get_bonded_device_info(&dev_addr) == BLUETOOTH_ERROR_NONE) {
817                 BT_DBG("Bonded device info query posted to stack successfully");
818                 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_BOND_DEVICE,
819                                 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
820         } else {
821                 BT_DBG("Possibly internal stack error in bonded device info query, perform cleanup");
822                 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
823                                 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
824                 /* Destroy if at all device got bonded at stack level */
825                 device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
826
827                 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
828                 __bt_free_pairing_info(&trigger_pairing_info);
829         }
830 }
831
832 /**********************************************************************************************
833 *  Bond removal event can be triggered for following reasons -
834 *  a. If Bonding procedure if failed (for Auth failed, Page timeout, cancelled by user etc)
835 *  b. If Application requests for explicitly removing the bond
836 *  c. When application attempt to create bond,bond is removed first which triggers this event
837 *     c. is in-line with Bluedroid bond create\emoval architecture
838 *********************************************************************************************/
839 static void __bt_device_handle_bond_removal_event(bt_address_t *bd_addr)
840 {
841         char address[BT_ADDRESS_STRING_SIZE];
842
843         _bt_convert_addr_type_to_string(address, bd_addr->addr);
844         _bt_service_remove_device_from_bonded_list(address);
845
846         if (trigger_unbond_info) {
847                 BT_DBG("Bond removal request successfully handled, return DBUS and send event");
848                 GVariant *param = NULL;
849                 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_UNBOND_DEVICE,
850                                 trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
851                 param = g_variant_new("(is)", BLUETOOTH_ERROR_NONE, trigger_unbond_info->addr);
852                 _bt_send_event(BT_ADAPTER_EVENT,
853                                 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
854                                 param);
855                 BT_PERMANENT_LOG("Unpaired %s", trigger_unbond_info->addr + 12);
856                 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
857                 __bt_free_pairing_info(&trigger_pairing_info);
858         } else if (trigger_bond_info) {
859                 BT_ERR("Bonding was removed");
860                 __bt_device_handle_bond_state();
861         }
862 }
863
864 static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_fail_event)
865 {
866         BT_INFO("+");
867         oal_status_t status = bond_fail_event->status;
868         BT_INFO("Bonding failed, reason: %d", status);
869
870         switch (status) {
871         case OAL_STATUS_RMT_DEVICE_DOWN:
872         {
873                 if (trigger_bond_info) {
874 #if !defined(TIZEN_PROFILE_WEARABLE) && !defined(TIZEN_PROFILE_IOT)
875                         BT_INFO("OAL_STATUS_RMT_DEVICE_DOWN:Lets retry bonding!! retry count [%d]",
876                                         bond_retry_count);
877                         int ret = OAL_STATUS_SUCCESS;
878                         if (bond_retry_count < MAX_BOND_RETRY_COUNT) {
879                                 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
880                                 if (ret != OAL_STATUS_SUCCESS) {
881                                         BT_ERR("Create Bond procedure could not suceed");
882                                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
883                                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
884                                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
885                                         __bt_free_pairing_info(&trigger_pairing_info);
886                                         bond_retry_count = 0;
887                                 } else
888                                         bond_retry_count++;
889                         } else {
890 #endif
891                                 BT_ERR("Create Bond failed MAX_BOND_RETRY_COUNT TIMES!!");
892                                 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
893                                                 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
894                                 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
895                                 __bt_free_pairing_info(&trigger_pairing_info);
896 #if !defined(TIZEN_PROFILE_WEARABLE) && !defined(TIZEN_PROFILE_IOT)
897                                 bond_retry_count = 0;
898                         }
899 #endif
900                 }
901                 break;
902         }
903         case OAL_STATUS_AUTH_FAILED:
904         {
905                 /*TODO Auto pairing status set & ignore auto pairing logics can be done at this point.
906                   To be considered later*/
907                 int result = BLUETOOTH_ERROR_INTERNAL;
908                 BT_INFO("BT_OPERATION_STATUS_AUTH_FAILED");
909
910                 BT_INFO("add device in pairing black list");
911                 _bt_set_autopair_status_in_bonding_info(FALSE);
912
913                 if (trigger_bond_info) {
914                         __bt_ignore_auto_pairing_request(trigger_bond_info->addr);
915                         BT_ERR("Create Bond procedure could not suceed, check if cancelled by User");
916                         if (trigger_bond_info->is_cancelled_by_user) {
917                                 BT_ERR("Bonding is cancelled by user");
918                                 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
919                         }
920                         __bt_device_handle_pending_requests(result, BT_BOND_DEVICE,
921                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
922                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
923                 }
924
925                 __bt_free_pairing_info(&trigger_pairing_info);
926                 break;
927         }
928         case OAL_STATUS_ALREADY_CONNECT:
929         {
930                 int result = BLUETOOTH_ERROR_ALREADY_CONNECT;
931                 BT_INFO("OAL_STATUS_ALREADY_CONNECT");
932
933                 if (trigger_bond_info) {
934                         __bt_device_handle_pending_requests(result, BT_BOND_DEVICE,
935                                 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
936                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
937                 }
938                 break;
939         }
940         case OAL_STATUS_INTERNAL_ERROR:
941         {
942                 BT_INFO("OAL_STATUS_INTERNAL_ERROR");
943                 if (trigger_unbond_info) {
944                         BT_INFO("Bond removal request failed, return DBUS and send event");
945                         GVariant *param = NULL;
946                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_UNBOND_DEVICE,
947                                         trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
948                         param = g_variant_new("(is)", BLUETOOTH_ERROR_INTERNAL, trigger_unbond_info->addr);
949                         _bt_send_event(BT_ADAPTER_EVENT,
950                                         BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
951                                         param);
952                         __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
953                 } else if (trigger_bond_info) {
954                         if (__bt_device_handle_bond_state() != BLUETOOTH_ERROR_NONE) {
955                                 if (trigger_bond_info) {
956                                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
957                                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
958                                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
959                                 }
960                         }
961                 }
962
963                 __bt_free_pairing_info(&trigger_pairing_info);
964                 break;
965         }
966         default:
967         {
968                 BT_ERR("Unknown status of Bond failed event status [%d]", status);
969                 break;
970         }
971
972         }
973         BT_INFO("-");
974 }
975
976 static gboolean __bt_device_init_dbfw_rssi_cb(gpointer user_data)
977 {
978         dbfw_rssi = 0;
979         return FALSE;
980 }
981
982 static void __bt_device_parse_dbfw_set_rssi(unsigned char *data, uint32_t length)
983 {
984         ret_if(length < 5);
985         dbfw_rssi = 0xFFFFFF00 | data[4];
986         BT_INFO("dbfw_rssi = %d", dbfw_rssi);
987         g_timeout_add(1000, __bt_device_init_dbfw_rssi_cb, NULL);
988 }
989
990 static void __bt_device_dbfw_plus_info_callback(event_dev_dbfw_plus_info_t *dbfw_info)
991 {
992         char evt_str[18];
993         char *data_str = NULL;
994         int i;
995
996         data_str = g_malloc0(dbfw_info->length * 2 + 1);
997         for (i = 0; i < dbfw_info->length; i++)
998                 snprintf(&data_str[i * 2], 3, "%02X", dbfw_info->data[i]);
999         data_str[i * 2] = '\0';
1000
1001         switch(dbfw_info->event_code) {
1002                 case BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_INFO:
1003                         BT_DBG("## Event Code: Linkloss Debug Info");
1004                         snprintf(evt_str, 18, "[DBFW]Linkloss(D)");
1005                         __bt_device_parse_dbfw_set_rssi(dbfw_info->data, dbfw_info->length);
1006                         break;
1007                 case BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_CLOCK_INFO:
1008                         BT_DBG("## Event Code: Linkloss Clock Info");
1009                         snprintf(evt_str, 18, "[DBFW]Linkloss(C)");
1010                         break;
1011                 case BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_PAGE_SLOTS:
1012                         BT_DBG("## Event Code: Linkloss Page slots");
1013                         snprintf(evt_str, 18, "[DBFW]Linkloss(P)");
1014                         break;
1015                 case BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_LMP_TRACE:
1016                         BT_DBG("## Event Code: Linkloss LMP trace");
1017                         snprintf(evt_str, 18, "[DBFW]Linkloss(L)");
1018                         break;
1019                 case BT_DBFW_PLUS_EVENT_CODE_A2DP_INFO:
1020                         BT_DBG("## Event Code: A2DP Info");
1021                         snprintf(evt_str, 18, "[DBFW]A2DP");
1022                         break;
1023                 case BT_DBFW_PLUS_EVENT_CODE_HFP_INFO:
1024                         BT_DBG("## Event Code: HFP Info");
1025                         snprintf(evt_str, 18, "[DBFW]HFP");
1026                         break;
1027                 case BT_DBFW_PLUS_EVENT_CODE_HFP_SCO_PACKET_TYPE_INFO:
1028                         BT_DBG("## Event Code: HFP SCO Packet Type");
1029                         snprintf(evt_str, 18, "[DBFW]SCO");
1030                         break;
1031                 default:
1032                         BT_DBG("## Unknown event code (0x%02x)", dbfw_info->event_code);
1033                         snprintf(evt_str, 18, "[DBFW]0x%02X", dbfw_info->event_code);
1034                         break;
1035         }
1036
1037         BT_PERMANENT_LOG("%s 0x%s", evt_str, data_str);
1038         g_free(data_str);
1039 }
1040
1041 static void __bt_device_event_handler(int event_type, gpointer event_data)
1042 {
1043         int eventcheck = OAL_EVENT_DEVICE_PROPERTIES;
1044         BT_DBG("event [%d] Event check = [%d]", event_type, eventcheck);
1045
1046         switch (event_type) {
1047         case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY: {
1048                 BT_INFO("BREDR Device Found");
1049                 __bt_device_remote_device_found_callback(event_data, FALSE);
1050                 break;
1051         }
1052         case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE: {
1053                 BT_INFO("Dual Device Found");
1054                 __bt_device_remote_device_found_callback(event_data, FALSE);
1055                 break;
1056         }
1057         case OAL_EVENT_DEVICE_PROPERTIES: {
1058                 BT_INFO("Remote Device properties Received");
1059                 __bt_device_remote_properties_callback((event_dev_properties_t *)event_data);
1060                 break;
1061         }
1062         case OAL_EVENT_DEVICE_BONDING_SUCCESS: {
1063                 BT_INFO("Bonding Success event Received");
1064                 __bt_device_handle_bond_completion_event((bt_address_t *)event_data);
1065                 break;
1066         }
1067         case OAL_EVENT_DEVICE_BONDING_REMOVED: {
1068                 BT_INFO("Bonding Removed event Received");
1069                 __bt_device_handle_bond_removal_event((bt_address_t *)event_data);
1070                 break;
1071         }
1072         case OAL_EVENT_DEVICE_BONDING_FAILED: {
1073                 BT_INFO("Bonding Failed event Received");
1074                 __bt_device_handle_bond_failed_event((event_dev_bond_failed_t*) event_data);
1075                 break;
1076         }
1077         case OAL_EVENT_DEVICE_ACL_CONNECTED: {
1078                 BT_DBG("ACL Connected event Received");
1079                 __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, TRUE, 0);
1080                 break;
1081         }
1082         case OAL_EVENT_DEVICE_ACL_DISCONNECTED: {
1083                 BT_DBG("ACL Disconnected event Received");
1084                 __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 0);
1085                 break;
1086         }
1087         case OAL_EVENT_DEVICE_LE_CONNECTED: {
1088                 BT_DBG("LE Connected event Received");
1089                 __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, TRUE, 1);
1090                 break;
1091         }
1092         case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
1093                 BT_DBG("LE Disconnected event Received");
1094                 __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 1);
1095                 break;
1096         }
1097         case OAL_EVENT_DEVICE_PIN_REQUEST: {
1098                 BT_INFO("PIN Request Received");
1099                 __bt_device_pin_request_callback((remote_device_t*)event_data);
1100                 break;
1101         }
1102         case OAL_EVENT_DEVICE_PASSKEY_ENTRY_REQUEST: {
1103                 BT_INFO("Passkey Entry request Received");
1104                 __bt_device_ssp_passkey_entry_callback((remote_device_t*)event_data);
1105                 break;
1106         }
1107         case OAL_EVENT_DEVICE_PASSKEY_CONFIRMATION_REQUEST:{
1108                 BT_INFO("Passkey Confirmation Request Received");
1109                 __bt_device_ssp_passkey_confirmation_callback((event_dev_passkey_t *)event_data);
1110                 break;
1111         }
1112         case OAL_EVENT_DEVICE_PASSKEY_DISPLAY: {
1113                 BT_INFO("Passkey Display Request Received");
1114                 __bt_device_ssp_passkey_display_callback((event_dev_passkey_t *)event_data);
1115                 break;
1116         }
1117         case OAL_EVENT_DEVICE_SSP_CONSENT_REQUEST: {
1118                 BT_INFO("SSP Consent Request Received");
1119                  __bt_device_ssp_consent_callback((remote_device_t*)event_data);
1120                 break;
1121         }
1122         case OAL_EVENT_DEVICE_SERVICES: {
1123                 BT_INFO("Remote Device Services Received");
1124                 __bt_device_services_callback((event_dev_services_t*)event_data);
1125                 break;
1126         }
1127         case OAL_EVENT_DEVICE_AUTHORIZE_REQUEST: {
1128                 BT_INFO("Remote Device Authorization Request");
1129                 __bt_device_authorization_request_callback((event_dev_authorize_req_t*)event_data);
1130                 break;
1131         }
1132         case OAL_EVENT_DEVICE_TRUSTED: {
1133                 BT_INFO("Remote Device Trusted");
1134                 __bt_device_trusted_callback(TRUE, (event_dev_trust_t*)event_data);
1135                 break;
1136         }
1137         case OAL_EVENT_DEVICE_UNTRUSTED: {
1138                 BT_INFO("Remote Device UnTrusted");
1139                 __bt_device_trusted_callback(FALSE, (event_dev_trust_t*)event_data);
1140                 break;
1141         }
1142         case OAL_EVENT_DEVICE_NAME: {
1143                 remote_device_t *rem_dev = event_data;
1144                 gchar address[BT_ADDRESS_STR_LEN];
1145
1146                 _bt_convert_addr_type_to_string(address, rem_dev->address.addr);
1147                 BT_INFO("Remote Device name Received");
1148                 BT_INFO("Name: %s, Address: %s", rem_dev->name, address);
1149
1150                 /* Update local cache */
1151                 _bt_update_remote_dev_property(address, DEV_PROP_NAME, (void *)rem_dev->name);
1152                 break;
1153         }
1154         case OAL_EVENT_DEVICE_TRUSTED_PROFILES_CHANGED: {
1155                 event_device_trusted_profiles_t *ev = event_data;
1156                 char address[BT_ADDRESS_STRING_SIZE];
1157
1158                 ret_if(NULL == ev);
1159
1160                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1161                 _bt_send_event(BT_DEVICE_EVENT,
1162                                 BLUETOOTH_EVENT_SUPPORTED_PROFILE_TRUSTED,
1163                                 g_variant_new("(isi)", BLUETOOTH_ERROR_NONE,
1164                                         address, ev->trust_val));
1165                 break;
1166         }
1167         case OAL_EVENT_RSSI_MONITORING_ENABLED: {
1168                 event_dev_rssi_info_t *ev = event_data;
1169                 char address[BT_ADDRESS_STRING_SIZE];
1170                 GVariant *param;
1171
1172                 ret_if(NULL == ev);
1173
1174                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1175                 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1176                                 address, ev->link_type, TRUE);
1177                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1178                 break;
1179         }
1180         case OAL_EVENT_RSSI_MONITORING_DISABLED: {
1181                 event_dev_rssi_info_t *ev = event_data;
1182                 char address[BT_ADDRESS_STRING_SIZE];
1183                 GVariant *param;
1184
1185                 ret_if(NULL == ev);
1186
1187                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1188                 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1189                                 address, ev->link_type, FALSE);
1190                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1191                 break;
1192         }
1193         case OAL_EVENT_RSSI_ALERT_RECEIVED: {
1194                 event_dev_rssi_info_t *ev = event_data;
1195                 char address[BT_ADDRESS_STRING_SIZE];
1196                 GVariant *param;
1197
1198                 ret_if(NULL == ev);
1199
1200                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1201                 param = g_variant_new("(isiii)", BLUETOOTH_ERROR_NONE,
1202                                 address, ev->link_type, ev->alert_type, ev->rssi);
1203                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ALERT, param);
1204                 break;
1205         }
1206         case OAL_EVENT_RAW_RSSI_RECEIVED: {
1207                 event_dev_rssi_info_t *ev = event_data;
1208                 char address[BT_ADDRESS_STRING_SIZE];
1209                 GVariant *param;
1210
1211                 ret_if(NULL == ev);
1212
1213                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1214                 param = g_variant_new("(isii)", BLUETOOTH_ERROR_NONE,
1215                                 address, ev->link_type, ev->rssi);
1216                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RAW_RSSI, param);
1217                 break;
1218         }
1219         case OAL_EVENT_DEVICE_DBFW_PLUS_INFO: {
1220                 __bt_device_dbfw_plus_info_callback((event_dev_dbfw_plus_info_t*)event_data);
1221                 break;
1222         }
1223         default:
1224                 BT_INFO("Unhandled event..");
1225         }
1226 }
1227
1228 /* Legacy Pairing event handler */
1229 static void __bt_device_pin_request_callback(remote_device_t* pin_req_event)
1230 {
1231         GVariant *param;
1232         char address[BT_ADDRESS_STRING_SIZE];
1233         char pin_code[BLUETOOTH_PIN_CODE_MAX_LENGTH + 1];
1234         bool incoming = false;
1235         BT_DBG("+");
1236
1237         _bt_convert_addr_type_to_string(address, pin_req_event->address.addr);
1238
1239         BT_INFO("Address[%s]", address);
1240         BT_INFO("Name[%s]", pin_req_event->name);
1241         BT_INFO("COD[%d]", pin_req_event->cod);
1242
1243         if (trigger_pairing_info) {
1244                 /* BTAPI support only one pairing at a time */
1245                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1246                 BT_ERR("New PIN request address [%s]", address);
1247                 device_reject_pin_request(&pin_req_event->address);
1248                 return;
1249         }
1250
1251         /* If user initiated bonding and auto response is possible, just reply with default 0000*/
1252         if (_bt_is_bonding_device_address(address) == TRUE &&
1253                         _bt_agent_is_auto_response(pin_req_event->cod, address, pin_req_event->name)) {
1254                 /* Note: Currently even if SYSPOPUP is supported, we use Fixed PIN "0000" for basic pairing
1255                    as BT SYSPOPUP is currently not working for PIN KEY entry in Tizen platform. This needs
1256                    to be checked and fixed apropriately */
1257                 _bt_set_autopair_status_in_bonding_info(TRUE);
1258                 device_accept_pin_request(&pin_req_event->address, "0000");
1259         } else if (_bt_agent_is_hid_keyboard(pin_req_event->cod)) {
1260                 BT_DBG("Remote Device is HID keyboard Type..");
1261                 char str_passkey[BT_PASSKEY_MAX_LENGTH + 1] = { 0 };
1262
1263                 if (_bt_agent_generate_passkey(str_passkey,
1264                                         BT_PASSKEY_MAX_LENGTH) != 0) {
1265                         device_reject_pin_request(&pin_req_event->address);
1266                         goto done;
1267                 }
1268                 device_accept_pin_request(&pin_req_event->address, str_passkey);
1269
1270                 BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1271
1272                 if(trigger_bond_info == NULL)
1273                         incoming = true;
1274
1275                 param = g_variant_new("(bsss)", incoming, address, pin_req_event->name, str_passkey);
1276                 _bt_send_event(BT_ADAPTER_EVENT,
1277                                 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1278                 BT_DBG("Key board pairing in process");
1279         } else if (BLUETOOTH_ERROR_NONE == __bt_get_device_pin_code(address, pin_code)) {
1280                 BT_DBG("Use stored PIN code [%s]", pin_code);
1281                 device_accept_pin_request(&pin_req_event->address, pin_code);
1282         } else {
1283                 if (_bt_is_bonding_device_address(address) == TRUE) {
1284                         BT_DBG("Show Pin entry");
1285                         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1286                         trigger_pairing_info->addr = g_strdup(address);
1287                         trigger_pairing_info->is_ssp = FALSE;
1288
1289                         BT_DBG("Send BLUETOOTH_EVENT_PIN_REQUEST");
1290
1291                         if(trigger_bond_info == NULL)
1292                                 incoming = true;
1293
1294                         param = g_variant_new("(bss)", incoming, address, pin_req_event->name);
1295                         _bt_send_event(BT_ADAPTER_EVENT,
1296                                         BLUETOOTH_EVENT_PIN_REQUEST, param);
1297                 }
1298         }
1299
1300 done:
1301         _bt_agent_release_memory();
1302         BT_DBG("-");
1303 }
1304
1305 /* SSP Pairing event handler */
1306 static void __bt_device_ssp_passkey_entry_callback(remote_device_t* dev_info)
1307 {
1308         GVariant *param;
1309         gchar address[BT_ADDRESS_STR_LEN];
1310         char *p_addr;
1311         gchar *name;
1312         bool incoming = false;
1313
1314         BT_DBG("+");
1315
1316         _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1317         p_addr = address;
1318         name = dev_info->name;
1319
1320         BT_INFO("Address[%s]", address);
1321         BT_INFO("Name[%s]", name);
1322         BT_INFO("COD[%d]", dev_info->cod);
1323
1324         if (trigger_pairing_info) {
1325                 /* BTAPI support only one pairing at a time */
1326                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1327                 BT_ERR("New PIN request address [%s]", address);
1328                 device_reject_pin_request(&dev_info->address);
1329                 BT_DBG("-");
1330                 return;
1331         }
1332
1333         /* Set pairing data */
1334         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1335         trigger_pairing_info->addr = g_strdup(address);
1336         trigger_pairing_info->is_ssp = TRUE;
1337
1338         if(trigger_bond_info == NULL)
1339                 incoming = true;
1340
1341         param = g_variant_new("(bss)", incoming, p_addr, name);
1342         _bt_send_event(BT_ADAPTER_EVENT,
1343                         BLUETOOTH_EVENT_PASSKEY_REQUEST, param);
1344         BT_DBG("-");
1345 }
1346
1347 static void __bt_device_ssp_passkey_confirmation_callback(event_dev_passkey_t *dev_info)
1348 {
1349         GVariant *param;
1350         gchar address[BT_ADDRESS_STR_LEN];
1351         char *p_addr;
1352         gchar *name;
1353         char str_passkey[7];
1354         bool incoming = false; /*Stores if bonding request is incoming(true) or outgoing(false) */
1355         BT_DBG("+");
1356
1357         _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1358         p_addr = address;
1359         name = dev_info->device_info.name;
1360
1361         BT_INFO("Address[%s]", address);
1362         BT_INFO("Name[%s]", name);
1363         BT_INFO("COD[%d]", dev_info->device_info.cod);
1364
1365         if (trigger_pairing_info) {
1366                 /* BTAPI support only one pairing at a time */
1367                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1368                 BT_ERR("New PIN request address [%s]", address);
1369                 device_reject_pin_request(&dev_info->device_info.address);
1370                 BT_DBG("-");
1371                 return;
1372         }
1373
1374         /* Set pairing data */
1375         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1376         trigger_pairing_info->addr = g_strdup(address);
1377         trigger_pairing_info->is_ssp = TRUE;
1378
1379         BT_DBG("Send BLUETOOTH_EVENT_PASSKEY_CONFIRMATION");
1380         snprintf(str_passkey, sizeof(str_passkey), "%.6d", dev_info->pass_key);
1381
1382         /*Storing if bond is incoming or outgoing*/
1383         if(trigger_bond_info == NULL)
1384                 incoming = true;
1385
1386         param = g_variant_new("(bsss)", incoming, p_addr, name, str_passkey);
1387         _bt_send_event(BT_ADAPTER_EVENT,
1388                         BLUETOOTH_EVENT_PASSKEY_CONFIRM_REQUEST, param);
1389         BT_DBG("-");
1390 }
1391
1392 static void __bt_device_ssp_passkey_display_callback(event_dev_passkey_t *dev_info)
1393 {
1394         GVariant *param;
1395         gchar address[BT_ADDRESS_STR_LEN];
1396         char *p_addr;
1397         gchar *name;
1398         char str_passkey[7];
1399         bool incoming = false;
1400         BT_DBG("+");
1401
1402         _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1403         p_addr = address;
1404         name = dev_info->device_info.name;
1405
1406         BT_INFO("Address[%s]", address);
1407         BT_INFO("Name[%s]", name);
1408         BT_INFO("COD[%d]", dev_info->device_info.cod);
1409
1410         if (trigger_pairing_info) {
1411                 /* BTAPI support only one pairing at a time */
1412                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1413                 BT_ERR("New PIN request address [%s]", address);
1414                 device_reject_pin_request(&dev_info->device_info.address);
1415                 BT_DBG("-");
1416                 return;
1417         }
1418
1419         /* Set pairing data */
1420         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1421         trigger_pairing_info->addr = g_strdup(address);
1422         trigger_pairing_info->is_ssp = TRUE;
1423
1424         BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1425         snprintf(str_passkey, sizeof(str_passkey), "%06d", dev_info->pass_key);
1426
1427         if(trigger_bond_info == NULL)
1428                 incoming = true;
1429
1430         param = g_variant_new("(bsss)", incoming, p_addr, name, str_passkey);
1431
1432         if (passkey_watcher) {
1433                 BT_INFO("Send passkey to %s", passkey_watcher);
1434                 _bt_send_event_to_dest(passkey_watcher, BT_ADAPTER_EVENT,
1435                                 BLUETOOTH_EVENT_PASSKEY_NOTIFICATION, param);
1436         } else {
1437                 _bt_send_event(BT_ADAPTER_EVENT,
1438                                 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1439         }
1440         BT_DBG("-");
1441 }
1442
1443 static void __bt_device_ssp_consent_callback(remote_device_t* dev_info)
1444 {
1445         gchar address[BT_ADDRESS_STR_LEN];
1446         gchar *name;
1447         int local_major;
1448         int local_minor;
1449         int cod;
1450         BT_DBG("+");
1451
1452         _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1453         name = dev_info->name;
1454         cod = dev_info->cod;
1455
1456         BT_INFO("Address[%s]", address);
1457         BT_INFO("Name[%s]", name);
1458         BT_INFO("COD[%d]", cod);
1459
1460         if (trigger_pairing_info) {
1461                 /* BTAPI support only one pairing at a time */
1462                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1463                 BT_ERR("New PIN request address [%s]", address);
1464                 device_reject_pin_request(&dev_info->address);
1465                 BT_DBG("-");
1466                 return;
1467         }
1468
1469         /* Set pairing data */
1470         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1471         trigger_pairing_info->addr = g_strdup(address);
1472         trigger_pairing_info->is_ssp = TRUE;
1473
1474         local_major = ((cod >> 8) & 0x001f);
1475         local_minor = (cod & 0x00fc);
1476         BT_DBG("SSP_CONSENT: Major type=[0x%x] and Minor type=[0x%x]", local_major, local_minor);
1477
1478         /*TODO: BLUETOOTH_EVENT_SSP_CONSENT_REQUEST to be handled in Tizen */
1479         BT_DBG("-");
1480 }
1481
1482 static void __bt_device_acl_state_changed_callback(event_dev_conn_status_t *acl_event,
1483                 gboolean connected, unsigned char type)
1484 {
1485         gchar address[BT_ADDRESS_STR_LEN];
1486         int disc_reason = 0;
1487         GVariant *param = NULL;
1488         bt_device_conn_info_t conn_info;
1489
1490         _bt_convert_addr_type_to_string(address, acl_event->address.addr);
1491
1492         _bt_logging_connection(connected, type);
1493
1494         disc_reason = acl_event->status;
1495
1496         if (connected) {
1497                 BT_PERMANENT_LOG("Connected %s %s", !type ? "EDR" : "LE", address + 12);
1498                 param = g_variant_new("(isy)", BLUETOOTH_ERROR_NONE, address, type);
1499                 _bt_send_event(BT_DEVICE_EVENT,
1500                                 BLUETOOTH_EVENT_DEVICE_CONNECTED,
1501                                 param);
1502         } else {
1503                 BT_PERMANENT_LOG("Disconnected %s(%d) %s", !type ? "EDR" : "LE",  disc_reason, address + 12);
1504                 param = g_variant_new("(isyi)", disc_reason, address, type, dbfw_rssi);
1505                 _bt_send_event(BT_DEVICE_EVENT,
1506                                 BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1507                                 param);
1508         }
1509
1510         conn_info.connected = connected;
1511         conn_info.type = type;
1512         /* Update local cache */
1513         _bt_update_remote_dev_property(address, DEV_PROP_CONNECTED, (void *)&conn_info);
1514
1515 #ifdef TIZEN_GATT_CLIENT
1516         /*handle LE connected device info*/
1517         if (type)
1518                 _bt_handle_le_connected_dev_info(address, connected);
1519 #endif
1520
1521 }
1522
1523 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble)
1524 {
1525         bt_remote_dev_info_t *dev_info = NULL;
1526         int result = BLUETOOTH_ERROR_NONE;
1527         GVariant *param = NULL;
1528         GVariant *uuids = NULL;
1529         GVariant *manufacturer_data = NULL;
1530         GVariantBuilder *builder = NULL;
1531         unsigned int i;
1532
1533         ret_if(_bt_is_discovering() == FALSE);
1534         ret_if(event_data == NULL);
1535
1536         dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
1537
1538         if (is_ble) {
1539                 event_ble_dev_found_t * oal_ble_dev = event_data;
1540                 BT_INFO("Device type [%d]", oal_ble_dev->device_info.type);
1541
1542                 _bt_copy_remote_dev(dev_info, &oal_ble_dev->device_info);
1543
1544                 dev_info->manufacturer_data_len = oal_ble_dev->adv_len;
1545                 if (dev_info->manufacturer_data_len)
1546                         dev_info->manufacturer_data = g_memdup(oal_ble_dev->adv_data,
1547                                         dev_info->manufacturer_data_len);
1548                 else
1549                         dev_info->manufacturer_data = NULL;
1550                 BT_DBG("----Advertising Data Length: %d", dev_info->manufacturer_data_len);
1551         } else {
1552                 event_dev_found_t * oal_dev = event_data;
1553                 _bt_copy_remote_dev(dev_info, &oal_dev->device_info);
1554         }
1555
1556         /* If Remote device name is NULL or still RNR is not done then display address as name. */
1557         if (dev_info->name == NULL)
1558                 dev_info->name = g_strdup(dev_info->address);
1559         BT_DBG("Name %s", dev_info->name);
1560
1561         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1562         for (i = 0; i < dev_info->uuid_count; i++)
1563                 g_variant_builder_add(builder, "s", dev_info->uuids[i]);
1564
1565         uuids = g_variant_new("as", builder);
1566         g_variant_builder_unref(builder);
1567
1568         manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1569                         dev_info->manufacturer_data,
1570                         dev_info->manufacturer_data_len,
1571                         TRUE,
1572                         NULL, NULL);
1573
1574         param = g_variant_new("(isunsbub@asn@ay)", result,
1575                         dev_info->address,
1576                         dev_info->class,
1577                         dev_info->rssi,
1578                         dev_info->name,
1579                         dev_info->paired,
1580                         dev_info->connected,
1581                         dev_info->trust,
1582                         uuids,
1583                         dev_info->manufacturer_data_len,
1584                         manufacturer_data);
1585
1586         _bt_send_event(BT_ADAPTER_EVENT,
1587                         BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
1588                         param);
1589
1590         _bt_free_remote_dev(dev_info);
1591 }
1592
1593 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info)
1594 {
1595         gchar address[BT_ADDRESS_STR_LEN];
1596         int result = BLUETOOTH_ERROR_NONE;
1597         GVariant *param = NULL;
1598         int event;
1599
1600         _bt_convert_addr_type_to_string(address, info->address.addr);
1601
1602         /* Update local cache */
1603         _bt_update_remote_dev_property(address, DEV_PROP_TRUST, (void *)&trusted);
1604
1605         param = g_variant_new("(is)", result, address);
1606         event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1607                 BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1608         /* Send event to application */
1609         _bt_send_event(BT_DEVICE_EVENT,
1610                         event,
1611                         param);
1612
1613 }
1614
1615 static void __bt_free_pairing_info(bt_pairing_data_t **p_info)
1616 {
1617         bt_pairing_data_t * info = *p_info;
1618         if (info) {
1619                 if (info->addr)
1620                         g_free(info->addr);
1621
1622                 g_free(info);
1623         }
1624         *p_info = NULL;
1625 }
1626
1627 static void __bt_free_bond_info(uint8_t type)
1628 {
1629         switch (type) {
1630         case BT_DEVICE_BOND_INFO:
1631                 if (!trigger_bond_info)
1632                         break;
1633
1634                 if (trigger_bond_info->addr)
1635                         g_free(trigger_bond_info->addr);
1636                 if (trigger_bond_info->dev_addr)
1637                         g_free(trigger_bond_info->dev_addr);
1638                 if (trigger_bond_info->dev_info)
1639                         _bt_free_remote_dev(trigger_bond_info->dev_info);
1640                 g_free(trigger_bond_info);
1641                 trigger_bond_info = NULL;
1642                 break;
1643         case BT_DEVICE_INCOMING_BOND_INFO:
1644                 if (!incoming_bond_info)
1645                         break;
1646
1647                 if (incoming_bond_info->dev_info)
1648                         _bt_free_remote_dev(incoming_bond_info->dev_info);
1649                 g_free(incoming_bond_info);
1650                 incoming_bond_info = NULL;
1651                 break;
1652         case BT_DEVICE_UNBOND_INFO:
1653                 if (!trigger_unbond_info)
1654                         break;
1655
1656                 if (trigger_unbond_info->addr)
1657                         g_free(trigger_unbond_info->addr);
1658                 if (trigger_unbond_info->dev_addr)
1659                         g_free(trigger_unbond_info->dev_addr);
1660                 if (trigger_unbond_info->dev_info)
1661                         _bt_free_remote_dev(trigger_unbond_info->dev_info);
1662                 g_free(trigger_unbond_info);
1663                 trigger_unbond_info = NULL;
1664                 break;
1665         }
1666 }
1667
1668 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info)
1669 {
1670         bt_service_search_info_data_t * info = *p_info;
1671         if (info) {
1672                 if (info->addr) {
1673                         g_free(info->addr);
1674                         info->addr = NULL;
1675                 }
1676
1677                 if (info->dev_addr) {
1678                         g_free(info->dev_addr);
1679                         info->dev_addr = NULL;
1680                 }
1681
1682                 if (info->dev_info) {
1683                         _bt_free_remote_dev(info->dev_info);
1684                         info->dev_info = NULL;
1685                 }
1686
1687                 g_free(info);
1688         }
1689         *p_info = NULL;
1690 }
1691
1692 static int __bt_device_handle_bond_state(void)
1693 {
1694         BT_DBG("Current Bond state: %d", bt_device_bond_state);
1695         int ret = OAL_STATUS_INTERNAL_ERROR;
1696
1697         switch (bt_device_bond_state) {
1698         case BT_DEVICE_BOND_STATE_CANCEL_DISCOVERY:
1699                 /*TODO:Bonding during discovery: Unhandled!!*/
1700                 BT_INFO("Bonding during discovery: Unhandled!!");
1701                 break;
1702         case BT_DEVICE_BOND_STATE_DISCOVERY_CANCELLED:
1703                 /*TODO:Bonding during discovery: Unhandled!!*/
1704                 BT_INFO("Bonding during discovery: Unhandled!!");
1705                 break;
1706         case BT_DEVICE_BOND_STATE_REMOVE_BONDING:
1707                 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVED_BONDING;
1708                 ret = device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
1709                 if (ret != OAL_STATUS_SUCCESS)
1710                         ret = __bt_device_handle_bond_state();
1711                 break;
1712         case BT_DEVICE_BOND_STATE_REMOVED_BONDING:
1713                 bt_device_bond_state = BT_DEVICE_BOND_STATE_NONE;
1714                 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
1715                 /* Bonding procedure was started but unfortunately could not complete.
1716                    Basically removed bonding was success, but create bond request could not proceed
1717                    So lets cleanup the context */
1718                 if (ret != OAL_STATUS_SUCCESS) {
1719                         BT_ERR("Create Bond procedure could not suceed");
1720                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1721                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1722                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1723                         __bt_free_pairing_info(&trigger_pairing_info);
1724                 }
1725                 break;
1726         case BT_DEVICE_BOND_STATE_NONE:
1727                 BT_INFO("Create Bond failed!!");
1728                 if (trigger_bond_info) {
1729                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1730                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1731                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1732                         __bt_free_pairing_info(&trigger_pairing_info);
1733                 }
1734                 break;
1735         default:
1736                 break;
1737         }
1738
1739         if (ret != OAL_STATUS_SUCCESS)
1740                 return BLUETOOTH_ERROR_INTERNAL;
1741         else
1742                 return BLUETOOTH_ERROR_NONE;
1743 }
1744
1745 int _bt_device_get_bonded_device_info(bluetooth_device_address_t *addr)
1746 {
1747         int result;
1748         bt_address_t bd_addr;
1749
1750         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
1751
1752         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
1753         result = device_query_attributes(&bd_addr);
1754         if (result != OAL_STATUS_SUCCESS) {
1755                 BT_ERR("device_query_attributes error: [%d]", result);
1756                 return BLUETOOTH_ERROR_INTERNAL;
1757         }
1758
1759         return BLUETOOTH_ERROR_NONE;
1760 }
1761
1762 int _bt_set_alias(bluetooth_device_address_t *device_address, const char *alias)
1763 {
1764         int ret;
1765         char address[BT_ADDRESS_STRING_SIZE];
1766
1767         BT_CHECK_PARAMETER(alias, return);
1768
1769         ret = device_set_alias((bt_address_t *)device_address, (char *)alias);
1770         if (ret != OAL_STATUS_SUCCESS) {
1771                 BT_ERR("device_set_alias: %d", ret);
1772                 return BLUETOOTH_ERROR_INTERNAL;
1773         }
1774
1775         /* Update local cache */
1776         _bt_convert_addr_type_to_string(address, device_address->addr);
1777         _bt_update_remote_dev_property(address, DEV_PROP_ALIAS, (void *)alias);
1778
1779         return BLUETOOTH_ERROR_NONE;
1780 }
1781
1782 int _bt_bond_device(bluetooth_device_address_t *device_address,
1783                 unsigned short conn_type, GArray **out_param1)
1784 {
1785         int result = BLUETOOTH_ERROR_NONE;
1786         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1787         bluetooth_device_info_t dev_info;
1788         const char *stack_name = NULL;
1789
1790         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1791
1792         /* If bonding or discovery already going on */
1793         if (trigger_bond_info || _bt_is_discovering()) {
1794                 BT_ERR("Device is buzy, bonding can not proceed now..");
1795                 result = BLUETOOTH_ERROR_DEVICE_BUSY;
1796                 goto fail;
1797         }
1798
1799         /*TODO: If unbonding with same device going on */
1800         _bt_convert_addr_type_to_string(address, device_address->addr);
1801
1802         trigger_bond_info = g_malloc0(sizeof(bt_bond_data_t));
1803         trigger_bond_info->addr = g_strdup(address);
1804         trigger_bond_info->conn_type = conn_type;
1805         trigger_bond_info->is_device_creating = TRUE;
1806         trigger_bond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1807         trigger_bond_info->dev_info = NULL;
1808
1809         /* Ready to initiate bonding */
1810
1811         stack_name = oal_get_stack_name();
1812         if (stack_name && !g_strcmp0(stack_name, "bluez")) {
1813                 BT_DBG("[bluez] Create bond by type %d", conn_type);
1814                 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVED_BONDING;
1815         } else {
1816                 /* In Tizen, we will first remove bond and then attempt to create bond to keep
1817                    consistency with bluedroid. Even if remove bond fails due to device not already
1818                    bonded, then straight away create bond is triggered. This is because, remove bond
1819                    is handled differently in bluedroid and bluez. In Bluez, if device is
1820                    already removed, remove bond call fails.
1821                    However in bluedroid, remove bond on already removed device returns success. So we will
1822                    handle the cases transparently */
1823                 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVE_BONDING;
1824         }
1825
1826 #if !defined(TIZEN_PROFILE_WEARABLE) && !defined(TIZEN_PROFILE_IOT)
1827         bond_retry_count = 0;
1828 #endif
1829         result = __bt_device_handle_bond_state();
1830
1831         if (result != BLUETOOTH_ERROR_NONE)
1832                 goto fail;
1833
1834         return result;
1835
1836 fail:
1837         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1838         memcpy(dev_info.device_address.addr, device_address->addr,
1839                         BLUETOOTH_ADDRESS_LENGTH);
1840
1841         g_array_append_vals(*out_param1, &dev_info,
1842                         sizeof(bluetooth_device_info_t));
1843         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1844
1845         return result;
1846 }
1847
1848 int _bt_unbond_device(bluetooth_device_address_t *device_address,
1849                 GArray **out_param1)
1850 {
1851         int result = OAL_STATUS_SUCCESS;
1852         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1853         bluetooth_device_info_t dev_info;
1854
1855         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1856
1857         _bt_convert_addr_type_to_string(address, device_address->addr);
1858
1859         trigger_unbond_info = g_malloc0(sizeof(bt_bond_data_t));
1860         trigger_unbond_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1861         trigger_unbond_info->addr = g_strdup(address);
1862         trigger_unbond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1863
1864         /* Check if Bonding is already going on, we should not abruptly remove bonding*/
1865         if (trigger_bond_info && strncmp(trigger_bond_info->addr, trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE) == 0) {
1866                 BT_ERR("Bonding with same device already ongoing");
1867                 result = BLUETOOTH_ERROR_PERMISSION_DEINED;
1868                 goto fail;
1869         }
1870
1871         result = device_destroy_bond((bt_address_t *)device_address);
1872         if (result != OAL_STATUS_SUCCESS)
1873                 goto fail;
1874
1875         return BLUETOOTH_ERROR_NONE;
1876
1877 fail:
1878         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1879         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1880                         trigger_unbond_info->addr);
1881
1882         g_array_append_vals(*out_param1, &dev_info,
1883                         sizeof(bluetooth_device_info_t));
1884         __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
1885
1886         if (result == OAL_STATUS_NOT_PAIRED)
1887                 return BLUETOOTH_ERROR_NOT_PAIRED;
1888
1889         return BLUETOOTH_ERROR_INTERNAL;
1890 }
1891
1892 int _bt_cancel_bonding(void)
1893 {
1894         int result = OAL_STATUS_SUCCESS;
1895         BT_INFO("+");
1896
1897         retv_if(trigger_bond_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1898
1899         result = device_stop_bond((bt_address_t *)trigger_bond_info->dev_addr);
1900
1901         if (result == OAL_STATUS_SUCCESS)
1902                 trigger_bond_info->is_cancelled_by_user = TRUE;
1903
1904         return result;
1905 }
1906
1907 int _bt_passkey_reply(const char *passkey, gboolean cnfm_reply)
1908 {
1909         bluetooth_device_address_t device_address;
1910         int ret = OAL_STATUS_SUCCESS;
1911         BT_INFO("reply: %d", cnfm_reply);
1912
1913         retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1914         retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1915
1916         _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1917
1918         if (trigger_pairing_info->is_ssp) {
1919                 if (cnfm_reply)
1920                         ret = device_accept_passkey_entry((bt_address_t *)&device_address, atoi(passkey));
1921                 else
1922                         ret = device_reject_passkey_entry((bt_address_t *)&device_address);
1923                 trigger_pairing_info->is_ssp = FALSE;
1924         } else {
1925                 if (cnfm_reply)
1926                         ret = device_accept_pin_request((bt_address_t *)&device_address, passkey);
1927                 else
1928                         ret = device_reject_pin_request((bt_address_t *)&device_address);
1929         }
1930
1931         __bt_free_pairing_info(&trigger_pairing_info);
1932
1933         if (ret != OAL_STATUS_SUCCESS) {
1934                 BT_ERR("_bt_device_handle_passkey_reply: err [%d]", ret);
1935                 return BLUETOOTH_ERROR_INTERNAL;
1936         }
1937
1938         BT_INFO("-");
1939         return BLUETOOTH_ERROR_NONE;
1940 }
1941
1942 int _bt_passkey_confirmation_reply(gboolean cnfm_reply)
1943 {
1944         BT_INFO("BT_PASSKEY_CONFIRMATION_REPLY");
1945         bluetooth_device_address_t device_address;
1946         int ret = OAL_STATUS_SUCCESS;
1947         BT_INFO("reply: %d", cnfm_reply);
1948
1949         retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1950         retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1951
1952         _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1953
1954         ret = device_reply_passkey_confirmation((bt_address_t *)&device_address, cnfm_reply);
1955
1956         __bt_free_pairing_info(&trigger_pairing_info);
1957         if (ret != OAL_STATUS_SUCCESS) {
1958                 BT_ERR("_bt_device_handle_passkey_confirmation_reply: err [%d]", ret);
1959                 return BLUETOOTH_ERROR_INTERNAL;
1960         }
1961
1962         BT_INFO("-");
1963         return BLUETOOTH_ERROR_NONE;
1964 }
1965
1966 gboolean _bt_device_is_pairing(void)
1967 {
1968         return (trigger_pairing_info) ? TRUE : FALSE;
1969 }
1970
1971 gboolean _bt_device_is_bonding(void)
1972 {
1973         return (trigger_bond_info) ? TRUE : FALSE;
1974 }
1975
1976 gboolean _bt_is_bonding_device_address(const char *address)
1977 {
1978         if (trigger_bond_info == NULL || trigger_bond_info->addr == NULL)
1979                 return FALSE;
1980
1981         if (g_strcmp0(trigger_bond_info->addr, address) == 0) {
1982                 BT_DBG("[%s]  is bonding device", address);
1983                 return TRUE;
1984         }
1985
1986         BT_DBG("[%s]  is NOT bonding device", address);
1987         return FALSE;
1988 }
1989
1990 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
1991 {
1992         ret_if(trigger_bond_info == NULL);
1993         trigger_bond_info->is_autopair = is_autopair;
1994 }
1995
1996 int _bt_search_device(bluetooth_device_address_t *device_address)
1997 {
1998         int result = OAL_STATUS_SUCCESS;
1999         BT_DBG("+");
2000
2001         BT_CHECK_PARAMETER(device_address, return);
2002
2003         if (trigger_bond_info) {
2004                 BT_ERR("Bonding in progress");
2005                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2006         }
2007
2008         if (service_search_info) {
2009                 BT_ERR("Service searching in progress");
2010                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2011         }
2012
2013         /* allocate user data so that it can be retrieved in callback */
2014         service_search_info = g_malloc0(sizeof(bt_service_search_info_data_t));
2015         service_search_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
2016         service_search_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
2017
2018         _bt_convert_addr_type_to_string(service_search_info->addr,
2019                         device_address->addr);
2020
2021         result = device_query_services((bt_address_t *)device_address);
2022
2023         if (result != OAL_STATUS_SUCCESS) {
2024                 BT_ERR("Device Service Search Failed..: %d", result);
2025                 __bt_free_service_search_info(&service_search_info);
2026                 return BLUETOOTH_ERROR_INTERNAL;
2027         }
2028         return BLUETOOTH_ERROR_NONE;
2029 }
2030
2031 int _bt_cancel_search_device(void)
2032 {
2033         int ret = OAL_STATUS_SUCCESS;
2034         retv_if(service_search_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
2035
2036         ret = device_stop_query_sevices((bt_address_t *)service_search_info->dev_addr);
2037
2038         if (ret != OAL_STATUS_SUCCESS) {
2039                 BT_ERR("SDP Cancel request failed [%d]", ret);
2040                 return BLUETOOTH_ERROR_INTERNAL;
2041         }
2042
2043         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_CANCEL_BY_USER, BT_SEARCH_SERVICE,
2044                         service_search_info->addr, BT_ADDRESS_STRING_SIZE);
2045
2046         __bt_free_service_search_info(&service_search_info);
2047
2048         return BLUETOOTH_ERROR_NONE;
2049         BT_DBG("-");
2050 }
2051
2052 int _bt_set_authorization(bluetooth_device_address_t *device_address,
2053                 gboolean authorize)
2054 {
2055         int ret = OAL_STATUS_SUCCESS;
2056         BT_DBG("+");
2057
2058         BT_CHECK_PARAMETER(device_address, return);
2059         BT_INFO("Device to be Trusted? [%d]", authorize);
2060
2061         ret = device_set_authorized((bt_address_t*)device_address, authorize);
2062         if (ret != OAL_STATUS_SUCCESS) {
2063                 BT_ERR("device_set_authorized: %d", ret);
2064                 return BLUETOOTH_ERROR_INTERNAL;
2065         }
2066
2067         return BLUETOOTH_ERROR_NONE;
2068 }
2069
2070 gboolean _bt_is_device_connected(bluetooth_device_address_t *device_address, int svc_type)
2071 {
2072         gboolean is_connected;
2073         oal_service_t svc_id;
2074
2075         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
2076
2077         /*
2078          * TODO: While adding support for new profiles, need to add more
2079          * <svc_type, svc_id> mapping here.
2080          */
2081         switch (svc_type) {
2082         case BT_PROFILE_CONN_HID:
2083                 svc_id = HID_SERVICE_ID;
2084                 break;
2085         case BT_PROFILE_CONN_A2DP:
2086                 svc_id = A2DP_SERVICE_ID; /* Remote is A2DP Sink */
2087                 break;
2088         case BT_PROFILE_CONN_A2DP_SINK:
2089                 svc_id = A2DP_SRC_SERVICE_ID; /* Remote is A2DP Source*/
2090                 break;
2091         case BT_PROFILE_CONN_HSP:
2092                 svc_id = HFP_HS_SERVICE_ID; /* Remote is HFP HF Unit */
2093                 break;
2094         case BT_PROFILE_CONN_HFG:
2095                 svc_id = HFP_SERVICE_ID; /* Remote is HFP AG Unit */
2096                 break;
2097 #ifdef TIZEN_GATT_CLIENT
2098         case BT_PROFILE_CONN_GATT:
2099                 return _bt_is_remote_gatt_device_connected(device_address); /* Remote is GATT client or Server */
2100 #endif
2101         default:
2102                 BT_DBG("Unknown svc_type: %d", svc_type);
2103                 return FALSE;
2104         }
2105
2106         is_connected = device_get_svc_conn_state((bt_address_t*)device_address, svc_id);
2107
2108         BT_DBG("svc_type: %d, is_connected: %s",
2109                         svc_type, is_connected ? "TRUE" : "FALSE");
2110
2111         return is_connected;
2112 }
2113
2114 int _bt_rfcomm_reply_conn_authorization(char *address, gboolean reply)
2115 {
2116         bt_address_t bd_addr;
2117         int res;
2118
2119         BT_DBG("+");
2120
2121         retv_if(NULL == address, BLUETOOTH_ERROR_INVALID_PARAM);
2122         _bt_convert_addr_string_to_type(bd_addr.addr, address);
2123         res = device_reply_auth_request(&bd_addr, 0, reply, FALSE);
2124         if (res != OAL_STATUS_SUCCESS) {
2125                 BT_ERR("authorize_response: %d", res);
2126                 return BLUETOOTH_ERROR_INTERNAL;
2127         }
2128
2129         BT_DBG("-");
2130         return BLUETOOTH_ERROR_NONE;
2131 }
2132
2133 int _bt_enable_rssi(bluetooth_device_address_t *addr, int link_type,
2134                 int low_threshold, int in_range_threshold, int high_threshold)
2135 {
2136         int result;
2137         bt_address_t bd_addr;
2138
2139         BT_DBG("+");
2140
2141         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2142
2143         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2144         result = device_enable_rssi_monitoring(&bd_addr, link_type,
2145                         low_threshold, in_range_threshold, high_threshold);
2146         if (result != OAL_STATUS_SUCCESS) {
2147                 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2148                 return BLUETOOTH_ERROR_INTERNAL;
2149         }
2150
2151         BT_DBG("-");
2152         return BLUETOOTH_ERROR_NONE;
2153 }
2154
2155 int _bt_get_rssi_strength(bluetooth_device_address_t *addr, int link_type)
2156 {
2157         int result;
2158         bt_address_t bd_addr;
2159
2160         BT_DBG("+");
2161
2162         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2163
2164         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2165         result = device_get_connected_link_rssi_strength(&bd_addr, link_type);
2166         if (result != OAL_STATUS_SUCCESS) {
2167                 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2168                 return BLUETOOTH_ERROR_INTERNAL;
2169         }
2170
2171         BT_DBG("-");
2172         return BLUETOOTH_ERROR_NONE;
2173 }
2174
2175 int _bt_set_passkey_notification(const char *sender, gboolean enable)
2176 {
2177         int result;
2178
2179         BT_INFO("Set passkey notification(sender:%s, %s)",
2180                         sender, enable ? "Enable" : "Disable");
2181
2182         result = device_enable_gap_auth_notifications(OAL_PASSKEY_DISPLAY, enable);
2183         if (OAL_STATUS_SUCCESS != result) {
2184                 BT_ERR("device_enable_gap_auth_notifications error: [%d]", result);
2185                 return BLUETOOTH_ERROR_INTERNAL;
2186         }
2187
2188         g_free(passkey_watcher);
2189         if (enable == TRUE)
2190                 passkey_watcher = g_strdup(sender);
2191         else
2192                 passkey_watcher = NULL;
2193
2194         return BLUETOOTH_ERROR_NONE;
2195 }
2196
2197 static int __bt_get_device_pin_code(const char *address, char *pin_code)
2198 {
2199         GSList *l = NULL;
2200
2201         BT_CHECK_PARAMETER(address, return);
2202         BT_CHECK_PARAMETER(pin_code, return);
2203
2204         for (l = pin_info_list; l != NULL; l = l->next) {
2205                 bt_pin_code_info_t *pin_info = l->data;
2206
2207                 if (!pin_info || !pin_info->address)
2208                         continue;
2209
2210                 if (g_strcmp0(pin_info->address, address) == 0) {
2211                         g_strlcpy(pin_code, pin_info->pin_code,
2212                                         BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2213                         return BLUETOOTH_ERROR_NONE;
2214                 }
2215         }
2216
2217         return BLUETOOTH_ERROR_NOT_FOUND;
2218 }
2219
2220 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2221                 bluetooth_device_pin_code_t *pin_code)
2222 {
2223         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2224         bt_pin_code_info_t *pin_info = NULL;
2225         GSList *l = NULL;
2226
2227         BT_CHECK_PARAMETER(device_address, return);
2228         BT_CHECK_PARAMETER(pin_code, return);
2229         retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2230                         BLUETOOTH_ERROR_NO_RESOURCES);
2231
2232         _bt_convert_addr_type_to_string(address, device_address->addr);
2233
2234         for (l = pin_info_list; l != NULL; l = l->next) {
2235                 pin_info = l->data;
2236
2237                 if (!pin_info || !pin_info->address)
2238                         continue;
2239
2240                 if (g_strcmp0(pin_info->address, address) == 0) {
2241                         g_free(pin_info->pin_code);
2242                         pin_info->pin_code = g_strdup(pin_code->pin_code);
2243                         return BLUETOOTH_ERROR_NONE;
2244                 }
2245         }
2246
2247         pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2248         pin_info->address = g_strdup(address);
2249         pin_info->pin_code = g_strdup(pin_code->pin_code);
2250         pin_info_list = g_slist_append(pin_info_list, pin_info);
2251
2252         return BLUETOOTH_ERROR_NONE;
2253 }
2254
2255 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2256 {
2257         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2258         bt_pin_code_info_t *pin_info = NULL;
2259         GSList *l = NULL;
2260
2261         BT_DBG("+");
2262
2263         BT_CHECK_PARAMETER(device_address, return);
2264
2265         _bt_convert_addr_type_to_string(address, device_address->addr);
2266
2267         for (l = pin_info_list; l != NULL; l = l->next) {
2268                 pin_info = l->data;
2269
2270                 if (!pin_info || !pin_info->address)
2271                         continue;
2272
2273                 if (g_strcmp0(pin_info->address, address) == 0) {
2274                         pin_info_list = g_slist_remove(pin_info_list, pin_info);
2275                         g_free(pin_info->address);
2276                         g_free(pin_info->pin_code);
2277                         g_free(pin_info);
2278                         break;
2279                 }
2280         }
2281
2282         BT_DBG("-");
2283         return BLUETOOTH_ERROR_NONE;
2284 }
2285
2286 int _bt_device_get_ida(bluetooth_device_address_t *device_address, bluetooth_device_address_t *id_address)
2287 {
2288         int result = BLUETOOTH_ERROR_NONE;
2289         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2290
2291         _bt_convert_addr_type_to_string(address, device_address->addr);
2292
2293         BT_DBG("getting IDA for remote device: [%s]", address);
2294
2295         result = device_get_ida((bt_address_t*)device_address, (bt_address_t*)id_address);
2296
2297         if (result != OAL_STATUS_SUCCESS) {
2298                 BT_ERR("device_get_ida Failed %d", result);
2299                 return BLUETOOTH_ERROR_INTERNAL;
2300         }
2301
2302         return result;
2303 }
2304
2305 #ifdef TIZEN_GATT_CLIENT
2306 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2307 {
2308         GSList *l = NULL;
2309         bt_connected_le_dev_t *dev;
2310
2311         if (!address)
2312                 return NULL;
2313
2314         for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2315                 dev = l->data;
2316
2317                 if (g_strcmp0(dev->address, address) == 0)
2318                         return dev;
2319         }
2320         return NULL;
2321 }
2322
2323 static void __bt_le_conn_param_free(void *data)
2324 {
2325         bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2326
2327         BT_DBG("%s", param->sender);
2328         g_free(param->sender);
2329         g_free(param);
2330 }
2331
2332 static void _bt_add_le_connected_dev_info(const char *address)
2333 {
2334         bt_connected_le_dev_t *dev = NULL;
2335
2336         if (!address)
2337                 return;
2338
2339         dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2340         dev->address = g_strdup(address);
2341
2342         le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2343
2344         return;
2345 }
2346
2347 static void _bt_remove_le_connected_dev_info(const char *address)
2348 {
2349         bt_connected_le_dev_t *dev = NULL;
2350
2351         if (!address)
2352                 return;
2353
2354         dev = __bt_get_le_connected_dev_info(address);
2355         if (!dev)
2356                 return;
2357
2358         g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2359         le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2360         g_free(dev->address);
2361         g_free(dev);
2362
2363         return;
2364 }
2365
2366 static void _bt_handle_le_connected_dev_info(const char *address, gboolean connected)
2367 {
2368         BT_DBG("update le_connected_dev_list");
2369
2370         if (connected)
2371                 _bt_add_le_connected_dev_info(address);
2372         else
2373                 _bt_remove_le_connected_dev_info(address);
2374 }
2375
2376 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2377 {
2378         GSList *l = NULL;
2379         bt_le_conn_param_t *param = NULL;
2380
2381         if (!dev || !sender)
2382                 return NULL;
2383
2384         for (l = dev->senders; l; l = g_slist_next(l)) {
2385                 param = l->data;
2386                 if (g_strcmp0(param->sender, sender) == 0)
2387                         return param;
2388         }
2389
2390         return NULL;
2391 }
2392
2393 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2394 {
2395         bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2396         bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2397
2398         return parama->key > paramb->key;
2399 }
2400
2401
2402 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2403                 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2404 {
2405         bt_connected_le_dev_t *dev = NULL;
2406         bt_le_conn_param_t *param = NULL;
2407         bt_le_conn_param_t *data = NULL;
2408
2409         if (!address || !sender)
2410                 return BLUETOOTH_ERROR_INVALID_PARAM;
2411
2412         dev = __bt_get_le_connected_dev_info(address);
2413         if (!dev)
2414                 return BLUETOOTH_ERROR_INTERNAL;
2415
2416         param = __bt_get_le_conn_param_info(dev, sender);
2417
2418         data = g_malloc0(sizeof(bt_le_conn_param_t));
2419         data->sender = g_strdup(sender);
2420         data->interval_min = interval_min;
2421         data->interval_max = interval_max;
2422         data->latency = latency;
2423         data->time_out = time_out;
2424         data->key = interval_min + (interval_max - interval_min)/2;
2425
2426         if (param == NULL) {
2427                 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2428                 dev->senders = g_slist_append(dev->senders, data);
2429         } else {
2430                 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2431                 dev->senders = g_slist_remove(dev->senders, param);
2432                 g_free(param->sender);
2433                 g_free(param);
2434                 dev->senders = g_slist_append(dev->senders, data);
2435         }
2436
2437         /* Sorting. First element have the minimum interval */
2438         dev->senders = g_slist_sort(dev->senders,
2439                         (GCompareFunc)__bt_compare_le_conn_param_key);
2440
2441         return BLUETOOTH_ERROR_NONE;
2442 }
2443
2444
2445 static int __bt_le_set_conn_parameter(const char *address,
2446                 float interval_min, float interval_max,
2447                 guint16 latency, guint16 time_out)
2448 {
2449         bt_address_t dev_addr = { {0} };
2450         guint32 min, max, to;
2451
2452         BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2453                         interval_min, interval_max, latency, time_out);
2454
2455         min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2456         max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2457         to = time_out / BT_LE_CONN_TO_SPLIT;
2458
2459         BT_DBG("updating: Min interval: %d, Max interval: %d, Latency: %d, Supervision timeout: %d",
2460                         min, max, latency, to);
2461
2462         _bt_convert_addr_string_to_type(dev_addr.addr, address);
2463
2464         return gattc_conn_param_update(&dev_addr, min, max, latency, to);
2465 }
2466
2467 int _bt_remove_le_conn_param_info(const char *address, const char *sender)
2468 {
2469         bt_connected_le_dev_t *dev = NULL;
2470         bt_le_conn_param_t *param = NULL;
2471
2472         if (!address || !sender)
2473                 return BLUETOOTH_ERROR_INVALID_PARAM;
2474
2475         dev = __bt_get_le_connected_dev_info(address);
2476         if (!dev)
2477                 return BLUETOOTH_ERROR_INTERNAL;
2478
2479         param = __bt_get_le_conn_param_info(dev, sender);
2480         if (param) {
2481                 BT_DBG("Remove param %s %s ", address, sender);
2482                 dev->senders = g_slist_remove(dev->senders, param);
2483                 g_free(param->sender);
2484                 g_free(param);
2485         }
2486
2487         return BLUETOOTH_ERROR_NONE;
2488 }
2489
2490 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2491                 bluetooth_le_connection_param_t *param)
2492 {
2493         if (param == NULL)
2494                 return BLUETOOTH_ERROR_INVALID_PARAM;
2495
2496         memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2497
2498         switch (mode) {
2499         case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2500                 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2501                 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2502                 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2503                 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2504                 break;
2505
2506         case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2507                 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2508                 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2509                 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2510                 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2511                 break;
2512
2513         case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2514                 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2515                 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2516                 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2517                 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2518                 break;
2519
2520         default:
2521                 BT_ERR("Unhandled mode : %d", mode);
2522                 break;
2523         }
2524
2525         return BLUETOOTH_ERROR_NONE;
2526 }
2527
2528 int _bt_le_connection_update(const char *sender,
2529                 unsigned char *device_address,
2530                 float interval_min, float interval_max,
2531                 guint16 latency, guint16 time_out)
2532 {
2533         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2534         guint32 min_supervision_to;
2535         bt_connected_le_dev_t *dev = NULL;
2536         bt_le_conn_param_t *param = NULL;
2537         int ret = BLUETOOTH_ERROR_NONE;
2538
2539         BT_CHECK_PARAMETER(device_address, return);
2540
2541         BT_DBG("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2542                         sender, interval_min, interval_max, latency, time_out);
2543
2544         if (interval_min > interval_max ||
2545                         interval_min < BT_LE_CONN_INTERVAL_MIN ||
2546                         interval_max > BT_LE_CONN_INTERVAL_MAX) {
2547                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2548                 goto fail;
2549         }
2550
2551         if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2552                         time_out > BT_LE_CONN_SUPER_TO_MAX) {
2553                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2554                 goto fail;
2555         }
2556
2557         if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2558                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2559                 goto fail;
2560         }
2561
2562         /*
2563          * The Supervision_Timeout in milliseconds shall be larger than
2564          * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2565          * where Conn_Interval_Max is given in milliseconds.
2566          */
2567
2568         min_supervision_to = (1 + latency) * interval_max * 2;
2569         if (time_out <= min_supervision_to) {
2570                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2571                 goto fail;
2572         }
2573
2574         _bt_convert_addr_type_to_string(address, device_address);
2575         BT_DBG("Remote device address: %s", address);
2576
2577         _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2578
2579         dev = __bt_get_le_connected_dev_info(address);
2580         if (dev == NULL) {
2581                 BT_ERR("device not found in the list");
2582                 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2583                 goto fail;
2584         }
2585
2586         if (g_slist_length(dev->senders) == 1)
2587                 goto update;
2588         else {
2589                 param = dev->senders->data;
2590
2591                 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2592
2593                 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2594                         BT_DBG("Skip due to same interval");
2595                         return ret;
2596                 }
2597
2598                 interval_min = param->interval_min;
2599                 interval_max = param->interval_max;
2600         }
2601
2602 update:
2603         ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2604
2605         if (ret != OAL_STATUS_SUCCESS) {
2606                 _bt_remove_le_conn_param_info(address, sender);
2607                 BT_DBG("fail to update the LE connection parameter");
2608                 ret = BLUETOOTH_ERROR_INTERNAL;
2609                 goto fail;
2610         }
2611
2612         dev->interval_min = interval_min;
2613         dev->interval_max = interval_max;
2614
2615         return BLUETOOTH_ERROR_NONE;
2616 fail:
2617         return ret;
2618 }
2619
2620 int _bt_disconnect_device(bluetooth_device_address_t *device_address)
2621 {
2622         int result = OAL_STATUS_SUCCESS;
2623
2624         BT_DBG("+");
2625
2626         retv_if(!device_address, BLUETOOTH_ERROR_INVALID_PARAM);
2627
2628         result = device_disconnect((bt_address_t *)device_address);
2629         if (result != OAL_STATUS_SUCCESS) {
2630                 BT_DBG("Failed to disconnect device");
2631                 return BLUETOOTH_ERROR_INTERNAL;
2632         }
2633
2634         BT_DBG("-");
2635         return BLUETOOTH_ERROR_NONE;
2636 }
2637
2638 static gboolean __bt_ignore_auto_pairing_request(const char *address)
2639 {
2640         gchar *buffer;
2641         char **lines;
2642         int i;
2643         char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
2644         char *temp_buffer;
2645         FILE *fp;
2646         long size;
2647         size_t result;
2648
2649         BT_DBG("+\n");
2650
2651         if (address == NULL)
2652                 return FALSE;
2653
2654         /* Get the LAP(Lower Address part) */
2655         /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
2656            a "," */
2657         snprintf(lap_address, sizeof(lap_address), ",%s", address);
2658
2659         fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
2660
2661         if (fp == NULL) {
2662                 BT_ERR("fopen failed \n");
2663                 return FALSE;
2664         }
2665
2666         fseek(fp, 0, SEEK_END);
2667         size = ftell(fp);
2668         rewind(fp);
2669
2670         if (size < 0) {
2671                 BT_ERR("Get file size failed \n");
2672                 fclose(fp);
2673                 return FALSE;
2674         }
2675
2676         buffer = g_malloc0(sizeof(char) * size);
2677         result = fread((char *)buffer, 1, size, fp);
2678         fclose(fp);
2679         if (result != size) {
2680                 BT_ERR("Read Error\n");
2681                 g_free(buffer);
2682                 return FALSE;
2683         }
2684
2685         lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
2686         g_free(buffer);
2687
2688         if (lines == NULL)
2689                 return FALSE;
2690
2691         /* Write the data and insert new device data */
2692         for (i = 0; lines[i] != NULL; i++) {
2693                 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
2694                         temp_buffer = g_strconcat(lines[i], lap_address, NULL);
2695                         g_free(lines[i]);
2696                         lines[i] = temp_buffer;
2697                 }
2698         }
2699         buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
2700         g_strfreev(lines);
2701         /* Fix : NULL_RETURNS */
2702         retv_if(buffer == NULL, FALSE);
2703
2704         fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
2705
2706         if (fp == NULL) {
2707                 BT_ERR("fopen failed \n");
2708                 g_free(buffer);
2709                 return FALSE;
2710         }
2711
2712         BT_DBG("Buffer = %s\n", buffer);
2713         fwrite(buffer, 1, strlen(buffer), fp);
2714         fclose(fp);
2715
2716         g_free(buffer);
2717
2718         BT_DBG("-\n");
2719
2720         return FALSE;
2721 }
2722 #endif