[bt-service]Added support to enable for Auth notifications
[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                                  !incoming_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         }
1021         case OAL_EVENT_RSSI_MONITORING_ENABLED: {
1022                 event_dev_rssi_info_t *ev = event_data;
1023                 char address[BT_ADDRESS_STRING_SIZE];
1024                 GVariant *param;
1025
1026                 ret_if(NULL == ev);
1027
1028                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1029                 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1030                                 address, ev->link_type, TRUE);
1031                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1032                 break;
1033         }
1034         case OAL_EVENT_RSSI_MONITORING_DISABLED: {
1035                 event_dev_rssi_info_t *ev = event_data;
1036                 char address[BT_ADDRESS_STRING_SIZE];
1037                 GVariant *param;
1038
1039                 ret_if(NULL == ev);
1040
1041                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1042                 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1043                                 address, ev->link_type, FALSE);
1044                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1045                 break;
1046         }
1047         case OAL_EVENT_RSSI_ALERT_RECEIVED: {
1048                 event_dev_rssi_info_t *ev = event_data;
1049                 char address[BT_ADDRESS_STRING_SIZE];
1050                 GVariant *param;
1051
1052                 ret_if(NULL == ev);
1053
1054                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1055                 param = g_variant_new("(isiii)", BLUETOOTH_ERROR_NONE,
1056                                 address, ev->link_type, ev->alert_type, ev->rssi);
1057                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ALERT, param);
1058                 break;
1059         }
1060         case OAL_EVENT_RAW_RSSI_RECEIVED: {
1061                 event_dev_rssi_info_t *ev = event_data;
1062                 char address[BT_ADDRESS_STRING_SIZE];
1063                 GVariant *param;
1064
1065                 ret_if(NULL == ev);
1066
1067                 _bt_convert_addr_type_to_string(address, ev->address.addr);
1068                 param = g_variant_new("(isii)", BLUETOOTH_ERROR_NONE,
1069                                 address, ev->link_type, ev->rssi);
1070                 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RAW_RSSI, param);
1071                 break;
1072         }
1073         default:
1074                 BT_INFO("Unhandled event..");
1075         }
1076 }
1077
1078 /* Legacy Pairing event handler */
1079 static void __bt_device_pin_request_callback(remote_device_t* pin_req_event)
1080 {
1081         GVariant *param;
1082         char address[BT_ADDRESS_STRING_SIZE];
1083         char pin_code[BLUETOOTH_PIN_CODE_MAX_LENGTH + 1];
1084         BT_DBG("+");
1085
1086         _bt_convert_addr_type_to_string(address, pin_req_event->address.addr);
1087
1088         BT_INFO("Address[%s]", address);
1089         BT_INFO("Name[%s]", pin_req_event->name);
1090         BT_INFO("COD[%d]", pin_req_event->cod);
1091
1092         if (trigger_pairing_info) {
1093                 /* BTAPI support only one pairing at a time */
1094                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1095                 BT_ERR("New PIN request address [%s]", address);
1096                 device_reject_pin_request(&pin_req_event->address);
1097                 return;
1098         }
1099
1100         /* If user initiated bonding and auto response is possible, just reply with default 0000*/
1101         if (_bt_is_bonding_device_address(address) == TRUE &&
1102                         _bt_agent_is_auto_response(pin_req_event->cod, address, pin_req_event->name)) {
1103                 /* Note: Currently even if SYSPOPUP is supported, we use Fixed PIN "0000" for basic pairing
1104                    as BT SYSPOPUP is currently not working for PIN KEY entry in Tizen platform. This needs
1105                    to be checked and fixed apropriately */
1106                 _bt_set_autopair_status_in_bonding_info(TRUE);
1107                 device_accept_pin_request(&pin_req_event->address, "0000");
1108         } else if (_bt_agent_is_hid_keyboard(pin_req_event->cod)) {
1109                 BT_DBG("Remote Device is HID keyboard Type..");
1110                 char str_passkey[BT_PASSKEY_MAX_LENGTH + 1] = { 0 };
1111
1112                 if (_bt_agent_generate_passkey(str_passkey,
1113                                         BT_PASSKEY_MAX_LENGTH) != 0) {
1114                         device_reject_pin_request(&pin_req_event->address);
1115                         goto done;
1116                 }
1117                 device_accept_pin_request(&pin_req_event->address, str_passkey);
1118
1119                 BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1120                 param = g_variant_new("(isss)", BLUETOOTH_ERROR_NONE, address, pin_req_event->name, str_passkey);
1121                 _bt_send_event(BT_ADAPTER_EVENT,
1122                                 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1123                 BT_DBG("Key board pairing in process");
1124         } else if (BLUETOOTH_ERROR_NONE == __bt_get_device_pin_code(address, pin_code)) {
1125                 BT_DBG("Use stored PIN code [%s]", pin_code);
1126                 device_accept_pin_request(&pin_req_event->address, pin_code);
1127         } else {
1128                 if (_bt_is_bonding_device_address(address) == TRUE) {
1129                         BT_DBG("Show Pin entry");
1130                         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1131                         trigger_pairing_info->addr = g_strdup(address);
1132                         trigger_pairing_info->is_ssp = FALSE;
1133
1134                         BT_DBG("Send BLUETOOTH_EVENT_PIN_REQUEST");
1135                         param = g_variant_new("(iss)", BLUETOOTH_ERROR_NONE, address, pin_req_event->name);
1136                         _bt_send_event(BT_ADAPTER_EVENT,
1137                                         BLUETOOTH_EVENT_PIN_REQUEST, param);
1138                 }
1139         }
1140
1141 done:
1142         _bt_agent_release_memory();
1143         BT_DBG("-");
1144 }
1145
1146 /* SSP Pairing event handler */
1147 static void __bt_device_ssp_passkey_entry_callback(remote_device_t* dev_info)
1148 {
1149         GVariant *param;
1150         gchar address[BT_ADDRESS_STR_LEN];
1151         char *p_addr;
1152         gchar *name;
1153         int result = BLUETOOTH_ERROR_NONE;
1154         BT_DBG("+");
1155
1156         _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1157         p_addr = address;
1158         name = dev_info->name;
1159
1160         BT_INFO("Address[%s]", address);
1161         BT_INFO("Name[%s]", name);
1162         BT_INFO("COD[%d]", dev_info->cod);
1163
1164         if (trigger_pairing_info) {
1165                 /* BTAPI support only one pairing at a time */
1166                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1167                 BT_ERR("New PIN request address [%s]", address);
1168                 device_reject_pin_request(&dev_info->address);
1169                 BT_DBG("-");
1170                 return;
1171         }
1172
1173         /* Set pairing data */
1174         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1175         trigger_pairing_info->addr = g_strdup(address);
1176         trigger_pairing_info->is_ssp = TRUE;
1177
1178         param = g_variant_new("(iss)", result, p_addr, name);
1179         _bt_send_event(BT_ADAPTER_EVENT,
1180                         BLUETOOTH_EVENT_PASSKEY_REQUEST, param);
1181         BT_DBG("-");
1182 }
1183
1184 static void __bt_device_ssp_passkey_confirmation_callback(event_dev_passkey_t *dev_info)
1185 {
1186         GVariant *param;
1187         gchar address[BT_ADDRESS_STR_LEN];
1188         char *p_addr;
1189         gchar *name;
1190         char str_passkey[7];
1191         int result = BLUETOOTH_ERROR_NONE;
1192         BT_DBG("+");
1193
1194         _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1195         p_addr = address;
1196         name = dev_info->device_info.name;
1197
1198         BT_INFO("Address[%s]", address);
1199         BT_INFO("Name[%s]", name);
1200         BT_INFO("COD[%d]", dev_info->device_info.cod);
1201
1202         if (trigger_pairing_info) {
1203                 /* BTAPI support only one pairing at a time */
1204                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1205                 BT_ERR("New PIN request address [%s]", address);
1206                 device_reject_pin_request(&dev_info->device_info.address);
1207                 BT_DBG("-");
1208                 return;
1209         }
1210
1211         /* Set pairing data */
1212         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1213         trigger_pairing_info->addr = g_strdup(address);
1214         trigger_pairing_info->is_ssp = TRUE;
1215
1216         BT_DBG("Send BLUETOOTH_EVENT_PASSKEY_CONFIRMATION");
1217         snprintf(str_passkey, sizeof(str_passkey), "%.6d", dev_info->pass_key);
1218
1219         param = g_variant_new("(isss)", result, p_addr, name, str_passkey);
1220         _bt_send_event(BT_ADAPTER_EVENT,
1221                         BLUETOOTH_EVENT_PASSKEY_CONFIRM_REQUEST, param);
1222         BT_DBG("-");
1223 }
1224
1225 static void __bt_device_ssp_passkey_display_callback(event_dev_passkey_t *dev_info)
1226 {
1227         GVariant *param;
1228         gchar address[BT_ADDRESS_STR_LEN];
1229         char *p_addr;
1230         gchar *name;
1231         char str_passkey[7];
1232         int result = BLUETOOTH_ERROR_NONE;
1233         BT_DBG("+");
1234
1235         _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1236         p_addr = address;
1237         name = dev_info->device_info.name;
1238
1239         BT_INFO("Address[%s]", address);
1240         BT_INFO("Name[%s]", name);
1241         BT_INFO("COD[%d]", dev_info->device_info.cod);
1242
1243         if (trigger_pairing_info) {
1244                 /* BTAPI support only one pairing at a time */
1245                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1246                 BT_ERR("New PIN request address [%s]", address);
1247                 device_reject_pin_request(&dev_info->device_info.address);
1248                 BT_DBG("-");
1249                 return;
1250         }
1251
1252         /* Set pairing data */
1253         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1254         trigger_pairing_info->addr = g_strdup(address);
1255         trigger_pairing_info->is_ssp = TRUE;
1256
1257         BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1258         snprintf(str_passkey, sizeof(str_passkey), "%06d", dev_info->pass_key);
1259
1260         param = g_variant_new("(isss)", result, p_addr, name, str_passkey);
1261         if (passkey_watcher) {
1262                 BT_INFO("Send passkey to %s", passkey_watcher);
1263                 _bt_send_event_to_dest(passkey_watcher, BT_ADAPTER_EVENT,
1264                                 BLUETOOTH_EVENT_PASSKEY_NOTIFICATION, param);
1265         } else {
1266                 _bt_send_event(BT_ADAPTER_EVENT,
1267                                 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1268         }
1269         BT_DBG("-");
1270 }
1271
1272 static void __bt_device_ssp_consent_callback(remote_device_t* dev_info)
1273 {
1274         gchar address[BT_ADDRESS_STR_LEN];
1275         gchar *name;
1276         int local_major;
1277         int local_minor;
1278         int cod;
1279         BT_DBG("+");
1280
1281         _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1282         name = dev_info->name;
1283         cod = dev_info->cod;
1284
1285         BT_INFO("Address[%s]", address);
1286         BT_INFO("Name[%s]", name);
1287         BT_INFO("COD[%d]", cod);
1288
1289         if (trigger_pairing_info) {
1290                 /* BTAPI support only one pairing at a time */
1291                 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1292                 BT_ERR("New PIN request address [%s]", address);
1293                 device_reject_pin_request(&dev_info->address);
1294                 BT_DBG("-");
1295                 return;
1296         }
1297
1298         /* Set pairing data */
1299         trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1300         trigger_pairing_info->addr = g_strdup(address);
1301         trigger_pairing_info->is_ssp = TRUE;
1302
1303         local_major = ((cod >> 8) & 0x001f);
1304         local_minor = (cod & 0x00fc);
1305         BT_DBG("SSP_CONSENT: Major type=[0x%x] and Minor type=[0x%x]", local_major, local_minor);
1306
1307         /*TODO: BLUETOOTH_EVENT_SSP_CONSENT_REQUEST to be handled in Tizen */
1308         BT_DBG("-");
1309 }
1310
1311 static void __bt_device_conn_state_changed_callback(event_dev_conn_status_t *acl_event,
1312                 gboolean connected, unsigned char type)
1313 {
1314         gchar address[BT_ADDRESS_STR_LEN];
1315         int result = BLUETOOTH_ERROR_NONE;
1316         GVariant *param = NULL;
1317         bt_device_conn_info_t conn_info;
1318
1319         BT_DBG("+");
1320         _bt_convert_addr_type_to_string(address, acl_event->address.addr);
1321
1322         if (connected) {
1323                 param = g_variant_new("(isy)", result, address, type);
1324                 _bt_send_event(BT_DEVICE_EVENT,
1325                                 BLUETOOTH_EVENT_DEVICE_CONNECTED,
1326                                 param);
1327         } else {
1328                 param = g_variant_new("(isy)", result, address, type);
1329                 _bt_send_event(BT_DEVICE_EVENT,
1330                                 BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1331                                 param);
1332         }
1333
1334         conn_info.connected = connected;
1335         conn_info.type = type;
1336         /* Update local cache */
1337         _bt_update_remote_dev_property(address, DEV_PROP_CONNECTED, (void *)&conn_info);
1338
1339         BT_DBG("-");
1340 }
1341
1342 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble)
1343 {
1344         bt_remote_dev_info_t *dev_info = NULL;
1345         int result = BLUETOOTH_ERROR_NONE;
1346         GVariant *param = NULL;
1347         GVariant *uuids = NULL;
1348         GVariant *manufacturer_data = NULL;
1349         GVariantBuilder *builder = NULL;
1350         unsigned int i;
1351
1352         BT_INFO("+");
1353         ret_if(_bt_is_discovering() == FALSE);
1354         ret_if(event_data == NULL);
1355
1356         dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
1357
1358         if (is_ble) {
1359                 event_ble_dev_found_t * oal_ble_dev = event_data;
1360                 BT_INFO("Device type [%d]", oal_ble_dev->device_info.type);
1361
1362                 _bt_copy_remote_dev(dev_info, &oal_ble_dev->device_info);
1363
1364                 dev_info->manufacturer_data_len = oal_ble_dev->adv_len;
1365                 if (dev_info->manufacturer_data_len)
1366                         dev_info->manufacturer_data = g_memdup(oal_ble_dev->adv_data,
1367                                         dev_info->manufacturer_data_len);
1368                 else
1369                         dev_info->manufacturer_data = NULL;
1370                 BT_DBG("----Advertising Data Length: %d", dev_info->manufacturer_data_len);
1371         } else {
1372                 event_dev_found_t * oal_dev = event_data;
1373                 _bt_copy_remote_dev(dev_info, &oal_dev->device_info);
1374         }
1375
1376         /* If Remote device name is NULL or still RNR is not done then display address as name. */
1377         if (dev_info->name == NULL)
1378                 dev_info->name = g_strdup(dev_info->address);
1379         BT_DBG("Name %s", dev_info->name);
1380
1381         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1382         for (i = 0; i < dev_info->uuid_count; i++)
1383                 g_variant_builder_add(builder, "s", dev_info->uuids[i]);
1384
1385         uuids = g_variant_new("as", builder);
1386         g_variant_builder_unref(builder);
1387
1388         manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1389                         dev_info->manufacturer_data,
1390                         dev_info->manufacturer_data_len,
1391                         TRUE,
1392                         NULL, NULL);
1393
1394         param = g_variant_new("(isunsbub@asn@ay)", result,
1395                         dev_info->address,
1396                         dev_info->class,
1397                         dev_info->rssi,
1398                         dev_info->name,
1399                         dev_info->paired,
1400                         dev_info->connected,
1401                         dev_info->trust,
1402                         uuids,
1403                         dev_info->manufacturer_data_len,
1404                         manufacturer_data);
1405
1406         _bt_send_event(BT_ADAPTER_EVENT,
1407                         BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
1408                         param);
1409
1410         _bt_free_remote_dev(dev_info);
1411         BT_DBG("-");
1412 }
1413
1414 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info)
1415 {
1416         gchar address[BT_ADDRESS_STR_LEN];
1417         int result = BLUETOOTH_ERROR_NONE;
1418         GVariant *param = NULL;
1419         int event;
1420         BT_DBG("+");
1421
1422         _bt_convert_addr_type_to_string(address, info->address.addr);
1423
1424         /* Update local cache */
1425         _bt_update_remote_dev_property(address, DEV_PROP_TRUST, (void *)&trusted);
1426
1427         param = g_variant_new("(is)", result, address);
1428         event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1429                 BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1430         /* Send event to application */
1431         _bt_send_event(BT_DEVICE_EVENT,
1432                         event,
1433                         param);
1434
1435         BT_DBG("-");
1436 }
1437
1438 static void __bt_free_pairing_info(bt_pairing_data_t **p_info)
1439 {
1440         BT_DBG("+");
1441         bt_pairing_data_t * info = *p_info;
1442         if (info) {
1443                 if (info->addr)
1444                         g_free(info->addr);
1445
1446                 g_free(info);
1447         }
1448         *p_info = NULL;
1449         BT_DBG("-");
1450 }
1451
1452 static void __bt_free_bond_info(uint8_t type)
1453 {
1454         BT_INFO("+");
1455
1456         switch (type) {
1457         case BT_DEVICE_BOND_INFO:
1458                 if (!trigger_bond_info)
1459                         break;
1460
1461                 if (trigger_bond_info->addr)
1462                         g_free(trigger_bond_info->addr);
1463                 if (trigger_bond_info->dev_addr)
1464                         g_free(trigger_bond_info->dev_addr);
1465                 if (trigger_bond_info->dev_info)
1466                         _bt_free_remote_dev(trigger_bond_info->dev_info);
1467                 g_free(trigger_bond_info);
1468                 trigger_bond_info = NULL;
1469                 break;
1470         case BT_DEVICE_INCOMING_BOND_INFO:
1471                 if (!incoming_bond_info)
1472                         break;
1473
1474                 if (incoming_bond_info->dev_info)
1475                         _bt_free_remote_dev(incoming_bond_info->dev_info);
1476                 g_free(incoming_bond_info);
1477                 incoming_bond_info = NULL;
1478                 break;
1479         case BT_DEVICE_UNBOND_INFO:
1480                 if (!trigger_unbond_info)
1481                         break;
1482
1483                 if (trigger_unbond_info->addr)
1484                         g_free(trigger_unbond_info->addr);
1485                 if (trigger_unbond_info->dev_addr)
1486                         g_free(trigger_unbond_info->dev_addr);
1487                 if (trigger_unbond_info->dev_info)
1488                         _bt_free_remote_dev(trigger_unbond_info->dev_info);
1489                 g_free(trigger_unbond_info);
1490                 trigger_unbond_info = NULL;
1491                 break;
1492         }
1493         BT_INFO("-");
1494 }
1495
1496 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info)
1497 {
1498         bt_service_search_info_data_t * info = *p_info;
1499         if (info) {
1500                 if (info->addr) {
1501                         g_free(info->addr);
1502                         info->addr = NULL;
1503                 }
1504
1505                 if (info->dev_addr) {
1506                         g_free(info->dev_addr);
1507                         info->dev_addr = NULL;
1508                 }
1509
1510                 if (info->dev_info) {
1511                         _bt_free_remote_dev(info->dev_info);
1512                         info->dev_info = NULL;
1513                 }
1514
1515                 g_free(info);
1516         }
1517         *p_info = NULL;
1518 }
1519
1520 static int __bt_device_handle_bond_state(void)
1521 {
1522         BT_INFO("Current Bond state: %d", bt_device_bond_state);
1523         int ret = OAL_STATUS_INTERNAL_ERROR;
1524
1525         switch (bt_device_bond_state) {
1526         case BT_DEVICE_BOND_STATE_CANCEL_DISCOVERY:
1527                 /*TODO:Bonding during discovery: Unhandled!!*/
1528                 BT_INFO("Bonding during discovery: Unhandled!!");
1529                 break;
1530         case BT_DEVICE_BOND_STATE_DISCOVERY_CANCELLED:
1531                 /*TODO:Bonding during discovery: Unhandled!!*/
1532                 BT_INFO("Bonding during discovery: Unhandled!!");
1533                 break;
1534         case BT_DEVICE_BOND_STATE_REMOVE_BONDING:
1535                 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVED_BONDING;
1536                 ret = device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
1537                 if (ret != OAL_STATUS_SUCCESS)
1538                         ret = __bt_device_handle_bond_state();
1539                 break;
1540         case BT_DEVICE_BOND_STATE_REMOVED_BONDING:
1541                 bt_device_bond_state = BT_DEVICE_BOND_STATE_NONE;
1542                 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
1543                 /* Bonding procedure was started but unfortunately could not complete.
1544                    Basically removed bonding was success, but create bond request could not proceed
1545                    So lets cleanup the context */
1546                 if (ret != OAL_STATUS_SUCCESS) {
1547                         BT_ERR("Create Bond procedure could not suceed");
1548                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1549                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1550                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1551                         __bt_free_pairing_info(&trigger_pairing_info);
1552                 }
1553                 break;
1554         case BT_DEVICE_BOND_STATE_NONE:
1555                 BT_INFO("Create Bond failed!!");
1556                 if (trigger_bond_info) {
1557                         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1558                                         trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1559                         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1560                         __bt_free_pairing_info(&trigger_pairing_info);
1561                 }
1562                 break;
1563         default:
1564                 break;
1565         }
1566
1567         if (ret != OAL_STATUS_SUCCESS)
1568                 return BLUETOOTH_ERROR_INTERNAL;
1569         else
1570                 return BLUETOOTH_ERROR_NONE;
1571 }
1572
1573 int _bt_device_get_bonded_device_info(bluetooth_device_address_t *addr)
1574 {
1575         int result;
1576         bt_address_t bd_addr;
1577
1578         BT_DBG("+");
1579
1580         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
1581
1582         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
1583         result = device_query_attributes(&bd_addr);
1584         if (result != OAL_STATUS_SUCCESS) {
1585                 BT_ERR("device_query_attributes error: [%d]", result);
1586                 return BLUETOOTH_ERROR_INTERNAL;
1587         }
1588
1589         BT_DBG("-");
1590         return BLUETOOTH_ERROR_NONE;
1591 }
1592
1593 int _bt_set_alias(bluetooth_device_address_t *device_address, const char *alias)
1594 {
1595         int ret;
1596         char address[BT_ADDRESS_STRING_SIZE];
1597
1598         BT_DBG("+");
1599         BT_CHECK_PARAMETER(alias, return);
1600
1601         ret = device_set_alias((bt_address_t *)device_address, (char *)alias);
1602         if (ret != OAL_STATUS_SUCCESS) {
1603                 BT_ERR("device_set_alias: %d", ret);
1604                 return BLUETOOTH_ERROR_INTERNAL;
1605         }
1606
1607         /* Update local cache */
1608         _bt_convert_addr_type_to_string(address, device_address->addr);
1609         _bt_update_remote_dev_property(address, DEV_PROP_ALIAS, (void *)alias);
1610
1611         BT_DBG("-");
1612         return BLUETOOTH_ERROR_NONE;
1613 }
1614
1615 int _bt_bond_device(bluetooth_device_address_t *device_address,
1616                 unsigned short conn_type, GArray **out_param1)
1617 {
1618         int result = BLUETOOTH_ERROR_NONE;
1619         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1620         bluetooth_device_info_t dev_info;
1621         BT_DBG("+");
1622
1623         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1624
1625         /* If bonding or discovery already going on */
1626         if (trigger_bond_info || _bt_is_discovering()) {
1627                 BT_ERR("Device is buzy, bonding can not proceed now..");
1628                 result = BLUETOOTH_ERROR_DEVICE_BUSY;
1629                 goto fail;
1630         }
1631
1632         /*TODO: If unbonding with same device going on */
1633         _bt_convert_addr_type_to_string(address, device_address->addr);
1634
1635         trigger_bond_info = g_malloc0(sizeof(bt_bond_data_t));
1636         trigger_bond_info->addr = g_strdup(address);
1637         trigger_bond_info->conn_type = conn_type;
1638         trigger_bond_info->is_device_creating = TRUE;
1639         trigger_bond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1640         trigger_bond_info->dev_info = NULL;
1641
1642         /* Ready to initiate bonding */
1643
1644         /* In Tizen, we will first remove bond and then attempt to create bond to keep
1645            consistency with bluedroid. Even if remove bond fails due to device not already
1646            bonded, then straight away create bond is triggered. This is because, remove bond
1647            is handled differently in bluedroid and bluez. In Bluez, if device is
1648            already removed, remove bond call fails.
1649            However in bluedroid, remove bond on already removed device returns success. So we will
1650            handle the cases transparently*/
1651         bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVE_BONDING;
1652         bond_retry_count = 0;
1653         result = __bt_device_handle_bond_state();
1654
1655         if (result != BLUETOOTH_ERROR_NONE)
1656                 goto fail;
1657
1658         BT_DBG("-");
1659         return result;
1660
1661 fail:
1662         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1663         memcpy(dev_info.device_address.addr, device_address->addr,
1664                         BLUETOOTH_ADDRESS_LENGTH);
1665
1666         g_array_append_vals(*out_param1, &dev_info,
1667                         sizeof(bluetooth_device_info_t));
1668         __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1669
1670         BT_DBG("-");
1671         return result;
1672 }
1673
1674 int _bt_unbond_device(bluetooth_device_address_t *device_address,
1675                 GArray **out_param1)
1676 {
1677         int result = OAL_STATUS_SUCCESS;
1678         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1679         bluetooth_device_info_t dev_info;
1680         BT_INFO("+");
1681
1682         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1683
1684         _bt_convert_addr_type_to_string(address, device_address->addr);
1685
1686         trigger_unbond_info = g_malloc0(sizeof(bt_bond_data_t));
1687         trigger_unbond_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1688         trigger_unbond_info->addr = g_strdup(address);
1689         trigger_unbond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1690
1691         /* Check if Bonding is already going on, we should not abruptly remove bonding*/
1692         if (trigger_bond_info && strncmp(trigger_bond_info->addr, trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE) == 0) {
1693                 BT_ERR("Bonding with same device already ongoing");
1694                 result = BLUETOOTH_ERROR_PERMISSION_DEINED;
1695                 goto fail;
1696         }
1697
1698         result = device_destroy_bond((bt_address_t *)device_address);
1699         if (result != OAL_STATUS_SUCCESS)
1700                 goto fail;
1701
1702         return result;
1703
1704 fail:
1705         memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1706         _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1707                         trigger_unbond_info->addr);
1708
1709         g_array_append_vals(*out_param1, &dev_info,
1710                         sizeof(bluetooth_device_info_t));
1711         __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
1712
1713         return result;
1714 }
1715
1716 int _bt_cancel_bonding(void)
1717 {
1718         int result = OAL_STATUS_SUCCESS;
1719         BT_INFO("+");
1720
1721         retv_if(trigger_bond_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1722
1723         result = device_stop_bond((bt_address_t *)trigger_bond_info->dev_addr);
1724
1725         if (result == OAL_STATUS_SUCCESS)
1726                 trigger_bond_info->is_cancelled_by_user = TRUE;
1727
1728         return result;
1729 }
1730
1731 int _bt_passkey_reply(const char *passkey, gboolean cnfm_reply)
1732 {
1733         bluetooth_device_address_t device_address;
1734         int ret = OAL_STATUS_SUCCESS;
1735         BT_INFO("reply: %d", cnfm_reply);
1736
1737         retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1738         retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1739
1740         _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1741
1742         if (trigger_pairing_info->is_ssp) {
1743                 if (cnfm_reply)
1744                         ret = device_accept_passkey_entry((bt_address_t *)&device_address, atoi(passkey));
1745                 else
1746                         ret = device_reject_passkey_entry((bt_address_t *)&device_address);
1747                 trigger_pairing_info->is_ssp = FALSE;
1748         } else {
1749                 if (cnfm_reply)
1750                         ret = device_accept_pin_request((bt_address_t *)&device_address, passkey);
1751                 else
1752                         ret = device_reject_pin_request((bt_address_t *)&device_address);
1753         }
1754
1755         __bt_free_pairing_info(&trigger_pairing_info);
1756
1757         if (ret != OAL_STATUS_SUCCESS) {
1758                 BT_ERR("_bt_device_handle_passkey_reply: err [%d]", ret);
1759                 return BLUETOOTH_ERROR_INTERNAL;
1760         }
1761
1762         BT_INFO("-");
1763         return BLUETOOTH_ERROR_NONE;
1764 }
1765
1766 int _bt_passkey_confirmation_reply(gboolean cnfm_reply)
1767 {
1768         BT_INFO("BT_PASSKEY_CONFIRMATION_REPLY");
1769         bluetooth_device_address_t device_address;
1770         int ret = OAL_STATUS_SUCCESS;
1771         BT_INFO("reply: %d", cnfm_reply);
1772
1773         retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1774         retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1775
1776         _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1777
1778         ret = device_reply_passkey_confirmation((bt_address_t *)&device_address, cnfm_reply);
1779
1780         __bt_free_pairing_info(&trigger_pairing_info);
1781         if (ret != OAL_STATUS_SUCCESS) {
1782                 BT_ERR("_bt_device_handle_passkey_confirmation_reply: err [%d]", ret);
1783                 return BLUETOOTH_ERROR_INTERNAL;
1784         }
1785
1786         BT_INFO("-");
1787         return BLUETOOTH_ERROR_NONE;
1788 }
1789
1790 gboolean _bt_device_is_pairing(void)
1791 {
1792         return (trigger_pairing_info) ? TRUE : FALSE;
1793 }
1794
1795 gboolean _bt_device_is_bonding(void)
1796 {
1797         return (trigger_bond_info) ? TRUE : FALSE;
1798 }
1799
1800 gboolean _bt_is_bonding_device_address(const char *address)
1801 {
1802         if (trigger_bond_info == NULL || trigger_bond_info->addr == NULL)
1803                 return FALSE;
1804
1805         if (g_strcmp0(trigger_bond_info->addr, address) == 0) {
1806                 BT_DBG("[%s]  is bonding device", address);
1807                 return TRUE;
1808         }
1809
1810         BT_DBG("[%s]  is NOT bonding device", address);
1811         return FALSE;
1812 }
1813
1814 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
1815 {
1816         ret_if(trigger_bond_info == NULL);
1817         trigger_bond_info->is_autopair = is_autopair;
1818 }
1819
1820 int _bt_search_device(bluetooth_device_address_t *device_address)
1821 {
1822         int result = OAL_STATUS_SUCCESS;
1823         BT_DBG("+");
1824
1825         BT_CHECK_PARAMETER(device_address, return);
1826
1827         if (trigger_bond_info) {
1828                 BT_ERR("Bonding in progress");
1829                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1830         }
1831
1832         if (service_search_info) {
1833                 BT_ERR("Service searching in progress");
1834                 return BLUETOOTH_ERROR_DEVICE_BUSY;
1835         }
1836
1837         /* allocate user data so that it can be retrieved in callback */
1838         service_search_info = g_malloc0(sizeof(bt_service_search_info_data_t));
1839         service_search_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1840         service_search_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1841
1842         _bt_convert_addr_type_to_string(service_search_info->addr,
1843                         device_address->addr);
1844
1845         result = device_query_services((bt_address_t *)device_address);
1846
1847         if (result != OAL_STATUS_SUCCESS) {
1848                 BT_ERR("Device Service Search Failed..: %d", result);
1849                 __bt_free_service_search_info(&service_search_info);
1850                 return BLUETOOTH_ERROR_INTERNAL;
1851         }
1852         return BLUETOOTH_ERROR_NONE;
1853 }
1854
1855 int _bt_cancel_search_device(void)
1856 {
1857         int ret = OAL_STATUS_SUCCESS;
1858         retv_if(service_search_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1859
1860         ret = device_stop_query_sevices((bt_address_t *)service_search_info->dev_addr);
1861
1862         if (ret != OAL_STATUS_SUCCESS) {
1863                 BT_ERR("SDP Cancel request failed [%d]", ret);
1864                 return BLUETOOTH_ERROR_INTERNAL;
1865         }
1866
1867         __bt_device_handle_pending_requests(BLUETOOTH_ERROR_CANCEL_BY_USER, BT_SEARCH_SERVICE,
1868                         service_search_info->addr, BT_ADDRESS_STRING_SIZE);
1869
1870         __bt_free_service_search_info(&service_search_info);
1871
1872         return BLUETOOTH_ERROR_NONE;
1873         BT_DBG("-");
1874 }
1875
1876 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1877                 gboolean authorize)
1878 {
1879         int ret = OAL_STATUS_SUCCESS;
1880         BT_DBG("+");
1881
1882         BT_CHECK_PARAMETER(device_address, return);
1883         BT_INFO("Device to be Trusted? [%d]", authorize);
1884
1885         ret = device_set_authorized((bt_address_t*)device_address, authorize);
1886         if (ret != OAL_STATUS_SUCCESS) {
1887                 BT_ERR("device_set_authorized: %d", ret);
1888                 return BLUETOOTH_ERROR_INTERNAL;
1889         }
1890
1891         return BLUETOOTH_ERROR_NONE;
1892 }
1893
1894 gboolean _bt_is_device_connected(bluetooth_device_address_t *device_address, int svc_type)
1895 {
1896         gboolean is_connected;
1897         oal_service_t svc_id;
1898
1899         retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1900
1901         /*
1902          * TODO: While adding support for new profiles, need to add more
1903          * <svc_type, svc_id> mapping here.
1904          */
1905         switch (svc_type) {
1906         case BT_PROFILE_CONN_HID:
1907                 svc_id = HID_SERVICE_ID;
1908                 break;
1909         case BT_PROFILE_CONN_A2DP:
1910                 svc_id = A2DP_SERVICE_ID; /* Remote is A2DP Sink */
1911                 break;
1912         case BT_PROFILE_CONN_A2DP_SINK:
1913                 svc_id = A2DP_SRC_SERVICE_ID; /* Remote is A2DP Source*/
1914                 break;
1915         case BT_PROFILE_CONN_HSP:
1916                 svc_id = HFP_HS_SERVICE_ID; /* Remote is HFP HF Unit */
1917                 break;
1918         default:
1919                 BT_DBG("Unknown svc_type: %d", svc_type);
1920                 return FALSE;
1921         }
1922
1923         is_connected = device_get_svc_conn_state((bt_address_t*)device_address, svc_id);
1924
1925         BT_DBG("svc_type: %d, is_connected: %s",
1926                         svc_type, is_connected ? "TRUE" : "FALSE");
1927
1928         return is_connected;
1929 }
1930
1931 int _bt_rfcomm_reply_conn_authorization(char *address, gboolean reply)
1932 {
1933         bt_address_t bd_addr;
1934         int res;
1935
1936         BT_DBG("+");
1937
1938         retv_if(NULL == address, BLUETOOTH_ERROR_INVALID_PARAM);
1939         _bt_convert_addr_string_to_type(bd_addr.addr, address);
1940         res = device_reply_auth_request(&bd_addr, 0, reply, FALSE);
1941         if (res != OAL_STATUS_SUCCESS) {
1942                 BT_ERR("authorize_response: %d", res);
1943                 return BLUETOOTH_ERROR_INTERNAL;
1944         }
1945
1946         BT_DBG("-");
1947         return BLUETOOTH_ERROR_NONE;
1948 }
1949
1950 int _bt_enable_rssi(bluetooth_device_address_t *addr, int link_type,
1951                 int low_threshold, int in_range_threshold, int high_threshold)
1952 {
1953         int result;
1954         bt_address_t bd_addr;
1955
1956         BT_DBG("+");
1957
1958         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
1959
1960         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
1961         result = device_enable_rssi_monitoring(&bd_addr, link_type,
1962                         low_threshold, in_range_threshold, high_threshold);
1963         if (result != OAL_STATUS_SUCCESS) {
1964                 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
1965                 return BLUETOOTH_ERROR_INTERNAL;
1966         }
1967
1968         BT_DBG("-");
1969         return BLUETOOTH_ERROR_NONE;
1970 }
1971
1972 int _bt_get_rssi_strength(bluetooth_device_address_t *addr, int link_type)
1973 {
1974         int result;
1975         bt_address_t bd_addr;
1976
1977         BT_DBG("+");
1978
1979         retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
1980
1981         memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
1982         result = device_get_connected_link_rssi_strength(&bd_addr, link_type);
1983         if (result != OAL_STATUS_SUCCESS) {
1984                 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
1985                 return BLUETOOTH_ERROR_INTERNAL;
1986         }
1987
1988         BT_DBG("-");
1989         return BLUETOOTH_ERROR_NONE;
1990 }
1991
1992 int _bt_set_passkey_notification(const char *sender, gboolean enable)
1993 {
1994         int result;
1995
1996         BT_INFO("Set passkey notification(sender:%s, %s)",
1997                         sender, enable ? "Enable" : "Disable");
1998
1999         result = device_enable_gap_auth_notifications(OAL_PASSKEY_DISPLAY,enable);
2000         if (OAL_STATUS_SUCCESS != result) {
2001                 BT_ERR("device_enable_gap_auth_notifications error: [%d]", result);
2002                 return BLUETOOTH_ERROR_INTERNAL;
2003         }
2004
2005         g_free(passkey_watcher);
2006         if (enable == TRUE)
2007                 passkey_watcher = g_strdup(sender);
2008         else
2009                 passkey_watcher = NULL;
2010
2011         return BLUETOOTH_ERROR_NONE;
2012 }
2013
2014 static int __bt_get_device_pin_code(const char *address, char *pin_code)
2015 {
2016         GSList *l = NULL;
2017
2018         BT_CHECK_PARAMETER(address, return);
2019         BT_CHECK_PARAMETER(pin_code, return);
2020
2021         for (l = pin_info_list; l != NULL; l = l->next) {
2022                 bt_pin_code_info_t *pin_info = l->data;
2023
2024                 if (!pin_info || !pin_info->address)
2025                         continue;
2026
2027                 if (g_strcmp0(pin_info->address, address) == 0) {
2028                         g_strlcpy(pin_code, pin_info->pin_code,
2029                                         BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2030                         return BLUETOOTH_ERROR_NONE;
2031                 }
2032         }
2033
2034         return BLUETOOTH_ERROR_NOT_FOUND;
2035 }
2036
2037 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2038                 bluetooth_device_pin_code_t *pin_code)
2039 {
2040         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2041         bt_pin_code_info_t *pin_info = NULL;
2042         GSList *l = NULL;
2043
2044         BT_CHECK_PARAMETER(device_address, return);
2045         BT_CHECK_PARAMETER(pin_code, return);
2046         retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2047                         BLUETOOTH_ERROR_NO_RESOURCES);
2048
2049         _bt_convert_addr_type_to_string(address, device_address->addr);
2050
2051         for (l = pin_info_list; l != NULL; l = l->next) {
2052                 pin_info = l->data;
2053
2054                 if (!pin_info || !pin_info->address)
2055                         continue;
2056
2057                 if (g_strcmp0(pin_info->address, address) == 0) {
2058                         g_free(pin_info->pin_code);
2059                         pin_info->pin_code = g_strdup(pin_code->pin_code);
2060                         return BLUETOOTH_ERROR_NONE;
2061                 }
2062         }
2063
2064         pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2065         pin_info->address = g_strdup(address);
2066         pin_info->pin_code = g_strdup(pin_code->pin_code);
2067         pin_info_list = g_slist_append(pin_info_list, pin_info);
2068
2069         return BLUETOOTH_ERROR_NONE;
2070 }
2071
2072 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2073 {
2074         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2075         bt_pin_code_info_t *pin_info = NULL;
2076         GSList *l = NULL;
2077
2078         BT_DBG("+");
2079
2080         BT_CHECK_PARAMETER(device_address, return);
2081
2082         _bt_convert_addr_type_to_string(address, device_address->addr);
2083
2084         for (l = pin_info_list; l != NULL; l = l->next) {
2085                 pin_info = l->data;
2086
2087                 if (!pin_info || !pin_info->address)
2088                         continue;
2089
2090                 if (g_strcmp0(pin_info->address, address) == 0) {
2091                         pin_info_list = g_slist_remove(pin_info_list, pin_info);
2092                         g_free(pin_info->pin_code);
2093                         g_free(pin_info->pin_code);
2094                         g_free(pin_info);
2095                         break;
2096                 }
2097         }
2098
2099         BT_DBG("-");
2100         return BLUETOOTH_ERROR_NONE;
2101 }