Merge "Remove flight mode implementation from core and add it to bt-service" into...
[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
690         if ((remote_dev_info->name || remote_dev_info->alias)
691                         && remote_dev_info->address
692                         && remote_dev_info->uuids) {
693                 GVariant *uuids = NULL;
694                 GVariantBuilder *builder = NULL;
695                 GVariant *manufacturer_data;
696                 unsigned int i = 0;
697                 char *name = NULL;
698
699                 if (remote_dev_info->alias)
700                         name = remote_dev_info->alias;
701                 else
702                         name = remote_dev_info->name;
703
704                 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
705                 for (i = 0; i < remote_dev_info->uuid_count; i++) {
706                         g_variant_builder_add(builder, "s",
707                                         remote_dev_info->uuids[i]);
708                 }
709                 uuids = g_variant_new("as", builder);
710                 g_variant_builder_unref(builder);
711                 manufacturer_data = g_variant_new_from_data((const GVariantType *)"ay",
712                                 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
713                                 TRUE, NULL, NULL);
714
715                 param = g_variant_new("(isunsbub@asn@ay)",
716                                 BLUETOOTH_ERROR_NONE,
717                                 remote_dev_info->address,
718                                 remote_dev_info->class,
719                                 remote_dev_info->rssi,
720                                 name,
721                                 remote_dev_info->paired,
722                                 remote_dev_info->connected,
723                                 remote_dev_info->trust,
724                                 uuids,
725                                 remote_dev_info->manufacturer_data_len,
726                                 manufacturer_data);
727                 /* Send the event to application */
728                 _bt_send_event(BT_ADAPTER_EVENT,
729                                 BLUETOOTH_EVENT_BONDING_FINISHED,
730                                 param);
731                 BT_PERMANENT_LOG("Paired %s", remote_dev_info->address + 12);
732                 BT_INFO_C("### Paired %s", remote_dev_info->address);
733                 if (incoming_bond) {
734                         __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
735                 } else {
736                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
737                         __bt_free_pairing_info(&trigger_pairing_info);
738                 }
739         } else {
740                 BT_INFO("Lets wait for more remote device properties");
741         }
742 }
743
744 static void __handle_incoming_bond_created_event(bt_address_t *bd_addr)
745 {
746         char address[BT_ADDRESS_STRING_SIZE];
747         bluetooth_device_address_t dev_addr;
748
749         /*
750          * BlueZ sends paired signal for each paired device, during activation,
751          * We should ignore this, otherwise application thinks that a new device
752          * got paired
753          */
754         if (_bt_adapter_get_status() != BT_ACTIVATED) {
755                 BT_DBG("BT is not activated, so ignore this");
756                 return;
757         }
758
759         _bt_convert_addr_type_to_string(address, bd_addr->addr);
760         if (!incoming_bond_info) {
761                 incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
762         } else {
763                 if (g_strcmp0(incoming_bond_info->addr, address)) {
764                         BT_DBG("Previous Bond address: [%s] differs from new address: [%s]",
765                                         address, incoming_bond_info->addr);
766                         __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
767                         incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
768                 }
769         }
770
771         BT_INFO("Incoming bond successfully completed");
772         g_strlcpy(incoming_bond_info->addr, address, BT_ADDRESS_STRING_SIZE);
773         incoming_bond_info->dev_info = NULL;
774
775         _bt_convert_addr_string_to_type(dev_addr.addr, incoming_bond_info->addr);
776         if (BLUETOOTH_ERROR_NONE == _bt_device_get_bonded_device_info(&dev_addr)) {
777                 BT_DBG("Bonded device info query posted to stack successfully");
778         } else {
779                 BT_ERR("Bonded device info query failed");
780                 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
781         }
782
783 }
784
785 static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr)
786 {
787         gchar address[BT_ADDRESS_STR_LEN];
788         bluetooth_device_address_t dev_addr;
789
790         if (trigger_bond_info == NULL) {
791                 /* Send reply */
792                 BT_DBG("trigger_bond_info == NULL, Handle incomming bond event");
793                 __handle_incoming_bond_created_event(bd_addr);
794                 return;
795         }
796
797         _bt_convert_addr_type_to_string(address, bd_addr->addr);
798         if (g_strcmp0(trigger_bond_info->addr, address)) {
799                 BT_DBG("Bonding address= [%s] is different from requested address =[%s]",
800                                 address, trigger_bond_info->addr);
801                 __handle_incoming_bond_created_event(bd_addr);
802                 return;
803         }
804
805         BT_INFO("Bonding successfully completed");
806         /* Bonding state will be cleaned up & BONDING FINISHED EVENT
807            will be sent only when Properties are fetched from stack
808            Till that time lets not free trigger_bond_info.
809            However it is possible that while fetching device properties, internal
810            stack error can occur which can lead to no valid properties or
811            no properties at all. So in such cases, we must not wait for properties,
812            otherwise, it can lead to infinite wait  */
813         _bt_convert_addr_string_to_type(dev_addr.addr,
814                         trigger_bond_info->addr);
815
816         if (_bt_device_get_bonded_device_info(&dev_addr) == BLUETOOTH_ERROR_NONE) {
817                 BT_DBG("BOnded device info query posted to stack successfully");
818                 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_BOND_DEVICE,
819                                 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
820         } else {
821                 BT_DBG("Possibly internal stack error in bonded device info query, perform cleanup");
822                 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
823                                 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
824                 /* Destroy if at all device got bonded at stack level */
825                 device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
826
827                 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
828                 __bt_free_pairing_info(&trigger_pairing_info);
829         }
830 }
831
832 /**********************************************************************************************
833 *  Bond removal event can be triggered for following reasons -
834 *  a. If Bonding procedure if failed (for Auth failed, Page timeout, cancelled by user etc)
835 *  b. If Application requests for explicitly removing the bond
836 *  c. When application attempt to create bond,bond is removed first which triggers this event
837 *     c. is in-line with Bluedroid bond create\emoval architecture
838 *********************************************************************************************/
839 static void __bt_device_handle_bond_removal_event(bt_address_t *bd_addr)
840 {
841         char address[BT_ADDRESS_STRING_SIZE];
842
843         BT_INFO("+");
844
845         _bt_convert_addr_type_to_string(address, bd_addr->addr);
846         _bt_service_remove_device_from_bonded_list(address);
847
848         if (trigger_unbond_info) {
849                 BT_INFO("Bond removal request successfully handled, return DBUS and send event");
850                 GVariant *param = NULL;
851                 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_UNBOND_DEVICE,
852                                 trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
853                 param = g_variant_new("(is)", BLUETOOTH_ERROR_NONE, trigger_unbond_info->addr);
854                 _bt_send_event(BT_ADAPTER_EVENT,
855                                 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
856                                 param);
857                 BT_PERMANENT_LOG("Unpaired %s", trigger_unbond_info->addr + 12);
858                 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
859                 __bt_free_pairing_info(&trigger_pairing_info);
860         } else if (trigger_bond_info) {
861                 BT_ERR("Bonding was removed");
862                 __bt_device_handle_bond_state();
863         }
864         BT_INFO("-");
865 }
866
867 static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_fail_event)
868 {
869         BT_INFO("+");
870         oal_status_t status = bond_fail_event->status;
871         BT_INFO("Bonding failed, reason: %d", status);
872
873         switch (status) {
874         case OAL_STATUS_RMT_DEVICE_DOWN:
875         {
876                 if (trigger_bond_info) {
877                         BT_INFO("OAL_STATUS_RMT_DEVICE_DOWN:Lets retry bonding!! retry count [%d]",
878                                         bond_retry_count);
879                         int ret = OAL_STATUS_SUCCESS;
880                         if (bond_retry_count < MAX_BOND_RETRY_COUNT) {
881                                 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
882                                 if (ret != OAL_STATUS_SUCCESS) {
883                                         BT_ERR("Create Bond procedure could not suceed");
884                                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
885                                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
886                                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
887                                         __bt_free_pairing_info(&trigger_pairing_info);
888                                         bond_retry_count = 0;
889                                 } else
890                                         bond_retry_count++;
891                         } else {
892                                 BT_ERR("Create Bond failed MAX_BOND_RETRY_COUNT TIMES!!");
893                                 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
894                                                 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
895                                 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
896                                 __bt_free_pairing_info(&trigger_pairing_info);
897                                 bond_retry_count = 0;
898                         }
899                 }
900                 break;
901         }
902         case OAL_STATUS_AUTH_FAILED:
903         {
904                 /*TODO Auto pairing status set & ignore auto pairing logics can be done at this point.
905                   To be considered later*/
906                 int result = BLUETOOTH_ERROR_INTERNAL;
907                 BT_INFO("BT_OPERATION_STATUS_AUTH_FAILED");
908
909                 BT_INFO("add device in pairing black list");
910                 _bt_set_autopair_status_in_bonding_info(FALSE);
911
912                 if (trigger_bond_info) {
913                         __bt_ignore_auto_pairing_request(trigger_bond_info->addr);
914                         BT_ERR("Create Bond procedure could not suceed, check if cancelled by User");
915                         if (trigger_bond_info->is_cancelled_by_user) {
916                                 BT_ERR("Bonding is cancelled by user");
917                                 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
918                         }
919                         __bt_device_handle_pending_requests(result, BT_BOND_DEVICE,
920                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
921                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
922                 }
923
924                 __bt_free_pairing_info(&trigger_pairing_info);
925                 break;
926         }
927         case OAL_STATUS_INTERNAL_ERROR:
928         {
929                 BT_INFO("OAL_STATUS_INTERNAL_ERROR");
930                 if (trigger_unbond_info) {
931                         BT_INFO("Bond removal request failed, return DBUS and send event");
932                         GVariant *param = NULL;
933                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_UNBOND_DEVICE,
934                                         trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
935                         param = g_variant_new("(is)", BLUETOOTH_ERROR_INTERNAL, trigger_unbond_info->addr);
936                         _bt_send_event(BT_ADAPTER_EVENT,
937                                         BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
938                                         param);
939                         __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
940                 } else if (trigger_bond_info) {
941                         if (__bt_device_handle_bond_state() != BLUETOOTH_ERROR_NONE) {
942                                 if (trigger_bond_info) {
943                                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
944                                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
945                                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
946                                 }
947                         }
948                 }
949
950                 __bt_free_pairing_info(&trigger_pairing_info);
951                 break;
952         }
953         default:
954         {
955                 BT_ERR("Unknown status of Bond failed event status [%d]", status);
956                 break;
957         }
958
959         }
960         BT_INFO("-");
961 }
962
963 static gboolean __bt_device_init_dbfw_rssi_cb(gpointer user_data)
964 {
965         dbfw_rssi = 0;
966         return FALSE;
967 }
968
969 static void __bt_device_parse_dbfw_set_rssi(unsigned char *data, uint32_t length)
970 {
971         ret_if(length < 5);
972         dbfw_rssi = 0xFFFFFF00 | data[4];
973         BT_INFO("dbfw_rssi = %d", dbfw_rssi);
974         g_timeout_add(1000, __bt_device_init_dbfw_rssi_cb, NULL);
975 }
976
977 static void __bt_device_dbfw_plus_info_callback(event_dev_dbfw_plus_info_t *dbfw_info)
978 {
979         char evt_str[18];
980         char *data_str = NULL;
981         int i;
982
983         data_str = g_malloc0(dbfw_info->length * 2 + 1);
984         for (i = 0; i < dbfw_info->length; i++)
985                 snprintf(&data_str[i * 2], 3, "%02X", dbfw_info->data[i]);
986         data_str[i * 2] = '\0';
987
988         switch(dbfw_info->event_code) {
989                 case BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_INFO:
990                         BT_DBG("## Event Code: Linkloss Debug Info");
991                         snprintf(evt_str, 18, "[DBFW]Linkloss(D)");
992                         __bt_device_parse_dbfw_set_rssi(dbfw_info->data, dbfw_info->length);
993                         break;
994                 case BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_CLOCK_INFO:
995                         BT_DBG("## Event Code: Linkloss Clock Info");
996                         snprintf(evt_str, 18, "[DBFW]Linkloss(C)");
997                         break;
998                 case BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_PAGE_SLOTS:
999                         BT_DBG("## Event Code: Linkloss Page slots");
1000                         snprintf(evt_str, 18, "[DBFW]Linkloss(P)");
1001                         break;
1002                 case BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_LMP_TRACE:
1003                         BT_DBG("## Event Code: Linkloss LMP trace");
1004                         snprintf(evt_str, 18, "[DBFW]Linkloss(L)");
1005                         break;
1006                 case BT_DBFW_PLUS_EVENT_CODE_A2DP_INFO:
1007                         BT_DBG("## Event Code: A2DP Info");
1008                         snprintf(evt_str, 18, "[DBFW]A2DP");
1009                         break;
1010                 case BT_DBFW_PLUS_EVENT_CODE_HFP_INFO:
1011                         BT_DBG("## Event Code: HFP Info");
1012                         snprintf(evt_str, 18, "[DBFW]HFP");
1013                         break;
1014                 case BT_DBFW_PLUS_EVENT_CODE_HFP_SCO_PACKET_TYPE_INFO:
1015                         BT_DBG("## Event Code: HFP SCO Packet Type");
1016                         snprintf(evt_str, 18, "[DBFW]SCO");
1017                         break;
1018                 default:
1019                         BT_DBG("## Unknown event code (0x%02x)", dbfw_info->event_code);
1020                         snprintf(evt_str, 18, "[DBFW]0x%02X", dbfw_info->event_code);
1021                         break;
1022         }
1023
1024         BT_PERMANENT_LOG("%s 0x%s", evt_str, data_str);
1025         g_free(data_str);
1026 }
1027
1028 static void __bt_device_event_handler(int event_type, gpointer event_data)
1029 {
1030         int eventcheck = OAL_EVENT_DEVICE_PROPERTIES;
1031         BT_DBG("event [%d] Event check = [%d]", event_type, eventcheck);
1032
1033         switch (event_type) {
1034         case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY: {
1035                 BT_INFO("BREDR Device Found");
1036                 __bt_device_remote_device_found_callback(event_data, FALSE);
1037                 break;
1038         }
1039         case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE: {
1040                 BT_INFO("Dual Device Found");
1041                 __bt_device_remote_device_found_callback(event_data, FALSE);
1042                 break;
1043         }
1044         case OAL_EVENT_DEVICE_PROPERTIES: {
1045                 BT_INFO("Remote Device properties Received");
1046                 __bt_device_remote_properties_callback((event_dev_properties_t *)event_data);
1047                 break;
1048         }
1049         case OAL_EVENT_DEVICE_BONDING_SUCCESS: {
1050                 BT_INFO("Bonding Success event Received");
1051                 __bt_device_handle_bond_completion_event((bt_address_t *)event_data);
1052                 break;
1053         }
1054         case OAL_EVENT_DEVICE_BONDING_REMOVED: {
1055                 BT_INFO("Bonding Removed event Received");
1056                 __bt_device_handle_bond_removal_event((bt_address_t *)event_data);
1057                 break;
1058         }
1059         case OAL_EVENT_DEVICE_BONDING_FAILED: {
1060                 BT_INFO("Bonding Failed event Received");
1061                 __bt_device_handle_bond_failed_event((event_dev_bond_failed_t*) event_data);
1062                 break;
1063         }
1064         case OAL_EVENT_DEVICE_ACL_CONNECTED: {
1065                 BT_INFO("ACL Connected event Received");
1066                 event_dev_conn_status_t* param = event_data;
1067                 __bt_device_acl_state_changed_callback(param, TRUE, 0);
1068                 break;
1069         }
1070         case OAL_EVENT_DEVICE_ACL_DISCONNECTED: {
1071                 BT_INFO("ACL Disconnected event Received");
1072                 __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 0);
1073                 break;
1074         }
1075         case OAL_EVENT_DEVICE_LE_CONNECTED: {
1076                 BT_INFO("LE Connected event Received");
1077                 event_dev_conn_status_t* param = event_data;
1078                 __bt_device_acl_state_changed_callback(param, TRUE, 1);
1079                 break;
1080         }
1081         case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
1082                 BT_INFO("LE Disconnected event Received");
1083                 __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 1);
1084                 break;
1085         }
1086         case OAL_EVENT_DEVICE_PIN_REQUEST: {
1087                 BT_INFO("PIN Request Received");
1088                 __bt_device_pin_request_callback((remote_device_t*)event_data);
1089                 break;
1090         }
1091         case OAL_EVENT_DEVICE_PASSKEY_ENTRY_REQUEST: {
1092                 BT_INFO("Passkey Entry request Received");
1093                 __bt_device_ssp_passkey_entry_callback((remote_device_t*)event_data);
1094                 break;
1095         }
1096         case OAL_EVENT_DEVICE_PASSKEY_CONFIRMATION_REQUEST:{
1097                 BT_INFO("Passkey Confirmation Request Received");
1098                 __bt_device_ssp_passkey_confirmation_callback((event_dev_passkey_t *)event_data);
1099                 break;
1100         }
1101         case OAL_EVENT_DEVICE_PASSKEY_DISPLAY: {
1102                 BT_INFO("Passkey Display Request Received");
1103                 __bt_device_ssp_passkey_display_callback((event_dev_passkey_t *)event_data);
1104                 break;
1105         }
1106         case OAL_EVENT_DEVICE_SSP_CONSENT_REQUEST: {
1107                 BT_INFO("SSP Consent Request Received");
1108                  __bt_device_ssp_consent_callback((remote_device_t*)event_data);
1109                 break;
1110         }
1111         case OAL_EVENT_DEVICE_SERVICES: {
1112                 BT_INFO("Remote Device Services Received");
1113                 __bt_device_services_callback((event_dev_services_t*)event_data);
1114                 break;
1115         }
1116         case OAL_EVENT_DEVICE_AUTHORIZE_REQUEST: {
1117                 BT_INFO("Remote Device Authorization Request");
1118                 __bt_device_authorization_request_callback((event_dev_authorize_req_t*)event_data);
1119                 break;
1120         }
1121         case OAL_EVENT_DEVICE_TRUSTED: {
1122                 BT_INFO("Remote Device Trusted");
1123                 __bt_device_trusted_callback(TRUE, (event_dev_trust_t*)event_data);
1124                 break;
1125         }
1126         case OAL_EVENT_DEVICE_UNTRUSTED: {
1127                 BT_INFO("Remote Device UnTrusted");
1128                 __bt_device_trusted_callback(FALSE, (event_dev_trust_t*)event_data);
1129                 break;
1130         }
1131         case OAL_EVENT_DEVICE_NAME: {
1132                 remote_device_t *rem_dev = event_data;
1133                 gchar address[BT_ADDRESS_STR_LEN];
1134
1135                 _bt_convert_addr_type_to_string(address, rem_dev->address.addr);
1136                 BT_INFO("Remote Device name Received");
1137                 BT_INFO("Name: %s, Address: %s", rem_dev->name, address);
1138
1139                 /* Update local cache */
1140                 _bt_update_remote_dev_property(address, DEV_PROP_NAME, (void *)rem_dev->name);
1141                 break;
1142         }
1143         case OAL_EVENT_DEVICE_TRUSTED_PROFILES_CHANGED: {
1144                 event_device_trusted_profiles_t *ev = event_data;
1145                 char address[BT_ADDRESS_STRING_SIZE];
1146
1147                 ret_if(NULL == ev);
1148
1149                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1150                 _bt_send_event(BT_DEVICE_EVENT,
1151                                 BLUETOOTH_EVENT_SUPPORTED_PROFILE_TRUSTED,
1152                                 g_variant_new("(isi)", BLUETOOTH_ERROR_NONE,
1153                                         address, ev->trust_val));
1154                 break;
1155         }
1156         case OAL_EVENT_RSSI_MONITORING_ENABLED: {
1157                 event_dev_rssi_info_t *ev = event_data;
1158                 char address[BT_ADDRESS_STRING_SIZE];
1159                 GVariant *param;
1160
1161                 ret_if(NULL == ev);
1162
1163                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1164                 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1165                                 address, ev->link_type, TRUE);
1166                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1167                 break;
1168         }
1169         case OAL_EVENT_RSSI_MONITORING_DISABLED: {
1170                 event_dev_rssi_info_t *ev = event_data;
1171                 char address[BT_ADDRESS_STRING_SIZE];
1172                 GVariant *param;
1173
1174                 ret_if(NULL == ev);
1175
1176                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1177                 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1178                                 address, ev->link_type, FALSE);
1179                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1180                 break;
1181         }
1182         case OAL_EVENT_RSSI_ALERT_RECEIVED: {
1183                 event_dev_rssi_info_t *ev = event_data;
1184                 char address[BT_ADDRESS_STRING_SIZE];
1185                 GVariant *param;
1186
1187                 ret_if(NULL == ev);
1188
1189                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1190                 param = g_variant_new("(isiii)", BLUETOOTH_ERROR_NONE,
1191                                 address, ev->link_type, ev->alert_type, ev->rssi);
1192                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ALERT, param);
1193                 break;
1194         }
1195         case OAL_EVENT_RAW_RSSI_RECEIVED: {
1196                 event_dev_rssi_info_t *ev = event_data;
1197                 char address[BT_ADDRESS_STRING_SIZE];
1198                 GVariant *param;
1199
1200                 ret_if(NULL == ev);
1201
1202                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1203                 param = g_variant_new("(isii)", BLUETOOTH_ERROR_NONE,
1204                                 address, ev->link_type, ev->rssi);
1205                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RAW_RSSI, param);
1206                 break;
1207         }
1208         case OAL_EVENT_DEVICE_DBFW_PLUS_INFO: {
1209                 __bt_device_dbfw_plus_info_callback((event_dev_dbfw_plus_info_t*)event_data);
1210                 break;
1211         }
1212         default:
1213                 BT_INFO("Unhandled event..");
1214         }
1215 }
1216
1217 /* Legacy Pairing event handler */
1218 static void __bt_device_pin_request_callback(remote_device_t* pin_req_event)
1219 {
1220         GVariant *param;
1221         char address[BT_ADDRESS_STRING_SIZE];
1222         char pin_code[BLUETOOTH_PIN_CODE_MAX_LENGTH + 1];
1223         bool incoming = false;
1224         BT_DBG("+");
1225
1226         _bt_convert_addr_type_to_string(address, pin_req_event->address.addr);
1227
1228         BT_INFO("Address[%s]", address);
1229         BT_INFO("Name[%s]", pin_req_event->name);
1230         BT_INFO("COD[%d]", pin_req_event->cod);
1231
1232         if (trigger_pairing_info) {
1233                 /* BTAPI support only one pairing at a time */
1234                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1235                 BT_ERR("New PIN request address [%s]", address);
1236                 device_reject_pin_request(&pin_req_event->address);
1237                 return;
1238         }
1239
1240         /* If user initiated bonding and auto response is possible, just reply with default 0000*/
1241         if (_bt_is_bonding_device_address(address) == TRUE &&
1242                         _bt_agent_is_auto_response(pin_req_event->cod, address, pin_req_event->name)) {
1243                 /* Note: Currently even if SYSPOPUP is supported, we use Fixed PIN "0000" for basic pairing
1244                    as BT SYSPOPUP is currently not working for PIN KEY entry in Tizen platform. This needs
1245                    to be checked and fixed apropriately */
1246                 _bt_set_autopair_status_in_bonding_info(TRUE);
1247                 device_accept_pin_request(&pin_req_event->address, "0000");
1248         } else if (_bt_agent_is_hid_keyboard(pin_req_event->cod)) {
1249                 BT_DBG("Remote Device is HID keyboard Type..");
1250                 char str_passkey[BT_PASSKEY_MAX_LENGTH + 1] = { 0 };
1251
1252                 if (_bt_agent_generate_passkey(str_passkey,
1253                                         BT_PASSKEY_MAX_LENGTH) != 0) {
1254                         device_reject_pin_request(&pin_req_event->address);
1255                         goto done;
1256                 }
1257                 device_accept_pin_request(&pin_req_event->address, str_passkey);
1258
1259                 BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1260
1261                 if(trigger_bond_info == NULL)
1262                         incoming = true;
1263
1264                 param = g_variant_new("(bsss)", incoming, address, pin_req_event->name, str_passkey);
1265                 _bt_send_event(BT_ADAPTER_EVENT,
1266                                 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1267                 BT_DBG("Key board pairing in process");
1268         } else if (BLUETOOTH_ERROR_NONE == __bt_get_device_pin_code(address, pin_code)) {
1269                 BT_DBG("Use stored PIN code [%s]", pin_code);
1270                 device_accept_pin_request(&pin_req_event->address, pin_code);
1271         } else {
1272                 if (_bt_is_bonding_device_address(address) == TRUE) {
1273                         BT_DBG("Show Pin entry");
1274                         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1275                         trigger_pairing_info->addr = g_strdup(address);
1276                         trigger_pairing_info->is_ssp = FALSE;
1277
1278                         BT_DBG("Send BLUETOOTH_EVENT_PIN_REQUEST");
1279
1280                         if(trigger_bond_info == NULL)
1281                                 incoming = true;
1282
1283                         param = g_variant_new("(bss)", incoming, address, pin_req_event->name);
1284                         _bt_send_event(BT_ADAPTER_EVENT,
1285                                         BLUETOOTH_EVENT_PIN_REQUEST, param);
1286                 }
1287         }
1288
1289 done:
1290         _bt_agent_release_memory();
1291         BT_DBG("-");
1292 }
1293
1294 /* SSP Pairing event handler */
1295 static void __bt_device_ssp_passkey_entry_callback(remote_device_t* dev_info)
1296 {
1297         GVariant *param;
1298         gchar address[BT_ADDRESS_STR_LEN];
1299         char *p_addr;
1300         gchar *name;
1301         bool incoming = false;
1302
1303         BT_DBG("+");
1304
1305         _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1306         p_addr = address;
1307         name = dev_info->name;
1308
1309         BT_INFO("Address[%s]", address);
1310         BT_INFO("Name[%s]", name);
1311         BT_INFO("COD[%d]", dev_info->cod);
1312
1313         if (trigger_pairing_info) {
1314                 /* BTAPI support only one pairing at a time */
1315                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1316                 BT_ERR("New PIN request address [%s]", address);
1317                 device_reject_pin_request(&dev_info->address);
1318                 BT_DBG("-");
1319                 return;
1320         }
1321
1322         /* Set pairing data */
1323         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1324         trigger_pairing_info->addr = g_strdup(address);
1325         trigger_pairing_info->is_ssp = TRUE;
1326
1327         if(trigger_bond_info == NULL)
1328                 incoming = true;
1329
1330         param = g_variant_new("(bss)", incoming, p_addr, name);
1331         _bt_send_event(BT_ADAPTER_EVENT,
1332                         BLUETOOTH_EVENT_PASSKEY_REQUEST, param);
1333         BT_DBG("-");
1334 }
1335
1336 static void __bt_device_ssp_passkey_confirmation_callback(event_dev_passkey_t *dev_info)
1337 {
1338         GVariant *param;
1339         gchar address[BT_ADDRESS_STR_LEN];
1340         char *p_addr;
1341         gchar *name;
1342         char str_passkey[7];
1343         bool incoming = false; /*Stores if bonding request is incoming(true) or outgoing(false) */
1344         BT_DBG("+");
1345
1346         _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1347         p_addr = address;
1348         name = dev_info->device_info.name;
1349
1350         BT_INFO("Address[%s]", address);
1351         BT_INFO("Name[%s]", name);
1352         BT_INFO("COD[%d]", dev_info->device_info.cod);
1353
1354         if (trigger_pairing_info) {
1355                 /* BTAPI support only one pairing at a time */
1356                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1357                 BT_ERR("New PIN request address [%s]", address);
1358                 device_reject_pin_request(&dev_info->device_info.address);
1359                 BT_DBG("-");
1360                 return;
1361         }
1362
1363         /* Set pairing data */
1364         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1365         trigger_pairing_info->addr = g_strdup(address);
1366         trigger_pairing_info->is_ssp = TRUE;
1367
1368         BT_DBG("Send BLUETOOTH_EVENT_PASSKEY_CONFIRMATION");
1369         snprintf(str_passkey, sizeof(str_passkey), "%.6d", dev_info->pass_key);
1370
1371         /*Storing if bond is incoming or outgoing*/
1372         if(trigger_bond_info == NULL)
1373                 incoming = true;
1374
1375         param = g_variant_new("(bsss)", incoming, p_addr, name, str_passkey);
1376         _bt_send_event(BT_ADAPTER_EVENT,
1377                         BLUETOOTH_EVENT_PASSKEY_CONFIRM_REQUEST, param);
1378         BT_DBG("-");
1379 }
1380
1381 static void __bt_device_ssp_passkey_display_callback(event_dev_passkey_t *dev_info)
1382 {
1383         GVariant *param;
1384         gchar address[BT_ADDRESS_STR_LEN];
1385         char *p_addr;
1386         gchar *name;
1387         char str_passkey[7];
1388         bool incoming = false;
1389         BT_DBG("+");
1390
1391         _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1392         p_addr = address;
1393         name = dev_info->device_info.name;
1394
1395         BT_INFO("Address[%s]", address);
1396         BT_INFO("Name[%s]", name);
1397         BT_INFO("COD[%d]", dev_info->device_info.cod);
1398
1399         if (trigger_pairing_info) {
1400                 /* BTAPI support only one pairing at a time */
1401                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1402                 BT_ERR("New PIN request address [%s]", address);
1403                 device_reject_pin_request(&dev_info->device_info.address);
1404                 BT_DBG("-");
1405                 return;
1406         }
1407
1408         /* Set pairing data */
1409         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1410         trigger_pairing_info->addr = g_strdup(address);
1411         trigger_pairing_info->is_ssp = TRUE;
1412
1413         BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1414         snprintf(str_passkey, sizeof(str_passkey), "%06d", dev_info->pass_key);
1415
1416         if(trigger_bond_info == NULL)
1417                 incoming = true;
1418
1419         param = g_variant_new("(bsss)", incoming, p_addr, name, str_passkey);
1420
1421         if (passkey_watcher) {
1422                 BT_INFO("Send passkey to %s", passkey_watcher);
1423                 _bt_send_event_to_dest(passkey_watcher, BT_ADAPTER_EVENT,
1424                                 BLUETOOTH_EVENT_PASSKEY_NOTIFICATION, param);
1425         } else {
1426                 _bt_send_event(BT_ADAPTER_EVENT,
1427                                 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1428         }
1429         BT_DBG("-");
1430 }
1431
1432 static void __bt_device_ssp_consent_callback(remote_device_t* dev_info)
1433 {
1434         gchar address[BT_ADDRESS_STR_LEN];
1435         gchar *name;
1436         int local_major;
1437         int local_minor;
1438         int cod;
1439         BT_DBG("+");
1440
1441         _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1442         name = dev_info->name;
1443         cod = dev_info->cod;
1444
1445         BT_INFO("Address[%s]", address);
1446         BT_INFO("Name[%s]", name);
1447         BT_INFO("COD[%d]", cod);
1448
1449         if (trigger_pairing_info) {
1450                 /* BTAPI support only one pairing at a time */
1451                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1452                 BT_ERR("New PIN request address [%s]", address);
1453                 device_reject_pin_request(&dev_info->address);
1454                 BT_DBG("-");
1455                 return;
1456         }
1457
1458         /* Set pairing data */
1459         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1460         trigger_pairing_info->addr = g_strdup(address);
1461         trigger_pairing_info->is_ssp = TRUE;
1462
1463         local_major = ((cod >> 8) & 0x001f);
1464         local_minor = (cod & 0x00fc);
1465         BT_DBG("SSP_CONSENT: Major type=[0x%x] and Minor type=[0x%x]", local_major, local_minor);
1466
1467         /*TODO: BLUETOOTH_EVENT_SSP_CONSENT_REQUEST to be handled in Tizen */
1468         BT_DBG("-");
1469 }
1470
1471 static int __bt_oal_status_to_bt_error(int oal_status)
1472 {
1473         int ret = 0;
1474
1475         switch (oal_status) {
1476         case OAL_STATUS_SUCCESS:
1477                 ret = BLUETOOTH_ERROR_NONE;
1478                 break;
1479         case OAL_STATUS_CONN_TIMEOUT:
1480         case OAL_STATUS_LINK_LOSS:
1481                 BT_INFO("Connection Timeout");
1482                 ret = BLUETOOTH_ERROR_CONNECTION_TIMEOUT;
1483                 break;
1484 #ifdef TIZEN_BT_HAL
1485         case OAL_STATUS_CONN_TERM_LOCAL_HOST:
1486                 ret = BLUETOOTH_ERROR_LOCAL_HOST_TERM;
1487                 break;
1488         case OAL_STATUS_CONN_TERM_RMT_HOST:
1489                 ret = BLUETOOTH_ERROR_REMOTE_USER_TERM;
1490                 break;
1491 #endif
1492         case OAL_STATUS_INTERNAL_ERROR:
1493                 ret = BLUETOOTH_ERROR_INTERNAL;
1494                 break;
1495         default:
1496                 ret = BLUETOOTH_ERROR_INTERNAL;
1497                 break;
1498         }
1499         return ret;
1500 }
1501
1502 static void __bt_device_acl_state_changed_callback(event_dev_conn_status_t *acl_event,
1503                 gboolean connected, unsigned char type)
1504 {
1505         gchar address[BT_ADDRESS_STR_LEN];
1506         int result = BLUETOOTH_ERROR_NONE;
1507         GVariant *param = NULL;
1508         bt_device_conn_info_t conn_info;
1509
1510         _bt_convert_addr_type_to_string(address, acl_event->address.addr);
1511
1512         _bt_logging_connection(connected, type);
1513
1514         result = __bt_oal_status_to_bt_error(acl_event->status);
1515         BT_DBG("Result [0x%x]", result);
1516
1517         if (connected) {
1518                 param = g_variant_new("(isy)", result, address, type);
1519                 _bt_send_event(BT_DEVICE_EVENT,
1520                                 BLUETOOTH_EVENT_DEVICE_CONNECTED,
1521                                 param);
1522         } else {
1523                 param = g_variant_new("(isyi)", result, address, type, dbfw_rssi);
1524                 _bt_send_event(BT_DEVICE_EVENT,
1525                                 BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1526                                 param);
1527         }
1528         BT_PERMANENT_LOG("%sonnected %s %s",
1529                 connected ? "C" : "Disc", !type ? "EDR" : "LE", address + 12);
1530
1531         conn_info.connected = connected;
1532         conn_info.type = type;
1533         /* Update local cache */
1534         _bt_update_remote_dev_property(address, DEV_PROP_CONNECTED, (void *)&conn_info);
1535
1536 #ifdef TIZEN_GATT_CLIENT
1537         /*handle LE connected device info*/
1538         if (type)
1539                 _bt_handle_le_connected_dev_info(address, connected);
1540 #endif
1541
1542 }
1543
1544 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble)
1545 {
1546         bt_remote_dev_info_t *dev_info = NULL;
1547         int result = BLUETOOTH_ERROR_NONE;
1548         GVariant *param = NULL;
1549         GVariant *uuids = NULL;
1550         GVariant *manufacturer_data = NULL;
1551         GVariantBuilder *builder = NULL;
1552         unsigned int i;
1553
1554         ret_if(_bt_is_discovering() == FALSE);
1555         ret_if(event_data == NULL);
1556
1557         dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
1558
1559         if (is_ble) {
1560                 event_ble_dev_found_t * oal_ble_dev = event_data;
1561                 BT_INFO("Device type [%d]", oal_ble_dev->device_info.type);
1562
1563                 _bt_copy_remote_dev(dev_info, &oal_ble_dev->device_info);
1564
1565                 dev_info->manufacturer_data_len = oal_ble_dev->adv_len;
1566                 if (dev_info->manufacturer_data_len)
1567                         dev_info->manufacturer_data = g_memdup(oal_ble_dev->adv_data,
1568                                         dev_info->manufacturer_data_len);
1569                 else
1570                         dev_info->manufacturer_data = NULL;
1571                 BT_DBG("----Advertising Data Length: %d", dev_info->manufacturer_data_len);
1572         } else {
1573                 event_dev_found_t * oal_dev = event_data;
1574                 _bt_copy_remote_dev(dev_info, &oal_dev->device_info);
1575         }
1576
1577         /* If Remote device name is NULL or still RNR is not done then display address as name. */
1578         if (dev_info->name == NULL)
1579                 dev_info->name = g_strdup(dev_info->address);
1580         BT_DBG("Name %s", dev_info->name);
1581
1582         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1583         for (i = 0; i < dev_info->uuid_count; i++)
1584                 g_variant_builder_add(builder, "s", dev_info->uuids[i]);
1585
1586         uuids = g_variant_new("as", builder);
1587         g_variant_builder_unref(builder);
1588
1589         manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1590                         dev_info->manufacturer_data,
1591                         dev_info->manufacturer_data_len,
1592                         TRUE,
1593                         NULL, NULL);
1594
1595         param = g_variant_new("(isunsbub@asn@ay)", result,
1596                         dev_info->address,
1597                         dev_info->class,
1598                         dev_info->rssi,
1599                         dev_info->name,
1600                         dev_info->paired,
1601                         dev_info->connected,
1602                         dev_info->trust,
1603                         uuids,
1604                         dev_info->manufacturer_data_len,
1605                         manufacturer_data);
1606
1607         _bt_send_event(BT_ADAPTER_EVENT,
1608                         BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
1609                         param);
1610
1611         _bt_free_remote_dev(dev_info);
1612 }
1613
1614 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info)
1615 {
1616         gchar address[BT_ADDRESS_STR_LEN];
1617         int result = BLUETOOTH_ERROR_NONE;
1618         GVariant *param = NULL;
1619         int event;
1620
1621         _bt_convert_addr_type_to_string(address, info->address.addr);
1622
1623         /* Update local cache */
1624         _bt_update_remote_dev_property(address, DEV_PROP_TRUST, (void *)&trusted);
1625
1626         param = g_variant_new("(is)", result, address);
1627         event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1628                 BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1629         /* Send event to application */
1630         _bt_send_event(BT_DEVICE_EVENT,
1631                         event,
1632                         param);
1633
1634 }
1635
1636 static void __bt_free_pairing_info(bt_pairing_data_t **p_info)
1637 {
1638         bt_pairing_data_t * info = *p_info;
1639         if (info) {
1640                 if (info->addr)
1641                         g_free(info->addr);
1642
1643                 g_free(info);
1644         }
1645         *p_info = NULL;
1646 }
1647
1648 static void __bt_free_bond_info(uint8_t type)
1649 {
1650         switch (type) {
1651         case BT_DEVICE_BOND_INFO:
1652                 if (!trigger_bond_info)
1653                         break;
1654
1655                 if (trigger_bond_info->addr)
1656                         g_free(trigger_bond_info->addr);
1657                 if (trigger_bond_info->dev_addr)
1658                         g_free(trigger_bond_info->dev_addr);
1659                 if (trigger_bond_info->dev_info)
1660                         _bt_free_remote_dev(trigger_bond_info->dev_info);
1661                 g_free(trigger_bond_info);
1662                 trigger_bond_info = NULL;
1663                 break;
1664         case BT_DEVICE_INCOMING_BOND_INFO:
1665                 if (!incoming_bond_info)
1666                         break;
1667
1668                 if (incoming_bond_info->dev_info)
1669                         _bt_free_remote_dev(incoming_bond_info->dev_info);
1670                 g_free(incoming_bond_info);
1671                 incoming_bond_info = NULL;
1672                 break;
1673         case BT_DEVICE_UNBOND_INFO:
1674                 if (!trigger_unbond_info)
1675                         break;
1676
1677                 if (trigger_unbond_info->addr)
1678                         g_free(trigger_unbond_info->addr);
1679                 if (trigger_unbond_info->dev_addr)
1680                         g_free(trigger_unbond_info->dev_addr);
1681                 if (trigger_unbond_info->dev_info)
1682                         _bt_free_remote_dev(trigger_unbond_info->dev_info);
1683                 g_free(trigger_unbond_info);
1684                 trigger_unbond_info = NULL;
1685                 break;
1686         }
1687 }
1688
1689 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info)
1690 {
1691         bt_service_search_info_data_t * info = *p_info;
1692         if (info) {
1693                 if (info->addr) {
1694                         g_free(info->addr);
1695                         info->addr = NULL;
1696                 }
1697
1698                 if (info->dev_addr) {
1699                         g_free(info->dev_addr);
1700                         info->dev_addr = NULL;
1701                 }
1702
1703                 if (info->dev_info) {
1704                         _bt_free_remote_dev(info->dev_info);
1705                         info->dev_info = NULL;
1706                 }
1707
1708                 g_free(info);
1709         }
1710         *p_info = NULL;
1711 }
1712
1713 static int __bt_device_handle_bond_state(void)
1714 {
1715         BT_INFO("Current Bond state: %d", bt_device_bond_state);
1716         int ret = OAL_STATUS_INTERNAL_ERROR;
1717
1718         switch (bt_device_bond_state) {
1719         case BT_DEVICE_BOND_STATE_CANCEL_DISCOVERY:
1720                 /*TODO:Bonding during discovery: Unhandled!!*/
1721                 BT_INFO("Bonding during discovery: Unhandled!!");
1722                 break;
1723         case BT_DEVICE_BOND_STATE_DISCOVERY_CANCELLED:
1724                 /*TODO:Bonding during discovery: Unhandled!!*/
1725                 BT_INFO("Bonding during discovery: Unhandled!!");
1726                 break;
1727         case BT_DEVICE_BOND_STATE_REMOVE_BONDING:
1728                 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVED_BONDING;
1729                 ret = device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
1730                 if (ret != OAL_STATUS_SUCCESS)
1731                         ret = __bt_device_handle_bond_state();
1732                 break;
1733         case BT_DEVICE_BOND_STATE_REMOVED_BONDING:
1734                 bt_device_bond_state = BT_DEVICE_BOND_STATE_NONE;
1735                 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
1736                 /* Bonding procedure was started but unfortunately could not complete.
1737                    Basically removed bonding was success, but create bond request could not proceed
1738                    So lets cleanup the context */
1739                 if (ret != OAL_STATUS_SUCCESS) {
1740                         BT_ERR("Create Bond procedure could not suceed");
1741                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1742                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1743                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1744                         __bt_free_pairing_info(&trigger_pairing_info);
1745                 }
1746                 break;
1747         case BT_DEVICE_BOND_STATE_NONE:
1748                 BT_INFO("Create Bond failed!!");
1749                 if (trigger_bond_info) {
1750                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1751                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1752                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1753                         __bt_free_pairing_info(&trigger_pairing_info);
1754                 }
1755                 break;
1756         default:
1757                 break;
1758         }
1759
1760         if (ret != OAL_STATUS_SUCCESS)
1761                 return BLUETOOTH_ERROR_INTERNAL;
1762         else
1763                 return BLUETOOTH_ERROR_NONE;
1764 }
1765
1766 int _bt_device_get_bonded_device_info(bluetooth_device_address_t *addr)
1767 {
1768         int result;
1769         bt_address_t bd_addr;
1770
1771         BT_DBG("+");
1772
1773         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
1774
1775         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
1776         result = device_query_attributes(&bd_addr);
1777         if (result != OAL_STATUS_SUCCESS) {
1778                 BT_ERR("device_query_attributes error: [%d]", result);
1779                 return BLUETOOTH_ERROR_INTERNAL;
1780         }
1781
1782         BT_DBG("-");
1783         return BLUETOOTH_ERROR_NONE;
1784 }
1785
1786 int _bt_set_alias(bluetooth_device_address_t *device_address, const char *alias)
1787 {
1788         int ret;
1789         char address[BT_ADDRESS_STRING_SIZE];
1790
1791         BT_DBG("+");
1792         BT_CHECK_PARAMETER(alias, return);
1793
1794         ret = device_set_alias((bt_address_t *)device_address, (char *)alias);
1795         if (ret != OAL_STATUS_SUCCESS) {
1796                 BT_ERR("device_set_alias: %d", ret);
1797                 return BLUETOOTH_ERROR_INTERNAL;
1798         }
1799
1800         /* Update local cache */
1801         _bt_convert_addr_type_to_string(address, device_address->addr);
1802         _bt_update_remote_dev_property(address, DEV_PROP_ALIAS, (void *)alias);
1803
1804         BT_DBG("-");
1805         return BLUETOOTH_ERROR_NONE;
1806 }
1807
1808 int _bt_bond_device(bluetooth_device_address_t *device_address,
1809                 unsigned short conn_type, GArray **out_param1)
1810 {
1811         int result = BLUETOOTH_ERROR_NONE;
1812         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1813         bluetooth_device_info_t dev_info;
1814         const char *stack_name = NULL;
1815         BT_DBG("+");
1816
1817         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1818
1819         /* If bonding or discovery already going on */
1820         if (trigger_bond_info || _bt_is_discovering()) {
1821                 BT_ERR("Device is buzy, bonding can not proceed now..");
1822                 result = BLUETOOTH_ERROR_DEVICE_BUSY;
1823                 goto fail;
1824         }
1825
1826         /*TODO: If unbonding with same device going on */
1827         _bt_convert_addr_type_to_string(address, device_address->addr);
1828
1829         trigger_bond_info = g_malloc0(sizeof(bt_bond_data_t));
1830         trigger_bond_info->addr = g_strdup(address);
1831         trigger_bond_info->conn_type = conn_type;
1832         trigger_bond_info->is_device_creating = TRUE;
1833         trigger_bond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1834         trigger_bond_info->dev_info = NULL;
1835
1836         /* Ready to initiate bonding */
1837
1838         stack_name = oal_get_stack_name();
1839         if (stack_name && !g_strcmp0(stack_name, "bluez")) {
1840                 BT_DBG("[bluez] Create bond by type %d", conn_type);
1841                 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVED_BONDING;
1842         } else {
1843                 /* In Tizen, we will first remove bond and then attempt to create bond to keep
1844                    consistency with bluedroid. Even if remove bond fails due to device not already
1845                    bonded, then straight away create bond is triggered. This is because, remove bond
1846                    is handled differently in bluedroid and bluez. In Bluez, if device is
1847                    already removed, remove bond call fails.
1848                    However in bluedroid, remove bond on already removed device returns success. So we will
1849                    handle the cases transparently */
1850                 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVE_BONDING;
1851         }
1852
1853         bond_retry_count = 0;
1854         result = __bt_device_handle_bond_state();
1855
1856         if (result != BLUETOOTH_ERROR_NONE)
1857                 goto fail;
1858
1859         BT_DBG("-");
1860         return result;
1861
1862 fail:
1863         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1864         memcpy(dev_info.device_address.addr, device_address->addr,
1865                         BLUETOOTH_ADDRESS_LENGTH);
1866
1867         g_array_append_vals(*out_param1, &dev_info,
1868                         sizeof(bluetooth_device_info_t));
1869         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1870
1871         BT_DBG("-");
1872         return result;
1873 }
1874
1875 int _bt_unbond_device(bluetooth_device_address_t *device_address,
1876                 GArray **out_param1)
1877 {
1878         int result = OAL_STATUS_SUCCESS;
1879         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1880         bluetooth_device_info_t dev_info;
1881         BT_INFO("+");
1882
1883         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1884
1885         _bt_convert_addr_type_to_string(address, device_address->addr);
1886
1887         trigger_unbond_info = g_malloc0(sizeof(bt_bond_data_t));
1888         trigger_unbond_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1889         trigger_unbond_info->addr = g_strdup(address);
1890         trigger_unbond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1891
1892         /* Check if Bonding is already going on, we should not abruptly remove bonding*/
1893         if (trigger_bond_info && strncmp(trigger_bond_info->addr, trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE) == 0) {
1894                 BT_ERR("Bonding with same device already ongoing");
1895                 result = BLUETOOTH_ERROR_PERMISSION_DEINED;
1896                 goto fail;
1897         }
1898
1899         result = device_destroy_bond((bt_address_t *)device_address);
1900         if (result != OAL_STATUS_SUCCESS)
1901                 goto fail;
1902
1903         return result;
1904
1905 fail:
1906         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1907         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1908                         trigger_unbond_info->addr);
1909
1910         g_array_append_vals(*out_param1, &dev_info,
1911                         sizeof(bluetooth_device_info_t));
1912         __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
1913
1914         return result;
1915 }
1916
1917 int _bt_cancel_bonding(void)
1918 {
1919         int result = OAL_STATUS_SUCCESS;
1920         BT_INFO("+");
1921
1922         retv_if(trigger_bond_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1923
1924         result = device_stop_bond((bt_address_t *)trigger_bond_info->dev_addr);
1925
1926         if (result == OAL_STATUS_SUCCESS)
1927                 trigger_bond_info->is_cancelled_by_user = TRUE;
1928
1929         return result;
1930 }
1931
1932 int _bt_passkey_reply(const char *passkey, gboolean cnfm_reply)
1933 {
1934         bluetooth_device_address_t device_address;
1935         int ret = OAL_STATUS_SUCCESS;
1936         BT_INFO("reply: %d", cnfm_reply);
1937
1938         retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1939         retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1940
1941         _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1942
1943         if (trigger_pairing_info->is_ssp) {
1944                 if (cnfm_reply)
1945                         ret = device_accept_passkey_entry((bt_address_t *)&device_address, atoi(passkey));
1946                 else
1947                         ret = device_reject_passkey_entry((bt_address_t *)&device_address);
1948                 trigger_pairing_info->is_ssp = FALSE;
1949         } else {
1950                 if (cnfm_reply)
1951                         ret = device_accept_pin_request((bt_address_t *)&device_address, passkey);
1952                 else
1953                         ret = device_reject_pin_request((bt_address_t *)&device_address);
1954         }
1955
1956         __bt_free_pairing_info(&trigger_pairing_info);
1957
1958         if (ret != OAL_STATUS_SUCCESS) {
1959                 BT_ERR("_bt_device_handle_passkey_reply: err [%d]", ret);
1960                 return BLUETOOTH_ERROR_INTERNAL;
1961         }
1962
1963         BT_INFO("-");
1964         return BLUETOOTH_ERROR_NONE;
1965 }
1966
1967 int _bt_passkey_confirmation_reply(gboolean cnfm_reply)
1968 {
1969         BT_INFO("BT_PASSKEY_CONFIRMATION_REPLY");
1970         bluetooth_device_address_t device_address;
1971         int ret = OAL_STATUS_SUCCESS;
1972         BT_INFO("reply: %d", cnfm_reply);
1973
1974         retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1975         retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1976
1977         _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1978
1979         ret = device_reply_passkey_confirmation((bt_address_t *)&device_address, cnfm_reply);
1980
1981         __bt_free_pairing_info(&trigger_pairing_info);
1982         if (ret != OAL_STATUS_SUCCESS) {
1983                 BT_ERR("_bt_device_handle_passkey_confirmation_reply: err [%d]", ret);
1984                 return BLUETOOTH_ERROR_INTERNAL;
1985         }
1986
1987         BT_INFO("-");
1988         return BLUETOOTH_ERROR_NONE;
1989 }
1990
1991 gboolean _bt_device_is_pairing(void)
1992 {
1993         return (trigger_pairing_info) ? TRUE : FALSE;
1994 }
1995
1996 gboolean _bt_device_is_bonding(void)
1997 {
1998         return (trigger_bond_info) ? TRUE : FALSE;
1999 }
2000
2001 gboolean _bt_is_bonding_device_address(const char *address)
2002 {
2003         if (trigger_bond_info == NULL || trigger_bond_info->addr == NULL)
2004                 return FALSE;
2005
2006         if (g_strcmp0(trigger_bond_info->addr, address) == 0) {
2007                 BT_DBG("[%s]  is bonding device", address);
2008                 return TRUE;
2009         }
2010
2011         BT_DBG("[%s]  is NOT bonding device", address);
2012         return FALSE;
2013 }
2014
2015 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
2016 {
2017         ret_if(trigger_bond_info == NULL);
2018         trigger_bond_info->is_autopair = is_autopair;
2019 }
2020
2021 int _bt_search_device(bluetooth_device_address_t *device_address)
2022 {
2023         int result = OAL_STATUS_SUCCESS;
2024         BT_DBG("+");
2025
2026         BT_CHECK_PARAMETER(device_address, return);
2027
2028         if (trigger_bond_info) {
2029                 BT_ERR("Bonding in progress");
2030                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2031         }
2032
2033         if (service_search_info) {
2034                 BT_ERR("Service searching in progress");
2035                 return BLUETOOTH_ERROR_DEVICE_BUSY;
2036         }
2037
2038         /* allocate user data so that it can be retrieved in callback */
2039         service_search_info = g_malloc0(sizeof(bt_service_search_info_data_t));
2040         service_search_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
2041         service_search_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
2042
2043         _bt_convert_addr_type_to_string(service_search_info->addr,
2044                         device_address->addr);
2045
2046         result = device_query_services((bt_address_t *)device_address);
2047
2048         if (result != OAL_STATUS_SUCCESS) {
2049                 BT_ERR("Device Service Search Failed..: %d", result);
2050                 __bt_free_service_search_info(&service_search_info);
2051                 return BLUETOOTH_ERROR_INTERNAL;
2052         }
2053         return BLUETOOTH_ERROR_NONE;
2054 }
2055
2056 int _bt_cancel_search_device(void)
2057 {
2058         int ret = OAL_STATUS_SUCCESS;
2059         retv_if(service_search_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
2060
2061         ret = device_stop_query_sevices((bt_address_t *)service_search_info->dev_addr);
2062
2063         if (ret != OAL_STATUS_SUCCESS) {
2064                 BT_ERR("SDP Cancel request failed [%d]", ret);
2065                 return BLUETOOTH_ERROR_INTERNAL;
2066         }
2067
2068         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_CANCEL_BY_USER, BT_SEARCH_SERVICE,
2069                         service_search_info->addr, BT_ADDRESS_STRING_SIZE);
2070
2071         __bt_free_service_search_info(&service_search_info);
2072
2073         return BLUETOOTH_ERROR_NONE;
2074         BT_DBG("-");
2075 }
2076
2077 int _bt_set_authorization(bluetooth_device_address_t *device_address,
2078                 gboolean authorize)
2079 {
2080         int ret = OAL_STATUS_SUCCESS;
2081         BT_DBG("+");
2082
2083         BT_CHECK_PARAMETER(device_address, return);
2084         BT_INFO("Device to be Trusted? [%d]", authorize);
2085
2086         ret = device_set_authorized((bt_address_t*)device_address, authorize);
2087         if (ret != OAL_STATUS_SUCCESS) {
2088                 BT_ERR("device_set_authorized: %d", ret);
2089                 return BLUETOOTH_ERROR_INTERNAL;
2090         }
2091
2092         return BLUETOOTH_ERROR_NONE;
2093 }
2094
2095 gboolean _bt_is_device_connected(bluetooth_device_address_t *device_address, int svc_type)
2096 {
2097         gboolean is_connected;
2098         oal_service_t svc_id;
2099
2100         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
2101
2102         /*
2103          * TODO: While adding support for new profiles, need to add more
2104          * <svc_type, svc_id> mapping here.
2105          */
2106         switch (svc_type) {
2107         case BT_PROFILE_CONN_HID:
2108                 svc_id = HID_SERVICE_ID;
2109                 break;
2110         case BT_PROFILE_CONN_A2DP:
2111                 svc_id = A2DP_SERVICE_ID; /* Remote is A2DP Sink */
2112                 break;
2113         case BT_PROFILE_CONN_A2DP_SINK:
2114                 svc_id = A2DP_SRC_SERVICE_ID; /* Remote is A2DP Source*/
2115                 break;
2116         case BT_PROFILE_CONN_HSP:
2117                 svc_id = HFP_HS_SERVICE_ID; /* Remote is HFP HF Unit */
2118                 break;
2119 #ifdef TIZEN_GATT_CLIENT
2120         case BT_PROFILE_CONN_GATT:
2121                 return _bt_is_remote_gatt_device_connected(device_address); /* Remote is GATT client or Server */
2122 #endif
2123         default:
2124                 BT_DBG("Unknown svc_type: %d", svc_type);
2125                 return FALSE;
2126         }
2127
2128         is_connected = device_get_svc_conn_state((bt_address_t*)device_address, svc_id);
2129
2130         BT_DBG("svc_type: %d, is_connected: %s",
2131                         svc_type, is_connected ? "TRUE" : "FALSE");
2132
2133         return is_connected;
2134 }
2135
2136 int _bt_rfcomm_reply_conn_authorization(char *address, gboolean reply)
2137 {
2138         bt_address_t bd_addr;
2139         int res;
2140
2141         BT_DBG("+");
2142
2143         retv_if(NULL == address, BLUETOOTH_ERROR_INVALID_PARAM);
2144         _bt_convert_addr_string_to_type(bd_addr.addr, address);
2145         res = device_reply_auth_request(&bd_addr, 0, reply, FALSE);
2146         if (res != OAL_STATUS_SUCCESS) {
2147                 BT_ERR("authorize_response: %d", res);
2148                 return BLUETOOTH_ERROR_INTERNAL;
2149         }
2150
2151         BT_DBG("-");
2152         return BLUETOOTH_ERROR_NONE;
2153 }
2154
2155 int _bt_enable_rssi(bluetooth_device_address_t *addr, int link_type,
2156                 int low_threshold, int in_range_threshold, int high_threshold)
2157 {
2158         int result;
2159         bt_address_t bd_addr;
2160
2161         BT_DBG("+");
2162
2163         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2164
2165         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2166         result = device_enable_rssi_monitoring(&bd_addr, link_type,
2167                         low_threshold, in_range_threshold, high_threshold);
2168         if (result != OAL_STATUS_SUCCESS) {
2169                 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2170                 return BLUETOOTH_ERROR_INTERNAL;
2171         }
2172
2173         BT_DBG("-");
2174         return BLUETOOTH_ERROR_NONE;
2175 }
2176
2177 int _bt_get_rssi_strength(bluetooth_device_address_t *addr, int link_type)
2178 {
2179         int result;
2180         bt_address_t bd_addr;
2181
2182         BT_DBG("+");
2183
2184         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2185
2186         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2187         result = device_get_connected_link_rssi_strength(&bd_addr, link_type);
2188         if (result != OAL_STATUS_SUCCESS) {
2189                 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2190                 return BLUETOOTH_ERROR_INTERNAL;
2191         }
2192
2193         BT_DBG("-");
2194         return BLUETOOTH_ERROR_NONE;
2195 }
2196
2197 int _bt_set_passkey_notification(const char *sender, gboolean enable)
2198 {
2199         int result;
2200
2201         BT_INFO("Set passkey notification(sender:%s, %s)",
2202                         sender, enable ? "Enable" : "Disable");
2203
2204         result = device_enable_gap_auth_notifications(OAL_PASSKEY_DISPLAY, enable);
2205         if (OAL_STATUS_SUCCESS != result) {
2206                 BT_ERR("device_enable_gap_auth_notifications error: [%d]", result);
2207                 return BLUETOOTH_ERROR_INTERNAL;
2208         }
2209
2210         g_free(passkey_watcher);
2211         if (enable == TRUE)
2212                 passkey_watcher = g_strdup(sender);
2213         else
2214                 passkey_watcher = NULL;
2215
2216         return BLUETOOTH_ERROR_NONE;
2217 }
2218
2219 static int __bt_get_device_pin_code(const char *address, char *pin_code)
2220 {
2221         GSList *l = NULL;
2222
2223         BT_CHECK_PARAMETER(address, return);
2224         BT_CHECK_PARAMETER(pin_code, return);
2225
2226         for (l = pin_info_list; l != NULL; l = l->next) {
2227                 bt_pin_code_info_t *pin_info = l->data;
2228
2229                 if (!pin_info || !pin_info->address)
2230                         continue;
2231
2232                 if (g_strcmp0(pin_info->address, address) == 0) {
2233                         g_strlcpy(pin_code, pin_info->pin_code,
2234                                         BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2235                         return BLUETOOTH_ERROR_NONE;
2236                 }
2237         }
2238
2239         return BLUETOOTH_ERROR_NOT_FOUND;
2240 }
2241
2242 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2243                 bluetooth_device_pin_code_t *pin_code)
2244 {
2245         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2246         bt_pin_code_info_t *pin_info = NULL;
2247         GSList *l = NULL;
2248
2249         BT_CHECK_PARAMETER(device_address, return);
2250         BT_CHECK_PARAMETER(pin_code, return);
2251         retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2252                         BLUETOOTH_ERROR_NO_RESOURCES);
2253
2254         _bt_convert_addr_type_to_string(address, device_address->addr);
2255
2256         for (l = pin_info_list; l != NULL; l = l->next) {
2257                 pin_info = l->data;
2258
2259                 if (!pin_info || !pin_info->address)
2260                         continue;
2261
2262                 if (g_strcmp0(pin_info->address, address) == 0) {
2263                         g_free(pin_info->pin_code);
2264                         pin_info->pin_code = g_strdup(pin_code->pin_code);
2265                         return BLUETOOTH_ERROR_NONE;
2266                 }
2267         }
2268
2269         pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2270         pin_info->address = g_strdup(address);
2271         pin_info->pin_code = g_strdup(pin_code->pin_code);
2272         pin_info_list = g_slist_append(pin_info_list, pin_info);
2273
2274         return BLUETOOTH_ERROR_NONE;
2275 }
2276
2277 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2278 {
2279         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2280         bt_pin_code_info_t *pin_info = NULL;
2281         GSList *l = NULL;
2282
2283         BT_DBG("+");
2284
2285         BT_CHECK_PARAMETER(device_address, return);
2286
2287         _bt_convert_addr_type_to_string(address, device_address->addr);
2288
2289         for (l = pin_info_list; l != NULL; l = l->next) {
2290                 pin_info = l->data;
2291
2292                 if (!pin_info || !pin_info->address)
2293                         continue;
2294
2295                 if (g_strcmp0(pin_info->address, address) == 0) {
2296                         pin_info_list = g_slist_remove(pin_info_list, pin_info);
2297                         g_free(pin_info->address);
2298                         g_free(pin_info->pin_code);
2299                         g_free(pin_info);
2300                         break;
2301                 }
2302         }
2303
2304         BT_DBG("-");
2305         return BLUETOOTH_ERROR_NONE;
2306 }
2307
2308 int _bt_device_get_ida(bluetooth_device_address_t *device_address, bluetooth_device_address_t *id_address)
2309 {
2310         int result = BLUETOOTH_ERROR_NONE;
2311         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2312
2313         _bt_convert_addr_type_to_string(address, device_address->addr);
2314
2315         BT_DBG("getting IDA for remote device: [%s]", address);
2316
2317         result = device_get_ida((bt_address_t*)device_address, (bt_address_t*)id_address);
2318
2319         if (result != OAL_STATUS_SUCCESS) {
2320                 BT_ERR("device_get_ida Failed %d", result);
2321                 return BLUETOOTH_ERROR_INTERNAL;
2322         }
2323
2324         return result;
2325 }
2326
2327 #ifdef TIZEN_GATT_CLIENT
2328 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2329 {
2330         GSList *l = NULL;
2331         bt_connected_le_dev_t *dev;
2332
2333         if (!address)
2334                 return NULL;
2335
2336         for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2337                 dev = l->data;
2338
2339                 if (g_strcmp0(dev->address, address) == 0)
2340                         return dev;
2341         }
2342         return NULL;
2343 }
2344
2345 static void __bt_le_conn_param_free(void *data)
2346 {
2347         bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2348
2349         BT_DBG("%s", param->sender);
2350         g_free(param->sender);
2351         g_free(param);
2352 }
2353
2354 static void _bt_add_le_connected_dev_info(const char *address)
2355 {
2356         bt_connected_le_dev_t *dev = NULL;
2357
2358         if (!address)
2359                 return;
2360
2361         dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2362         dev->address = g_strdup(address);
2363
2364         le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2365
2366         return;
2367 }
2368
2369 static void _bt_remove_le_connected_dev_info(const char *address)
2370 {
2371         bt_connected_le_dev_t *dev = NULL;
2372
2373         if (!address)
2374                 return;
2375
2376         dev = __bt_get_le_connected_dev_info(address);
2377         if (!dev)
2378                 return;
2379
2380         g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2381         le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2382         g_free(dev->address);
2383         g_free(dev);
2384
2385         return;
2386 }
2387
2388 static void _bt_handle_le_connected_dev_info(const char *address, gboolean connected)
2389 {
2390         BT_DBG("update le_connected_dev_list");
2391
2392         if (connected)
2393                 _bt_add_le_connected_dev_info(address);
2394         else
2395                 _bt_remove_le_connected_dev_info(address);
2396 }
2397
2398 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2399 {
2400         GSList *l = NULL;
2401         bt_le_conn_param_t *param = NULL;
2402
2403         if (!dev || !sender)
2404                 return NULL;
2405
2406         for (l = dev->senders; l; l = g_slist_next(l)) {
2407                 param = l->data;
2408                 if (g_strcmp0(param->sender, sender) == 0)
2409                         return param;
2410         }
2411
2412         return NULL;
2413 }
2414
2415 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2416 {
2417         bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2418         bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2419
2420         return parama->key > paramb->key;
2421 }
2422
2423
2424 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2425                 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2426 {
2427         bt_connected_le_dev_t *dev = NULL;
2428         bt_le_conn_param_t *param = NULL;
2429         bt_le_conn_param_t *data = NULL;
2430
2431         BT_DBG("+");
2432
2433         if (!address || !sender)
2434                 return BLUETOOTH_ERROR_INVALID_PARAM;
2435
2436         dev = __bt_get_le_connected_dev_info(address);
2437         if (!dev)
2438                 return BLUETOOTH_ERROR_INTERNAL;
2439
2440         param = __bt_get_le_conn_param_info(dev, sender);
2441
2442         data = g_malloc0(sizeof(bt_le_conn_param_t));
2443         data->sender = g_strdup(sender);
2444         data->interval_min = interval_min;
2445         data->interval_max = interval_max;
2446         data->latency = latency;
2447         data->time_out = time_out;
2448         data->key = interval_min + (interval_max - interval_min)/2;
2449
2450         if (param == NULL) {
2451                 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2452                 dev->senders = g_slist_append(dev->senders, data);
2453         } else {
2454                 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2455                 dev->senders = g_slist_remove(dev->senders, param);
2456                 g_free(param->sender);
2457                 g_free(param);
2458                 dev->senders = g_slist_append(dev->senders, data);
2459         }
2460
2461         /* Sorting. First element have the minimum interval */
2462         dev->senders = g_slist_sort(dev->senders,
2463                         (GCompareFunc)__bt_compare_le_conn_param_key);
2464
2465         return BLUETOOTH_ERROR_NONE;
2466 }
2467
2468
2469 static int __bt_le_set_conn_parameter(const char *address,
2470                 float interval_min, float interval_max,
2471                 guint16 latency, guint16 time_out)
2472 {
2473         bt_address_t dev_addr = { {0} };
2474         guint32 min, max, to;
2475
2476         BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2477                         interval_min, interval_max, latency, time_out);
2478
2479         min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2480         max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2481         to = time_out / BT_LE_CONN_TO_SPLIT;
2482
2483         BT_INFO("updating: Min interval: %d, Max interval: %d, Latency: %d, Supervision timeout: %d",
2484                         min, max, latency, to);
2485
2486         _bt_convert_addr_string_to_type(dev_addr.addr, address);
2487
2488         return gattc_conn_param_update(&dev_addr, min, max, latency, to);
2489 }
2490
2491 int _bt_remove_le_conn_param_info(const char *address, const char *sender)
2492 {
2493         bt_connected_le_dev_t *dev = NULL;
2494         bt_le_conn_param_t *param = NULL;
2495
2496         if (!address || !sender)
2497                 return BLUETOOTH_ERROR_INVALID_PARAM;
2498
2499         dev = __bt_get_le_connected_dev_info(address);
2500         if (!dev)
2501                 return BLUETOOTH_ERROR_INTERNAL;
2502
2503         param = __bt_get_le_conn_param_info(dev, sender);
2504         if (param) {
2505                 BT_DBG("Remove param %s %s ", address, sender);
2506                 dev->senders = g_slist_remove(dev->senders, param);
2507                 g_free(param->sender);
2508                 g_free(param);
2509         }
2510
2511         return BLUETOOTH_ERROR_NONE;
2512 }
2513
2514 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2515                 bluetooth_le_connection_param_t *param)
2516 {
2517         if (param == NULL)
2518                 return BLUETOOTH_ERROR_INVALID_PARAM;
2519
2520         memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2521
2522         switch (mode) {
2523         case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2524                 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2525                 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2526                 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2527                 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2528                 break;
2529
2530         case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2531                 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2532                 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2533                 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2534                 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2535                 break;
2536
2537         case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2538                 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2539                 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2540                 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2541                 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2542                 break;
2543
2544         default:
2545                 BT_ERR("Unhandled mode : %d", mode);
2546                 break;
2547         }
2548
2549         return BLUETOOTH_ERROR_NONE;
2550 }
2551
2552 int _bt_le_connection_update(const char *sender,
2553                 unsigned char *device_address,
2554                 float interval_min, float interval_max,
2555                 guint16 latency, guint16 time_out)
2556 {
2557         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2558         guint32 min_supervision_to;
2559         bt_connected_le_dev_t *dev = NULL;
2560         bt_le_conn_param_t *param = NULL;
2561         int ret = BLUETOOTH_ERROR_NONE;
2562
2563         BT_CHECK_PARAMETER(device_address, return);
2564
2565         BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2566                         sender, interval_min, interval_max, latency, time_out);
2567
2568         if (interval_min > interval_max ||
2569                         interval_min < BT_LE_CONN_INTERVAL_MIN ||
2570                         interval_max > BT_LE_CONN_INTERVAL_MAX) {
2571                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2572                 goto fail;
2573         }
2574
2575         if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2576                         time_out > BT_LE_CONN_SUPER_TO_MAX) {
2577                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2578                 goto fail;
2579         }
2580
2581         if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2582                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2583                 goto fail;
2584         }
2585
2586         /*
2587          * The Supervision_Timeout in milliseconds shall be larger than
2588          * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2589          * where Conn_Interval_Max is given in milliseconds.
2590          */
2591
2592         min_supervision_to = (1 + latency) * interval_max * 2;
2593         if (time_out <= min_supervision_to) {
2594                 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2595                 goto fail;
2596         }
2597
2598         _bt_convert_addr_type_to_string(address, device_address);
2599         BT_DBG("Remote device address: %s", address);
2600
2601         _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2602
2603         dev = __bt_get_le_connected_dev_info(address);
2604         if (dev == NULL) {
2605                 BT_DBG("device not found in the list");
2606                 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2607                 goto fail;
2608         }
2609
2610         if (g_slist_length(dev->senders) == 1)
2611                 goto update;
2612         else {
2613                 param = dev->senders->data;
2614
2615                 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2616
2617                 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2618                         BT_DBG("Skip due to same interval");
2619                         return ret;
2620                 }
2621
2622                 interval_min = param->interval_min;
2623                 interval_max = param->interval_max;
2624         }
2625
2626 update:
2627         ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2628
2629         if (ret != OAL_STATUS_SUCCESS) {
2630                 _bt_remove_le_conn_param_info(address, sender);
2631                 BT_DBG("fail to update the LE connection parameter");
2632                 ret = BLUETOOTH_ERROR_INTERNAL;
2633                 goto fail;
2634         }
2635
2636         BT_DBG("updated LE connection parameter");
2637         dev->interval_min = interval_min;
2638         dev->interval_max = interval_max;
2639
2640         return BLUETOOTH_ERROR_NONE;
2641 fail:
2642         return ret;
2643 }
2644
2645 int _bt_disconnect_device(bluetooth_device_address_t *device_address)
2646 {
2647         int result = OAL_STATUS_SUCCESS;
2648
2649         BT_DBG("+");
2650
2651         retv_if(!device_address, BLUETOOTH_ERROR_INVALID_PARAM);
2652
2653         result = device_disconnect((bt_address_t *)device_address);
2654         if (result != OAL_STATUS_SUCCESS) {
2655                 BT_DBG("Failed to disconnect device");
2656                 return BLUETOOTH_ERROR_INTERNAL;
2657         }
2658
2659         BT_DBG("-");
2660         return BLUETOOTH_ERROR_NONE;
2661 }
2662
2663 static gboolean __bt_ignore_auto_pairing_request(const char *address)
2664 {
2665         gchar *buffer;
2666         char **lines;
2667         int i;
2668         char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
2669         char *temp_buffer;
2670         FILE *fp;
2671         long size;
2672         size_t result;
2673
2674         BT_DBG("+\n");
2675
2676         if (address == NULL)
2677                 return FALSE;
2678
2679         /* Get the LAP(Lower Address part) */
2680         /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
2681            a "," */
2682         snprintf(lap_address, sizeof(lap_address), ",%s", address);
2683
2684         fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
2685
2686         if (fp == NULL) {
2687                 BT_ERR("fopen failed \n");
2688                 return FALSE;
2689         }
2690
2691         fseek(fp, 0, SEEK_END);
2692         size = ftell(fp);
2693         rewind(fp);
2694
2695         if (size < 0) {
2696                 BT_ERR("Get file size failed \n");
2697                 fclose(fp);
2698                 return FALSE;
2699         }
2700
2701         buffer = g_malloc0(sizeof(char) * size);
2702         result = fread((char *)buffer, 1, size, fp);
2703         fclose(fp);
2704         if (result != size) {
2705                 BT_ERR("Read Error\n");
2706                 g_free(buffer);
2707                 return FALSE;
2708         }
2709
2710         BT_DBG("Buffer = %s\n", buffer);
2711
2712         lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
2713         g_free(buffer);
2714
2715         if (lines == NULL)
2716                 return FALSE;
2717
2718         /* Write the data and insert new device data */
2719         for (i = 0; lines[i] != NULL; i++) {
2720                 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
2721                         temp_buffer = g_strconcat(lines[i], lap_address, NULL);
2722                         g_free(lines[i]);
2723                         lines[i] = temp_buffer;
2724                 }
2725         }
2726         buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
2727         g_strfreev(lines);
2728         /* Fix : NULL_RETURNS */
2729         retv_if(buffer == NULL, FALSE);
2730
2731         fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
2732
2733         if (fp == NULL) {
2734                 BT_ERR("fopen failed \n");
2735                 g_free(buffer);
2736                 return FALSE;
2737         }
2738
2739         BT_DBG("Buffer = %s\n", buffer);
2740         fwrite(buffer, 1, strlen(buffer), fp);
2741         fclose(fp);
2742
2743         g_free(buffer);
2744
2745         BT_DBG("-\n");
2746
2747         return FALSE;
2748 }
2749 #endif