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