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