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 #ifdef TIZEN_GATT_CLIENT
147 } bt_connected_le_dev_t;
156 } bt_le_conn_param_t;
158 static GSList *le_connected_dev_list = NULL;
160 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
161 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
162 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
163 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
164 #define BT_LE_CONN_SLAVE_LATENCY_MAX 499
165 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
166 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
168 static void _bt_handle_le_connected_dev_info(const char *address, gboolean connected);
171 /* Forward declaration */
172 static void __bt_device_event_handler(int event_type, gpointer event_data);
173 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble);
176 static int __bt_device_handle_bond_state(void);
177 static void __bt_free_bond_info(uint8_t type);
178 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info);
179 static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr);
180 static void __bt_device_handle_bond_removal_event(bt_address_t *bd_addr);
181 static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_fail_event);
182 static void __bt_handle_ongoing_bond(bt_remote_dev_info_t *remote_dev_info, gboolean incoming_bond);
183 static void __bt_device_acl_state_changed_callback(event_dev_conn_status_t *acl_event,
184 gboolean connected, unsigned char type);
185 static void __bt_free_pairing_info(bt_pairing_data_t **p_info);
187 static void __bt_device_ssp_consent_callback(remote_device_t* dev_info);
188 static void __bt_device_pin_request_callback(remote_device_t* pin_req_event);
189 static void __bt_device_ssp_passkey_display_callback(event_dev_passkey_t *dev_info);
190 static void __bt_device_ssp_passkey_confirmation_callback(event_dev_passkey_t *dev_info);
191 static void __bt_device_ssp_passkey_entry_callback(remote_device_t* dev_info);
192 static void __bt_device_authorization_request_callback(event_dev_authorize_req_t* auth_event);
194 static void __bt_device_services_callback(event_dev_services_t* uuid_list);
195 static void __bt_handle_ongoing_device_service_search(bt_remote_dev_info_t *remote_dev_info);
197 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info);
199 static int __bt_get_device_pin_code(const char *address, char *pin_code);
201 gboolean _bt_is_device_creating(void)
203 if (!trigger_bond_info)
205 return trigger_bond_info->is_device_creating;
208 void _bt_device_state_handle_callback_set_request(void)
210 _bt_service_register_event_handler_callback(
211 BT_DEVICE_MODULE, __bt_device_event_handler);
214 void __bt_device_handle_pending_requests(int result, int service_function,
215 void *user_data, unsigned int size)
219 invocation_info_t *req_info = NULL;
223 /* Get method invocation context */
224 for (l = _bt_get_invocation_list(); l != NULL; ) {
227 if (req_info == NULL || req_info->service_function != service_function)
230 switch (service_function) {
231 case BT_SEARCH_SERVICE: {
232 char *address = (char *)user_data;
233 if (strncmp((char*)req_info->user_data, address, BT_ADDRESS_STRING_SIZE)) {
234 BT_ERR("Unexpected: Info request pending for a different address!!");
237 BT_INFO("Found info request addr [%s]", (char*)req_info->user_data);
238 bt_sdp_info_t sdp_info;
240 memset(&sdp_info, 0x00, sizeof(bt_sdp_info_t));
241 _bt_convert_addr_string_to_type(sdp_info.device_addr.addr, address);
243 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
244 g_array_append_vals(out_param, &sdp_info, sizeof(bt_sdp_info_t));
245 _bt_service_method_return(req_info->context, out_param, result);
247 g_free(req_info->user_data);
248 _bt_free_info_from_invocation_list(req_info);
249 g_array_free(out_param, TRUE);
253 case BT_BOND_DEVICE: {
254 char *address = (char *)user_data;
255 if (strncmp((char*)req_info->user_data, address, BT_ADDRESS_STRING_SIZE)) {
256 BT_ERR("Unexpected: Info request pending for a different address!!");
259 BT_INFO("Found info request addr [%s]", (char*)req_info->user_data);
260 bluetooth_device_info_t dev_info;
261 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
262 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
264 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
265 g_array_append_vals(out_param, &dev_info,
266 sizeof(bluetooth_device_info_t));
267 _bt_service_method_return(req_info->context, out_param, result);
269 g_free(req_info->user_data);
270 _bt_free_info_from_invocation_list(req_info);
271 g_array_free(out_param, TRUE);
275 case BT_UNBOND_DEVICE: {
276 char *address = (char *)user_data;
277 if (strncmp((char*)req_info->user_data, address, BT_ADDRESS_STRING_SIZE)) {
278 BT_ERR("Unexpected: Info request pending for a different address!!");
281 BT_INFO("Found info request addr [%s]", (char*)req_info->user_data);
282 bluetooth_device_address_t dev_addr;
283 _bt_convert_addr_string_to_type(dev_addr.addr, address);
284 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
285 g_array_append_vals(out_param, &dev_addr,
286 sizeof(bluetooth_device_address_t));
287 _bt_service_method_return(req_info->context, out_param, result);
289 g_free(req_info->user_data);
290 _bt_free_info_from_invocation_list(req_info);
291 g_array_free(out_param, TRUE);
296 BT_ERR("Unhandled case");
304 * Remote device properties are received on all following conditions
305 * a. When Bonding in on-going
306 * b. When device properties are updated\changed for a connected device
307 * (due to SDP or any other reason)
309 static void __bt_device_remote_properties_callback(event_dev_properties_t *oal_dev_props)
311 bt_remote_dev_info_t *rem_info = NULL;
314 rem_info = g_malloc0(sizeof(bt_remote_dev_info_t));
315 _bt_copy_remote_dev(rem_info, &(oal_dev_props->device_info));
317 if (oal_dev_props->adv_len > 0) {
320 rem_info->manufacturer_data_len = oal_dev_props->adv_len;
321 rem_info->manufacturer_data =
322 g_memdup(oal_dev_props->adv_data,
323 oal_dev_props->adv_len);
324 BT_DBG("----Advertising Data Length: %d",
325 rem_info->manufacturer_data_len);
327 for (k = 0; k < rem_info->manufacturer_data_len; k++) {
328 BT_INFO("Check data[%d] = [[0x%x]",
329 k, oal_dev_props->adv_data[k]);
332 rem_info->manufacturer_data = NULL;
333 rem_info->manufacturer_data_len = 0;
336 /* a. Check if bonding is on-going, if yes, we MUST update the bonding device properties */
337 if (trigger_bond_info && !strcmp(trigger_bond_info->addr, rem_info->address)) {
338 BT_INFO("Bonding is ongoing, try update properties");
339 if (!trigger_bond_info->dev_info ||
340 (!trigger_bond_info->dev_info->name &&
341 !trigger_bond_info->dev_info->alias) ||
342 !trigger_bond_info->dev_info->address ||
343 trigger_bond_info->dev_info->uuid_count == 0) {
344 BT_INFO("Complete data is not present, Assigning rem_info");
345 if (!trigger_bond_info->dev_info)
346 trigger_bond_info->dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
347 _bt_copy_remote_dev_info(trigger_bond_info->dev_info, rem_info);
350 BT_DBG("Bonding dev addr has matched with remote dev properties address [%s]", rem_info->address);
351 __bt_handle_ongoing_bond(trigger_bond_info->dev_info, FALSE);
352 } else if (incoming_bond_info && !g_strcmp0(incoming_bond_info->addr, rem_info->address)) {
353 BT_INFO("Incoming Bond is ongoing, try update properties");
354 if (!incoming_bond_info->dev_info ||
355 (!incoming_bond_info->dev_info->name &&
356 !incoming_bond_info->dev_info->alias) ||
357 !incoming_bond_info->dev_info->address ||
358 incoming_bond_info->dev_info->uuid_count == 0) {
359 BT_INFO("Complete data is not present, Assigning rem_info");
360 if (!incoming_bond_info->dev_info)
361 incoming_bond_info->dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
362 _bt_copy_remote_dev_info(incoming_bond_info->dev_info, rem_info);
365 BT_DBG("Incoming Bond addr matches with remote dev properties address [%s]", rem_info->address);
366 __bt_handle_ongoing_bond(incoming_bond_info->dev_info, TRUE);
369 /* Add device to bonded list */
370 _bt_service_add_device_to_bonded_list(rem_info);
372 /* Handle SDP Device properties update */
373 if (service_search_info && service_search_info->dev_info) {
374 if (!strcmp(service_search_info->addr, rem_info->address)) {
375 BT_DBG("Properties received and SDP request pending, fill device properties and send event");
376 service_search_info->dev_info->class = rem_info->class;
377 service_search_info->dev_info->paired = rem_info->paired;
378 service_search_info->dev_info->connected = rem_info->connected;
379 service_search_info->dev_info->rssi = rem_info->rssi;
380 service_search_info->dev_info->addr_type = rem_info->addr_type;
381 service_search_info->dev_info->trust = rem_info->trust;
384 service_search_info->dev_info->manufacturer_data = NULL;
385 service_search_info->dev_info->manufacturer_data_len = 0;
387 __bt_handle_ongoing_device_service_search(service_search_info->dev_info);
391 _bt_free_remote_dev(rem_info);
395 static int __get_oal_trusted_profile(bluetooth_trusted_profile_t profile)
398 case TRUSTED_PROFILE_PBAP:
399 return OAL_TRUSTED_PROFILE_PBAP;
400 case TRUSTED_PROFILE_MAP:
401 return OAL_TRUSTED_PROFILE_MAP;
402 case TRUSTED_PROFILE_SAP:
403 return OAL_TRUSTED_PROFILE_SAP;
404 case TRUSTED_PROFILE_HFP_HF:
405 return OAL_TRUSTED_PROFILE_HFP_HF;
406 case TRUSTED_PROFILE_A2DP:
407 return OAL_TRUSTED_PROFILE_A2DP;
408 case TRUSTED_PROFILE_ALL:
409 return OAL_TRUSTED_PROFILE_ALL;
415 int _bt_set_trust_profile(bluetooth_device_address_t *addr,
416 bluetooth_trusted_profile_t profile, gboolean trust)
419 bt_address_t bd_addr;
420 oal_trusted_profile_e oal_profile;
424 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
426 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
427 oal_profile = __get_oal_trusted_profile(profile);
428 retv_if(0 == oal_profile, BLUETOOTH_ERROR_NOT_SUPPORT);
430 result = device_set_trust_profile(&bd_addr, oal_profile, trust);
431 if (result != OAL_STATUS_SUCCESS) {
432 BT_ERR("device_set_trust_profile error: [%d]", result);
433 return BLUETOOTH_ERROR_INTERNAL;
437 return BLUETOOTH_ERROR_NONE;
440 int _bt_get_trust_profile(bluetooth_device_address_t *addr,
441 bluetooth_trusted_profile_t profile, guint *trust)
444 bt_address_t bd_addr;
445 oal_trusted_profile_e oal_profile;
449 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
451 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
452 oal_profile = __get_oal_trusted_profile(profile);
453 retv_if(0 == oal_profile, BLUETOOTH_ERROR_NOT_SUPPORT);
455 result = device_get_trust_profile(&bd_addr, oal_profile, trust);
456 if (result != OAL_STATUS_SUCCESS) {
457 BT_ERR("device_set_trust_profile error: [%d]", result);
458 return BLUETOOTH_ERROR_INTERNAL;
462 return BLUETOOTH_ERROR_NONE;
465 static void __bt_handle_ongoing_device_service_search(bt_remote_dev_info_t *remote_dev_info)
467 GVariant *param = NULL;
468 GVariant *uuids = NULL;
469 GVariantBuilder *builder = NULL;
470 GVariant *manufacturer_data;
474 BT_INFO("Send Service Search request event");
476 if (remote_dev_info->alias)
477 name = remote_dev_info->alias;
479 name = remote_dev_info->name;
481 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
482 for (i = 0; i < remote_dev_info->uuid_count; i++) {
483 g_variant_builder_add(builder, "s",
484 remote_dev_info->uuids[i]);
486 uuids = g_variant_new("as", builder);
487 g_variant_builder_unref(builder);
488 manufacturer_data = g_variant_new_from_data((const GVariantType *)"ay",
489 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
492 param = g_variant_new("(isunsbub@asn@ay)",
493 BLUETOOTH_ERROR_NONE,
494 remote_dev_info->address,
495 remote_dev_info->class,
496 remote_dev_info->rssi,
498 remote_dev_info->paired,
499 remote_dev_info->connected,
500 remote_dev_info->trust,
502 remote_dev_info->manufacturer_data_len,
504 /* Send the event to application */
505 _bt_send_event(BT_ADAPTER_EVENT,
506 BLUETOOTH_EVENT_SERVICE_SEARCHED,
509 __bt_free_service_search_info(&service_search_info);
513 static void __bt_device_services_callback(event_dev_services_t* uuid_list)
515 bt_remote_dev_info_t *rem_info = NULL;
519 if (trigger_bond_info && _bt_compare_adddress(trigger_bond_info->dev_addr,
520 (bluetooth_device_address_t *)&uuid_list->address) == TRUE) {
521 bluetooth_device_address_t *dev_addr = trigger_bond_info->dev_addr;
523 BT_DBG("Bonding dev addr has matched");
524 /* Bonding ongoing, Query device properties again */
525 if (BLUETOOTH_ERROR_NONE ==
526 _bt_device_get_bonded_device_info(dev_addr))
527 BT_DBG("_bt_device_get_bonded_device_info success");
529 BT_ERR("_bt_device_get_bonded_device_info failed");
532 if (service_search_info == NULL) {
534 BT_DBG("searching_info == NULL");
538 if (_bt_compare_adddress(service_search_info->dev_addr,
539 (bluetooth_device_address_t *)&uuid_list->address) == FALSE) {
540 BT_DBG("This device is not queried");
544 rem_info = g_malloc0(sizeof(bt_remote_dev_info_t));
545 memset(rem_info, 0x00, sizeof(bt_remote_dev_info_t));
547 rem_info->address = g_new0(char, BT_ADDRESS_STRING_SIZE);
548 _bt_convert_addr_type_to_string(rem_info->address, uuid_list->address.addr);
550 rem_info->uuid_count = uuid_list->num;
552 BT_INFO("Address [%s]", rem_info->address);
553 BT_INFO("Number of UUID's [%d]", rem_info->uuid_count);
554 if (rem_info->uuid_count > 0)
555 rem_info->uuids = g_new0(char *, rem_info->uuid_count);
557 /* Fill Remote Device Service List list */
558 for (i = 0; i < rem_info->uuid_count; i++) {
559 rem_info->uuids[i] = g_malloc0(BLUETOOTH_UUID_STRING_MAX);
560 _bt_uuid_to_string((service_uuid_t *)&uuid_list->service_list[i].uuid, rem_info->uuids[i]);
561 BT_DBG("UUID value=%s", rem_info->uuids[i]);
564 /* Update local cache */
565 _bt_update_remote_dev_property(rem_info->address, DEV_PROP_SERVICES, (void *)rem_info);
567 BT_DBG("DBUS return");
568 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_SEARCH_SERVICE,
569 service_search_info->addr, BT_ADDRESS_STRING_SIZE);
571 /* Save UUID List of remote devices */
572 if (service_search_info->dev_info)
573 _bt_free_remote_dev(service_search_info->dev_info);
574 service_search_info->dev_info = rem_info;
576 /* Query Other device properties */
577 if (_bt_device_get_bonded_device_info(service_search_info->dev_addr) == BLUETOOTH_ERROR_NONE) {
578 BT_DBG("Bonded device info query posted to stack successfully");
580 BT_DBG("Possibly internal stack error in bonded device info query, perform cleanup");
581 __bt_free_service_search_info(&service_search_info);
586 static void __bt_device_authorization_request_callback(event_dev_authorize_req_t* auth_event)
588 oal_service_t service_d = auth_event->service_id;
589 gchar address[BT_ADDRESS_STR_LEN];
592 _bt_convert_addr_type_to_string(address, auth_event->address.addr);
594 BT_INFO("service_d: %d", service_d);
598 BT_INFO("Incoming HID Profile conn Req from device addr [%s]", address);
600 case A2DP_SERVICE_ID:
601 BT_INFO("Incoming A2DP(Remote Sink) profile conn Req from device addr [%s]", address);
602 _bt_a2dp_src_handle_incoming_authorization(address, service_d);
604 case A2DP_SRC_SERVICE_ID:
605 BT_INFO("Incoming A2DP(Remote Source) Profile conn Req from device addr [%s]", address);
606 _bt_a2dp_sink_handle_incoming_authorization(address, service_d);
608 case AVRCP_SERVICE_ID:
609 BT_INFO("Incoming AVRCP (Remote) Profile conn Req from device addr [%s]", address);
611 case AVRCP_CT_SERVICE_ID:
612 BT_INFO("Incoming AVRCP (Controller) Profile conn Req from device addr [%s]", address);
614 #ifdef TIZEN_FEATURE_BT_OBEX
615 case OPP_SERVICE_ID: {
616 GVariant *param = NULL;
619 if (_bt_obex_server_is_custom() == false) {
620 /* Allow the connection for native OPP server */
624 name = g_strdup(address);
626 BT_INFO("Incoming OPP conn Req from device addr [%s]", address);
627 _bt_obex_server_set_pending_conn_auth_device_addr(address);
628 param = g_variant_new("(iss)", BLUETOOTH_ERROR_NONE, address, name);
629 _bt_send_event(BT_OPP_SERVER_EVENT,
630 BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE, param);
636 BT_INFO("Incoming HSP_SERVICE_ID conn Req from device addr [%s]", address);
639 BT_INFO("Incoming HFP_SERVICE_ID conn Req from device addr [%s]", address);
642 BT_INFO("Incoming SAP_SERVICE_ID conn Req from device addr [%s]", address);
644 case HSP_HS_SERVICE_ID:
645 BT_INFO("Incoming HSP_HS_SERVICE_ID conn Req from device addr [%s]", address);
647 case HFP_HS_SERVICE_ID:
648 BT_INFO("Incoming HFP_HS_SERVICE_ID conn Req from device addr [%s]", address);
651 case IOTIVITY_SERVICE_ID:
652 BT_INFO("Incoming IOTIVITY_SERVICE_ID conn Req from device addr [%s]", address);
656 /* For now, reject authorization for any service apart from above switch cases */
657 BT_INFO("Incoming Profile conn req with service ID [%d] from device addr [%s]", service_d, address);
658 res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, FALSE, FALSE);
659 if (res != OAL_STATUS_SUCCESS)
660 BT_ERR("authorize_response: %d", res);
664 /* Auto accept authorization request for HID, A2DP and AVRCP profiles */
665 BT_INFO("Auto Accept authorization");
666 res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, TRUE, FALSE);
667 if (res != OAL_STATUS_SUCCESS)
668 BT_ERR("authorize_response: %d", res);
672 static void __bt_handle_ongoing_bond(bt_remote_dev_info_t *remote_dev_info, gboolean incoming_bond)
674 GVariant *param = NULL;
677 if ((remote_dev_info->name || remote_dev_info->alias)
678 && remote_dev_info->address
679 && remote_dev_info->uuids) {
680 BT_INFO("All properties updated, time to send bonding finished event");
681 GVariant *uuids = NULL;
682 GVariantBuilder *builder = NULL;
683 GVariant *manufacturer_data;
687 if (remote_dev_info->alias)
688 name = remote_dev_info->alias;
690 name = remote_dev_info->name;
692 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
693 for (i = 0; i < remote_dev_info->uuid_count; i++) {
694 g_variant_builder_add(builder, "s",
695 remote_dev_info->uuids[i]);
697 uuids = g_variant_new("as", builder);
698 g_variant_builder_unref(builder);
699 manufacturer_data = g_variant_new_from_data((const GVariantType *)"ay",
700 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
703 param = g_variant_new("(isunsbub@asn@ay)",
704 BLUETOOTH_ERROR_NONE,
705 remote_dev_info->address,
706 remote_dev_info->class,
707 remote_dev_info->rssi,
709 remote_dev_info->paired,
710 remote_dev_info->connected,
711 remote_dev_info->trust,
713 remote_dev_info->manufacturer_data_len,
715 /* Send the event to application */
716 _bt_send_event(BT_ADAPTER_EVENT,
717 BLUETOOTH_EVENT_BONDING_FINISHED,
720 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
722 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
723 __bt_free_pairing_info(&trigger_pairing_info);
726 BT_INFO("Lets wait for more remote device properties");
730 static void __handle_incoming_bond_created_event(bt_address_t *bd_addr)
732 char address[BT_ADDRESS_STRING_SIZE];
733 bluetooth_device_address_t dev_addr;
738 * BlueZ sends paired signal for each paired device, during activation,
739 * We should ignore this, otherwise application thinks that a new device
742 if (_bt_adapter_get_status() != BT_ACTIVATED) {
743 BT_DBG("BT is not activated, so ignore this");
747 _bt_convert_addr_type_to_string(address, bd_addr->addr);
748 if (!incoming_bond_info) {
749 incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
751 if (g_strcmp0(incoming_bond_info->addr, address)) {
752 BT_DBG("Previous Bond address: [%s] differs from new address: [%s]",
753 address, incoming_bond_info->addr);
754 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
755 incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
759 BT_INFO("Incoming bond successfully completed");
760 g_strlcpy(incoming_bond_info->addr, address, BT_ADDRESS_STRING_SIZE);
761 incoming_bond_info->dev_info = NULL;
763 _bt_convert_addr_string_to_type(dev_addr.addr, incoming_bond_info->addr);
764 if (BLUETOOTH_ERROR_NONE == _bt_device_get_bonded_device_info(&dev_addr)) {
765 BT_DBG("Bonded device info query posted to stack successfully");
767 BT_ERR("Bonded device info query failed");
768 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
774 static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr)
776 gchar address[BT_ADDRESS_STR_LEN];
777 bluetooth_device_address_t dev_addr;
781 if (trigger_bond_info == NULL) {
783 BT_DBG("trigger_bond_info == NULL, Handle incomming bond event");
784 __handle_incoming_bond_created_event(bd_addr);
788 _bt_convert_addr_type_to_string(address, bd_addr->addr);
789 if (g_strcmp0(trigger_bond_info->addr, address)) {
790 BT_DBG("Bonding address= [%s] is different from requested address =[%s]",
791 address, trigger_bond_info->addr);
792 __handle_incoming_bond_created_event(bd_addr);
796 BT_INFO("Bonding successfully completed");
797 /* Bonding state will be cleaned up & BONDING FINISHED EVENT
798 will be sent only when Properties are fetched from stack
799 Till that time lets not free trigger_bond_info.
800 However it is possible that while fetching device properties, internal
801 stack error can occur which can lead to no valid properties or
802 no properties at all. So in such cases, we must not wait for properties,
803 otherwise, it can lead to infinite wait */
804 _bt_convert_addr_string_to_type(dev_addr.addr,
805 trigger_bond_info->addr);
807 if (_bt_device_get_bonded_device_info(&dev_addr) == BLUETOOTH_ERROR_NONE) {
808 BT_DBG("BOnded device info query posted to stack successfully");
809 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_BOND_DEVICE,
810 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
812 BT_DBG("Possibly internal stack error in bonded device info query, perform cleanup");
813 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
814 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
815 /* Destroy if at all device got bonded at stack level */
816 device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
818 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
819 __bt_free_pairing_info(&trigger_pairing_info);
825 /**********************************************************************************************
826 * Bond removal event can be triggered for following reasons -
827 * a. If Bonding procedure if failed (for Auth failed, Page timeout, cancelled by user etc)
828 * b. If Application requests for explicitly removing the bond
829 * c. When application attempt to create bond,bond is removed first which triggers this event
830 * c. is in-line with Bluedroid bond create\emoval architecture
831 *********************************************************************************************/
832 static void __bt_device_handle_bond_removal_event(bt_address_t *bd_addr)
834 char address[BT_ADDRESS_STRING_SIZE];
838 _bt_convert_addr_type_to_string(address, bd_addr->addr);
839 _bt_service_remove_device_from_bonded_list(address);
841 if (trigger_unbond_info) {
842 BT_INFO("Bond removal request successfully handled, return DBUS and send event");
843 GVariant *param = NULL;
844 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_UNBOND_DEVICE,
845 trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
846 param = g_variant_new("(is)", BLUETOOTH_ERROR_NONE, trigger_unbond_info->addr);
847 _bt_send_event(BT_ADAPTER_EVENT,
848 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
850 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
851 __bt_free_pairing_info(&trigger_pairing_info);
852 } else if (trigger_bond_info) {
853 BT_ERR("Bonding was removed");
854 __bt_device_handle_bond_state();
859 static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_fail_event)
862 oal_status_t status = bond_fail_event->status;
863 BT_INFO("Bonding failed, reason: %d", status);
866 case OAL_STATUS_RMT_DEVICE_DOWN:
868 if (trigger_bond_info) {
869 BT_INFO("OAL_STATUS_RMT_DEVICE_DOWN:Lets retry bonding!! retry count [%d]",
871 int ret = OAL_STATUS_SUCCESS;
872 if (bond_retry_count < MAX_BOND_RETRY_COUNT) {
873 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
874 if (ret != OAL_STATUS_SUCCESS) {
875 BT_ERR("Create Bond procedure could not suceed");
876 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
877 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
878 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
879 __bt_free_pairing_info(&trigger_pairing_info);
880 bond_retry_count = 0;
884 BT_ERR("Create Bond failed MAX_BOND_RETRY_COUNT TIMES!!");
885 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
886 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
887 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
888 __bt_free_pairing_info(&trigger_pairing_info);
889 bond_retry_count = 0;
894 case OAL_STATUS_AUTH_FAILED:
896 /*TODO Auto pairing status set & ignore auto pairing logics can be done at this point.
897 To be considered later*/
898 int result = BLUETOOTH_ERROR_INTERNAL;
899 BT_INFO("BT_OPERATION_STATUS_AUTH_FAILED");
900 if (trigger_bond_info) {
901 BT_ERR("Create Bond procedure could not suceed, check if cancelled by User");
902 if (trigger_bond_info->is_cancelled_by_user) {
903 BT_ERR("Bonding is cancelled by user");
904 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
906 __bt_device_handle_pending_requests(result, BT_BOND_DEVICE,
907 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
908 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
911 __bt_free_pairing_info(&trigger_pairing_info);
914 case OAL_STATUS_INTERNAL_ERROR:
916 BT_INFO("OAL_STATUS_INTERNAL_ERROR");
917 if (trigger_unbond_info) {
918 BT_INFO("Bond removal request failed, return DBUS and send event");
919 GVariant *param = NULL;
920 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_UNBOND_DEVICE,
921 trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
922 param = g_variant_new("(is)", BLUETOOTH_ERROR_INTERNAL, trigger_unbond_info->addr);
923 _bt_send_event(BT_ADAPTER_EVENT,
924 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
926 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
927 } else if (trigger_bond_info) {
928 if (__bt_device_handle_bond_state() != BLUETOOTH_ERROR_NONE) {
929 if (trigger_bond_info) {
930 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
931 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
932 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
937 __bt_free_pairing_info(&trigger_pairing_info);
942 BT_ERR("Unknown status of Bond failed event status [%d]", status);
950 static void __bt_device_event_handler(int event_type, gpointer event_data)
952 int eventcheck = OAL_EVENT_DEVICE_PROPERTIES;
953 BT_INFO("event [%d] Event check = [%d]", event_type, eventcheck);
955 switch (event_type) {
956 case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY: {
957 BT_INFO("BREDR Device Found");
958 __bt_device_remote_device_found_callback(event_data, FALSE);
961 case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE: {
962 BT_INFO("Dual Device Found");
963 __bt_device_remote_device_found_callback(event_data, FALSE);
966 case OAL_EVENT_DEVICE_PROPERTIES: {
967 BT_INFO("Remote Device properties Received");
968 __bt_device_remote_properties_callback((event_dev_properties_t *)event_data);
971 case OAL_EVENT_DEVICE_BONDING_SUCCESS: {
972 BT_INFO("Bonding Success event Received");
973 __bt_device_handle_bond_completion_event((bt_address_t *)event_data);
976 case OAL_EVENT_DEVICE_BONDING_REMOVED: {
977 BT_INFO("Bonding Removed event Received");
978 __bt_device_handle_bond_removal_event((bt_address_t *)event_data);
981 case OAL_EVENT_DEVICE_BONDING_FAILED: {
982 BT_INFO("Bonding Failed event Received");
983 __bt_device_handle_bond_failed_event((event_dev_bond_failed_t*) event_data);
986 case OAL_EVENT_DEVICE_ACL_CONNECTED: {
987 BT_INFO("ACL Connected event Received");
988 event_dev_conn_status_t* param = event_data;
989 __bt_device_acl_state_changed_callback(param, TRUE, 0);
992 case OAL_EVENT_DEVICE_ACL_DISCONNECTED: {
993 BT_INFO("ACL Disconnected event Received");
994 __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 0);
997 case OAL_EVENT_DEVICE_LE_CONNECTED: {
998 BT_INFO("LE Connected event Received");
999 event_dev_conn_status_t* param = event_data;
1000 __bt_device_acl_state_changed_callback(param, TRUE, 1);
1003 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
1004 BT_INFO("LE Disconnected event Received");
1005 __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 1);
1008 case OAL_EVENT_DEVICE_PIN_REQUEST: {
1009 BT_INFO("PIN Request Received");
1010 __bt_device_pin_request_callback((remote_device_t*)event_data);
1013 case OAL_EVENT_DEVICE_PASSKEY_ENTRY_REQUEST: {
1014 BT_INFO("Passkey Entry request Received");
1015 __bt_device_ssp_passkey_entry_callback((remote_device_t*)event_data);
1018 case OAL_EVENT_DEVICE_PASSKEY_CONFIRMATION_REQUEST:{
1019 BT_INFO("Passkey Confirmation Request Received");
1020 __bt_device_ssp_passkey_confirmation_callback((event_dev_passkey_t *)event_data);
1023 case OAL_EVENT_DEVICE_PASSKEY_DISPLAY: {
1024 BT_INFO("Passkey Display Request Received");
1025 __bt_device_ssp_passkey_display_callback((event_dev_passkey_t *)event_data);
1028 case OAL_EVENT_DEVICE_SSP_CONSENT_REQUEST: {
1029 BT_INFO("SSP Consent Request Received");
1030 __bt_device_ssp_consent_callback((remote_device_t*)event_data);
1033 case OAL_EVENT_DEVICE_SERVICES: {
1034 BT_INFO("Remote Device Services Received");
1035 __bt_device_services_callback((event_dev_services_t*)event_data);
1038 case OAL_EVENT_DEVICE_AUTHORIZE_REQUEST: {
1039 BT_INFO("Remote Device Authorization Request");
1040 __bt_device_authorization_request_callback((event_dev_authorize_req_t*)event_data);
1043 case OAL_EVENT_DEVICE_TRUSTED: {
1044 BT_INFO("Remote Device Trusted");
1045 __bt_device_trusted_callback(TRUE, (event_dev_trust_t*)event_data);
1048 case OAL_EVENT_DEVICE_UNTRUSTED: {
1049 BT_INFO("Remote Device UnTrusted");
1050 __bt_device_trusted_callback(FALSE, (event_dev_trust_t*)event_data);
1053 case OAL_EVENT_DEVICE_NAME: {
1054 remote_device_t *rem_dev = event_data;
1055 gchar address[BT_ADDRESS_STR_LEN];
1057 _bt_convert_addr_type_to_string(address, rem_dev->address.addr);
1058 BT_INFO("Remote Device name Received");
1059 BT_INFO("Name: %s, Address: %s", rem_dev->name, address);
1061 /* Update local cache */
1062 _bt_update_remote_dev_property(address, DEV_PROP_NAME, (void *)rem_dev->name);
1065 case OAL_EVENT_DEVICE_TRUSTED_PROFILES_CHANGED: {
1066 event_device_trusted_profiles_t *ev = event_data;
1067 char address[BT_ADDRESS_STRING_SIZE];
1071 _bt_convert_addr_type_to_string(address, ev->address.addr);
1072 _bt_send_event(BT_DEVICE_EVENT,
1073 BLUETOOTH_EVENT_SUPPORTED_PROFILE_TRUSTED,
1074 g_variant_new("(isi)", BLUETOOTH_ERROR_NONE,
1075 address, ev->trust_val));
1078 case OAL_EVENT_RSSI_MONITORING_ENABLED: {
1079 event_dev_rssi_info_t *ev = event_data;
1080 char address[BT_ADDRESS_STRING_SIZE];
1085 _bt_convert_addr_type_to_string(address, ev->address.addr);
1086 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1087 address, ev->link_type, TRUE);
1088 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1091 case OAL_EVENT_RSSI_MONITORING_DISABLED: {
1092 event_dev_rssi_info_t *ev = event_data;
1093 char address[BT_ADDRESS_STRING_SIZE];
1098 _bt_convert_addr_type_to_string(address, ev->address.addr);
1099 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1100 address, ev->link_type, FALSE);
1101 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1104 case OAL_EVENT_RSSI_ALERT_RECEIVED: {
1105 event_dev_rssi_info_t *ev = event_data;
1106 char address[BT_ADDRESS_STRING_SIZE];
1111 _bt_convert_addr_type_to_string(address, ev->address.addr);
1112 param = g_variant_new("(isiii)", BLUETOOTH_ERROR_NONE,
1113 address, ev->link_type, ev->alert_type, ev->rssi);
1114 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ALERT, param);
1117 case OAL_EVENT_RAW_RSSI_RECEIVED: {
1118 event_dev_rssi_info_t *ev = event_data;
1119 char address[BT_ADDRESS_STRING_SIZE];
1124 _bt_convert_addr_type_to_string(address, ev->address.addr);
1125 param = g_variant_new("(isii)", BLUETOOTH_ERROR_NONE,
1126 address, ev->link_type, ev->rssi);
1127 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RAW_RSSI, param);
1131 BT_INFO("Unhandled event..");
1135 /* Legacy Pairing event handler */
1136 static void __bt_device_pin_request_callback(remote_device_t* pin_req_event)
1139 char address[BT_ADDRESS_STRING_SIZE];
1140 char pin_code[BLUETOOTH_PIN_CODE_MAX_LENGTH + 1];
1141 bool incoming = false;
1144 _bt_convert_addr_type_to_string(address, pin_req_event->address.addr);
1146 BT_INFO("Address[%s]", address);
1147 BT_INFO("Name[%s]", pin_req_event->name);
1148 BT_INFO("COD[%d]", pin_req_event->cod);
1150 if (trigger_pairing_info) {
1151 /* BTAPI support only one pairing at a time */
1152 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1153 BT_ERR("New PIN request address [%s]", address);
1154 device_reject_pin_request(&pin_req_event->address);
1158 /* If user initiated bonding and auto response is possible, just reply with default 0000*/
1159 if (_bt_is_bonding_device_address(address) == TRUE &&
1160 _bt_agent_is_auto_response(pin_req_event->cod, address, pin_req_event->name)) {
1161 /* Note: Currently even if SYSPOPUP is supported, we use Fixed PIN "0000" for basic pairing
1162 as BT SYSPOPUP is currently not working for PIN KEY entry in Tizen platform. This needs
1163 to be checked and fixed apropriately */
1164 _bt_set_autopair_status_in_bonding_info(TRUE);
1165 device_accept_pin_request(&pin_req_event->address, "0000");
1166 } else if (_bt_agent_is_hid_keyboard(pin_req_event->cod)) {
1167 BT_DBG("Remote Device is HID keyboard Type..");
1168 char str_passkey[BT_PASSKEY_MAX_LENGTH + 1] = { 0 };
1170 if (_bt_agent_generate_passkey(str_passkey,
1171 BT_PASSKEY_MAX_LENGTH) != 0) {
1172 device_reject_pin_request(&pin_req_event->address);
1175 device_accept_pin_request(&pin_req_event->address, str_passkey);
1177 BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1179 if(trigger_bond_info == NULL)
1182 param = g_variant_new("(bsss)", incoming, address, pin_req_event->name, str_passkey);
1183 _bt_send_event(BT_ADAPTER_EVENT,
1184 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1185 BT_DBG("Key board pairing in process");
1186 } else if (BLUETOOTH_ERROR_NONE == __bt_get_device_pin_code(address, pin_code)) {
1187 BT_DBG("Use stored PIN code [%s]", pin_code);
1188 device_accept_pin_request(&pin_req_event->address, pin_code);
1190 if (_bt_is_bonding_device_address(address) == TRUE) {
1191 BT_DBG("Show Pin entry");
1192 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1193 trigger_pairing_info->addr = g_strdup(address);
1194 trigger_pairing_info->is_ssp = FALSE;
1196 BT_DBG("Send BLUETOOTH_EVENT_PIN_REQUEST");
1198 if(trigger_bond_info == NULL)
1201 param = g_variant_new("(bss)", incoming, address, pin_req_event->name);
1202 _bt_send_event(BT_ADAPTER_EVENT,
1203 BLUETOOTH_EVENT_PIN_REQUEST, param);
1208 _bt_agent_release_memory();
1212 /* SSP Pairing event handler */
1213 static void __bt_device_ssp_passkey_entry_callback(remote_device_t* dev_info)
1216 gchar address[BT_ADDRESS_STR_LEN];
1219 bool incoming = false;
1223 _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1225 name = dev_info->name;
1227 BT_INFO("Address[%s]", address);
1228 BT_INFO("Name[%s]", name);
1229 BT_INFO("COD[%d]", dev_info->cod);
1231 if (trigger_pairing_info) {
1232 /* BTAPI support only one pairing at a time */
1233 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1234 BT_ERR("New PIN request address [%s]", address);
1235 device_reject_pin_request(&dev_info->address);
1240 /* Set pairing data */
1241 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1242 trigger_pairing_info->addr = g_strdup(address);
1243 trigger_pairing_info->is_ssp = TRUE;
1245 if(trigger_bond_info == NULL)
1248 param = g_variant_new("(bss)", incoming, p_addr, name);
1249 _bt_send_event(BT_ADAPTER_EVENT,
1250 BLUETOOTH_EVENT_PASSKEY_REQUEST, param);
1254 static void __bt_device_ssp_passkey_confirmation_callback(event_dev_passkey_t *dev_info)
1257 gchar address[BT_ADDRESS_STR_LEN];
1260 char str_passkey[7];
1261 bool incoming = false; /*Stores if bonding request is incoming(true) or outgoing(false) */
1264 _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1266 name = dev_info->device_info.name;
1268 BT_INFO("Address[%s]", address);
1269 BT_INFO("Name[%s]", name);
1270 BT_INFO("COD[%d]", dev_info->device_info.cod);
1272 if (trigger_pairing_info) {
1273 /* BTAPI support only one pairing at a time */
1274 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1275 BT_ERR("New PIN request address [%s]", address);
1276 device_reject_pin_request(&dev_info->device_info.address);
1281 /* Set pairing data */
1282 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1283 trigger_pairing_info->addr = g_strdup(address);
1284 trigger_pairing_info->is_ssp = TRUE;
1286 BT_DBG("Send BLUETOOTH_EVENT_PASSKEY_CONFIRMATION");
1287 snprintf(str_passkey, sizeof(str_passkey), "%.6d", dev_info->pass_key);
1289 /*Storing if bond is incoming or outgoing*/
1290 if(trigger_bond_info == NULL)
1293 param = g_variant_new("(bsss)", incoming, p_addr, name, str_passkey);
1294 _bt_send_event(BT_ADAPTER_EVENT,
1295 BLUETOOTH_EVENT_PASSKEY_CONFIRM_REQUEST, param);
1299 static void __bt_device_ssp_passkey_display_callback(event_dev_passkey_t *dev_info)
1302 gchar address[BT_ADDRESS_STR_LEN];
1305 char str_passkey[7];
1306 bool incoming = false;
1309 _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1311 name = dev_info->device_info.name;
1313 BT_INFO("Address[%s]", address);
1314 BT_INFO("Name[%s]", name);
1315 BT_INFO("COD[%d]", dev_info->device_info.cod);
1317 if (trigger_pairing_info) {
1318 /* BTAPI support only one pairing at a time */
1319 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1320 BT_ERR("New PIN request address [%s]", address);
1321 device_reject_pin_request(&dev_info->device_info.address);
1326 /* Set pairing data */
1327 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1328 trigger_pairing_info->addr = g_strdup(address);
1329 trigger_pairing_info->is_ssp = TRUE;
1331 BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1332 snprintf(str_passkey, sizeof(str_passkey), "%06d", dev_info->pass_key);
1334 if(trigger_bond_info == NULL)
1337 param = g_variant_new("(bsss)", incoming, p_addr, name, str_passkey);
1339 if (passkey_watcher) {
1340 BT_INFO("Send passkey to %s", passkey_watcher);
1341 _bt_send_event_to_dest(passkey_watcher, BT_ADAPTER_EVENT,
1342 BLUETOOTH_EVENT_PASSKEY_NOTIFICATION, param);
1344 _bt_send_event(BT_ADAPTER_EVENT,
1345 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1350 static void __bt_device_ssp_consent_callback(remote_device_t* dev_info)
1352 gchar address[BT_ADDRESS_STR_LEN];
1359 _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1360 name = dev_info->name;
1361 cod = dev_info->cod;
1363 BT_INFO("Address[%s]", address);
1364 BT_INFO("Name[%s]", name);
1365 BT_INFO("COD[%d]", cod);
1367 if (trigger_pairing_info) {
1368 /* BTAPI support only one pairing at a time */
1369 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1370 BT_ERR("New PIN request address [%s]", address);
1371 device_reject_pin_request(&dev_info->address);
1376 /* Set pairing data */
1377 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1378 trigger_pairing_info->addr = g_strdup(address);
1379 trigger_pairing_info->is_ssp = TRUE;
1381 local_major = ((cod >> 8) & 0x001f);
1382 local_minor = (cod & 0x00fc);
1383 BT_DBG("SSP_CONSENT: Major type=[0x%x] and Minor type=[0x%x]", local_major, local_minor);
1385 /*TODO: BLUETOOTH_EVENT_SSP_CONSENT_REQUEST to be handled in Tizen */
1389 static int __bt_oal_status_to_bt_error(int oal_status)
1393 switch (oal_status) {
1394 case OAL_STATUS_SUCCESS:
1395 ret = BLUETOOTH_ERROR_NONE;
1397 case OAL_STATUS_CONN_TIMEOUT:
1398 case OAL_STATUS_LINK_LOSS:
1399 BT_INFO("Connection Timeout");
1400 ret = BLUETOOTH_ERROR_CONNECTION_TIMEOUT;
1403 case OAL_STATUS_CONN_TERM_LOCAL_HOST:
1404 ret = BLUETOOTH_ERROR_LOCAL_HOST_TERM;
1406 case OAL_STATUS_CONN_TERM_RMT_HOST:
1407 ret = BLUETOOTH_ERROR_REMOTE_USER_TERM;
1410 case OAL_STATUS_INTERNAL_ERROR:
1411 ret = BLUETOOTH_ERROR_INTERNAL;
1414 ret = BLUETOOTH_ERROR_INTERNAL;
1420 static void __bt_device_acl_state_changed_callback(event_dev_conn_status_t *acl_event,
1421 gboolean connected, unsigned char type)
1423 gchar address[BT_ADDRESS_STR_LEN];
1424 int result = BLUETOOTH_ERROR_NONE;
1425 GVariant *param = NULL;
1426 bt_device_conn_info_t conn_info;
1429 _bt_convert_addr_type_to_string(address, acl_event->address.addr);
1431 _bt_logging_connection(connected, type);
1433 result = __bt_oal_status_to_bt_error(acl_event->status);
1434 BT_INFO("Result [0x%x]", result);
1437 param = g_variant_new("(isy)", result, address, type);
1438 _bt_send_event(BT_DEVICE_EVENT,
1439 BLUETOOTH_EVENT_DEVICE_CONNECTED,
1442 param = g_variant_new("(isyi)", result, address, type, acl_event->rssi);
1443 _bt_send_event(BT_DEVICE_EVENT,
1444 BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1448 conn_info.connected = connected;
1449 conn_info.type = type;
1450 /* Update local cache */
1451 _bt_update_remote_dev_property(address, DEV_PROP_CONNECTED, (void *)&conn_info);
1453 #ifdef TIZEN_GATT_CLIENT
1454 /*handle LE connected device info*/
1456 BT_DBG("handle LE connected device info");
1457 _bt_handle_le_connected_dev_info(address, connected);
1464 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble)
1466 bt_remote_dev_info_t *dev_info = NULL;
1467 int result = BLUETOOTH_ERROR_NONE;
1468 GVariant *param = NULL;
1469 GVariant *uuids = NULL;
1470 GVariant *manufacturer_data = NULL;
1471 GVariantBuilder *builder = NULL;
1475 ret_if(_bt_is_discovering() == FALSE);
1476 ret_if(event_data == NULL);
1478 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
1481 event_ble_dev_found_t * oal_ble_dev = event_data;
1482 BT_INFO("Device type [%d]", oal_ble_dev->device_info.type);
1484 _bt_copy_remote_dev(dev_info, &oal_ble_dev->device_info);
1486 dev_info->manufacturer_data_len = oal_ble_dev->adv_len;
1487 if (dev_info->manufacturer_data_len)
1488 dev_info->manufacturer_data = g_memdup(oal_ble_dev->adv_data,
1489 dev_info->manufacturer_data_len);
1491 dev_info->manufacturer_data = NULL;
1492 BT_DBG("----Advertising Data Length: %d", dev_info->manufacturer_data_len);
1494 event_dev_found_t * oal_dev = event_data;
1495 _bt_copy_remote_dev(dev_info, &oal_dev->device_info);
1498 /* If Remote device name is NULL or still RNR is not done then display address as name. */
1499 if (dev_info->name == NULL)
1500 dev_info->name = g_strdup(dev_info->address);
1501 BT_DBG("Name %s", dev_info->name);
1503 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1504 for (i = 0; i < dev_info->uuid_count; i++)
1505 g_variant_builder_add(builder, "s", dev_info->uuids[i]);
1507 uuids = g_variant_new("as", builder);
1508 g_variant_builder_unref(builder);
1510 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1511 dev_info->manufacturer_data,
1512 dev_info->manufacturer_data_len,
1516 param = g_variant_new("(isunsbub@asn@ay)", result,
1522 dev_info->connected,
1525 dev_info->manufacturer_data_len,
1528 _bt_send_event(BT_ADAPTER_EVENT,
1529 BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
1532 _bt_free_remote_dev(dev_info);
1536 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info)
1538 gchar address[BT_ADDRESS_STR_LEN];
1539 int result = BLUETOOTH_ERROR_NONE;
1540 GVariant *param = NULL;
1544 _bt_convert_addr_type_to_string(address, info->address.addr);
1546 /* Update local cache */
1547 _bt_update_remote_dev_property(address, DEV_PROP_TRUST, (void *)&trusted);
1549 param = g_variant_new("(is)", result, address);
1550 event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1551 BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1552 /* Send event to application */
1553 _bt_send_event(BT_DEVICE_EVENT,
1560 static void __bt_free_pairing_info(bt_pairing_data_t **p_info)
1563 bt_pairing_data_t * info = *p_info;
1574 static void __bt_free_bond_info(uint8_t type)
1579 case BT_DEVICE_BOND_INFO:
1580 if (!trigger_bond_info)
1583 if (trigger_bond_info->addr)
1584 g_free(trigger_bond_info->addr);
1585 if (trigger_bond_info->dev_addr)
1586 g_free(trigger_bond_info->dev_addr);
1587 if (trigger_bond_info->dev_info)
1588 _bt_free_remote_dev(trigger_bond_info->dev_info);
1589 g_free(trigger_bond_info);
1590 trigger_bond_info = NULL;
1592 case BT_DEVICE_INCOMING_BOND_INFO:
1593 if (!incoming_bond_info)
1596 if (incoming_bond_info->dev_info)
1597 _bt_free_remote_dev(incoming_bond_info->dev_info);
1598 g_free(incoming_bond_info);
1599 incoming_bond_info = NULL;
1601 case BT_DEVICE_UNBOND_INFO:
1602 if (!trigger_unbond_info)
1605 if (trigger_unbond_info->addr)
1606 g_free(trigger_unbond_info->addr);
1607 if (trigger_unbond_info->dev_addr)
1608 g_free(trigger_unbond_info->dev_addr);
1609 if (trigger_unbond_info->dev_info)
1610 _bt_free_remote_dev(trigger_unbond_info->dev_info);
1611 g_free(trigger_unbond_info);
1612 trigger_unbond_info = NULL;
1618 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info)
1620 bt_service_search_info_data_t * info = *p_info;
1627 if (info->dev_addr) {
1628 g_free(info->dev_addr);
1629 info->dev_addr = NULL;
1632 if (info->dev_info) {
1633 _bt_free_remote_dev(info->dev_info);
1634 info->dev_info = NULL;
1642 static int __bt_device_handle_bond_state(void)
1644 BT_INFO("Current Bond state: %d", bt_device_bond_state);
1645 int ret = OAL_STATUS_INTERNAL_ERROR;
1647 switch (bt_device_bond_state) {
1648 case BT_DEVICE_BOND_STATE_CANCEL_DISCOVERY:
1649 /*TODO:Bonding during discovery: Unhandled!!*/
1650 BT_INFO("Bonding during discovery: Unhandled!!");
1652 case BT_DEVICE_BOND_STATE_DISCOVERY_CANCELLED:
1653 /*TODO:Bonding during discovery: Unhandled!!*/
1654 BT_INFO("Bonding during discovery: Unhandled!!");
1656 case BT_DEVICE_BOND_STATE_REMOVE_BONDING:
1657 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVED_BONDING;
1658 ret = device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
1659 if (ret != OAL_STATUS_SUCCESS)
1660 ret = __bt_device_handle_bond_state();
1662 case BT_DEVICE_BOND_STATE_REMOVED_BONDING:
1663 bt_device_bond_state = BT_DEVICE_BOND_STATE_NONE;
1664 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
1665 /* Bonding procedure was started but unfortunately could not complete.
1666 Basically removed bonding was success, but create bond request could not proceed
1667 So lets cleanup the context */
1668 if (ret != OAL_STATUS_SUCCESS) {
1669 BT_ERR("Create Bond procedure could not suceed");
1670 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1671 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1672 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1673 __bt_free_pairing_info(&trigger_pairing_info);
1676 case BT_DEVICE_BOND_STATE_NONE:
1677 BT_INFO("Create Bond failed!!");
1678 if (trigger_bond_info) {
1679 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1680 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1681 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1682 __bt_free_pairing_info(&trigger_pairing_info);
1689 if (ret != OAL_STATUS_SUCCESS)
1690 return BLUETOOTH_ERROR_INTERNAL;
1692 return BLUETOOTH_ERROR_NONE;
1695 int _bt_device_get_bonded_device_info(bluetooth_device_address_t *addr)
1698 bt_address_t bd_addr;
1702 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
1704 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
1705 result = device_query_attributes(&bd_addr);
1706 if (result != OAL_STATUS_SUCCESS) {
1707 BT_ERR("device_query_attributes error: [%d]", result);
1708 return BLUETOOTH_ERROR_INTERNAL;
1712 return BLUETOOTH_ERROR_NONE;
1715 int _bt_set_alias(bluetooth_device_address_t *device_address, const char *alias)
1718 char address[BT_ADDRESS_STRING_SIZE];
1721 BT_CHECK_PARAMETER(alias, return);
1723 ret = device_set_alias((bt_address_t *)device_address, (char *)alias);
1724 if (ret != OAL_STATUS_SUCCESS) {
1725 BT_ERR("device_set_alias: %d", ret);
1726 return BLUETOOTH_ERROR_INTERNAL;
1729 /* Update local cache */
1730 _bt_convert_addr_type_to_string(address, device_address->addr);
1731 _bt_update_remote_dev_property(address, DEV_PROP_ALIAS, (void *)alias);
1734 return BLUETOOTH_ERROR_NONE;
1737 int _bt_bond_device(bluetooth_device_address_t *device_address,
1738 unsigned short conn_type, GArray **out_param1)
1740 int result = BLUETOOTH_ERROR_NONE;
1741 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1742 bluetooth_device_info_t dev_info;
1745 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1747 /* If bonding or discovery already going on */
1748 if (trigger_bond_info || _bt_is_discovering()) {
1749 BT_ERR("Device is buzy, bonding can not proceed now..");
1750 result = BLUETOOTH_ERROR_DEVICE_BUSY;
1754 /*TODO: If unbonding with same device going on */
1755 _bt_convert_addr_type_to_string(address, device_address->addr);
1757 trigger_bond_info = g_malloc0(sizeof(bt_bond_data_t));
1758 trigger_bond_info->addr = g_strdup(address);
1759 trigger_bond_info->conn_type = conn_type;
1760 trigger_bond_info->is_device_creating = TRUE;
1761 trigger_bond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1762 trigger_bond_info->dev_info = NULL;
1764 /* Ready to initiate bonding */
1766 /* In Tizen, we will first remove bond and then attempt to create bond to keep
1767 consistency with bluedroid. Even if remove bond fails due to device not already
1768 bonded, then straight away create bond is triggered. This is because, remove bond
1769 is handled differently in bluedroid and bluez. In Bluez, if device is
1770 already removed, remove bond call fails.
1771 However in bluedroid, remove bond on already removed device returns success. So we will
1772 handle the cases transparently*/
1773 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVE_BONDING;
1774 bond_retry_count = 0;
1775 result = __bt_device_handle_bond_state();
1777 if (result != BLUETOOTH_ERROR_NONE)
1784 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1785 memcpy(dev_info.device_address.addr, device_address->addr,
1786 BLUETOOTH_ADDRESS_LENGTH);
1788 g_array_append_vals(*out_param1, &dev_info,
1789 sizeof(bluetooth_device_info_t));
1790 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1796 int _bt_unbond_device(bluetooth_device_address_t *device_address,
1797 GArray **out_param1)
1799 int result = OAL_STATUS_SUCCESS;
1800 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1801 bluetooth_device_info_t dev_info;
1804 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1806 _bt_convert_addr_type_to_string(address, device_address->addr);
1808 trigger_unbond_info = g_malloc0(sizeof(bt_bond_data_t));
1809 trigger_unbond_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1810 trigger_unbond_info->addr = g_strdup(address);
1811 trigger_unbond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1813 /* Check if Bonding is already going on, we should not abruptly remove bonding*/
1814 if (trigger_bond_info && strncmp(trigger_bond_info->addr, trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE) == 0) {
1815 BT_ERR("Bonding with same device already ongoing");
1816 result = BLUETOOTH_ERROR_PERMISSION_DEINED;
1820 result = device_destroy_bond((bt_address_t *)device_address);
1821 if (result != OAL_STATUS_SUCCESS)
1827 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1828 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1829 trigger_unbond_info->addr);
1831 g_array_append_vals(*out_param1, &dev_info,
1832 sizeof(bluetooth_device_info_t));
1833 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
1838 int _bt_cancel_bonding(void)
1840 int result = OAL_STATUS_SUCCESS;
1843 retv_if(trigger_bond_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1845 result = device_stop_bond((bt_address_t *)trigger_bond_info->dev_addr);
1847 if (result == OAL_STATUS_SUCCESS)
1848 trigger_bond_info->is_cancelled_by_user = TRUE;
1853 int _bt_passkey_reply(const char *passkey, gboolean cnfm_reply)
1855 bluetooth_device_address_t device_address;
1856 int ret = OAL_STATUS_SUCCESS;
1857 BT_INFO("reply: %d", cnfm_reply);
1859 retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1860 retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1862 _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1864 if (trigger_pairing_info->is_ssp) {
1866 ret = device_accept_passkey_entry((bt_address_t *)&device_address, atoi(passkey));
1868 ret = device_reject_passkey_entry((bt_address_t *)&device_address);
1869 trigger_pairing_info->is_ssp = FALSE;
1872 ret = device_accept_pin_request((bt_address_t *)&device_address, passkey);
1874 ret = device_reject_pin_request((bt_address_t *)&device_address);
1877 __bt_free_pairing_info(&trigger_pairing_info);
1879 if (ret != OAL_STATUS_SUCCESS) {
1880 BT_ERR("_bt_device_handle_passkey_reply: err [%d]", ret);
1881 return BLUETOOTH_ERROR_INTERNAL;
1885 return BLUETOOTH_ERROR_NONE;
1888 int _bt_passkey_confirmation_reply(gboolean cnfm_reply)
1890 BT_INFO("BT_PASSKEY_CONFIRMATION_REPLY");
1891 bluetooth_device_address_t device_address;
1892 int ret = OAL_STATUS_SUCCESS;
1893 BT_INFO("reply: %d", cnfm_reply);
1895 retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1896 retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1898 _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1900 ret = device_reply_passkey_confirmation((bt_address_t *)&device_address, cnfm_reply);
1902 __bt_free_pairing_info(&trigger_pairing_info);
1903 if (ret != OAL_STATUS_SUCCESS) {
1904 BT_ERR("_bt_device_handle_passkey_confirmation_reply: err [%d]", ret);
1905 return BLUETOOTH_ERROR_INTERNAL;
1909 return BLUETOOTH_ERROR_NONE;
1912 gboolean _bt_device_is_pairing(void)
1914 return (trigger_pairing_info) ? TRUE : FALSE;
1917 gboolean _bt_device_is_bonding(void)
1919 return (trigger_bond_info) ? TRUE : FALSE;
1922 gboolean _bt_is_bonding_device_address(const char *address)
1924 if (trigger_bond_info == NULL || trigger_bond_info->addr == NULL)
1927 if (g_strcmp0(trigger_bond_info->addr, address) == 0) {
1928 BT_DBG("[%s] is bonding device", address);
1932 BT_DBG("[%s] is NOT bonding device", address);
1936 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
1938 ret_if(trigger_bond_info == NULL);
1939 trigger_bond_info->is_autopair = is_autopair;
1942 int _bt_search_device(bluetooth_device_address_t *device_address)
1944 int result = OAL_STATUS_SUCCESS;
1947 BT_CHECK_PARAMETER(device_address, return);
1949 if (trigger_bond_info) {
1950 BT_ERR("Bonding in progress");
1951 return BLUETOOTH_ERROR_DEVICE_BUSY;
1954 if (service_search_info) {
1955 BT_ERR("Service searching in progress");
1956 return BLUETOOTH_ERROR_DEVICE_BUSY;
1959 /* allocate user data so that it can be retrieved in callback */
1960 service_search_info = g_malloc0(sizeof(bt_service_search_info_data_t));
1961 service_search_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1962 service_search_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1964 _bt_convert_addr_type_to_string(service_search_info->addr,
1965 device_address->addr);
1967 result = device_query_services((bt_address_t *)device_address);
1969 if (result != OAL_STATUS_SUCCESS) {
1970 BT_ERR("Device Service Search Failed..: %d", result);
1971 __bt_free_service_search_info(&service_search_info);
1972 return BLUETOOTH_ERROR_INTERNAL;
1974 return BLUETOOTH_ERROR_NONE;
1977 int _bt_cancel_search_device(void)
1979 int ret = OAL_STATUS_SUCCESS;
1980 retv_if(service_search_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1982 ret = device_stop_query_sevices((bt_address_t *)service_search_info->dev_addr);
1984 if (ret != OAL_STATUS_SUCCESS) {
1985 BT_ERR("SDP Cancel request failed [%d]", ret);
1986 return BLUETOOTH_ERROR_INTERNAL;
1989 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_CANCEL_BY_USER, BT_SEARCH_SERVICE,
1990 service_search_info->addr, BT_ADDRESS_STRING_SIZE);
1992 __bt_free_service_search_info(&service_search_info);
1994 return BLUETOOTH_ERROR_NONE;
1998 int _bt_set_authorization(bluetooth_device_address_t *device_address,
2001 int ret = OAL_STATUS_SUCCESS;
2004 BT_CHECK_PARAMETER(device_address, return);
2005 BT_INFO("Device to be Trusted? [%d]", authorize);
2007 ret = device_set_authorized((bt_address_t*)device_address, authorize);
2008 if (ret != OAL_STATUS_SUCCESS) {
2009 BT_ERR("device_set_authorized: %d", ret);
2010 return BLUETOOTH_ERROR_INTERNAL;
2013 return BLUETOOTH_ERROR_NONE;
2016 gboolean _bt_is_device_connected(bluetooth_device_address_t *device_address, int svc_type)
2018 gboolean is_connected;
2019 oal_service_t svc_id;
2021 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
2024 * TODO: While adding support for new profiles, need to add more
2025 * <svc_type, svc_id> mapping here.
2028 case BT_PROFILE_CONN_HID:
2029 svc_id = HID_SERVICE_ID;
2031 case BT_PROFILE_CONN_A2DP:
2032 svc_id = A2DP_SERVICE_ID; /* Remote is A2DP Sink */
2034 case BT_PROFILE_CONN_A2DP_SINK:
2035 svc_id = A2DP_SRC_SERVICE_ID; /* Remote is A2DP Source*/
2037 case BT_PROFILE_CONN_HSP:
2038 svc_id = HFP_HS_SERVICE_ID; /* Remote is HFP HF Unit */
2040 #ifdef TIZEN_GATT_CLIENT
2041 case BT_PROFILE_CONN_GATT:
2042 return _bt_is_remote_gatt_device_connected(device_address); /* Remote is GATT client or Server */
2045 BT_DBG("Unknown svc_type: %d", svc_type);
2049 is_connected = device_get_svc_conn_state((bt_address_t*)device_address, svc_id);
2051 BT_DBG("svc_type: %d, is_connected: %s",
2052 svc_type, is_connected ? "TRUE" : "FALSE");
2054 return is_connected;
2057 int _bt_rfcomm_reply_conn_authorization(char *address, gboolean reply)
2059 bt_address_t bd_addr;
2064 retv_if(NULL == address, BLUETOOTH_ERROR_INVALID_PARAM);
2065 _bt_convert_addr_string_to_type(bd_addr.addr, address);
2066 res = device_reply_auth_request(&bd_addr, 0, reply, FALSE);
2067 if (res != OAL_STATUS_SUCCESS) {
2068 BT_ERR("authorize_response: %d", res);
2069 return BLUETOOTH_ERROR_INTERNAL;
2073 return BLUETOOTH_ERROR_NONE;
2076 int _bt_enable_rssi(bluetooth_device_address_t *addr, int link_type,
2077 int low_threshold, int in_range_threshold, int high_threshold)
2080 bt_address_t bd_addr;
2084 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2086 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2087 result = device_enable_rssi_monitoring(&bd_addr, link_type,
2088 low_threshold, in_range_threshold, high_threshold);
2089 if (result != OAL_STATUS_SUCCESS) {
2090 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2091 return BLUETOOTH_ERROR_INTERNAL;
2095 return BLUETOOTH_ERROR_NONE;
2098 int _bt_get_rssi_strength(bluetooth_device_address_t *addr, int link_type)
2101 bt_address_t bd_addr;
2105 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2107 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2108 result = device_get_connected_link_rssi_strength(&bd_addr, link_type);
2109 if (result != OAL_STATUS_SUCCESS) {
2110 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2111 return BLUETOOTH_ERROR_INTERNAL;
2115 return BLUETOOTH_ERROR_NONE;
2118 int _bt_set_passkey_notification(const char *sender, gboolean enable)
2122 BT_INFO("Set passkey notification(sender:%s, %s)",
2123 sender, enable ? "Enable" : "Disable");
2125 result = device_enable_gap_auth_notifications(OAL_PASSKEY_DISPLAY, enable);
2126 if (OAL_STATUS_SUCCESS != result) {
2127 BT_ERR("device_enable_gap_auth_notifications error: [%d]", result);
2128 return BLUETOOTH_ERROR_INTERNAL;
2131 g_free(passkey_watcher);
2133 passkey_watcher = g_strdup(sender);
2135 passkey_watcher = NULL;
2137 return BLUETOOTH_ERROR_NONE;
2140 static int __bt_get_device_pin_code(const char *address, char *pin_code)
2144 BT_CHECK_PARAMETER(address, return);
2145 BT_CHECK_PARAMETER(pin_code, return);
2147 for (l = pin_info_list; l != NULL; l = l->next) {
2148 bt_pin_code_info_t *pin_info = l->data;
2150 if (!pin_info || !pin_info->address)
2153 if (g_strcmp0(pin_info->address, address) == 0) {
2154 g_strlcpy(pin_code, pin_info->pin_code,
2155 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2156 return BLUETOOTH_ERROR_NONE;
2160 return BLUETOOTH_ERROR_NOT_FOUND;
2163 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2164 bluetooth_device_pin_code_t *pin_code)
2166 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2167 bt_pin_code_info_t *pin_info = NULL;
2170 BT_CHECK_PARAMETER(device_address, return);
2171 BT_CHECK_PARAMETER(pin_code, return);
2172 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2173 BLUETOOTH_ERROR_NO_RESOURCES);
2175 _bt_convert_addr_type_to_string(address, device_address->addr);
2177 for (l = pin_info_list; l != NULL; l = l->next) {
2180 if (!pin_info || !pin_info->address)
2183 if (g_strcmp0(pin_info->address, address) == 0) {
2184 g_free(pin_info->pin_code);
2185 pin_info->pin_code = g_strdup(pin_code->pin_code);
2186 return BLUETOOTH_ERROR_NONE;
2190 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2191 pin_info->address = g_strdup(address);
2192 pin_info->pin_code = g_strdup(pin_code->pin_code);
2193 pin_info_list = g_slist_append(pin_info_list, pin_info);
2195 return BLUETOOTH_ERROR_NONE;
2198 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2200 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2201 bt_pin_code_info_t *pin_info = NULL;
2206 BT_CHECK_PARAMETER(device_address, return);
2208 _bt_convert_addr_type_to_string(address, device_address->addr);
2210 for (l = pin_info_list; l != NULL; l = l->next) {
2213 if (!pin_info || !pin_info->address)
2216 if (g_strcmp0(pin_info->address, address) == 0) {
2217 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2218 g_free(pin_info->address);
2219 g_free(pin_info->pin_code);
2226 return BLUETOOTH_ERROR_NONE;
2229 #ifdef TIZEN_GATT_CLIENT
2230 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2233 bt_connected_le_dev_t *dev;
2238 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2241 if (g_strcmp0(dev->address, address) == 0)
2247 static void __bt_le_conn_param_free(void *data)
2249 bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2251 BT_DBG("%s", param->sender);
2252 g_free(param->sender);
2256 static void _bt_add_le_connected_dev_info(const char *address)
2258 bt_connected_le_dev_t *dev = NULL;
2263 dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2264 dev->address = g_strdup(address);
2266 le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2271 static void _bt_remove_le_connected_dev_info(const char *address)
2273 bt_connected_le_dev_t *dev = NULL;
2278 dev = __bt_get_le_connected_dev_info(address);
2282 g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2283 le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2284 g_free(dev->address);
2291 static void _bt_handle_le_connected_dev_info(const char *address, gboolean connected)
2296 _bt_add_le_connected_dev_info(address);
2298 _bt_remove_le_connected_dev_info(address);
2301 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2304 bt_le_conn_param_t *param = NULL;
2306 if (!dev || !sender)
2309 for (l = dev->senders; l; l = g_slist_next(l)) {
2311 if (g_strcmp0(param->sender, sender) == 0)
2318 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2320 bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2321 bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2323 return parama->key > paramb->key;
2327 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2328 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2330 bt_connected_le_dev_t *dev = NULL;
2331 bt_le_conn_param_t *param = NULL;
2332 bt_le_conn_param_t *data = NULL;
2336 if (!address || !sender)
2337 return BLUETOOTH_ERROR_INVALID_PARAM;
2339 dev = __bt_get_le_connected_dev_info(address);
2341 return BLUETOOTH_ERROR_INTERNAL;
2343 param = __bt_get_le_conn_param_info(dev, sender);
2345 data = g_malloc0(sizeof(bt_le_conn_param_t));
2346 data->sender = g_strdup(sender);
2347 data->interval_min = interval_min;
2348 data->interval_max = interval_max;
2349 data->latency = latency;
2350 data->time_out = time_out;
2351 data->key = interval_min + (interval_max - interval_min)/2;
2353 if (param == NULL) {
2354 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2355 dev->senders = g_slist_append(dev->senders, data);
2357 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2358 dev->senders = g_slist_remove(dev->senders, param);
2359 g_free(param->sender);
2361 dev->senders = g_slist_append(dev->senders, data);
2364 /* Sorting. First element have the minimum interval */
2365 dev->senders = g_slist_sort(dev->senders,
2366 (GCompareFunc)__bt_compare_le_conn_param_key);
2368 return BLUETOOTH_ERROR_NONE;
2372 static int __bt_le_set_conn_parameter(const char *address,
2373 float interval_min, float interval_max,
2374 guint16 latency, guint16 time_out)
2376 bt_address_t dev_addr = { {0} };
2377 guint32 min, max, to;
2379 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2380 interval_min, interval_max, latency, time_out);
2382 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2383 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2384 to = time_out / BT_LE_CONN_TO_SPLIT;
2386 BT_INFO("updating: Min interval: %d, Max interval: %d, Latency: %d, Supervision timeout: %d",
2387 min, max, latency, to);
2389 _bt_convert_addr_string_to_type(dev_addr.addr, address);
2391 return gattc_conn_param_update(&dev_addr, min, max, latency, to);
2394 int _bt_remove_le_conn_param_info(const char *address, const char *sender)
2396 bt_connected_le_dev_t *dev = NULL;
2397 bt_le_conn_param_t *param = NULL;
2399 if (!address || !sender)
2400 return BLUETOOTH_ERROR_INVALID_PARAM;
2402 dev = __bt_get_le_connected_dev_info(address);
2404 return BLUETOOTH_ERROR_INTERNAL;
2406 param = __bt_get_le_conn_param_info(dev, sender);
2408 BT_DBG("Remove param %s %s ", address, sender);
2409 dev->senders = g_slist_remove(dev->senders, param);
2410 g_free(param->sender);
2414 return BLUETOOTH_ERROR_NONE;
2417 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2418 bluetooth_le_connection_param_t *param)
2421 return BLUETOOTH_ERROR_INVALID_PARAM;
2423 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2426 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2427 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2428 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2429 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2430 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2433 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2434 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2435 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2436 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2437 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2440 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2441 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2442 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2443 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2444 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2448 BT_ERR("Unhandled mode : %d", mode);
2452 return BLUETOOTH_ERROR_NONE;
2455 int _bt_le_connection_update(const char *sender,
2456 unsigned char *device_address,
2457 float interval_min, float interval_max,
2458 guint16 latency, guint16 time_out)
2460 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2461 guint32 min_supervision_to;
2462 bt_connected_le_dev_t *dev = NULL;
2463 bt_le_conn_param_t *param = NULL;
2464 int ret = BLUETOOTH_ERROR_NONE;
2466 BT_CHECK_PARAMETER(device_address, return);
2468 BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2469 sender, interval_min, interval_max, latency, time_out);
2471 if (interval_min > interval_max ||
2472 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2473 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2474 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2478 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2479 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2480 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2484 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2485 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2490 * The Supervision_Timeout in milliseconds shall be larger than
2491 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2492 * where Conn_Interval_Max is given in milliseconds.
2495 min_supervision_to = (1 + latency) * interval_max * 2;
2496 if (time_out <= min_supervision_to) {
2497 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2501 _bt_convert_addr_type_to_string(address, device_address);
2502 BT_DBG("Remote device address: %s", address);
2504 _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2506 dev = __bt_get_le_connected_dev_info(address);
2508 BT_DBG("device not found in the list");
2509 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2513 if (g_slist_length(dev->senders) == 1)
2516 param = dev->senders->data;
2518 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2520 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2521 BT_DBG("Skip due to same interval");
2525 interval_min = param->interval_min;
2526 interval_max = param->interval_max;
2530 ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2532 if (ret != OAL_STATUS_SUCCESS) {
2533 _bt_remove_le_conn_param_info(address, sender);
2534 BT_DBG("fail to update the LE connection parameter");
2535 ret = BLUETOOTH_ERROR_INTERNAL;
2539 BT_DBG("updated LE connection parameter");
2540 dev->interval_min = interval_min;
2541 dev->interval_max = interval_max;
2543 return BLUETOOTH_ERROR_NONE;
2548 int _bt_disconnect_device(bluetooth_device_address_t *device_address)
2550 int result = OAL_STATUS_SUCCESS;
2554 retv_if(!device_address, BLUETOOTH_ERROR_INVALID_PARAM);
2556 result = device_disconnect((bt_address_t *)device_address);
2557 if (result != OAL_STATUS_SUCCESS) {
2558 BT_DBG("Failed to disconnect device");
2559 return BLUETOOTH_ERROR_INTERNAL;
2563 return BLUETOOTH_ERROR_NONE;