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