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