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