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;
604 if (_bt_obex_server_is_custom() == false) {
605 /* Allow the connection for native OPP server */
609 name = g_strdup(address);
611 BT_INFO("Incoming OPP conn Req from device addr [%s]", address);
612 _bt_obex_server_set_pending_conn_auth_device_addr(address);
613 param = g_variant_new("(iss)", BLUETOOTH_ERROR_NONE, address, name);
614 _bt_send_event(BT_OPP_SERVER_EVENT,
615 BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE, param);
621 BT_INFO("Incoming HSP_SERVICE_ID conn Req from device addr [%s]", address);
624 BT_INFO("Incoming HFP_SERVICE_ID conn Req from device addr [%s]", address);
627 BT_INFO("Incoming SAP_SERVICE_ID conn Req from device addr [%s]", address);
629 case HSP_HS_SERVICE_ID:
630 BT_INFO("Incoming HSP_HS_SERVICE_ID conn Req from device addr [%s]", address);
632 case HFP_HS_SERVICE_ID:
633 BT_INFO("Incoming HFP_HS_SERVICE_ID conn Req from device addr [%s]", address);
636 case IOTIVITY_SERVICE_ID:
637 BT_INFO("Incoming IOTIVITY_SERVICE_ID conn Req from device addr [%s]", address);
641 /* For now, reject authorization for any service apart from above switch cases */
642 BT_INFO("Incoming Profile conn req with service ID [%d] from device addr [%s]", service_d, address);
643 res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, FALSE, FALSE);
644 if (res != OAL_STATUS_SUCCESS)
645 BT_ERR("authorize_response: %d", res);
649 /* Auto accept authorization request for HID, A2DP and AVRCP profiles */
650 BT_INFO("Auto Accept authorization");
651 res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, TRUE, FALSE);
652 if (res != OAL_STATUS_SUCCESS)
653 BT_ERR("authorize_response: %d", res);
657 static void __bt_handle_ongoing_bond(bt_remote_dev_info_t *remote_dev_info, gboolean incoming_bond)
659 GVariant *param = NULL;
662 if ((remote_dev_info->name || remote_dev_info->alias)
663 && remote_dev_info->address
664 && remote_dev_info->uuids) {
665 BT_INFO("All properties updated, time to send bonding finished event");
666 GVariant *uuids = NULL;
667 GVariantBuilder *builder = NULL;
668 GVariant *manufacturer_data;
672 if (remote_dev_info->alias)
673 name = remote_dev_info->alias;
675 name = remote_dev_info->name;
677 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
678 for (i = 0; i < remote_dev_info->uuid_count; i++) {
679 g_variant_builder_add(builder, "s",
680 remote_dev_info->uuids[i]);
682 uuids = g_variant_new("as", builder);
683 g_variant_builder_unref(builder);
684 manufacturer_data = g_variant_new_from_data((const GVariantType *)"ay",
685 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
688 param = g_variant_new("(isunsbub@asn@ay)",
689 BLUETOOTH_ERROR_NONE,
690 remote_dev_info->address,
691 remote_dev_info->class,
692 remote_dev_info->rssi,
694 remote_dev_info->paired,
695 remote_dev_info->connected,
696 remote_dev_info->trust,
698 remote_dev_info->manufacturer_data_len,
700 /* Send the event to application */
701 _bt_send_event(BT_ADAPTER_EVENT,
702 BLUETOOTH_EVENT_BONDING_FINISHED,
705 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
707 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
708 __bt_free_pairing_info(&trigger_pairing_info);
711 BT_INFO("Lets wait for more remote device properties");
715 static void __handle_incoming_bond_created_event(bt_address_t *bd_addr)
717 char address[BT_ADDRESS_STRING_SIZE];
718 bluetooth_device_address_t dev_addr;
723 * BlueZ sends paired signal for each paired device, during activation,
724 * We should ignore this, otherwise application thinks that a new device
727 if (_bt_adapter_get_status() != BT_ACTIVATED) {
728 BT_DBG("BT is not activated, so ignore this");
732 _bt_convert_addr_type_to_string(address, bd_addr->addr);
733 if (!incoming_bond_info) {
734 incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
736 if (g_strcmp0(incoming_bond_info->addr, address)) {
737 BT_DBG("Previous Bond address: [%s] differs from new address: [%s]",
738 address, incoming_bond_info->addr);
739 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
740 incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
744 BT_INFO("Incoming bond successfully completed");
745 g_strlcpy(incoming_bond_info->addr, address, BT_ADDRESS_STRING_SIZE);
746 incoming_bond_info->dev_info = NULL;
748 _bt_convert_addr_string_to_type(dev_addr.addr, incoming_bond_info->addr);
749 if (BLUETOOTH_ERROR_NONE == _bt_device_get_bonded_device_info(&dev_addr)) {
750 BT_DBG("Bonded device info query posted to stack successfully");
752 BT_ERR("Bonded device info query failed");
753 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
759 static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr)
761 gchar address[BT_ADDRESS_STR_LEN];
762 bluetooth_device_address_t dev_addr;
766 if (trigger_bond_info == NULL) {
768 BT_DBG("trigger_bond_info == NULL, Handle incomming bond event");
769 __handle_incoming_bond_created_event(bd_addr);
773 _bt_convert_addr_type_to_string(address, bd_addr->addr);
774 if (g_strcmp0(trigger_bond_info->addr, address)) {
775 BT_DBG("Bonding address= [%s] is different from requested address =[%s]",
776 address, trigger_bond_info->addr);
777 __handle_incoming_bond_created_event(bd_addr);
781 BT_INFO("Bonding successfully completed");
782 /* Bonding state will be cleaned up & BONDING FINISHED EVENT
783 will be sent only when Properties are fetched from stack
784 Till that time lets not free trigger_bond_info.
785 However it is possible that while fetching device properties, internal
786 stack error can occur which can lead to no valid properties or
787 no properties at all. So in such cases, we must not wait for properties,
788 otherwise, it can lead to infinite wait */
789 _bt_convert_addr_string_to_type(dev_addr.addr,
790 trigger_bond_info->addr);
792 if (_bt_device_get_bonded_device_info(&dev_addr) == BLUETOOTH_ERROR_NONE) {
793 BT_DBG("BOnded device info query posted to stack successfully");
794 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_BOND_DEVICE,
795 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
797 BT_DBG("Possibly internal stack error in bonded device info query, perform cleanup");
798 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
799 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
800 /* Destroy if at all device got bonded at stack level */
801 device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
803 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
804 __bt_free_pairing_info(&trigger_pairing_info);
810 /**********************************************************************************************
811 * Bond removal event can be triggered for following reasons -
812 * a. If Bonding procedure if failed (for Auth failed, Page timeout, cancelled by user etc)
813 * b. If Application requests for explicitly removing the bond
814 * c. When application attempt to create bond,bond is removed first which triggers this event
815 * c. is in-line with Bluedroid bond create\emoval architecture
816 *********************************************************************************************/
817 static void __bt_device_handle_bond_removal_event(bt_address_t *bd_addr)
819 char address[BT_ADDRESS_STRING_SIZE];
823 _bt_convert_addr_type_to_string(address, bd_addr->addr);
824 _bt_service_remove_device_from_bonded_list(address);
826 if (trigger_unbond_info) {
827 BT_INFO("Bond removal request successfully handled, return DBUS and send event");
828 GVariant *param = NULL;
829 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_UNBOND_DEVICE,
830 trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
831 param = g_variant_new("(is)", BLUETOOTH_ERROR_NONE, trigger_unbond_info->addr);
832 _bt_send_event(BT_ADAPTER_EVENT,
833 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
835 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
836 __bt_free_pairing_info(&trigger_pairing_info);
837 } else if (trigger_bond_info) {
838 BT_ERR("Bonding was removed");
839 __bt_device_handle_bond_state();
844 static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_fail_event)
847 oal_status_t status = bond_fail_event->status;
848 BT_INFO("Bonding failed, reason: %d", status);
851 case OAL_STATUS_RMT_DEVICE_DOWN:
853 if (trigger_bond_info) {
854 BT_INFO("OAL_STATUS_RMT_DEVICE_DOWN:Lets retry bonding!! retry count [%d]",
856 int ret = OAL_STATUS_SUCCESS;
857 if (bond_retry_count < MAX_BOND_RETRY_COUNT) {
858 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
859 if (ret != OAL_STATUS_SUCCESS) {
860 BT_ERR("Create Bond procedure could not suceed");
861 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
862 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
863 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
864 __bt_free_pairing_info(&trigger_pairing_info);
865 bond_retry_count = 0;
869 BT_ERR("Create Bond failed MAX_BOND_RETRY_COUNT TIMES!!");
870 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
871 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
872 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
873 __bt_free_pairing_info(&trigger_pairing_info);
874 bond_retry_count = 0;
879 case OAL_STATUS_AUTH_FAILED:
881 /*TODO Auto pairing status set & ignore auto pairing logics can be done at this point.
882 To be considered later*/
883 int result = BLUETOOTH_ERROR_INTERNAL;
884 BT_INFO("BT_OPERATION_STATUS_AUTH_FAILED");
885 if (trigger_bond_info) {
886 BT_ERR("Create Bond procedure could not suceed, check if cancelled by User");
887 if (trigger_bond_info->is_cancelled_by_user) {
888 BT_ERR("Bonding is cancelled by user");
889 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
891 __bt_device_handle_pending_requests(result, BT_BOND_DEVICE,
892 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
893 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
896 __bt_free_pairing_info(&trigger_pairing_info);
899 case OAL_STATUS_INTERNAL_ERROR:
901 BT_INFO("OAL_STATUS_INTERNAL_ERROR");
902 if (trigger_unbond_info) {
903 BT_INFO("Bond removal request failed, return DBUS and send event");
904 GVariant *param = NULL;
905 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_UNBOND_DEVICE,
906 trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
907 param = g_variant_new("(is)", BLUETOOTH_ERROR_INTERNAL, trigger_unbond_info->addr);
908 _bt_send_event(BT_ADAPTER_EVENT,
909 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
911 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
912 } else if (trigger_bond_info) {
913 if (__bt_device_handle_bond_state() != BLUETOOTH_ERROR_NONE) {
914 if (trigger_bond_info) {
915 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
916 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
917 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
922 __bt_free_pairing_info(&trigger_pairing_info);
927 BT_ERR("Unknown status of Bond failed event status [%d]", status);
935 static void __bt_device_event_handler(int event_type, gpointer event_data)
937 int eventcheck = OAL_EVENT_DEVICE_PROPERTIES;
938 BT_INFO("event [%d] Event check = [%d]", event_type, eventcheck);
940 switch (event_type) {
941 case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY: {
942 BT_INFO("BREDR Device Found");
943 __bt_device_remote_device_found_callback(event_data, FALSE);
946 case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE: {
947 BT_INFO("Dual Device Found");
948 __bt_device_remote_device_found_callback(event_data, FALSE);
951 case OAL_EVENT_DEVICE_PROPERTIES: {
952 BT_INFO("Remote Device properties Received");
953 __bt_device_remote_properties_callback((event_dev_properties_t *)event_data);
956 case OAL_EVENT_DEVICE_BONDING_SUCCESS: {
957 BT_INFO("Bonding Success event Received");
958 __bt_device_handle_bond_completion_event((bt_address_t *)event_data);
961 case OAL_EVENT_DEVICE_BONDING_REMOVED: {
962 BT_INFO("Bonding Removed event Received");
963 __bt_device_handle_bond_removal_event((bt_address_t *)event_data);
966 case OAL_EVENT_DEVICE_BONDING_FAILED: {
967 BT_INFO("Bonding Failed event Received");
968 __bt_device_handle_bond_failed_event((event_dev_bond_failed_t*) event_data);
971 case OAL_EVENT_DEVICE_ACL_CONNECTED: {
972 BT_INFO("ACL Connected event Received");
973 event_dev_conn_status_t* param = event_data;
974 __bt_device_conn_state_changed_callback(param, TRUE, 0);
977 case OAL_EVENT_DEVICE_ACL_DISCONNECTED: {
978 BT_INFO("ACL Disconnected event Received");
979 __bt_device_conn_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 0);
982 case OAL_EVENT_DEVICE_LE_CONNECTED: {
983 BT_INFO("LE Connected event Received");
984 event_dev_conn_status_t* param = event_data;
985 __bt_device_conn_state_changed_callback(param, TRUE, 1);
988 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
989 BT_INFO("LE Disconnected event Received");
990 __bt_device_conn_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 1);
993 case OAL_EVENT_DEVICE_PIN_REQUEST: {
994 BT_INFO("PIN Request Received");
995 __bt_device_pin_request_callback((remote_device_t*)event_data);
998 case OAL_EVENT_DEVICE_PASSKEY_ENTRY_REQUEST: {
999 BT_INFO("Passkey Entry request Received");
1000 __bt_device_ssp_passkey_entry_callback((remote_device_t*)event_data);
1003 case OAL_EVENT_DEVICE_PASSKEY_CONFIRMATION_REQUEST:{
1004 BT_INFO("Passkey Confirmation Request Received");
1005 __bt_device_ssp_passkey_confirmation_callback((event_dev_passkey_t *)event_data);
1008 case OAL_EVENT_DEVICE_PASSKEY_DISPLAY: {
1009 BT_INFO("Passkey Display Request Received");
1010 __bt_device_ssp_passkey_display_callback((event_dev_passkey_t *)event_data);
1013 case OAL_EVENT_DEVICE_SSP_CONSENT_REQUEST: {
1014 BT_INFO("SSP Consent Request Received");
1015 __bt_device_ssp_consent_callback((remote_device_t*)event_data);
1018 case OAL_EVENT_DEVICE_SERVICES: {
1019 BT_INFO("Remote Device Services Received");
1020 __bt_device_services_callback((event_dev_services_t*)event_data);
1023 case OAL_EVENT_DEVICE_AUTHORIZE_REQUEST: {
1024 BT_INFO("Remote Device Authorization Request");
1025 __bt_device_authorization_request_callback((event_dev_authorize_req_t*)event_data);
1028 case OAL_EVENT_DEVICE_TRUSTED: {
1029 BT_INFO("Remote Device Trusted");
1030 __bt_device_trusted_callback(TRUE, (event_dev_trust_t*)event_data);
1033 case OAL_EVENT_DEVICE_UNTRUSTED: {
1034 BT_INFO("Remote Device UnTrusted");
1035 __bt_device_trusted_callback(FALSE, (event_dev_trust_t*)event_data);
1038 case OAL_EVENT_DEVICE_NAME: {
1039 remote_device_t *rem_dev = event_data;
1040 gchar address[BT_ADDRESS_STR_LEN];
1042 _bt_convert_addr_type_to_string(address, rem_dev->address.addr);
1043 BT_INFO("Remote Device name Received");
1044 BT_INFO("Name: %s, Address: %s", rem_dev->name, address);
1046 /* Update local cache */
1047 _bt_update_remote_dev_property(address, DEV_PROP_NAME, (void *)rem_dev->name);
1050 case OAL_EVENT_DEVICE_TRUSTED_PROFILES_CHANGED: {
1051 event_device_trusted_profiles_t *ev = event_data;
1052 char address[BT_ADDRESS_STRING_SIZE];
1056 _bt_convert_addr_type_to_string(address, ev->address.addr);
1057 _bt_send_event(BT_DEVICE_EVENT,
1058 BLUETOOTH_EVENT_SUPPORTED_PROFILE_TRUSTED,
1059 g_variant_new("(isi)", BLUETOOTH_ERROR_NONE,
1060 address, ev->trust_val));
1063 case OAL_EVENT_RSSI_MONITORING_ENABLED: {
1064 event_dev_rssi_info_t *ev = event_data;
1065 char address[BT_ADDRESS_STRING_SIZE];
1070 _bt_convert_addr_type_to_string(address, ev->address.addr);
1071 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1072 address, ev->link_type, TRUE);
1073 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1076 case OAL_EVENT_RSSI_MONITORING_DISABLED: {
1077 event_dev_rssi_info_t *ev = event_data;
1078 char address[BT_ADDRESS_STRING_SIZE];
1083 _bt_convert_addr_type_to_string(address, ev->address.addr);
1084 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1085 address, ev->link_type, FALSE);
1086 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1089 case OAL_EVENT_RSSI_ALERT_RECEIVED: {
1090 event_dev_rssi_info_t *ev = event_data;
1091 char address[BT_ADDRESS_STRING_SIZE];
1096 _bt_convert_addr_type_to_string(address, ev->address.addr);
1097 param = g_variant_new("(isiii)", BLUETOOTH_ERROR_NONE,
1098 address, ev->link_type, ev->alert_type, ev->rssi);
1099 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ALERT, param);
1102 case OAL_EVENT_RAW_RSSI_RECEIVED: {
1103 event_dev_rssi_info_t *ev = event_data;
1104 char address[BT_ADDRESS_STRING_SIZE];
1109 _bt_convert_addr_type_to_string(address, ev->address.addr);
1110 param = g_variant_new("(isii)", BLUETOOTH_ERROR_NONE,
1111 address, ev->link_type, ev->rssi);
1112 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RAW_RSSI, param);
1116 BT_INFO("Unhandled event..");
1120 /* Legacy Pairing event handler */
1121 static void __bt_device_pin_request_callback(remote_device_t* pin_req_event)
1124 char address[BT_ADDRESS_STRING_SIZE];
1125 char pin_code[BLUETOOTH_PIN_CODE_MAX_LENGTH + 1];
1128 _bt_convert_addr_type_to_string(address, pin_req_event->address.addr);
1130 BT_INFO("Address[%s]", address);
1131 BT_INFO("Name[%s]", pin_req_event->name);
1132 BT_INFO("COD[%d]", pin_req_event->cod);
1134 if (trigger_pairing_info) {
1135 /* BTAPI support only one pairing at a time */
1136 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1137 BT_ERR("New PIN request address [%s]", address);
1138 device_reject_pin_request(&pin_req_event->address);
1142 /* If user initiated bonding and auto response is possible, just reply with default 0000*/
1143 if (_bt_is_bonding_device_address(address) == TRUE &&
1144 _bt_agent_is_auto_response(pin_req_event->cod, address, pin_req_event->name)) {
1145 /* Note: Currently even if SYSPOPUP is supported, we use Fixed PIN "0000" for basic pairing
1146 as BT SYSPOPUP is currently not working for PIN KEY entry in Tizen platform. This needs
1147 to be checked and fixed apropriately */
1148 _bt_set_autopair_status_in_bonding_info(TRUE);
1149 device_accept_pin_request(&pin_req_event->address, "0000");
1150 } else if (_bt_agent_is_hid_keyboard(pin_req_event->cod)) {
1151 BT_DBG("Remote Device is HID keyboard Type..");
1152 char str_passkey[BT_PASSKEY_MAX_LENGTH + 1] = { 0 };
1154 if (_bt_agent_generate_passkey(str_passkey,
1155 BT_PASSKEY_MAX_LENGTH) != 0) {
1156 device_reject_pin_request(&pin_req_event->address);
1159 device_accept_pin_request(&pin_req_event->address, str_passkey);
1161 BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1162 param = g_variant_new("(isss)", BLUETOOTH_ERROR_NONE, address, pin_req_event->name, str_passkey);
1163 _bt_send_event(BT_ADAPTER_EVENT,
1164 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1165 BT_DBG("Key board pairing in process");
1166 } else if (BLUETOOTH_ERROR_NONE == __bt_get_device_pin_code(address, pin_code)) {
1167 BT_DBG("Use stored PIN code [%s]", pin_code);
1168 device_accept_pin_request(&pin_req_event->address, pin_code);
1170 if (_bt_is_bonding_device_address(address) == TRUE) {
1171 BT_DBG("Show Pin entry");
1172 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1173 trigger_pairing_info->addr = g_strdup(address);
1174 trigger_pairing_info->is_ssp = FALSE;
1176 BT_DBG("Send BLUETOOTH_EVENT_PIN_REQUEST");
1177 param = g_variant_new("(iss)", BLUETOOTH_ERROR_NONE, address, pin_req_event->name);
1178 _bt_send_event(BT_ADAPTER_EVENT,
1179 BLUETOOTH_EVENT_PIN_REQUEST, param);
1184 _bt_agent_release_memory();
1188 /* SSP Pairing event handler */
1189 static void __bt_device_ssp_passkey_entry_callback(remote_device_t* dev_info)
1192 gchar address[BT_ADDRESS_STR_LEN];
1195 int result = BLUETOOTH_ERROR_NONE;
1198 _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1200 name = dev_info->name;
1202 BT_INFO("Address[%s]", address);
1203 BT_INFO("Name[%s]", name);
1204 BT_INFO("COD[%d]", dev_info->cod);
1206 if (trigger_pairing_info) {
1207 /* BTAPI support only one pairing at a time */
1208 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1209 BT_ERR("New PIN request address [%s]", address);
1210 device_reject_pin_request(&dev_info->address);
1215 /* Set pairing data */
1216 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1217 trigger_pairing_info->addr = g_strdup(address);
1218 trigger_pairing_info->is_ssp = TRUE;
1220 param = g_variant_new("(iss)", result, p_addr, name);
1221 _bt_send_event(BT_ADAPTER_EVENT,
1222 BLUETOOTH_EVENT_PASSKEY_REQUEST, param);
1226 static void __bt_device_ssp_passkey_confirmation_callback(event_dev_passkey_t *dev_info)
1229 gchar address[BT_ADDRESS_STR_LEN];
1232 char str_passkey[7];
1233 int result = BLUETOOTH_ERROR_NONE;
1236 _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1238 name = dev_info->device_info.name;
1240 BT_INFO("Address[%s]", address);
1241 BT_INFO("Name[%s]", name);
1242 BT_INFO("COD[%d]", dev_info->device_info.cod);
1244 if (trigger_pairing_info) {
1245 /* BTAPI support only one pairing at a time */
1246 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1247 BT_ERR("New PIN request address [%s]", address);
1248 device_reject_pin_request(&dev_info->device_info.address);
1253 /* Set pairing data */
1254 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1255 trigger_pairing_info->addr = g_strdup(address);
1256 trigger_pairing_info->is_ssp = TRUE;
1258 BT_DBG("Send BLUETOOTH_EVENT_PASSKEY_CONFIRMATION");
1259 snprintf(str_passkey, sizeof(str_passkey), "%.6d", dev_info->pass_key);
1261 param = g_variant_new("(isss)", result, p_addr, name, str_passkey);
1262 _bt_send_event(BT_ADAPTER_EVENT,
1263 BLUETOOTH_EVENT_PASSKEY_CONFIRM_REQUEST, param);
1267 static void __bt_device_ssp_passkey_display_callback(event_dev_passkey_t *dev_info)
1270 gchar address[BT_ADDRESS_STR_LEN];
1273 char str_passkey[7];
1274 int result = BLUETOOTH_ERROR_NONE;
1277 _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1279 name = dev_info->device_info.name;
1281 BT_INFO("Address[%s]", address);
1282 BT_INFO("Name[%s]", name);
1283 BT_INFO("COD[%d]", dev_info->device_info.cod);
1285 if (trigger_pairing_info) {
1286 /* BTAPI support only one pairing at a time */
1287 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1288 BT_ERR("New PIN request address [%s]", address);
1289 device_reject_pin_request(&dev_info->device_info.address);
1294 /* Set pairing data */
1295 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1296 trigger_pairing_info->addr = g_strdup(address);
1297 trigger_pairing_info->is_ssp = TRUE;
1299 BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1300 snprintf(str_passkey, sizeof(str_passkey), "%06d", dev_info->pass_key);
1302 param = g_variant_new("(isss)", result, p_addr, name, str_passkey);
1303 if (passkey_watcher) {
1304 BT_INFO("Send passkey to %s", passkey_watcher);
1305 _bt_send_event_to_dest(passkey_watcher, BT_ADAPTER_EVENT,
1306 BLUETOOTH_EVENT_PASSKEY_NOTIFICATION, param);
1308 _bt_send_event(BT_ADAPTER_EVENT,
1309 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1314 static void __bt_device_ssp_consent_callback(remote_device_t* dev_info)
1316 gchar address[BT_ADDRESS_STR_LEN];
1323 _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1324 name = dev_info->name;
1325 cod = dev_info->cod;
1327 BT_INFO("Address[%s]", address);
1328 BT_INFO("Name[%s]", name);
1329 BT_INFO("COD[%d]", cod);
1331 if (trigger_pairing_info) {
1332 /* BTAPI support only one pairing at a time */
1333 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1334 BT_ERR("New PIN request address [%s]", address);
1335 device_reject_pin_request(&dev_info->address);
1340 /* Set pairing data */
1341 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1342 trigger_pairing_info->addr = g_strdup(address);
1343 trigger_pairing_info->is_ssp = TRUE;
1345 local_major = ((cod >> 8) & 0x001f);
1346 local_minor = (cod & 0x00fc);
1347 BT_DBG("SSP_CONSENT: Major type=[0x%x] and Minor type=[0x%x]", local_major, local_minor);
1349 /*TODO: BLUETOOTH_EVENT_SSP_CONSENT_REQUEST to be handled in Tizen */
1353 static void __bt_device_conn_state_changed_callback(event_dev_conn_status_t *acl_event,
1354 gboolean connected, unsigned char type)
1356 gchar address[BT_ADDRESS_STR_LEN];
1357 int result = BLUETOOTH_ERROR_NONE;
1358 GVariant *param = NULL;
1359 bt_device_conn_info_t conn_info;
1362 _bt_convert_addr_type_to_string(address, acl_event->address.addr);
1364 _bt_logging_connection(connected, type);
1367 param = g_variant_new("(isy)", result, address, type);
1368 _bt_send_event(BT_DEVICE_EVENT,
1369 BLUETOOTH_EVENT_DEVICE_CONNECTED,
1372 param = g_variant_new("(isy)", result, address, type);
1373 _bt_send_event(BT_DEVICE_EVENT,
1374 BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1378 conn_info.connected = connected;
1379 conn_info.type = type;
1380 /* Update local cache */
1381 _bt_update_remote_dev_property(address, DEV_PROP_CONNECTED, (void *)&conn_info);
1383 #ifdef TIZEN_GATT_CLIENT
1384 /*handle LE connected device info*/
1386 BT_DBG("handle LE connected device info");
1387 _bt_handle_le_connected_dev_info(address, connected);
1394 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble)
1396 bt_remote_dev_info_t *dev_info = NULL;
1397 int result = BLUETOOTH_ERROR_NONE;
1398 GVariant *param = NULL;
1399 GVariant *uuids = NULL;
1400 GVariant *manufacturer_data = NULL;
1401 GVariantBuilder *builder = NULL;
1405 ret_if(_bt_is_discovering() == FALSE);
1406 ret_if(event_data == NULL);
1408 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
1411 event_ble_dev_found_t * oal_ble_dev = event_data;
1412 BT_INFO("Device type [%d]", oal_ble_dev->device_info.type);
1414 _bt_copy_remote_dev(dev_info, &oal_ble_dev->device_info);
1416 dev_info->manufacturer_data_len = oal_ble_dev->adv_len;
1417 if (dev_info->manufacturer_data_len)
1418 dev_info->manufacturer_data = g_memdup(oal_ble_dev->adv_data,
1419 dev_info->manufacturer_data_len);
1421 dev_info->manufacturer_data = NULL;
1422 BT_DBG("----Advertising Data Length: %d", dev_info->manufacturer_data_len);
1424 event_dev_found_t * oal_dev = event_data;
1425 _bt_copy_remote_dev(dev_info, &oal_dev->device_info);
1428 /* If Remote device name is NULL or still RNR is not done then display address as name. */
1429 if (dev_info->name == NULL)
1430 dev_info->name = g_strdup(dev_info->address);
1431 BT_DBG("Name %s", dev_info->name);
1433 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1434 for (i = 0; i < dev_info->uuid_count; i++)
1435 g_variant_builder_add(builder, "s", dev_info->uuids[i]);
1437 uuids = g_variant_new("as", builder);
1438 g_variant_builder_unref(builder);
1440 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1441 dev_info->manufacturer_data,
1442 dev_info->manufacturer_data_len,
1446 param = g_variant_new("(isunsbub@asn@ay)", result,
1452 dev_info->connected,
1455 dev_info->manufacturer_data_len,
1458 _bt_send_event(BT_ADAPTER_EVENT,
1459 BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
1462 _bt_free_remote_dev(dev_info);
1466 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info)
1468 gchar address[BT_ADDRESS_STR_LEN];
1469 int result = BLUETOOTH_ERROR_NONE;
1470 GVariant *param = NULL;
1474 _bt_convert_addr_type_to_string(address, info->address.addr);
1476 /* Update local cache */
1477 _bt_update_remote_dev_property(address, DEV_PROP_TRUST, (void *)&trusted);
1479 param = g_variant_new("(is)", result, address);
1480 event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1481 BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1482 /* Send event to application */
1483 _bt_send_event(BT_DEVICE_EVENT,
1490 static void __bt_free_pairing_info(bt_pairing_data_t **p_info)
1493 bt_pairing_data_t * info = *p_info;
1504 static void __bt_free_bond_info(uint8_t type)
1509 case BT_DEVICE_BOND_INFO:
1510 if (!trigger_bond_info)
1513 if (trigger_bond_info->addr)
1514 g_free(trigger_bond_info->addr);
1515 if (trigger_bond_info->dev_addr)
1516 g_free(trigger_bond_info->dev_addr);
1517 if (trigger_bond_info->dev_info)
1518 _bt_free_remote_dev(trigger_bond_info->dev_info);
1519 g_free(trigger_bond_info);
1520 trigger_bond_info = NULL;
1522 case BT_DEVICE_INCOMING_BOND_INFO:
1523 if (!incoming_bond_info)
1526 if (incoming_bond_info->dev_info)
1527 _bt_free_remote_dev(incoming_bond_info->dev_info);
1528 g_free(incoming_bond_info);
1529 incoming_bond_info = NULL;
1531 case BT_DEVICE_UNBOND_INFO:
1532 if (!trigger_unbond_info)
1535 if (trigger_unbond_info->addr)
1536 g_free(trigger_unbond_info->addr);
1537 if (trigger_unbond_info->dev_addr)
1538 g_free(trigger_unbond_info->dev_addr);
1539 if (trigger_unbond_info->dev_info)
1540 _bt_free_remote_dev(trigger_unbond_info->dev_info);
1541 g_free(trigger_unbond_info);
1542 trigger_unbond_info = NULL;
1548 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info)
1550 bt_service_search_info_data_t * info = *p_info;
1557 if (info->dev_addr) {
1558 g_free(info->dev_addr);
1559 info->dev_addr = NULL;
1562 if (info->dev_info) {
1563 _bt_free_remote_dev(info->dev_info);
1564 info->dev_info = NULL;
1572 static int __bt_device_handle_bond_state(void)
1574 BT_INFO("Current Bond state: %d", bt_device_bond_state);
1575 int ret = OAL_STATUS_INTERNAL_ERROR;
1577 switch (bt_device_bond_state) {
1578 case BT_DEVICE_BOND_STATE_CANCEL_DISCOVERY:
1579 /*TODO:Bonding during discovery: Unhandled!!*/
1580 BT_INFO("Bonding during discovery: Unhandled!!");
1582 case BT_DEVICE_BOND_STATE_DISCOVERY_CANCELLED:
1583 /*TODO:Bonding during discovery: Unhandled!!*/
1584 BT_INFO("Bonding during discovery: Unhandled!!");
1586 case BT_DEVICE_BOND_STATE_REMOVE_BONDING:
1587 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVED_BONDING;
1588 ret = device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
1589 if (ret != OAL_STATUS_SUCCESS)
1590 ret = __bt_device_handle_bond_state();
1592 case BT_DEVICE_BOND_STATE_REMOVED_BONDING:
1593 bt_device_bond_state = BT_DEVICE_BOND_STATE_NONE;
1594 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
1595 /* Bonding procedure was started but unfortunately could not complete.
1596 Basically removed bonding was success, but create bond request could not proceed
1597 So lets cleanup the context */
1598 if (ret != OAL_STATUS_SUCCESS) {
1599 BT_ERR("Create Bond procedure could not suceed");
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);
1606 case BT_DEVICE_BOND_STATE_NONE:
1607 BT_INFO("Create Bond failed!!");
1608 if (trigger_bond_info) {
1609 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1610 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1611 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1612 __bt_free_pairing_info(&trigger_pairing_info);
1619 if (ret != OAL_STATUS_SUCCESS)
1620 return BLUETOOTH_ERROR_INTERNAL;
1622 return BLUETOOTH_ERROR_NONE;
1625 int _bt_device_get_bonded_device_info(bluetooth_device_address_t *addr)
1628 bt_address_t bd_addr;
1632 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
1634 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
1635 result = device_query_attributes(&bd_addr);
1636 if (result != OAL_STATUS_SUCCESS) {
1637 BT_ERR("device_query_attributes error: [%d]", result);
1638 return BLUETOOTH_ERROR_INTERNAL;
1642 return BLUETOOTH_ERROR_NONE;
1645 int _bt_set_alias(bluetooth_device_address_t *device_address, const char *alias)
1648 char address[BT_ADDRESS_STRING_SIZE];
1651 BT_CHECK_PARAMETER(alias, return);
1653 ret = device_set_alias((bt_address_t *)device_address, (char *)alias);
1654 if (ret != OAL_STATUS_SUCCESS) {
1655 BT_ERR("device_set_alias: %d", ret);
1656 return BLUETOOTH_ERROR_INTERNAL;
1659 /* Update local cache */
1660 _bt_convert_addr_type_to_string(address, device_address->addr);
1661 _bt_update_remote_dev_property(address, DEV_PROP_ALIAS, (void *)alias);
1664 return BLUETOOTH_ERROR_NONE;
1667 int _bt_bond_device(bluetooth_device_address_t *device_address,
1668 unsigned short conn_type, GArray **out_param1)
1670 int result = BLUETOOTH_ERROR_NONE;
1671 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1672 bluetooth_device_info_t dev_info;
1675 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1677 /* If bonding or discovery already going on */
1678 if (trigger_bond_info || _bt_is_discovering()) {
1679 BT_ERR("Device is buzy, bonding can not proceed now..");
1680 result = BLUETOOTH_ERROR_DEVICE_BUSY;
1684 /*TODO: If unbonding with same device going on */
1685 _bt_convert_addr_type_to_string(address, device_address->addr);
1687 trigger_bond_info = g_malloc0(sizeof(bt_bond_data_t));
1688 trigger_bond_info->addr = g_strdup(address);
1689 trigger_bond_info->conn_type = conn_type;
1690 trigger_bond_info->is_device_creating = TRUE;
1691 trigger_bond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1692 trigger_bond_info->dev_info = NULL;
1694 /* Ready to initiate bonding */
1696 /* In Tizen, we will first remove bond and then attempt to create bond to keep
1697 consistency with bluedroid. Even if remove bond fails due to device not already
1698 bonded, then straight away create bond is triggered. This is because, remove bond
1699 is handled differently in bluedroid and bluez. In Bluez, if device is
1700 already removed, remove bond call fails.
1701 However in bluedroid, remove bond on already removed device returns success. So we will
1702 handle the cases transparently*/
1703 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVE_BONDING;
1704 bond_retry_count = 0;
1705 result = __bt_device_handle_bond_state();
1707 if (result != BLUETOOTH_ERROR_NONE)
1714 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1715 memcpy(dev_info.device_address.addr, device_address->addr,
1716 BLUETOOTH_ADDRESS_LENGTH);
1718 g_array_append_vals(*out_param1, &dev_info,
1719 sizeof(bluetooth_device_info_t));
1720 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1726 int _bt_unbond_device(bluetooth_device_address_t *device_address,
1727 GArray **out_param1)
1729 int result = OAL_STATUS_SUCCESS;
1730 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1731 bluetooth_device_info_t dev_info;
1734 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1736 _bt_convert_addr_type_to_string(address, device_address->addr);
1738 trigger_unbond_info = g_malloc0(sizeof(bt_bond_data_t));
1739 trigger_unbond_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1740 trigger_unbond_info->addr = g_strdup(address);
1741 trigger_unbond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1743 /* Check if Bonding is already going on, we should not abruptly remove bonding*/
1744 if (trigger_bond_info && strncmp(trigger_bond_info->addr, trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE) == 0) {
1745 BT_ERR("Bonding with same device already ongoing");
1746 result = BLUETOOTH_ERROR_PERMISSION_DEINED;
1750 result = device_destroy_bond((bt_address_t *)device_address);
1751 if (result != OAL_STATUS_SUCCESS)
1757 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1758 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1759 trigger_unbond_info->addr);
1761 g_array_append_vals(*out_param1, &dev_info,
1762 sizeof(bluetooth_device_info_t));
1763 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
1768 int _bt_cancel_bonding(void)
1770 int result = OAL_STATUS_SUCCESS;
1773 retv_if(trigger_bond_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1775 result = device_stop_bond((bt_address_t *)trigger_bond_info->dev_addr);
1777 if (result == OAL_STATUS_SUCCESS)
1778 trigger_bond_info->is_cancelled_by_user = TRUE;
1783 int _bt_passkey_reply(const char *passkey, gboolean cnfm_reply)
1785 bluetooth_device_address_t device_address;
1786 int ret = OAL_STATUS_SUCCESS;
1787 BT_INFO("reply: %d", cnfm_reply);
1789 retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1790 retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1792 _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1794 if (trigger_pairing_info->is_ssp) {
1796 ret = device_accept_passkey_entry((bt_address_t *)&device_address, atoi(passkey));
1798 ret = device_reject_passkey_entry((bt_address_t *)&device_address);
1799 trigger_pairing_info->is_ssp = FALSE;
1802 ret = device_accept_pin_request((bt_address_t *)&device_address, passkey);
1804 ret = device_reject_pin_request((bt_address_t *)&device_address);
1807 __bt_free_pairing_info(&trigger_pairing_info);
1809 if (ret != OAL_STATUS_SUCCESS) {
1810 BT_ERR("_bt_device_handle_passkey_reply: err [%d]", ret);
1811 return BLUETOOTH_ERROR_INTERNAL;
1815 return BLUETOOTH_ERROR_NONE;
1818 int _bt_passkey_confirmation_reply(gboolean cnfm_reply)
1820 BT_INFO("BT_PASSKEY_CONFIRMATION_REPLY");
1821 bluetooth_device_address_t device_address;
1822 int ret = OAL_STATUS_SUCCESS;
1823 BT_INFO("reply: %d", cnfm_reply);
1825 retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1826 retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1828 _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1830 ret = device_reply_passkey_confirmation((bt_address_t *)&device_address, cnfm_reply);
1832 __bt_free_pairing_info(&trigger_pairing_info);
1833 if (ret != OAL_STATUS_SUCCESS) {
1834 BT_ERR("_bt_device_handle_passkey_confirmation_reply: err [%d]", ret);
1835 return BLUETOOTH_ERROR_INTERNAL;
1839 return BLUETOOTH_ERROR_NONE;
1842 gboolean _bt_device_is_pairing(void)
1844 return (trigger_pairing_info) ? TRUE : FALSE;
1847 gboolean _bt_device_is_bonding(void)
1849 return (trigger_bond_info) ? TRUE : FALSE;
1852 gboolean _bt_is_bonding_device_address(const char *address)
1854 if (trigger_bond_info == NULL || trigger_bond_info->addr == NULL)
1857 if (g_strcmp0(trigger_bond_info->addr, address) == 0) {
1858 BT_DBG("[%s] is bonding device", address);
1862 BT_DBG("[%s] is NOT bonding device", address);
1866 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
1868 ret_if(trigger_bond_info == NULL);
1869 trigger_bond_info->is_autopair = is_autopair;
1872 int _bt_search_device(bluetooth_device_address_t *device_address)
1874 int result = OAL_STATUS_SUCCESS;
1877 BT_CHECK_PARAMETER(device_address, return);
1879 if (trigger_bond_info) {
1880 BT_ERR("Bonding in progress");
1881 return BLUETOOTH_ERROR_DEVICE_BUSY;
1884 if (service_search_info) {
1885 BT_ERR("Service searching in progress");
1886 return BLUETOOTH_ERROR_DEVICE_BUSY;
1889 /* allocate user data so that it can be retrieved in callback */
1890 service_search_info = g_malloc0(sizeof(bt_service_search_info_data_t));
1891 service_search_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1892 service_search_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1894 _bt_convert_addr_type_to_string(service_search_info->addr,
1895 device_address->addr);
1897 result = device_query_services((bt_address_t *)device_address);
1899 if (result != OAL_STATUS_SUCCESS) {
1900 BT_ERR("Device Service Search Failed..: %d", result);
1901 __bt_free_service_search_info(&service_search_info);
1902 return BLUETOOTH_ERROR_INTERNAL;
1904 return BLUETOOTH_ERROR_NONE;
1907 int _bt_cancel_search_device(void)
1909 int ret = OAL_STATUS_SUCCESS;
1910 retv_if(service_search_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1912 ret = device_stop_query_sevices((bt_address_t *)service_search_info->dev_addr);
1914 if (ret != OAL_STATUS_SUCCESS) {
1915 BT_ERR("SDP Cancel request failed [%d]", ret);
1916 return BLUETOOTH_ERROR_INTERNAL;
1919 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_CANCEL_BY_USER, BT_SEARCH_SERVICE,
1920 service_search_info->addr, BT_ADDRESS_STRING_SIZE);
1922 __bt_free_service_search_info(&service_search_info);
1924 return BLUETOOTH_ERROR_NONE;
1928 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1931 int ret = OAL_STATUS_SUCCESS;
1934 BT_CHECK_PARAMETER(device_address, return);
1935 BT_INFO("Device to be Trusted? [%d]", authorize);
1937 ret = device_set_authorized((bt_address_t*)device_address, authorize);
1938 if (ret != OAL_STATUS_SUCCESS) {
1939 BT_ERR("device_set_authorized: %d", ret);
1940 return BLUETOOTH_ERROR_INTERNAL;
1943 return BLUETOOTH_ERROR_NONE;
1946 gboolean _bt_is_device_connected(bluetooth_device_address_t *device_address, int svc_type)
1948 gboolean is_connected;
1949 oal_service_t svc_id;
1951 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1954 * TODO: While adding support for new profiles, need to add more
1955 * <svc_type, svc_id> mapping here.
1958 case BT_PROFILE_CONN_HID:
1959 svc_id = HID_SERVICE_ID;
1961 case BT_PROFILE_CONN_A2DP:
1962 svc_id = A2DP_SERVICE_ID; /* Remote is A2DP Sink */
1964 case BT_PROFILE_CONN_A2DP_SINK:
1965 svc_id = A2DP_SRC_SERVICE_ID; /* Remote is A2DP Source*/
1967 case BT_PROFILE_CONN_HSP:
1968 svc_id = HFP_HS_SERVICE_ID; /* Remote is HFP HF Unit */
1970 #ifdef TIZEN_GATT_CLIENT
1971 case BT_PROFILE_CONN_GATT:
1972 return _bt_is_remote_gatt_device_connected(device_address); /* Remote is GATT client or Server */
1975 BT_DBG("Unknown svc_type: %d", svc_type);
1979 is_connected = device_get_svc_conn_state((bt_address_t*)device_address, svc_id);
1981 BT_DBG("svc_type: %d, is_connected: %s",
1982 svc_type, is_connected ? "TRUE" : "FALSE");
1984 return is_connected;
1987 int _bt_rfcomm_reply_conn_authorization(char *address, gboolean reply)
1989 bt_address_t bd_addr;
1994 retv_if(NULL == address, BLUETOOTH_ERROR_INVALID_PARAM);
1995 _bt_convert_addr_string_to_type(bd_addr.addr, address);
1996 res = device_reply_auth_request(&bd_addr, 0, reply, FALSE);
1997 if (res != OAL_STATUS_SUCCESS) {
1998 BT_ERR("authorize_response: %d", res);
1999 return BLUETOOTH_ERROR_INTERNAL;
2003 return BLUETOOTH_ERROR_NONE;
2006 int _bt_enable_rssi(bluetooth_device_address_t *addr, int link_type,
2007 int low_threshold, int in_range_threshold, int high_threshold)
2010 bt_address_t bd_addr;
2014 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2016 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2017 result = device_enable_rssi_monitoring(&bd_addr, link_type,
2018 low_threshold, in_range_threshold, high_threshold);
2019 if (result != OAL_STATUS_SUCCESS) {
2020 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2021 return BLUETOOTH_ERROR_INTERNAL;
2025 return BLUETOOTH_ERROR_NONE;
2028 int _bt_get_rssi_strength(bluetooth_device_address_t *addr, int link_type)
2031 bt_address_t bd_addr;
2035 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2037 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2038 result = device_get_connected_link_rssi_strength(&bd_addr, link_type);
2039 if (result != OAL_STATUS_SUCCESS) {
2040 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2041 return BLUETOOTH_ERROR_INTERNAL;
2045 return BLUETOOTH_ERROR_NONE;
2048 int _bt_set_passkey_notification(const char *sender, gboolean enable)
2052 BT_INFO("Set passkey notification(sender:%s, %s)",
2053 sender, enable ? "Enable" : "Disable");
2055 result = device_enable_gap_auth_notifications(OAL_PASSKEY_DISPLAY, enable);
2056 if (OAL_STATUS_SUCCESS != result) {
2057 BT_ERR("device_enable_gap_auth_notifications error: [%d]", result);
2058 return BLUETOOTH_ERROR_INTERNAL;
2061 g_free(passkey_watcher);
2063 passkey_watcher = g_strdup(sender);
2065 passkey_watcher = NULL;
2067 return BLUETOOTH_ERROR_NONE;
2070 static int __bt_get_device_pin_code(const char *address, char *pin_code)
2074 BT_CHECK_PARAMETER(address, return);
2075 BT_CHECK_PARAMETER(pin_code, return);
2077 for (l = pin_info_list; l != NULL; l = l->next) {
2078 bt_pin_code_info_t *pin_info = l->data;
2080 if (!pin_info || !pin_info->address)
2083 if (g_strcmp0(pin_info->address, address) == 0) {
2084 g_strlcpy(pin_code, pin_info->pin_code,
2085 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2086 return BLUETOOTH_ERROR_NONE;
2090 return BLUETOOTH_ERROR_NOT_FOUND;
2093 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2094 bluetooth_device_pin_code_t *pin_code)
2096 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2097 bt_pin_code_info_t *pin_info = NULL;
2100 BT_CHECK_PARAMETER(device_address, return);
2101 BT_CHECK_PARAMETER(pin_code, return);
2102 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2103 BLUETOOTH_ERROR_NO_RESOURCES);
2105 _bt_convert_addr_type_to_string(address, device_address->addr);
2107 for (l = pin_info_list; l != NULL; l = l->next) {
2110 if (!pin_info || !pin_info->address)
2113 if (g_strcmp0(pin_info->address, address) == 0) {
2114 g_free(pin_info->pin_code);
2115 pin_info->pin_code = g_strdup(pin_code->pin_code);
2116 return BLUETOOTH_ERROR_NONE;
2120 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2121 pin_info->address = g_strdup(address);
2122 pin_info->pin_code = g_strdup(pin_code->pin_code);
2123 pin_info_list = g_slist_append(pin_info_list, pin_info);
2125 return BLUETOOTH_ERROR_NONE;
2128 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2130 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2131 bt_pin_code_info_t *pin_info = NULL;
2136 BT_CHECK_PARAMETER(device_address, return);
2138 _bt_convert_addr_type_to_string(address, device_address->addr);
2140 for (l = pin_info_list; l != NULL; l = l->next) {
2143 if (!pin_info || !pin_info->address)
2146 if (g_strcmp0(pin_info->address, address) == 0) {
2147 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2148 g_free(pin_info->address);
2149 g_free(pin_info->pin_code);
2156 return BLUETOOTH_ERROR_NONE;
2159 #ifdef TIZEN_GATT_CLIENT
2160 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2163 bt_connected_le_dev_t *dev;
2168 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2171 if (g_strcmp0(dev->address, address) == 0)
2177 static void __bt_le_conn_param_free(void *data)
2179 bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2181 BT_DBG("%s", param->sender);
2182 g_free(param->sender);
2186 static void _bt_add_le_connected_dev_info(const char *address)
2188 bt_connected_le_dev_t *dev = NULL;
2193 dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2194 dev->address = g_strdup(address);
2196 le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2201 static void _bt_remove_le_connected_dev_info(const char *address)
2203 bt_connected_le_dev_t *dev = NULL;
2208 dev = __bt_get_le_connected_dev_info(address);
2212 g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2213 le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2214 g_free(dev->address);
2221 static void _bt_handle_le_connected_dev_info(const char *address, gboolean connected)
2226 _bt_add_le_connected_dev_info(address);
2228 _bt_remove_le_connected_dev_info(address);
2231 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2234 bt_le_conn_param_t *param = NULL;
2236 if (!dev || !sender)
2239 for (l = dev->senders; l; l = g_slist_next(l)) {
2241 if (g_strcmp0(param->sender, sender) == 0)
2248 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2250 bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2251 bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2253 return parama->key > paramb->key;
2257 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2258 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2260 bt_connected_le_dev_t *dev = NULL;
2261 bt_le_conn_param_t *param = NULL;
2262 bt_le_conn_param_t *data = NULL;
2266 if (!address || !sender)
2267 return BLUETOOTH_ERROR_INVALID_PARAM;
2269 dev = __bt_get_le_connected_dev_info(address);
2271 return BLUETOOTH_ERROR_INTERNAL;
2273 param = __bt_get_le_conn_param_info(dev, sender);
2275 data = g_malloc0(sizeof(bt_le_conn_param_t));
2276 data->sender = g_strdup(sender);
2277 data->interval_min = interval_min;
2278 data->interval_max = interval_max;
2279 data->latency = latency;
2280 data->time_out = time_out;
2281 data->key = interval_min + (interval_max - interval_min)/2;
2283 if (param == NULL) {
2284 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2285 dev->senders = g_slist_append(dev->senders, data);
2287 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2288 dev->senders = g_slist_remove(dev->senders, param);
2289 g_free(param->sender);
2291 dev->senders = g_slist_append(dev->senders, data);
2294 /* Sorting. First element have the minimum interval */
2295 dev->senders = g_slist_sort(dev->senders,
2296 (GCompareFunc)__bt_compare_le_conn_param_key);
2298 return BLUETOOTH_ERROR_NONE;
2302 static int __bt_le_set_conn_parameter(const char *address,
2303 float interval_min, float interval_max,
2304 guint16 latency, guint16 time_out)
2306 bt_address_t dev_addr = { {0} };
2307 guint32 min, max, to;
2309 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2310 interval_min, interval_max, latency, time_out);
2312 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2313 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2314 to = time_out / BT_LE_CONN_TO_SPLIT;
2316 BT_INFO("updating: Min interval: %d, Max interval: %d, Latency: %d, Supervision timeout: %d",
2317 min, max, latency, to);
2319 _bt_convert_addr_string_to_type(dev_addr.addr, address);
2321 return gattc_conn_param_update(&dev_addr, min, max, latency, to);
2324 int _bt_remove_le_conn_param_info(const char *address, const char *sender)
2326 bt_connected_le_dev_t *dev = NULL;
2327 bt_le_conn_param_t *param = NULL;
2329 if (!address || !sender)
2330 return BLUETOOTH_ERROR_INVALID_PARAM;
2332 dev = __bt_get_le_connected_dev_info(address);
2334 return BLUETOOTH_ERROR_INTERNAL;
2336 param = __bt_get_le_conn_param_info(dev, sender);
2338 BT_DBG("Remove param %s %s ", address, sender);
2339 dev->senders = g_slist_remove(dev->senders, param);
2340 g_free(param->sender);
2344 return BLUETOOTH_ERROR_NONE;
2348 int _bt_le_connection_update(const char *sender,
2349 unsigned char *device_address,
2350 float interval_min, float interval_max,
2351 guint16 latency, guint16 time_out)
2353 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2354 guint32 min_supervision_to;
2355 bt_connected_le_dev_t *dev = NULL;
2356 bt_le_conn_param_t *param = NULL;
2357 int ret = BLUETOOTH_ERROR_NONE;
2359 BT_CHECK_PARAMETER(device_address, return);
2361 BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2362 sender, interval_min, interval_max, latency, time_out);
2364 if (interval_min > interval_max ||
2365 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2366 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2367 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2371 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2372 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2373 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2377 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2378 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2383 * The Supervision_Timeout in milliseconds shall be larger than
2384 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2385 * where Conn_Interval_Max is given in milliseconds.
2388 min_supervision_to = (1 + latency) * interval_max * 2;
2389 if (time_out <= min_supervision_to) {
2390 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2394 _bt_convert_addr_type_to_string(address, device_address);
2395 BT_DBG("Remote device address: %s", address);
2397 _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2399 dev = __bt_get_le_connected_dev_info(address);
2401 BT_DBG("device not found in the list");
2402 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2406 if (g_slist_length(dev->senders) == 1)
2409 param = dev->senders->data;
2411 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2413 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2414 BT_DBG("Skip due to same interval");
2418 interval_min = param->interval_min;
2419 interval_max = param->interval_max;
2423 ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2425 if (ret != OAL_STATUS_SUCCESS) {
2426 _bt_remove_le_conn_param_info(address, sender);
2427 BT_DBG("fail to update the LE connection parameter");
2428 ret = BLUETOOTH_ERROR_INTERNAL;
2432 BT_DBG("updated LE connection parameter");
2433 dev->interval_min = interval_min;
2434 dev->interval_max = interval_max;
2436 return BLUETOOTH_ERROR_NONE;