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 /* Bonding Info structure */
67 unsigned short conn_type;
68 gboolean is_cancelled_by_user;
69 gboolean is_device_creating;
70 bluetooth_device_address_t *dev_addr;
71 bt_remote_dev_info_t *dev_info;
74 /* Searching Info structure */
78 gboolean is_cancelled_by_user;
79 bluetooth_device_address_t *dev_addr;
80 bt_remote_dev_info_t *dev_info;
81 } bt_service_search_info_data_t;
83 /* Pairing Info structure */
91 char addr[BT_ADDRESS_STRING_SIZE];
92 bt_remote_dev_info_t *dev_info;
93 } bt_incoming_bond_data_t;
95 /* Bonding and Pairing Informations */
96 bt_bond_data_t *trigger_bond_info;
97 bt_bond_data_t *trigger_unbond_info;
98 bt_pairing_data_t *trigger_pairing_info;
99 bt_service_search_info_data_t *service_search_info;
101 bt_incoming_bond_data_t *incoming_bond_info;
104 BT_DEVICE_BOND_STATE_NONE,
105 BT_DEVICE_BOND_STATE_CANCEL_DISCOVERY,
106 BT_DEVICE_BOND_STATE_DISCOVERY_CANCELLED,
107 BT_DEVICE_BOND_STATE_REMOVE_BONDING,
108 BT_DEVICE_BOND_STATE_REMOVED_BONDING,
109 BT_DEVICE_BOND_STATE_STARTED,
110 BT_DEVICE_BOND_STATE_WAIT_PROP,
111 BT_DEVICE_BOND_STATE_WAIT_DID
116 BT_DEVICE_INCOMING_BOND_INFO,
117 BT_DEVICE_UNBOND_INFO
120 /* BT device bond state variable */
121 static bt_bond_state_e bt_device_bond_state;
122 static int bond_retry_count;
124 static char *passkey_watcher;
125 static GSList *pin_info_list = NULL;
127 #ifdef TIZEN_GATT_CLIENT
133 } bt_connected_le_dev_t;
142 } bt_le_conn_param_t;
144 static GSList *le_connected_dev_list = NULL;
146 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
147 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
148 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
149 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
150 #define BT_LE_CONN_SLAVE_LATENCY_MAX 499
151 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
152 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
154 static void _bt_handle_le_connected_dev_info(const char *address, gboolean connected);
157 /* Forward declaration */
158 static void __bt_device_event_handler(int event_type, gpointer event_data);
159 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble);
162 static int __bt_device_handle_bond_state(void);
163 static void __bt_free_bond_info(uint8_t type);
164 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info);
165 static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr);
166 static void __bt_device_handle_bond_removal_event(bt_address_t *bd_addr);
167 static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_fail_event);
168 static void __bt_handle_ongoing_bond(bt_remote_dev_info_t *remote_dev_info, gboolean incoming_bond);
169 static void __bt_device_conn_state_changed_callback(event_dev_conn_status_t *acl_event,
170 gboolean connected, unsigned char type);
171 static void __bt_free_pairing_info(bt_pairing_data_t **p_info);
173 static void __bt_device_ssp_consent_callback(remote_device_t* dev_info);
174 static void __bt_device_pin_request_callback(remote_device_t* pin_req_event);
175 static void __bt_device_ssp_passkey_display_callback(event_dev_passkey_t *dev_info);
176 static void __bt_device_ssp_passkey_confirmation_callback(event_dev_passkey_t *dev_info);
177 static void __bt_device_ssp_passkey_entry_callback(remote_device_t* dev_info);
178 static void __bt_device_authorization_request_callback(event_dev_authorize_req_t* auth_event);
180 static void __bt_device_services_callback(event_dev_services_t* uuid_list);
181 static void __bt_handle_ongoing_device_service_search(bt_remote_dev_info_t *remote_dev_info);
183 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info);
185 static int __bt_get_device_pin_code(const char *address, char *pin_code);
187 gboolean _bt_is_device_creating(void)
189 if (!trigger_bond_info)
191 return trigger_bond_info->is_device_creating;
194 void _bt_device_state_handle_callback_set_request(void)
196 _bt_service_register_event_handler_callback(
197 BT_DEVICE_MODULE, __bt_device_event_handler);
200 void __bt_device_handle_pending_requests(int result, int service_function,
201 void *user_data, unsigned int size)
205 invocation_info_t *req_info = NULL;
209 /* Get method invocation context */
210 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
212 if (req_info == NULL || req_info->service_function != service_function)
215 switch (service_function) {
216 case BT_SEARCH_SERVICE: {
217 char *address = (char *)user_data;
218 if (strncmp((char*)req_info->user_data, address, BT_ADDRESS_STRING_SIZE)) {
219 BT_ERR("Unexpected: Info request pending for a different address!!");
222 BT_INFO("Found info request addr [%s]", (char*)req_info->user_data);
223 bt_sdp_info_t sdp_info;
225 memset(&sdp_info, 0x00, sizeof(bt_sdp_info_t));
226 _bt_convert_addr_string_to_type(sdp_info.device_addr.addr, address);
228 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
229 g_array_append_vals(out_param, &sdp_info, sizeof(bt_sdp_info_t));
230 _bt_service_method_return(req_info->context, out_param, result);
232 g_free(req_info->user_data);
233 _bt_free_info_from_invocation_list(req_info);
234 g_array_free(out_param, TRUE);
238 case BT_BOND_DEVICE: {
239 char *address = (char *)user_data;
240 if (strncmp((char*)req_info->user_data, address, BT_ADDRESS_STRING_SIZE)) {
241 BT_ERR("Unexpected: Info request pending for a different address!!");
244 BT_INFO("Found info request addr [%s]", (char*)req_info->user_data);
245 bluetooth_device_info_t dev_info;
246 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
247 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
249 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
250 g_array_append_vals(out_param, &dev_info,
251 sizeof(bluetooth_device_info_t));
252 _bt_service_method_return(req_info->context, out_param, result);
254 g_free(req_info->user_data);
255 _bt_free_info_from_invocation_list(req_info);
256 g_array_free(out_param, TRUE);
260 case BT_UNBOND_DEVICE: {
261 char *address = (char *)user_data;
262 if (strncmp((char*)req_info->user_data, address, BT_ADDRESS_STRING_SIZE)) {
263 BT_ERR("Unexpected: Info request pending for a different address!!");
266 BT_INFO("Found info request addr [%s]", (char*)req_info->user_data);
267 bluetooth_device_address_t dev_addr;
268 _bt_convert_addr_string_to_type(dev_addr.addr, address);
269 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
270 g_array_append_vals(out_param, &dev_addr,
271 sizeof(bluetooth_device_address_t));
272 _bt_service_method_return(req_info->context, out_param, result);
274 g_free(req_info->user_data);
275 _bt_free_info_from_invocation_list(req_info);
276 g_array_free(out_param, TRUE);
281 BT_ERR("Unhandled case");
289 * Remote device properties are received on all following conditions
290 * a. When Bonding in on-going
291 * b. When device properties are updated\changed for a connected device
292 * (due to SDP or any other reason)
294 static void __bt_device_remote_properties_callback(event_dev_properties_t *oal_dev_props)
296 bt_remote_dev_info_t *rem_info = NULL;
299 rem_info = g_malloc0(sizeof(bt_remote_dev_info_t));
300 _bt_copy_remote_dev(rem_info, &(oal_dev_props->device_info));
302 if (oal_dev_props->adv_len > 0) {
305 rem_info->manufacturer_data_len = oal_dev_props->adv_len;
306 rem_info->manufacturer_data =
307 g_memdup(oal_dev_props->adv_data,
308 oal_dev_props->adv_len);
309 BT_DBG("----Advertising Data Length: %d",
310 rem_info->manufacturer_data_len);
312 for (k = 0; k < rem_info->manufacturer_data_len; k++) {
313 BT_INFO("Check data[%d] = [[0x%x]",
314 k, oal_dev_props->adv_data[k]);
317 rem_info->manufacturer_data = NULL;
318 rem_info->manufacturer_data_len = 0;
321 /* a. Check if bonding is on-going, if yes, we MUST update the bonding device properties */
322 if (trigger_bond_info && !strcmp(trigger_bond_info->addr, rem_info->address)) {
323 BT_INFO("Bonding is ongoing, try update properties");
324 if (!trigger_bond_info->dev_info ||
325 (!trigger_bond_info->dev_info->name &&
326 !trigger_bond_info->dev_info->alias) ||
327 !trigger_bond_info->dev_info->address ||
328 trigger_bond_info->dev_info->uuid_count == 0) {
329 BT_INFO("Complete data is not present, Assigning rem_info");
330 if (!trigger_bond_info->dev_info)
331 trigger_bond_info->dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
332 _bt_copy_remote_dev_info(trigger_bond_info->dev_info, rem_info);
335 BT_DBG("Bonding dev addr has matched with remote dev properties address [%s]", rem_info->address);
336 __bt_handle_ongoing_bond(trigger_bond_info->dev_info, FALSE);
337 } else if (incoming_bond_info && !g_strcmp0(incoming_bond_info->addr, rem_info->address)) {
338 BT_INFO("Incoming Bond is ongoing, try update properties");
339 if (!incoming_bond_info->dev_info ||
340 (!incoming_bond_info->dev_info->name &&
341 !incoming_bond_info->dev_info->alias) ||
342 !incoming_bond_info->dev_info->address ||
343 incoming_bond_info->dev_info->uuid_count == 0) {
344 BT_INFO("Complete data is not present, Assigning rem_info");
345 if (!incoming_bond_info->dev_info)
346 incoming_bond_info->dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
347 _bt_copy_remote_dev_info(incoming_bond_info->dev_info, rem_info);
350 BT_DBG("Incoming Bond addr matches with remote dev properties address [%s]", rem_info->address);
351 __bt_handle_ongoing_bond(incoming_bond_info->dev_info, TRUE);
354 /* Add device to bonded list */
355 _bt_service_add_device_to_bonded_list(rem_info);
357 /* Handle SDP Device properties update */
358 if (service_search_info && service_search_info->dev_info) {
359 if (!strcmp(service_search_info->addr, rem_info->address)) {
360 BT_DBG("Properties received and SDP request pending, fill device properties and send event");
361 service_search_info->dev_info->class = rem_info->class;
362 service_search_info->dev_info->paired = rem_info->paired;
363 service_search_info->dev_info->connected = rem_info->connected;
364 service_search_info->dev_info->rssi = rem_info->rssi;
365 service_search_info->dev_info->addr_type = rem_info->addr_type;
366 service_search_info->dev_info->trust = rem_info->trust;
369 service_search_info->dev_info->manufacturer_data = NULL;
370 service_search_info->dev_info->manufacturer_data_len = 0;
372 __bt_handle_ongoing_device_service_search(service_search_info->dev_info);
376 _bt_free_remote_dev(rem_info);
380 static int __get_oal_trusted_profile(bluetooth_trusted_profile_t profile)
383 case TRUSTED_PROFILE_PBAP:
384 return OAL_TRUSTED_PROFILE_PBAP;
385 case TRUSTED_PROFILE_MAP:
386 return OAL_TRUSTED_PROFILE_MAP;
387 case TRUSTED_PROFILE_SAP:
388 return OAL_TRUSTED_PROFILE_SAP;
389 case TRUSTED_PROFILE_HFP_HF:
390 return OAL_TRUSTED_PROFILE_HFP_HF;
391 case TRUSTED_PROFILE_A2DP:
392 return OAL_TRUSTED_PROFILE_A2DP;
393 case TRUSTED_PROFILE_ALL:
394 return OAL_TRUSTED_PROFILE_ALL;
400 int _bt_set_trust_profile(bluetooth_device_address_t *addr,
401 bluetooth_trusted_profile_t profile, gboolean trust)
404 bt_address_t bd_addr;
405 oal_trusted_profile_e oal_profile;
409 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
411 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
412 oal_profile = __get_oal_trusted_profile(profile);
413 retv_if(0 == oal_profile, BLUETOOTH_ERROR_NOT_SUPPORT);
415 result = device_set_trust_profile(&bd_addr, oal_profile, trust);
416 if (result != OAL_STATUS_SUCCESS) {
417 BT_ERR("device_set_trust_profile error: [%d]", result);
418 return BLUETOOTH_ERROR_INTERNAL;
422 return BLUETOOTH_ERROR_NONE;
425 int _bt_get_trust_profile(bluetooth_device_address_t *addr,
426 bluetooth_trusted_profile_t profile, guint *trust)
429 bt_address_t bd_addr;
430 oal_trusted_profile_e oal_profile;
434 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
436 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
437 oal_profile = __get_oal_trusted_profile(profile);
438 retv_if(0 == oal_profile, BLUETOOTH_ERROR_NOT_SUPPORT);
440 result = device_get_trust_profile(&bd_addr, oal_profile, trust);
441 if (result != OAL_STATUS_SUCCESS) {
442 BT_ERR("device_set_trust_profile error: [%d]", result);
443 return BLUETOOTH_ERROR_INTERNAL;
447 return BLUETOOTH_ERROR_NONE;
450 static void __bt_handle_ongoing_device_service_search(bt_remote_dev_info_t *remote_dev_info)
452 GVariant *param = NULL;
453 GVariant *uuids = NULL;
454 GVariantBuilder *builder = NULL;
455 GVariant *manufacturer_data;
459 BT_INFO("Send Service Search request event");
461 if (remote_dev_info->alias)
462 name = remote_dev_info->alias;
464 name = remote_dev_info->name;
466 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
467 for (i = 0; i < remote_dev_info->uuid_count; i++) {
468 g_variant_builder_add(builder, "s",
469 remote_dev_info->uuids[i]);
471 uuids = g_variant_new("as", builder);
472 g_variant_builder_unref(builder);
473 manufacturer_data = g_variant_new_from_data((const GVariantType *)"ay",
474 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
477 param = g_variant_new("(isunsbub@asn@ay)",
478 BLUETOOTH_ERROR_NONE,
479 remote_dev_info->address,
480 remote_dev_info->class,
481 remote_dev_info->rssi,
483 remote_dev_info->paired,
484 remote_dev_info->connected,
485 remote_dev_info->trust,
487 remote_dev_info->manufacturer_data_len,
489 /* Send the event to application */
490 _bt_send_event(BT_ADAPTER_EVENT,
491 BLUETOOTH_EVENT_SERVICE_SEARCHED,
494 __bt_free_service_search_info(&service_search_info);
498 static void __bt_device_services_callback(event_dev_services_t* uuid_list)
500 bt_remote_dev_info_t *rem_info = NULL;
504 if (trigger_bond_info && _bt_compare_adddress(trigger_bond_info->dev_addr,
505 (bluetooth_device_address_t *)&uuid_list->address) == TRUE) {
506 bluetooth_device_address_t *dev_addr = trigger_bond_info->dev_addr;
508 BT_DBG("Bonding dev addr has matched");
509 /* Bonding ongoing, Query device properties again */
510 if (BLUETOOTH_ERROR_NONE ==
511 _bt_device_get_bonded_device_info(dev_addr))
512 BT_DBG("_bt_device_get_bonded_device_info success");
514 BT_ERR("_bt_device_get_bonded_device_info failed");
517 if (service_search_info == NULL) {
519 BT_DBG("searching_info == NULL");
523 if (_bt_compare_adddress(service_search_info->dev_addr,
524 (bluetooth_device_address_t *)&uuid_list->address) == FALSE) {
525 BT_DBG("This device is not queried");
529 rem_info = g_malloc0(sizeof(bt_remote_dev_info_t));
530 memset(rem_info, 0x00, sizeof(bt_remote_dev_info_t));
532 rem_info->address = g_new0(char, BT_ADDRESS_STRING_SIZE);
533 _bt_convert_addr_type_to_string(rem_info->address, uuid_list->address.addr);
535 rem_info->uuid_count = uuid_list->num;
537 BT_INFO("Address [%s]", rem_info->address);
538 BT_INFO("Number of UUID's [%d]", rem_info->uuid_count);
539 if (rem_info->uuid_count > 0)
540 rem_info->uuids = g_new0(char *, rem_info->uuid_count);
542 /* Fill Remote Device Service List list */
543 for (i = 0; i < rem_info->uuid_count; i++) {
544 rem_info->uuids[i] = g_malloc0(BLUETOOTH_UUID_STRING_MAX);
545 _bt_uuid_to_string((service_uuid_t *)&uuid_list->service_list[i].uuid, rem_info->uuids[i]);
546 BT_DBG("UUID value=%s", rem_info->uuids[i]);
549 /* Update local cache */
550 _bt_update_remote_dev_property(rem_info->address, DEV_PROP_SERVICES, (void *)rem_info);
552 BT_DBG("DBUS return");
553 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_SEARCH_SERVICE,
554 service_search_info->addr, BT_ADDRESS_STRING_SIZE);
556 /* Save UUID List of remote devices */
557 if (service_search_info->dev_info)
558 _bt_free_remote_dev(service_search_info->dev_info);
559 service_search_info->dev_info = rem_info;
561 /* Query Other device properties */
562 if (_bt_device_get_bonded_device_info(service_search_info->dev_addr) == BLUETOOTH_ERROR_NONE) {
563 BT_DBG("Bonded device info query posted to stack successfully");
565 BT_DBG("Possibly internal stack error in bonded device info query, perform cleanup");
566 __bt_free_service_search_info(&service_search_info);
571 static void __bt_device_authorization_request_callback(event_dev_authorize_req_t* auth_event)
573 oal_service_t service_d = auth_event->service_id;
574 gchar address[BT_ADDRESS_STR_LEN];
577 _bt_convert_addr_type_to_string(address, auth_event->address.addr);
579 BT_INFO("service_d: %d", service_d);
583 BT_INFO("Incoming HID Profile conn Req from device addr [%s]", address);
585 case A2DP_SERVICE_ID:
586 BT_INFO("Incoming A2DP(Remote Sink) profile conn Req from device addr [%s]", address);
587 _bt_a2dp_src_handle_incoming_authorization(address, service_d);
589 case A2DP_SRC_SERVICE_ID:
590 BT_INFO("Incoming A2DP(Remote Source) Profile conn Req from device addr [%s]", address);
591 _bt_a2dp_sink_handle_incoming_authorization(address, service_d);
593 case AVRCP_SERVICE_ID:
594 BT_INFO("Incoming AVRCP (Remote) Profile conn Req from device addr [%s]", address);
596 case AVRCP_CT_SERVICE_ID:
597 BT_INFO("Incoming AVRCP (Controller) Profile conn Req from device addr [%s]", address);
599 #ifdef TIZEN_FEATURE_BT_OBEX
600 case OPP_SERVICE_ID: {
601 GVariant *param = NULL;
602 char *name = g_strdup(address); /* TODO: Retrieve and send correct name in OBEX conn auth req event */
604 BT_INFO("Incoming OPP conn Req from device addr [%s]", address);
605 _bt_obex_server_set_pending_conn_auth_device_addr(address);
606 param = g_variant_new("(iss)", BLUETOOTH_ERROR_NONE, address, name);
607 _bt_send_event(BT_OPP_SERVER_EVENT,
608 BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE, param);
614 BT_INFO("Incoming HSP_SERVICE_ID conn Req from device addr [%s]", address);
617 BT_INFO("Incoming HFP_SERVICE_ID conn Req from device addr [%s]", address);
620 BT_INFO("Incoming SAP_SERVICE_ID conn Req from device addr [%s]", address);
622 case HSP_HS_SERVICE_ID:
623 BT_INFO("Incoming HSP_HS_SERVICE_ID conn Req from device addr [%s]", address);
625 case HFP_HS_SERVICE_ID:
626 BT_INFO("Incoming HFP_HS_SERVICE_ID conn Req from device addr [%s]", address);
629 case IOTIVITY_SERVICE_ID:
630 BT_INFO("Incoming IOTIVITY_SERVICE_ID conn Req from device addr [%s]", address);
634 /* For now, reject authorization for any service apart from above switch cases */
635 BT_INFO("Incoming Profile conn req with service ID [%d] from device addr [%s]", service_d, address);
636 res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, FALSE, FALSE);
637 if (res != OAL_STATUS_SUCCESS)
638 BT_ERR("authorize_response: %d", res);
642 /* Auto accept authorization request for HID, A2DP and AVRCP profiles */
643 BT_INFO("Auto Accept authorization");
644 res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, TRUE, FALSE);
645 if (res != OAL_STATUS_SUCCESS)
646 BT_ERR("authorize_response: %d", res);
650 static void __bt_handle_ongoing_bond(bt_remote_dev_info_t *remote_dev_info, gboolean incoming_bond)
652 GVariant *param = NULL;
655 if ((remote_dev_info->name || remote_dev_info->alias)
656 && remote_dev_info->address
657 && remote_dev_info->uuids) {
658 BT_INFO("All properties updated, time to send bonding finished event");
659 GVariant *uuids = NULL;
660 GVariantBuilder *builder = NULL;
661 GVariant *manufacturer_data;
665 if (remote_dev_info->alias)
666 name = remote_dev_info->alias;
668 name = remote_dev_info->name;
670 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
671 for (i = 0; i < remote_dev_info->uuid_count; i++) {
672 g_variant_builder_add(builder, "s",
673 remote_dev_info->uuids[i]);
675 uuids = g_variant_new("as", builder);
676 g_variant_builder_unref(builder);
677 manufacturer_data = g_variant_new_from_data((const GVariantType *)"ay",
678 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
681 param = g_variant_new("(isunsbub@asn@ay)",
682 BLUETOOTH_ERROR_NONE,
683 remote_dev_info->address,
684 remote_dev_info->class,
685 remote_dev_info->rssi,
687 remote_dev_info->paired,
688 remote_dev_info->connected,
689 remote_dev_info->trust,
691 remote_dev_info->manufacturer_data_len,
693 /* Send the event to application */
694 _bt_send_event(BT_ADAPTER_EVENT,
695 BLUETOOTH_EVENT_BONDING_FINISHED,
698 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
700 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
701 __bt_free_pairing_info(&trigger_pairing_info);
704 BT_INFO("Lets wait for more remote device properties");
708 static void __handle_incoming_bond_created_event(bt_address_t *bd_addr)
710 char address[BT_ADDRESS_STRING_SIZE];
711 bluetooth_device_address_t dev_addr;
716 * BlueZ sends paired signal for each paired device, during activation,
717 * We should ignore this, otherwise application thinks that a new device
720 if (_bt_adapter_get_status() != BT_ACTIVATED) {
721 BT_DBG("BT is not activated, so ignore this");
725 _bt_convert_addr_type_to_string(address, bd_addr->addr);
726 if (!incoming_bond_info) {
727 incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
729 if (g_strcmp0(incoming_bond_info->addr, address)) {
730 BT_DBG("Previous Bond address: [%s] differs from new address: [%s]",
731 address, incoming_bond_info->addr);
732 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
733 incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
737 BT_INFO("Incoming bond successfully completed");
738 g_strlcpy(incoming_bond_info->addr, address, BT_ADDRESS_STRING_SIZE);
739 incoming_bond_info->dev_info = NULL;
741 _bt_convert_addr_string_to_type(dev_addr.addr, incoming_bond_info->addr);
742 if (BLUETOOTH_ERROR_NONE == _bt_device_get_bonded_device_info(&dev_addr)) {
743 BT_DBG("Bonded device info query posted to stack successfully");
745 BT_ERR("Bonded device info query failed");
746 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
752 static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr)
754 gchar address[BT_ADDRESS_STR_LEN];
755 bluetooth_device_address_t dev_addr;
759 if (trigger_bond_info == NULL) {
761 BT_DBG("trigger_bond_info == NULL, Handle incomming bond event");
762 __handle_incoming_bond_created_event(bd_addr);
766 _bt_convert_addr_type_to_string(address, bd_addr->addr);
767 if (g_strcmp0(trigger_bond_info->addr, address)) {
768 BT_DBG("Bonding address= [%s] is different from requested address =[%s]",
769 address, trigger_bond_info->addr);
770 __handle_incoming_bond_created_event(bd_addr);
774 BT_INFO("Bonding successfully completed");
775 /* Bonding state will be cleaned up & BONDING FINISHED EVENT
776 will be sent only when Properties are fetched from stack
777 Till that time lets not free trigger_bond_info.
778 However it is possible that while fetching device properties, internal
779 stack error can occur which can lead to no valid properties or
780 no properties at all. So in such cases, we must not wait for properties,
781 otherwise, it can lead to infinite wait */
782 _bt_convert_addr_string_to_type(dev_addr.addr,
783 trigger_bond_info->addr);
785 if (_bt_device_get_bonded_device_info(&dev_addr) == BLUETOOTH_ERROR_NONE) {
786 BT_DBG("BOnded device info query posted to stack successfully");
787 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_BOND_DEVICE,
788 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
790 BT_DBG("Possibly internal stack error in bonded device info query, perform cleanup");
791 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
792 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
793 /* Destroy if at all device got bonded at stack level */
794 device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
796 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
797 __bt_free_pairing_info(&trigger_pairing_info);
803 /**********************************************************************************************
804 * Bond removal event can be triggered for following reasons -
805 * a. If Bonding procedure if failed (for Auth failed, Page timeout, cancelled by user etc)
806 * b. If Application requests for explicitly removing the bond
807 * c. When application attempt to create bond,bond is removed first which triggers this event
808 * c. is in-line with Bluedroid bond create\emoval architecture
809 *********************************************************************************************/
810 static void __bt_device_handle_bond_removal_event(bt_address_t *bd_addr)
812 char address[BT_ADDRESS_STRING_SIZE];
816 _bt_convert_addr_type_to_string(address, bd_addr->addr);
817 _bt_service_remove_device_from_bonded_list(address);
819 if (trigger_unbond_info) {
820 BT_INFO("Bond removal request successfully handled, return DBUS and send event");
821 GVariant *param = NULL;
822 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_UNBOND_DEVICE,
823 trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
824 param = g_variant_new("(is)", BLUETOOTH_ERROR_NONE, trigger_unbond_info->addr);
825 _bt_send_event(BT_ADAPTER_EVENT,
826 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
828 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
829 __bt_free_pairing_info(&trigger_pairing_info);
830 } else if (trigger_bond_info) {
831 BT_ERR("Bonding was removed");
832 __bt_device_handle_bond_state();
837 static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_fail_event)
840 oal_status_t status = bond_fail_event->status;
841 BT_INFO("Bonding failed, reason: %d", status);
844 case OAL_STATUS_RMT_DEVICE_DOWN:
846 if (trigger_bond_info) {
847 BT_INFO("OAL_STATUS_RMT_DEVICE_DOWN:Lets retry bonding!! retry count [%d]",
849 int ret = OAL_STATUS_SUCCESS;
850 if (bond_retry_count < MAX_BOND_RETRY_COUNT) {
851 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
852 if (ret != OAL_STATUS_SUCCESS) {
853 BT_ERR("Create Bond procedure could not suceed");
854 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
855 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
856 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
857 __bt_free_pairing_info(&trigger_pairing_info);
858 bond_retry_count = 0;
862 BT_ERR("Create Bond failed MAX_BOND_RETRY_COUNT TIMES!!");
863 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
864 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
865 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
866 __bt_free_pairing_info(&trigger_pairing_info);
867 bond_retry_count = 0;
872 case OAL_STATUS_AUTH_FAILED:
874 /*TODO Auto pairing status set & ignore auto pairing logics can be done at this point.
875 To be considered later*/
876 int result = BLUETOOTH_ERROR_INTERNAL;
877 BT_INFO("BT_OPERATION_STATUS_AUTH_FAILED");
878 if (trigger_bond_info) {
879 BT_ERR("Create Bond procedure could not suceed, check if cancelled by User");
880 if (trigger_bond_info->is_cancelled_by_user) {
881 BT_ERR("Bonding is cancelled by user");
882 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
884 __bt_device_handle_pending_requests(result, BT_BOND_DEVICE,
885 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
886 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
889 __bt_free_pairing_info(&trigger_pairing_info);
892 case OAL_STATUS_INTERNAL_ERROR:
894 BT_INFO("OAL_STATUS_INTERNAL_ERROR");
895 if (trigger_unbond_info) {
896 BT_INFO("Bond removal request failed, return DBUS and send event");
897 GVariant *param = NULL;
898 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_UNBOND_DEVICE,
899 trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
900 param = g_variant_new("(is)", BLUETOOTH_ERROR_INTERNAL, trigger_unbond_info->addr);
901 _bt_send_event(BT_ADAPTER_EVENT,
902 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
904 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
905 } else if (trigger_bond_info) {
906 if (__bt_device_handle_bond_state() != BLUETOOTH_ERROR_NONE) {
907 if (trigger_bond_info) {
908 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
909 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
910 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
915 __bt_free_pairing_info(&trigger_pairing_info);
920 BT_ERR("Unknown status of Bond failed event status [%d]", status);
928 static void __bt_device_event_handler(int event_type, gpointer event_data)
930 int eventcheck = OAL_EVENT_DEVICE_PROPERTIES;
931 BT_INFO("event [%d] Event check = [%d]", event_type, eventcheck);
933 switch (event_type) {
934 case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY: {
935 BT_INFO("BREDR Device Found");
936 __bt_device_remote_device_found_callback(event_data, FALSE);
939 case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE: {
940 BT_INFO("Dual Device Found");
941 __bt_device_remote_device_found_callback(event_data, FALSE);
944 case OAL_EVENT_DEVICE_PROPERTIES: {
945 BT_INFO("Remote Device properties Received");
946 __bt_device_remote_properties_callback((event_dev_properties_t *)event_data);
949 case OAL_EVENT_DEVICE_BONDING_SUCCESS: {
950 BT_INFO("Bonding Success event Received");
951 __bt_device_handle_bond_completion_event((bt_address_t *)event_data);
954 case OAL_EVENT_DEVICE_BONDING_REMOVED: {
955 BT_INFO("Bonding Removed event Received");
956 __bt_device_handle_bond_removal_event((bt_address_t *)event_data);
959 case OAL_EVENT_DEVICE_BONDING_FAILED: {
960 BT_INFO("Bonding Failed event Received");
961 __bt_device_handle_bond_failed_event((event_dev_bond_failed_t*) event_data);
964 case OAL_EVENT_DEVICE_ACL_CONNECTED: {
965 BT_INFO("ACL Connected event Received");
966 event_dev_conn_status_t* param = event_data;
967 __bt_device_conn_state_changed_callback(param, TRUE, 0);
970 case OAL_EVENT_DEVICE_ACL_DISCONNECTED: {
971 BT_INFO("ACL Disconnected event Received");
972 __bt_device_conn_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 0);
975 case OAL_EVENT_DEVICE_LE_CONNECTED: {
976 BT_INFO("LE Connected event Received");
977 event_dev_conn_status_t* param = event_data;
978 __bt_device_conn_state_changed_callback(param, TRUE, 1);
981 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
982 BT_INFO("LE Disconnected event Received");
983 __bt_device_conn_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 1);
986 case OAL_EVENT_DEVICE_PIN_REQUEST: {
987 BT_INFO("PIN Request Received");
988 __bt_device_pin_request_callback((remote_device_t*)event_data);
991 case OAL_EVENT_DEVICE_PASSKEY_ENTRY_REQUEST: {
992 BT_INFO("Passkey Entry request Received");
993 __bt_device_ssp_passkey_entry_callback((remote_device_t*)event_data);
996 case OAL_EVENT_DEVICE_PASSKEY_CONFIRMATION_REQUEST:{
997 BT_INFO("Passkey Confirmation Request Received");
998 __bt_device_ssp_passkey_confirmation_callback((event_dev_passkey_t *)event_data);
1001 case OAL_EVENT_DEVICE_PASSKEY_DISPLAY: {
1002 BT_INFO("Passkey Display Request Received");
1003 __bt_device_ssp_passkey_display_callback((event_dev_passkey_t *)event_data);
1006 case OAL_EVENT_DEVICE_SSP_CONSENT_REQUEST: {
1007 BT_INFO("SSP Consent Request Received");
1008 __bt_device_ssp_consent_callback((remote_device_t*)event_data);
1011 case OAL_EVENT_DEVICE_SERVICES: {
1012 BT_INFO("Remote Device Services Received");
1013 __bt_device_services_callback((event_dev_services_t*)event_data);
1016 case OAL_EVENT_DEVICE_AUTHORIZE_REQUEST: {
1017 BT_INFO("Remote Device Authorization Request");
1018 __bt_device_authorization_request_callback((event_dev_authorize_req_t*)event_data);
1021 case OAL_EVENT_DEVICE_TRUSTED: {
1022 BT_INFO("Remote Device Trusted");
1023 __bt_device_trusted_callback(TRUE, (event_dev_trust_t*)event_data);
1026 case OAL_EVENT_DEVICE_UNTRUSTED: {
1027 BT_INFO("Remote Device UnTrusted");
1028 __bt_device_trusted_callback(FALSE, (event_dev_trust_t*)event_data);
1031 case OAL_EVENT_DEVICE_NAME: {
1032 remote_device_t *rem_dev = event_data;
1033 gchar address[BT_ADDRESS_STR_LEN];
1035 _bt_convert_addr_type_to_string(address, rem_dev->address.addr);
1036 BT_INFO("Remote Device name Received");
1037 BT_INFO("Name: %s, Address: %s", rem_dev->name, address);
1039 /* Update local cache */
1040 _bt_update_remote_dev_property(address, DEV_PROP_NAME, (void *)rem_dev->name);
1043 case OAL_EVENT_DEVICE_TRUSTED_PROFILES_CHANGED: {
1044 event_device_trusted_profiles_t *ev = event_data;
1045 char address[BT_ADDRESS_STRING_SIZE];
1049 _bt_convert_addr_type_to_string(address, ev->address.addr);
1050 _bt_send_event(BT_DEVICE_EVENT,
1051 BLUETOOTH_EVENT_SUPPORTED_PROFILE_TRUSTED,
1052 g_variant_new("(isi)", BLUETOOTH_ERROR_NONE,
1053 address, ev->trust_val));
1056 case OAL_EVENT_RSSI_MONITORING_ENABLED: {
1057 event_dev_rssi_info_t *ev = event_data;
1058 char address[BT_ADDRESS_STRING_SIZE];
1063 _bt_convert_addr_type_to_string(address, ev->address.addr);
1064 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1065 address, ev->link_type, TRUE);
1066 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1069 case OAL_EVENT_RSSI_MONITORING_DISABLED: {
1070 event_dev_rssi_info_t *ev = event_data;
1071 char address[BT_ADDRESS_STRING_SIZE];
1076 _bt_convert_addr_type_to_string(address, ev->address.addr);
1077 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1078 address, ev->link_type, FALSE);
1079 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1082 case OAL_EVENT_RSSI_ALERT_RECEIVED: {
1083 event_dev_rssi_info_t *ev = event_data;
1084 char address[BT_ADDRESS_STRING_SIZE];
1089 _bt_convert_addr_type_to_string(address, ev->address.addr);
1090 param = g_variant_new("(isiii)", BLUETOOTH_ERROR_NONE,
1091 address, ev->link_type, ev->alert_type, ev->rssi);
1092 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ALERT, param);
1095 case OAL_EVENT_RAW_RSSI_RECEIVED: {
1096 event_dev_rssi_info_t *ev = event_data;
1097 char address[BT_ADDRESS_STRING_SIZE];
1102 _bt_convert_addr_type_to_string(address, ev->address.addr);
1103 param = g_variant_new("(isii)", BLUETOOTH_ERROR_NONE,
1104 address, ev->link_type, ev->rssi);
1105 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RAW_RSSI, param);
1109 BT_INFO("Unhandled event..");
1113 /* Legacy Pairing event handler */
1114 static void __bt_device_pin_request_callback(remote_device_t* pin_req_event)
1117 char address[BT_ADDRESS_STRING_SIZE];
1118 char pin_code[BLUETOOTH_PIN_CODE_MAX_LENGTH + 1];
1121 _bt_convert_addr_type_to_string(address, pin_req_event->address.addr);
1123 BT_INFO("Address[%s]", address);
1124 BT_INFO("Name[%s]", pin_req_event->name);
1125 BT_INFO("COD[%d]", pin_req_event->cod);
1127 if (trigger_pairing_info) {
1128 /* BTAPI support only one pairing at a time */
1129 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1130 BT_ERR("New PIN request address [%s]", address);
1131 device_reject_pin_request(&pin_req_event->address);
1135 /* If user initiated bonding and auto response is possible, just reply with default 0000*/
1136 if (_bt_is_bonding_device_address(address) == TRUE &&
1137 _bt_agent_is_auto_response(pin_req_event->cod, address, pin_req_event->name)) {
1138 /* Note: Currently even if SYSPOPUP is supported, we use Fixed PIN "0000" for basic pairing
1139 as BT SYSPOPUP is currently not working for PIN KEY entry in Tizen platform. This needs
1140 to be checked and fixed apropriately */
1141 _bt_set_autopair_status_in_bonding_info(TRUE);
1142 device_accept_pin_request(&pin_req_event->address, "0000");
1143 } else if (_bt_agent_is_hid_keyboard(pin_req_event->cod)) {
1144 BT_DBG("Remote Device is HID keyboard Type..");
1145 char str_passkey[BT_PASSKEY_MAX_LENGTH + 1] = { 0 };
1147 if (_bt_agent_generate_passkey(str_passkey,
1148 BT_PASSKEY_MAX_LENGTH) != 0) {
1149 device_reject_pin_request(&pin_req_event->address);
1152 device_accept_pin_request(&pin_req_event->address, str_passkey);
1154 BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1155 param = g_variant_new("(isss)", BLUETOOTH_ERROR_NONE, address, pin_req_event->name, str_passkey);
1156 _bt_send_event(BT_ADAPTER_EVENT,
1157 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1158 BT_DBG("Key board pairing in process");
1159 } else if (BLUETOOTH_ERROR_NONE == __bt_get_device_pin_code(address, pin_code)) {
1160 BT_DBG("Use stored PIN code [%s]", pin_code);
1161 device_accept_pin_request(&pin_req_event->address, pin_code);
1163 if (_bt_is_bonding_device_address(address) == TRUE) {
1164 BT_DBG("Show Pin entry");
1165 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1166 trigger_pairing_info->addr = g_strdup(address);
1167 trigger_pairing_info->is_ssp = FALSE;
1169 BT_DBG("Send BLUETOOTH_EVENT_PIN_REQUEST");
1170 param = g_variant_new("(iss)", BLUETOOTH_ERROR_NONE, address, pin_req_event->name);
1171 _bt_send_event(BT_ADAPTER_EVENT,
1172 BLUETOOTH_EVENT_PIN_REQUEST, param);
1177 _bt_agent_release_memory();
1181 /* SSP Pairing event handler */
1182 static void __bt_device_ssp_passkey_entry_callback(remote_device_t* dev_info)
1185 gchar address[BT_ADDRESS_STR_LEN];
1188 int result = BLUETOOTH_ERROR_NONE;
1191 _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1193 name = dev_info->name;
1195 BT_INFO("Address[%s]", address);
1196 BT_INFO("Name[%s]", name);
1197 BT_INFO("COD[%d]", dev_info->cod);
1199 if (trigger_pairing_info) {
1200 /* BTAPI support only one pairing at a time */
1201 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1202 BT_ERR("New PIN request address [%s]", address);
1203 device_reject_pin_request(&dev_info->address);
1208 /* Set pairing data */
1209 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1210 trigger_pairing_info->addr = g_strdup(address);
1211 trigger_pairing_info->is_ssp = TRUE;
1213 param = g_variant_new("(iss)", result, p_addr, name);
1214 _bt_send_event(BT_ADAPTER_EVENT,
1215 BLUETOOTH_EVENT_PASSKEY_REQUEST, param);
1219 static void __bt_device_ssp_passkey_confirmation_callback(event_dev_passkey_t *dev_info)
1222 gchar address[BT_ADDRESS_STR_LEN];
1225 char str_passkey[7];
1226 int result = BLUETOOTH_ERROR_NONE;
1229 _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1231 name = dev_info->device_info.name;
1233 BT_INFO("Address[%s]", address);
1234 BT_INFO("Name[%s]", name);
1235 BT_INFO("COD[%d]", dev_info->device_info.cod);
1237 if (trigger_pairing_info) {
1238 /* BTAPI support only one pairing at a time */
1239 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1240 BT_ERR("New PIN request address [%s]", address);
1241 device_reject_pin_request(&dev_info->device_info.address);
1246 /* Set pairing data */
1247 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1248 trigger_pairing_info->addr = g_strdup(address);
1249 trigger_pairing_info->is_ssp = TRUE;
1251 BT_DBG("Send BLUETOOTH_EVENT_PASSKEY_CONFIRMATION");
1252 snprintf(str_passkey, sizeof(str_passkey), "%.6d", dev_info->pass_key);
1254 param = g_variant_new("(isss)", result, p_addr, name, str_passkey);
1255 _bt_send_event(BT_ADAPTER_EVENT,
1256 BLUETOOTH_EVENT_PASSKEY_CONFIRM_REQUEST, param);
1260 static void __bt_device_ssp_passkey_display_callback(event_dev_passkey_t *dev_info)
1263 gchar address[BT_ADDRESS_STR_LEN];
1266 char str_passkey[7];
1267 int result = BLUETOOTH_ERROR_NONE;
1270 _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1272 name = dev_info->device_info.name;
1274 BT_INFO("Address[%s]", address);
1275 BT_INFO("Name[%s]", name);
1276 BT_INFO("COD[%d]", dev_info->device_info.cod);
1278 if (trigger_pairing_info) {
1279 /* BTAPI support only one pairing at a time */
1280 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1281 BT_ERR("New PIN request address [%s]", address);
1282 device_reject_pin_request(&dev_info->device_info.address);
1287 /* Set pairing data */
1288 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1289 trigger_pairing_info->addr = g_strdup(address);
1290 trigger_pairing_info->is_ssp = TRUE;
1292 BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1293 snprintf(str_passkey, sizeof(str_passkey), "%06d", dev_info->pass_key);
1295 param = g_variant_new("(isss)", result, p_addr, name, str_passkey);
1296 if (passkey_watcher) {
1297 BT_INFO("Send passkey to %s", passkey_watcher);
1298 _bt_send_event_to_dest(passkey_watcher, BT_ADAPTER_EVENT,
1299 BLUETOOTH_EVENT_PASSKEY_NOTIFICATION, param);
1301 _bt_send_event(BT_ADAPTER_EVENT,
1302 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1307 static void __bt_device_ssp_consent_callback(remote_device_t* dev_info)
1309 gchar address[BT_ADDRESS_STR_LEN];
1316 _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1317 name = dev_info->name;
1318 cod = dev_info->cod;
1320 BT_INFO("Address[%s]", address);
1321 BT_INFO("Name[%s]", name);
1322 BT_INFO("COD[%d]", cod);
1324 if (trigger_pairing_info) {
1325 /* BTAPI support only one pairing at a time */
1326 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1327 BT_ERR("New PIN request address [%s]", address);
1328 device_reject_pin_request(&dev_info->address);
1333 /* Set pairing data */
1334 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1335 trigger_pairing_info->addr = g_strdup(address);
1336 trigger_pairing_info->is_ssp = TRUE;
1338 local_major = ((cod >> 8) & 0x001f);
1339 local_minor = (cod & 0x00fc);
1340 BT_DBG("SSP_CONSENT: Major type=[0x%x] and Minor type=[0x%x]", local_major, local_minor);
1342 /*TODO: BLUETOOTH_EVENT_SSP_CONSENT_REQUEST to be handled in Tizen */
1346 static void __bt_device_conn_state_changed_callback(event_dev_conn_status_t *acl_event,
1347 gboolean connected, unsigned char type)
1349 gchar address[BT_ADDRESS_STR_LEN];
1350 int result = BLUETOOTH_ERROR_NONE;
1351 GVariant *param = NULL;
1352 bt_device_conn_info_t conn_info;
1355 _bt_convert_addr_type_to_string(address, acl_event->address.addr);
1357 _bt_logging_connection(connected, type);
1360 param = g_variant_new("(isy)", result, address, type);
1361 _bt_send_event(BT_DEVICE_EVENT,
1362 BLUETOOTH_EVENT_DEVICE_CONNECTED,
1365 param = g_variant_new("(isy)", result, address, type);
1366 _bt_send_event(BT_DEVICE_EVENT,
1367 BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1371 conn_info.connected = connected;
1372 conn_info.type = type;
1373 /* Update local cache */
1374 _bt_update_remote_dev_property(address, DEV_PROP_CONNECTED, (void *)&conn_info);
1376 #ifdef TIZEN_GATT_CLIENT
1377 /*handle LE connected device info*/
1379 BT_DBG("handle LE connected device info");
1380 _bt_handle_le_connected_dev_info(address, connected);
1387 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble)
1389 bt_remote_dev_info_t *dev_info = NULL;
1390 int result = BLUETOOTH_ERROR_NONE;
1391 GVariant *param = NULL;
1392 GVariant *uuids = NULL;
1393 GVariant *manufacturer_data = NULL;
1394 GVariantBuilder *builder = NULL;
1398 ret_if(_bt_is_discovering() == FALSE);
1399 ret_if(event_data == NULL);
1401 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
1404 event_ble_dev_found_t * oal_ble_dev = event_data;
1405 BT_INFO("Device type [%d]", oal_ble_dev->device_info.type);
1407 _bt_copy_remote_dev(dev_info, &oal_ble_dev->device_info);
1409 dev_info->manufacturer_data_len = oal_ble_dev->adv_len;
1410 if (dev_info->manufacturer_data_len)
1411 dev_info->manufacturer_data = g_memdup(oal_ble_dev->adv_data,
1412 dev_info->manufacturer_data_len);
1414 dev_info->manufacturer_data = NULL;
1415 BT_DBG("----Advertising Data Length: %d", dev_info->manufacturer_data_len);
1417 event_dev_found_t * oal_dev = event_data;
1418 _bt_copy_remote_dev(dev_info, &oal_dev->device_info);
1421 /* If Remote device name is NULL or still RNR is not done then display address as name. */
1422 if (dev_info->name == NULL)
1423 dev_info->name = g_strdup(dev_info->address);
1424 BT_DBG("Name %s", dev_info->name);
1426 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1427 for (i = 0; i < dev_info->uuid_count; i++)
1428 g_variant_builder_add(builder, "s", dev_info->uuids[i]);
1430 uuids = g_variant_new("as", builder);
1431 g_variant_builder_unref(builder);
1433 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1434 dev_info->manufacturer_data,
1435 dev_info->manufacturer_data_len,
1439 param = g_variant_new("(isunsbub@asn@ay)", result,
1445 dev_info->connected,
1448 dev_info->manufacturer_data_len,
1451 _bt_send_event(BT_ADAPTER_EVENT,
1452 BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
1455 _bt_free_remote_dev(dev_info);
1459 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info)
1461 gchar address[BT_ADDRESS_STR_LEN];
1462 int result = BLUETOOTH_ERROR_NONE;
1463 GVariant *param = NULL;
1467 _bt_convert_addr_type_to_string(address, info->address.addr);
1469 /* Update local cache */
1470 _bt_update_remote_dev_property(address, DEV_PROP_TRUST, (void *)&trusted);
1472 param = g_variant_new("(is)", result, address);
1473 event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1474 BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1475 /* Send event to application */
1476 _bt_send_event(BT_DEVICE_EVENT,
1483 static void __bt_free_pairing_info(bt_pairing_data_t **p_info)
1486 bt_pairing_data_t * info = *p_info;
1497 static void __bt_free_bond_info(uint8_t type)
1502 case BT_DEVICE_BOND_INFO:
1503 if (!trigger_bond_info)
1506 if (trigger_bond_info->addr)
1507 g_free(trigger_bond_info->addr);
1508 if (trigger_bond_info->dev_addr)
1509 g_free(trigger_bond_info->dev_addr);
1510 if (trigger_bond_info->dev_info)
1511 _bt_free_remote_dev(trigger_bond_info->dev_info);
1512 g_free(trigger_bond_info);
1513 trigger_bond_info = NULL;
1515 case BT_DEVICE_INCOMING_BOND_INFO:
1516 if (!incoming_bond_info)
1519 if (incoming_bond_info->dev_info)
1520 _bt_free_remote_dev(incoming_bond_info->dev_info);
1521 g_free(incoming_bond_info);
1522 incoming_bond_info = NULL;
1524 case BT_DEVICE_UNBOND_INFO:
1525 if (!trigger_unbond_info)
1528 if (trigger_unbond_info->addr)
1529 g_free(trigger_unbond_info->addr);
1530 if (trigger_unbond_info->dev_addr)
1531 g_free(trigger_unbond_info->dev_addr);
1532 if (trigger_unbond_info->dev_info)
1533 _bt_free_remote_dev(trigger_unbond_info->dev_info);
1534 g_free(trigger_unbond_info);
1535 trigger_unbond_info = NULL;
1541 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info)
1543 bt_service_search_info_data_t * info = *p_info;
1550 if (info->dev_addr) {
1551 g_free(info->dev_addr);
1552 info->dev_addr = NULL;
1555 if (info->dev_info) {
1556 _bt_free_remote_dev(info->dev_info);
1557 info->dev_info = NULL;
1565 static int __bt_device_handle_bond_state(void)
1567 BT_INFO("Current Bond state: %d", bt_device_bond_state);
1568 int ret = OAL_STATUS_INTERNAL_ERROR;
1570 switch (bt_device_bond_state) {
1571 case BT_DEVICE_BOND_STATE_CANCEL_DISCOVERY:
1572 /*TODO:Bonding during discovery: Unhandled!!*/
1573 BT_INFO("Bonding during discovery: Unhandled!!");
1575 case BT_DEVICE_BOND_STATE_DISCOVERY_CANCELLED:
1576 /*TODO:Bonding during discovery: Unhandled!!*/
1577 BT_INFO("Bonding during discovery: Unhandled!!");
1579 case BT_DEVICE_BOND_STATE_REMOVE_BONDING:
1580 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVED_BONDING;
1581 ret = device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
1582 if (ret != OAL_STATUS_SUCCESS)
1583 ret = __bt_device_handle_bond_state();
1585 case BT_DEVICE_BOND_STATE_REMOVED_BONDING:
1586 bt_device_bond_state = BT_DEVICE_BOND_STATE_NONE;
1587 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
1588 /* Bonding procedure was started but unfortunately could not complete.
1589 Basically removed bonding was success, but create bond request could not proceed
1590 So lets cleanup the context */
1591 if (ret != OAL_STATUS_SUCCESS) {
1592 BT_ERR("Create Bond procedure could not suceed");
1593 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1594 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1595 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1596 __bt_free_pairing_info(&trigger_pairing_info);
1599 case BT_DEVICE_BOND_STATE_NONE:
1600 BT_INFO("Create Bond failed!!");
1601 if (trigger_bond_info) {
1602 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1603 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1604 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1605 __bt_free_pairing_info(&trigger_pairing_info);
1612 if (ret != OAL_STATUS_SUCCESS)
1613 return BLUETOOTH_ERROR_INTERNAL;
1615 return BLUETOOTH_ERROR_NONE;
1618 int _bt_device_get_bonded_device_info(bluetooth_device_address_t *addr)
1621 bt_address_t bd_addr;
1625 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
1627 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
1628 result = device_query_attributes(&bd_addr);
1629 if (result != OAL_STATUS_SUCCESS) {
1630 BT_ERR("device_query_attributes error: [%d]", result);
1631 return BLUETOOTH_ERROR_INTERNAL;
1635 return BLUETOOTH_ERROR_NONE;
1638 int _bt_set_alias(bluetooth_device_address_t *device_address, const char *alias)
1641 char address[BT_ADDRESS_STRING_SIZE];
1644 BT_CHECK_PARAMETER(alias, return);
1646 ret = device_set_alias((bt_address_t *)device_address, (char *)alias);
1647 if (ret != OAL_STATUS_SUCCESS) {
1648 BT_ERR("device_set_alias: %d", ret);
1649 return BLUETOOTH_ERROR_INTERNAL;
1652 /* Update local cache */
1653 _bt_convert_addr_type_to_string(address, device_address->addr);
1654 _bt_update_remote_dev_property(address, DEV_PROP_ALIAS, (void *)alias);
1657 return BLUETOOTH_ERROR_NONE;
1660 int _bt_bond_device(bluetooth_device_address_t *device_address,
1661 unsigned short conn_type, GArray **out_param1)
1663 int result = BLUETOOTH_ERROR_NONE;
1664 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1665 bluetooth_device_info_t dev_info;
1668 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1670 /* If bonding or discovery already going on */
1671 if (trigger_bond_info || _bt_is_discovering()) {
1672 BT_ERR("Device is buzy, bonding can not proceed now..");
1673 result = BLUETOOTH_ERROR_DEVICE_BUSY;
1677 /*TODO: If unbonding with same device going on */
1678 _bt_convert_addr_type_to_string(address, device_address->addr);
1680 trigger_bond_info = g_malloc0(sizeof(bt_bond_data_t));
1681 trigger_bond_info->addr = g_strdup(address);
1682 trigger_bond_info->conn_type = conn_type;
1683 trigger_bond_info->is_device_creating = TRUE;
1684 trigger_bond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1685 trigger_bond_info->dev_info = NULL;
1687 /* Ready to initiate bonding */
1689 /* In Tizen, we will first remove bond and then attempt to create bond to keep
1690 consistency with bluedroid. Even if remove bond fails due to device not already
1691 bonded, then straight away create bond is triggered. This is because, remove bond
1692 is handled differently in bluedroid and bluez. In Bluez, if device is
1693 already removed, remove bond call fails.
1694 However in bluedroid, remove bond on already removed device returns success. So we will
1695 handle the cases transparently*/
1696 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVE_BONDING;
1697 bond_retry_count = 0;
1698 result = __bt_device_handle_bond_state();
1700 if (result != BLUETOOTH_ERROR_NONE)
1707 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1708 memcpy(dev_info.device_address.addr, device_address->addr,
1709 BLUETOOTH_ADDRESS_LENGTH);
1711 g_array_append_vals(*out_param1, &dev_info,
1712 sizeof(bluetooth_device_info_t));
1713 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1719 int _bt_unbond_device(bluetooth_device_address_t *device_address,
1720 GArray **out_param1)
1722 int result = OAL_STATUS_SUCCESS;
1723 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1724 bluetooth_device_info_t dev_info;
1727 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1729 _bt_convert_addr_type_to_string(address, device_address->addr);
1731 trigger_unbond_info = g_malloc0(sizeof(bt_bond_data_t));
1732 trigger_unbond_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1733 trigger_unbond_info->addr = g_strdup(address);
1734 trigger_unbond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1736 /* Check if Bonding is already going on, we should not abruptly remove bonding*/
1737 if (trigger_bond_info && strncmp(trigger_bond_info->addr, trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE) == 0) {
1738 BT_ERR("Bonding with same device already ongoing");
1739 result = BLUETOOTH_ERROR_PERMISSION_DEINED;
1743 result = device_destroy_bond((bt_address_t *)device_address);
1744 if (result != OAL_STATUS_SUCCESS)
1750 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1751 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1752 trigger_unbond_info->addr);
1754 g_array_append_vals(*out_param1, &dev_info,
1755 sizeof(bluetooth_device_info_t));
1756 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
1761 int _bt_cancel_bonding(void)
1763 int result = OAL_STATUS_SUCCESS;
1766 retv_if(trigger_bond_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1768 result = device_stop_bond((bt_address_t *)trigger_bond_info->dev_addr);
1770 if (result == OAL_STATUS_SUCCESS)
1771 trigger_bond_info->is_cancelled_by_user = TRUE;
1776 int _bt_passkey_reply(const char *passkey, gboolean cnfm_reply)
1778 bluetooth_device_address_t device_address;
1779 int ret = OAL_STATUS_SUCCESS;
1780 BT_INFO("reply: %d", cnfm_reply);
1782 retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1783 retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1785 _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1787 if (trigger_pairing_info->is_ssp) {
1789 ret = device_accept_passkey_entry((bt_address_t *)&device_address, atoi(passkey));
1791 ret = device_reject_passkey_entry((bt_address_t *)&device_address);
1792 trigger_pairing_info->is_ssp = FALSE;
1795 ret = device_accept_pin_request((bt_address_t *)&device_address, passkey);
1797 ret = device_reject_pin_request((bt_address_t *)&device_address);
1800 __bt_free_pairing_info(&trigger_pairing_info);
1802 if (ret != OAL_STATUS_SUCCESS) {
1803 BT_ERR("_bt_device_handle_passkey_reply: err [%d]", ret);
1804 return BLUETOOTH_ERROR_INTERNAL;
1808 return BLUETOOTH_ERROR_NONE;
1811 int _bt_passkey_confirmation_reply(gboolean cnfm_reply)
1813 BT_INFO("BT_PASSKEY_CONFIRMATION_REPLY");
1814 bluetooth_device_address_t device_address;
1815 int ret = OAL_STATUS_SUCCESS;
1816 BT_INFO("reply: %d", cnfm_reply);
1818 retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1819 retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1821 _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1823 ret = device_reply_passkey_confirmation((bt_address_t *)&device_address, cnfm_reply);
1825 __bt_free_pairing_info(&trigger_pairing_info);
1826 if (ret != OAL_STATUS_SUCCESS) {
1827 BT_ERR("_bt_device_handle_passkey_confirmation_reply: err [%d]", ret);
1828 return BLUETOOTH_ERROR_INTERNAL;
1832 return BLUETOOTH_ERROR_NONE;
1835 gboolean _bt_device_is_pairing(void)
1837 return (trigger_pairing_info) ? TRUE : FALSE;
1840 gboolean _bt_device_is_bonding(void)
1842 return (trigger_bond_info) ? TRUE : FALSE;
1845 gboolean _bt_is_bonding_device_address(const char *address)
1847 if (trigger_bond_info == NULL || trigger_bond_info->addr == NULL)
1850 if (g_strcmp0(trigger_bond_info->addr, address) == 0) {
1851 BT_DBG("[%s] is bonding device", address);
1855 BT_DBG("[%s] is NOT bonding device", address);
1859 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
1861 ret_if(trigger_bond_info == NULL);
1862 trigger_bond_info->is_autopair = is_autopair;
1865 int _bt_search_device(bluetooth_device_address_t *device_address)
1867 int result = OAL_STATUS_SUCCESS;
1870 BT_CHECK_PARAMETER(device_address, return);
1872 if (trigger_bond_info) {
1873 BT_ERR("Bonding in progress");
1874 return BLUETOOTH_ERROR_DEVICE_BUSY;
1877 if (service_search_info) {
1878 BT_ERR("Service searching in progress");
1879 return BLUETOOTH_ERROR_DEVICE_BUSY;
1882 /* allocate user data so that it can be retrieved in callback */
1883 service_search_info = g_malloc0(sizeof(bt_service_search_info_data_t));
1884 service_search_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1885 service_search_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1887 _bt_convert_addr_type_to_string(service_search_info->addr,
1888 device_address->addr);
1890 result = device_query_services((bt_address_t *)device_address);
1892 if (result != OAL_STATUS_SUCCESS) {
1893 BT_ERR("Device Service Search Failed..: %d", result);
1894 __bt_free_service_search_info(&service_search_info);
1895 return BLUETOOTH_ERROR_INTERNAL;
1897 return BLUETOOTH_ERROR_NONE;
1900 int _bt_cancel_search_device(void)
1902 int ret = OAL_STATUS_SUCCESS;
1903 retv_if(service_search_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1905 ret = device_stop_query_sevices((bt_address_t *)service_search_info->dev_addr);
1907 if (ret != OAL_STATUS_SUCCESS) {
1908 BT_ERR("SDP Cancel request failed [%d]", ret);
1909 return BLUETOOTH_ERROR_INTERNAL;
1912 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_CANCEL_BY_USER, BT_SEARCH_SERVICE,
1913 service_search_info->addr, BT_ADDRESS_STRING_SIZE);
1915 __bt_free_service_search_info(&service_search_info);
1917 return BLUETOOTH_ERROR_NONE;
1921 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1924 int ret = OAL_STATUS_SUCCESS;
1927 BT_CHECK_PARAMETER(device_address, return);
1928 BT_INFO("Device to be Trusted? [%d]", authorize);
1930 ret = device_set_authorized((bt_address_t*)device_address, authorize);
1931 if (ret != OAL_STATUS_SUCCESS) {
1932 BT_ERR("device_set_authorized: %d", ret);
1933 return BLUETOOTH_ERROR_INTERNAL;
1936 return BLUETOOTH_ERROR_NONE;
1939 gboolean _bt_is_device_connected(bluetooth_device_address_t *device_address, int svc_type)
1941 gboolean is_connected;
1942 oal_service_t svc_id;
1944 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1947 * TODO: While adding support for new profiles, need to add more
1948 * <svc_type, svc_id> mapping here.
1951 case BT_PROFILE_CONN_HID:
1952 svc_id = HID_SERVICE_ID;
1954 case BT_PROFILE_CONN_A2DP:
1955 svc_id = A2DP_SERVICE_ID; /* Remote is A2DP Sink */
1957 case BT_PROFILE_CONN_A2DP_SINK:
1958 svc_id = A2DP_SRC_SERVICE_ID; /* Remote is A2DP Source*/
1960 case BT_PROFILE_CONN_HSP:
1961 svc_id = HFP_HS_SERVICE_ID; /* Remote is HFP HF Unit */
1963 #ifdef TIZEN_GATT_CLIENT
1964 case BT_PROFILE_CONN_GATT:
1965 return _bt_is_remote_gatt_device_connected(device_address); /* Remote is GATT client or Server */
1968 BT_DBG("Unknown svc_type: %d", svc_type);
1972 is_connected = device_get_svc_conn_state((bt_address_t*)device_address, svc_id);
1974 BT_DBG("svc_type: %d, is_connected: %s",
1975 svc_type, is_connected ? "TRUE" : "FALSE");
1977 return is_connected;
1980 int _bt_rfcomm_reply_conn_authorization(char *address, gboolean reply)
1982 bt_address_t bd_addr;
1987 retv_if(NULL == address, BLUETOOTH_ERROR_INVALID_PARAM);
1988 _bt_convert_addr_string_to_type(bd_addr.addr, address);
1989 res = device_reply_auth_request(&bd_addr, 0, reply, FALSE);
1990 if (res != OAL_STATUS_SUCCESS) {
1991 BT_ERR("authorize_response: %d", res);
1992 return BLUETOOTH_ERROR_INTERNAL;
1996 return BLUETOOTH_ERROR_NONE;
1999 int _bt_enable_rssi(bluetooth_device_address_t *addr, int link_type,
2000 int low_threshold, int in_range_threshold, int high_threshold)
2003 bt_address_t bd_addr;
2007 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2009 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2010 result = device_enable_rssi_monitoring(&bd_addr, link_type,
2011 low_threshold, in_range_threshold, high_threshold);
2012 if (result != OAL_STATUS_SUCCESS) {
2013 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2014 return BLUETOOTH_ERROR_INTERNAL;
2018 return BLUETOOTH_ERROR_NONE;
2021 int _bt_get_rssi_strength(bluetooth_device_address_t *addr, int link_type)
2024 bt_address_t bd_addr;
2028 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2030 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2031 result = device_get_connected_link_rssi_strength(&bd_addr, link_type);
2032 if (result != OAL_STATUS_SUCCESS) {
2033 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2034 return BLUETOOTH_ERROR_INTERNAL;
2038 return BLUETOOTH_ERROR_NONE;
2041 int _bt_set_passkey_notification(const char *sender, gboolean enable)
2045 BT_INFO("Set passkey notification(sender:%s, %s)",
2046 sender, enable ? "Enable" : "Disable");
2048 result = device_enable_gap_auth_notifications(OAL_PASSKEY_DISPLAY, enable);
2049 if (OAL_STATUS_SUCCESS != result) {
2050 BT_ERR("device_enable_gap_auth_notifications error: [%d]", result);
2051 return BLUETOOTH_ERROR_INTERNAL;
2054 g_free(passkey_watcher);
2056 passkey_watcher = g_strdup(sender);
2058 passkey_watcher = NULL;
2060 return BLUETOOTH_ERROR_NONE;
2063 static int __bt_get_device_pin_code(const char *address, char *pin_code)
2067 BT_CHECK_PARAMETER(address, return);
2068 BT_CHECK_PARAMETER(pin_code, return);
2070 for (l = pin_info_list; l != NULL; l = l->next) {
2071 bt_pin_code_info_t *pin_info = l->data;
2073 if (!pin_info || !pin_info->address)
2076 if (g_strcmp0(pin_info->address, address) == 0) {
2077 g_strlcpy(pin_code, pin_info->pin_code,
2078 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2079 return BLUETOOTH_ERROR_NONE;
2083 return BLUETOOTH_ERROR_NOT_FOUND;
2086 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2087 bluetooth_device_pin_code_t *pin_code)
2089 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2090 bt_pin_code_info_t *pin_info = NULL;
2093 BT_CHECK_PARAMETER(device_address, return);
2094 BT_CHECK_PARAMETER(pin_code, return);
2095 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2096 BLUETOOTH_ERROR_NO_RESOURCES);
2098 _bt_convert_addr_type_to_string(address, device_address->addr);
2100 for (l = pin_info_list; l != NULL; l = l->next) {
2103 if (!pin_info || !pin_info->address)
2106 if (g_strcmp0(pin_info->address, address) == 0) {
2107 g_free(pin_info->pin_code);
2108 pin_info->pin_code = g_strdup(pin_code->pin_code);
2109 return BLUETOOTH_ERROR_NONE;
2113 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2114 pin_info->address = g_strdup(address);
2115 pin_info->pin_code = g_strdup(pin_code->pin_code);
2116 pin_info_list = g_slist_append(pin_info_list, pin_info);
2118 return BLUETOOTH_ERROR_NONE;
2121 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2123 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2124 bt_pin_code_info_t *pin_info = NULL;
2129 BT_CHECK_PARAMETER(device_address, return);
2131 _bt_convert_addr_type_to_string(address, device_address->addr);
2133 for (l = pin_info_list; l != NULL; l = l->next) {
2136 if (!pin_info || !pin_info->address)
2139 if (g_strcmp0(pin_info->address, address) == 0) {
2140 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2141 g_free(pin_info->address);
2142 g_free(pin_info->pin_code);
2149 return BLUETOOTH_ERROR_NONE;
2152 #ifdef TIZEN_GATT_CLIENT
2153 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2156 bt_connected_le_dev_t *dev;
2161 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2164 if (g_strcmp0(dev->address, address) == 0)
2170 static void __bt_le_conn_param_free(void *data)
2172 bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2174 BT_DBG("%s", param->sender);
2175 g_free(param->sender);
2179 static void _bt_add_le_connected_dev_info(const char *address)
2181 bt_connected_le_dev_t *dev = NULL;
2186 dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2187 dev->address = g_strdup(address);
2189 le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2194 static void _bt_remove_le_connected_dev_info(const char *address)
2196 bt_connected_le_dev_t *dev = NULL;
2201 dev = __bt_get_le_connected_dev_info(address);
2205 g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2206 le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2207 g_free(dev->address);
2214 static void _bt_handle_le_connected_dev_info(const char *address, gboolean connected)
2219 _bt_add_le_connected_dev_info(address);
2221 _bt_remove_le_connected_dev_info(address);
2224 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2227 bt_le_conn_param_t *param = NULL;
2229 if (!dev || !sender)
2232 for (l = dev->senders; l; l = g_slist_next(l)) {
2234 if (g_strcmp0(param->sender, sender) == 0)
2241 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2243 bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2244 bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2246 return parama->key > paramb->key;
2250 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2251 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2253 bt_connected_le_dev_t *dev = NULL;
2254 bt_le_conn_param_t *param = NULL;
2255 bt_le_conn_param_t *data = NULL;
2259 if (!address || !sender)
2260 return BLUETOOTH_ERROR_INVALID_PARAM;
2262 dev = __bt_get_le_connected_dev_info(address);
2264 return BLUETOOTH_ERROR_INTERNAL;
2266 param = __bt_get_le_conn_param_info(dev, sender);
2268 data = g_malloc0(sizeof(bt_le_conn_param_t));
2269 data->sender = g_strdup(sender);
2270 data->interval_min = interval_min;
2271 data->interval_max = interval_max;
2272 data->latency = latency;
2273 data->time_out = time_out;
2274 data->key = interval_min + (interval_max - interval_min)/2;
2276 if (param == NULL) {
2277 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2278 dev->senders = g_slist_append(dev->senders, data);
2280 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2281 dev->senders = g_slist_remove(dev->senders, param);
2282 g_free(param->sender);
2284 dev->senders = g_slist_append(dev->senders, data);
2287 /* Sorting. First element have the minimum interval */
2288 dev->senders = g_slist_sort(dev->senders,
2289 (GCompareFunc)__bt_compare_le_conn_param_key);
2291 return BLUETOOTH_ERROR_NONE;
2295 static int __bt_le_set_conn_parameter(const char *address,
2296 float interval_min, float interval_max,
2297 guint16 latency, guint16 time_out)
2299 bt_address_t dev_addr = { {0} };
2300 guint32 min, max, to;
2302 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2303 interval_min, interval_max, latency, time_out);
2305 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2306 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2307 to = time_out / BT_LE_CONN_TO_SPLIT;
2309 BT_INFO("updating: Min interval: %d, Max interval: %d, Latency: %d, Supervision timeout: %d",
2310 min, max, latency, to);
2312 _bt_convert_addr_string_to_type(dev_addr.addr, address);
2314 return gattc_conn_param_update(&dev_addr, min, max, latency, to);
2317 int _bt_remove_le_conn_param_info(const char *address, const char *sender)
2319 bt_connected_le_dev_t *dev = NULL;
2320 bt_le_conn_param_t *param = NULL;
2322 if (!address || !sender)
2323 return BLUETOOTH_ERROR_INVALID_PARAM;
2325 dev = __bt_get_le_connected_dev_info(address);
2327 return BLUETOOTH_ERROR_INTERNAL;
2329 param = __bt_get_le_conn_param_info(dev, sender);
2331 BT_DBG("Remove param %s %s ", address, sender);
2332 dev->senders = g_slist_remove(dev->senders, param);
2333 g_free(param->sender);
2337 return BLUETOOTH_ERROR_NONE;
2341 int _bt_le_connection_update(const char *sender,
2342 unsigned char *device_address,
2343 float interval_min, float interval_max,
2344 guint16 latency, guint16 time_out)
2346 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2347 guint32 min_supervision_to;
2348 bt_connected_le_dev_t *dev = NULL;
2349 bt_le_conn_param_t *param = NULL;
2350 int ret = BLUETOOTH_ERROR_NONE;
2352 BT_CHECK_PARAMETER(device_address, return);
2354 BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2355 sender, interval_min, interval_max, latency, time_out);
2357 if (interval_min > interval_max ||
2358 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2359 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2360 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2364 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2365 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2366 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2370 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2371 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2376 * The Supervision_Timeout in milliseconds shall be larger than
2377 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2378 * where Conn_Interval_Max is given in milliseconds.
2381 min_supervision_to = (1 + latency) * interval_max * 2;
2382 if (time_out <= min_supervision_to) {
2383 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2387 _bt_convert_addr_type_to_string(address, device_address);
2388 BT_DBG("Remote device address: %s", address);
2390 _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2392 dev = __bt_get_le_connected_dev_info(address);
2394 BT_DBG("device not found in the list");
2395 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2399 if (g_slist_length(dev->senders) == 1)
2402 param = dev->senders->data;
2404 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2406 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2407 BT_DBG("Skip due to same interval");
2411 interval_min = param->interval_min;
2412 interval_max = param->interval_max;
2416 ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2418 if (ret != OAL_STATUS_SUCCESS) {
2419 _bt_remove_le_conn_param_info(address, sender);
2420 BT_DBG("fail to update the LE connection parameter");
2421 ret = BLUETOOTH_ERROR_INTERNAL;
2425 BT_DBG("updated LE connection parameter");
2426 dev->interval_min = interval_min;
2427 dev->interval_max = interval_max;
2429 return BLUETOOTH_ERROR_NONE;