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"
50 #include <oal-event.h>
51 #include <oal-manager.h>
52 #include <oal-adapter-mgr.h>
53 #include <oal-device-mgr.h>
55 #define MAX_BOND_RETRY_COUNT 3
56 #define BT_PASSKEY_MAX_LENGTH 4
58 /* Bonding Info structure */
63 unsigned short conn_type;
64 gboolean is_cancelled_by_user;
65 gboolean is_device_creating;
66 bluetooth_device_address_t *dev_addr;
67 bt_remote_dev_info_t *dev_info;
70 /* Searching Info structure */
74 gboolean is_cancelled_by_user;
75 bluetooth_device_address_t *dev_addr;
76 bt_remote_dev_info_t *dev_info;
77 } bt_service_search_info_data_t;
79 /* Pairing Info structure */
87 char addr[BT_ADDRESS_STRING_SIZE];
88 bt_remote_dev_info_t *dev_info;
89 } bt_incoming_bond_data_t;
91 /* Bonding and Pairing Informations */
92 bt_bond_data_t *trigger_bond_info;
93 bt_bond_data_t *trigger_unbond_info;
94 bt_pairing_data_t *trigger_pairing_info;
95 bt_service_search_info_data_t *service_search_info;
97 bt_incoming_bond_data_t *incoming_bond_info;
100 BT_DEVICE_BOND_STATE_NONE,
101 BT_DEVICE_BOND_STATE_CANCEL_DISCOVERY,
102 BT_DEVICE_BOND_STATE_DISCOVERY_CANCELLED,
103 BT_DEVICE_BOND_STATE_REMOVE_BONDING,
104 BT_DEVICE_BOND_STATE_REMOVED_BONDING,
105 BT_DEVICE_BOND_STATE_STARTED,
106 BT_DEVICE_BOND_STATE_WAIT_PROP,
107 BT_DEVICE_BOND_STATE_WAIT_DID
112 BT_DEVICE_INCOMING_BOND_INFO,
113 BT_DEVICE_UNBOND_INFO
116 /* BT device bond state variable */
117 static bt_bond_state_e bt_device_bond_state;
118 static int bond_retry_count;
120 static char *passkey_watcher;
121 static GSList *pin_info_list = NULL;
123 /* Forward declaration */
124 static void __bt_device_event_handler(int event_type, gpointer event_data);
125 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble);
128 static int __bt_device_handle_bond_state(void);
129 static void __bt_free_bond_info(uint8_t type);
130 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info);
131 static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr);
132 static void __bt_device_handle_bond_removal_event(bt_address_t *bd_addr);
133 static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_fail_event);
134 static void __bt_handle_ongoing_bond(bt_remote_dev_info_t *remote_dev_info, gboolean incoming_bond);
135 static void __bt_device_conn_state_changed_callback(event_dev_conn_status_t *acl_event,
136 gboolean connected, unsigned char type);
137 static void __bt_free_pairing_info(bt_pairing_data_t **p_info);
139 static void __bt_device_ssp_consent_callback(remote_device_t* dev_info);
140 static void __bt_device_pin_request_callback(remote_device_t* pin_req_event);
141 static void __bt_device_ssp_passkey_display_callback(event_dev_passkey_t *dev_info);
142 static void __bt_device_ssp_passkey_confirmation_callback(event_dev_passkey_t *dev_info);
143 static void __bt_device_ssp_passkey_entry_callback(remote_device_t* dev_info);
144 static void __bt_device_authorization_request_callback(event_dev_authorize_req_t* auth_event);
146 static void __bt_device_services_callback(event_dev_services_t* uuid_list);
147 static void __bt_handle_ongoing_device_service_search(bt_remote_dev_info_t *remote_dev_info);
149 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info);
151 static int __bt_get_device_pin_code(const char *address, char *pin_code);
153 gboolean _bt_is_device_creating(void)
155 if (!trigger_bond_info)
157 return trigger_bond_info->is_device_creating;
160 void _bt_device_state_handle_callback_set_request(void)
162 _bt_service_register_event_handler_callback(
163 BT_DEVICE_MODULE, __bt_device_event_handler);
166 void __bt_device_handle_pending_requests(int result, int service_function,
167 void *user_data, unsigned int size)
171 invocation_info_t *req_info = NULL;
175 /* Get method invocation context */
176 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
178 if (req_info == NULL || req_info->service_function != service_function)
181 switch (service_function) {
182 case BT_SEARCH_SERVICE: {
183 char *address = (char *)user_data;
184 if (strncmp((char*)req_info->user_data, address, BT_ADDRESS_STRING_SIZE)) {
185 BT_ERR("Unexpected: Info request pending for a different address!!");
188 BT_INFO("Found info request addr [%s]", (char*)req_info->user_data);
189 bt_sdp_info_t sdp_info;
191 memset(&sdp_info, 0x00, sizeof(bt_sdp_info_t));
192 _bt_convert_addr_string_to_type(sdp_info.device_addr.addr, address);
194 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
195 g_array_append_vals(out_param, &sdp_info, sizeof(bt_sdp_info_t));
196 _bt_service_method_return(req_info->context, out_param, result);
198 g_free(req_info->user_data);
199 _bt_free_info_from_invocation_list(req_info);
200 g_array_free(out_param, TRUE);
204 case BT_BOND_DEVICE: {
205 char *address = (char *)user_data;
206 if (strncmp((char*)req_info->user_data, address, BT_ADDRESS_STRING_SIZE)) {
207 BT_ERR("Unexpected: Info request pending for a different address!!");
210 BT_INFO("Found info request addr [%s]", (char*)req_info->user_data);
211 bluetooth_device_info_t dev_info;
212 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
213 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
215 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
216 g_array_append_vals(out_param, &dev_info,
217 sizeof(bluetooth_device_info_t));
218 _bt_service_method_return(req_info->context, out_param, result);
220 g_free(req_info->user_data);
221 _bt_free_info_from_invocation_list(req_info);
222 g_array_free(out_param, TRUE);
226 case BT_UNBOND_DEVICE: {
227 char *address = (char *)user_data;
228 if (strncmp((char*)req_info->user_data, address, BT_ADDRESS_STRING_SIZE)) {
229 BT_ERR("Unexpected: Info request pending for a different address!!");
232 BT_INFO("Found info request addr [%s]", (char*)req_info->user_data);
233 bluetooth_device_address_t dev_addr;
234 _bt_convert_addr_string_to_type(dev_addr.addr, address);
235 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
236 g_array_append_vals(out_param, &dev_addr,
237 sizeof(bluetooth_device_address_t));
238 _bt_service_method_return(req_info->context, out_param, result);
240 g_free(req_info->user_data);
241 _bt_free_info_from_invocation_list(req_info);
242 g_array_free(out_param, TRUE);
247 BT_ERR("Unhandled case");
255 * Remote device properties are received on all following conditions
256 * a. When Bonding in on-going
257 * b. When device properties are updated\changed for a connected device
258 * (due to SDP or any other reason)
260 static void __bt_device_remote_properties_callback(event_dev_properties_t *oal_dev_props)
262 bt_remote_dev_info_t *rem_info = NULL;
265 rem_info = g_malloc0(sizeof(bt_remote_dev_info_t));
266 _bt_copy_remote_dev(rem_info, &(oal_dev_props->device_info));
268 if (oal_dev_props->adv_len > 0) {
271 rem_info->manufacturer_data_len = oal_dev_props->adv_len;
272 rem_info->manufacturer_data =
273 g_memdup(oal_dev_props->adv_data,
274 oal_dev_props->adv_len);
275 BT_DBG("----Advertising Data Length: %d",
276 rem_info->manufacturer_data_len);
278 for (k = 0; k < rem_info->manufacturer_data_len; k++) {
279 BT_INFO("Check data[%d] = [[0x%x]",
280 k, oal_dev_props->adv_data[k]);
283 rem_info->manufacturer_data = NULL;
284 rem_info->manufacturer_data_len = 0;
287 /* a. Check if bonding is on-going, if yes, we MUST update the bonding device properties */
288 if (trigger_bond_info && !strcmp(trigger_bond_info->addr, rem_info->address)) {
289 BT_INFO("Bonding is ongoing, try update properties");
290 if (!trigger_bond_info->dev_info ||
291 (!trigger_bond_info->dev_info->name &&
292 !trigger_bond_info->dev_info->alias) ||
293 !trigger_bond_info->dev_info->address ||
294 trigger_bond_info->dev_info->uuid_count == 0) {
295 BT_INFO("Complete data is not present, Assigning rem_info");
296 if (!trigger_bond_info->dev_info)
297 trigger_bond_info->dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
298 _bt_copy_remote_dev_info(trigger_bond_info->dev_info, rem_info);
301 BT_DBG("Bonding dev addr has matched with remote dev properties address [%s]", rem_info->address);
302 __bt_handle_ongoing_bond(trigger_bond_info->dev_info, FALSE);
303 } else if (incoming_bond_info && !g_strcmp0(incoming_bond_info->addr, rem_info->address)) {
304 BT_INFO("Incoming Bond is ongoing, try update properties");
305 if (!incoming_bond_info->dev_info ||
306 (!incoming_bond_info->dev_info->name &&
307 !incoming_bond_info->dev_info->alias) ||
308 !incoming_bond_info->dev_info->address ||
309 incoming_bond_info->dev_info->uuid_count == 0) {
310 BT_INFO("Complete data is not present, Assigning rem_info");
311 if (!incoming_bond_info->dev_info)
312 incoming_bond_info->dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
313 _bt_copy_remote_dev_info(incoming_bond_info->dev_info, rem_info);
316 BT_DBG("Incoming Bond addr matches with remote dev properties address [%s]", rem_info->address);
317 __bt_handle_ongoing_bond(incoming_bond_info->dev_info, TRUE);
320 /* Add device to bonded list */
321 _bt_service_add_device_to_bonded_list(rem_info);
323 /* Handle SDP Device properties update */
324 if (service_search_info && service_search_info->dev_info) {
325 if (!strcmp(service_search_info->addr, rem_info->address)) {
326 BT_DBG("Properties received and SDP request pending, fill device properties and send event");
327 service_search_info->dev_info->class = rem_info->class;
328 service_search_info->dev_info->paired = rem_info->paired;
329 service_search_info->dev_info->connected = rem_info->connected;
330 service_search_info->dev_info->rssi = rem_info->rssi;
331 service_search_info->dev_info->addr_type = rem_info->addr_type;
332 service_search_info->dev_info->trust = rem_info->trust;
335 service_search_info->dev_info->manufacturer_data = NULL;
336 service_search_info->dev_info->manufacturer_data_len = 0;
338 __bt_handle_ongoing_device_service_search(service_search_info->dev_info);
342 _bt_free_remote_dev(rem_info);
346 static int __get_oal_trusted_profile(bluetooth_trusted_profile_t profile)
349 case TRUSTED_PROFILE_PBAP:
350 return OAL_TRUSTED_PROFILE_PBAP;
351 case TRUSTED_PROFILE_MAP:
352 return OAL_TRUSTED_PROFILE_MAP;
353 case TRUSTED_PROFILE_SAP:
354 return OAL_TRUSTED_PROFILE_SAP;
355 case TRUSTED_PROFILE_HFP_HF:
356 return OAL_TRUSTED_PROFILE_HFP_HF;
357 case TRUSTED_PROFILE_A2DP:
358 return OAL_TRUSTED_PROFILE_A2DP;
359 case TRUSTED_PROFILE_ALL:
360 return OAL_TRUSTED_PROFILE_ALL;
366 int _bt_set_trust_profile(bluetooth_device_address_t *addr,
367 bluetooth_trusted_profile_t profile, gboolean trust)
370 bt_address_t bd_addr;
371 oal_trusted_profile_e oal_profile;
375 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
377 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
378 oal_profile = __get_oal_trusted_profile(profile);
379 retv_if(0 == oal_profile, BLUETOOTH_ERROR_NOT_SUPPORT);
381 result = device_set_trust_profile(&bd_addr, oal_profile, trust);
382 if (result != OAL_STATUS_SUCCESS) {
383 BT_ERR("device_set_trust_profile error: [%d]", result);
384 return BLUETOOTH_ERROR_INTERNAL;
388 return BLUETOOTH_ERROR_NONE;
391 int _bt_get_trust_profile(bluetooth_device_address_t *addr,
392 bluetooth_trusted_profile_t profile, guint *trust)
395 bt_address_t bd_addr;
396 oal_trusted_profile_e oal_profile;
400 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
402 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
403 oal_profile = __get_oal_trusted_profile(profile);
404 retv_if(0 == oal_profile, BLUETOOTH_ERROR_NOT_SUPPORT);
406 result = device_get_trust_profile(&bd_addr, oal_profile, trust);
407 if (result != OAL_STATUS_SUCCESS) {
408 BT_ERR("device_set_trust_profile error: [%d]", result);
409 return BLUETOOTH_ERROR_INTERNAL;
413 return BLUETOOTH_ERROR_NONE;
416 static void __bt_handle_ongoing_device_service_search(bt_remote_dev_info_t *remote_dev_info)
418 GVariant *param = NULL;
419 GVariant *uuids = NULL;
420 GVariantBuilder *builder = NULL;
421 GVariant *manufacturer_data;
425 BT_INFO("Send Service Search request event");
427 if (remote_dev_info->alias)
428 name = remote_dev_info->alias;
430 name = remote_dev_info->name;
432 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
433 for (i = 0; i < remote_dev_info->uuid_count; i++) {
434 g_variant_builder_add(builder, "s",
435 remote_dev_info->uuids[i]);
437 uuids = g_variant_new("as", builder);
438 g_variant_builder_unref(builder);
439 manufacturer_data = g_variant_new_from_data((const GVariantType *)"ay",
440 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
443 param = g_variant_new("(isunsbub@asn@ay)",
444 BLUETOOTH_ERROR_NONE,
445 remote_dev_info->address,
446 remote_dev_info->class,
447 remote_dev_info->rssi,
449 remote_dev_info->paired,
450 remote_dev_info->connected,
451 remote_dev_info->trust,
453 remote_dev_info->manufacturer_data_len,
455 /* Send the event to application */
456 _bt_send_event(BT_ADAPTER_EVENT,
457 BLUETOOTH_EVENT_SERVICE_SEARCHED,
460 __bt_free_service_search_info(&service_search_info);
464 static void __bt_device_services_callback(event_dev_services_t* uuid_list)
466 bt_remote_dev_info_t *rem_info = NULL;
470 if (trigger_bond_info && _bt_compare_adddress(trigger_bond_info->dev_addr,
471 (bluetooth_device_address_t *)&uuid_list->address) == TRUE) {
472 bluetooth_device_address_t *dev_addr = trigger_bond_info->dev_addr;
474 BT_DBG("Bonding dev addr has matched");
475 /* Bonding ongoing, Query device properties again */
476 if (BLUETOOTH_ERROR_NONE ==
477 _bt_device_get_bonded_device_info(dev_addr))
478 BT_DBG("_bt_device_get_bonded_device_info success");
480 BT_ERR("_bt_device_get_bonded_device_info failed");
483 if (service_search_info == NULL) {
485 BT_DBG("searching_info == NULL");
489 if (_bt_compare_adddress(service_search_info->dev_addr,
490 (bluetooth_device_address_t *)&uuid_list->address) == FALSE) {
491 BT_DBG("This device is not queried");
495 rem_info = g_malloc0(sizeof(bt_remote_dev_info_t));
496 memset(rem_info, 0x00, sizeof(bt_remote_dev_info_t));
498 rem_info->address = g_new0(char, BT_ADDRESS_STRING_SIZE);
499 _bt_convert_addr_type_to_string(rem_info->address, uuid_list->address.addr);
501 rem_info->uuid_count = uuid_list->num;
503 BT_INFO("Address [%s]", rem_info->address);
504 BT_INFO("Number of UUID's [%d]", rem_info->uuid_count);
505 if (rem_info->uuid_count > 0)
506 rem_info->uuids = g_new0(char *, rem_info->uuid_count);
508 /* Fill Remote Device Service List list */
509 for (i = 0; i < rem_info->uuid_count; i++) {
510 rem_info->uuids[i] = g_malloc0(BLUETOOTH_UUID_STRING_MAX);
511 _bt_uuid_to_string((service_uuid_t *)&uuid_list->service_list[i].uuid, rem_info->uuids[i]);
512 BT_DBG("UUID value=%s", rem_info->uuids[i]);
515 /* Update local cache */
516 _bt_update_remote_dev_property(rem_info->address, DEV_PROP_SERVICES, (void *)rem_info);
518 BT_DBG("DBUS return");
519 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_SEARCH_SERVICE,
520 service_search_info->addr, BT_ADDRESS_STRING_SIZE);
522 /* Save UUID List of remote devices */
523 if (service_search_info->dev_info)
524 _bt_free_remote_dev(service_search_info->dev_info);
525 service_search_info->dev_info = rem_info;
527 /* Query Other device properties */
528 if (_bt_device_get_bonded_device_info(service_search_info->dev_addr) == BLUETOOTH_ERROR_NONE) {
529 BT_DBG("Bonded device info query posted to stack successfully");
531 BT_DBG("Possibly internal stack error in bonded device info query, perform cleanup");
532 __bt_free_service_search_info(&service_search_info);
537 static void __bt_device_authorization_request_callback(event_dev_authorize_req_t* auth_event)
539 oal_service_t service_d = auth_event->service_id;
540 gchar address[BT_ADDRESS_STR_LEN];
543 _bt_convert_addr_type_to_string(address, auth_event->address.addr);
545 BT_INFO("service_d: %d", service_d);
549 BT_INFO("Incoming HID Profile conn Req from device addr [%s]", address);
551 case A2DP_SERVICE_ID:
552 BT_INFO("Incoming A2DP(Remote Sink) profile conn Req from device addr [%s]", address);
553 _bt_a2dp_src_handle_incoming_authorization(address, service_d);
555 case A2DP_SRC_SERVICE_ID:
556 BT_INFO("Incoming A2DP(Remote Source) Profile conn Req from device addr [%s]", address);
557 _bt_a2dp_sink_handle_incoming_authorization(address, service_d);
559 case AVRCP_SERVICE_ID:
560 BT_INFO("Incoming AVRCP (Remote) Profile conn Req from device addr [%s]", address);
562 case AVRCP_CT_SERVICE_ID:
563 BT_INFO("Incoming AVRCP (Controller) Profile conn Req from device addr [%s]", address);
565 #ifdef TIZEN_FEATURE_BT_OBEX
566 case OPP_SERVICE_ID: {
567 GVariant *param = NULL;
568 char *name = g_strdup(address); /* TODO: Retrieve and send correct name in OBEX conn auth req event */
570 BT_INFO("Incoming OPP conn Req from device addr [%s]", address);
571 _bt_obex_server_set_pending_conn_auth_device_addr(address);
572 param = g_variant_new("(iss)", BLUETOOTH_ERROR_NONE, address, name);
573 _bt_send_event(BT_OPP_SERVER_EVENT,
574 BLUETOOTH_EVENT_OBEX_SERVER_CONNECTION_AUTHORIZE, param);
580 BT_INFO("Incoming HSP_SERVICE_ID conn Req from device addr [%s]", address);
583 BT_INFO("Incoming HFP_SERVICE_ID conn Req from device addr [%s]", address);
586 BT_INFO("Incoming SAP_SERVICE_ID conn Req from device addr [%s]", address);
588 case HSP_HS_SERVICE_ID:
589 BT_INFO("Incoming HSP_HS_SERVICE_ID conn Req from device addr [%s]", address);
591 case HFP_HS_SERVICE_ID:
592 BT_INFO("Incoming HFP_HS_SERVICE_ID conn Req from device addr [%s]", address);
595 case IOTIVITY_SERVICE_ID:
596 BT_INFO("Incoming IOTIVITY_SERVICE_ID conn Req from device addr [%s]", address);
600 /* For now, reject authorization for any service apart from above switch cases */
601 BT_INFO("Incoming Profile conn req with service ID [%d] from device addr [%s]", service_d, address);
602 res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, FALSE, FALSE);
603 if (res != OAL_STATUS_SUCCESS)
604 BT_ERR("authorize_response: %d", res);
608 /* Auto accept authorization request for HID, A2DP and AVRCP profiles */
609 BT_INFO("Auto Accept authorization");
610 res = device_reply_auth_request((bt_address_t*)&auth_event->address, service_d, TRUE, FALSE);
611 if (res != OAL_STATUS_SUCCESS)
612 BT_ERR("authorize_response: %d", res);
616 static void __bt_handle_ongoing_bond(bt_remote_dev_info_t *remote_dev_info, gboolean incoming_bond)
618 GVariant *param = NULL;
621 if ((remote_dev_info->name || remote_dev_info->alias)
622 && remote_dev_info->address
623 && remote_dev_info->uuids) {
624 BT_INFO("All properties updated, time to send bonding finished event");
625 GVariant *uuids = NULL;
626 GVariantBuilder *builder = NULL;
627 GVariant *manufacturer_data;
631 if (remote_dev_info->alias)
632 name = remote_dev_info->alias;
634 name = remote_dev_info->name;
636 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
637 for (i = 0; i < remote_dev_info->uuid_count; i++) {
638 g_variant_builder_add(builder, "s",
639 remote_dev_info->uuids[i]);
641 uuids = g_variant_new("as", builder);
642 g_variant_builder_unref(builder);
643 manufacturer_data = g_variant_new_from_data((const GVariantType *)"ay",
644 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
647 param = g_variant_new("(isunsbub@asn@ay)",
648 BLUETOOTH_ERROR_NONE,
649 remote_dev_info->address,
650 remote_dev_info->class,
651 remote_dev_info->rssi,
653 remote_dev_info->paired,
654 remote_dev_info->connected,
655 remote_dev_info->trust,
657 remote_dev_info->manufacturer_data_len,
659 /* Send the event to application */
660 _bt_send_event(BT_ADAPTER_EVENT,
661 BLUETOOTH_EVENT_BONDING_FINISHED,
664 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
666 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
667 __bt_free_pairing_info(&trigger_pairing_info);
670 BT_INFO("Lets wait for more remote device properties");
674 static void __handle_incoming_bond_created_event(bt_address_t *bd_addr)
676 char address[BT_ADDRESS_STRING_SIZE];
677 bluetooth_device_address_t dev_addr;
682 * BlueZ sends paired signal for each paired device, during activation,
683 * We should ignore this, otherwise application thinks that a new device
686 if (_bt_adapter_get_status() != BT_ACTIVATED) {
687 BT_DBG("BT is not activated, so ignore this");
691 _bt_convert_addr_type_to_string(address, bd_addr->addr);
692 if (!incoming_bond_info) {
693 incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
695 if (g_strcmp0(incoming_bond_info->addr, address)) {
696 BT_DBG("Previous Bond address: [%s] differs from new address: [%s]",
697 address, incoming_bond_info->addr);
698 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
699 incoming_bond_info = g_malloc0(sizeof(bt_incoming_bond_data_t));
703 BT_INFO("Incoming bond successfully completed");
704 g_strlcpy(incoming_bond_info->addr, address, BT_ADDRESS_STRING_SIZE);
705 incoming_bond_info->dev_info = NULL;
707 _bt_convert_addr_string_to_type(dev_addr.addr, incoming_bond_info->addr);
708 if (BLUETOOTH_ERROR_NONE == _bt_device_get_bonded_device_info(&dev_addr)) {
709 BT_DBG("Bonded device info query posted to stack successfully");
711 BT_ERR("Bonded device info query failed");
712 __bt_free_bond_info(BT_DEVICE_INCOMING_BOND_INFO);
718 static void __bt_device_handle_bond_completion_event(bt_address_t *bd_addr)
720 gchar address[BT_ADDRESS_STR_LEN];
721 bluetooth_device_address_t dev_addr;
725 if (trigger_bond_info == NULL) {
727 BT_DBG("trigger_bond_info == NULL, Handle incomming bond event");
728 __handle_incoming_bond_created_event(bd_addr);
732 _bt_convert_addr_type_to_string(address, bd_addr->addr);
733 if (g_strcmp0(trigger_bond_info->addr, address)) {
734 BT_DBG("Bonding address= [%s] is different from requested address =[%s]",
735 address, trigger_bond_info->addr);
736 __handle_incoming_bond_created_event(bd_addr);
740 BT_INFO("Bonding successfully completed");
741 /* Bonding state will be cleaned up & BONDING FINISHED EVENT
742 will be sent only when Properties are fetched from stack
743 Till that time lets not free trigger_bond_info.
744 However it is possible that while fetching device properties, internal
745 stack error can occur which can lead to no valid properties or
746 no properties at all. So in such cases, we must not wait for properties,
747 otherwise, it can lead to infinite wait */
748 _bt_convert_addr_string_to_type(dev_addr.addr,
749 trigger_bond_info->addr);
751 if (_bt_device_get_bonded_device_info(&dev_addr) == BLUETOOTH_ERROR_NONE) {
752 BT_DBG("BOnded device info query posted to stack successfully");
753 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_BOND_DEVICE,
754 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
756 BT_DBG("Possibly internal stack error in bonded device info query, perform cleanup");
757 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
758 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
759 /* Destroy if at all device got bonded at stack level */
760 device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
762 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
763 __bt_free_pairing_info(&trigger_pairing_info);
769 /**********************************************************************************************
770 * Bond removal event can be triggered for following reasons -
771 * a. If Bonding procedure if failed (for Auth failed, Page timeout, cancelled by user etc)
772 * b. If Application requests for explicitly removing the bond
773 * c. When application attempt to create bond,bond is removed first which triggers this event
774 * c. is in-line with Bluedroid bond create\emoval architecture
775 *********************************************************************************************/
776 static void __bt_device_handle_bond_removal_event(bt_address_t *bd_addr)
778 char address[BT_ADDRESS_STRING_SIZE];
782 _bt_convert_addr_type_to_string(address, bd_addr->addr);
783 _bt_service_remove_device_from_bonded_list(address);
785 if (trigger_unbond_info) {
786 BT_INFO("Bond removal request successfully handled, return DBUS and send event");
787 GVariant *param = NULL;
788 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_NONE, BT_UNBOND_DEVICE,
789 trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
790 param = g_variant_new("(is)", BLUETOOTH_ERROR_NONE, trigger_unbond_info->addr);
791 _bt_send_event(BT_ADAPTER_EVENT,
792 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
794 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
795 __bt_free_pairing_info(&trigger_pairing_info);
796 } else if (trigger_bond_info) {
797 BT_ERR("Bonding was removed");
798 __bt_device_handle_bond_state();
803 static void __bt_device_handle_bond_failed_event(event_dev_bond_failed_t* bond_fail_event)
806 oal_status_t status = bond_fail_event->status;
807 BT_INFO("Bonding failed, reason: %d", status);
810 case OAL_STATUS_RMT_DEVICE_DOWN:
812 if (trigger_bond_info) {
813 BT_INFO("OAL_STATUS_RMT_DEVICE_DOWN:Lets retry bonding!! retry count [%d]",
815 int ret = OAL_STATUS_SUCCESS;
816 if (bond_retry_count < MAX_BOND_RETRY_COUNT) {
817 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
818 if (ret != OAL_STATUS_SUCCESS) {
819 BT_ERR("Create Bond procedure could not suceed");
820 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
821 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
822 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
823 __bt_free_pairing_info(&trigger_pairing_info);
824 bond_retry_count = 0;
828 BT_ERR("Create Bond failed MAX_BOND_RETRY_COUNT TIMES!!");
829 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
830 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
831 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
832 __bt_free_pairing_info(&trigger_pairing_info);
833 bond_retry_count = 0;
838 case OAL_STATUS_AUTH_FAILED:
840 /*TODO Auto pairing status set & ignore auto pairing logics can be done at this point.
841 To be considered later*/
842 int result = BLUETOOTH_ERROR_INTERNAL;
843 BT_INFO("BT_OPERATION_STATUS_AUTH_FAILED");
844 if (trigger_bond_info) {
845 BT_ERR("Create Bond procedure could not suceed, check if cancelled by User");
846 if (trigger_bond_info->is_cancelled_by_user) {
847 BT_ERR("Bonding is cancelled by user");
848 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
850 __bt_device_handle_pending_requests(result, BT_BOND_DEVICE,
851 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
852 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
855 __bt_free_pairing_info(&trigger_pairing_info);
858 case OAL_STATUS_INTERNAL_ERROR:
860 BT_INFO("OAL_STATUS_INTERNAL_ERROR");
861 if (trigger_unbond_info) {
862 BT_INFO("Bond removal request failed, return DBUS and send event");
863 GVariant *param = NULL;
864 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_UNBOND_DEVICE,
865 trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE);
866 param = g_variant_new("(is)", BLUETOOTH_ERROR_INTERNAL, trigger_unbond_info->addr);
867 _bt_send_event(BT_ADAPTER_EVENT,
868 BLUETOOTH_EVENT_BONDED_DEVICE_REMOVED,
870 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
871 } else if (trigger_bond_info) {
872 if (__bt_device_handle_bond_state() != BLUETOOTH_ERROR_NONE) {
873 if (trigger_bond_info) {
874 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
875 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
876 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
881 __bt_free_pairing_info(&trigger_pairing_info);
886 BT_ERR("Unknown status of Bond failed event status [%d]", status);
894 static void __bt_device_event_handler(int event_type, gpointer event_data)
896 int eventcheck = OAL_EVENT_DEVICE_PROPERTIES;
897 BT_INFO("event [%d] Event check = [%d]", event_type, eventcheck);
899 switch (event_type) {
900 case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY: {
901 BT_INFO("BREDR Device Found");
902 __bt_device_remote_device_found_callback(event_data, FALSE);
905 case OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE: {
906 BT_INFO("Dual Device Found");
907 __bt_device_remote_device_found_callback(event_data, FALSE);
910 case OAL_EVENT_DEVICE_PROPERTIES: {
911 BT_INFO("Remote Device properties Received");
912 __bt_device_remote_properties_callback((event_dev_properties_t *)event_data);
915 case OAL_EVENT_DEVICE_BONDING_SUCCESS: {
916 BT_INFO("Bonding Success event Received");
917 __bt_device_handle_bond_completion_event((bt_address_t *)event_data);
920 case OAL_EVENT_DEVICE_BONDING_REMOVED: {
921 BT_INFO("Bonding Removed event Received");
922 __bt_device_handle_bond_removal_event((bt_address_t *)event_data);
925 case OAL_EVENT_DEVICE_BONDING_FAILED: {
926 BT_INFO("Bonding Failed event Received");
927 __bt_device_handle_bond_failed_event((event_dev_bond_failed_t*) event_data);
930 case OAL_EVENT_DEVICE_ACL_CONNECTED: {
931 BT_INFO("ACL Connected event Received");
932 event_dev_conn_status_t* param = event_data;
933 __bt_device_conn_state_changed_callback(param, TRUE, 0);
936 case OAL_EVENT_DEVICE_ACL_DISCONNECTED: {
937 BT_INFO("ACL Disconnected event Received");
938 __bt_device_conn_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 0);
941 case OAL_EVENT_DEVICE_LE_CONNECTED: {
942 BT_INFO("LE Connected event Received");
943 event_dev_conn_status_t* param = event_data;
944 __bt_device_conn_state_changed_callback(param, TRUE, 1);
947 case OAL_EVENT_DEVICE_LE_DISCONNECTED: {
948 BT_INFO("LE Disconnected event Received");
949 __bt_device_conn_state_changed_callback((event_dev_conn_status_t *)event_data, FALSE, 1);
952 case OAL_EVENT_DEVICE_PIN_REQUEST: {
953 BT_INFO("PIN Request Received");
954 __bt_device_pin_request_callback((remote_device_t*)event_data);
957 case OAL_EVENT_DEVICE_PASSKEY_ENTRY_REQUEST: {
958 BT_INFO("Passkey Entry request Received");
959 __bt_device_ssp_passkey_entry_callback((remote_device_t*)event_data);
962 case OAL_EVENT_DEVICE_PASSKEY_CONFIRMATION_REQUEST:{
963 BT_INFO("Passkey Confirmation Request Received");
964 __bt_device_ssp_passkey_confirmation_callback((event_dev_passkey_t *)event_data);
967 case OAL_EVENT_DEVICE_PASSKEY_DISPLAY: {
968 BT_INFO("Passkey Display Request Received");
969 __bt_device_ssp_passkey_display_callback((event_dev_passkey_t *)event_data);
972 case OAL_EVENT_DEVICE_SSP_CONSENT_REQUEST: {
973 BT_INFO("SSP Consent Request Received");
974 __bt_device_ssp_consent_callback((remote_device_t*)event_data);
977 case OAL_EVENT_DEVICE_SERVICES: {
978 BT_INFO("Remote Device Services Received");
979 __bt_device_services_callback((event_dev_services_t*)event_data);
982 case OAL_EVENT_DEVICE_AUTHORIZE_REQUEST: {
983 BT_INFO("Remote Device Authorization Request");
984 __bt_device_authorization_request_callback((event_dev_authorize_req_t*)event_data);
987 case OAL_EVENT_DEVICE_TRUSTED: {
988 BT_INFO("Remote Device Trusted");
989 __bt_device_trusted_callback(TRUE, (event_dev_trust_t*)event_data);
992 case OAL_EVENT_DEVICE_UNTRUSTED: {
993 BT_INFO("Remote Device UnTrusted");
994 __bt_device_trusted_callback(FALSE, (event_dev_trust_t*)event_data);
997 case OAL_EVENT_DEVICE_NAME: {
998 remote_device_t *rem_dev = event_data;
999 gchar address[BT_ADDRESS_STR_LEN];
1001 _bt_convert_addr_type_to_string(address, rem_dev->address.addr);
1002 BT_INFO("Remote Device name Received");
1003 BT_INFO("Name: %s, Address: %s", rem_dev->name, address);
1005 /* Update local cache */
1006 _bt_update_remote_dev_property(address, DEV_PROP_NAME, (void *)rem_dev->name);
1009 case OAL_EVENT_DEVICE_TRUSTED_PROFILES_CHANGED: {
1010 event_device_trusted_profiles_t *ev = event_data;
1011 char address[BT_ADDRESS_STRING_SIZE];
1015 _bt_convert_addr_type_to_string(address, ev->address.addr);
1016 _bt_send_event(BT_DEVICE_EVENT,
1017 BLUETOOTH_EVENT_SUPPORTED_PROFILE_TRUSTED,
1018 g_variant_new("(isi)", BLUETOOTH_ERROR_NONE,
1019 address, ev->trust_val));
1022 case OAL_EVENT_RSSI_MONITORING_ENABLED: {
1023 event_dev_rssi_info_t *ev = event_data;
1024 char address[BT_ADDRESS_STRING_SIZE];
1029 _bt_convert_addr_type_to_string(address, ev->address.addr);
1030 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1031 address, ev->link_type, TRUE);
1032 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1035 case OAL_EVENT_RSSI_MONITORING_DISABLED: {
1036 event_dev_rssi_info_t *ev = event_data;
1037 char address[BT_ADDRESS_STRING_SIZE];
1042 _bt_convert_addr_type_to_string(address, ev->address.addr);
1043 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1044 address, ev->link_type, FALSE);
1045 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1048 case OAL_EVENT_RSSI_ALERT_RECEIVED: {
1049 event_dev_rssi_info_t *ev = event_data;
1050 char address[BT_ADDRESS_STRING_SIZE];
1055 _bt_convert_addr_type_to_string(address, ev->address.addr);
1056 param = g_variant_new("(isiii)", BLUETOOTH_ERROR_NONE,
1057 address, ev->link_type, ev->alert_type, ev->rssi);
1058 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ALERT, param);
1061 case OAL_EVENT_RAW_RSSI_RECEIVED: {
1062 event_dev_rssi_info_t *ev = event_data;
1063 char address[BT_ADDRESS_STRING_SIZE];
1068 _bt_convert_addr_type_to_string(address, ev->address.addr);
1069 param = g_variant_new("(isii)", BLUETOOTH_ERROR_NONE,
1070 address, ev->link_type, ev->rssi);
1071 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RAW_RSSI, param);
1075 BT_INFO("Unhandled event..");
1079 /* Legacy Pairing event handler */
1080 static void __bt_device_pin_request_callback(remote_device_t* pin_req_event)
1083 char address[BT_ADDRESS_STRING_SIZE];
1084 char pin_code[BLUETOOTH_PIN_CODE_MAX_LENGTH + 1];
1087 _bt_convert_addr_type_to_string(address, pin_req_event->address.addr);
1089 BT_INFO("Address[%s]", address);
1090 BT_INFO("Name[%s]", pin_req_event->name);
1091 BT_INFO("COD[%d]", pin_req_event->cod);
1093 if (trigger_pairing_info) {
1094 /* BTAPI support only one pairing at a time */
1095 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1096 BT_ERR("New PIN request address [%s]", address);
1097 device_reject_pin_request(&pin_req_event->address);
1101 /* If user initiated bonding and auto response is possible, just reply with default 0000*/
1102 if (_bt_is_bonding_device_address(address) == TRUE &&
1103 _bt_agent_is_auto_response(pin_req_event->cod, address, pin_req_event->name)) {
1104 /* Note: Currently even if SYSPOPUP is supported, we use Fixed PIN "0000" for basic pairing
1105 as BT SYSPOPUP is currently not working for PIN KEY entry in Tizen platform. This needs
1106 to be checked and fixed apropriately */
1107 _bt_set_autopair_status_in_bonding_info(TRUE);
1108 device_accept_pin_request(&pin_req_event->address, "0000");
1109 } else if (_bt_agent_is_hid_keyboard(pin_req_event->cod)) {
1110 BT_DBG("Remote Device is HID keyboard Type..");
1111 char str_passkey[BT_PASSKEY_MAX_LENGTH + 1] = { 0 };
1113 if (_bt_agent_generate_passkey(str_passkey,
1114 BT_PASSKEY_MAX_LENGTH) != 0) {
1115 device_reject_pin_request(&pin_req_event->address);
1118 device_accept_pin_request(&pin_req_event->address, str_passkey);
1120 BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1121 param = g_variant_new("(isss)", BLUETOOTH_ERROR_NONE, address, pin_req_event->name, str_passkey);
1122 _bt_send_event(BT_ADAPTER_EVENT,
1123 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1124 BT_DBG("Key board pairing in process");
1125 } else if (BLUETOOTH_ERROR_NONE == __bt_get_device_pin_code(address, pin_code)) {
1126 BT_DBG("Use stored PIN code [%s]", pin_code);
1127 device_accept_pin_request(&pin_req_event->address, pin_code);
1129 if (_bt_is_bonding_device_address(address) == TRUE) {
1130 BT_DBG("Show Pin entry");
1131 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1132 trigger_pairing_info->addr = g_strdup(address);
1133 trigger_pairing_info->is_ssp = FALSE;
1135 BT_DBG("Send BLUETOOTH_EVENT_PIN_REQUEST");
1136 param = g_variant_new("(iss)", BLUETOOTH_ERROR_NONE, address, pin_req_event->name);
1137 _bt_send_event(BT_ADAPTER_EVENT,
1138 BLUETOOTH_EVENT_PIN_REQUEST, param);
1143 _bt_agent_release_memory();
1147 /* SSP Pairing event handler */
1148 static void __bt_device_ssp_passkey_entry_callback(remote_device_t* dev_info)
1151 gchar address[BT_ADDRESS_STR_LEN];
1154 int result = BLUETOOTH_ERROR_NONE;
1157 _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1159 name = dev_info->name;
1161 BT_INFO("Address[%s]", address);
1162 BT_INFO("Name[%s]", name);
1163 BT_INFO("COD[%d]", dev_info->cod);
1165 if (trigger_pairing_info) {
1166 /* BTAPI support only one pairing at a time */
1167 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1168 BT_ERR("New PIN request address [%s]", address);
1169 device_reject_pin_request(&dev_info->address);
1174 /* Set pairing data */
1175 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1176 trigger_pairing_info->addr = g_strdup(address);
1177 trigger_pairing_info->is_ssp = TRUE;
1179 param = g_variant_new("(iss)", result, p_addr, name);
1180 _bt_send_event(BT_ADAPTER_EVENT,
1181 BLUETOOTH_EVENT_PASSKEY_REQUEST, param);
1185 static void __bt_device_ssp_passkey_confirmation_callback(event_dev_passkey_t *dev_info)
1188 gchar address[BT_ADDRESS_STR_LEN];
1191 char str_passkey[7];
1192 int result = BLUETOOTH_ERROR_NONE;
1195 _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1197 name = dev_info->device_info.name;
1199 BT_INFO("Address[%s]", address);
1200 BT_INFO("Name[%s]", name);
1201 BT_INFO("COD[%d]", dev_info->device_info.cod);
1203 if (trigger_pairing_info) {
1204 /* BTAPI support only one pairing at a time */
1205 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1206 BT_ERR("New PIN request address [%s]", address);
1207 device_reject_pin_request(&dev_info->device_info.address);
1212 /* Set pairing data */
1213 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1214 trigger_pairing_info->addr = g_strdup(address);
1215 trigger_pairing_info->is_ssp = TRUE;
1217 BT_DBG("Send BLUETOOTH_EVENT_PASSKEY_CONFIRMATION");
1218 snprintf(str_passkey, sizeof(str_passkey), "%.6d", dev_info->pass_key);
1220 param = g_variant_new("(isss)", result, p_addr, name, str_passkey);
1221 _bt_send_event(BT_ADAPTER_EVENT,
1222 BLUETOOTH_EVENT_PASSKEY_CONFIRM_REQUEST, param);
1226 static void __bt_device_ssp_passkey_display_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_KEYBOARD_PASSKEY_DISPLAY");
1259 snprintf(str_passkey, sizeof(str_passkey), "%06d", dev_info->pass_key);
1261 param = g_variant_new("(isss)", result, p_addr, name, str_passkey);
1262 if (passkey_watcher) {
1263 BT_INFO("Send passkey to %s", passkey_watcher);
1264 _bt_send_event_to_dest(passkey_watcher, BT_ADAPTER_EVENT,
1265 BLUETOOTH_EVENT_PASSKEY_NOTIFICATION, param);
1267 _bt_send_event(BT_ADAPTER_EVENT,
1268 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1273 static void __bt_device_ssp_consent_callback(remote_device_t* dev_info)
1275 gchar address[BT_ADDRESS_STR_LEN];
1282 _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1283 name = dev_info->name;
1284 cod = dev_info->cod;
1286 BT_INFO("Address[%s]", address);
1287 BT_INFO("Name[%s]", name);
1288 BT_INFO("COD[%d]", cod);
1290 if (trigger_pairing_info) {
1291 /* BTAPI support only one pairing at a time */
1292 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1293 BT_ERR("New PIN request address [%s]", address);
1294 device_reject_pin_request(&dev_info->address);
1299 /* Set pairing data */
1300 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1301 trigger_pairing_info->addr = g_strdup(address);
1302 trigger_pairing_info->is_ssp = TRUE;
1304 local_major = ((cod >> 8) & 0x001f);
1305 local_minor = (cod & 0x00fc);
1306 BT_DBG("SSP_CONSENT: Major type=[0x%x] and Minor type=[0x%x]", local_major, local_minor);
1308 /*TODO: BLUETOOTH_EVENT_SSP_CONSENT_REQUEST to be handled in Tizen */
1312 static void __bt_device_conn_state_changed_callback(event_dev_conn_status_t *acl_event,
1313 gboolean connected, unsigned char type)
1315 gchar address[BT_ADDRESS_STR_LEN];
1316 int result = BLUETOOTH_ERROR_NONE;
1317 GVariant *param = NULL;
1318 bt_device_conn_info_t conn_info;
1321 _bt_convert_addr_type_to_string(address, acl_event->address.addr);
1324 param = g_variant_new("(isy)", result, address, type);
1325 _bt_send_event(BT_DEVICE_EVENT,
1326 BLUETOOTH_EVENT_DEVICE_CONNECTED,
1329 param = g_variant_new("(isy)", result, address, type);
1330 _bt_send_event(BT_DEVICE_EVENT,
1331 BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1335 conn_info.connected = connected;
1336 conn_info.type = type;
1337 /* Update local cache */
1338 _bt_update_remote_dev_property(address, DEV_PROP_CONNECTED, (void *)&conn_info);
1343 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble)
1345 bt_remote_dev_info_t *dev_info = NULL;
1346 int result = BLUETOOTH_ERROR_NONE;
1347 GVariant *param = NULL;
1348 GVariant *uuids = NULL;
1349 GVariant *manufacturer_data = NULL;
1350 GVariantBuilder *builder = NULL;
1354 ret_if(_bt_is_discovering() == FALSE);
1355 ret_if(event_data == NULL);
1357 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
1360 event_ble_dev_found_t * oal_ble_dev = event_data;
1361 BT_INFO("Device type [%d]", oal_ble_dev->device_info.type);
1363 _bt_copy_remote_dev(dev_info, &oal_ble_dev->device_info);
1365 dev_info->manufacturer_data_len = oal_ble_dev->adv_len;
1366 if (dev_info->manufacturer_data_len)
1367 dev_info->manufacturer_data = g_memdup(oal_ble_dev->adv_data,
1368 dev_info->manufacturer_data_len);
1370 dev_info->manufacturer_data = NULL;
1371 BT_DBG("----Advertising Data Length: %d", dev_info->manufacturer_data_len);
1373 event_dev_found_t * oal_dev = event_data;
1374 _bt_copy_remote_dev(dev_info, &oal_dev->device_info);
1377 /* If Remote device name is NULL or still RNR is not done then display address as name. */
1378 if (dev_info->name == NULL)
1379 dev_info->name = g_strdup(dev_info->address);
1380 BT_DBG("Name %s", dev_info->name);
1382 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1383 for (i = 0; i < dev_info->uuid_count; i++)
1384 g_variant_builder_add(builder, "s", dev_info->uuids[i]);
1386 uuids = g_variant_new("as", builder);
1387 g_variant_builder_unref(builder);
1389 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1390 dev_info->manufacturer_data,
1391 dev_info->manufacturer_data_len,
1395 param = g_variant_new("(isunsbub@asn@ay)", result,
1401 dev_info->connected,
1404 dev_info->manufacturer_data_len,
1407 _bt_send_event(BT_ADAPTER_EVENT,
1408 BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
1411 _bt_free_remote_dev(dev_info);
1415 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info)
1417 gchar address[BT_ADDRESS_STR_LEN];
1418 int result = BLUETOOTH_ERROR_NONE;
1419 GVariant *param = NULL;
1423 _bt_convert_addr_type_to_string(address, info->address.addr);
1425 /* Update local cache */
1426 _bt_update_remote_dev_property(address, DEV_PROP_TRUST, (void *)&trusted);
1428 param = g_variant_new("(is)", result, address);
1429 event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1430 BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1431 /* Send event to application */
1432 _bt_send_event(BT_DEVICE_EVENT,
1439 static void __bt_free_pairing_info(bt_pairing_data_t **p_info)
1442 bt_pairing_data_t * info = *p_info;
1453 static void __bt_free_bond_info(uint8_t type)
1458 case BT_DEVICE_BOND_INFO:
1459 if (!trigger_bond_info)
1462 if (trigger_bond_info->addr)
1463 g_free(trigger_bond_info->addr);
1464 if (trigger_bond_info->dev_addr)
1465 g_free(trigger_bond_info->dev_addr);
1466 if (trigger_bond_info->dev_info)
1467 _bt_free_remote_dev(trigger_bond_info->dev_info);
1468 g_free(trigger_bond_info);
1469 trigger_bond_info = NULL;
1471 case BT_DEVICE_INCOMING_BOND_INFO:
1472 if (!incoming_bond_info)
1475 if (incoming_bond_info->dev_info)
1476 _bt_free_remote_dev(incoming_bond_info->dev_info);
1477 g_free(incoming_bond_info);
1478 incoming_bond_info = NULL;
1480 case BT_DEVICE_UNBOND_INFO:
1481 if (!trigger_unbond_info)
1484 if (trigger_unbond_info->addr)
1485 g_free(trigger_unbond_info->addr);
1486 if (trigger_unbond_info->dev_addr)
1487 g_free(trigger_unbond_info->dev_addr);
1488 if (trigger_unbond_info->dev_info)
1489 _bt_free_remote_dev(trigger_unbond_info->dev_info);
1490 g_free(trigger_unbond_info);
1491 trigger_unbond_info = NULL;
1497 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info)
1499 bt_service_search_info_data_t * info = *p_info;
1506 if (info->dev_addr) {
1507 g_free(info->dev_addr);
1508 info->dev_addr = NULL;
1511 if (info->dev_info) {
1512 _bt_free_remote_dev(info->dev_info);
1513 info->dev_info = NULL;
1521 static int __bt_device_handle_bond_state(void)
1523 BT_INFO("Current Bond state: %d", bt_device_bond_state);
1524 int ret = OAL_STATUS_INTERNAL_ERROR;
1526 switch (bt_device_bond_state) {
1527 case BT_DEVICE_BOND_STATE_CANCEL_DISCOVERY:
1528 /*TODO:Bonding during discovery: Unhandled!!*/
1529 BT_INFO("Bonding during discovery: Unhandled!!");
1531 case BT_DEVICE_BOND_STATE_DISCOVERY_CANCELLED:
1532 /*TODO:Bonding during discovery: Unhandled!!*/
1533 BT_INFO("Bonding during discovery: Unhandled!!");
1535 case BT_DEVICE_BOND_STATE_REMOVE_BONDING:
1536 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVED_BONDING;
1537 ret = device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
1538 if (ret != OAL_STATUS_SUCCESS)
1539 ret = __bt_device_handle_bond_state();
1541 case BT_DEVICE_BOND_STATE_REMOVED_BONDING:
1542 bt_device_bond_state = BT_DEVICE_BOND_STATE_NONE;
1543 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
1544 /* Bonding procedure was started but unfortunately could not complete.
1545 Basically removed bonding was success, but create bond request could not proceed
1546 So lets cleanup the context */
1547 if (ret != OAL_STATUS_SUCCESS) {
1548 BT_ERR("Create Bond procedure could not suceed");
1549 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1550 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1551 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1552 __bt_free_pairing_info(&trigger_pairing_info);
1555 case BT_DEVICE_BOND_STATE_NONE:
1556 BT_INFO("Create Bond failed!!");
1557 if (trigger_bond_info) {
1558 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1559 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1560 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1561 __bt_free_pairing_info(&trigger_pairing_info);
1568 if (ret != OAL_STATUS_SUCCESS)
1569 return BLUETOOTH_ERROR_INTERNAL;
1571 return BLUETOOTH_ERROR_NONE;
1574 int _bt_device_get_bonded_device_info(bluetooth_device_address_t *addr)
1577 bt_address_t bd_addr;
1581 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
1583 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
1584 result = device_query_attributes(&bd_addr);
1585 if (result != OAL_STATUS_SUCCESS) {
1586 BT_ERR("device_query_attributes error: [%d]", result);
1587 return BLUETOOTH_ERROR_INTERNAL;
1591 return BLUETOOTH_ERROR_NONE;
1594 int _bt_set_alias(bluetooth_device_address_t *device_address, const char *alias)
1597 char address[BT_ADDRESS_STRING_SIZE];
1600 BT_CHECK_PARAMETER(alias, return);
1602 ret = device_set_alias((bt_address_t *)device_address, (char *)alias);
1603 if (ret != OAL_STATUS_SUCCESS) {
1604 BT_ERR("device_set_alias: %d", ret);
1605 return BLUETOOTH_ERROR_INTERNAL;
1608 /* Update local cache */
1609 _bt_convert_addr_type_to_string(address, device_address->addr);
1610 _bt_update_remote_dev_property(address, DEV_PROP_ALIAS, (void *)alias);
1613 return BLUETOOTH_ERROR_NONE;
1616 int _bt_bond_device(bluetooth_device_address_t *device_address,
1617 unsigned short conn_type, GArray **out_param1)
1619 int result = BLUETOOTH_ERROR_NONE;
1620 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1621 bluetooth_device_info_t dev_info;
1624 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1626 /* If bonding or discovery already going on */
1627 if (trigger_bond_info || _bt_is_discovering()) {
1628 BT_ERR("Device is buzy, bonding can not proceed now..");
1629 result = BLUETOOTH_ERROR_DEVICE_BUSY;
1633 /*TODO: If unbonding with same device going on */
1634 _bt_convert_addr_type_to_string(address, device_address->addr);
1636 trigger_bond_info = g_malloc0(sizeof(bt_bond_data_t));
1637 trigger_bond_info->addr = g_strdup(address);
1638 trigger_bond_info->conn_type = conn_type;
1639 trigger_bond_info->is_device_creating = TRUE;
1640 trigger_bond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1641 trigger_bond_info->dev_info = NULL;
1643 /* Ready to initiate bonding */
1645 /* In Tizen, we will first remove bond and then attempt to create bond to keep
1646 consistency with bluedroid. Even if remove bond fails due to device not already
1647 bonded, then straight away create bond is triggered. This is because, remove bond
1648 is handled differently in bluedroid and bluez. In Bluez, if device is
1649 already removed, remove bond call fails.
1650 However in bluedroid, remove bond on already removed device returns success. So we will
1651 handle the cases transparently*/
1652 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVE_BONDING;
1653 bond_retry_count = 0;
1654 result = __bt_device_handle_bond_state();
1656 if (result != BLUETOOTH_ERROR_NONE)
1663 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1664 memcpy(dev_info.device_address.addr, device_address->addr,
1665 BLUETOOTH_ADDRESS_LENGTH);
1667 g_array_append_vals(*out_param1, &dev_info,
1668 sizeof(bluetooth_device_info_t));
1669 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1675 int _bt_unbond_device(bluetooth_device_address_t *device_address,
1676 GArray **out_param1)
1678 int result = OAL_STATUS_SUCCESS;
1679 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1680 bluetooth_device_info_t dev_info;
1683 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1685 _bt_convert_addr_type_to_string(address, device_address->addr);
1687 trigger_unbond_info = g_malloc0(sizeof(bt_bond_data_t));
1688 trigger_unbond_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1689 trigger_unbond_info->addr = g_strdup(address);
1690 trigger_unbond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1692 /* Check if Bonding is already going on, we should not abruptly remove bonding*/
1693 if (trigger_bond_info && strncmp(trigger_bond_info->addr, trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE) == 0) {
1694 BT_ERR("Bonding with same device already ongoing");
1695 result = BLUETOOTH_ERROR_PERMISSION_DEINED;
1699 result = device_destroy_bond((bt_address_t *)device_address);
1700 if (result != OAL_STATUS_SUCCESS)
1706 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1707 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1708 trigger_unbond_info->addr);
1710 g_array_append_vals(*out_param1, &dev_info,
1711 sizeof(bluetooth_device_info_t));
1712 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
1717 int _bt_cancel_bonding(void)
1719 int result = OAL_STATUS_SUCCESS;
1722 retv_if(trigger_bond_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1724 result = device_stop_bond((bt_address_t *)trigger_bond_info->dev_addr);
1726 if (result == OAL_STATUS_SUCCESS)
1727 trigger_bond_info->is_cancelled_by_user = TRUE;
1732 int _bt_passkey_reply(const char *passkey, gboolean cnfm_reply)
1734 bluetooth_device_address_t device_address;
1735 int ret = OAL_STATUS_SUCCESS;
1736 BT_INFO("reply: %d", cnfm_reply);
1738 retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1739 retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1741 _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1743 if (trigger_pairing_info->is_ssp) {
1745 ret = device_accept_passkey_entry((bt_address_t *)&device_address, atoi(passkey));
1747 ret = device_reject_passkey_entry((bt_address_t *)&device_address);
1748 trigger_pairing_info->is_ssp = FALSE;
1751 ret = device_accept_pin_request((bt_address_t *)&device_address, passkey);
1753 ret = device_reject_pin_request((bt_address_t *)&device_address);
1756 __bt_free_pairing_info(&trigger_pairing_info);
1758 if (ret != OAL_STATUS_SUCCESS) {
1759 BT_ERR("_bt_device_handle_passkey_reply: err [%d]", ret);
1760 return BLUETOOTH_ERROR_INTERNAL;
1764 return BLUETOOTH_ERROR_NONE;
1767 int _bt_passkey_confirmation_reply(gboolean cnfm_reply)
1769 BT_INFO("BT_PASSKEY_CONFIRMATION_REPLY");
1770 bluetooth_device_address_t device_address;
1771 int ret = OAL_STATUS_SUCCESS;
1772 BT_INFO("reply: %d", cnfm_reply);
1774 retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1775 retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1777 _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1779 ret = device_reply_passkey_confirmation((bt_address_t *)&device_address, cnfm_reply);
1781 __bt_free_pairing_info(&trigger_pairing_info);
1782 if (ret != OAL_STATUS_SUCCESS) {
1783 BT_ERR("_bt_device_handle_passkey_confirmation_reply: err [%d]", ret);
1784 return BLUETOOTH_ERROR_INTERNAL;
1788 return BLUETOOTH_ERROR_NONE;
1791 gboolean _bt_device_is_pairing(void)
1793 return (trigger_pairing_info) ? TRUE : FALSE;
1796 gboolean _bt_device_is_bonding(void)
1798 return (trigger_bond_info) ? TRUE : FALSE;
1801 gboolean _bt_is_bonding_device_address(const char *address)
1803 if (trigger_bond_info == NULL || trigger_bond_info->addr == NULL)
1806 if (g_strcmp0(trigger_bond_info->addr, address) == 0) {
1807 BT_DBG("[%s] is bonding device", address);
1811 BT_DBG("[%s] is NOT bonding device", address);
1815 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
1817 ret_if(trigger_bond_info == NULL);
1818 trigger_bond_info->is_autopair = is_autopair;
1821 int _bt_search_device(bluetooth_device_address_t *device_address)
1823 int result = OAL_STATUS_SUCCESS;
1826 BT_CHECK_PARAMETER(device_address, return);
1828 if (trigger_bond_info) {
1829 BT_ERR("Bonding in progress");
1830 return BLUETOOTH_ERROR_DEVICE_BUSY;
1833 if (service_search_info) {
1834 BT_ERR("Service searching in progress");
1835 return BLUETOOTH_ERROR_DEVICE_BUSY;
1838 /* allocate user data so that it can be retrieved in callback */
1839 service_search_info = g_malloc0(sizeof(bt_service_search_info_data_t));
1840 service_search_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1841 service_search_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1843 _bt_convert_addr_type_to_string(service_search_info->addr,
1844 device_address->addr);
1846 result = device_query_services((bt_address_t *)device_address);
1848 if (result != OAL_STATUS_SUCCESS) {
1849 BT_ERR("Device Service Search Failed..: %d", result);
1850 __bt_free_service_search_info(&service_search_info);
1851 return BLUETOOTH_ERROR_INTERNAL;
1853 return BLUETOOTH_ERROR_NONE;
1856 int _bt_cancel_search_device(void)
1858 int ret = OAL_STATUS_SUCCESS;
1859 retv_if(service_search_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1861 ret = device_stop_query_sevices((bt_address_t *)service_search_info->dev_addr);
1863 if (ret != OAL_STATUS_SUCCESS) {
1864 BT_ERR("SDP Cancel request failed [%d]", ret);
1865 return BLUETOOTH_ERROR_INTERNAL;
1868 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_CANCEL_BY_USER, BT_SEARCH_SERVICE,
1869 service_search_info->addr, BT_ADDRESS_STRING_SIZE);
1871 __bt_free_service_search_info(&service_search_info);
1873 return BLUETOOTH_ERROR_NONE;
1877 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1880 int ret = OAL_STATUS_SUCCESS;
1883 BT_CHECK_PARAMETER(device_address, return);
1884 BT_INFO("Device to be Trusted? [%d]", authorize);
1886 ret = device_set_authorized((bt_address_t*)device_address, authorize);
1887 if (ret != OAL_STATUS_SUCCESS) {
1888 BT_ERR("device_set_authorized: %d", ret);
1889 return BLUETOOTH_ERROR_INTERNAL;
1892 return BLUETOOTH_ERROR_NONE;
1895 gboolean _bt_is_device_connected(bluetooth_device_address_t *device_address, int svc_type)
1897 gboolean is_connected;
1898 oal_service_t svc_id;
1900 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1903 * TODO: While adding support for new profiles, need to add more
1904 * <svc_type, svc_id> mapping here.
1907 case BT_PROFILE_CONN_HID:
1908 svc_id = HID_SERVICE_ID;
1910 case BT_PROFILE_CONN_A2DP:
1911 svc_id = A2DP_SERVICE_ID; /* Remote is A2DP Sink */
1913 case BT_PROFILE_CONN_A2DP_SINK:
1914 svc_id = A2DP_SRC_SERVICE_ID; /* Remote is A2DP Source*/
1916 case BT_PROFILE_CONN_HSP:
1917 svc_id = HFP_HS_SERVICE_ID; /* Remote is HFP HF Unit */
1920 BT_DBG("Unknown svc_type: %d", svc_type);
1924 is_connected = device_get_svc_conn_state((bt_address_t*)device_address, svc_id);
1926 BT_DBG("svc_type: %d, is_connected: %s",
1927 svc_type, is_connected ? "TRUE" : "FALSE");
1929 return is_connected;
1932 int _bt_rfcomm_reply_conn_authorization(char *address, gboolean reply)
1934 bt_address_t bd_addr;
1939 retv_if(NULL == address, BLUETOOTH_ERROR_INVALID_PARAM);
1940 _bt_convert_addr_string_to_type(bd_addr.addr, address);
1941 res = device_reply_auth_request(&bd_addr, 0, reply, FALSE);
1942 if (res != OAL_STATUS_SUCCESS) {
1943 BT_ERR("authorize_response: %d", res);
1944 return BLUETOOTH_ERROR_INTERNAL;
1948 return BLUETOOTH_ERROR_NONE;
1951 int _bt_enable_rssi(bluetooth_device_address_t *addr, int link_type,
1952 int low_threshold, int in_range_threshold, int high_threshold)
1955 bt_address_t bd_addr;
1959 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
1961 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
1962 result = device_enable_rssi_monitoring(&bd_addr, link_type,
1963 low_threshold, in_range_threshold, high_threshold);
1964 if (result != OAL_STATUS_SUCCESS) {
1965 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
1966 return BLUETOOTH_ERROR_INTERNAL;
1970 return BLUETOOTH_ERROR_NONE;
1973 int _bt_get_rssi_strength(bluetooth_device_address_t *addr, int link_type)
1976 bt_address_t bd_addr;
1980 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
1982 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
1983 result = device_get_connected_link_rssi_strength(&bd_addr, link_type);
1984 if (result != OAL_STATUS_SUCCESS) {
1985 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
1986 return BLUETOOTH_ERROR_INTERNAL;
1990 return BLUETOOTH_ERROR_NONE;
1993 int _bt_set_passkey_notification(const char *sender, gboolean enable)
1997 BT_INFO("Set passkey notification(sender:%s, %s)",
1998 sender, enable ? "Enable" : "Disable");
2000 result = device_enable_gap_auth_notifications(OAL_PASSKEY_DISPLAY, enable);
2001 if (OAL_STATUS_SUCCESS != result) {
2002 BT_ERR("device_enable_gap_auth_notifications error: [%d]", result);
2003 return BLUETOOTH_ERROR_INTERNAL;
2006 g_free(passkey_watcher);
2008 passkey_watcher = g_strdup(sender);
2010 passkey_watcher = NULL;
2012 return BLUETOOTH_ERROR_NONE;
2015 static int __bt_get_device_pin_code(const char *address, char *pin_code)
2019 BT_CHECK_PARAMETER(address, return);
2020 BT_CHECK_PARAMETER(pin_code, return);
2022 for (l = pin_info_list; l != NULL; l = l->next) {
2023 bt_pin_code_info_t *pin_info = l->data;
2025 if (!pin_info || !pin_info->address)
2028 if (g_strcmp0(pin_info->address, address) == 0) {
2029 g_strlcpy(pin_code, pin_info->pin_code,
2030 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2031 return BLUETOOTH_ERROR_NONE;
2035 return BLUETOOTH_ERROR_NOT_FOUND;
2038 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2039 bluetooth_device_pin_code_t *pin_code)
2041 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2042 bt_pin_code_info_t *pin_info = NULL;
2045 BT_CHECK_PARAMETER(device_address, return);
2046 BT_CHECK_PARAMETER(pin_code, return);
2047 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2048 BLUETOOTH_ERROR_NO_RESOURCES);
2050 _bt_convert_addr_type_to_string(address, device_address->addr);
2052 for (l = pin_info_list; l != NULL; l = l->next) {
2055 if (!pin_info || !pin_info->address)
2058 if (g_strcmp0(pin_info->address, address) == 0) {
2059 g_free(pin_info->pin_code);
2060 pin_info->pin_code = g_strdup(pin_code->pin_code);
2061 return BLUETOOTH_ERROR_NONE;
2065 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2066 pin_info->address = g_strdup(address);
2067 pin_info->pin_code = g_strdup(pin_code->pin_code);
2068 pin_info_list = g_slist_append(pin_info_list, pin_info);
2070 return BLUETOOTH_ERROR_NONE;
2073 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2075 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2076 bt_pin_code_info_t *pin_info = NULL;
2081 BT_CHECK_PARAMETER(device_address, return);
2083 _bt_convert_addr_type_to_string(address, device_address->addr);
2085 for (l = pin_info_list; l != NULL; l = l->next) {
2088 if (!pin_info || !pin_info->address)
2091 if (g_strcmp0(pin_info->address, address) == 0) {
2092 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2093 g_free(pin_info->address);
2094 g_free(pin_info->pin_code);
2101 return BLUETOOTH_ERROR_NONE;