Simplify the logic to manage invocation list
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / device / bt-service-core-device.c
1 /*
2  * Copyright (c) 2015 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Anupam Roy <anupam.r@samsung.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *              http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <gio/gio.h>
22 #include <glib.h>
23 #include <dlog.h>
24 #include <string.h>
25 #include <vconf.h>
26 #include <vconf-internal-keys.h>
27 #include <bundle.h>
28 #include <bundle_internal.h>
29 #include <eventsystem.h>
30
31 /*bt-service headers */
32 #include "bt-internal-types.h"
33 #include "bt-service-common.h"
34 #include "bt-service-util.h"
35 #include "bt-service-main.h"
36 #include "bt-service-core-device.h"
37 #include "bt-service-core-adapter.h"
38 #include "bt-service-event-receiver.h"
39 #include "bt-request-handler.h"
40 #include "bt-service-event.h"
41 #include "bt-service-agent-util.h"
42 #include "bt-service-a2dp-src.h"
43 #include "bt-service-a2dp-sink.h"
44 #ifdef TIZEN_FEATURE_BT_OBEX
45 #include "bt-service-obex-server.h"
46 #endif
47 #include "bt-service-device-internal.h"
48
49 #ifdef TIZEN_GATT_CLIENT
50 #include "bt-service-gatt.h"
51 #endif
52
53 /* OAL headers */
54 #include <oal-event.h>
55 #include <oal-manager.h>
56 #include <oal-adapter-mgr.h>
57 #include <oal-device-mgr.h>
58
59 #if !defined(TIZEN_PROFILE_WEARABLE) && !defined(TIZEN_PROFILE_IOT)
60 #define MAX_BOND_RETRY_COUNT 3
61 #endif
62 #define BT_PASSKEY_MAX_LENGTH 4
63
64 #define BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT    6000    /* msec */
65
66 #define BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL  30      /* msec */
67 #define BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL  50      /* msec */
68 #define BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY 0       /* event */
69
70 #define BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL       10      /* msec */
71 #define BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL       30      /* msec */
72 #define BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY      0       /* event */
73
74 #define BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL         80      /* msec */
75 #define BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL         100     /* msec */
76 #define BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY        2       /* event */
77
78 /* Bonding Info structure */
79 typedef struct {
80         int result;
81         char *addr;
82         gboolean is_autopair;
83         unsigned short conn_type;
84         gboolean is_cancelled_by_user;
85         gboolean is_device_creating;
86         bluetooth_device_address_t *dev_addr;
87         bt_remote_dev_info_t *dev_info;
88 } bt_bond_data_t;
89
90 /* Searching Info structure */
91 typedef struct {
92         int result;
93         char *addr;
94         gboolean is_cancelled_by_user;
95         bluetooth_device_address_t *dev_addr;
96         bt_remote_dev_info_t *dev_info;
97 } bt_service_search_info_data_t;
98
99 /* Pairing Info structure */
100 typedef struct {
101         char *addr;
102         gboolean is_autopair;
103         int is_ssp;
104 } bt_pairing_data_t;
105
106 typedef struct {
107         char addr[BT_ADDRESS_STRING_SIZE];
108         bt_remote_dev_info_t *dev_info;
109 } bt_incoming_bond_data_t;
110
111 /* Bonding and Pairing Informations */
112 bt_bond_data_t *trigger_bond_info;
113 bt_bond_data_t *trigger_unbond_info;
114 bt_pairing_data_t *trigger_pairing_info;
115 bt_service_search_info_data_t *service_search_info;
116
117 bt_incoming_bond_data_t *incoming_bond_info;
118
119 typedef enum {
120         BT_DEVICE_BOND_STATE_NONE,
121         BT_DEVICE_BOND_STATE_CANCEL_DISCOVERY,
122         BT_DEVICE_BOND_STATE_DISCOVERY_CANCELLED,
123         BT_DEVICE_BOND_STATE_REMOVE_BONDING,
124         BT_DEVICE_BOND_STATE_REMOVED_BONDING,
125         BT_DEVICE_BOND_STATE_STARTED,
126         BT_DEVICE_BOND_STATE_WAIT_PROP,
127         BT_DEVICE_BOND_STATE_WAIT_DID
128 } bt_bond_state_e;
129
130 typedef enum {
131         BT_DEVICE_BOND_INFO,
132         BT_DEVICE_INCOMING_BOND_INFO,
133         BT_DEVICE_UNBOND_INFO
134 } bt_bond_info_e;
135
136 /* BT device bond state variable */
137 static bt_bond_state_e bt_device_bond_state;
138 #if !defined(TIZEN_PROFILE_WEARABLE) && !defined(TIZEN_PROFILE_IOT)
139 static int bond_retry_count;
140 #endif
141
142 static char *passkey_watcher;
143 static GSList *pin_info_list = NULL;
144
145 /** DBFW+ Event Code */
146 typedef enum {
147         BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_INFO = 0x10,
148         BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_CLOCK_INFO = 0x11,
149         BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_PAGE_SLOTS = 0x12,
150         BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_LMP_TRACE = 0x13,
151         BT_DBFW_PLUS_EVENT_CODE_A2DP_INFO = 0x20,
152         BT_DBFW_PLUS_EVENT_CODE_HFP_INFO = 0x31,
153         BT_DBFW_PLUS_EVENT_CODE_HFP_SCO_PACKET_TYPE_INFO = 0x32,
154 } bt_dbfw_plus_event_code_t;
155
156 static int dbfw_rssi;
157
158 #ifdef TIZEN_GATT_CLIENT
159 typedef struct {
160         char *address;
161         float interval_min;
162         float interval_max;
163         GSList *senders;
164 } bt_connected_le_dev_t;
165
166 typedef struct {
167         char *sender;
168         float interval_min;
169         float interval_max;
170         guint16 latency;
171         guint16 time_out;
172         float key;
173 } bt_le_conn_param_t;
174
175 static GSList *le_connected_dev_list = NULL;
176
177 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
178 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
179 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
180 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
181 #define BT_LE_CONN_SLAVE_LATENCY_MAX 499
182 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
183 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
184
185 static void _bt_handle_le_connected_dev_info(const char *address, gboolean connected);
186 #endif
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_d = 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_d);
608
609         switch (service_d) {
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_d);
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_d);
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                 BT_DBG("Incoming HSP_SERVICE_ID conn Req from device addr [%s]", address);
650                 break;
651         case HFP_SERVICE_ID:
652                 BT_DBG("Incoming HFP_SERVICE_ID conn Req from device addr [%s]", address);
653                 break;
654         case SAP_SERVICE_ID:
655                 BT_DBG("Incoming SAP_SERVICE_ID conn Req from device addr [%s]", address);
656                 break;
657         case HSP_HS_SERVICE_ID:
658                 BT_DBG("Incoming HSP_HS_SERVICE_ID conn Req from device addr [%s]", address);
659                 break;
660         case HFP_HS_SERVICE_ID:
661                 BT_DBG("Incoming HFP_HS_SERVICE_ID conn Req from device addr [%s]", address);
662                 break;
663 #ifdef TIZEN_BT_HAL
664         case IOTIVITY_SERVICE_ID:
665                 BT_DBG("Incoming IOTIVITY_SERVICE_ID conn Req from device addr [%s]", address);
666                 break;
667 #endif
668         default:
669                 /* For now, reject authorization for any service apart from above switch cases */
670                 BT_DBG("Incoming Profile conn req with service ID [%d] from device addr [%s]", service_d, address);
671                 res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, FALSE, FALSE);
672                 if (res != OAL_STATUS_SUCCESS)
673                         BT_ERR("authorize_response: %d", res);
674                 return;
675         }
676
677         /* Auto accept authorization request for HID, A2DP and AVRCP profiles */
678         BT_INFO("Auto Accept authorization");
679         res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, TRUE, FALSE);
680         if (res != OAL_STATUS_SUCCESS)
681                 BT_ERR("authorize_response: %d", res);
682 }
683
684 static void __bt_handle_ongoing_bond(bt_remote_dev_info_t *remote_dev_info, gboolean incoming_bond)
685 {
686         GVariant *param = NULL;
687
688         if ((remote_dev_info->name || remote_dev_info->alias)
689                         && remote_dev_info->address
690                         && remote_dev_info->uuids) {
691                 GVariant *uuids = NULL;
692                 GVariantBuilder *builder = NULL;
693                 GVariant *manufacturer_data;
694                 unsigned int i = 0;
695                 char *name = NULL;
696
697                 if (remote_dev_info->alias)
698                         name = remote_dev_info->alias;
699                 else
700                         name = remote_dev_info->name;
701
702                 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
703                 for (i = 0; i < remote_dev_info->uuid_count; i++) {
704                         g_variant_builder_add(builder, "s",
705                                         remote_dev_info->uuids[i]);
706                 }
707                 uuids = g_variant_new("as", builder);
708                 g_variant_builder_unref(builder);
709                 manufacturer_data = g_variant_new_from_data((const GVariantType *)"ay",
710                                 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
711                                 TRUE, NULL, NULL);
712
713                 param = g_variant_new("(isunsbub@asn@ay)",
714                                 BLUETOOTH_ERROR_NONE,
715                                 remote_dev_info->address,
716                                 remote_dev_info->class,
717                                 remote_dev_info->rssi,
718                                 name,
719                                 remote_dev_info->paired,
720                                 remote_dev_info->connected,
721                                 remote_dev_info->trust,
722                                 uuids,
723                                 remote_dev_info->manufacturer_data_len,
724                                 manufacturer_data);
725                 /* Send the event to application */
726                 _bt_send_event(BT_ADAPTER_EVENT,
727                                 BLUETOOTH_EVENT_BONDING_FINISHED,
728                                 param);
729                 BT_PERMANENT_LOG("Paired %s", remote_dev_info->address + 12);
730                 BT_INFO_C("### Paired %s", remote_dev_info->address + 12);
731                 if (incoming_bond) {
732                         __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
733                 } else {
734                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
735                         __bt_free_pairing_info(&trigger_pairing_info);
736                 }
737         } else {
738                 BT_INFO("Lets wait for more remote device properties");
739         }
740 }
741
742 static void __handle_incoming_bond_created_event(bt_address_t *bd_addr)
743 {
744         char address[BT_ADDRESS_STRING_SIZE];
745         bluetooth_device_address_t dev_addr;
746
747         /*
748          * BlueZ sends paired signal for each paired device, during activation,
749          * We should ignore this, otherwise application thinks that a new device
750          * got paired
751          */
752         if (_bt_adapter_get_status() != BT_ACTIVATED) {
753                 BT_DBG("BT is not activated, so ignore this");
754                 return;
755         }
756
757         _bt_convert_addr_type_to_string(address, bd_addr->addr);
758         if (!incoming_bond_info) {
759                 incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
760         } else {
761                 if (g_strcmp0(incoming_bond_info->addr, address)) {
762                         BT_DBG("Previous Bond address: [%s] differs from new address: [%s]",
763                                         address, incoming_bond_info->addr);
764                         __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
765                         incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
766                 }
767         }
768
769         BT_INFO("Incoming bond successfully completed");
770         g_strlcpy(incoming_bond_info->addr, address, BT_ADDRESS_STRING_SIZE);
771         incoming_bond_info->dev_info = NULL;
772
773         _bt_convert_addr_string_to_type(dev_addr.addr, incoming_bond_info->addr);
774         if (BLUETOOTH_ERROR_NONE == _bt_device_get_bonded_device_info(&dev_addr)) {
775                 BT_DBG("Bonded device info query posted to stack successfully");
776         } else {
777                 BT_ERR("Bonded device info query failed");
778                 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
779         }
780
781 }
782
783 static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr)
784 {
785         gchar address[BT_ADDRESS_STR_LEN];
786         bluetooth_device_address_t dev_addr;
787
788         if (trigger_bond_info == NULL) {
789                 /* Send reply */
790                 BT_DBG("trigger_bond_info == NULL, Handle incomming bond event");
791                 __handle_incoming_bond_created_event(bd_addr);
792                 return;
793         }
794
795         _bt_convert_addr_type_to_string(address, bd_addr->addr);
796         if (g_strcmp0(trigger_bond_info->addr, address)) {
797                 BT_DBG("Bonding address= [%s] is different from requested address =[%s]",
798                                 address, trigger_bond_info->addr);
799                 __handle_incoming_bond_created_event(bd_addr);
800                 return;
801         }
802
803         /* Bonding state will be cleaned up & BONDING FINISHED EVENT
804            will be sent only when Properties are fetched from stack
805            Till that time lets not free trigger_bond_info.
806            However it is possible that while fetching device properties, internal
807            stack error can occur which can lead to no valid properties or
808            no properties at all. So in such cases, we must not wait for properties,
809            otherwise, it can lead to infinite wait  */
810         _bt_convert_addr_string_to_type(dev_addr.addr,
811                         trigger_bond_info->addr);
812
813         if (_bt_device_get_bonded_device_info(&dev_addr) == BLUETOOTH_ERROR_NONE) {
814                 BT_DBG("Bonded device info query posted to stack successfully");
815                 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_BOND_DEVICE,
816                                 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
817         } else {
818                 BT_DBG("Possibly internal stack error in bonded device info query, perform cleanup");
819                 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
820                                 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
821                 /* Destroy if at all device got bonded at stack level */
822                 device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
823
824                 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
825                 __bt_free_pairing_info(&trigger_pairing_info);
826         }
827 }
828
829 /**********************************************************************************************
830 *  Bond removal event can be triggered for following reasons -
831 *  a. If Bonding procedure if failed (for Auth failed, Page timeout, cancelled by user etc)
832 *  b. If Application requests for explicitly removing the bond
833 *  c. When application attempt to create bond,bond is removed first which triggers this event
834 *     c. is in-line with Bluedroid bond create\emoval architecture
835 *********************************************************************************************/
836 static void __bt_device_handle_bond_removal_event(bt_address_t *bd_addr)
837 {
838         char address[BT_ADDRESS_STRING_SIZE];
839
840         _bt_convert_addr_type_to_string(address, bd_addr->addr);
841         _bt_service_remove_device_from_bonded_list(address);
842
843         if (trigger_unbond_info) {
844                 BT_DBG("Bond removal request successfully handled, return DBUS and send event");
845                 GVariant *param = NULL;
846                 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_UNBOND_DEVICE,
847                                 trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
848                 param = g_variant_new("(is)", BLUETOOTH_ERROR_NONE, trigger_unbond_info->addr);
849                 _bt_send_event(BT_ADAPTER_EVENT,
850                                 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
851                                 param);
852                 BT_PERMANENT_LOG("Unpaired %s", trigger_unbond_info->addr + 12);
853                 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
854                 __bt_free_pairing_info(&trigger_pairing_info);
855         } else if (trigger_bond_info) {
856                 BT_ERR("Bonding was removed");
857                 __bt_device_handle_bond_state();
858         }
859 }
860
861 static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_fail_event)
862 {
863         BT_INFO("+");
864         oal_status_t status = bond_fail_event->status;
865         BT_INFO("Bonding failed, reason: %d", status);
866
867         switch (status) {
868         case OAL_STATUS_RMT_DEVICE_DOWN:
869         {
870                 if (trigger_bond_info) {
871 #if !defined(TIZEN_PROFILE_WEARABLE) && !defined(TIZEN_PROFILE_IOT)
872                         BT_INFO("OAL_STATUS_RMT_DEVICE_DOWN:Lets retry bonding!! retry count [%d]",
873                                         bond_retry_count);
874                         int ret = OAL_STATUS_SUCCESS;
875                         if (bond_retry_count < MAX_BOND_RETRY_COUNT) {
876                                 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
877                                 if (ret != OAL_STATUS_SUCCESS) {
878                                         BT_ERR("Create Bond procedure could not suceed");
879                                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
880                                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
881                                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
882                                         __bt_free_pairing_info(&trigger_pairing_info);
883                                         bond_retry_count = 0;
884                                 } else
885                                         bond_retry_count++;
886                         } else {
887 #endif
888                                 BT_ERR("Create Bond failed MAX_BOND_RETRY_COUNT TIMES!!");
889                                 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
890                                                 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
891                                 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
892                                 __bt_free_pairing_info(&trigger_pairing_info);
893 #if !defined(TIZEN_PROFILE_WEARABLE) && !defined(TIZEN_PROFILE_IOT)
894                                 bond_retry_count = 0;
895                         }
896 #endif
897                 }
898                 break;
899         }
900         case OAL_STATUS_AUTH_FAILED:
901         {
902                 /*TODO Auto pairing status set & ignore auto pairing logics can be done at this point.
903                   To be considered later*/
904                 int result = BLUETOOTH_ERROR_INTERNAL;
905                 BT_INFO("BT_OPERATION_STATUS_AUTH_FAILED");
906
907                 BT_INFO("add device in pairing black list");
908                 _bt_set_autopair_status_in_bonding_info(FALSE);
909
910                 if (trigger_bond_info) {
911                         __bt_ignore_auto_pairing_request(trigger_bond_info->addr);
912                         BT_ERR("Create Bond procedure could not suceed, check if cancelled by User");
913                         if (trigger_bond_info->is_cancelled_by_user) {
914                                 BT_ERR("Bonding is cancelled by user");
915                                 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
916                         }
917                         __bt_device_handle_pending_requests(result, BT_BOND_DEVICE,
918                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
919                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
920                 }
921
922                 __bt_free_pairing_info(&trigger_pairing_info);
923                 break;
924         }
925         case OAL_STATUS_ALREADY_CONNECT:
926         {
927                 int result = BLUETOOTH_ERROR_ALREADY_CONNECT;
928                 BT_INFO("OAL_STATUS_ALREADY_CONNECT");
929
930                 if (trigger_bond_info) {
931                         __bt_device_handle_pending_requests(result, BT_BOND_DEVICE,
932                                 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
933                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
934                 }
935                 break;
936         }
937         case OAL_STATUS_INTERNAL_ERROR:
938         {
939                 BT_INFO("OAL_STATUS_INTERNAL_ERROR");
940                 if (trigger_unbond_info) {
941                         BT_INFO("Bond removal request failed, return DBUS and send event");
942                         GVariant *param = NULL;
943                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_UNBOND_DEVICE,
944                                         trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
945                         param = g_variant_new("(is)", BLUETOOTH_ERROR_INTERNAL, trigger_unbond_info->addr);
946                         _bt_send_event(BT_ADAPTER_EVENT,
947                                         BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
948                                         param);
949                         __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
950                 } else if (trigger_bond_info) {
951                         if (__bt_device_handle_bond_state() != BLUETOOTH_ERROR_NONE) {
952                                 if (trigger_bond_info) {
953                                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
954                                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
955                                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
956                                 }
957                         }
958                 }
959
960                 __bt_free_pairing_info(&trigger_pairing_info);
961                 break;
962         }
963         default:
964         {
965                 BT_ERR("Unknown status of Bond failed event status [%d]", status);
966                 break;
967         }
968
969         }
970         BT_INFO("-");
971 }
972
973 static gboolean __bt_device_init_dbfw_rssi_cb(gpointer user_data)
974 {
975         dbfw_rssi = 0;
976         return FALSE;
977 }
978
979 static void __bt_device_parse_dbfw_set_rssi(unsigned char *data, uint32_t length)
980 {
981         ret_if(length < 5);
982         dbfw_rssi = 0xFFFFFF00 | data[4];
983         BT_INFO("dbfw_rssi = %d", dbfw_rssi);
984         g_timeout_add(1000, __bt_device_init_dbfw_rssi_cb, NULL);
985 }
986
987 static void __bt_device_dbfw_plus_info_callback(event_dev_dbfw_plus_info_t *dbfw_info)
988 {
989         char evt_str[18];
990         char *data_str = NULL;
991         int i;
992
993         data_str = g_malloc0(dbfw_info->length * 2 + 1);
994         for (i = 0; i < dbfw_info->length; i++)
995                 snprintf(&data_str[i * 2], 3, "%02X", dbfw_info->data[i]);
996         data_str[i * 2] = '\0';
997
998         switch(dbfw_info->event_code) {
999                 case BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_INFO:
1000                         BT_DBG("## Event Code: Linkloss Debug Info");
1001                         snprintf(evt_str, 18, "[DBFW]Linkloss(D)");
1002                         __bt_device_parse_dbfw_set_rssi(dbfw_info->data, dbfw_info->length);
1003                         break;
1004                 case BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_CLOCK_INFO:
1005                         BT_DBG("## Event Code: Linkloss Clock Info");
1006                         snprintf(evt_str, 18, "[DBFW]Linkloss(C)");
1007                         break;
1008                 case BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_PAGE_SLOTS:
1009                         BT_DBG("## Event Code: Linkloss Page slots");
1010                         snprintf(evt_str, 18, "[DBFW]Linkloss(P)");
1011                         break;
1012                 case BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_LMP_TRACE:
1013                         BT_DBG("## Event Code: Linkloss LMP trace");
1014                         snprintf(evt_str, 18, "[DBFW]Linkloss(L)");
1015                         break;
1016                 case BT_DBFW_PLUS_EVENT_CODE_A2DP_INFO:
1017                         BT_DBG("## Event Code: A2DP Info");
1018                         snprintf(evt_str, 18, "[DBFW]A2DP");
1019                         break;
1020                 case BT_DBFW_PLUS_EVENT_CODE_HFP_INFO:
1021                         BT_DBG("## Event Code: HFP Info");
1022                         snprintf(evt_str, 18, "[DBFW]HFP");
1023                         break;
1024                 case BT_DBFW_PLUS_EVENT_CODE_HFP_SCO_PACKET_TYPE_INFO:
1025                         BT_DBG("## Event Code: HFP SCO Packet Type");
1026                         snprintf(evt_str, 18, "[DBFW]SCO");
1027                         break;
1028                 default:
1029                         BT_DBG("## Unknown event code (0x%02x)", dbfw_info->event_code);
1030                         snprintf(evt_str, 18, "[DBFW]0x%02X", dbfw_info->event_code);
1031                         break;
1032         }
1033
1034         BT_PERMANENT_LOG("%s 0x%s", evt_str, data_str);
1035         g_free(data_str);
1036 }
1037
1038 static void __bt_device_event_handler(int event_type, gpointer event_data)
1039 {
1040         int eventcheck = OAL_EVENT_DEVICE_PROPERTIES;
1041         BT_DBG("event [%d] Event check = [%d]", event_type, eventcheck);
1042
1043         switch (event_type) {
1044         case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY: {
1045                 BT_INFO("BREDR Device Found");
1046                 __bt_device_remote_device_found_callback(event_data, FALSE);
1047                 break;
1048         }
1049         case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE: {
1050                 BT_INFO("Dual Device Found");
1051                 __bt_device_remote_device_found_callback(event_data, FALSE);
1052                 break;
1053         }
1054         case OAL_EVENT_DEVICE_PROPERTIES: {
1055                 BT_INFO("Remote Device properties Received");
1056                 __bt_device_remote_properties_callback((event_dev_properties_t *)event_data);
1057                 break;
1058         }
1059         case OAL_EVENT_DEVICE_BONDING_SUCCESS: {
1060                 BT_INFO("Bonding Success event Received");
1061                 __bt_device_handle_bond_completion_event((bt_address_t *)event_data);
1062                 break;
1063         }
1064         case OAL_EVENT_DEVICE_BONDING_REMOVED: {
1065                 BT_INFO("Bonding Removed event Received");
1066                 __bt_device_handle_bond_removal_event((bt_address_t *)event_data);
1067                 break;
1068         }
1069         case OAL_EVENT_DEVICE_BONDING_FAILED: {
1070                 BT_INFO("Bonding Failed event Received");
1071                 __bt_device_handle_bond_failed_event((event_dev_bond_failed_t*) event_data);
1072                 break;
1073         }
1074         case OAL_EVENT_DEVICE_ACL_CONNECTED: {
1075                 BT_DBG("ACL Connected event Received");
1076                 __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, TRUE, 0);
1077                 break;
1078         }
1079         case OAL_EVENT_DEVICE_ACL_DISCONNECTED: {
1080                 BT_DBG("ACL Disconnected event Received");
1081                 __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 0);
1082                 break;
1083         }
1084         case OAL_EVENT_DEVICE_LE_CONNECTED: {
1085                 BT_DBG("LE Connected event Received");
1086                 __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, TRUE, 1);
1087                 break;
1088         }
1089         case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
1090                 BT_DBG("LE Disconnected event Received");
1091                 __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 1);
1092                 break;
1093         }
1094         case OAL_EVENT_DEVICE_PIN_REQUEST: {
1095                 BT_INFO("PIN Request Received");
1096                 __bt_device_pin_request_callback((remote_device_t*)event_data);
1097                 break;
1098         }
1099         case OAL_EVENT_DEVICE_PASSKEY_ENTRY_REQUEST: {
1100                 BT_INFO("Passkey Entry request Received");
1101                 __bt_device_ssp_passkey_entry_callback((remote_device_t*)event_data);
1102                 break;
1103         }
1104         case OAL_EVENT_DEVICE_PASSKEY_CONFIRMATION_REQUEST:{
1105                 BT_INFO("Passkey Confirmation Request Received");
1106                 __bt_device_ssp_passkey_confirmation_callback((event_dev_passkey_t *)event_data);
1107                 break;
1108         }
1109         case OAL_EVENT_DEVICE_PASSKEY_DISPLAY: {
1110                 BT_INFO("Passkey Display Request Received");
1111                 __bt_device_ssp_passkey_display_callback((event_dev_passkey_t *)event_data);
1112                 break;
1113         }
1114         case OAL_EVENT_DEVICE_SSP_CONSENT_REQUEST: {
1115                 BT_INFO("SSP Consent Request Received");
1116                  __bt_device_ssp_consent_callback((remote_device_t*)event_data);
1117                 break;
1118         }
1119         case OAL_EVENT_DEVICE_SERVICES: {
1120                 BT_INFO("Remote Device Services Received");
1121                 __bt_device_services_callback((event_dev_services_t*)event_data);
1122                 break;
1123         }
1124         case OAL_EVENT_DEVICE_AUTHORIZE_REQUEST: {
1125                 BT_INFO("Remote Device Authorization Request");
1126                 __bt_device_authorization_request_callback((event_dev_authorize_req_t*)event_data);
1127                 break;
1128         }
1129         case OAL_EVENT_DEVICE_TRUSTED: {
1130                 BT_INFO("Remote Device Trusted");
1131                 __bt_device_trusted_callback(TRUE, (event_dev_trust_t*)event_data);
1132                 break;
1133         }
1134         case OAL_EVENT_DEVICE_UNTRUSTED: {
1135                 BT_INFO("Remote Device UnTrusted");
1136                 __bt_device_trusted_callback(FALSE, (event_dev_trust_t*)event_data);
1137                 break;
1138         }
1139         case OAL_EVENT_DEVICE_NAME: {
1140                 remote_device_t *rem_dev = event_data;
1141                 gchar address[BT_ADDRESS_STR_LEN];
1142
1143                 _bt_convert_addr_type_to_string(address, rem_dev->address.addr);
1144                 BT_INFO("Remote Device name Received");
1145                 BT_INFO("Name: %s, Address: %s", rem_dev->name, address);
1146
1147                 /* Update local cache */
1148                 _bt_update_remote_dev_property(address, DEV_PROP_NAME, (void *)rem_dev->name);
1149                 break;
1150         }
1151         case OAL_EVENT_DEVICE_TRUSTED_PROFILES_CHANGED: {
1152                 event_device_trusted_profiles_t *ev = event_data;
1153                 char address[BT_ADDRESS_STRING_SIZE];
1154
1155                 ret_if(NULL == ev);
1156
1157                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1158                 _bt_send_event(BT_DEVICE_EVENT,
1159                                 BLUETOOTH_EVENT_SUPPORTED_PROFILE_TRUSTED,
1160                                 g_variant_new("(isi)", BLUETOOTH_ERROR_NONE,
1161                                         address, ev->trust_val));
1162                 break;
1163         }
1164         case OAL_EVENT_RSSI_MONITORING_ENABLED: {
1165                 event_dev_rssi_info_t *ev = event_data;
1166                 char address[BT_ADDRESS_STRING_SIZE];
1167                 GVariant *param;
1168
1169                 ret_if(NULL == ev);
1170
1171                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1172                 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1173                                 address, ev->link_type, TRUE);
1174                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1175                 break;
1176         }
1177         case OAL_EVENT_RSSI_MONITORING_DISABLED: {
1178                 event_dev_rssi_info_t *ev = event_data;
1179                 char address[BT_ADDRESS_STRING_SIZE];
1180                 GVariant *param;
1181
1182                 ret_if(NULL == ev);
1183
1184                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1185                 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1186                                 address, ev->link_type, FALSE);
1187                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1188                 break;
1189         }
1190         case OAL_EVENT_RSSI_ALERT_RECEIVED: {
1191                 event_dev_rssi_info_t *ev = event_data;
1192                 char address[BT_ADDRESS_STRING_SIZE];
1193                 GVariant *param;
1194
1195                 ret_if(NULL == ev);
1196
1197                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1198                 param = g_variant_new("(isiii)", BLUETOOTH_ERROR_NONE,
1199                                 address, ev->link_type, ev->alert_type, ev->rssi);
1200                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ALERT, param);
1201                 break;
1202         }
1203         case OAL_EVENT_RAW_RSSI_RECEIVED: {
1204                 event_dev_rssi_info_t *ev = event_data;
1205                 char address[BT_ADDRESS_STRING_SIZE];
1206                 GVariant *param;
1207
1208                 ret_if(NULL == ev);
1209
1210                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1211                 param = g_variant_new("(isii)", BLUETOOTH_ERROR_NONE,
1212                                 address, ev->link_type, ev->rssi);
1213                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RAW_RSSI, param);
1214                 break;
1215         }
1216         case OAL_EVENT_DEVICE_DBFW_PLUS_INFO: {
1217                 __bt_device_dbfw_plus_info_callback((event_dev_dbfw_plus_info_t*)event_data);
1218                 break;
1219         }
1220         default:
1221                 BT_INFO("Unhandled event..");
1222         }
1223 }
1224
1225 /* Legacy Pairing event handler */
1226 static void __bt_device_pin_request_callback(remote_device_t* pin_req_event)
1227 {
1228         GVariant *param;
1229         char address[BT_ADDRESS_STRING_SIZE];
1230         char pin_code[BLUETOOTH_PIN_CODE_MAX_LENGTH + 1];
1231         bool incoming = false;
1232         BT_DBG("+");
1233
1234         _bt_convert_addr_type_to_string(address, pin_req_event->address.addr);
1235
1236         BT_INFO("Address[%s]", address);
1237         BT_INFO("Name[%s]", pin_req_event->name);
1238         BT_INFO("COD[%d]", pin_req_event->cod);
1239
1240         if (trigger_pairing_info) {
1241                 /* BTAPI support only one pairing at a time */
1242                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1243                 BT_ERR("New PIN request address [%s]", address);
1244                 device_reject_pin_request(&pin_req_event->address);
1245                 return;
1246         }
1247
1248         /* If user initiated bonding and auto response is possible, just reply with default 0000*/
1249         if (_bt_is_bonding_device_address(address) == TRUE &&
1250                         _bt_agent_is_auto_response(pin_req_event->cod, address, pin_req_event->name)) {
1251                 /* Note: Currently even if SYSPOPUP is supported, we use Fixed PIN "0000" for basic pairing
1252                    as BT SYSPOPUP is currently not working for PIN KEY entry in Tizen platform. This needs
1253                    to be checked and fixed apropriately */
1254                 _bt_set_autopair_status_in_bonding_info(TRUE);
1255                 device_accept_pin_request(&pin_req_event->address, "0000");
1256         } else if (_bt_agent_is_hid_keyboard(pin_req_event->cod)) {
1257                 BT_DBG("Remote Device is HID keyboard Type..");
1258                 char str_passkey[BT_PASSKEY_MAX_LENGTH + 1] = { 0 };
1259
1260                 if (_bt_agent_generate_passkey(str_passkey,
1261                                         BT_PASSKEY_MAX_LENGTH) != 0) {
1262                         device_reject_pin_request(&pin_req_event->address);
1263                         goto done;
1264                 }
1265                 device_accept_pin_request(&pin_req_event->address, str_passkey);
1266
1267                 BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1268
1269                 if(trigger_bond_info == NULL)
1270                         incoming = true;
1271
1272                 param = g_variant_new("(bsss)", incoming, address, pin_req_event->name, str_passkey);
1273                 _bt_send_event(BT_ADAPTER_EVENT,
1274                                 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1275                 BT_DBG("Key board pairing in process");
1276         } else if (BLUETOOTH_ERROR_NONE == __bt_get_device_pin_code(address, pin_code)) {
1277                 BT_DBG("Use stored PIN code [%s]", pin_code);
1278                 device_accept_pin_request(&pin_req_event->address, pin_code);
1279         } else {
1280                 if (_bt_is_bonding_device_address(address) == TRUE) {
1281                         BT_DBG("Show Pin entry");
1282                         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1283                         trigger_pairing_info->addr = g_strdup(address);
1284                         trigger_pairing_info->is_ssp = FALSE;
1285
1286                         BT_DBG("Send BLUETOOTH_EVENT_PIN_REQUEST");
1287
1288                         if(trigger_bond_info == NULL)
1289                                 incoming = true;
1290
1291                         param = g_variant_new("(bss)", incoming, address, pin_req_event->name);
1292                         _bt_send_event(BT_ADAPTER_EVENT,
1293                                         BLUETOOTH_EVENT_PIN_REQUEST, param);
1294                 }
1295         }
1296
1297 done:
1298         _bt_agent_release_memory();
1299         BT_DBG("-");
1300 }
1301
1302 /* SSP Pairing event handler */
1303 static void __bt_device_ssp_passkey_entry_callback(remote_device_t* dev_info)
1304 {
1305         GVariant *param;
1306         gchar address[BT_ADDRESS_STR_LEN];
1307         char *p_addr;
1308         gchar *name;
1309         bool incoming = false;
1310
1311         BT_DBG("+");
1312
1313         _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1314         p_addr = address;
1315         name = dev_info->name;
1316
1317         BT_INFO("Address[%s]", address);
1318         BT_INFO("Name[%s]", name);
1319         BT_INFO("COD[%d]", dev_info->cod);
1320
1321         if (trigger_pairing_info) {
1322                 /* BTAPI support only one pairing at a time */
1323                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1324                 BT_ERR("New PIN request address [%s]", address);
1325                 device_reject_pin_request(&dev_info->address);
1326                 BT_DBG("-");
1327                 return;
1328         }
1329
1330         /* Set pairing data */
1331         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1332         trigger_pairing_info->addr = g_strdup(address);
1333         trigger_pairing_info->is_ssp = TRUE;
1334
1335         if(trigger_bond_info == NULL)
1336                 incoming = true;
1337
1338         param = g_variant_new("(bss)", incoming, p_addr, name);
1339         _bt_send_event(BT_ADAPTER_EVENT,
1340                         BLUETOOTH_EVENT_PASSKEY_REQUEST, param);
1341         BT_DBG("-");
1342 }
1343
1344 static void __bt_device_ssp_passkey_confirmation_callback(event_dev_passkey_t *dev_info)
1345 {
1346         GVariant *param;
1347         gchar address[BT_ADDRESS_STR_LEN];
1348         char *p_addr;
1349         gchar *name;
1350         char str_passkey[7];
1351         bool incoming = false; /*Stores if bonding request is incoming(true) or outgoing(false) */
1352         BT_DBG("+");
1353
1354         _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1355         p_addr = address;
1356         name = dev_info->device_info.name;
1357
1358         BT_INFO("Address[%s]", address);
1359         BT_INFO("Name[%s]", name);
1360         BT_INFO("COD[%d]", dev_info->device_info.cod);
1361
1362         if (trigger_pairing_info) {
1363                 /* BTAPI support only one pairing at a time */
1364                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1365                 BT_ERR("New PIN request address [%s]", address);
1366                 device_reject_pin_request(&dev_info->device_info.address);
1367                 BT_DBG("-");
1368                 return;
1369         }
1370
1371         /* Set pairing data */
1372         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1373         trigger_pairing_info->addr = g_strdup(address);
1374         trigger_pairing_info->is_ssp = TRUE;
1375
1376         BT_DBG("Send BLUETOOTH_EVENT_PASSKEY_CONFIRMATION");
1377         snprintf(str_passkey, sizeof(str_passkey), "%.6d", dev_info->pass_key);
1378
1379         /*Storing if bond is incoming or outgoing*/
1380         if(trigger_bond_info == NULL)
1381                 incoming = true;
1382
1383         param = g_variant_new("(bsss)", incoming, p_addr, name, str_passkey);
1384         _bt_send_event(BT_ADAPTER_EVENT,
1385                         BLUETOOTH_EVENT_PASSKEY_CONFIRM_REQUEST, param);
1386         BT_DBG("-");
1387 }
1388
1389 static void __bt_device_ssp_passkey_display_callback(event_dev_passkey_t *dev_info)
1390 {
1391         GVariant *param;
1392         gchar address[BT_ADDRESS_STR_LEN];
1393         char *p_addr;
1394         gchar *name;
1395         char str_passkey[7];
1396         bool incoming = false;
1397         BT_DBG("+");
1398
1399         _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1400         p_addr = address;
1401         name = dev_info->device_info.name;
1402
1403         BT_INFO("Address[%s]", address);
1404         BT_INFO("Name[%s]", name);
1405         BT_INFO("COD[%d]", dev_info->device_info.cod);
1406
1407         if (trigger_pairing_info) {
1408                 /* BTAPI support only one pairing at a time */
1409                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1410                 BT_ERR("New PIN request address [%s]", address);
1411                 device_reject_pin_request(&dev_info->device_info.address);
1412                 BT_DBG("-");
1413                 return;
1414         }
1415
1416         /* Set pairing data */
1417         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1418         trigger_pairing_info->addr = g_strdup(address);
1419         trigger_pairing_info->is_ssp = TRUE;
1420
1421         BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1422         snprintf(str_passkey, sizeof(str_passkey), "%06d", dev_info->pass_key);
1423
1424         if(trigger_bond_info == NULL)
1425                 incoming = true;
1426
1427         param = g_variant_new("(bsss)", incoming, p_addr, name, str_passkey);
1428
1429         if (passkey_watcher) {
1430                 BT_INFO("Send passkey to %s", passkey_watcher);
1431                 _bt_send_event_to_dest(passkey_watcher, BT_ADAPTER_EVENT,
1432                                 BLUETOOTH_EVENT_PASSKEY_NOTIFICATION, param);
1433         } else {
1434                 _bt_send_event(BT_ADAPTER_EVENT,
1435                                 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1436         }
1437         BT_DBG("-");
1438 }
1439
1440 static void __bt_device_ssp_consent_callback(remote_device_t* dev_info)
1441 {
1442         gchar address[BT_ADDRESS_STR_LEN];
1443         gchar *name;
1444         int local_major;
1445         int local_minor;
1446         int cod;
1447         BT_DBG("+");
1448
1449         _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1450         name = dev_info->name;
1451         cod = dev_info->cod;
1452
1453         BT_INFO("Address[%s]", address);
1454         BT_INFO("Name[%s]", name);
1455         BT_INFO("COD[%d]", cod);
1456
1457         if (trigger_pairing_info) {
1458                 /* BTAPI support only one pairing at a time */
1459                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1460                 BT_ERR("New PIN request address [%s]", address);
1461                 device_reject_pin_request(&dev_info->address);
1462                 BT_DBG("-");
1463                 return;
1464         }
1465
1466         /* Set pairing data */
1467         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1468         trigger_pairing_info->addr = g_strdup(address);
1469         trigger_pairing_info->is_ssp = TRUE;
1470
1471         local_major = ((cod >> 8) & 0x001f);
1472         local_minor = (cod & 0x00fc);
1473         BT_DBG("SSP_CONSENT: Major type=[0x%x] and Minor type=[0x%x]", local_major, local_minor);
1474
1475         /*TODO: BLUETOOTH_EVENT_SSP_CONSENT_REQUEST to be handled in Tizen */
1476         BT_DBG("-");
1477 }
1478
1479 static void __bt_device_acl_state_changed_callback(event_dev_conn_status_t *acl_event,
1480                 gboolean connected, unsigned char type)
1481 {
1482         gchar address[BT_ADDRESS_STR_LEN];
1483         int disc_reason = 0;
1484         GVariant *param = NULL;
1485         bt_device_conn_info_t conn_info;
1486
1487         _bt_convert_addr_type_to_string(address, acl_event->address.addr);
1488
1489         _bt_logging_connection(connected, type);
1490
1491         disc_reason = acl_event->status;
1492
1493         if (connected) {
1494                 BT_PERMANENT_LOG("Connected %s %s", !type ? "EDR" : "LE", address + 12);
1495                 param = g_variant_new("(isy)", BLUETOOTH_ERROR_NONE, address, type);
1496                 _bt_send_event(BT_DEVICE_EVENT,
1497                                 BLUETOOTH_EVENT_DEVICE_CONNECTED,
1498                                 param);
1499         } else {
1500                 BT_PERMANENT_LOG("Disconnected %s(%d) %s", !type ? "EDR" : "LE",  disc_reason, address + 12);
1501                 param = g_variant_new("(isyi)", disc_reason, address, type, dbfw_rssi);
1502                 _bt_send_event(BT_DEVICE_EVENT,
1503                                 BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1504                                 param);
1505         }
1506
1507         conn_info.connected = connected;
1508         conn_info.type = type;
1509         /* Update local cache */
1510         _bt_update_remote_dev_property(address, DEV_PROP_CONNECTED, (void *)&conn_info);
1511
1512 #ifdef TIZEN_GATT_CLIENT
1513         /*handle LE connected device info*/
1514         if (type)
1515                 _bt_handle_le_connected_dev_info(address, connected);
1516 #endif
1517
1518 }
1519
1520 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble)
1521 {
1522         bt_remote_dev_info_t *dev_info = NULL;
1523         int result = BLUETOOTH_ERROR_NONE;
1524         GVariant *param = NULL;
1525         GVariant *uuids = NULL;
1526         GVariant *manufacturer_data = NULL;
1527         GVariantBuilder *builder = NULL;
1528         unsigned int i;
1529
1530         ret_if(_bt_is_discovering() == FALSE);
1531         ret_if(event_data == NULL);
1532
1533         dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
1534
1535         if (is_ble) {
1536                 event_ble_dev_found_t * oal_ble_dev = event_data;
1537                 BT_INFO("Device type [%d]", oal_ble_dev->device_info.type);
1538
1539                 _bt_copy_remote_dev(dev_info, &oal_ble_dev->device_info);
1540
1541                 dev_info->manufacturer_data_len = oal_ble_dev->adv_len;
1542                 if (dev_info->manufacturer_data_len)
1543                         dev_info->manufacturer_data = g_memdup(oal_ble_dev->adv_data,
1544                                         dev_info->manufacturer_data_len);
1545                 else
1546                         dev_info->manufacturer_data = NULL;
1547                 BT_DBG("----Advertising Data Length: %d", dev_info->manufacturer_data_len);
1548         } else {
1549                 event_dev_found_t * oal_dev = event_data;
1550                 _bt_copy_remote_dev(dev_info, &oal_dev->device_info);
1551         }
1552
1553         /* If Remote device name is NULL or still RNR is not done then display address as name. */
1554         if (dev_info->name == NULL)
1555                 dev_info->name = g_strdup(dev_info->address);
1556         BT_DBG("Name %s", dev_info->name);
1557
1558         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1559         for (i = 0; i < dev_info->uuid_count; i++)
1560                 g_variant_builder_add(builder, "s", dev_info->uuids[i]);
1561
1562         uuids = g_variant_new("as", builder);
1563         g_variant_builder_unref(builder);
1564
1565         manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1566                         dev_info->manufacturer_data,
1567                         dev_info->manufacturer_data_len,
1568                         TRUE,
1569                         NULL, NULL);
1570
1571         param = g_variant_new("(isunsbub@asn@ay)", result,
1572                         dev_info->address,
1573                         dev_info->class,
1574                         dev_info->rssi,
1575                         dev_info->name,
1576                         dev_info->paired,
1577                         dev_info->connected,
1578                         dev_info->trust,
1579                         uuids,
1580                         dev_info->manufacturer_data_len,
1581                         manufacturer_data);
1582
1583         _bt_send_event(BT_ADAPTER_EVENT,
1584                         BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
1585                         param);
1586
1587         _bt_free_remote_dev(dev_info);
1588 }
1589
1590 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info)
1591 {
1592         gchar address[BT_ADDRESS_STR_LEN];
1593         int result = BLUETOOTH_ERROR_NONE;
1594         GVariant *param = NULL;
1595         int event;
1596
1597         _bt_convert_addr_type_to_string(address, info->address.addr);
1598
1599         /* Update local cache */
1600         _bt_update_remote_dev_property(address, DEV_PROP_TRUST, (void *)&trusted);
1601
1602         param = g_variant_new("(is)", result, address);
1603         event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1604                 BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1605         /* Send event to application */
1606         _bt_send_event(BT_DEVICE_EVENT,
1607                         event,
1608                         param);
1609
1610 }
1611
1612 static void __bt_free_pairing_info(bt_pairing_data_t **p_info)
1613 {
1614         bt_pairing_data_t * info = *p_info;
1615         if (info) {
1616                 if (info->addr)
1617                         g_free(info->addr);
1618
1619                 g_free(info);
1620         }
1621         *p_info = NULL;
1622 }
1623
1624 static void __bt_free_bond_info(uint8_t type)
1625 {
1626         switch (type) {
1627         case BT_DEVICE_BOND_INFO:
1628                 if (!trigger_bond_info)
1629                         break;
1630
1631                 if (trigger_bond_info->addr)
1632                         g_free(trigger_bond_info->addr);
1633                 if (trigger_bond_info->dev_addr)
1634                         g_free(trigger_bond_info->dev_addr);
1635                 if (trigger_bond_info->dev_info)
1636                         _bt_free_remote_dev(trigger_bond_info->dev_info);
1637                 g_free(trigger_bond_info);
1638                 trigger_bond_info = NULL;
1639                 break;
1640         case BT_DEVICE_INCOMING_BOND_INFO:
1641                 if (!incoming_bond_info)
1642                         break;
1643
1644                 if (incoming_bond_info->dev_info)
1645                         _bt_free_remote_dev(incoming_bond_info->dev_info);
1646                 g_free(incoming_bond_info);
1647                 incoming_bond_info = NULL;
1648                 break;
1649         case BT_DEVICE_UNBOND_INFO:
1650                 if (!trigger_unbond_info)
1651                         break;
1652
1653                 if (trigger_unbond_info->addr)
1654                         g_free(trigger_unbond_info->addr);
1655                 if (trigger_unbond_info->dev_addr)
1656                         g_free(trigger_unbond_info->dev_addr);
1657                 if (trigger_unbond_info->dev_info)
1658                         _bt_free_remote_dev(trigger_unbond_info->dev_info);
1659                 g_free(trigger_unbond_info);
1660                 trigger_unbond_info = NULL;
1661                 break;
1662         }
1663 }
1664
1665 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info)
1666 {
1667         bt_service_search_info_data_t * info = *p_info;
1668         if (info) {
1669                 if (info->addr) {
1670                         g_free(info->addr);
1671                         info->addr = NULL;
1672                 }
1673
1674                 if (info->dev_addr) {
1675                         g_free(info->dev_addr);
1676                         info->dev_addr = NULL;
1677                 }
1678
1679                 if (info->dev_info) {
1680                         _bt_free_remote_dev(info->dev_info);
1681                         info->dev_info = NULL;
1682                 }
1683
1684                 g_free(info);
1685         }
1686         *p_info = NULL;
1687 }
1688
1689 static int __bt_device_handle_bond_state(void)
1690 {
1691         BT_DBG("Current Bond state: %d", bt_device_bond_state);
1692         int ret = OAL_STATUS_INTERNAL_ERROR;
1693
1694         switch (bt_device_bond_state) {
1695         case BT_DEVICE_BOND_STATE_CANCEL_DISCOVERY:
1696                 /*TODO:Bonding during discovery: Unhandled!!*/
1697                 BT_INFO("Bonding during discovery: Unhandled!!");
1698                 break;
1699         case BT_DEVICE_BOND_STATE_DISCOVERY_CANCELLED:
1700                 /*TODO:Bonding during discovery: Unhandled!!*/
1701                 BT_INFO("Bonding during discovery: Unhandled!!");
1702                 break;
1703         case BT_DEVICE_BOND_STATE_REMOVE_BONDING:
1704                 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVED_BONDING;
1705                 ret = device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
1706                 if (ret != OAL_STATUS_SUCCESS)
1707                         ret = __bt_device_handle_bond_state();
1708                 break;
1709         case BT_DEVICE_BOND_STATE_REMOVED_BONDING:
1710                 bt_device_bond_state = BT_DEVICE_BOND_STATE_NONE;
1711                 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
1712                 /* Bonding procedure was started but unfortunately could not complete.
1713                    Basically removed bonding was success, but create bond request could not proceed
1714                    So lets cleanup the context */
1715                 if (ret != OAL_STATUS_SUCCESS) {
1716                         BT_ERR("Create Bond procedure could not suceed");
1717                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1718                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1719                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1720                         __bt_free_pairing_info(&trigger_pairing_info);
1721                 }
1722                 break;
1723         case BT_DEVICE_BOND_STATE_NONE:
1724                 BT_INFO("Create Bond failed!!");
1725                 if (trigger_bond_info) {
1726                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1727                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1728                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1729                         __bt_free_pairing_info(&trigger_pairing_info);
1730                 }
1731                 break;
1732         default:
1733                 break;
1734         }
1735
1736         if (ret != OAL_STATUS_SUCCESS)
1737                 return _bt_convert_oal_status_to_bt_error(ret);
1738         else
1739                 return BLUETOOTH_ERROR_NONE;
1740 }
1741
1742 int _bt_device_get_bonded_device_info(bluetooth_device_address_t *addr)
1743 {
1744         int result;
1745         bt_address_t bd_addr;
1746
1747         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
1748
1749         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
1750         result = device_query_attributes(&bd_addr);
1751         if (result != OAL_STATUS_SUCCESS) {
1752                 BT_ERR("device_query_attributes error: [%d]", result);
1753                 return _bt_convert_oal_status_to_bt_error(result);
1754         }
1755
1756         return BLUETOOTH_ERROR_NONE;
1757 }
1758
1759 int _bt_set_alias(bluetooth_device_address_t *device_address, const char *alias)
1760 {
1761         int ret;
1762         char address[BT_ADDRESS_STRING_SIZE];
1763
1764         BT_CHECK_PARAMETER(alias, return);
1765
1766         ret = device_set_alias((bt_address_t *)device_address, (char *)alias);
1767         if (ret != OAL_STATUS_SUCCESS) {
1768                 BT_ERR("device_set_alias: %d", ret);
1769                 return _bt_convert_oal_status_to_bt_error(ret);
1770         }
1771
1772         /* Update local cache */
1773         _bt_convert_addr_type_to_string(address, device_address->addr);
1774         _bt_update_remote_dev_property(address, DEV_PROP_ALIAS, (void *)alias);
1775
1776         return BLUETOOTH_ERROR_NONE;
1777 }
1778
1779 int _bt_bond_device(bluetooth_device_address_t *device_address,
1780                 unsigned short conn_type, GArray **out_param1)
1781 {
1782         int result = BLUETOOTH_ERROR_NONE;
1783         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1784         bluetooth_device_info_t dev_info;
1785         const char *stack_name = NULL;
1786
1787         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1788
1789         /* If bonding or discovery already going on */
1790         if (trigger_bond_info || _bt_is_discovering()) {
1791                 BT_ERR("Device is buzy, bonding can not proceed now..");
1792                 result = BLUETOOTH_ERROR_DEVICE_BUSY;
1793                 goto fail;
1794         }
1795
1796         /*TODO: If unbonding with same device going on */
1797         _bt_convert_addr_type_to_string(address, device_address->addr);
1798
1799         trigger_bond_info = g_malloc0(sizeof(bt_bond_data_t));
1800         trigger_bond_info->addr = g_strdup(address);
1801         trigger_bond_info->conn_type = conn_type;
1802         trigger_bond_info->is_device_creating = TRUE;
1803         trigger_bond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1804         trigger_bond_info->dev_info = NULL;
1805
1806         /* Ready to initiate bonding */
1807
1808         stack_name = oal_get_stack_name();
1809         if (stack_name && !g_strcmp0(stack_name, "bluez")) {
1810                 BT_DBG("[bluez] Create bond by type %d", conn_type);
1811                 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVED_BONDING;
1812         } else {
1813                 /* In Tizen, we will first remove bond and then attempt to create bond to keep
1814                    consistency with bluedroid. Even if remove bond fails due to device not already
1815                    bonded, then straight away create bond is triggered. This is because, remove bond
1816                    is handled differently in bluedroid and bluez. In Bluez, if device is
1817                    already removed, remove bond call fails.
1818                    However in bluedroid, remove bond on already removed device returns success. So we will
1819                    handle the cases transparently */
1820                 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVE_BONDING;
1821         }
1822
1823 #if !defined(TIZEN_PROFILE_WEARABLE) && !defined(TIZEN_PROFILE_IOT)
1824         bond_retry_count = 0;
1825 #endif
1826         result = __bt_device_handle_bond_state();
1827
1828         if (result != BLUETOOTH_ERROR_NONE)
1829                 goto fail;
1830
1831         return result;
1832
1833 fail:
1834         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1835         memcpy(dev_info.device_address.addr, device_address->addr,
1836                         BLUETOOTH_ADDRESS_LENGTH);
1837
1838         g_array_append_vals(*out_param1, &dev_info,
1839                         sizeof(bluetooth_device_info_t));
1840         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1841
1842         return result;
1843 }
1844
1845 int _bt_unbond_device(bluetooth_device_address_t *device_address,
1846                 GArray **out_param1)
1847 {
1848         int result = OAL_STATUS_SUCCESS;
1849         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1850         bluetooth_device_info_t dev_info;
1851
1852         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1853
1854         _bt_convert_addr_type_to_string(address, device_address->addr);
1855
1856         trigger_unbond_info = g_malloc0(sizeof(bt_bond_data_t));
1857         trigger_unbond_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1858         trigger_unbond_info->addr = g_strdup(address);
1859         trigger_unbond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1860
1861         /* Check if Bonding is already going on, we should not abruptly remove bonding*/
1862         if (trigger_bond_info && strncmp(trigger_bond_info->addr, trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE) == 0) {
1863                 BT_ERR("Bonding with same device already ongoing");
1864                 result = BLUETOOTH_ERROR_PERMISSION_DEINED;
1865                 goto fail;
1866         }
1867
1868         result = device_destroy_bond((bt_address_t *)device_address);
1869         if (result != OAL_STATUS_SUCCESS)
1870                 goto fail;
1871
1872         return BLUETOOTH_ERROR_NONE;
1873
1874 fail:
1875         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1876         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1877                         trigger_unbond_info->addr);
1878
1879         g_array_append_vals(*out_param1, &dev_info,
1880                         sizeof(bluetooth_device_info_t));
1881         __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
1882
1883         return _bt_convert_oal_status_to_bt_error(result);
1884 }
1885
1886 int _bt_cancel_bonding(void)
1887 {
1888         int result = OAL_STATUS_SUCCESS;
1889         BT_INFO("+");
1890
1891         retv_if(trigger_bond_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1892
1893         result = device_stop_bond((bt_address_t *)trigger_bond_info->dev_addr);
1894
1895         if (result == OAL_STATUS_SUCCESS)
1896                 trigger_bond_info->is_cancelled_by_user = TRUE;
1897
1898         return result;
1899 }
1900
1901 int _bt_passkey_reply(const char *passkey, gboolean cnfm_reply)
1902 {
1903         bluetooth_device_address_t device_address;
1904         int ret = OAL_STATUS_SUCCESS;
1905         BT_INFO("reply: %d", cnfm_reply);
1906
1907         retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1908         retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1909
1910         _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1911
1912         if (trigger_pairing_info->is_ssp) {
1913                 if (cnfm_reply)
1914                         ret = device_accept_passkey_entry((bt_address_t *)&device_address, atoi(passkey));
1915                 else
1916                         ret = device_reject_passkey_entry((bt_address_t *)&device_address);
1917                 trigger_pairing_info->is_ssp = FALSE;
1918         } else {
1919                 if (cnfm_reply)
1920                         ret = device_accept_pin_request((bt_address_t *)&device_address, passkey);
1921                 else
1922                         ret = device_reject_pin_request((bt_address_t *)&device_address);
1923         }
1924
1925         __bt_free_pairing_info(&trigger_pairing_info);
1926
1927         if (ret != OAL_STATUS_SUCCESS) {
1928                 BT_ERR("_bt_device_handle_passkey_reply: err [%d]", ret);
1929                 return _bt_convert_oal_status_to_bt_error(ret);
1930         }
1931
1932         BT_INFO("-");
1933         return BLUETOOTH_ERROR_NONE;
1934 }
1935
1936 int _bt_passkey_confirmation_reply(gboolean cnfm_reply)
1937 {
1938         BT_INFO("BT_PASSKEY_CONFIRMATION_REPLY");
1939         bluetooth_device_address_t device_address;
1940         int ret = OAL_STATUS_SUCCESS;
1941         BT_INFO("reply: %d", cnfm_reply);
1942
1943         retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1944         retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1945
1946         _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1947
1948         ret = device_reply_passkey_confirmation((bt_address_t *)&device_address, cnfm_reply);
1949
1950         __bt_free_pairing_info(&trigger_pairing_info);
1951         if (ret != OAL_STATUS_SUCCESS) {
1952                 BT_ERR("_bt_device_handle_passkey_confirmation_reply: err [%d]", ret);
1953                 return BLUETOOTH_ERROR_INTERNAL;
1954         }
1955
1956         BT_INFO("-");
1957         return BLUETOOTH_ERROR_NONE;
1958 }
1959
1960 gboolean _bt_device_is_pairing(void)
1961 {
1962         return (trigger_pairing_info) ? TRUE : FALSE;
1963 }
1964
1965 gboolean _bt_device_is_bonding(void)
1966 {
1967         return (trigger_bond_info) ? TRUE : FALSE;
1968 }
1969
1970 gboolean _bt_is_bonding_device_address(const char *address)
1971 {
1972         if (trigger_bond_info == NULL || trigger_bond_info->addr == NULL)
1973                 return FALSE;
1974
1975         if (g_strcmp0(trigger_bond_info->addr, address) == 0) {
1976                 BT_DBG("[%s]  is bonding device", address);
1977                 return TRUE;
1978         }
1979
1980         BT_DBG("[%s]  is NOT bonding device", address);
1981         return FALSE;
1982 }
1983
1984 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
1985 {
1986         ret_if(trigger_bond_info == NULL);
1987         trigger_bond_info->is_autopair = is_autopair;
1988 }
1989
1990 int _bt_search_device(bluetooth_device_address_t *device_address)
1991 {
1992         int result = OAL_STATUS_SUCCESS;
1993         BT_DBG("+");
1994
1995         BT_CHECK_PARAMETER(device_address, return);
1996
1997         if (trigger_bond_info) {
1998                 BT_ERR("Bonding in progress");
1999                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2000         }
2001
2002         if (service_search_info) {
2003                 BT_ERR("Service searching in progress");
2004                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2005         }
2006
2007         /* allocate user data so that it can be retrieved in callback */
2008         service_search_info = g_malloc0(sizeof(bt_service_search_info_data_t));
2009         service_search_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
2010         service_search_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
2011
2012         _bt_convert_addr_type_to_string(service_search_info->addr,
2013                         device_address->addr);
2014
2015         result = device_query_services((bt_address_t *)device_address);
2016
2017         if (result != OAL_STATUS_SUCCESS) {
2018                 BT_ERR("Device Service Search Failed..: %d", result);
2019                 __bt_free_service_search_info(&service_search_info);
2020                 return _bt_convert_oal_status_to_bt_error(result);
2021         }
2022         return BLUETOOTH_ERROR_NONE;
2023 }
2024
2025 int _bt_cancel_search_device(void)
2026 {
2027         int ret = OAL_STATUS_SUCCESS;
2028         retv_if(service_search_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
2029
2030         ret = device_stop_query_sevices((bt_address_t *)service_search_info->dev_addr);
2031
2032         if (ret != OAL_STATUS_SUCCESS) {
2033                 BT_ERR("SDP Cancel request failed [%d]", ret);
2034                 return _bt_convert_oal_status_to_bt_error(ret);
2035         }
2036
2037         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_CANCEL_BY_USER, BT_SEARCH_SERVICE,
2038                         service_search_info->addr, BT_ADDRESS_STRING_SIZE);
2039
2040         __bt_free_service_search_info(&service_search_info);
2041
2042         return BLUETOOTH_ERROR_NONE;
2043         BT_DBG("-");
2044 }
2045
2046 int _bt_set_authorization(bluetooth_device_address_t *device_address,
2047                 gboolean authorize)
2048 {
2049         int ret = OAL_STATUS_SUCCESS;
2050         BT_DBG("+");
2051
2052         BT_CHECK_PARAMETER(device_address, return);
2053         BT_INFO("Device to be Trusted? [%d]", authorize);
2054
2055         ret = device_set_authorized((bt_address_t*)device_address, authorize);
2056         if (ret != OAL_STATUS_SUCCESS) {
2057                 BT_ERR("device_set_authorized: %d", ret);
2058                 return _bt_convert_oal_status_to_bt_error(ret);
2059         }
2060
2061         return BLUETOOTH_ERROR_NONE;
2062 }
2063
2064 gboolean _bt_is_device_connected(bluetooth_device_address_t *device_address, int svc_type)
2065 {
2066         gboolean is_connected;
2067         oal_service_t svc_id;
2068
2069         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
2070
2071         /*
2072          * TODO: While adding support for new profiles, need to add more
2073          * <svc_type, svc_id> mapping here.
2074          */
2075         switch (svc_type) {
2076         case BT_PROFILE_CONN_HID:
2077                 svc_id = HID_SERVICE_ID;
2078                 break;
2079         case BT_PROFILE_CONN_A2DP:
2080                 svc_id = A2DP_SERVICE_ID; /* Remote is A2DP Sink */
2081                 break;
2082         case BT_PROFILE_CONN_A2DP_SINK:
2083                 svc_id = A2DP_SRC_SERVICE_ID; /* Remote is A2DP Source*/
2084                 break;
2085         case BT_PROFILE_CONN_HSP:
2086                 svc_id = HFP_HS_SERVICE_ID; /* Remote is HFP HF Unit */
2087                 break;
2088         case BT_PROFILE_CONN_HFG:
2089                 svc_id = HFP_SERVICE_ID; /* Remote is HFP AG Unit */
2090                 break;
2091 #ifdef TIZEN_GATT_CLIENT
2092         case BT_PROFILE_CONN_GATT:
2093                 return _bt_is_remote_gatt_device_connected(device_address); /* Remote is GATT client or Server */
2094 #endif
2095         default:
2096                 BT_DBG("Unknown svc_type: %d", svc_type);
2097                 return FALSE;
2098         }
2099
2100         is_connected = device_get_svc_conn_state((bt_address_t*)device_address, svc_id);
2101
2102         BT_DBG("svc_type: %d, is_connected: %s",
2103                         svc_type, is_connected ? "TRUE" : "FALSE");
2104
2105         return is_connected;
2106 }
2107
2108 int _bt_rfcomm_reply_conn_authorization(char *address, gboolean reply)
2109 {
2110         bt_address_t bd_addr;
2111         int res;
2112
2113         BT_DBG("+");
2114
2115         retv_if(NULL == address, BLUETOOTH_ERROR_INVALID_PARAM);
2116         _bt_convert_addr_string_to_type(bd_addr.addr, address);
2117         res = device_reply_auth_request(&bd_addr, 0, reply, FALSE);
2118         if (res != OAL_STATUS_SUCCESS) {
2119                 BT_ERR("authorize_response: %d", res);
2120                 return _bt_convert_oal_status_to_bt_error(res);
2121         }
2122
2123         BT_DBG("-");
2124         return BLUETOOTH_ERROR_NONE;
2125 }
2126
2127 int _bt_enable_rssi(bluetooth_device_address_t *addr, int link_type,
2128                 int low_threshold, int in_range_threshold, int high_threshold)
2129 {
2130         int result;
2131         bt_address_t bd_addr;
2132
2133         BT_DBG("+");
2134
2135         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2136
2137         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2138         result = device_enable_rssi_monitoring(&bd_addr, link_type,
2139                         low_threshold, in_range_threshold, high_threshold);
2140         if (result != OAL_STATUS_SUCCESS) {
2141                 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2142                 return _bt_convert_oal_status_to_bt_error(result);
2143         }
2144
2145         BT_DBG("-");
2146         return BLUETOOTH_ERROR_NONE;
2147 }
2148
2149 int _bt_get_rssi_strength(bluetooth_device_address_t *addr, int link_type)
2150 {
2151         int result;
2152         bt_address_t bd_addr;
2153
2154         BT_DBG("+");
2155
2156         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2157
2158         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2159         result = device_get_connected_link_rssi_strength(&bd_addr, link_type);
2160         if (result != OAL_STATUS_SUCCESS) {
2161                 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2162                 return _bt_convert_oal_status_to_bt_error(result);
2163         }
2164
2165         BT_DBG("-");
2166         return BLUETOOTH_ERROR_NONE;
2167 }
2168
2169 int _bt_set_passkey_notification(const char *sender, gboolean enable)
2170 {
2171         int result;
2172
2173         BT_INFO("Set passkey notification(sender:%s, %s)",
2174                         sender, enable ? "Enable" : "Disable");
2175
2176         result = device_enable_gap_auth_notifications(OAL_PASSKEY_DISPLAY, enable);
2177         if (OAL_STATUS_SUCCESS != result) {
2178                 BT_ERR("device_enable_gap_auth_notifications error: [%d]", result);
2179                 return _bt_convert_oal_status_to_bt_error(result);
2180         }
2181
2182         g_free(passkey_watcher);
2183         if (enable == TRUE)
2184                 passkey_watcher = g_strdup(sender);
2185         else
2186                 passkey_watcher = NULL;
2187
2188         return BLUETOOTH_ERROR_NONE;
2189 }
2190
2191 static int __bt_get_device_pin_code(const char *address, char *pin_code)
2192 {
2193         GSList *l = NULL;
2194
2195         BT_CHECK_PARAMETER(address, return);
2196         BT_CHECK_PARAMETER(pin_code, return);
2197
2198         for (l = pin_info_list; l != NULL; l = l->next) {
2199                 bt_pin_code_info_t *pin_info = l->data;
2200
2201                 if (!pin_info || !pin_info->address)
2202                         continue;
2203
2204                 if (g_strcmp0(pin_info->address, address) == 0) {
2205                         g_strlcpy(pin_code, pin_info->pin_code,
2206                                         BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2207                         return BLUETOOTH_ERROR_NONE;
2208                 }
2209         }
2210
2211         return BLUETOOTH_ERROR_NOT_FOUND;
2212 }
2213
2214 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2215                 bluetooth_device_pin_code_t *pin_code)
2216 {
2217         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2218         bt_pin_code_info_t *pin_info = NULL;
2219         GSList *l = NULL;
2220
2221         BT_CHECK_PARAMETER(device_address, return);
2222         BT_CHECK_PARAMETER(pin_code, return);
2223         retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2224                         BLUETOOTH_ERROR_NO_RESOURCES);
2225
2226         _bt_convert_addr_type_to_string(address, device_address->addr);
2227
2228         for (l = pin_info_list; l != NULL; l = l->next) {
2229                 pin_info = l->data;
2230
2231                 if (!pin_info || !pin_info->address)
2232                         continue;
2233
2234                 if (g_strcmp0(pin_info->address, address) == 0) {
2235                         g_free(pin_info->pin_code);
2236                         pin_info->pin_code = g_strdup(pin_code->pin_code);
2237                         return BLUETOOTH_ERROR_NONE;
2238                 }
2239         }
2240
2241         pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2242         pin_info->address = g_strdup(address);
2243         pin_info->pin_code = g_strdup(pin_code->pin_code);
2244         pin_info_list = g_slist_append(pin_info_list, pin_info);
2245
2246         return BLUETOOTH_ERROR_NONE;
2247 }
2248
2249 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2250 {
2251         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2252         bt_pin_code_info_t *pin_info = NULL;
2253         GSList *l = NULL;
2254
2255         BT_DBG("+");
2256
2257         BT_CHECK_PARAMETER(device_address, return);
2258
2259         _bt_convert_addr_type_to_string(address, device_address->addr);
2260
2261         for (l = pin_info_list; l != NULL; l = l->next) {
2262                 pin_info = l->data;
2263
2264                 if (!pin_info || !pin_info->address)
2265                         continue;
2266
2267                 if (g_strcmp0(pin_info->address, address) == 0) {
2268                         pin_info_list = g_slist_remove(pin_info_list, pin_info);
2269                         g_free(pin_info->address);
2270                         g_free(pin_info->pin_code);
2271                         g_free(pin_info);
2272                         break;
2273                 }
2274         }
2275
2276         BT_DBG("-");
2277         return BLUETOOTH_ERROR_NONE;
2278 }
2279
2280 int _bt_device_get_ida(bluetooth_device_address_t *device_address, bluetooth_device_address_t *id_address)
2281 {
2282         int result = BLUETOOTH_ERROR_NONE;
2283         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2284
2285         _bt_convert_addr_type_to_string(address, device_address->addr);
2286
2287         BT_DBG("getting IDA for remote device: [%s]", address);
2288
2289         result = device_get_ida((bt_address_t*)device_address, (bt_address_t*)id_address);
2290
2291         if (result != OAL_STATUS_SUCCESS) {
2292                 BT_ERR("device_get_ida Failed %d", result);
2293                 return _bt_convert_oal_status_to_bt_error(result);
2294         }
2295
2296         return result;
2297 }
2298
2299 #ifdef TIZEN_GATT_CLIENT
2300 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2301 {
2302         GSList *l = NULL;
2303         bt_connected_le_dev_t *dev;
2304
2305         if (!address)
2306                 return NULL;
2307
2308         for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2309                 dev = l->data;
2310
2311                 if (g_strcmp0(dev->address, address) == 0)
2312                         return dev;
2313         }
2314         return NULL;
2315 }
2316
2317 static void __bt_le_conn_param_free(void *data)
2318 {
2319         bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2320
2321         BT_DBG("%s", param->sender);
2322         g_free(param->sender);
2323         g_free(param);
2324 }
2325
2326 static void _bt_add_le_connected_dev_info(const char *address)
2327 {
2328         bt_connected_le_dev_t *dev = NULL;
2329
2330         if (!address)
2331                 return;
2332
2333         dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2334         dev->address = g_strdup(address);
2335
2336         le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2337
2338         return;
2339 }
2340
2341 static void _bt_remove_le_connected_dev_info(const char *address)
2342 {
2343         bt_connected_le_dev_t *dev = NULL;
2344
2345         if (!address)
2346                 return;
2347
2348         dev = __bt_get_le_connected_dev_info(address);
2349         if (!dev)
2350                 return;
2351
2352         g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2353         le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2354         g_free(dev->address);
2355         g_free(dev);
2356
2357         return;
2358 }
2359
2360 static void _bt_handle_le_connected_dev_info(const char *address, gboolean connected)
2361 {
2362         BT_DBG("update le_connected_dev_list");
2363
2364         if (connected)
2365                 _bt_add_le_connected_dev_info(address);
2366         else
2367                 _bt_remove_le_connected_dev_info(address);
2368 }
2369
2370 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2371 {
2372         GSList *l = NULL;
2373         bt_le_conn_param_t *param = NULL;
2374
2375         if (!dev || !sender)
2376                 return NULL;
2377
2378         for (l = dev->senders; l; l = g_slist_next(l)) {
2379                 param = l->data;
2380                 if (g_strcmp0(param->sender, sender) == 0)
2381                         return param;
2382         }
2383
2384         return NULL;
2385 }
2386
2387 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2388 {
2389         bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2390         bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2391
2392         return parama->key > paramb->key;
2393 }
2394
2395
2396 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2397                 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2398 {
2399         bt_connected_le_dev_t *dev = NULL;
2400         bt_le_conn_param_t *param = NULL;
2401         bt_le_conn_param_t *data = NULL;
2402
2403         if (!address || !sender)
2404                 return BLUETOOTH_ERROR_INVALID_PARAM;
2405
2406         dev = __bt_get_le_connected_dev_info(address);
2407         if (!dev)
2408                 return BLUETOOTH_ERROR_INTERNAL;
2409
2410         param = __bt_get_le_conn_param_info(dev, sender);
2411
2412         data = g_malloc0(sizeof(bt_le_conn_param_t));
2413         data->sender = g_strdup(sender);
2414         data->interval_min = interval_min;
2415         data->interval_max = interval_max;
2416         data->latency = latency;
2417         data->time_out = time_out;
2418         data->key = interval_min + (interval_max - interval_min)/2;
2419
2420         if (param == NULL) {
2421                 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2422                 dev->senders = g_slist_append(dev->senders, data);
2423         } else {
2424                 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2425                 dev->senders = g_slist_remove(dev->senders, param);
2426                 g_free(param->sender);
2427                 g_free(param);
2428                 dev->senders = g_slist_append(dev->senders, data);
2429         }
2430
2431         /* Sorting. First element have the minimum interval */
2432         dev->senders = g_slist_sort(dev->senders,
2433                         (GCompareFunc)__bt_compare_le_conn_param_key);
2434
2435         return BLUETOOTH_ERROR_NONE;
2436 }
2437
2438
2439 static int __bt_le_set_conn_parameter(const char *address,
2440                 float interval_min, float interval_max,
2441                 guint16 latency, guint16 time_out)
2442 {
2443         bt_address_t dev_addr = { {0} };
2444         guint32 min, max, to;
2445
2446         BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2447                         interval_min, interval_max, latency, time_out);
2448
2449         min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2450         max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2451         to = time_out / BT_LE_CONN_TO_SPLIT;
2452
2453         BT_DBG("updating: Min interval: %d, Max interval: %d, Latency: %d, Supervision timeout: %d",
2454                         min, max, latency, to);
2455
2456         _bt_convert_addr_string_to_type(dev_addr.addr, address);
2457
2458         return gattc_conn_param_update(&dev_addr, min, max, latency, to);
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 }
2716 #endif