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