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