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