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);
1358 param = g_variant_new("(isy)", result, address, type);
1359 _bt_send_event(BT_DEVICE_EVENT,
1360 BLUETOOTH_EVENT_DEVICE_CONNECTED,
1363 param = g_variant_new("(isy)", result, address, type);
1364 _bt_send_event(BT_DEVICE_EVENT,
1365 BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1369 conn_info.connected = connected;
1370 conn_info.type = type;
1371 /* Update local cache */
1372 _bt_update_remote_dev_property(address, DEV_PROP_CONNECTED, (void *)&conn_info);
1374 #ifdef TIZEN_GATT_CLIENT
1375 /*handle LE connected device info*/
1377 BT_DBG("handle LE connected device info");
1378 _bt_handle_le_connected_dev_info(address, connected);
1385 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble)
1387 bt_remote_dev_info_t *dev_info = NULL;
1388 int result = BLUETOOTH_ERROR_NONE;
1389 GVariant *param = NULL;
1390 GVariant *uuids = NULL;
1391 GVariant *manufacturer_data = NULL;
1392 GVariantBuilder *builder = NULL;
1396 ret_if(_bt_is_discovering() == FALSE);
1397 ret_if(event_data == NULL);
1399 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
1402 event_ble_dev_found_t * oal_ble_dev = event_data;
1403 BT_INFO("Device type [%d]", oal_ble_dev->device_info.type);
1405 _bt_copy_remote_dev(dev_info, &oal_ble_dev->device_info);
1407 dev_info->manufacturer_data_len = oal_ble_dev->adv_len;
1408 if (dev_info->manufacturer_data_len)
1409 dev_info->manufacturer_data = g_memdup(oal_ble_dev->adv_data,
1410 dev_info->manufacturer_data_len);
1412 dev_info->manufacturer_data = NULL;
1413 BT_DBG("----Advertising Data Length: %d", dev_info->manufacturer_data_len);
1415 event_dev_found_t * oal_dev = event_data;
1416 _bt_copy_remote_dev(dev_info, &oal_dev->device_info);
1419 /* If Remote device name is NULL or still RNR is not done then display address as name. */
1420 if (dev_info->name == NULL)
1421 dev_info->name = g_strdup(dev_info->address);
1422 BT_DBG("Name %s", dev_info->name);
1424 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1425 for (i = 0; i < dev_info->uuid_count; i++)
1426 g_variant_builder_add(builder, "s", dev_info->uuids[i]);
1428 uuids = g_variant_new("as", builder);
1429 g_variant_builder_unref(builder);
1431 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1432 dev_info->manufacturer_data,
1433 dev_info->manufacturer_data_len,
1437 param = g_variant_new("(isunsbub@asn@ay)", result,
1443 dev_info->connected,
1446 dev_info->manufacturer_data_len,
1449 _bt_send_event(BT_ADAPTER_EVENT,
1450 BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
1453 _bt_free_remote_dev(dev_info);
1457 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info)
1459 gchar address[BT_ADDRESS_STR_LEN];
1460 int result = BLUETOOTH_ERROR_NONE;
1461 GVariant *param = NULL;
1465 _bt_convert_addr_type_to_string(address, info->address.addr);
1467 /* Update local cache */
1468 _bt_update_remote_dev_property(address, DEV_PROP_TRUST, (void *)&trusted);
1470 param = g_variant_new("(is)", result, address);
1471 event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1472 BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1473 /* Send event to application */
1474 _bt_send_event(BT_DEVICE_EVENT,
1481 static void __bt_free_pairing_info(bt_pairing_data_t **p_info)
1484 bt_pairing_data_t * info = *p_info;
1495 static void __bt_free_bond_info(uint8_t type)
1500 case BT_DEVICE_BOND_INFO:
1501 if (!trigger_bond_info)
1504 if (trigger_bond_info->addr)
1505 g_free(trigger_bond_info->addr);
1506 if (trigger_bond_info->dev_addr)
1507 g_free(trigger_bond_info->dev_addr);
1508 if (trigger_bond_info->dev_info)
1509 _bt_free_remote_dev(trigger_bond_info->dev_info);
1510 g_free(trigger_bond_info);
1511 trigger_bond_info = NULL;
1513 case BT_DEVICE_INCOMING_BOND_INFO:
1514 if (!incoming_bond_info)
1517 if (incoming_bond_info->dev_info)
1518 _bt_free_remote_dev(incoming_bond_info->dev_info);
1519 g_free(incoming_bond_info);
1520 incoming_bond_info = NULL;
1522 case BT_DEVICE_UNBOND_INFO:
1523 if (!trigger_unbond_info)
1526 if (trigger_unbond_info->addr)
1527 g_free(trigger_unbond_info->addr);
1528 if (trigger_unbond_info->dev_addr)
1529 g_free(trigger_unbond_info->dev_addr);
1530 if (trigger_unbond_info->dev_info)
1531 _bt_free_remote_dev(trigger_unbond_info->dev_info);
1532 g_free(trigger_unbond_info);
1533 trigger_unbond_info = NULL;
1539 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info)
1541 bt_service_search_info_data_t * info = *p_info;
1548 if (info->dev_addr) {
1549 g_free(info->dev_addr);
1550 info->dev_addr = NULL;
1553 if (info->dev_info) {
1554 _bt_free_remote_dev(info->dev_info);
1555 info->dev_info = NULL;
1563 static int __bt_device_handle_bond_state(void)
1565 BT_INFO("Current Bond state: %d", bt_device_bond_state);
1566 int ret = OAL_STATUS_INTERNAL_ERROR;
1568 switch (bt_device_bond_state) {
1569 case BT_DEVICE_BOND_STATE_CANCEL_DISCOVERY:
1570 /*TODO:Bonding during discovery: Unhandled!!*/
1571 BT_INFO("Bonding during discovery: Unhandled!!");
1573 case BT_DEVICE_BOND_STATE_DISCOVERY_CANCELLED:
1574 /*TODO:Bonding during discovery: Unhandled!!*/
1575 BT_INFO("Bonding during discovery: Unhandled!!");
1577 case BT_DEVICE_BOND_STATE_REMOVE_BONDING:
1578 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVED_BONDING;
1579 ret = device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
1580 if (ret != OAL_STATUS_SUCCESS)
1581 ret = __bt_device_handle_bond_state();
1583 case BT_DEVICE_BOND_STATE_REMOVED_BONDING:
1584 bt_device_bond_state = BT_DEVICE_BOND_STATE_NONE;
1585 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
1586 /* Bonding procedure was started but unfortunately could not complete.
1587 Basically removed bonding was success, but create bond request could not proceed
1588 So lets cleanup the context */
1589 if (ret != OAL_STATUS_SUCCESS) {
1590 BT_ERR("Create Bond procedure could not suceed");
1591 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1592 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1593 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1594 __bt_free_pairing_info(&trigger_pairing_info);
1597 case BT_DEVICE_BOND_STATE_NONE:
1598 BT_INFO("Create Bond failed!!");
1599 if (trigger_bond_info) {
1600 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1601 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1602 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1603 __bt_free_pairing_info(&trigger_pairing_info);
1610 if (ret != OAL_STATUS_SUCCESS)
1611 return BLUETOOTH_ERROR_INTERNAL;
1613 return BLUETOOTH_ERROR_NONE;
1616 int _bt_device_get_bonded_device_info(bluetooth_device_address_t *addr)
1619 bt_address_t bd_addr;
1623 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
1625 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
1626 result = device_query_attributes(&bd_addr);
1627 if (result != OAL_STATUS_SUCCESS) {
1628 BT_ERR("device_query_attributes error: [%d]", result);
1629 return BLUETOOTH_ERROR_INTERNAL;
1633 return BLUETOOTH_ERROR_NONE;
1636 int _bt_set_alias(bluetooth_device_address_t *device_address, const char *alias)
1639 char address[BT_ADDRESS_STRING_SIZE];
1642 BT_CHECK_PARAMETER(alias, return);
1644 ret = device_set_alias((bt_address_t *)device_address, (char *)alias);
1645 if (ret != OAL_STATUS_SUCCESS) {
1646 BT_ERR("device_set_alias: %d", ret);
1647 return BLUETOOTH_ERROR_INTERNAL;
1650 /* Update local cache */
1651 _bt_convert_addr_type_to_string(address, device_address->addr);
1652 _bt_update_remote_dev_property(address, DEV_PROP_ALIAS, (void *)alias);
1655 return BLUETOOTH_ERROR_NONE;
1658 int _bt_bond_device(bluetooth_device_address_t *device_address,
1659 unsigned short conn_type, GArray **out_param1)
1661 int result = BLUETOOTH_ERROR_NONE;
1662 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1663 bluetooth_device_info_t dev_info;
1666 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1668 /* If bonding or discovery already going on */
1669 if (trigger_bond_info || _bt_is_discovering()) {
1670 BT_ERR("Device is buzy, bonding can not proceed now..");
1671 result = BLUETOOTH_ERROR_DEVICE_BUSY;
1675 /*TODO: If unbonding with same device going on */
1676 _bt_convert_addr_type_to_string(address, device_address->addr);
1678 trigger_bond_info = g_malloc0(sizeof(bt_bond_data_t));
1679 trigger_bond_info->addr = g_strdup(address);
1680 trigger_bond_info->conn_type = conn_type;
1681 trigger_bond_info->is_device_creating = TRUE;
1682 trigger_bond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1683 trigger_bond_info->dev_info = NULL;
1685 /* Ready to initiate bonding */
1687 /* In Tizen, we will first remove bond and then attempt to create bond to keep
1688 consistency with bluedroid. Even if remove bond fails due to device not already
1689 bonded, then straight away create bond is triggered. This is because, remove bond
1690 is handled differently in bluedroid and bluez. In Bluez, if device is
1691 already removed, remove bond call fails.
1692 However in bluedroid, remove bond on already removed device returns success. So we will
1693 handle the cases transparently*/
1694 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVE_BONDING;
1695 bond_retry_count = 0;
1696 result = __bt_device_handle_bond_state();
1698 if (result != BLUETOOTH_ERROR_NONE)
1705 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1706 memcpy(dev_info.device_address.addr, device_address->addr,
1707 BLUETOOTH_ADDRESS_LENGTH);
1709 g_array_append_vals(*out_param1, &dev_info,
1710 sizeof(bluetooth_device_info_t));
1711 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1717 int _bt_unbond_device(bluetooth_device_address_t *device_address,
1718 GArray **out_param1)
1720 int result = OAL_STATUS_SUCCESS;
1721 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1722 bluetooth_device_info_t dev_info;
1725 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1727 _bt_convert_addr_type_to_string(address, device_address->addr);
1729 trigger_unbond_info = g_malloc0(sizeof(bt_bond_data_t));
1730 trigger_unbond_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1731 trigger_unbond_info->addr = g_strdup(address);
1732 trigger_unbond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1734 /* Check if Bonding is already going on, we should not abruptly remove bonding*/
1735 if (trigger_bond_info && strncmp(trigger_bond_info->addr, trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE) == 0) {
1736 BT_ERR("Bonding with same device already ongoing");
1737 result = BLUETOOTH_ERROR_PERMISSION_DEINED;
1741 result = device_destroy_bond((bt_address_t *)device_address);
1742 if (result != OAL_STATUS_SUCCESS)
1748 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1749 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1750 trigger_unbond_info->addr);
1752 g_array_append_vals(*out_param1, &dev_info,
1753 sizeof(bluetooth_device_info_t));
1754 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
1759 int _bt_cancel_bonding(void)
1761 int result = OAL_STATUS_SUCCESS;
1764 retv_if(trigger_bond_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1766 result = device_stop_bond((bt_address_t *)trigger_bond_info->dev_addr);
1768 if (result == OAL_STATUS_SUCCESS)
1769 trigger_bond_info->is_cancelled_by_user = TRUE;
1774 int _bt_passkey_reply(const char *passkey, gboolean cnfm_reply)
1776 bluetooth_device_address_t device_address;
1777 int ret = OAL_STATUS_SUCCESS;
1778 BT_INFO("reply: %d", cnfm_reply);
1780 retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1781 retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1783 _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1785 if (trigger_pairing_info->is_ssp) {
1787 ret = device_accept_passkey_entry((bt_address_t *)&device_address, atoi(passkey));
1789 ret = device_reject_passkey_entry((bt_address_t *)&device_address);
1790 trigger_pairing_info->is_ssp = FALSE;
1793 ret = device_accept_pin_request((bt_address_t *)&device_address, passkey);
1795 ret = device_reject_pin_request((bt_address_t *)&device_address);
1798 __bt_free_pairing_info(&trigger_pairing_info);
1800 if (ret != OAL_STATUS_SUCCESS) {
1801 BT_ERR("_bt_device_handle_passkey_reply: err [%d]", ret);
1802 return BLUETOOTH_ERROR_INTERNAL;
1806 return BLUETOOTH_ERROR_NONE;
1809 int _bt_passkey_confirmation_reply(gboolean cnfm_reply)
1811 BT_INFO("BT_PASSKEY_CONFIRMATION_REPLY");
1812 bluetooth_device_address_t device_address;
1813 int ret = OAL_STATUS_SUCCESS;
1814 BT_INFO("reply: %d", cnfm_reply);
1816 retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1817 retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1819 _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1821 ret = device_reply_passkey_confirmation((bt_address_t *)&device_address, cnfm_reply);
1823 __bt_free_pairing_info(&trigger_pairing_info);
1824 if (ret != OAL_STATUS_SUCCESS) {
1825 BT_ERR("_bt_device_handle_passkey_confirmation_reply: err [%d]", ret);
1826 return BLUETOOTH_ERROR_INTERNAL;
1830 return BLUETOOTH_ERROR_NONE;
1833 gboolean _bt_device_is_pairing(void)
1835 return (trigger_pairing_info) ? TRUE : FALSE;
1838 gboolean _bt_device_is_bonding(void)
1840 return (trigger_bond_info) ? TRUE : FALSE;
1843 gboolean _bt_is_bonding_device_address(const char *address)
1845 if (trigger_bond_info == NULL || trigger_bond_info->addr == NULL)
1848 if (g_strcmp0(trigger_bond_info->addr, address) == 0) {
1849 BT_DBG("[%s] is bonding device", address);
1853 BT_DBG("[%s] is NOT bonding device", address);
1857 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
1859 ret_if(trigger_bond_info == NULL);
1860 trigger_bond_info->is_autopair = is_autopair;
1863 int _bt_search_device(bluetooth_device_address_t *device_address)
1865 int result = OAL_STATUS_SUCCESS;
1868 BT_CHECK_PARAMETER(device_address, return);
1870 if (trigger_bond_info) {
1871 BT_ERR("Bonding in progress");
1872 return BLUETOOTH_ERROR_DEVICE_BUSY;
1875 if (service_search_info) {
1876 BT_ERR("Service searching in progress");
1877 return BLUETOOTH_ERROR_DEVICE_BUSY;
1880 /* allocate user data so that it can be retrieved in callback */
1881 service_search_info = g_malloc0(sizeof(bt_service_search_info_data_t));
1882 service_search_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1883 service_search_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1885 _bt_convert_addr_type_to_string(service_search_info->addr,
1886 device_address->addr);
1888 result = device_query_services((bt_address_t *)device_address);
1890 if (result != OAL_STATUS_SUCCESS) {
1891 BT_ERR("Device Service Search Failed..: %d", result);
1892 __bt_free_service_search_info(&service_search_info);
1893 return BLUETOOTH_ERROR_INTERNAL;
1895 return BLUETOOTH_ERROR_NONE;
1898 int _bt_cancel_search_device(void)
1900 int ret = OAL_STATUS_SUCCESS;
1901 retv_if(service_search_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1903 ret = device_stop_query_sevices((bt_address_t *)service_search_info->dev_addr);
1905 if (ret != OAL_STATUS_SUCCESS) {
1906 BT_ERR("SDP Cancel request failed [%d]", ret);
1907 return BLUETOOTH_ERROR_INTERNAL;
1910 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_CANCEL_BY_USER, BT_SEARCH_SERVICE,
1911 service_search_info->addr, BT_ADDRESS_STRING_SIZE);
1913 __bt_free_service_search_info(&service_search_info);
1915 return BLUETOOTH_ERROR_NONE;
1919 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1922 int ret = OAL_STATUS_SUCCESS;
1925 BT_CHECK_PARAMETER(device_address, return);
1926 BT_INFO("Device to be Trusted? [%d]", authorize);
1928 ret = device_set_authorized((bt_address_t*)device_address, authorize);
1929 if (ret != OAL_STATUS_SUCCESS) {
1930 BT_ERR("device_set_authorized: %d", ret);
1931 return BLUETOOTH_ERROR_INTERNAL;
1934 return BLUETOOTH_ERROR_NONE;
1937 gboolean _bt_is_device_connected(bluetooth_device_address_t *device_address, int svc_type)
1939 gboolean is_connected;
1940 oal_service_t svc_id;
1942 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1945 * TODO: While adding support for new profiles, need to add more
1946 * <svc_type, svc_id> mapping here.
1949 case BT_PROFILE_CONN_HID:
1950 svc_id = HID_SERVICE_ID;
1952 case BT_PROFILE_CONN_A2DP:
1953 svc_id = A2DP_SERVICE_ID; /* Remote is A2DP Sink */
1955 case BT_PROFILE_CONN_A2DP_SINK:
1956 svc_id = A2DP_SRC_SERVICE_ID; /* Remote is A2DP Source*/
1958 case BT_PROFILE_CONN_HSP:
1959 svc_id = HFP_HS_SERVICE_ID; /* Remote is HFP HF Unit */
1961 #ifdef TIZEN_GATT_CLIENT
1962 case BT_PROFILE_CONN_GATT:
1963 return _bt_is_remote_gatt_device_connected(device_address); /* Remote is GATT client or Server */
1966 BT_DBG("Unknown svc_type: %d", svc_type);
1970 is_connected = device_get_svc_conn_state((bt_address_t*)device_address, svc_id);
1972 BT_DBG("svc_type: %d, is_connected: %s",
1973 svc_type, is_connected ? "TRUE" : "FALSE");
1975 return is_connected;
1978 int _bt_rfcomm_reply_conn_authorization(char *address, gboolean reply)
1980 bt_address_t bd_addr;
1985 retv_if(NULL == address, BLUETOOTH_ERROR_INVALID_PARAM);
1986 _bt_convert_addr_string_to_type(bd_addr.addr, address);
1987 res = device_reply_auth_request(&bd_addr, 0, reply, FALSE);
1988 if (res != OAL_STATUS_SUCCESS) {
1989 BT_ERR("authorize_response: %d", res);
1990 return BLUETOOTH_ERROR_INTERNAL;
1994 return BLUETOOTH_ERROR_NONE;
1997 int _bt_enable_rssi(bluetooth_device_address_t *addr, int link_type,
1998 int low_threshold, int in_range_threshold, int high_threshold)
2001 bt_address_t bd_addr;
2005 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2007 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2008 result = device_enable_rssi_monitoring(&bd_addr, link_type,
2009 low_threshold, in_range_threshold, high_threshold);
2010 if (result != OAL_STATUS_SUCCESS) {
2011 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2012 return BLUETOOTH_ERROR_INTERNAL;
2016 return BLUETOOTH_ERROR_NONE;
2019 int _bt_get_rssi_strength(bluetooth_device_address_t *addr, int link_type)
2022 bt_address_t bd_addr;
2026 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2028 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2029 result = device_get_connected_link_rssi_strength(&bd_addr, link_type);
2030 if (result != OAL_STATUS_SUCCESS) {
2031 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2032 return BLUETOOTH_ERROR_INTERNAL;
2036 return BLUETOOTH_ERROR_NONE;
2039 int _bt_set_passkey_notification(const char *sender, gboolean enable)
2043 BT_INFO("Set passkey notification(sender:%s, %s)",
2044 sender, enable ? "Enable" : "Disable");
2046 result = device_enable_gap_auth_notifications(OAL_PASSKEY_DISPLAY, enable);
2047 if (OAL_STATUS_SUCCESS != result) {
2048 BT_ERR("device_enable_gap_auth_notifications error: [%d]", result);
2049 return BLUETOOTH_ERROR_INTERNAL;
2052 g_free(passkey_watcher);
2054 passkey_watcher = g_strdup(sender);
2056 passkey_watcher = NULL;
2058 return BLUETOOTH_ERROR_NONE;
2061 static int __bt_get_device_pin_code(const char *address, char *pin_code)
2065 BT_CHECK_PARAMETER(address, return);
2066 BT_CHECK_PARAMETER(pin_code, return);
2068 for (l = pin_info_list; l != NULL; l = l->next) {
2069 bt_pin_code_info_t *pin_info = l->data;
2071 if (!pin_info || !pin_info->address)
2074 if (g_strcmp0(pin_info->address, address) == 0) {
2075 g_strlcpy(pin_code, pin_info->pin_code,
2076 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2077 return BLUETOOTH_ERROR_NONE;
2081 return BLUETOOTH_ERROR_NOT_FOUND;
2084 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2085 bluetooth_device_pin_code_t *pin_code)
2087 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2088 bt_pin_code_info_t *pin_info = NULL;
2091 BT_CHECK_PARAMETER(device_address, return);
2092 BT_CHECK_PARAMETER(pin_code, return);
2093 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2094 BLUETOOTH_ERROR_NO_RESOURCES);
2096 _bt_convert_addr_type_to_string(address, device_address->addr);
2098 for (l = pin_info_list; l != NULL; l = l->next) {
2101 if (!pin_info || !pin_info->address)
2104 if (g_strcmp0(pin_info->address, address) == 0) {
2105 g_free(pin_info->pin_code);
2106 pin_info->pin_code = g_strdup(pin_code->pin_code);
2107 return BLUETOOTH_ERROR_NONE;
2111 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2112 pin_info->address = g_strdup(address);
2113 pin_info->pin_code = g_strdup(pin_code->pin_code);
2114 pin_info_list = g_slist_append(pin_info_list, pin_info);
2116 return BLUETOOTH_ERROR_NONE;
2119 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2121 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2122 bt_pin_code_info_t *pin_info = NULL;
2127 BT_CHECK_PARAMETER(device_address, return);
2129 _bt_convert_addr_type_to_string(address, device_address->addr);
2131 for (l = pin_info_list; l != NULL; l = l->next) {
2134 if (!pin_info || !pin_info->address)
2137 if (g_strcmp0(pin_info->address, address) == 0) {
2138 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2139 g_free(pin_info->address);
2140 g_free(pin_info->pin_code);
2147 return BLUETOOTH_ERROR_NONE;
2150 #ifdef TIZEN_GATT_CLIENT
2151 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2154 bt_connected_le_dev_t *dev;
2159 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2162 if (g_strcmp0(dev->address, address) == 0)
2168 static void __bt_le_conn_param_free(void *data)
2170 bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2172 BT_DBG("%s", param->sender);
2173 g_free(param->sender);
2177 static void _bt_add_le_connected_dev_info(const char *address)
2179 bt_connected_le_dev_t *dev = NULL;
2184 dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2185 dev->address = g_strdup(address);
2187 le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2192 static void _bt_remove_le_connected_dev_info(const char *address)
2194 bt_connected_le_dev_t *dev = NULL;
2199 dev = __bt_get_le_connected_dev_info(address);
2203 g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2204 le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2205 g_free(dev->address);
2212 static void _bt_handle_le_connected_dev_info(const char *address, gboolean connected)
2217 _bt_add_le_connected_dev_info(address);
2219 _bt_remove_le_connected_dev_info(address);
2222 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2225 bt_le_conn_param_t *param = NULL;
2227 if (!dev || !sender)
2230 for (l = dev->senders; l; l = g_slist_next(l)) {
2232 if (g_strcmp0(param->sender, sender) == 0)
2239 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2241 bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2242 bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2244 return parama->key > paramb->key;
2248 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2249 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2251 bt_connected_le_dev_t *dev = NULL;
2252 bt_le_conn_param_t *param = NULL;
2253 bt_le_conn_param_t *data = NULL;
2257 if (!address || !sender)
2258 return BLUETOOTH_ERROR_INVALID_PARAM;
2260 dev = __bt_get_le_connected_dev_info(address);
2262 return BLUETOOTH_ERROR_INTERNAL;
2264 param = __bt_get_le_conn_param_info(dev, sender);
2266 data = g_malloc0(sizeof(bt_le_conn_param_t));
2267 data->sender = g_strdup(sender);
2268 data->interval_min = interval_min;
2269 data->interval_max = interval_max;
2270 data->latency = latency;
2271 data->time_out = time_out;
2272 data->key = interval_min + (interval_max - interval_min)/2;
2274 if (param == NULL) {
2275 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2276 dev->senders = g_slist_append(dev->senders, data);
2278 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2279 dev->senders = g_slist_remove(dev->senders, param);
2280 g_free(param->sender);
2282 dev->senders = g_slist_append(dev->senders, data);
2285 /* Sorting. First element have the minimum interval */
2286 dev->senders = g_slist_sort(dev->senders,
2287 (GCompareFunc)__bt_compare_le_conn_param_key);
2289 return BLUETOOTH_ERROR_NONE;
2293 static int __bt_le_set_conn_parameter(const char *address,
2294 float interval_min, float interval_max,
2295 guint16 latency, guint16 time_out)
2297 bt_address_t dev_addr = { {0} };
2298 guint32 min, max, to;
2300 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2301 interval_min, interval_max, latency, time_out);
2303 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2304 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2305 to = time_out / BT_LE_CONN_TO_SPLIT;
2307 BT_INFO("updating: Min interval: %d, Max interval: %d, Latency: %d, Supervision timeout: %d",
2308 min, max, latency, to);
2310 _bt_convert_addr_string_to_type(dev_addr.addr, address);
2312 return gattc_conn_param_update(&dev_addr, min, max, latency, to);
2315 int _bt_remove_le_conn_param_info(const char *address, const char *sender)
2317 bt_connected_le_dev_t *dev = NULL;
2318 bt_le_conn_param_t *param = NULL;
2320 if (!address || !sender)
2321 return BLUETOOTH_ERROR_INVALID_PARAM;
2323 dev = __bt_get_le_connected_dev_info(address);
2325 return BLUETOOTH_ERROR_INTERNAL;
2327 param = __bt_get_le_conn_param_info(dev, sender);
2329 BT_DBG("Remove param %s %s ", address, sender);
2330 dev->senders = g_slist_remove(dev->senders, param);
2331 g_free(param->sender);
2335 return BLUETOOTH_ERROR_NONE;
2339 int _bt_le_connection_update(const char *sender,
2340 unsigned char *device_address,
2341 float interval_min, float interval_max,
2342 guint16 latency, guint16 time_out)
2344 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2345 guint32 min_supervision_to;
2346 bt_connected_le_dev_t *dev = NULL;
2347 bt_le_conn_param_t *param = NULL;
2348 int ret = BLUETOOTH_ERROR_NONE;
2350 BT_CHECK_PARAMETER(device_address, return);
2352 BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2353 sender, interval_min, interval_max, latency, time_out);
2355 if (interval_min > interval_max ||
2356 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2357 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2358 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2362 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2363 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2364 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2368 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2369 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2374 * The Supervision_Timeout in milliseconds shall be larger than
2375 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2376 * where Conn_Interval_Max is given in milliseconds.
2379 min_supervision_to = (1 + latency) * interval_max * 2;
2380 if (time_out <= min_supervision_to) {
2381 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2385 _bt_convert_addr_type_to_string(address, device_address);
2386 BT_DBG("Remote device address: %s", address);
2388 _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2390 dev = __bt_get_le_connected_dev_info(address);
2392 BT_DBG("device not found in the list");
2393 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2397 if (g_slist_length(dev->senders) == 1)
2400 param = dev->senders->data;
2402 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2404 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2405 BT_DBG("Skip due to same interval");
2409 interval_min = param->interval_min;
2410 interval_max = param->interval_max;
2414 ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2416 if (ret != OAL_STATUS_SUCCESS) {
2417 _bt_remove_le_conn_param_info(address, sender);
2418 BT_DBG("fail to update the LE connection parameter");
2419 ret = BLUETOOTH_ERROR_INTERNAL;
2423 BT_DBG("updated LE connection parameter");
2424 dev->interval_min = interval_min;
2425 dev->interval_max = interval_max;
2427 return BLUETOOTH_ERROR_NONE;