2 * Copyright (c) 2015 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Anupam Roy <anupam.r@samsung.com>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <vconf-internal-keys.h>
28 #include <bundle_internal.h>
29 #include <eventsystem.h>
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"
47 #include "bt-service-device-internal.h"
49 #ifdef TIZEN_GATT_CLIENT
50 #include "bt-service-gatt.h"
54 #include <oal-event.h>
55 #include <oal-manager.h>
56 #include <oal-adapter-mgr.h>
57 #include <oal-device-mgr.h>
59 #define MAX_BOND_RETRY_COUNT 3
60 #define BT_PASSKEY_MAX_LENGTH 4
62 #define BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT 6000 /* msec */
64 #define BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL 30 /* msec */
65 #define BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL 50 /* msec */
66 #define BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY 0 /* event */
68 #define BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL 10 /* msec */
69 #define BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL 30 /* msec */
70 #define BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY 0 /* event */
72 #define BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL 80 /* msec */
73 #define BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL 100 /* msec */
74 #define BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY 2 /* event */
76 /* Bonding Info structure */
81 unsigned short conn_type;
82 gboolean is_cancelled_by_user;
83 gboolean is_device_creating;
84 bluetooth_device_address_t *dev_addr;
85 bt_remote_dev_info_t *dev_info;
88 /* Searching Info structure */
92 gboolean is_cancelled_by_user;
93 bluetooth_device_address_t *dev_addr;
94 bt_remote_dev_info_t *dev_info;
95 } bt_service_search_info_data_t;
97 /* Pairing Info structure */
100 gboolean is_autopair;
105 char addr[BT_ADDRESS_STRING_SIZE];
106 bt_remote_dev_info_t *dev_info;
107 } bt_incoming_bond_data_t;
109 /* Bonding and Pairing Informations */
110 bt_bond_data_t *trigger_bond_info;
111 bt_bond_data_t *trigger_unbond_info;
112 bt_pairing_data_t *trigger_pairing_info;
113 bt_service_search_info_data_t *service_search_info;
115 bt_incoming_bond_data_t *incoming_bond_info;
118 BT_DEVICE_BOND_STATE_NONE,
119 BT_DEVICE_BOND_STATE_CANCEL_DISCOVERY,
120 BT_DEVICE_BOND_STATE_DISCOVERY_CANCELLED,
121 BT_DEVICE_BOND_STATE_REMOVE_BONDING,
122 BT_DEVICE_BOND_STATE_REMOVED_BONDING,
123 BT_DEVICE_BOND_STATE_STARTED,
124 BT_DEVICE_BOND_STATE_WAIT_PROP,
125 BT_DEVICE_BOND_STATE_WAIT_DID
130 BT_DEVICE_INCOMING_BOND_INFO,
131 BT_DEVICE_UNBOND_INFO
134 /* BT device bond state variable */
135 static bt_bond_state_e bt_device_bond_state;
136 static int bond_retry_count;
138 static char *passkey_watcher;
139 static GSList *pin_info_list = NULL;
141 /** DBFW+ Event Code */
143 BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_INFO = 0x10,
144 BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_CLOCK_INFO = 0x11,
145 BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_PAGE_SLOTS = 0x12,
146 BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_LMP_TRACE = 0x13,
147 BT_DBFW_PLUS_EVENT_CODE_A2DP_INFO = 0x20,
148 BT_DBFW_PLUS_EVENT_CODE_HFP_INFO = 0x31,
149 BT_DBFW_PLUS_EVENT_CODE_HFP_SCO_PACKET_TYPE_INFO = 0x32,
150 } bt_dbfw_plus_event_code_t;
152 static int dbfw_rssi;
154 #ifdef TIZEN_GATT_CLIENT
160 } bt_connected_le_dev_t;
169 } bt_le_conn_param_t;
171 static GSList *le_connected_dev_list = NULL;
173 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
174 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
175 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
176 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
177 #define BT_LE_CONN_SLAVE_LATENCY_MAX 499
178 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
179 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
181 static void _bt_handle_le_connected_dev_info(const char *address, gboolean connected);
184 /* Forward declaration */
185 static void __bt_device_event_handler(int event_type, gpointer event_data);
186 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble);
189 static int __bt_device_handle_bond_state(void);
190 static void __bt_free_bond_info(uint8_t type);
191 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info);
192 static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr);
193 static void __bt_device_handle_bond_removal_event(bt_address_t *bd_addr);
194 static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_fail_event);
195 static void __bt_handle_ongoing_bond(bt_remote_dev_info_t *remote_dev_info, gboolean incoming_bond);
196 static void __bt_device_acl_state_changed_callback(event_dev_conn_status_t *acl_event,
197 gboolean connected, unsigned char type);
198 static void __bt_free_pairing_info(bt_pairing_data_t **p_info);
200 static void __bt_device_ssp_consent_callback(remote_device_t* dev_info);
201 static void __bt_device_pin_request_callback(remote_device_t* pin_req_event);
202 static void __bt_device_ssp_passkey_display_callback(event_dev_passkey_t *dev_info);
203 static void __bt_device_ssp_passkey_confirmation_callback(event_dev_passkey_t *dev_info);
204 static void __bt_device_ssp_passkey_entry_callback(remote_device_t* dev_info);
205 static void __bt_device_authorization_request_callback(event_dev_authorize_req_t* auth_event);
207 static void __bt_device_services_callback(event_dev_services_t* uuid_list);
208 static void __bt_handle_ongoing_device_service_search(bt_remote_dev_info_t *remote_dev_info);
210 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info);
212 static int __bt_get_device_pin_code(const char *address, char *pin_code);
213 static gboolean __bt_ignore_auto_pairing_request(const char *address);
215 gboolean _bt_is_device_creating(void)
217 if (!trigger_bond_info)
219 return trigger_bond_info->is_device_creating;
222 void _bt_device_state_handle_callback_set_request(void)
224 _bt_service_register_event_handler_callback(
225 BT_DEVICE_MODULE, __bt_device_event_handler);
228 void __bt_device_handle_pending_requests(int result, int service_function,
229 void *user_data, unsigned int size)
233 invocation_info_t *req_info = NULL;
237 /* Get method invocation context */
238 for (l = _bt_get_invocation_list(); l != NULL; ) {
241 if (req_info == NULL || req_info->service_function != service_function)
244 switch (service_function) {
245 case BT_SEARCH_SERVICE: {
246 char *address = (char *)user_data;
247 if (strncmp((char*)req_info->user_data, address, BT_ADDRESS_STRING_SIZE)) {
248 BT_ERR("Unexpected: Info request pending for a different address!!");
251 BT_INFO("Found info request addr [%s]", (char*)req_info->user_data);
252 bt_sdp_info_t sdp_info;
254 memset(&sdp_info, 0x00, sizeof(bt_sdp_info_t));
255 _bt_convert_addr_string_to_type(sdp_info.device_addr.addr, address);
257 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
258 g_array_append_vals(out_param, &sdp_info, sizeof(bt_sdp_info_t));
259 _bt_service_method_return(req_info->context, out_param, result);
261 g_free(req_info->user_data);
262 _bt_free_info_from_invocation_list(req_info);
263 g_array_free(out_param, TRUE);
267 case BT_BOND_DEVICE: {
268 char *address = (char *)user_data;
269 if (strncmp((char*)req_info->user_data, address, BT_ADDRESS_STRING_SIZE)) {
270 BT_ERR("Unexpected: Info request pending for a different address!!");
273 BT_INFO("Found info request addr [%s]", (char*)req_info->user_data);
274 bluetooth_device_info_t dev_info;
275 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
276 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
278 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
279 g_array_append_vals(out_param, &dev_info,
280 sizeof(bluetooth_device_info_t));
281 _bt_service_method_return(req_info->context, out_param, result);
283 g_free(req_info->user_data);
284 _bt_free_info_from_invocation_list(req_info);
285 g_array_free(out_param, TRUE);
289 case BT_UNBOND_DEVICE: {
290 char *address = (char *)user_data;
291 if (strncmp((char*)req_info->user_data, address, BT_ADDRESS_STRING_SIZE)) {
292 BT_ERR("Unexpected: Info request pending for a different address!!");
295 BT_INFO("Found info request addr [%s]", (char*)req_info->user_data);
296 bluetooth_device_address_t dev_addr;
297 _bt_convert_addr_string_to_type(dev_addr.addr, address);
298 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
299 g_array_append_vals(out_param, &dev_addr,
300 sizeof(bluetooth_device_address_t));
301 _bt_service_method_return(req_info->context, out_param, result);
303 g_free(req_info->user_data);
304 _bt_free_info_from_invocation_list(req_info);
305 g_array_free(out_param, TRUE);
310 BT_ERR("Unhandled case");
318 * Remote device properties are received on all following conditions
319 * a. When Bonding in on-going
320 * b. When device properties are updated\changed for a connected device
321 * (due to SDP or any other reason)
323 static void __bt_device_remote_properties_callback(event_dev_properties_t *oal_dev_props)
325 bt_remote_dev_info_t *rem_info = NULL;
328 rem_info = g_malloc0(sizeof(bt_remote_dev_info_t));
329 _bt_copy_remote_dev(rem_info, &(oal_dev_props->device_info));
331 if (oal_dev_props->adv_len > 0) {
334 rem_info->manufacturer_data_len = oal_dev_props->adv_len;
335 rem_info->manufacturer_data =
336 g_memdup(oal_dev_props->adv_data,
337 oal_dev_props->adv_len);
338 BT_DBG("----Advertising Data Length: %d",
339 rem_info->manufacturer_data_len);
341 for (k = 0; k < rem_info->manufacturer_data_len; k++) {
342 BT_INFO("Check data[%d] = [[0x%x]",
343 k, oal_dev_props->adv_data[k]);
346 rem_info->manufacturer_data = NULL;
347 rem_info->manufacturer_data_len = 0;
350 /* a. Check if bonding is on-going, if yes, we MUST update the bonding device properties */
351 if (trigger_bond_info && !strcmp(trigger_bond_info->addr, rem_info->address)) {
352 BT_INFO("Bonding is ongoing, try update properties");
353 if (!trigger_bond_info->dev_info ||
354 (!trigger_bond_info->dev_info->name &&
355 !trigger_bond_info->dev_info->alias) ||
356 !trigger_bond_info->dev_info->address ||
357 trigger_bond_info->dev_info->uuid_count == 0) {
358 BT_INFO("Complete data is not present, Assigning rem_info");
359 if (!trigger_bond_info->dev_info)
360 trigger_bond_info->dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
361 _bt_copy_remote_dev_info(trigger_bond_info->dev_info, rem_info);
364 BT_DBG("Bonding dev addr has matched with remote dev properties address [%s]", rem_info->address);
365 __bt_handle_ongoing_bond(trigger_bond_info->dev_info, FALSE);
366 } else if (incoming_bond_info && !g_strcmp0(incoming_bond_info->addr, rem_info->address)) {
367 BT_INFO("Incoming Bond is ongoing, try update properties");
368 if (!incoming_bond_info->dev_info ||
369 (!incoming_bond_info->dev_info->name &&
370 !incoming_bond_info->dev_info->alias) ||
371 !incoming_bond_info->dev_info->address ||
372 incoming_bond_info->dev_info->uuid_count == 0) {
373 BT_INFO("Complete data is not present, Assigning rem_info");
374 if (!incoming_bond_info->dev_info)
375 incoming_bond_info->dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
376 _bt_copy_remote_dev_info(incoming_bond_info->dev_info, rem_info);
379 BT_DBG("Incoming Bond addr matches with remote dev properties address [%s]", rem_info->address);
380 __bt_handle_ongoing_bond(incoming_bond_info->dev_info, TRUE);
383 /* Add device to bonded list */
384 _bt_service_add_device_to_bonded_list(rem_info);
386 /* Handle SDP Device properties update */
387 if (service_search_info && service_search_info->dev_info) {
388 if (!strcmp(service_search_info->addr, rem_info->address)) {
389 BT_DBG("Properties received and SDP request pending, fill device properties and send event");
390 service_search_info->dev_info->class = rem_info->class;
391 service_search_info->dev_info->paired = rem_info->paired;
392 service_search_info->dev_info->connected = rem_info->connected;
393 service_search_info->dev_info->rssi = rem_info->rssi;
394 service_search_info->dev_info->addr_type = rem_info->addr_type;
395 service_search_info->dev_info->trust = rem_info->trust;
398 service_search_info->dev_info->manufacturer_data = NULL;
399 service_search_info->dev_info->manufacturer_data_len = 0;
401 __bt_handle_ongoing_device_service_search(service_search_info->dev_info);
405 _bt_free_remote_dev(rem_info);
409 static int __get_oal_trusted_profile(bluetooth_trusted_profile_t profile)
412 case TRUSTED_PROFILE_PBAP:
413 return OAL_TRUSTED_PROFILE_PBAP;
414 case TRUSTED_PROFILE_MAP:
415 return OAL_TRUSTED_PROFILE_MAP;
416 case TRUSTED_PROFILE_SAP:
417 return OAL_TRUSTED_PROFILE_SAP;
418 case TRUSTED_PROFILE_HFP_HF:
419 return OAL_TRUSTED_PROFILE_HFP_HF;
420 case TRUSTED_PROFILE_A2DP:
421 return OAL_TRUSTED_PROFILE_A2DP;
422 case TRUSTED_PROFILE_ALL:
423 return OAL_TRUSTED_PROFILE_ALL;
429 int _bt_set_trust_profile(bluetooth_device_address_t *addr,
430 bluetooth_trusted_profile_t profile, gboolean trust)
433 bt_address_t bd_addr;
434 oal_trusted_profile_e oal_profile;
438 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
440 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
441 oal_profile = __get_oal_trusted_profile(profile);
442 retv_if(0 == oal_profile, BLUETOOTH_ERROR_NOT_SUPPORT);
444 result = device_set_trust_profile(&bd_addr, oal_profile, trust);
445 if (result != OAL_STATUS_SUCCESS) {
446 BT_ERR("device_set_trust_profile error: [%d]", result);
447 return BLUETOOTH_ERROR_INTERNAL;
451 return BLUETOOTH_ERROR_NONE;
454 int _bt_get_trust_profile(bluetooth_device_address_t *addr,
455 bluetooth_trusted_profile_t profile, guint *trust)
458 bt_address_t bd_addr;
459 oal_trusted_profile_e oal_profile;
463 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
465 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
466 oal_profile = __get_oal_trusted_profile(profile);
467 retv_if(0 == oal_profile, BLUETOOTH_ERROR_NOT_SUPPORT);
469 result = device_get_trust_profile(&bd_addr, oal_profile, trust);
470 if (result != OAL_STATUS_SUCCESS) {
471 BT_ERR("device_set_trust_profile error: [%d]", result);
472 return BLUETOOTH_ERROR_INTERNAL;
476 return BLUETOOTH_ERROR_NONE;
479 static void __bt_handle_ongoing_device_service_search(bt_remote_dev_info_t *remote_dev_info)
481 GVariant *param = NULL;
482 GVariant *uuids = NULL;
483 GVariantBuilder *builder = NULL;
484 GVariant *manufacturer_data;
488 BT_INFO("Send Service Search request event");
490 if (remote_dev_info->alias)
491 name = remote_dev_info->alias;
493 name = remote_dev_info->name;
495 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
496 for (i = 0; i < remote_dev_info->uuid_count; i++) {
497 g_variant_builder_add(builder, "s",
498 remote_dev_info->uuids[i]);
500 uuids = g_variant_new("as", builder);
501 g_variant_builder_unref(builder);
502 manufacturer_data = g_variant_new_from_data((const GVariantType *)"ay",
503 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
506 param = g_variant_new("(isunsbub@asn@ay)",
507 BLUETOOTH_ERROR_NONE,
508 remote_dev_info->address,
509 remote_dev_info->class,
510 remote_dev_info->rssi,
512 remote_dev_info->paired,
513 remote_dev_info->connected,
514 remote_dev_info->trust,
516 remote_dev_info->manufacturer_data_len,
518 /* Send the event to application */
519 _bt_send_event(BT_ADAPTER_EVENT,
520 BLUETOOTH_EVENT_SERVICE_SEARCHED,
523 __bt_free_service_search_info(&service_search_info);
527 static void __bt_device_services_callback(event_dev_services_t* uuid_list)
529 bt_remote_dev_info_t *rem_info = NULL;
533 if (trigger_bond_info && _bt_compare_adddress(trigger_bond_info->dev_addr,
534 (bluetooth_device_address_t *)&uuid_list->address) == TRUE) {
535 bluetooth_device_address_t *dev_addr = trigger_bond_info->dev_addr;
537 BT_DBG("Bonding dev addr has matched");
538 /* Bonding ongoing, Query device properties again */
539 if (BLUETOOTH_ERROR_NONE ==
540 _bt_device_get_bonded_device_info(dev_addr))
541 BT_DBG("_bt_device_get_bonded_device_info success");
543 BT_ERR("_bt_device_get_bonded_device_info failed");
546 if (service_search_info == NULL) {
548 BT_DBG("searching_info == NULL");
552 if (_bt_compare_adddress(service_search_info->dev_addr,
553 (bluetooth_device_address_t *)&uuid_list->address) == FALSE) {
554 BT_DBG("This device is not queried");
558 rem_info = g_malloc0(sizeof(bt_remote_dev_info_t));
559 memset(rem_info, 0x00, sizeof(bt_remote_dev_info_t));
561 rem_info->address = g_new0(char, BT_ADDRESS_STRING_SIZE);
562 _bt_convert_addr_type_to_string(rem_info->address, uuid_list->address.addr);
564 rem_info->uuid_count = uuid_list->num;
566 BT_INFO("Address [%s]", rem_info->address);
567 BT_INFO("Number of UUID's [%d]", rem_info->uuid_count);
568 if (rem_info->uuid_count > 0)
569 rem_info->uuids = g_new0(char *, rem_info->uuid_count);
571 /* Fill Remote Device Service List list */
572 for (i = 0; i < rem_info->uuid_count; i++) {
573 rem_info->uuids[i] = g_malloc0(BLUETOOTH_UUID_STRING_MAX);
574 _bt_uuid_to_string((service_uuid_t *)&uuid_list->service_list[i].uuid, rem_info->uuids[i]);
575 BT_DBG("UUID value=%s", rem_info->uuids[i]);
578 /* Update local cache */
579 _bt_update_remote_dev_property(rem_info->address, DEV_PROP_SERVICES, (void *)rem_info);
581 BT_DBG("DBUS return");
582 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_SEARCH_SERVICE,
583 service_search_info->addr, BT_ADDRESS_STRING_SIZE);
585 /* Save UUID List of remote devices */
586 if (service_search_info->dev_info)
587 _bt_free_remote_dev(service_search_info->dev_info);
588 service_search_info->dev_info = rem_info;
590 /* Query Other device properties */
591 if (_bt_device_get_bonded_device_info(service_search_info->dev_addr) == BLUETOOTH_ERROR_NONE) {
592 BT_DBG("Bonded device info query posted to stack successfully");
594 BT_DBG("Possibly internal stack error in bonded device info query, perform cleanup");
595 __bt_free_service_search_info(&service_search_info);
600 static void __bt_device_authorization_request_callback(event_dev_authorize_req_t* auth_event)
602 oal_service_t service_d = auth_event->service_id;
603 gchar address[BT_ADDRESS_STR_LEN];
606 _bt_convert_addr_type_to_string(address, auth_event->address.addr);
608 BT_INFO("service_d: %d", service_d);
612 BT_INFO("Incoming HID Profile conn Req from device addr [%s]", address);
614 case A2DP_SERVICE_ID:
615 BT_INFO("Incoming A2DP(Remote Sink) profile conn Req from device addr [%s]", address);
616 _bt_a2dp_src_handle_incoming_authorization(address, service_d);
618 case A2DP_SRC_SERVICE_ID:
619 BT_INFO("Incoming A2DP(Remote Source) Profile conn Req from device addr [%s]", address);
620 _bt_a2dp_sink_handle_incoming_authorization(address, service_d);
622 case AVRCP_SERVICE_ID:
623 BT_INFO("Incoming AVRCP (Remote) Profile conn Req from device addr [%s]", address);
625 case AVRCP_CT_SERVICE_ID:
626 BT_INFO("Incoming AVRCP (Controller) Profile conn Req from device addr [%s]", address);
628 #ifdef TIZEN_FEATURE_BT_OBEX
629 case OPP_SERVICE_ID: {
630 GVariant *param = NULL;
633 if (_bt_obex_server_is_custom() == false) {
634 /* Allow the connection for native OPP server */
638 name = g_strdup(address);
640 BT_INFO("Incoming OPP conn Req from device addr [%s]", address);
641 _bt_obex_server_set_pending_conn_auth_device_addr(address);
642 param = g_variant_new("(iss)", BLUETOOTH_ERROR_NONE, address, name);
643 _bt_send_event(BT_OPP_SERVER_EVENT,
644 BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE, param);
650 BT_INFO("Incoming HSP_SERVICE_ID conn Req from device addr [%s]", address);
653 BT_INFO("Incoming HFP_SERVICE_ID conn Req from device addr [%s]", address);
656 BT_INFO("Incoming SAP_SERVICE_ID conn Req from device addr [%s]", address);
658 case HSP_HS_SERVICE_ID:
659 BT_INFO("Incoming HSP_HS_SERVICE_ID conn Req from device addr [%s]", address);
661 case HFP_HS_SERVICE_ID:
662 BT_INFO("Incoming HFP_HS_SERVICE_ID conn Req from device addr [%s]", address);
665 case IOTIVITY_SERVICE_ID:
666 BT_INFO("Incoming IOTIVITY_SERVICE_ID conn Req from device addr [%s]", address);
670 /* For now, reject authorization for any service apart from above switch cases */
671 BT_INFO("Incoming Profile conn req with service ID [%d] from device addr [%s]", service_d, address);
672 res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, FALSE, FALSE);
673 if (res != OAL_STATUS_SUCCESS)
674 BT_ERR("authorize_response: %d", res);
678 /* Auto accept authorization request for HID, A2DP and AVRCP profiles */
679 BT_INFO("Auto Accept authorization");
680 res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, TRUE, FALSE);
681 if (res != OAL_STATUS_SUCCESS)
682 BT_ERR("authorize_response: %d", res);
686 static void __bt_handle_ongoing_bond(bt_remote_dev_info_t *remote_dev_info, gboolean incoming_bond)
688 GVariant *param = NULL;
690 if ((remote_dev_info->name || remote_dev_info->alias)
691 && remote_dev_info->address
692 && remote_dev_info->uuids) {
693 GVariant *uuids = NULL;
694 GVariantBuilder *builder = NULL;
695 GVariant *manufacturer_data;
699 if (remote_dev_info->alias)
700 name = remote_dev_info->alias;
702 name = remote_dev_info->name;
704 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
705 for (i = 0; i < remote_dev_info->uuid_count; i++) {
706 g_variant_builder_add(builder, "s",
707 remote_dev_info->uuids[i]);
709 uuids = g_variant_new("as", builder);
710 g_variant_builder_unref(builder);
711 manufacturer_data = g_variant_new_from_data((const GVariantType *)"ay",
712 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
715 param = g_variant_new("(isunsbub@asn@ay)",
716 BLUETOOTH_ERROR_NONE,
717 remote_dev_info->address,
718 remote_dev_info->class,
719 remote_dev_info->rssi,
721 remote_dev_info->paired,
722 remote_dev_info->connected,
723 remote_dev_info->trust,
725 remote_dev_info->manufacturer_data_len,
727 /* Send the event to application */
728 _bt_send_event(BT_ADAPTER_EVENT,
729 BLUETOOTH_EVENT_BONDING_FINISHED,
731 BT_PERMANENT_LOG("Paired %s", remote_dev_info->address + 12);
732 BT_INFO_C("### Paired %s", remote_dev_info->address);
734 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
736 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
737 __bt_free_pairing_info(&trigger_pairing_info);
740 BT_INFO("Lets wait for more remote device properties");
744 static void __handle_incoming_bond_created_event(bt_address_t *bd_addr)
746 char address[BT_ADDRESS_STRING_SIZE];
747 bluetooth_device_address_t dev_addr;
750 * BlueZ sends paired signal for each paired device, during activation,
751 * We should ignore this, otherwise application thinks that a new device
754 if (_bt_adapter_get_status() != BT_ACTIVATED) {
755 BT_DBG("BT is not activated, so ignore this");
759 _bt_convert_addr_type_to_string(address, bd_addr->addr);
760 if (!incoming_bond_info) {
761 incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
763 if (g_strcmp0(incoming_bond_info->addr, address)) {
764 BT_DBG("Previous Bond address: [%s] differs from new address: [%s]",
765 address, incoming_bond_info->addr);
766 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
767 incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
771 BT_INFO("Incoming bond successfully completed");
772 g_strlcpy(incoming_bond_info->addr, address, BT_ADDRESS_STRING_SIZE);
773 incoming_bond_info->dev_info = NULL;
775 _bt_convert_addr_string_to_type(dev_addr.addr, incoming_bond_info->addr);
776 if (BLUETOOTH_ERROR_NONE == _bt_device_get_bonded_device_info(&dev_addr)) {
777 BT_DBG("Bonded device info query posted to stack successfully");
779 BT_ERR("Bonded device info query failed");
780 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
785 static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr)
787 gchar address[BT_ADDRESS_STR_LEN];
788 bluetooth_device_address_t dev_addr;
790 if (trigger_bond_info == NULL) {
792 BT_DBG("trigger_bond_info == NULL, Handle incomming bond event");
793 __handle_incoming_bond_created_event(bd_addr);
797 _bt_convert_addr_type_to_string(address, bd_addr->addr);
798 if (g_strcmp0(trigger_bond_info->addr, address)) {
799 BT_DBG("Bonding address= [%s] is different from requested address =[%s]",
800 address, trigger_bond_info->addr);
801 __handle_incoming_bond_created_event(bd_addr);
805 BT_INFO("Bonding successfully completed");
806 /* Bonding state will be cleaned up & BONDING FINISHED EVENT
807 will be sent only when Properties are fetched from stack
808 Till that time lets not free trigger_bond_info.
809 However it is possible that while fetching device properties, internal
810 stack error can occur which can lead to no valid properties or
811 no properties at all. So in such cases, we must not wait for properties,
812 otherwise, it can lead to infinite wait */
813 _bt_convert_addr_string_to_type(dev_addr.addr,
814 trigger_bond_info->addr);
816 if (_bt_device_get_bonded_device_info(&dev_addr) == BLUETOOTH_ERROR_NONE) {
817 BT_DBG("BOnded device info query posted to stack successfully");
818 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_BOND_DEVICE,
819 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
821 BT_DBG("Possibly internal stack error in bonded device info query, perform cleanup");
822 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
823 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
824 /* Destroy if at all device got bonded at stack level */
825 device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
827 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
828 __bt_free_pairing_info(&trigger_pairing_info);
832 /**********************************************************************************************
833 * Bond removal event can be triggered for following reasons -
834 * a. If Bonding procedure if failed (for Auth failed, Page timeout, cancelled by user etc)
835 * b. If Application requests for explicitly removing the bond
836 * c. When application attempt to create bond,bond is removed first which triggers this event
837 * c. is in-line with Bluedroid bond create\emoval architecture
838 *********************************************************************************************/
839 static void __bt_device_handle_bond_removal_event(bt_address_t *bd_addr)
841 char address[BT_ADDRESS_STRING_SIZE];
845 _bt_convert_addr_type_to_string(address, bd_addr->addr);
846 _bt_service_remove_device_from_bonded_list(address);
848 if (trigger_unbond_info) {
849 BT_INFO("Bond removal request successfully handled, return DBUS and send event");
850 GVariant *param = NULL;
851 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_UNBOND_DEVICE,
852 trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
853 param = g_variant_new("(is)", BLUETOOTH_ERROR_NONE, trigger_unbond_info->addr);
854 _bt_send_event(BT_ADAPTER_EVENT,
855 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
857 BT_PERMANENT_LOG("Unpaired %s", trigger_unbond_info->addr + 12);
858 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
859 __bt_free_pairing_info(&trigger_pairing_info);
860 } else if (trigger_bond_info) {
861 BT_ERR("Bonding was removed");
862 __bt_device_handle_bond_state();
867 static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_fail_event)
870 oal_status_t status = bond_fail_event->status;
871 BT_INFO("Bonding failed, reason: %d", status);
874 case OAL_STATUS_RMT_DEVICE_DOWN:
876 if (trigger_bond_info) {
877 BT_INFO("OAL_STATUS_RMT_DEVICE_DOWN:Lets retry bonding!! retry count [%d]",
879 int ret = OAL_STATUS_SUCCESS;
880 if (bond_retry_count < MAX_BOND_RETRY_COUNT) {
881 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
882 if (ret != OAL_STATUS_SUCCESS) {
883 BT_ERR("Create Bond procedure could not suceed");
884 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
885 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
886 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
887 __bt_free_pairing_info(&trigger_pairing_info);
888 bond_retry_count = 0;
892 BT_ERR("Create Bond failed MAX_BOND_RETRY_COUNT TIMES!!");
893 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
894 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
895 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
896 __bt_free_pairing_info(&trigger_pairing_info);
897 bond_retry_count = 0;
902 case OAL_STATUS_AUTH_FAILED:
904 /*TODO Auto pairing status set & ignore auto pairing logics can be done at this point.
905 To be considered later*/
906 int result = BLUETOOTH_ERROR_INTERNAL;
907 BT_INFO("BT_OPERATION_STATUS_AUTH_FAILED");
909 BT_INFO("add device in pairing black list");
910 _bt_set_autopair_status_in_bonding_info(FALSE);
912 if (trigger_bond_info) {
913 __bt_ignore_auto_pairing_request(trigger_bond_info->addr);
914 BT_ERR("Create Bond procedure could not suceed, check if cancelled by User");
915 if (trigger_bond_info->is_cancelled_by_user) {
916 BT_ERR("Bonding is cancelled by user");
917 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
919 __bt_device_handle_pending_requests(result, BT_BOND_DEVICE,
920 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
921 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
924 __bt_free_pairing_info(&trigger_pairing_info);
927 case OAL_STATUS_ALREADY_CONNECT:
929 int result = BLUETOOTH_ERROR_ALREADY_CONNECT;
930 BT_INFO("OAL_STATUS_ALREADY_CONNECT");
932 if (trigger_bond_info) {
933 __bt_device_handle_pending_requests(result, BT_BOND_DEVICE,
934 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
935 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
939 case OAL_STATUS_INTERNAL_ERROR:
941 BT_INFO("OAL_STATUS_INTERNAL_ERROR");
942 if (trigger_unbond_info) {
943 BT_INFO("Bond removal request failed, return DBUS and send event");
944 GVariant *param = NULL;
945 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_UNBOND_DEVICE,
946 trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
947 param = g_variant_new("(is)", BLUETOOTH_ERROR_INTERNAL, trigger_unbond_info->addr);
948 _bt_send_event(BT_ADAPTER_EVENT,
949 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
951 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
952 } else if (trigger_bond_info) {
953 if (__bt_device_handle_bond_state() != BLUETOOTH_ERROR_NONE) {
954 if (trigger_bond_info) {
955 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
956 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
957 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
962 __bt_free_pairing_info(&trigger_pairing_info);
967 BT_ERR("Unknown status of Bond failed event status [%d]", status);
975 static gboolean __bt_device_init_dbfw_rssi_cb(gpointer user_data)
981 static void __bt_device_parse_dbfw_set_rssi(unsigned char *data, uint32_t length)
984 dbfw_rssi = 0xFFFFFF00 | data[4];
985 BT_INFO("dbfw_rssi = %d", dbfw_rssi);
986 g_timeout_add(1000, __bt_device_init_dbfw_rssi_cb, NULL);
989 static void __bt_device_dbfw_plus_info_callback(event_dev_dbfw_plus_info_t *dbfw_info)
992 char *data_str = NULL;
995 data_str = g_malloc0(dbfw_info->length * 2 + 1);
996 for (i = 0; i < dbfw_info->length; i++)
997 snprintf(&data_str[i * 2], 3, "%02X", dbfw_info->data[i]);
998 data_str[i * 2] = '\0';
1000 switch(dbfw_info->event_code) {
1001 case BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_INFO:
1002 BT_DBG("## Event Code: Linkloss Debug Info");
1003 snprintf(evt_str, 18, "[DBFW]Linkloss(D)");
1004 __bt_device_parse_dbfw_set_rssi(dbfw_info->data, dbfw_info->length);
1006 case BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_CLOCK_INFO:
1007 BT_DBG("## Event Code: Linkloss Clock Info");
1008 snprintf(evt_str, 18, "[DBFW]Linkloss(C)");
1010 case BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_PAGE_SLOTS:
1011 BT_DBG("## Event Code: Linkloss Page slots");
1012 snprintf(evt_str, 18, "[DBFW]Linkloss(P)");
1014 case BT_DBFW_PLUS_EVENT_CODE_LINK_LOSS_LMP_TRACE:
1015 BT_DBG("## Event Code: Linkloss LMP trace");
1016 snprintf(evt_str, 18, "[DBFW]Linkloss(L)");
1018 case BT_DBFW_PLUS_EVENT_CODE_A2DP_INFO:
1019 BT_DBG("## Event Code: A2DP Info");
1020 snprintf(evt_str, 18, "[DBFW]A2DP");
1022 case BT_DBFW_PLUS_EVENT_CODE_HFP_INFO:
1023 BT_DBG("## Event Code: HFP Info");
1024 snprintf(evt_str, 18, "[DBFW]HFP");
1026 case BT_DBFW_PLUS_EVENT_CODE_HFP_SCO_PACKET_TYPE_INFO:
1027 BT_DBG("## Event Code: HFP SCO Packet Type");
1028 snprintf(evt_str, 18, "[DBFW]SCO");
1031 BT_DBG("## Unknown event code (0x%02x)", dbfw_info->event_code);
1032 snprintf(evt_str, 18, "[DBFW]0x%02X", dbfw_info->event_code);
1036 BT_PERMANENT_LOG("%s 0x%s", evt_str, data_str);
1040 static void __bt_device_event_handler(int event_type, gpointer event_data)
1042 int eventcheck = OAL_EVENT_DEVICE_PROPERTIES;
1043 BT_DBG("event [%d] Event check = [%d]", event_type, eventcheck);
1045 switch (event_type) {
1046 case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY: {
1047 BT_INFO("BREDR Device Found");
1048 __bt_device_remote_device_found_callback(event_data, FALSE);
1051 case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE: {
1052 BT_INFO("Dual Device Found");
1053 __bt_device_remote_device_found_callback(event_data, FALSE);
1056 case OAL_EVENT_DEVICE_PROPERTIES: {
1057 BT_INFO("Remote Device properties Received");
1058 __bt_device_remote_properties_callback((event_dev_properties_t *)event_data);
1061 case OAL_EVENT_DEVICE_BONDING_SUCCESS: {
1062 BT_INFO("Bonding Success event Received");
1063 __bt_device_handle_bond_completion_event((bt_address_t *)event_data);
1066 case OAL_EVENT_DEVICE_BONDING_REMOVED: {
1067 BT_INFO("Bonding Removed event Received");
1068 __bt_device_handle_bond_removal_event((bt_address_t *)event_data);
1071 case OAL_EVENT_DEVICE_BONDING_FAILED: {
1072 BT_INFO("Bonding Failed event Received");
1073 __bt_device_handle_bond_failed_event((event_dev_bond_failed_t*) event_data);
1076 case OAL_EVENT_DEVICE_ACL_CONNECTED: {
1077 BT_DBG("ACL Connected event Received");
1078 __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, TRUE, 0);
1081 case OAL_EVENT_DEVICE_ACL_DISCONNECTED: {
1082 BT_DBG("ACL Disconnected event Received");
1083 __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 0);
1086 case OAL_EVENT_DEVICE_LE_CONNECTED: {
1087 BT_DBG("LE Connected event Received");
1088 __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, TRUE, 1);
1091 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
1092 BT_DBG("LE Disconnected event Received");
1093 __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 1);
1096 case OAL_EVENT_DEVICE_PIN_REQUEST: {
1097 BT_INFO("PIN Request Received");
1098 __bt_device_pin_request_callback((remote_device_t*)event_data);
1101 case OAL_EVENT_DEVICE_PASSKEY_ENTRY_REQUEST: {
1102 BT_INFO("Passkey Entry request Received");
1103 __bt_device_ssp_passkey_entry_callback((remote_device_t*)event_data);
1106 case OAL_EVENT_DEVICE_PASSKEY_CONFIRMATION_REQUEST:{
1107 BT_INFO("Passkey Confirmation Request Received");
1108 __bt_device_ssp_passkey_confirmation_callback((event_dev_passkey_t *)event_data);
1111 case OAL_EVENT_DEVICE_PASSKEY_DISPLAY: {
1112 BT_INFO("Passkey Display Request Received");
1113 __bt_device_ssp_passkey_display_callback((event_dev_passkey_t *)event_data);
1116 case OAL_EVENT_DEVICE_SSP_CONSENT_REQUEST: {
1117 BT_INFO("SSP Consent Request Received");
1118 __bt_device_ssp_consent_callback((remote_device_t*)event_data);
1121 case OAL_EVENT_DEVICE_SERVICES: {
1122 BT_INFO("Remote Device Services Received");
1123 __bt_device_services_callback((event_dev_services_t*)event_data);
1126 case OAL_EVENT_DEVICE_AUTHORIZE_REQUEST: {
1127 BT_INFO("Remote Device Authorization Request");
1128 __bt_device_authorization_request_callback((event_dev_authorize_req_t*)event_data);
1131 case OAL_EVENT_DEVICE_TRUSTED: {
1132 BT_INFO("Remote Device Trusted");
1133 __bt_device_trusted_callback(TRUE, (event_dev_trust_t*)event_data);
1136 case OAL_EVENT_DEVICE_UNTRUSTED: {
1137 BT_INFO("Remote Device UnTrusted");
1138 __bt_device_trusted_callback(FALSE, (event_dev_trust_t*)event_data);
1141 case OAL_EVENT_DEVICE_NAME: {
1142 remote_device_t *rem_dev = event_data;
1143 gchar address[BT_ADDRESS_STR_LEN];
1145 _bt_convert_addr_type_to_string(address, rem_dev->address.addr);
1146 BT_INFO("Remote Device name Received");
1147 BT_INFO("Name: %s, Address: %s", rem_dev->name, address);
1149 /* Update local cache */
1150 _bt_update_remote_dev_property(address, DEV_PROP_NAME, (void *)rem_dev->name);
1153 case OAL_EVENT_DEVICE_TRUSTED_PROFILES_CHANGED: {
1154 event_device_trusted_profiles_t *ev = event_data;
1155 char address[BT_ADDRESS_STRING_SIZE];
1159 _bt_convert_addr_type_to_string(address, ev->address.addr);
1160 _bt_send_event(BT_DEVICE_EVENT,
1161 BLUETOOTH_EVENT_SUPPORTED_PROFILE_TRUSTED,
1162 g_variant_new("(isi)", BLUETOOTH_ERROR_NONE,
1163 address, ev->trust_val));
1166 case OAL_EVENT_RSSI_MONITORING_ENABLED: {
1167 event_dev_rssi_info_t *ev = event_data;
1168 char address[BT_ADDRESS_STRING_SIZE];
1173 _bt_convert_addr_type_to_string(address, ev->address.addr);
1174 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1175 address, ev->link_type, TRUE);
1176 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1179 case OAL_EVENT_RSSI_MONITORING_DISABLED: {
1180 event_dev_rssi_info_t *ev = event_data;
1181 char address[BT_ADDRESS_STRING_SIZE];
1186 _bt_convert_addr_type_to_string(address, ev->address.addr);
1187 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1188 address, ev->link_type, FALSE);
1189 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1192 case OAL_EVENT_RSSI_ALERT_RECEIVED: {
1193 event_dev_rssi_info_t *ev = event_data;
1194 char address[BT_ADDRESS_STRING_SIZE];
1199 _bt_convert_addr_type_to_string(address, ev->address.addr);
1200 param = g_variant_new("(isiii)", BLUETOOTH_ERROR_NONE,
1201 address, ev->link_type, ev->alert_type, ev->rssi);
1202 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ALERT, param);
1205 case OAL_EVENT_RAW_RSSI_RECEIVED: {
1206 event_dev_rssi_info_t *ev = event_data;
1207 char address[BT_ADDRESS_STRING_SIZE];
1212 _bt_convert_addr_type_to_string(address, ev->address.addr);
1213 param = g_variant_new("(isii)", BLUETOOTH_ERROR_NONE,
1214 address, ev->link_type, ev->rssi);
1215 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RAW_RSSI, param);
1218 case OAL_EVENT_DEVICE_DBFW_PLUS_INFO: {
1219 __bt_device_dbfw_plus_info_callback((event_dev_dbfw_plus_info_t*)event_data);
1223 BT_INFO("Unhandled event..");
1227 /* Legacy Pairing event handler */
1228 static void __bt_device_pin_request_callback(remote_device_t* pin_req_event)
1231 char address[BT_ADDRESS_STRING_SIZE];
1232 char pin_code[BLUETOOTH_PIN_CODE_MAX_LENGTH + 1];
1233 bool incoming = false;
1236 _bt_convert_addr_type_to_string(address, pin_req_event->address.addr);
1238 BT_INFO("Address[%s]", address);
1239 BT_INFO("Name[%s]", pin_req_event->name);
1240 BT_INFO("COD[%d]", pin_req_event->cod);
1242 if (trigger_pairing_info) {
1243 /* BTAPI support only one pairing at a time */
1244 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1245 BT_ERR("New PIN request address [%s]", address);
1246 device_reject_pin_request(&pin_req_event->address);
1250 /* If user initiated bonding and auto response is possible, just reply with default 0000*/
1251 if (_bt_is_bonding_device_address(address) == TRUE &&
1252 _bt_agent_is_auto_response(pin_req_event->cod, address, pin_req_event->name)) {
1253 /* Note: Currently even if SYSPOPUP is supported, we use Fixed PIN "0000" for basic pairing
1254 as BT SYSPOPUP is currently not working for PIN KEY entry in Tizen platform. This needs
1255 to be checked and fixed apropriately */
1256 _bt_set_autopair_status_in_bonding_info(TRUE);
1257 device_accept_pin_request(&pin_req_event->address, "0000");
1258 } else if (_bt_agent_is_hid_keyboard(pin_req_event->cod)) {
1259 BT_DBG("Remote Device is HID keyboard Type..");
1260 char str_passkey[BT_PASSKEY_MAX_LENGTH + 1] = { 0 };
1262 if (_bt_agent_generate_passkey(str_passkey,
1263 BT_PASSKEY_MAX_LENGTH) != 0) {
1264 device_reject_pin_request(&pin_req_event->address);
1267 device_accept_pin_request(&pin_req_event->address, str_passkey);
1269 BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1271 if(trigger_bond_info == NULL)
1274 param = g_variant_new("(bsss)", incoming, address, pin_req_event->name, str_passkey);
1275 _bt_send_event(BT_ADAPTER_EVENT,
1276 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1277 BT_DBG("Key board pairing in process");
1278 } else if (BLUETOOTH_ERROR_NONE == __bt_get_device_pin_code(address, pin_code)) {
1279 BT_DBG("Use stored PIN code [%s]", pin_code);
1280 device_accept_pin_request(&pin_req_event->address, pin_code);
1282 if (_bt_is_bonding_device_address(address) == TRUE) {
1283 BT_DBG("Show Pin entry");
1284 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1285 trigger_pairing_info->addr = g_strdup(address);
1286 trigger_pairing_info->is_ssp = FALSE;
1288 BT_DBG("Send BLUETOOTH_EVENT_PIN_REQUEST");
1290 if(trigger_bond_info == NULL)
1293 param = g_variant_new("(bss)", incoming, address, pin_req_event->name);
1294 _bt_send_event(BT_ADAPTER_EVENT,
1295 BLUETOOTH_EVENT_PIN_REQUEST, param);
1300 _bt_agent_release_memory();
1304 /* SSP Pairing event handler */
1305 static void __bt_device_ssp_passkey_entry_callback(remote_device_t* dev_info)
1308 gchar address[BT_ADDRESS_STR_LEN];
1311 bool incoming = false;
1315 _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1317 name = dev_info->name;
1319 BT_INFO("Address[%s]", address);
1320 BT_INFO("Name[%s]", name);
1321 BT_INFO("COD[%d]", dev_info->cod);
1323 if (trigger_pairing_info) {
1324 /* BTAPI support only one pairing at a time */
1325 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1326 BT_ERR("New PIN request address [%s]", address);
1327 device_reject_pin_request(&dev_info->address);
1332 /* Set pairing data */
1333 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1334 trigger_pairing_info->addr = g_strdup(address);
1335 trigger_pairing_info->is_ssp = TRUE;
1337 if(trigger_bond_info == NULL)
1340 param = g_variant_new("(bss)", incoming, p_addr, name);
1341 _bt_send_event(BT_ADAPTER_EVENT,
1342 BLUETOOTH_EVENT_PASSKEY_REQUEST, param);
1346 static void __bt_device_ssp_passkey_confirmation_callback(event_dev_passkey_t *dev_info)
1349 gchar address[BT_ADDRESS_STR_LEN];
1352 char str_passkey[7];
1353 bool incoming = false; /*Stores if bonding request is incoming(true) or outgoing(false) */
1356 _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1358 name = dev_info->device_info.name;
1360 BT_INFO("Address[%s]", address);
1361 BT_INFO("Name[%s]", name);
1362 BT_INFO("COD[%d]", dev_info->device_info.cod);
1364 if (trigger_pairing_info) {
1365 /* BTAPI support only one pairing at a time */
1366 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1367 BT_ERR("New PIN request address [%s]", address);
1368 device_reject_pin_request(&dev_info->device_info.address);
1373 /* Set pairing data */
1374 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1375 trigger_pairing_info->addr = g_strdup(address);
1376 trigger_pairing_info->is_ssp = TRUE;
1378 BT_DBG("Send BLUETOOTH_EVENT_PASSKEY_CONFIRMATION");
1379 snprintf(str_passkey, sizeof(str_passkey), "%.6d", dev_info->pass_key);
1381 /*Storing if bond is incoming or outgoing*/
1382 if(trigger_bond_info == NULL)
1385 param = g_variant_new("(bsss)", incoming, p_addr, name, str_passkey);
1386 _bt_send_event(BT_ADAPTER_EVENT,
1387 BLUETOOTH_EVENT_PASSKEY_CONFIRM_REQUEST, param);
1391 static void __bt_device_ssp_passkey_display_callback(event_dev_passkey_t *dev_info)
1394 gchar address[BT_ADDRESS_STR_LEN];
1397 char str_passkey[7];
1398 bool incoming = false;
1401 _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1403 name = dev_info->device_info.name;
1405 BT_INFO("Address[%s]", address);
1406 BT_INFO("Name[%s]", name);
1407 BT_INFO("COD[%d]", dev_info->device_info.cod);
1409 if (trigger_pairing_info) {
1410 /* BTAPI support only one pairing at a time */
1411 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1412 BT_ERR("New PIN request address [%s]", address);
1413 device_reject_pin_request(&dev_info->device_info.address);
1418 /* Set pairing data */
1419 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1420 trigger_pairing_info->addr = g_strdup(address);
1421 trigger_pairing_info->is_ssp = TRUE;
1423 BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1424 snprintf(str_passkey, sizeof(str_passkey), "%06d", dev_info->pass_key);
1426 if(trigger_bond_info == NULL)
1429 param = g_variant_new("(bsss)", incoming, p_addr, name, str_passkey);
1431 if (passkey_watcher) {
1432 BT_INFO("Send passkey to %s", passkey_watcher);
1433 _bt_send_event_to_dest(passkey_watcher, BT_ADAPTER_EVENT,
1434 BLUETOOTH_EVENT_PASSKEY_NOTIFICATION, param);
1436 _bt_send_event(BT_ADAPTER_EVENT,
1437 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1442 static void __bt_device_ssp_consent_callback(remote_device_t* dev_info)
1444 gchar address[BT_ADDRESS_STR_LEN];
1451 _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1452 name = dev_info->name;
1453 cod = dev_info->cod;
1455 BT_INFO("Address[%s]", address);
1456 BT_INFO("Name[%s]", name);
1457 BT_INFO("COD[%d]", cod);
1459 if (trigger_pairing_info) {
1460 /* BTAPI support only one pairing at a time */
1461 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1462 BT_ERR("New PIN request address [%s]", address);
1463 device_reject_pin_request(&dev_info->address);
1468 /* Set pairing data */
1469 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1470 trigger_pairing_info->addr = g_strdup(address);
1471 trigger_pairing_info->is_ssp = TRUE;
1473 local_major = ((cod >> 8) & 0x001f);
1474 local_minor = (cod & 0x00fc);
1475 BT_DBG("SSP_CONSENT: Major type=[0x%x] and Minor type=[0x%x]", local_major, local_minor);
1477 /*TODO: BLUETOOTH_EVENT_SSP_CONSENT_REQUEST to be handled in Tizen */
1481 static void __bt_device_acl_state_changed_callback(event_dev_conn_status_t *acl_event,
1482 gboolean connected, unsigned char type)
1484 gchar address[BT_ADDRESS_STR_LEN];
1485 int disc_reason = 0;
1486 GVariant *param = NULL;
1487 bt_device_conn_info_t conn_info;
1489 _bt_convert_addr_type_to_string(address, acl_event->address.addr);
1491 _bt_logging_connection(connected, type);
1493 disc_reason = acl_event->status;
1496 BT_PERMANENT_LOG("Connected %s %s", !type ? "EDR" : "LE", address + 12);
1497 param = g_variant_new("(isy)", BLUETOOTH_ERROR_NONE, address, type);
1498 _bt_send_event(BT_DEVICE_EVENT,
1499 BLUETOOTH_EVENT_DEVICE_CONNECTED,
1502 BT_PERMANENT_LOG("Disconnected %s(%d) %s", !type ? "EDR" : "LE", disc_reason, address + 12);
1503 param = g_variant_new("(isyi)", disc_reason, address, type, dbfw_rssi);
1504 _bt_send_event(BT_DEVICE_EVENT,
1505 BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1509 conn_info.connected = connected;
1510 conn_info.type = type;
1511 /* Update local cache */
1512 _bt_update_remote_dev_property(address, DEV_PROP_CONNECTED, (void *)&conn_info);
1514 #ifdef TIZEN_GATT_CLIENT
1515 /*handle LE connected device info*/
1517 _bt_handle_le_connected_dev_info(address, connected);
1522 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble)
1524 bt_remote_dev_info_t *dev_info = NULL;
1525 int result = BLUETOOTH_ERROR_NONE;
1526 GVariant *param = NULL;
1527 GVariant *uuids = NULL;
1528 GVariant *manufacturer_data = NULL;
1529 GVariantBuilder *builder = NULL;
1532 ret_if(_bt_is_discovering() == FALSE);
1533 ret_if(event_data == NULL);
1535 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
1538 event_ble_dev_found_t * oal_ble_dev = event_data;
1539 BT_INFO("Device type [%d]", oal_ble_dev->device_info.type);
1541 _bt_copy_remote_dev(dev_info, &oal_ble_dev->device_info);
1543 dev_info->manufacturer_data_len = oal_ble_dev->adv_len;
1544 if (dev_info->manufacturer_data_len)
1545 dev_info->manufacturer_data = g_memdup(oal_ble_dev->adv_data,
1546 dev_info->manufacturer_data_len);
1548 dev_info->manufacturer_data = NULL;
1549 BT_DBG("----Advertising Data Length: %d", dev_info->manufacturer_data_len);
1551 event_dev_found_t * oal_dev = event_data;
1552 _bt_copy_remote_dev(dev_info, &oal_dev->device_info);
1555 /* If Remote device name is NULL or still RNR is not done then display address as name. */
1556 if (dev_info->name == NULL)
1557 dev_info->name = g_strdup(dev_info->address);
1558 BT_DBG("Name %s", dev_info->name);
1560 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1561 for (i = 0; i < dev_info->uuid_count; i++)
1562 g_variant_builder_add(builder, "s", dev_info->uuids[i]);
1564 uuids = g_variant_new("as", builder);
1565 g_variant_builder_unref(builder);
1567 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1568 dev_info->manufacturer_data,
1569 dev_info->manufacturer_data_len,
1573 param = g_variant_new("(isunsbub@asn@ay)", result,
1579 dev_info->connected,
1582 dev_info->manufacturer_data_len,
1585 _bt_send_event(BT_ADAPTER_EVENT,
1586 BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
1589 _bt_free_remote_dev(dev_info);
1592 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info)
1594 gchar address[BT_ADDRESS_STR_LEN];
1595 int result = BLUETOOTH_ERROR_NONE;
1596 GVariant *param = NULL;
1599 _bt_convert_addr_type_to_string(address, info->address.addr);
1601 /* Update local cache */
1602 _bt_update_remote_dev_property(address, DEV_PROP_TRUST, (void *)&trusted);
1604 param = g_variant_new("(is)", result, address);
1605 event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1606 BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1607 /* Send event to application */
1608 _bt_send_event(BT_DEVICE_EVENT,
1614 static void __bt_free_pairing_info(bt_pairing_data_t **p_info)
1616 bt_pairing_data_t * info = *p_info;
1626 static void __bt_free_bond_info(uint8_t type)
1629 case BT_DEVICE_BOND_INFO:
1630 if (!trigger_bond_info)
1633 if (trigger_bond_info->addr)
1634 g_free(trigger_bond_info->addr);
1635 if (trigger_bond_info->dev_addr)
1636 g_free(trigger_bond_info->dev_addr);
1637 if (trigger_bond_info->dev_info)
1638 _bt_free_remote_dev(trigger_bond_info->dev_info);
1639 g_free(trigger_bond_info);
1640 trigger_bond_info = NULL;
1642 case BT_DEVICE_INCOMING_BOND_INFO:
1643 if (!incoming_bond_info)
1646 if (incoming_bond_info->dev_info)
1647 _bt_free_remote_dev(incoming_bond_info->dev_info);
1648 g_free(incoming_bond_info);
1649 incoming_bond_info = NULL;
1651 case BT_DEVICE_UNBOND_INFO:
1652 if (!trigger_unbond_info)
1655 if (trigger_unbond_info->addr)
1656 g_free(trigger_unbond_info->addr);
1657 if (trigger_unbond_info->dev_addr)
1658 g_free(trigger_unbond_info->dev_addr);
1659 if (trigger_unbond_info->dev_info)
1660 _bt_free_remote_dev(trigger_unbond_info->dev_info);
1661 g_free(trigger_unbond_info);
1662 trigger_unbond_info = NULL;
1667 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info)
1669 bt_service_search_info_data_t * info = *p_info;
1676 if (info->dev_addr) {
1677 g_free(info->dev_addr);
1678 info->dev_addr = NULL;
1681 if (info->dev_info) {
1682 _bt_free_remote_dev(info->dev_info);
1683 info->dev_info = NULL;
1691 static int __bt_device_handle_bond_state(void)
1693 BT_INFO("Current Bond state: %d", bt_device_bond_state);
1694 int ret = OAL_STATUS_INTERNAL_ERROR;
1696 switch (bt_device_bond_state) {
1697 case BT_DEVICE_BOND_STATE_CANCEL_DISCOVERY:
1698 /*TODO:Bonding during discovery: Unhandled!!*/
1699 BT_INFO("Bonding during discovery: Unhandled!!");
1701 case BT_DEVICE_BOND_STATE_DISCOVERY_CANCELLED:
1702 /*TODO:Bonding during discovery: Unhandled!!*/
1703 BT_INFO("Bonding during discovery: Unhandled!!");
1705 case BT_DEVICE_BOND_STATE_REMOVE_BONDING:
1706 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVED_BONDING;
1707 ret = device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
1708 if (ret != OAL_STATUS_SUCCESS)
1709 ret = __bt_device_handle_bond_state();
1711 case BT_DEVICE_BOND_STATE_REMOVED_BONDING:
1712 bt_device_bond_state = BT_DEVICE_BOND_STATE_NONE;
1713 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
1714 /* Bonding procedure was started but unfortunately could not complete.
1715 Basically removed bonding was success, but create bond request could not proceed
1716 So lets cleanup the context */
1717 if (ret != OAL_STATUS_SUCCESS) {
1718 BT_ERR("Create Bond procedure could not suceed");
1719 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1720 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1721 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1722 __bt_free_pairing_info(&trigger_pairing_info);
1725 case BT_DEVICE_BOND_STATE_NONE:
1726 BT_INFO("Create Bond failed!!");
1727 if (trigger_bond_info) {
1728 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1729 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1730 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1731 __bt_free_pairing_info(&trigger_pairing_info);
1738 if (ret != OAL_STATUS_SUCCESS)
1739 return BLUETOOTH_ERROR_INTERNAL;
1741 return BLUETOOTH_ERROR_NONE;
1744 int _bt_device_get_bonded_device_info(bluetooth_device_address_t *addr)
1747 bt_address_t bd_addr;
1751 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
1753 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
1754 result = device_query_attributes(&bd_addr);
1755 if (result != OAL_STATUS_SUCCESS) {
1756 BT_ERR("device_query_attributes error: [%d]", result);
1757 return BLUETOOTH_ERROR_INTERNAL;
1761 return BLUETOOTH_ERROR_NONE;
1764 int _bt_set_alias(bluetooth_device_address_t *device_address, const char *alias)
1767 char address[BT_ADDRESS_STRING_SIZE];
1770 BT_CHECK_PARAMETER(alias, return);
1772 ret = device_set_alias((bt_address_t *)device_address, (char *)alias);
1773 if (ret != OAL_STATUS_SUCCESS) {
1774 BT_ERR("device_set_alias: %d", ret);
1775 return BLUETOOTH_ERROR_INTERNAL;
1778 /* Update local cache */
1779 _bt_convert_addr_type_to_string(address, device_address->addr);
1780 _bt_update_remote_dev_property(address, DEV_PROP_ALIAS, (void *)alias);
1783 return BLUETOOTH_ERROR_NONE;
1786 int _bt_bond_device(bluetooth_device_address_t *device_address,
1787 unsigned short conn_type, GArray **out_param1)
1789 int result = BLUETOOTH_ERROR_NONE;
1790 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1791 bluetooth_device_info_t dev_info;
1792 const char *stack_name = NULL;
1795 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1797 /* If bonding or discovery already going on */
1798 if (trigger_bond_info || _bt_is_discovering()) {
1799 BT_ERR("Device is buzy, bonding can not proceed now..");
1800 result = BLUETOOTH_ERROR_DEVICE_BUSY;
1804 /*TODO: If unbonding with same device going on */
1805 _bt_convert_addr_type_to_string(address, device_address->addr);
1807 trigger_bond_info = g_malloc0(sizeof(bt_bond_data_t));
1808 trigger_bond_info->addr = g_strdup(address);
1809 trigger_bond_info->conn_type = conn_type;
1810 trigger_bond_info->is_device_creating = TRUE;
1811 trigger_bond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1812 trigger_bond_info->dev_info = NULL;
1814 /* Ready to initiate bonding */
1816 stack_name = oal_get_stack_name();
1817 if (stack_name && !g_strcmp0(stack_name, "bluez")) {
1818 BT_DBG("[bluez] Create bond by type %d", conn_type);
1819 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVED_BONDING;
1821 /* In Tizen, we will first remove bond and then attempt to create bond to keep
1822 consistency with bluedroid. Even if remove bond fails due to device not already
1823 bonded, then straight away create bond is triggered. This is because, remove bond
1824 is handled differently in bluedroid and bluez. In Bluez, if device is
1825 already removed, remove bond call fails.
1826 However in bluedroid, remove bond on already removed device returns success. So we will
1827 handle the cases transparently */
1828 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVE_BONDING;
1831 bond_retry_count = 0;
1832 result = __bt_device_handle_bond_state();
1834 if (result != BLUETOOTH_ERROR_NONE)
1841 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1842 memcpy(dev_info.device_address.addr, device_address->addr,
1843 BLUETOOTH_ADDRESS_LENGTH);
1845 g_array_append_vals(*out_param1, &dev_info,
1846 sizeof(bluetooth_device_info_t));
1847 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1853 int _bt_unbond_device(bluetooth_device_address_t *device_address,
1854 GArray **out_param1)
1856 int result = OAL_STATUS_SUCCESS;
1857 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1858 bluetooth_device_info_t dev_info;
1861 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1863 _bt_convert_addr_type_to_string(address, device_address->addr);
1865 trigger_unbond_info = g_malloc0(sizeof(bt_bond_data_t));
1866 trigger_unbond_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1867 trigger_unbond_info->addr = g_strdup(address);
1868 trigger_unbond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1870 /* Check if Bonding is already going on, we should not abruptly remove bonding*/
1871 if (trigger_bond_info && strncmp(trigger_bond_info->addr, trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE) == 0) {
1872 BT_ERR("Bonding with same device already ongoing");
1873 result = BLUETOOTH_ERROR_PERMISSION_DEINED;
1877 result = device_destroy_bond((bt_address_t *)device_address);
1878 if (result != OAL_STATUS_SUCCESS)
1881 return BLUETOOTH_ERROR_NONE;
1884 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1885 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1886 trigger_unbond_info->addr);
1888 g_array_append_vals(*out_param1, &dev_info,
1889 sizeof(bluetooth_device_info_t));
1890 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
1892 if (result == OAL_STATUS_NOT_PAIRED)
1893 return BLUETOOTH_ERROR_NOT_PAIRED;
1895 return BLUETOOTH_ERROR_INTERNAL;
1898 int _bt_cancel_bonding(void)
1900 int result = OAL_STATUS_SUCCESS;
1903 retv_if(trigger_bond_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1905 result = device_stop_bond((bt_address_t *)trigger_bond_info->dev_addr);
1907 if (result == OAL_STATUS_SUCCESS)
1908 trigger_bond_info->is_cancelled_by_user = TRUE;
1913 int _bt_passkey_reply(const char *passkey, gboolean cnfm_reply)
1915 bluetooth_device_address_t device_address;
1916 int ret = OAL_STATUS_SUCCESS;
1917 BT_INFO("reply: %d", cnfm_reply);
1919 retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1920 retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1922 _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1924 if (trigger_pairing_info->is_ssp) {
1926 ret = device_accept_passkey_entry((bt_address_t *)&device_address, atoi(passkey));
1928 ret = device_reject_passkey_entry((bt_address_t *)&device_address);
1929 trigger_pairing_info->is_ssp = FALSE;
1932 ret = device_accept_pin_request((bt_address_t *)&device_address, passkey);
1934 ret = device_reject_pin_request((bt_address_t *)&device_address);
1937 __bt_free_pairing_info(&trigger_pairing_info);
1939 if (ret != OAL_STATUS_SUCCESS) {
1940 BT_ERR("_bt_device_handle_passkey_reply: err [%d]", ret);
1941 return BLUETOOTH_ERROR_INTERNAL;
1945 return BLUETOOTH_ERROR_NONE;
1948 int _bt_passkey_confirmation_reply(gboolean cnfm_reply)
1950 BT_INFO("BT_PASSKEY_CONFIRMATION_REPLY");
1951 bluetooth_device_address_t device_address;
1952 int ret = OAL_STATUS_SUCCESS;
1953 BT_INFO("reply: %d", cnfm_reply);
1955 retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1956 retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1958 _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1960 ret = device_reply_passkey_confirmation((bt_address_t *)&device_address, cnfm_reply);
1962 __bt_free_pairing_info(&trigger_pairing_info);
1963 if (ret != OAL_STATUS_SUCCESS) {
1964 BT_ERR("_bt_device_handle_passkey_confirmation_reply: err [%d]", ret);
1965 return BLUETOOTH_ERROR_INTERNAL;
1969 return BLUETOOTH_ERROR_NONE;
1972 gboolean _bt_device_is_pairing(void)
1974 return (trigger_pairing_info) ? TRUE : FALSE;
1977 gboolean _bt_device_is_bonding(void)
1979 return (trigger_bond_info) ? TRUE : FALSE;
1982 gboolean _bt_is_bonding_device_address(const char *address)
1984 if (trigger_bond_info == NULL || trigger_bond_info->addr == NULL)
1987 if (g_strcmp0(trigger_bond_info->addr, address) == 0) {
1988 BT_DBG("[%s] is bonding device", address);
1992 BT_DBG("[%s] is NOT bonding device", address);
1996 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
1998 ret_if(trigger_bond_info == NULL);
1999 trigger_bond_info->is_autopair = is_autopair;
2002 int _bt_search_device(bluetooth_device_address_t *device_address)
2004 int result = OAL_STATUS_SUCCESS;
2007 BT_CHECK_PARAMETER(device_address, return);
2009 if (trigger_bond_info) {
2010 BT_ERR("Bonding in progress");
2011 return BLUETOOTH_ERROR_DEVICE_BUSY;
2014 if (service_search_info) {
2015 BT_ERR("Service searching in progress");
2016 return BLUETOOTH_ERROR_DEVICE_BUSY;
2019 /* allocate user data so that it can be retrieved in callback */
2020 service_search_info = g_malloc0(sizeof(bt_service_search_info_data_t));
2021 service_search_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
2022 service_search_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
2024 _bt_convert_addr_type_to_string(service_search_info->addr,
2025 device_address->addr);
2027 result = device_query_services((bt_address_t *)device_address);
2029 if (result != OAL_STATUS_SUCCESS) {
2030 BT_ERR("Device Service Search Failed..: %d", result);
2031 __bt_free_service_search_info(&service_search_info);
2032 return BLUETOOTH_ERROR_INTERNAL;
2034 return BLUETOOTH_ERROR_NONE;
2037 int _bt_cancel_search_device(void)
2039 int ret = OAL_STATUS_SUCCESS;
2040 retv_if(service_search_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
2042 ret = device_stop_query_sevices((bt_address_t *)service_search_info->dev_addr);
2044 if (ret != OAL_STATUS_SUCCESS) {
2045 BT_ERR("SDP Cancel request failed [%d]", ret);
2046 return BLUETOOTH_ERROR_INTERNAL;
2049 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_CANCEL_BY_USER, BT_SEARCH_SERVICE,
2050 service_search_info->addr, BT_ADDRESS_STRING_SIZE);
2052 __bt_free_service_search_info(&service_search_info);
2054 return BLUETOOTH_ERROR_NONE;
2058 int _bt_set_authorization(bluetooth_device_address_t *device_address,
2061 int ret = OAL_STATUS_SUCCESS;
2064 BT_CHECK_PARAMETER(device_address, return);
2065 BT_INFO("Device to be Trusted? [%d]", authorize);
2067 ret = device_set_authorized((bt_address_t*)device_address, authorize);
2068 if (ret != OAL_STATUS_SUCCESS) {
2069 BT_ERR("device_set_authorized: %d", ret);
2070 return BLUETOOTH_ERROR_INTERNAL;
2073 return BLUETOOTH_ERROR_NONE;
2076 gboolean _bt_is_device_connected(bluetooth_device_address_t *device_address, int svc_type)
2078 gboolean is_connected;
2079 oal_service_t svc_id;
2081 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
2084 * TODO: While adding support for new profiles, need to add more
2085 * <svc_type, svc_id> mapping here.
2088 case BT_PROFILE_CONN_HID:
2089 svc_id = HID_SERVICE_ID;
2091 case BT_PROFILE_CONN_A2DP:
2092 svc_id = A2DP_SERVICE_ID; /* Remote is A2DP Sink */
2094 case BT_PROFILE_CONN_A2DP_SINK:
2095 svc_id = A2DP_SRC_SERVICE_ID; /* Remote is A2DP Source*/
2097 case BT_PROFILE_CONN_HSP:
2098 svc_id = HFP_HS_SERVICE_ID; /* Remote is HFP HF Unit */
2100 #ifdef TIZEN_GATT_CLIENT
2101 case BT_PROFILE_CONN_GATT:
2102 return _bt_is_remote_gatt_device_connected(device_address); /* Remote is GATT client or Server */
2105 BT_DBG("Unknown svc_type: %d", svc_type);
2109 is_connected = device_get_svc_conn_state((bt_address_t*)device_address, svc_id);
2111 BT_DBG("svc_type: %d, is_connected: %s",
2112 svc_type, is_connected ? "TRUE" : "FALSE");
2114 return is_connected;
2117 int _bt_rfcomm_reply_conn_authorization(char *address, gboolean reply)
2119 bt_address_t bd_addr;
2124 retv_if(NULL == address, BLUETOOTH_ERROR_INVALID_PARAM);
2125 _bt_convert_addr_string_to_type(bd_addr.addr, address);
2126 res = device_reply_auth_request(&bd_addr, 0, reply, FALSE);
2127 if (res != OAL_STATUS_SUCCESS) {
2128 BT_ERR("authorize_response: %d", res);
2129 return BLUETOOTH_ERROR_INTERNAL;
2133 return BLUETOOTH_ERROR_NONE;
2136 int _bt_enable_rssi(bluetooth_device_address_t *addr, int link_type,
2137 int low_threshold, int in_range_threshold, int high_threshold)
2140 bt_address_t bd_addr;
2144 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2146 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2147 result = device_enable_rssi_monitoring(&bd_addr, link_type,
2148 low_threshold, in_range_threshold, high_threshold);
2149 if (result != OAL_STATUS_SUCCESS) {
2150 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2151 return BLUETOOTH_ERROR_INTERNAL;
2155 return BLUETOOTH_ERROR_NONE;
2158 int _bt_get_rssi_strength(bluetooth_device_address_t *addr, int link_type)
2161 bt_address_t bd_addr;
2165 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2167 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2168 result = device_get_connected_link_rssi_strength(&bd_addr, link_type);
2169 if (result != OAL_STATUS_SUCCESS) {
2170 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2171 return BLUETOOTH_ERROR_INTERNAL;
2175 return BLUETOOTH_ERROR_NONE;
2178 int _bt_set_passkey_notification(const char *sender, gboolean enable)
2182 BT_INFO("Set passkey notification(sender:%s, %s)",
2183 sender, enable ? "Enable" : "Disable");
2185 result = device_enable_gap_auth_notifications(OAL_PASSKEY_DISPLAY, enable);
2186 if (OAL_STATUS_SUCCESS != result) {
2187 BT_ERR("device_enable_gap_auth_notifications error: [%d]", result);
2188 return BLUETOOTH_ERROR_INTERNAL;
2191 g_free(passkey_watcher);
2193 passkey_watcher = g_strdup(sender);
2195 passkey_watcher = NULL;
2197 return BLUETOOTH_ERROR_NONE;
2200 static int __bt_get_device_pin_code(const char *address, char *pin_code)
2204 BT_CHECK_PARAMETER(address, return);
2205 BT_CHECK_PARAMETER(pin_code, return);
2207 for (l = pin_info_list; l != NULL; l = l->next) {
2208 bt_pin_code_info_t *pin_info = l->data;
2210 if (!pin_info || !pin_info->address)
2213 if (g_strcmp0(pin_info->address, address) == 0) {
2214 g_strlcpy(pin_code, pin_info->pin_code,
2215 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2216 return BLUETOOTH_ERROR_NONE;
2220 return BLUETOOTH_ERROR_NOT_FOUND;
2223 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2224 bluetooth_device_pin_code_t *pin_code)
2226 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2227 bt_pin_code_info_t *pin_info = NULL;
2230 BT_CHECK_PARAMETER(device_address, return);
2231 BT_CHECK_PARAMETER(pin_code, return);
2232 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2233 BLUETOOTH_ERROR_NO_RESOURCES);
2235 _bt_convert_addr_type_to_string(address, device_address->addr);
2237 for (l = pin_info_list; l != NULL; l = l->next) {
2240 if (!pin_info || !pin_info->address)
2243 if (g_strcmp0(pin_info->address, address) == 0) {
2244 g_free(pin_info->pin_code);
2245 pin_info->pin_code = g_strdup(pin_code->pin_code);
2246 return BLUETOOTH_ERROR_NONE;
2250 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2251 pin_info->address = g_strdup(address);
2252 pin_info->pin_code = g_strdup(pin_code->pin_code);
2253 pin_info_list = g_slist_append(pin_info_list, pin_info);
2255 return BLUETOOTH_ERROR_NONE;
2258 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2260 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2261 bt_pin_code_info_t *pin_info = NULL;
2266 BT_CHECK_PARAMETER(device_address, return);
2268 _bt_convert_addr_type_to_string(address, device_address->addr);
2270 for (l = pin_info_list; l != NULL; l = l->next) {
2273 if (!pin_info || !pin_info->address)
2276 if (g_strcmp0(pin_info->address, address) == 0) {
2277 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2278 g_free(pin_info->address);
2279 g_free(pin_info->pin_code);
2286 return BLUETOOTH_ERROR_NONE;
2289 int _bt_device_get_ida(bluetooth_device_address_t *device_address, bluetooth_device_address_t *id_address)
2291 int result = BLUETOOTH_ERROR_NONE;
2292 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2294 _bt_convert_addr_type_to_string(address, device_address->addr);
2296 BT_DBG("getting IDA for remote device: [%s]", address);
2298 result = device_get_ida((bt_address_t*)device_address, (bt_address_t*)id_address);
2300 if (result != OAL_STATUS_SUCCESS) {
2301 BT_ERR("device_get_ida Failed %d", result);
2302 return BLUETOOTH_ERROR_INTERNAL;
2308 #ifdef TIZEN_GATT_CLIENT
2309 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2312 bt_connected_le_dev_t *dev;
2317 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2320 if (g_strcmp0(dev->address, address) == 0)
2326 static void __bt_le_conn_param_free(void *data)
2328 bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2330 BT_DBG("%s", param->sender);
2331 g_free(param->sender);
2335 static void _bt_add_le_connected_dev_info(const char *address)
2337 bt_connected_le_dev_t *dev = NULL;
2342 dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2343 dev->address = g_strdup(address);
2345 le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2350 static void _bt_remove_le_connected_dev_info(const char *address)
2352 bt_connected_le_dev_t *dev = NULL;
2357 dev = __bt_get_le_connected_dev_info(address);
2361 g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2362 le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2363 g_free(dev->address);
2369 static void _bt_handle_le_connected_dev_info(const char *address, gboolean connected)
2371 BT_DBG("update le_connected_dev_list");
2374 _bt_add_le_connected_dev_info(address);
2376 _bt_remove_le_connected_dev_info(address);
2379 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2382 bt_le_conn_param_t *param = NULL;
2384 if (!dev || !sender)
2387 for (l = dev->senders; l; l = g_slist_next(l)) {
2389 if (g_strcmp0(param->sender, sender) == 0)
2396 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2398 bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2399 bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2401 return parama->key > paramb->key;
2405 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2406 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2408 bt_connected_le_dev_t *dev = NULL;
2409 bt_le_conn_param_t *param = NULL;
2410 bt_le_conn_param_t *data = NULL;
2412 if (!address || !sender)
2413 return BLUETOOTH_ERROR_INVALID_PARAM;
2415 dev = __bt_get_le_connected_dev_info(address);
2417 return BLUETOOTH_ERROR_INTERNAL;
2419 param = __bt_get_le_conn_param_info(dev, sender);
2421 data = g_malloc0(sizeof(bt_le_conn_param_t));
2422 data->sender = g_strdup(sender);
2423 data->interval_min = interval_min;
2424 data->interval_max = interval_max;
2425 data->latency = latency;
2426 data->time_out = time_out;
2427 data->key = interval_min + (interval_max - interval_min)/2;
2429 if (param == NULL) {
2430 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2431 dev->senders = g_slist_append(dev->senders, data);
2433 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2434 dev->senders = g_slist_remove(dev->senders, param);
2435 g_free(param->sender);
2437 dev->senders = g_slist_append(dev->senders, data);
2440 /* Sorting. First element have the minimum interval */
2441 dev->senders = g_slist_sort(dev->senders,
2442 (GCompareFunc)__bt_compare_le_conn_param_key);
2444 return BLUETOOTH_ERROR_NONE;
2448 static int __bt_le_set_conn_parameter(const char *address,
2449 float interval_min, float interval_max,
2450 guint16 latency, guint16 time_out)
2452 bt_address_t dev_addr = { {0} };
2453 guint32 min, max, to;
2455 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2456 interval_min, interval_max, latency, time_out);
2458 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2459 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2460 to = time_out / BT_LE_CONN_TO_SPLIT;
2462 BT_INFO("updating: Min interval: %d, Max interval: %d, Latency: %d, Supervision timeout: %d",
2463 min, max, latency, to);
2465 _bt_convert_addr_string_to_type(dev_addr.addr, address);
2467 return gattc_conn_param_update(&dev_addr, min, max, latency, to);
2470 int _bt_remove_le_conn_param_info(const char *address, const char *sender)
2472 bt_connected_le_dev_t *dev = NULL;
2473 bt_le_conn_param_t *param = NULL;
2475 if (!address || !sender)
2476 return BLUETOOTH_ERROR_INVALID_PARAM;
2478 dev = __bt_get_le_connected_dev_info(address);
2480 return BLUETOOTH_ERROR_INTERNAL;
2482 param = __bt_get_le_conn_param_info(dev, sender);
2484 BT_DBG("Remove param %s %s ", address, sender);
2485 dev->senders = g_slist_remove(dev->senders, param);
2486 g_free(param->sender);
2490 return BLUETOOTH_ERROR_NONE;
2493 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2494 bluetooth_le_connection_param_t *param)
2497 return BLUETOOTH_ERROR_INVALID_PARAM;
2499 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2502 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2503 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2504 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2505 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2506 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2509 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2510 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2511 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2512 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2513 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2516 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2517 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2518 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2519 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2520 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2524 BT_ERR("Unhandled mode : %d", mode);
2528 return BLUETOOTH_ERROR_NONE;
2531 int _bt_le_connection_update(const char *sender,
2532 unsigned char *device_address,
2533 float interval_min, float interval_max,
2534 guint16 latency, guint16 time_out)
2536 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2537 guint32 min_supervision_to;
2538 bt_connected_le_dev_t *dev = NULL;
2539 bt_le_conn_param_t *param = NULL;
2540 int ret = BLUETOOTH_ERROR_NONE;
2542 BT_CHECK_PARAMETER(device_address, return);
2544 BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2545 sender, interval_min, interval_max, latency, time_out);
2547 if (interval_min > interval_max ||
2548 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2549 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2550 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2554 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2555 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2556 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2560 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2561 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2566 * The Supervision_Timeout in milliseconds shall be larger than
2567 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2568 * where Conn_Interval_Max is given in milliseconds.
2571 min_supervision_to = (1 + latency) * interval_max * 2;
2572 if (time_out <= min_supervision_to) {
2573 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2577 _bt_convert_addr_type_to_string(address, device_address);
2578 BT_DBG("Remote device address: %s", address);
2580 _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2582 dev = __bt_get_le_connected_dev_info(address);
2584 BT_ERR("device not found in the list");
2585 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2589 if (g_slist_length(dev->senders) == 1)
2592 param = dev->senders->data;
2594 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2596 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2597 BT_DBG("Skip due to same interval");
2601 interval_min = param->interval_min;
2602 interval_max = param->interval_max;
2606 ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2608 if (ret != OAL_STATUS_SUCCESS) {
2609 _bt_remove_le_conn_param_info(address, sender);
2610 BT_DBG("fail to update the LE connection parameter");
2611 ret = BLUETOOTH_ERROR_INTERNAL;
2615 dev->interval_min = interval_min;
2616 dev->interval_max = interval_max;
2618 return BLUETOOTH_ERROR_NONE;
2623 int _bt_disconnect_device(bluetooth_device_address_t *device_address)
2625 int result = OAL_STATUS_SUCCESS;
2629 retv_if(!device_address, BLUETOOTH_ERROR_INVALID_PARAM);
2631 result = device_disconnect((bt_address_t *)device_address);
2632 if (result != OAL_STATUS_SUCCESS) {
2633 BT_DBG("Failed to disconnect device");
2634 return BLUETOOTH_ERROR_INTERNAL;
2638 return BLUETOOTH_ERROR_NONE;
2641 static gboolean __bt_ignore_auto_pairing_request(const char *address)
2646 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
2654 if (address == NULL)
2657 /* Get the LAP(Lower Address part) */
2658 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
2660 snprintf(lap_address, sizeof(lap_address), ",%s", address);
2662 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
2665 BT_ERR("fopen failed \n");
2669 fseek(fp, 0, SEEK_END);
2674 BT_ERR("Get file size failed \n");
2679 buffer = g_malloc0(sizeof(char) * size);
2680 result = fread((char *)buffer, 1, size, fp);
2682 if (result != size) {
2683 BT_ERR("Read Error\n");
2688 BT_DBG("Buffer = %s\n", buffer);
2690 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
2696 /* Write the data and insert new device data */
2697 for (i = 0; lines[i] != NULL; i++) {
2698 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
2699 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
2701 lines[i] = temp_buffer;
2704 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
2706 /* Fix : NULL_RETURNS */
2707 retv_if(buffer == NULL, FALSE);
2709 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
2712 BT_ERR("fopen failed \n");
2717 BT_DBG("Buffer = %s\n", buffer);
2718 fwrite(buffer, 1, strlen(buffer), fp);