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