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_acl_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; ) {
213 if (req_info == NULL || req_info->service_function != service_function)
216 switch (service_function) {
217 case BT_SEARCH_SERVICE: {
218 char *address = (char *)user_data;
219 if (strncmp((char*)req_info->user_data, address, BT_ADDRESS_STRING_SIZE)) {
220 BT_ERR("Unexpected: Info request pending for a different address!!");
223 BT_INFO("Found info request addr [%s]", (char*)req_info->user_data);
224 bt_sdp_info_t sdp_info;
226 memset(&sdp_info, 0x00, sizeof(bt_sdp_info_t));
227 _bt_convert_addr_string_to_type(sdp_info.device_addr.addr, address);
229 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
230 g_array_append_vals(out_param, &sdp_info, sizeof(bt_sdp_info_t));
231 _bt_service_method_return(req_info->context, out_param, result);
233 g_free(req_info->user_data);
234 _bt_free_info_from_invocation_list(req_info);
235 g_array_free(out_param, TRUE);
239 case BT_BOND_DEVICE: {
240 char *address = (char *)user_data;
241 if (strncmp((char*)req_info->user_data, address, BT_ADDRESS_STRING_SIZE)) {
242 BT_ERR("Unexpected: Info request pending for a different address!!");
245 BT_INFO("Found info request addr [%s]", (char*)req_info->user_data);
246 bluetooth_device_info_t dev_info;
247 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
248 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
250 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
251 g_array_append_vals(out_param, &dev_info,
252 sizeof(bluetooth_device_info_t));
253 _bt_service_method_return(req_info->context, out_param, result);
255 g_free(req_info->user_data);
256 _bt_free_info_from_invocation_list(req_info);
257 g_array_free(out_param, TRUE);
261 case BT_UNBOND_DEVICE: {
262 char *address = (char *)user_data;
263 if (strncmp((char*)req_info->user_data, address, BT_ADDRESS_STRING_SIZE)) {
264 BT_ERR("Unexpected: Info request pending for a different address!!");
267 BT_INFO("Found info request addr [%s]", (char*)req_info->user_data);
268 bluetooth_device_address_t dev_addr;
269 _bt_convert_addr_string_to_type(dev_addr.addr, address);
270 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
271 g_array_append_vals(out_param, &dev_addr,
272 sizeof(bluetooth_device_address_t));
273 _bt_service_method_return(req_info->context, out_param, result);
275 g_free(req_info->user_data);
276 _bt_free_info_from_invocation_list(req_info);
277 g_array_free(out_param, TRUE);
282 BT_ERR("Unhandled case");
290 * Remote device properties are received on all following conditions
291 * a. When Bonding in on-going
292 * b. When device properties are updated\changed for a connected device
293 * (due to SDP or any other reason)
295 static void __bt_device_remote_properties_callback(event_dev_properties_t *oal_dev_props)
297 bt_remote_dev_info_t *rem_info = NULL;
300 rem_info = g_malloc0(sizeof(bt_remote_dev_info_t));
301 _bt_copy_remote_dev(rem_info, &(oal_dev_props->device_info));
303 if (oal_dev_props->adv_len > 0) {
306 rem_info->manufacturer_data_len = oal_dev_props->adv_len;
307 rem_info->manufacturer_data =
308 g_memdup(oal_dev_props->adv_data,
309 oal_dev_props->adv_len);
310 BT_DBG("----Advertising Data Length: %d",
311 rem_info->manufacturer_data_len);
313 for (k = 0; k < rem_info->manufacturer_data_len; k++) {
314 BT_INFO("Check data[%d] = [[0x%x]",
315 k, oal_dev_props->adv_data[k]);
318 rem_info->manufacturer_data = NULL;
319 rem_info->manufacturer_data_len = 0;
322 /* a. Check if bonding is on-going, if yes, we MUST update the bonding device properties */
323 if (trigger_bond_info && !strcmp(trigger_bond_info->addr, rem_info->address)) {
324 BT_INFO("Bonding is ongoing, try update properties");
325 if (!trigger_bond_info->dev_info ||
326 (!trigger_bond_info->dev_info->name &&
327 !trigger_bond_info->dev_info->alias) ||
328 !trigger_bond_info->dev_info->address ||
329 trigger_bond_info->dev_info->uuid_count == 0) {
330 BT_INFO("Complete data is not present, Assigning rem_info");
331 if (!trigger_bond_info->dev_info)
332 trigger_bond_info->dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
333 _bt_copy_remote_dev_info(trigger_bond_info->dev_info, rem_info);
336 BT_DBG("Bonding dev addr has matched with remote dev properties address [%s]", rem_info->address);
337 __bt_handle_ongoing_bond(trigger_bond_info->dev_info, FALSE);
338 } else if (incoming_bond_info && !g_strcmp0(incoming_bond_info->addr, rem_info->address)) {
339 BT_INFO("Incoming Bond is ongoing, try update properties");
340 if (!incoming_bond_info->dev_info ||
341 (!incoming_bond_info->dev_info->name &&
342 !incoming_bond_info->dev_info->alias) ||
343 !incoming_bond_info->dev_info->address ||
344 incoming_bond_info->dev_info->uuid_count == 0) {
345 BT_INFO("Complete data is not present, Assigning rem_info");
346 if (!incoming_bond_info->dev_info)
347 incoming_bond_info->dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
348 _bt_copy_remote_dev_info(incoming_bond_info->dev_info, rem_info);
351 BT_DBG("Incoming Bond addr matches with remote dev properties address [%s]", rem_info->address);
352 __bt_handle_ongoing_bond(incoming_bond_info->dev_info, TRUE);
355 /* Add device to bonded list */
356 _bt_service_add_device_to_bonded_list(rem_info);
358 /* Handle SDP Device properties update */
359 if (service_search_info && service_search_info->dev_info) {
360 if (!strcmp(service_search_info->addr, rem_info->address)) {
361 BT_DBG("Properties received and SDP request pending, fill device properties and send event");
362 service_search_info->dev_info->class = rem_info->class;
363 service_search_info->dev_info->paired = rem_info->paired;
364 service_search_info->dev_info->connected = rem_info->connected;
365 service_search_info->dev_info->rssi = rem_info->rssi;
366 service_search_info->dev_info->addr_type = rem_info->addr_type;
367 service_search_info->dev_info->trust = rem_info->trust;
370 service_search_info->dev_info->manufacturer_data = NULL;
371 service_search_info->dev_info->manufacturer_data_len = 0;
373 __bt_handle_ongoing_device_service_search(service_search_info->dev_info);
377 _bt_free_remote_dev(rem_info);
381 static int __get_oal_trusted_profile(bluetooth_trusted_profile_t profile)
384 case TRUSTED_PROFILE_PBAP:
385 return OAL_TRUSTED_PROFILE_PBAP;
386 case TRUSTED_PROFILE_MAP:
387 return OAL_TRUSTED_PROFILE_MAP;
388 case TRUSTED_PROFILE_SAP:
389 return OAL_TRUSTED_PROFILE_SAP;
390 case TRUSTED_PROFILE_HFP_HF:
391 return OAL_TRUSTED_PROFILE_HFP_HF;
392 case TRUSTED_PROFILE_A2DP:
393 return OAL_TRUSTED_PROFILE_A2DP;
394 case TRUSTED_PROFILE_ALL:
395 return OAL_TRUSTED_PROFILE_ALL;
401 int _bt_set_trust_profile(bluetooth_device_address_t *addr,
402 bluetooth_trusted_profile_t profile, gboolean trust)
405 bt_address_t bd_addr;
406 oal_trusted_profile_e oal_profile;
410 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
412 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
413 oal_profile = __get_oal_trusted_profile(profile);
414 retv_if(0 == oal_profile, BLUETOOTH_ERROR_NOT_SUPPORT);
416 result = device_set_trust_profile(&bd_addr, oal_profile, trust);
417 if (result != OAL_STATUS_SUCCESS) {
418 BT_ERR("device_set_trust_profile error: [%d]", result);
419 return BLUETOOTH_ERROR_INTERNAL;
423 return BLUETOOTH_ERROR_NONE;
426 int _bt_get_trust_profile(bluetooth_device_address_t *addr,
427 bluetooth_trusted_profile_t profile, guint *trust)
430 bt_address_t bd_addr;
431 oal_trusted_profile_e oal_profile;
435 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
437 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
438 oal_profile = __get_oal_trusted_profile(profile);
439 retv_if(0 == oal_profile, BLUETOOTH_ERROR_NOT_SUPPORT);
441 result = device_get_trust_profile(&bd_addr, oal_profile, trust);
442 if (result != OAL_STATUS_SUCCESS) {
443 BT_ERR("device_set_trust_profile error: [%d]", result);
444 return BLUETOOTH_ERROR_INTERNAL;
448 return BLUETOOTH_ERROR_NONE;
451 static void __bt_handle_ongoing_device_service_search(bt_remote_dev_info_t *remote_dev_info)
453 GVariant *param = NULL;
454 GVariant *uuids = NULL;
455 GVariantBuilder *builder = NULL;
456 GVariant *manufacturer_data;
460 BT_INFO("Send Service Search request event");
462 if (remote_dev_info->alias)
463 name = remote_dev_info->alias;
465 name = remote_dev_info->name;
467 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
468 for (i = 0; i < remote_dev_info->uuid_count; i++) {
469 g_variant_builder_add(builder, "s",
470 remote_dev_info->uuids[i]);
472 uuids = g_variant_new("as", builder);
473 g_variant_builder_unref(builder);
474 manufacturer_data = g_variant_new_from_data((const GVariantType *)"ay",
475 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
478 param = g_variant_new("(isunsbub@asn@ay)",
479 BLUETOOTH_ERROR_NONE,
480 remote_dev_info->address,
481 remote_dev_info->class,
482 remote_dev_info->rssi,
484 remote_dev_info->paired,
485 remote_dev_info->connected,
486 remote_dev_info->trust,
488 remote_dev_info->manufacturer_data_len,
490 /* Send the event to application */
491 _bt_send_event(BT_ADAPTER_EVENT,
492 BLUETOOTH_EVENT_SERVICE_SEARCHED,
495 __bt_free_service_search_info(&service_search_info);
499 static void __bt_device_services_callback(event_dev_services_t* uuid_list)
501 bt_remote_dev_info_t *rem_info = NULL;
505 if (trigger_bond_info && _bt_compare_adddress(trigger_bond_info->dev_addr,
506 (bluetooth_device_address_t *)&uuid_list->address) == TRUE) {
507 bluetooth_device_address_t *dev_addr = trigger_bond_info->dev_addr;
509 BT_DBG("Bonding dev addr has matched");
510 /* Bonding ongoing, Query device properties again */
511 if (BLUETOOTH_ERROR_NONE ==
512 _bt_device_get_bonded_device_info(dev_addr))
513 BT_DBG("_bt_device_get_bonded_device_info success");
515 BT_ERR("_bt_device_get_bonded_device_info failed");
518 if (service_search_info == NULL) {
520 BT_DBG("searching_info == NULL");
524 if (_bt_compare_adddress(service_search_info->dev_addr,
525 (bluetooth_device_address_t *)&uuid_list->address) == FALSE) {
526 BT_DBG("This device is not queried");
530 rem_info = g_malloc0(sizeof(bt_remote_dev_info_t));
531 memset(rem_info, 0x00, sizeof(bt_remote_dev_info_t));
533 rem_info->address = g_new0(char, BT_ADDRESS_STRING_SIZE);
534 _bt_convert_addr_type_to_string(rem_info->address, uuid_list->address.addr);
536 rem_info->uuid_count = uuid_list->num;
538 BT_INFO("Address [%s]", rem_info->address);
539 BT_INFO("Number of UUID's [%d]", rem_info->uuid_count);
540 if (rem_info->uuid_count > 0)
541 rem_info->uuids = g_new0(char *, rem_info->uuid_count);
543 /* Fill Remote Device Service List list */
544 for (i = 0; i < rem_info->uuid_count; i++) {
545 rem_info->uuids[i] = g_malloc0(BLUETOOTH_UUID_STRING_MAX);
546 _bt_uuid_to_string((service_uuid_t *)&uuid_list->service_list[i].uuid, rem_info->uuids[i]);
547 BT_DBG("UUID value=%s", rem_info->uuids[i]);
550 /* Update local cache */
551 _bt_update_remote_dev_property(rem_info->address, DEV_PROP_SERVICES, (void *)rem_info);
553 BT_DBG("DBUS return");
554 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_SEARCH_SERVICE,
555 service_search_info->addr, BT_ADDRESS_STRING_SIZE);
557 /* Save UUID List of remote devices */
558 if (service_search_info->dev_info)
559 _bt_free_remote_dev(service_search_info->dev_info);
560 service_search_info->dev_info = rem_info;
562 /* Query Other device properties */
563 if (_bt_device_get_bonded_device_info(service_search_info->dev_addr) == BLUETOOTH_ERROR_NONE) {
564 BT_DBG("Bonded device info query posted to stack successfully");
566 BT_DBG("Possibly internal stack error in bonded device info query, perform cleanup");
567 __bt_free_service_search_info(&service_search_info);
572 static void __bt_device_authorization_request_callback(event_dev_authorize_req_t* auth_event)
574 oal_service_t service_d = auth_event->service_id;
575 gchar address[BT_ADDRESS_STR_LEN];
578 _bt_convert_addr_type_to_string(address, auth_event->address.addr);
580 BT_INFO("service_d: %d", service_d);
584 BT_INFO("Incoming HID Profile conn Req from device addr [%s]", address);
586 case A2DP_SERVICE_ID:
587 BT_INFO("Incoming A2DP(Remote Sink) profile conn Req from device addr [%s]", address);
588 _bt_a2dp_src_handle_incoming_authorization(address, service_d);
590 case A2DP_SRC_SERVICE_ID:
591 BT_INFO("Incoming A2DP(Remote Source) Profile conn Req from device addr [%s]", address);
592 _bt_a2dp_sink_handle_incoming_authorization(address, service_d);
594 case AVRCP_SERVICE_ID:
595 BT_INFO("Incoming AVRCP (Remote) Profile conn Req from device addr [%s]", address);
597 case AVRCP_CT_SERVICE_ID:
598 BT_INFO("Incoming AVRCP (Controller) Profile conn Req from device addr [%s]", address);
600 #ifdef TIZEN_FEATURE_BT_OBEX
601 case OPP_SERVICE_ID: {
602 GVariant *param = NULL;
605 if (_bt_obex_server_is_custom() == false) {
606 /* Allow the connection for native OPP server */
610 name = g_strdup(address);
612 BT_INFO("Incoming OPP conn Req from device addr [%s]", address);
613 _bt_obex_server_set_pending_conn_auth_device_addr(address);
614 param = g_variant_new("(iss)", BLUETOOTH_ERROR_NONE, address, name);
615 _bt_send_event(BT_OPP_SERVER_EVENT,
616 BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE, param);
622 BT_INFO("Incoming HSP_SERVICE_ID conn Req from device addr [%s]", address);
625 BT_INFO("Incoming HFP_SERVICE_ID conn Req from device addr [%s]", address);
628 BT_INFO("Incoming SAP_SERVICE_ID conn Req from device addr [%s]", address);
630 case HSP_HS_SERVICE_ID:
631 BT_INFO("Incoming HSP_HS_SERVICE_ID conn Req from device addr [%s]", address);
633 case HFP_HS_SERVICE_ID:
634 BT_INFO("Incoming HFP_HS_SERVICE_ID conn Req from device addr [%s]", address);
637 case IOTIVITY_SERVICE_ID:
638 BT_INFO("Incoming IOTIVITY_SERVICE_ID conn Req from device addr [%s]", address);
642 /* For now, reject authorization for any service apart from above switch cases */
643 BT_INFO("Incoming Profile conn req with service ID [%d] from device addr [%s]", service_d, address);
644 res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, FALSE, FALSE);
645 if (res != OAL_STATUS_SUCCESS)
646 BT_ERR("authorize_response: %d", res);
650 /* Auto accept authorization request for HID, A2DP and AVRCP profiles */
651 BT_INFO("Auto Accept authorization");
652 res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, TRUE, FALSE);
653 if (res != OAL_STATUS_SUCCESS)
654 BT_ERR("authorize_response: %d", res);
658 static void __bt_handle_ongoing_bond(bt_remote_dev_info_t *remote_dev_info, gboolean incoming_bond)
660 GVariant *param = NULL;
663 if ((remote_dev_info->name || remote_dev_info->alias)
664 && remote_dev_info->address
665 && remote_dev_info->uuids) {
666 BT_INFO("All properties updated, time to send bonding finished event");
667 GVariant *uuids = NULL;
668 GVariantBuilder *builder = NULL;
669 GVariant *manufacturer_data;
673 if (remote_dev_info->alias)
674 name = remote_dev_info->alias;
676 name = remote_dev_info->name;
678 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
679 for (i = 0; i < remote_dev_info->uuid_count; i++) {
680 g_variant_builder_add(builder, "s",
681 remote_dev_info->uuids[i]);
683 uuids = g_variant_new("as", builder);
684 g_variant_builder_unref(builder);
685 manufacturer_data = g_variant_new_from_data((const GVariantType *)"ay",
686 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
689 param = g_variant_new("(isunsbub@asn@ay)",
690 BLUETOOTH_ERROR_NONE,
691 remote_dev_info->address,
692 remote_dev_info->class,
693 remote_dev_info->rssi,
695 remote_dev_info->paired,
696 remote_dev_info->connected,
697 remote_dev_info->trust,
699 remote_dev_info->manufacturer_data_len,
701 /* Send the event to application */
702 _bt_send_event(BT_ADAPTER_EVENT,
703 BLUETOOTH_EVENT_BONDING_FINISHED,
706 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
708 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
709 __bt_free_pairing_info(&trigger_pairing_info);
712 BT_INFO("Lets wait for more remote device properties");
716 static void __handle_incoming_bond_created_event(bt_address_t *bd_addr)
718 char address[BT_ADDRESS_STRING_SIZE];
719 bluetooth_device_address_t dev_addr;
724 * BlueZ sends paired signal for each paired device, during activation,
725 * We should ignore this, otherwise application thinks that a new device
728 if (_bt_adapter_get_status() != BT_ACTIVATED) {
729 BT_DBG("BT is not activated, so ignore this");
733 _bt_convert_addr_type_to_string(address, bd_addr->addr);
734 if (!incoming_bond_info) {
735 incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
737 if (g_strcmp0(incoming_bond_info->addr, address)) {
738 BT_DBG("Previous Bond address: [%s] differs from new address: [%s]",
739 address, incoming_bond_info->addr);
740 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
741 incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
745 BT_INFO("Incoming bond successfully completed");
746 g_strlcpy(incoming_bond_info->addr, address, BT_ADDRESS_STRING_SIZE);
747 incoming_bond_info->dev_info = NULL;
749 _bt_convert_addr_string_to_type(dev_addr.addr, incoming_bond_info->addr);
750 if (BLUETOOTH_ERROR_NONE == _bt_device_get_bonded_device_info(&dev_addr)) {
751 BT_DBG("Bonded device info query posted to stack successfully");
753 BT_ERR("Bonded device info query failed");
754 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
760 static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr)
762 gchar address[BT_ADDRESS_STR_LEN];
763 bluetooth_device_address_t dev_addr;
767 if (trigger_bond_info == NULL) {
769 BT_DBG("trigger_bond_info == NULL, Handle incomming bond event");
770 __handle_incoming_bond_created_event(bd_addr);
774 _bt_convert_addr_type_to_string(address, bd_addr->addr);
775 if (g_strcmp0(trigger_bond_info->addr, address)) {
776 BT_DBG("Bonding address= [%s] is different from requested address =[%s]",
777 address, trigger_bond_info->addr);
778 __handle_incoming_bond_created_event(bd_addr);
782 BT_INFO("Bonding successfully completed");
783 /* Bonding state will be cleaned up & BONDING FINISHED EVENT
784 will be sent only when Properties are fetched from stack
785 Till that time lets not free trigger_bond_info.
786 However it is possible that while fetching device properties, internal
787 stack error can occur which can lead to no valid properties or
788 no properties at all. So in such cases, we must not wait for properties,
789 otherwise, it can lead to infinite wait */
790 _bt_convert_addr_string_to_type(dev_addr.addr,
791 trigger_bond_info->addr);
793 if (_bt_device_get_bonded_device_info(&dev_addr) == BLUETOOTH_ERROR_NONE) {
794 BT_DBG("BOnded device info query posted to stack successfully");
795 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_BOND_DEVICE,
796 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
798 BT_DBG("Possibly internal stack error in bonded device info query, perform cleanup");
799 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
800 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
801 /* Destroy if at all device got bonded at stack level */
802 device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
804 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
805 __bt_free_pairing_info(&trigger_pairing_info);
811 /**********************************************************************************************
812 * Bond removal event can be triggered for following reasons -
813 * a. If Bonding procedure if failed (for Auth failed, Page timeout, cancelled by user etc)
814 * b. If Application requests for explicitly removing the bond
815 * c. When application attempt to create bond,bond is removed first which triggers this event
816 * c. is in-line with Bluedroid bond create\emoval architecture
817 *********************************************************************************************/
818 static void __bt_device_handle_bond_removal_event(bt_address_t *bd_addr)
820 char address[BT_ADDRESS_STRING_SIZE];
824 _bt_convert_addr_type_to_string(address, bd_addr->addr);
825 _bt_service_remove_device_from_bonded_list(address);
827 if (trigger_unbond_info) {
828 BT_INFO("Bond removal request successfully handled, return DBUS and send event");
829 GVariant *param = NULL;
830 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_UNBOND_DEVICE,
831 trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
832 param = g_variant_new("(is)", BLUETOOTH_ERROR_NONE, trigger_unbond_info->addr);
833 _bt_send_event(BT_ADAPTER_EVENT,
834 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
836 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
837 __bt_free_pairing_info(&trigger_pairing_info);
838 } else if (trigger_bond_info) {
839 BT_ERR("Bonding was removed");
840 __bt_device_handle_bond_state();
845 static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_fail_event)
848 oal_status_t status = bond_fail_event->status;
849 BT_INFO("Bonding failed, reason: %d", status);
852 case OAL_STATUS_RMT_DEVICE_DOWN:
854 if (trigger_bond_info) {
855 BT_INFO("OAL_STATUS_RMT_DEVICE_DOWN:Lets retry bonding!! retry count [%d]",
857 int ret = OAL_STATUS_SUCCESS;
858 if (bond_retry_count < MAX_BOND_RETRY_COUNT) {
859 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
860 if (ret != OAL_STATUS_SUCCESS) {
861 BT_ERR("Create Bond procedure could not suceed");
862 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
863 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
864 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
865 __bt_free_pairing_info(&trigger_pairing_info);
866 bond_retry_count = 0;
870 BT_ERR("Create Bond failed MAX_BOND_RETRY_COUNT TIMES!!");
871 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
872 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
873 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
874 __bt_free_pairing_info(&trigger_pairing_info);
875 bond_retry_count = 0;
880 case OAL_STATUS_AUTH_FAILED:
882 /*TODO Auto pairing status set & ignore auto pairing logics can be done at this point.
883 To be considered later*/
884 int result = BLUETOOTH_ERROR_INTERNAL;
885 BT_INFO("BT_OPERATION_STATUS_AUTH_FAILED");
886 if (trigger_bond_info) {
887 BT_ERR("Create Bond procedure could not suceed, check if cancelled by User");
888 if (trigger_bond_info->is_cancelled_by_user) {
889 BT_ERR("Bonding is cancelled by user");
890 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
892 __bt_device_handle_pending_requests(result, BT_BOND_DEVICE,
893 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
894 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
897 __bt_free_pairing_info(&trigger_pairing_info);
900 case OAL_STATUS_INTERNAL_ERROR:
902 BT_INFO("OAL_STATUS_INTERNAL_ERROR");
903 if (trigger_unbond_info) {
904 BT_INFO("Bond removal request failed, return DBUS and send event");
905 GVariant *param = NULL;
906 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_UNBOND_DEVICE,
907 trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
908 param = g_variant_new("(is)", BLUETOOTH_ERROR_INTERNAL, trigger_unbond_info->addr);
909 _bt_send_event(BT_ADAPTER_EVENT,
910 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
912 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
913 } else if (trigger_bond_info) {
914 if (__bt_device_handle_bond_state() != BLUETOOTH_ERROR_NONE) {
915 if (trigger_bond_info) {
916 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
917 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
918 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
923 __bt_free_pairing_info(&trigger_pairing_info);
928 BT_ERR("Unknown status of Bond failed event status [%d]", status);
936 static void __bt_device_event_handler(int event_type, gpointer event_data)
938 int eventcheck = OAL_EVENT_DEVICE_PROPERTIES;
939 BT_INFO("event [%d] Event check = [%d]", event_type, eventcheck);
941 switch (event_type) {
942 case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY: {
943 BT_INFO("BREDR Device Found");
944 __bt_device_remote_device_found_callback(event_data, FALSE);
947 case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE: {
948 BT_INFO("Dual Device Found");
949 __bt_device_remote_device_found_callback(event_data, FALSE);
952 case OAL_EVENT_DEVICE_PROPERTIES: {
953 BT_INFO("Remote Device properties Received");
954 __bt_device_remote_properties_callback((event_dev_properties_t *)event_data);
957 case OAL_EVENT_DEVICE_BONDING_SUCCESS: {
958 BT_INFO("Bonding Success event Received");
959 __bt_device_handle_bond_completion_event((bt_address_t *)event_data);
962 case OAL_EVENT_DEVICE_BONDING_REMOVED: {
963 BT_INFO("Bonding Removed event Received");
964 __bt_device_handle_bond_removal_event((bt_address_t *)event_data);
967 case OAL_EVENT_DEVICE_BONDING_FAILED: {
968 BT_INFO("Bonding Failed event Received");
969 __bt_device_handle_bond_failed_event((event_dev_bond_failed_t*) event_data);
972 case OAL_EVENT_DEVICE_ACL_CONNECTED: {
973 BT_INFO("ACL Connected event Received");
974 event_dev_conn_status_t* param = event_data;
975 __bt_device_acl_state_changed_callback(param, TRUE, 0);
978 case OAL_EVENT_DEVICE_ACL_DISCONNECTED: {
979 BT_INFO("ACL Disconnected event Received");
980 __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 0);
983 case OAL_EVENT_DEVICE_LE_CONNECTED: {
984 BT_INFO("LE Connected event Received");
985 event_dev_conn_status_t* param = event_data;
986 __bt_device_acl_state_changed_callback(param, TRUE, 1);
989 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
990 BT_INFO("LE Disconnected event Received");
991 __bt_device_acl_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 1);
994 case OAL_EVENT_DEVICE_PIN_REQUEST: {
995 BT_INFO("PIN Request Received");
996 __bt_device_pin_request_callback((remote_device_t*)event_data);
999 case OAL_EVENT_DEVICE_PASSKEY_ENTRY_REQUEST: {
1000 BT_INFO("Passkey Entry request Received");
1001 __bt_device_ssp_passkey_entry_callback((remote_device_t*)event_data);
1004 case OAL_EVENT_DEVICE_PASSKEY_CONFIRMATION_REQUEST:{
1005 BT_INFO("Passkey Confirmation Request Received");
1006 __bt_device_ssp_passkey_confirmation_callback((event_dev_passkey_t *)event_data);
1009 case OAL_EVENT_DEVICE_PASSKEY_DISPLAY: {
1010 BT_INFO("Passkey Display Request Received");
1011 __bt_device_ssp_passkey_display_callback((event_dev_passkey_t *)event_data);
1014 case OAL_EVENT_DEVICE_SSP_CONSENT_REQUEST: {
1015 BT_INFO("SSP Consent Request Received");
1016 __bt_device_ssp_consent_callback((remote_device_t*)event_data);
1019 case OAL_EVENT_DEVICE_SERVICES: {
1020 BT_INFO("Remote Device Services Received");
1021 __bt_device_services_callback((event_dev_services_t*)event_data);
1024 case OAL_EVENT_DEVICE_AUTHORIZE_REQUEST: {
1025 BT_INFO("Remote Device Authorization Request");
1026 __bt_device_authorization_request_callback((event_dev_authorize_req_t*)event_data);
1029 case OAL_EVENT_DEVICE_TRUSTED: {
1030 BT_INFO("Remote Device Trusted");
1031 __bt_device_trusted_callback(TRUE, (event_dev_trust_t*)event_data);
1034 case OAL_EVENT_DEVICE_UNTRUSTED: {
1035 BT_INFO("Remote Device UnTrusted");
1036 __bt_device_trusted_callback(FALSE, (event_dev_trust_t*)event_data);
1039 case OAL_EVENT_DEVICE_NAME: {
1040 remote_device_t *rem_dev = event_data;
1041 gchar address[BT_ADDRESS_STR_LEN];
1043 _bt_convert_addr_type_to_string(address, rem_dev->address.addr);
1044 BT_INFO("Remote Device name Received");
1045 BT_INFO("Name: %s, Address: %s", rem_dev->name, address);
1047 /* Update local cache */
1048 _bt_update_remote_dev_property(address, DEV_PROP_NAME, (void *)rem_dev->name);
1051 case OAL_EVENT_DEVICE_TRUSTED_PROFILES_CHANGED: {
1052 event_device_trusted_profiles_t *ev = event_data;
1053 char address[BT_ADDRESS_STRING_SIZE];
1057 _bt_convert_addr_type_to_string(address, ev->address.addr);
1058 _bt_send_event(BT_DEVICE_EVENT,
1059 BLUETOOTH_EVENT_SUPPORTED_PROFILE_TRUSTED,
1060 g_variant_new("(isi)", BLUETOOTH_ERROR_NONE,
1061 address, ev->trust_val));
1064 case OAL_EVENT_RSSI_MONITORING_ENABLED: {
1065 event_dev_rssi_info_t *ev = event_data;
1066 char address[BT_ADDRESS_STRING_SIZE];
1071 _bt_convert_addr_type_to_string(address, ev->address.addr);
1072 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1073 address, ev->link_type, TRUE);
1074 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1077 case OAL_EVENT_RSSI_MONITORING_DISABLED: {
1078 event_dev_rssi_info_t *ev = event_data;
1079 char address[BT_ADDRESS_STRING_SIZE];
1084 _bt_convert_addr_type_to_string(address, ev->address.addr);
1085 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1086 address, ev->link_type, FALSE);
1087 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1090 case OAL_EVENT_RSSI_ALERT_RECEIVED: {
1091 event_dev_rssi_info_t *ev = event_data;
1092 char address[BT_ADDRESS_STRING_SIZE];
1097 _bt_convert_addr_type_to_string(address, ev->address.addr);
1098 param = g_variant_new("(isiii)", BLUETOOTH_ERROR_NONE,
1099 address, ev->link_type, ev->alert_type, ev->rssi);
1100 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ALERT, param);
1103 case OAL_EVENT_RAW_RSSI_RECEIVED: {
1104 event_dev_rssi_info_t *ev = event_data;
1105 char address[BT_ADDRESS_STRING_SIZE];
1110 _bt_convert_addr_type_to_string(address, ev->address.addr);
1111 param = g_variant_new("(isii)", BLUETOOTH_ERROR_NONE,
1112 address, ev->link_type, ev->rssi);
1113 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RAW_RSSI, param);
1117 BT_INFO("Unhandled event..");
1121 /* Legacy Pairing event handler */
1122 static void __bt_device_pin_request_callback(remote_device_t* pin_req_event)
1125 char address[BT_ADDRESS_STRING_SIZE];
1126 char pin_code[BLUETOOTH_PIN_CODE_MAX_LENGTH + 1];
1127 bool incoming = false;
1130 _bt_convert_addr_type_to_string(address, pin_req_event->address.addr);
1132 BT_INFO("Address[%s]", address);
1133 BT_INFO("Name[%s]", pin_req_event->name);
1134 BT_INFO("COD[%d]", pin_req_event->cod);
1136 if (trigger_pairing_info) {
1137 /* BTAPI support only one pairing at a time */
1138 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1139 BT_ERR("New PIN request address [%s]", address);
1140 device_reject_pin_request(&pin_req_event->address);
1144 /* If user initiated bonding and auto response is possible, just reply with default 0000*/
1145 if (_bt_is_bonding_device_address(address) == TRUE &&
1146 _bt_agent_is_auto_response(pin_req_event->cod, address, pin_req_event->name)) {
1147 /* Note: Currently even if SYSPOPUP is supported, we use Fixed PIN "0000" for basic pairing
1148 as BT SYSPOPUP is currently not working for PIN KEY entry in Tizen platform. This needs
1149 to be checked and fixed apropriately */
1150 _bt_set_autopair_status_in_bonding_info(TRUE);
1151 device_accept_pin_request(&pin_req_event->address, "0000");
1152 } else if (_bt_agent_is_hid_keyboard(pin_req_event->cod)) {
1153 BT_DBG("Remote Device is HID keyboard Type..");
1154 char str_passkey[BT_PASSKEY_MAX_LENGTH + 1] = { 0 };
1156 if (_bt_agent_generate_passkey(str_passkey,
1157 BT_PASSKEY_MAX_LENGTH) != 0) {
1158 device_reject_pin_request(&pin_req_event->address);
1161 device_accept_pin_request(&pin_req_event->address, str_passkey);
1163 BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1165 if(trigger_bond_info == NULL)
1168 param = g_variant_new("(bsss)", incoming, address, pin_req_event->name, str_passkey);
1169 _bt_send_event(BT_ADAPTER_EVENT,
1170 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1171 BT_DBG("Key board pairing in process");
1172 } else if (BLUETOOTH_ERROR_NONE == __bt_get_device_pin_code(address, pin_code)) {
1173 BT_DBG("Use stored PIN code [%s]", pin_code);
1174 device_accept_pin_request(&pin_req_event->address, pin_code);
1176 if (_bt_is_bonding_device_address(address) == TRUE) {
1177 BT_DBG("Show Pin entry");
1178 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1179 trigger_pairing_info->addr = g_strdup(address);
1180 trigger_pairing_info->is_ssp = FALSE;
1182 BT_DBG("Send BLUETOOTH_EVENT_PIN_REQUEST");
1184 if(trigger_bond_info == NULL)
1187 param = g_variant_new("(bss)", incoming, address, pin_req_event->name);
1188 _bt_send_event(BT_ADAPTER_EVENT,
1189 BLUETOOTH_EVENT_PIN_REQUEST, param);
1194 _bt_agent_release_memory();
1198 /* SSP Pairing event handler */
1199 static void __bt_device_ssp_passkey_entry_callback(remote_device_t* dev_info)
1202 gchar address[BT_ADDRESS_STR_LEN];
1205 bool incoming = false;
1209 _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1211 name = dev_info->name;
1213 BT_INFO("Address[%s]", address);
1214 BT_INFO("Name[%s]", name);
1215 BT_INFO("COD[%d]", dev_info->cod);
1217 if (trigger_pairing_info) {
1218 /* BTAPI support only one pairing at a time */
1219 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1220 BT_ERR("New PIN request address [%s]", address);
1221 device_reject_pin_request(&dev_info->address);
1226 /* Set pairing data */
1227 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1228 trigger_pairing_info->addr = g_strdup(address);
1229 trigger_pairing_info->is_ssp = TRUE;
1231 if(trigger_bond_info == NULL)
1234 param = g_variant_new("(bss)", incoming, p_addr, name);
1235 _bt_send_event(BT_ADAPTER_EVENT,
1236 BLUETOOTH_EVENT_PASSKEY_REQUEST, param);
1240 static void __bt_device_ssp_passkey_confirmation_callback(event_dev_passkey_t *dev_info)
1243 gchar address[BT_ADDRESS_STR_LEN];
1246 char str_passkey[7];
1247 bool incoming = false; /*Stores if bonding request is incoming(true) or outgoing(false) */
1250 _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1252 name = dev_info->device_info.name;
1254 BT_INFO("Address[%s]", address);
1255 BT_INFO("Name[%s]", name);
1256 BT_INFO("COD[%d]", dev_info->device_info.cod);
1258 if (trigger_pairing_info) {
1259 /* BTAPI support only one pairing at a time */
1260 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1261 BT_ERR("New PIN request address [%s]", address);
1262 device_reject_pin_request(&dev_info->device_info.address);
1267 /* Set pairing data */
1268 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1269 trigger_pairing_info->addr = g_strdup(address);
1270 trigger_pairing_info->is_ssp = TRUE;
1272 BT_DBG("Send BLUETOOTH_EVENT_PASSKEY_CONFIRMATION");
1273 snprintf(str_passkey, sizeof(str_passkey), "%.6d", dev_info->pass_key);
1275 /*Storing if bond is incoming or outgoing*/
1276 if(trigger_bond_info == NULL)
1279 param = g_variant_new("(bsss)", incoming, p_addr, name, str_passkey);
1280 _bt_send_event(BT_ADAPTER_EVENT,
1281 BLUETOOTH_EVENT_PASSKEY_CONFIRM_REQUEST, param);
1285 static void __bt_device_ssp_passkey_display_callback(event_dev_passkey_t *dev_info)
1288 gchar address[BT_ADDRESS_STR_LEN];
1291 char str_passkey[7];
1292 bool incoming = false;
1295 _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1297 name = dev_info->device_info.name;
1299 BT_INFO("Address[%s]", address);
1300 BT_INFO("Name[%s]", name);
1301 BT_INFO("COD[%d]", dev_info->device_info.cod);
1303 if (trigger_pairing_info) {
1304 /* BTAPI support only one pairing at a time */
1305 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1306 BT_ERR("New PIN request address [%s]", address);
1307 device_reject_pin_request(&dev_info->device_info.address);
1312 /* Set pairing data */
1313 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1314 trigger_pairing_info->addr = g_strdup(address);
1315 trigger_pairing_info->is_ssp = TRUE;
1317 BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1318 snprintf(str_passkey, sizeof(str_passkey), "%06d", dev_info->pass_key);
1320 if(trigger_bond_info == NULL)
1323 param = g_variant_new("(bsss)", incoming, p_addr, name, str_passkey);
1325 if (passkey_watcher) {
1326 BT_INFO("Send passkey to %s", passkey_watcher);
1327 _bt_send_event_to_dest(passkey_watcher, BT_ADAPTER_EVENT,
1328 BLUETOOTH_EVENT_PASSKEY_NOTIFICATION, param);
1330 _bt_send_event(BT_ADAPTER_EVENT,
1331 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1336 static void __bt_device_ssp_consent_callback(remote_device_t* dev_info)
1338 gchar address[BT_ADDRESS_STR_LEN];
1345 _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1346 name = dev_info->name;
1347 cod = dev_info->cod;
1349 BT_INFO("Address[%s]", address);
1350 BT_INFO("Name[%s]", name);
1351 BT_INFO("COD[%d]", cod);
1353 if (trigger_pairing_info) {
1354 /* BTAPI support only one pairing at a time */
1355 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1356 BT_ERR("New PIN request address [%s]", address);
1357 device_reject_pin_request(&dev_info->address);
1362 /* Set pairing data */
1363 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1364 trigger_pairing_info->addr = g_strdup(address);
1365 trigger_pairing_info->is_ssp = TRUE;
1367 local_major = ((cod >> 8) & 0x001f);
1368 local_minor = (cod & 0x00fc);
1369 BT_DBG("SSP_CONSENT: Major type=[0x%x] and Minor type=[0x%x]", local_major, local_minor);
1371 /*TODO: BLUETOOTH_EVENT_SSP_CONSENT_REQUEST to be handled in Tizen */
1375 static int __bt_oal_status_to_bt_error(int oal_status)
1379 switch (oal_status) {
1380 case OAL_STATUS_SUCCESS:
1381 ret = BLUETOOTH_ERROR_NONE;
1383 case OAL_STATUS_CONN_TIMEOUT:
1384 case OAL_STATUS_LINK_LOSS:
1385 BT_INFO("Connection Timeout");
1386 ret = BLUETOOTH_ERROR_CONNECTION_TIMEOUT;
1389 case OAL_STATUS_CONN_TERM_LOCAL_HOST:
1390 ret = BLUETOOTH_ERROR_LOCAL_HOST_TERM;
1392 case OAL_STATUS_CONN_TERM_RMT_HOST:
1393 ret = BLUETOOTH_ERROR_REMOTE_USER_TERM;
1396 case OAL_STATUS_INTERNAL_ERROR:
1397 ret = BLUETOOTH_ERROR_INTERNAL;
1400 ret = BLUETOOTH_ERROR_INTERNAL;
1406 static void __bt_device_acl_state_changed_callback(event_dev_conn_status_t *acl_event,
1407 gboolean connected, unsigned char type)
1409 gchar address[BT_ADDRESS_STR_LEN];
1410 int result = BLUETOOTH_ERROR_NONE;
1411 GVariant *param = NULL;
1412 bt_device_conn_info_t conn_info;
1415 _bt_convert_addr_type_to_string(address, acl_event->address.addr);
1417 _bt_logging_connection(connected, type);
1419 result = __bt_oal_status_to_bt_error(acl_event->status);
1420 BT_INFO("Result [0x%x]", result);
1423 param = g_variant_new("(isy)", result, address, type);
1424 _bt_send_event(BT_DEVICE_EVENT,
1425 BLUETOOTH_EVENT_DEVICE_CONNECTED,
1428 param = g_variant_new("(isy)", result, address, type);
1429 _bt_send_event(BT_DEVICE_EVENT,
1430 BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1434 conn_info.connected = connected;
1435 conn_info.type = type;
1436 /* Update local cache */
1437 _bt_update_remote_dev_property(address, DEV_PROP_CONNECTED, (void *)&conn_info);
1439 #ifdef TIZEN_GATT_CLIENT
1440 /*handle LE connected device info*/
1442 BT_DBG("handle LE connected device info");
1443 _bt_handle_le_connected_dev_info(address, connected);
1450 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble)
1452 bt_remote_dev_info_t *dev_info = NULL;
1453 int result = BLUETOOTH_ERROR_NONE;
1454 GVariant *param = NULL;
1455 GVariant *uuids = NULL;
1456 GVariant *manufacturer_data = NULL;
1457 GVariantBuilder *builder = NULL;
1461 ret_if(_bt_is_discovering() == FALSE);
1462 ret_if(event_data == NULL);
1464 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
1467 event_ble_dev_found_t * oal_ble_dev = event_data;
1468 BT_INFO("Device type [%d]", oal_ble_dev->device_info.type);
1470 _bt_copy_remote_dev(dev_info, &oal_ble_dev->device_info);
1472 dev_info->manufacturer_data_len = oal_ble_dev->adv_len;
1473 if (dev_info->manufacturer_data_len)
1474 dev_info->manufacturer_data = g_memdup(oal_ble_dev->adv_data,
1475 dev_info->manufacturer_data_len);
1477 dev_info->manufacturer_data = NULL;
1478 BT_DBG("----Advertising Data Length: %d", dev_info->manufacturer_data_len);
1480 event_dev_found_t * oal_dev = event_data;
1481 _bt_copy_remote_dev(dev_info, &oal_dev->device_info);
1484 /* If Remote device name is NULL or still RNR is not done then display address as name. */
1485 if (dev_info->name == NULL)
1486 dev_info->name = g_strdup(dev_info->address);
1487 BT_DBG("Name %s", dev_info->name);
1489 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1490 for (i = 0; i < dev_info->uuid_count; i++)
1491 g_variant_builder_add(builder, "s", dev_info->uuids[i]);
1493 uuids = g_variant_new("as", builder);
1494 g_variant_builder_unref(builder);
1496 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1497 dev_info->manufacturer_data,
1498 dev_info->manufacturer_data_len,
1502 param = g_variant_new("(isunsbub@asn@ay)", result,
1508 dev_info->connected,
1511 dev_info->manufacturer_data_len,
1514 _bt_send_event(BT_ADAPTER_EVENT,
1515 BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
1518 _bt_free_remote_dev(dev_info);
1522 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info)
1524 gchar address[BT_ADDRESS_STR_LEN];
1525 int result = BLUETOOTH_ERROR_NONE;
1526 GVariant *param = NULL;
1530 _bt_convert_addr_type_to_string(address, info->address.addr);
1532 /* Update local cache */
1533 _bt_update_remote_dev_property(address, DEV_PROP_TRUST, (void *)&trusted);
1535 param = g_variant_new("(is)", result, address);
1536 event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1537 BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1538 /* Send event to application */
1539 _bt_send_event(BT_DEVICE_EVENT,
1546 static void __bt_free_pairing_info(bt_pairing_data_t **p_info)
1549 bt_pairing_data_t * info = *p_info;
1560 static void __bt_free_bond_info(uint8_t type)
1565 case BT_DEVICE_BOND_INFO:
1566 if (!trigger_bond_info)
1569 if (trigger_bond_info->addr)
1570 g_free(trigger_bond_info->addr);
1571 if (trigger_bond_info->dev_addr)
1572 g_free(trigger_bond_info->dev_addr);
1573 if (trigger_bond_info->dev_info)
1574 _bt_free_remote_dev(trigger_bond_info->dev_info);
1575 g_free(trigger_bond_info);
1576 trigger_bond_info = NULL;
1578 case BT_DEVICE_INCOMING_BOND_INFO:
1579 if (!incoming_bond_info)
1582 if (incoming_bond_info->dev_info)
1583 _bt_free_remote_dev(incoming_bond_info->dev_info);
1584 g_free(incoming_bond_info);
1585 incoming_bond_info = NULL;
1587 case BT_DEVICE_UNBOND_INFO:
1588 if (!trigger_unbond_info)
1591 if (trigger_unbond_info->addr)
1592 g_free(trigger_unbond_info->addr);
1593 if (trigger_unbond_info->dev_addr)
1594 g_free(trigger_unbond_info->dev_addr);
1595 if (trigger_unbond_info->dev_info)
1596 _bt_free_remote_dev(trigger_unbond_info->dev_info);
1597 g_free(trigger_unbond_info);
1598 trigger_unbond_info = NULL;
1604 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info)
1606 bt_service_search_info_data_t * info = *p_info;
1613 if (info->dev_addr) {
1614 g_free(info->dev_addr);
1615 info->dev_addr = NULL;
1618 if (info->dev_info) {
1619 _bt_free_remote_dev(info->dev_info);
1620 info->dev_info = NULL;
1628 static int __bt_device_handle_bond_state(void)
1630 BT_INFO("Current Bond state: %d", bt_device_bond_state);
1631 int ret = OAL_STATUS_INTERNAL_ERROR;
1633 switch (bt_device_bond_state) {
1634 case BT_DEVICE_BOND_STATE_CANCEL_DISCOVERY:
1635 /*TODO:Bonding during discovery: Unhandled!!*/
1636 BT_INFO("Bonding during discovery: Unhandled!!");
1638 case BT_DEVICE_BOND_STATE_DISCOVERY_CANCELLED:
1639 /*TODO:Bonding during discovery: Unhandled!!*/
1640 BT_INFO("Bonding during discovery: Unhandled!!");
1642 case BT_DEVICE_BOND_STATE_REMOVE_BONDING:
1643 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVED_BONDING;
1644 ret = device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
1645 if (ret != OAL_STATUS_SUCCESS)
1646 ret = __bt_device_handle_bond_state();
1648 case BT_DEVICE_BOND_STATE_REMOVED_BONDING:
1649 bt_device_bond_state = BT_DEVICE_BOND_STATE_NONE;
1650 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
1651 /* Bonding procedure was started but unfortunately could not complete.
1652 Basically removed bonding was success, but create bond request could not proceed
1653 So lets cleanup the context */
1654 if (ret != OAL_STATUS_SUCCESS) {
1655 BT_ERR("Create Bond procedure could not suceed");
1656 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1657 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1658 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1659 __bt_free_pairing_info(&trigger_pairing_info);
1662 case BT_DEVICE_BOND_STATE_NONE:
1663 BT_INFO("Create Bond failed!!");
1664 if (trigger_bond_info) {
1665 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1666 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1667 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1668 __bt_free_pairing_info(&trigger_pairing_info);
1675 if (ret != OAL_STATUS_SUCCESS)
1676 return BLUETOOTH_ERROR_INTERNAL;
1678 return BLUETOOTH_ERROR_NONE;
1681 int _bt_device_get_bonded_device_info(bluetooth_device_address_t *addr)
1684 bt_address_t bd_addr;
1688 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
1690 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
1691 result = device_query_attributes(&bd_addr);
1692 if (result != OAL_STATUS_SUCCESS) {
1693 BT_ERR("device_query_attributes error: [%d]", result);
1694 return BLUETOOTH_ERROR_INTERNAL;
1698 return BLUETOOTH_ERROR_NONE;
1701 int _bt_set_alias(bluetooth_device_address_t *device_address, const char *alias)
1704 char address[BT_ADDRESS_STRING_SIZE];
1707 BT_CHECK_PARAMETER(alias, return);
1709 ret = device_set_alias((bt_address_t *)device_address, (char *)alias);
1710 if (ret != OAL_STATUS_SUCCESS) {
1711 BT_ERR("device_set_alias: %d", ret);
1712 return BLUETOOTH_ERROR_INTERNAL;
1715 /* Update local cache */
1716 _bt_convert_addr_type_to_string(address, device_address->addr);
1717 _bt_update_remote_dev_property(address, DEV_PROP_ALIAS, (void *)alias);
1720 return BLUETOOTH_ERROR_NONE;
1723 int _bt_bond_device(bluetooth_device_address_t *device_address,
1724 unsigned short conn_type, GArray **out_param1)
1726 int result = BLUETOOTH_ERROR_NONE;
1727 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1728 bluetooth_device_info_t dev_info;
1731 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1733 /* If bonding or discovery already going on */
1734 if (trigger_bond_info || _bt_is_discovering()) {
1735 BT_ERR("Device is buzy, bonding can not proceed now..");
1736 result = BLUETOOTH_ERROR_DEVICE_BUSY;
1740 /*TODO: If unbonding with same device going on */
1741 _bt_convert_addr_type_to_string(address, device_address->addr);
1743 trigger_bond_info = g_malloc0(sizeof(bt_bond_data_t));
1744 trigger_bond_info->addr = g_strdup(address);
1745 trigger_bond_info->conn_type = conn_type;
1746 trigger_bond_info->is_device_creating = TRUE;
1747 trigger_bond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1748 trigger_bond_info->dev_info = NULL;
1750 /* Ready to initiate bonding */
1752 /* In Tizen, we will first remove bond and then attempt to create bond to keep
1753 consistency with bluedroid. Even if remove bond fails due to device not already
1754 bonded, then straight away create bond is triggered. This is because, remove bond
1755 is handled differently in bluedroid and bluez. In Bluez, if device is
1756 already removed, remove bond call fails.
1757 However in bluedroid, remove bond on already removed device returns success. So we will
1758 handle the cases transparently*/
1759 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVE_BONDING;
1760 bond_retry_count = 0;
1761 result = __bt_device_handle_bond_state();
1763 if (result != BLUETOOTH_ERROR_NONE)
1770 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1771 memcpy(dev_info.device_address.addr, device_address->addr,
1772 BLUETOOTH_ADDRESS_LENGTH);
1774 g_array_append_vals(*out_param1, &dev_info,
1775 sizeof(bluetooth_device_info_t));
1776 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1782 int _bt_unbond_device(bluetooth_device_address_t *device_address,
1783 GArray **out_param1)
1785 int result = OAL_STATUS_SUCCESS;
1786 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1787 bluetooth_device_info_t dev_info;
1790 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1792 _bt_convert_addr_type_to_string(address, device_address->addr);
1794 trigger_unbond_info = g_malloc0(sizeof(bt_bond_data_t));
1795 trigger_unbond_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1796 trigger_unbond_info->addr = g_strdup(address);
1797 trigger_unbond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1799 /* Check if Bonding is already going on, we should not abruptly remove bonding*/
1800 if (trigger_bond_info && strncmp(trigger_bond_info->addr, trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE) == 0) {
1801 BT_ERR("Bonding with same device already ongoing");
1802 result = BLUETOOTH_ERROR_PERMISSION_DEINED;
1806 result = device_destroy_bond((bt_address_t *)device_address);
1807 if (result != OAL_STATUS_SUCCESS)
1813 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1814 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1815 trigger_unbond_info->addr);
1817 g_array_append_vals(*out_param1, &dev_info,
1818 sizeof(bluetooth_device_info_t));
1819 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
1824 int _bt_cancel_bonding(void)
1826 int result = OAL_STATUS_SUCCESS;
1829 retv_if(trigger_bond_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1831 result = device_stop_bond((bt_address_t *)trigger_bond_info->dev_addr);
1833 if (result == OAL_STATUS_SUCCESS)
1834 trigger_bond_info->is_cancelled_by_user = TRUE;
1839 int _bt_passkey_reply(const char *passkey, gboolean cnfm_reply)
1841 bluetooth_device_address_t device_address;
1842 int ret = OAL_STATUS_SUCCESS;
1843 BT_INFO("reply: %d", cnfm_reply);
1845 retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1846 retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1848 _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1850 if (trigger_pairing_info->is_ssp) {
1852 ret = device_accept_passkey_entry((bt_address_t *)&device_address, atoi(passkey));
1854 ret = device_reject_passkey_entry((bt_address_t *)&device_address);
1855 trigger_pairing_info->is_ssp = FALSE;
1858 ret = device_accept_pin_request((bt_address_t *)&device_address, passkey);
1860 ret = device_reject_pin_request((bt_address_t *)&device_address);
1863 __bt_free_pairing_info(&trigger_pairing_info);
1865 if (ret != OAL_STATUS_SUCCESS) {
1866 BT_ERR("_bt_device_handle_passkey_reply: err [%d]", ret);
1867 return BLUETOOTH_ERROR_INTERNAL;
1871 return BLUETOOTH_ERROR_NONE;
1874 int _bt_passkey_confirmation_reply(gboolean cnfm_reply)
1876 BT_INFO("BT_PASSKEY_CONFIRMATION_REPLY");
1877 bluetooth_device_address_t device_address;
1878 int ret = OAL_STATUS_SUCCESS;
1879 BT_INFO("reply: %d", cnfm_reply);
1881 retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1882 retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1884 _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1886 ret = device_reply_passkey_confirmation((bt_address_t *)&device_address, cnfm_reply);
1888 __bt_free_pairing_info(&trigger_pairing_info);
1889 if (ret != OAL_STATUS_SUCCESS) {
1890 BT_ERR("_bt_device_handle_passkey_confirmation_reply: err [%d]", ret);
1891 return BLUETOOTH_ERROR_INTERNAL;
1895 return BLUETOOTH_ERROR_NONE;
1898 gboolean _bt_device_is_pairing(void)
1900 return (trigger_pairing_info) ? TRUE : FALSE;
1903 gboolean _bt_device_is_bonding(void)
1905 return (trigger_bond_info) ? TRUE : FALSE;
1908 gboolean _bt_is_bonding_device_address(const char *address)
1910 if (trigger_bond_info == NULL || trigger_bond_info->addr == NULL)
1913 if (g_strcmp0(trigger_bond_info->addr, address) == 0) {
1914 BT_DBG("[%s] is bonding device", address);
1918 BT_DBG("[%s] is NOT bonding device", address);
1922 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
1924 ret_if(trigger_bond_info == NULL);
1925 trigger_bond_info->is_autopair = is_autopair;
1928 int _bt_search_device(bluetooth_device_address_t *device_address)
1930 int result = OAL_STATUS_SUCCESS;
1933 BT_CHECK_PARAMETER(device_address, return);
1935 if (trigger_bond_info) {
1936 BT_ERR("Bonding in progress");
1937 return BLUETOOTH_ERROR_DEVICE_BUSY;
1940 if (service_search_info) {
1941 BT_ERR("Service searching in progress");
1942 return BLUETOOTH_ERROR_DEVICE_BUSY;
1945 /* allocate user data so that it can be retrieved in callback */
1946 service_search_info = g_malloc0(sizeof(bt_service_search_info_data_t));
1947 service_search_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1948 service_search_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1950 _bt_convert_addr_type_to_string(service_search_info->addr,
1951 device_address->addr);
1953 result = device_query_services((bt_address_t *)device_address);
1955 if (result != OAL_STATUS_SUCCESS) {
1956 BT_ERR("Device Service Search Failed..: %d", result);
1957 __bt_free_service_search_info(&service_search_info);
1958 return BLUETOOTH_ERROR_INTERNAL;
1960 return BLUETOOTH_ERROR_NONE;
1963 int _bt_cancel_search_device(void)
1965 int ret = OAL_STATUS_SUCCESS;
1966 retv_if(service_search_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1968 ret = device_stop_query_sevices((bt_address_t *)service_search_info->dev_addr);
1970 if (ret != OAL_STATUS_SUCCESS) {
1971 BT_ERR("SDP Cancel request failed [%d]", ret);
1972 return BLUETOOTH_ERROR_INTERNAL;
1975 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_CANCEL_BY_USER, BT_SEARCH_SERVICE,
1976 service_search_info->addr, BT_ADDRESS_STRING_SIZE);
1978 __bt_free_service_search_info(&service_search_info);
1980 return BLUETOOTH_ERROR_NONE;
1984 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1987 int ret = OAL_STATUS_SUCCESS;
1990 BT_CHECK_PARAMETER(device_address, return);
1991 BT_INFO("Device to be Trusted? [%d]", authorize);
1993 ret = device_set_authorized((bt_address_t*)device_address, authorize);
1994 if (ret != OAL_STATUS_SUCCESS) {
1995 BT_ERR("device_set_authorized: %d", ret);
1996 return BLUETOOTH_ERROR_INTERNAL;
1999 return BLUETOOTH_ERROR_NONE;
2002 gboolean _bt_is_device_connected(bluetooth_device_address_t *device_address, int svc_type)
2004 gboolean is_connected;
2005 oal_service_t svc_id;
2007 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
2010 * TODO: While adding support for new profiles, need to add more
2011 * <svc_type, svc_id> mapping here.
2014 case BT_PROFILE_CONN_HID:
2015 svc_id = HID_SERVICE_ID;
2017 case BT_PROFILE_CONN_A2DP:
2018 svc_id = A2DP_SERVICE_ID; /* Remote is A2DP Sink */
2020 case BT_PROFILE_CONN_A2DP_SINK:
2021 svc_id = A2DP_SRC_SERVICE_ID; /* Remote is A2DP Source*/
2023 case BT_PROFILE_CONN_HSP:
2024 svc_id = HFP_HS_SERVICE_ID; /* Remote is HFP HF Unit */
2026 #ifdef TIZEN_GATT_CLIENT
2027 case BT_PROFILE_CONN_GATT:
2028 return _bt_is_remote_gatt_device_connected(device_address); /* Remote is GATT client or Server */
2031 BT_DBG("Unknown svc_type: %d", svc_type);
2035 is_connected = device_get_svc_conn_state((bt_address_t*)device_address, svc_id);
2037 BT_DBG("svc_type: %d, is_connected: %s",
2038 svc_type, is_connected ? "TRUE" : "FALSE");
2040 return is_connected;
2043 int _bt_rfcomm_reply_conn_authorization(char *address, gboolean reply)
2045 bt_address_t bd_addr;
2050 retv_if(NULL == address, BLUETOOTH_ERROR_INVALID_PARAM);
2051 _bt_convert_addr_string_to_type(bd_addr.addr, address);
2052 res = device_reply_auth_request(&bd_addr, 0, reply, FALSE);
2053 if (res != OAL_STATUS_SUCCESS) {
2054 BT_ERR("authorize_response: %d", res);
2055 return BLUETOOTH_ERROR_INTERNAL;
2059 return BLUETOOTH_ERROR_NONE;
2062 int _bt_enable_rssi(bluetooth_device_address_t *addr, int link_type,
2063 int low_threshold, int in_range_threshold, int high_threshold)
2066 bt_address_t bd_addr;
2070 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2072 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2073 result = device_enable_rssi_monitoring(&bd_addr, link_type,
2074 low_threshold, in_range_threshold, high_threshold);
2075 if (result != OAL_STATUS_SUCCESS) {
2076 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2077 return BLUETOOTH_ERROR_INTERNAL;
2081 return BLUETOOTH_ERROR_NONE;
2084 int _bt_get_rssi_strength(bluetooth_device_address_t *addr, int link_type)
2087 bt_address_t bd_addr;
2091 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
2093 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
2094 result = device_get_connected_link_rssi_strength(&bd_addr, link_type);
2095 if (result != OAL_STATUS_SUCCESS) {
2096 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
2097 return BLUETOOTH_ERROR_INTERNAL;
2101 return BLUETOOTH_ERROR_NONE;
2104 int _bt_set_passkey_notification(const char *sender, gboolean enable)
2108 BT_INFO("Set passkey notification(sender:%s, %s)",
2109 sender, enable ? "Enable" : "Disable");
2111 result = device_enable_gap_auth_notifications(OAL_PASSKEY_DISPLAY, enable);
2112 if (OAL_STATUS_SUCCESS != result) {
2113 BT_ERR("device_enable_gap_auth_notifications error: [%d]", result);
2114 return BLUETOOTH_ERROR_INTERNAL;
2117 g_free(passkey_watcher);
2119 passkey_watcher = g_strdup(sender);
2121 passkey_watcher = NULL;
2123 return BLUETOOTH_ERROR_NONE;
2126 static int __bt_get_device_pin_code(const char *address, char *pin_code)
2130 BT_CHECK_PARAMETER(address, return);
2131 BT_CHECK_PARAMETER(pin_code, return);
2133 for (l = pin_info_list; l != NULL; l = l->next) {
2134 bt_pin_code_info_t *pin_info = l->data;
2136 if (!pin_info || !pin_info->address)
2139 if (g_strcmp0(pin_info->address, address) == 0) {
2140 g_strlcpy(pin_code, pin_info->pin_code,
2141 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2142 return BLUETOOTH_ERROR_NONE;
2146 return BLUETOOTH_ERROR_NOT_FOUND;
2149 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2150 bluetooth_device_pin_code_t *pin_code)
2152 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2153 bt_pin_code_info_t *pin_info = NULL;
2156 BT_CHECK_PARAMETER(device_address, return);
2157 BT_CHECK_PARAMETER(pin_code, return);
2158 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2159 BLUETOOTH_ERROR_NO_RESOURCES);
2161 _bt_convert_addr_type_to_string(address, device_address->addr);
2163 for (l = pin_info_list; l != NULL; l = l->next) {
2166 if (!pin_info || !pin_info->address)
2169 if (g_strcmp0(pin_info->address, address) == 0) {
2170 g_free(pin_info->pin_code);
2171 pin_info->pin_code = g_strdup(pin_code->pin_code);
2172 return BLUETOOTH_ERROR_NONE;
2176 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2177 pin_info->address = g_strdup(address);
2178 pin_info->pin_code = g_strdup(pin_code->pin_code);
2179 pin_info_list = g_slist_append(pin_info_list, pin_info);
2181 return BLUETOOTH_ERROR_NONE;
2184 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2186 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2187 bt_pin_code_info_t *pin_info = NULL;
2192 BT_CHECK_PARAMETER(device_address, return);
2194 _bt_convert_addr_type_to_string(address, device_address->addr);
2196 for (l = pin_info_list; l != NULL; l = l->next) {
2199 if (!pin_info || !pin_info->address)
2202 if (g_strcmp0(pin_info->address, address) == 0) {
2203 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2204 g_free(pin_info->address);
2205 g_free(pin_info->pin_code);
2212 return BLUETOOTH_ERROR_NONE;
2215 #ifdef TIZEN_GATT_CLIENT
2216 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2219 bt_connected_le_dev_t *dev;
2224 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2227 if (g_strcmp0(dev->address, address) == 0)
2233 static void __bt_le_conn_param_free(void *data)
2235 bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2237 BT_DBG("%s", param->sender);
2238 g_free(param->sender);
2242 static void _bt_add_le_connected_dev_info(const char *address)
2244 bt_connected_le_dev_t *dev = NULL;
2249 dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2250 dev->address = g_strdup(address);
2252 le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2257 static void _bt_remove_le_connected_dev_info(const char *address)
2259 bt_connected_le_dev_t *dev = NULL;
2264 dev = __bt_get_le_connected_dev_info(address);
2268 g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2269 le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2270 g_free(dev->address);
2277 static void _bt_handle_le_connected_dev_info(const char *address, gboolean connected)
2282 _bt_add_le_connected_dev_info(address);
2284 _bt_remove_le_connected_dev_info(address);
2287 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2290 bt_le_conn_param_t *param = NULL;
2292 if (!dev || !sender)
2295 for (l = dev->senders; l; l = g_slist_next(l)) {
2297 if (g_strcmp0(param->sender, sender) == 0)
2304 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2306 bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2307 bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2309 return parama->key > paramb->key;
2313 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2314 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2316 bt_connected_le_dev_t *dev = NULL;
2317 bt_le_conn_param_t *param = NULL;
2318 bt_le_conn_param_t *data = NULL;
2322 if (!address || !sender)
2323 return BLUETOOTH_ERROR_INVALID_PARAM;
2325 dev = __bt_get_le_connected_dev_info(address);
2327 return BLUETOOTH_ERROR_INTERNAL;
2329 param = __bt_get_le_conn_param_info(dev, sender);
2331 data = g_malloc0(sizeof(bt_le_conn_param_t));
2332 data->sender = g_strdup(sender);
2333 data->interval_min = interval_min;
2334 data->interval_max = interval_max;
2335 data->latency = latency;
2336 data->time_out = time_out;
2337 data->key = interval_min + (interval_max - interval_min)/2;
2339 if (param == NULL) {
2340 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2341 dev->senders = g_slist_append(dev->senders, data);
2343 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2344 dev->senders = g_slist_remove(dev->senders, param);
2345 g_free(param->sender);
2347 dev->senders = g_slist_append(dev->senders, data);
2350 /* Sorting. First element have the minimum interval */
2351 dev->senders = g_slist_sort(dev->senders,
2352 (GCompareFunc)__bt_compare_le_conn_param_key);
2354 return BLUETOOTH_ERROR_NONE;
2358 static int __bt_le_set_conn_parameter(const char *address,
2359 float interval_min, float interval_max,
2360 guint16 latency, guint16 time_out)
2362 bt_address_t dev_addr = { {0} };
2363 guint32 min, max, to;
2365 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2366 interval_min, interval_max, latency, time_out);
2368 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2369 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2370 to = time_out / BT_LE_CONN_TO_SPLIT;
2372 BT_INFO("updating: Min interval: %d, Max interval: %d, Latency: %d, Supervision timeout: %d",
2373 min, max, latency, to);
2375 _bt_convert_addr_string_to_type(dev_addr.addr, address);
2377 return gattc_conn_param_update(&dev_addr, min, max, latency, to);
2380 int _bt_remove_le_conn_param_info(const char *address, const char *sender)
2382 bt_connected_le_dev_t *dev = NULL;
2383 bt_le_conn_param_t *param = NULL;
2385 if (!address || !sender)
2386 return BLUETOOTH_ERROR_INVALID_PARAM;
2388 dev = __bt_get_le_connected_dev_info(address);
2390 return BLUETOOTH_ERROR_INTERNAL;
2392 param = __bt_get_le_conn_param_info(dev, sender);
2394 BT_DBG("Remove param %s %s ", address, sender);
2395 dev->senders = g_slist_remove(dev->senders, param);
2396 g_free(param->sender);
2400 return BLUETOOTH_ERROR_NONE;
2404 int _bt_le_connection_update(const char *sender,
2405 unsigned char *device_address,
2406 float interval_min, float interval_max,
2407 guint16 latency, guint16 time_out)
2409 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2410 guint32 min_supervision_to;
2411 bt_connected_le_dev_t *dev = NULL;
2412 bt_le_conn_param_t *param = NULL;
2413 int ret = BLUETOOTH_ERROR_NONE;
2415 BT_CHECK_PARAMETER(device_address, return);
2417 BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2418 sender, interval_min, interval_max, latency, time_out);
2420 if (interval_min > interval_max ||
2421 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2422 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2423 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2427 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2428 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2429 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2433 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2434 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2439 * The Supervision_Timeout in milliseconds shall be larger than
2440 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2441 * where Conn_Interval_Max is given in milliseconds.
2444 min_supervision_to = (1 + latency) * interval_max * 2;
2445 if (time_out <= min_supervision_to) {
2446 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2450 _bt_convert_addr_type_to_string(address, device_address);
2451 BT_DBG("Remote device address: %s", address);
2453 _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2455 dev = __bt_get_le_connected_dev_info(address);
2457 BT_DBG("device not found in the list");
2458 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2462 if (g_slist_length(dev->senders) == 1)
2465 param = dev->senders->data;
2467 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2469 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2470 BT_DBG("Skip due to same interval");
2474 interval_min = param->interval_min;
2475 interval_max = param->interval_max;
2479 ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2481 if (ret != OAL_STATUS_SUCCESS) {
2482 _bt_remove_le_conn_param_info(address, sender);
2483 BT_DBG("fail to update the LE connection parameter");
2484 ret = BLUETOOTH_ERROR_INTERNAL;
2488 BT_DBG("updated LE connection parameter");
2489 dev->interval_min = interval_min;
2490 dev->interval_max = interval_max;
2492 return BLUETOOTH_ERROR_NONE;