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