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