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 !incoming_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));
1021 case OAL_EVENT_RSSI_MONITORING_ENABLED: {
1022 event_dev_rssi_info_t *ev = event_data;
1023 char address[BT_ADDRESS_STRING_SIZE];
1028 _bt_convert_addr_type_to_string(address, ev->address.addr);
1029 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1030 address, ev->link_type, TRUE);
1031 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1034 case OAL_EVENT_RSSI_MONITORING_DISABLED: {
1035 event_dev_rssi_info_t *ev = event_data;
1036 char address[BT_ADDRESS_STRING_SIZE];
1041 _bt_convert_addr_type_to_string(address, ev->address.addr);
1042 param = g_variant_new("(isib)", BLUETOOTH_ERROR_NONE,
1043 address, ev->link_type, FALSE);
1044 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ENABLED, param);
1047 case OAL_EVENT_RSSI_ALERT_RECEIVED: {
1048 event_dev_rssi_info_t *ev = event_data;
1049 char address[BT_ADDRESS_STRING_SIZE];
1054 _bt_convert_addr_type_to_string(address, ev->address.addr);
1055 param = g_variant_new("(isiii)", BLUETOOTH_ERROR_NONE,
1056 address, ev->link_type, ev->alert_type, ev->rssi);
1057 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RSSI_ALERT, param);
1060 case OAL_EVENT_RAW_RSSI_RECEIVED: {
1061 event_dev_rssi_info_t *ev = event_data;
1062 char address[BT_ADDRESS_STRING_SIZE];
1067 _bt_convert_addr_type_to_string(address, ev->address.addr);
1068 param = g_variant_new("(isii)", BLUETOOTH_ERROR_NONE,
1069 address, ev->link_type, ev->rssi);
1070 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_RAW_RSSI, param);
1074 BT_INFO("Unhandled event..");
1078 /* Legacy Pairing event handler */
1079 static void __bt_device_pin_request_callback(remote_device_t* pin_req_event)
1082 char address[BT_ADDRESS_STRING_SIZE];
1083 char pin_code[BLUETOOTH_PIN_CODE_MAX_LENGTH + 1];
1086 _bt_convert_addr_type_to_string(address, pin_req_event->address.addr);
1088 BT_INFO("Address[%s]", address);
1089 BT_INFO("Name[%s]", pin_req_event->name);
1090 BT_INFO("COD[%d]", pin_req_event->cod);
1092 if (trigger_pairing_info) {
1093 /* BTAPI support only one pairing at a time */
1094 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1095 BT_ERR("New PIN request address [%s]", address);
1096 device_reject_pin_request(&pin_req_event->address);
1100 /* If user initiated bonding and auto response is possible, just reply with default 0000*/
1101 if (_bt_is_bonding_device_address(address) == TRUE &&
1102 _bt_agent_is_auto_response(pin_req_event->cod, address, pin_req_event->name)) {
1103 /* Note: Currently even if SYSPOPUP is supported, we use Fixed PIN "0000" for basic pairing
1104 as BT SYSPOPUP is currently not working for PIN KEY entry in Tizen platform. This needs
1105 to be checked and fixed apropriately */
1106 _bt_set_autopair_status_in_bonding_info(TRUE);
1107 device_accept_pin_request(&pin_req_event->address, "0000");
1108 } else if (_bt_agent_is_hid_keyboard(pin_req_event->cod)) {
1109 BT_DBG("Remote Device is HID keyboard Type..");
1110 char str_passkey[BT_PASSKEY_MAX_LENGTH + 1] = { 0 };
1112 if (_bt_agent_generate_passkey(str_passkey,
1113 BT_PASSKEY_MAX_LENGTH) != 0) {
1114 device_reject_pin_request(&pin_req_event->address);
1117 device_accept_pin_request(&pin_req_event->address, str_passkey);
1119 BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1120 param = g_variant_new("(isss)", BLUETOOTH_ERROR_NONE, address, pin_req_event->name, str_passkey);
1121 _bt_send_event(BT_ADAPTER_EVENT,
1122 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1123 BT_DBG("Key board pairing in process");
1124 } else if (BLUETOOTH_ERROR_NONE == __bt_get_device_pin_code(address, pin_code)) {
1125 BT_DBG("Use stored PIN code [%s]", pin_code);
1126 device_accept_pin_request(&pin_req_event->address, pin_code);
1128 if (_bt_is_bonding_device_address(address) == TRUE) {
1129 BT_DBG("Show Pin entry");
1130 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1131 trigger_pairing_info->addr = g_strdup(address);
1132 trigger_pairing_info->is_ssp = FALSE;
1134 BT_DBG("Send BLUETOOTH_EVENT_PIN_REQUEST");
1135 param = g_variant_new("(iss)", BLUETOOTH_ERROR_NONE, address, pin_req_event->name);
1136 _bt_send_event(BT_ADAPTER_EVENT,
1137 BLUETOOTH_EVENT_PIN_REQUEST, param);
1142 _bt_agent_release_memory();
1146 /* SSP Pairing event handler */
1147 static void __bt_device_ssp_passkey_entry_callback(remote_device_t* dev_info)
1150 gchar address[BT_ADDRESS_STR_LEN];
1153 int result = BLUETOOTH_ERROR_NONE;
1156 _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1158 name = dev_info->name;
1160 BT_INFO("Address[%s]", address);
1161 BT_INFO("Name[%s]", name);
1162 BT_INFO("COD[%d]", dev_info->cod);
1164 if (trigger_pairing_info) {
1165 /* BTAPI support only one pairing at a time */
1166 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1167 BT_ERR("New PIN request address [%s]", address);
1168 device_reject_pin_request(&dev_info->address);
1173 /* Set pairing data */
1174 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1175 trigger_pairing_info->addr = g_strdup(address);
1176 trigger_pairing_info->is_ssp = TRUE;
1178 param = g_variant_new("(iss)", result, p_addr, name);
1179 _bt_send_event(BT_ADAPTER_EVENT,
1180 BLUETOOTH_EVENT_PASSKEY_REQUEST, param);
1184 static void __bt_device_ssp_passkey_confirmation_callback(event_dev_passkey_t *dev_info)
1187 gchar address[BT_ADDRESS_STR_LEN];
1190 char str_passkey[7];
1191 int result = BLUETOOTH_ERROR_NONE;
1194 _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1196 name = dev_info->device_info.name;
1198 BT_INFO("Address[%s]", address);
1199 BT_INFO("Name[%s]", name);
1200 BT_INFO("COD[%d]", dev_info->device_info.cod);
1202 if (trigger_pairing_info) {
1203 /* BTAPI support only one pairing at a time */
1204 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1205 BT_ERR("New PIN request address [%s]", address);
1206 device_reject_pin_request(&dev_info->device_info.address);
1211 /* Set pairing data */
1212 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1213 trigger_pairing_info->addr = g_strdup(address);
1214 trigger_pairing_info->is_ssp = TRUE;
1216 BT_DBG("Send BLUETOOTH_EVENT_PASSKEY_CONFIRMATION");
1217 snprintf(str_passkey, sizeof(str_passkey), "%.6d", dev_info->pass_key);
1219 param = g_variant_new("(isss)", result, p_addr, name, str_passkey);
1220 _bt_send_event(BT_ADAPTER_EVENT,
1221 BLUETOOTH_EVENT_PASSKEY_CONFIRM_REQUEST, param);
1225 static void __bt_device_ssp_passkey_display_callback(event_dev_passkey_t *dev_info)
1228 gchar address[BT_ADDRESS_STR_LEN];
1231 char str_passkey[7];
1232 int result = BLUETOOTH_ERROR_NONE;
1235 _bt_convert_addr_type_to_string(address, dev_info->device_info.address.addr);
1237 name = dev_info->device_info.name;
1239 BT_INFO("Address[%s]", address);
1240 BT_INFO("Name[%s]", name);
1241 BT_INFO("COD[%d]", dev_info->device_info.cod);
1243 if (trigger_pairing_info) {
1244 /* BTAPI support only one pairing at a time */
1245 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1246 BT_ERR("New PIN request address [%s]", address);
1247 device_reject_pin_request(&dev_info->device_info.address);
1252 /* Set pairing data */
1253 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1254 trigger_pairing_info->addr = g_strdup(address);
1255 trigger_pairing_info->is_ssp = TRUE;
1257 BT_DBG("Send BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY");
1258 snprintf(str_passkey, sizeof(str_passkey), "%06d", dev_info->pass_key);
1260 param = g_variant_new("(isss)", result, p_addr, name, str_passkey);
1261 if (passkey_watcher) {
1262 BT_INFO("Send passkey to %s", passkey_watcher);
1263 _bt_send_event_to_dest(passkey_watcher, BT_ADAPTER_EVENT,
1264 BLUETOOTH_EVENT_PASSKEY_NOTIFICATION, param);
1266 _bt_send_event(BT_ADAPTER_EVENT,
1267 BLUETOOTH_EVENT_KEYBOARD_PASSKEY_DISPLAY, param);
1272 static void __bt_device_ssp_consent_callback(remote_device_t* dev_info)
1274 gchar address[BT_ADDRESS_STR_LEN];
1281 _bt_convert_addr_type_to_string(address, dev_info->address.addr);
1282 name = dev_info->name;
1283 cod = dev_info->cod;
1285 BT_INFO("Address[%s]", address);
1286 BT_INFO("Name[%s]", name);
1287 BT_INFO("COD[%d]", cod);
1289 if (trigger_pairing_info) {
1290 /* BTAPI support only one pairing at a time */
1291 BT_ERR("Already Pairing address [%s]", trigger_pairing_info->addr);
1292 BT_ERR("New PIN request address [%s]", address);
1293 device_reject_pin_request(&dev_info->address);
1298 /* Set pairing data */
1299 trigger_pairing_info = g_malloc0(sizeof(bt_pairing_data_t));
1300 trigger_pairing_info->addr = g_strdup(address);
1301 trigger_pairing_info->is_ssp = TRUE;
1303 local_major = ((cod >> 8) & 0x001f);
1304 local_minor = (cod & 0x00fc);
1305 BT_DBG("SSP_CONSENT: Major type=[0x%x] and Minor type=[0x%x]", local_major, local_minor);
1307 /*TODO: BLUETOOTH_EVENT_SSP_CONSENT_REQUEST to be handled in Tizen */
1311 static void __bt_device_conn_state_changed_callback(event_dev_conn_status_t *acl_event,
1312 gboolean connected, unsigned char type)
1314 gchar address[BT_ADDRESS_STR_LEN];
1315 int result = BLUETOOTH_ERROR_NONE;
1316 GVariant *param = NULL;
1317 bt_device_conn_info_t conn_info;
1320 _bt_convert_addr_type_to_string(address, acl_event->address.addr);
1323 param = g_variant_new("(isy)", result, address, type);
1324 _bt_send_event(BT_DEVICE_EVENT,
1325 BLUETOOTH_EVENT_DEVICE_CONNECTED,
1328 param = g_variant_new("(isy)", result, address, type);
1329 _bt_send_event(BT_DEVICE_EVENT,
1330 BLUETOOTH_EVENT_DEVICE_DISCONNECTED,
1334 conn_info.connected = connected;
1335 conn_info.type = type;
1336 /* Update local cache */
1337 _bt_update_remote_dev_property(address, DEV_PROP_CONNECTED, (void *)&conn_info);
1342 static void __bt_device_remote_device_found_callback(gpointer event_data, gboolean is_ble)
1344 bt_remote_dev_info_t *dev_info = NULL;
1345 int result = BLUETOOTH_ERROR_NONE;
1346 GVariant *param = NULL;
1347 GVariant *uuids = NULL;
1348 GVariant *manufacturer_data = NULL;
1349 GVariantBuilder *builder = NULL;
1353 ret_if(_bt_is_discovering() == FALSE);
1354 ret_if(event_data == NULL);
1356 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
1359 event_ble_dev_found_t * oal_ble_dev = event_data;
1360 BT_INFO("Device type [%d]", oal_ble_dev->device_info.type);
1362 _bt_copy_remote_dev(dev_info, &oal_ble_dev->device_info);
1364 dev_info->manufacturer_data_len = oal_ble_dev->adv_len;
1365 if (dev_info->manufacturer_data_len)
1366 dev_info->manufacturer_data = g_memdup(oal_ble_dev->adv_data,
1367 dev_info->manufacturer_data_len);
1369 dev_info->manufacturer_data = NULL;
1370 BT_DBG("----Advertising Data Length: %d", dev_info->manufacturer_data_len);
1372 event_dev_found_t * oal_dev = event_data;
1373 _bt_copy_remote_dev(dev_info, &oal_dev->device_info);
1376 /* If Remote device name is NULL or still RNR is not done then display address as name. */
1377 if (dev_info->name == NULL)
1378 dev_info->name = g_strdup(dev_info->address);
1379 BT_DBG("Name %s", dev_info->name);
1381 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1382 for (i = 0; i < dev_info->uuid_count; i++)
1383 g_variant_builder_add(builder, "s", dev_info->uuids[i]);
1385 uuids = g_variant_new("as", builder);
1386 g_variant_builder_unref(builder);
1388 manufacturer_data = g_variant_new_from_data(G_VARIANT_TYPE_BYTESTRING,
1389 dev_info->manufacturer_data,
1390 dev_info->manufacturer_data_len,
1394 param = g_variant_new("(isunsbub@asn@ay)", result,
1400 dev_info->connected,
1403 dev_info->manufacturer_data_len,
1406 _bt_send_event(BT_ADAPTER_EVENT,
1407 BLUETOOTH_EVENT_REMOTE_DEVICE_FOUND,
1410 _bt_free_remote_dev(dev_info);
1414 static void __bt_device_trusted_callback(gboolean trusted, event_dev_trust_t* info)
1416 gchar address[BT_ADDRESS_STR_LEN];
1417 int result = BLUETOOTH_ERROR_NONE;
1418 GVariant *param = NULL;
1422 _bt_convert_addr_type_to_string(address, info->address.addr);
1424 /* Update local cache */
1425 _bt_update_remote_dev_property(address, DEV_PROP_TRUST, (void *)&trusted);
1427 param = g_variant_new("(is)", result, address);
1428 event = trusted ? BLUETOOTH_EVENT_DEVICE_AUTHORIZED :
1429 BLUETOOTH_EVENT_DEVICE_UNAUTHORIZED;
1430 /* Send event to application */
1431 _bt_send_event(BT_DEVICE_EVENT,
1438 static void __bt_free_pairing_info(bt_pairing_data_t **p_info)
1441 bt_pairing_data_t * info = *p_info;
1452 static void __bt_free_bond_info(uint8_t type)
1457 case BT_DEVICE_BOND_INFO:
1458 if (!trigger_bond_info)
1461 if (trigger_bond_info->addr)
1462 g_free(trigger_bond_info->addr);
1463 if (trigger_bond_info->dev_addr)
1464 g_free(trigger_bond_info->dev_addr);
1465 if (trigger_bond_info->dev_info)
1466 _bt_free_remote_dev(trigger_bond_info->dev_info);
1467 g_free(trigger_bond_info);
1468 trigger_bond_info = NULL;
1470 case BT_DEVICE_INCOMING_BOND_INFO:
1471 if (!incoming_bond_info)
1474 if (incoming_bond_info->dev_info)
1475 _bt_free_remote_dev(incoming_bond_info->dev_info);
1476 g_free(incoming_bond_info);
1477 incoming_bond_info = NULL;
1479 case BT_DEVICE_UNBOND_INFO:
1480 if (!trigger_unbond_info)
1483 if (trigger_unbond_info->addr)
1484 g_free(trigger_unbond_info->addr);
1485 if (trigger_unbond_info->dev_addr)
1486 g_free(trigger_unbond_info->dev_addr);
1487 if (trigger_unbond_info->dev_info)
1488 _bt_free_remote_dev(trigger_unbond_info->dev_info);
1489 g_free(trigger_unbond_info);
1490 trigger_unbond_info = NULL;
1496 static void __bt_free_service_search_info(bt_service_search_info_data_t **p_info)
1498 bt_service_search_info_data_t * info = *p_info;
1505 if (info->dev_addr) {
1506 g_free(info->dev_addr);
1507 info->dev_addr = NULL;
1510 if (info->dev_info) {
1511 _bt_free_remote_dev(info->dev_info);
1512 info->dev_info = NULL;
1520 static int __bt_device_handle_bond_state(void)
1522 BT_INFO("Current Bond state: %d", bt_device_bond_state);
1523 int ret = OAL_STATUS_INTERNAL_ERROR;
1525 switch (bt_device_bond_state) {
1526 case BT_DEVICE_BOND_STATE_CANCEL_DISCOVERY:
1527 /*TODO:Bonding during discovery: Unhandled!!*/
1528 BT_INFO("Bonding during discovery: Unhandled!!");
1530 case BT_DEVICE_BOND_STATE_DISCOVERY_CANCELLED:
1531 /*TODO:Bonding during discovery: Unhandled!!*/
1532 BT_INFO("Bonding during discovery: Unhandled!!");
1534 case BT_DEVICE_BOND_STATE_REMOVE_BONDING:
1535 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVED_BONDING;
1536 ret = device_destroy_bond((bt_address_t *)trigger_bond_info->dev_addr);
1537 if (ret != OAL_STATUS_SUCCESS)
1538 ret = __bt_device_handle_bond_state();
1540 case BT_DEVICE_BOND_STATE_REMOVED_BONDING:
1541 bt_device_bond_state = BT_DEVICE_BOND_STATE_NONE;
1542 ret = device_create_bond((bt_address_t *)trigger_bond_info->dev_addr, trigger_bond_info->conn_type);
1543 /* Bonding procedure was started but unfortunately could not complete.
1544 Basically removed bonding was success, but create bond request could not proceed
1545 So lets cleanup the context */
1546 if (ret != OAL_STATUS_SUCCESS) {
1547 BT_ERR("Create Bond procedure could not suceed");
1548 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1549 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1550 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1551 __bt_free_pairing_info(&trigger_pairing_info);
1554 case BT_DEVICE_BOND_STATE_NONE:
1555 BT_INFO("Create Bond failed!!");
1556 if (trigger_bond_info) {
1557 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_INTERNAL, BT_BOND_DEVICE,
1558 trigger_bond_info->addr, BT_ADDRESS_STRING_SIZE);
1559 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1560 __bt_free_pairing_info(&trigger_pairing_info);
1567 if (ret != OAL_STATUS_SUCCESS)
1568 return BLUETOOTH_ERROR_INTERNAL;
1570 return BLUETOOTH_ERROR_NONE;
1573 int _bt_device_get_bonded_device_info(bluetooth_device_address_t *addr)
1576 bt_address_t bd_addr;
1580 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
1582 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
1583 result = device_query_attributes(&bd_addr);
1584 if (result != OAL_STATUS_SUCCESS) {
1585 BT_ERR("device_query_attributes error: [%d]", result);
1586 return BLUETOOTH_ERROR_INTERNAL;
1590 return BLUETOOTH_ERROR_NONE;
1593 int _bt_set_alias(bluetooth_device_address_t *device_address, const char *alias)
1596 char address[BT_ADDRESS_STRING_SIZE];
1599 BT_CHECK_PARAMETER(alias, return);
1601 ret = device_set_alias((bt_address_t *)device_address, (char *)alias);
1602 if (ret != OAL_STATUS_SUCCESS) {
1603 BT_ERR("device_set_alias: %d", ret);
1604 return BLUETOOTH_ERROR_INTERNAL;
1607 /* Update local cache */
1608 _bt_convert_addr_type_to_string(address, device_address->addr);
1609 _bt_update_remote_dev_property(address, DEV_PROP_ALIAS, (void *)alias);
1612 return BLUETOOTH_ERROR_NONE;
1615 int _bt_bond_device(bluetooth_device_address_t *device_address,
1616 unsigned short conn_type, GArray **out_param1)
1618 int result = BLUETOOTH_ERROR_NONE;
1619 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1620 bluetooth_device_info_t dev_info;
1623 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1625 /* If bonding or discovery already going on */
1626 if (trigger_bond_info || _bt_is_discovering()) {
1627 BT_ERR("Device is buzy, bonding can not proceed now..");
1628 result = BLUETOOTH_ERROR_DEVICE_BUSY;
1632 /*TODO: If unbonding with same device going on */
1633 _bt_convert_addr_type_to_string(address, device_address->addr);
1635 trigger_bond_info = g_malloc0(sizeof(bt_bond_data_t));
1636 trigger_bond_info->addr = g_strdup(address);
1637 trigger_bond_info->conn_type = conn_type;
1638 trigger_bond_info->is_device_creating = TRUE;
1639 trigger_bond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1640 trigger_bond_info->dev_info = NULL;
1642 /* Ready to initiate bonding */
1644 /* In Tizen, we will first remove bond and then attempt to create bond to keep
1645 consistency with bluedroid. Even if remove bond fails due to device not already
1646 bonded, then straight away create bond is triggered. This is because, remove bond
1647 is handled differently in bluedroid and bluez. In Bluez, if device is
1648 already removed, remove bond call fails.
1649 However in bluedroid, remove bond on already removed device returns success. So we will
1650 handle the cases transparently*/
1651 bt_device_bond_state = BT_DEVICE_BOND_STATE_REMOVE_BONDING;
1652 bond_retry_count = 0;
1653 result = __bt_device_handle_bond_state();
1655 if (result != BLUETOOTH_ERROR_NONE)
1662 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1663 memcpy(dev_info.device_address.addr, device_address->addr,
1664 BLUETOOTH_ADDRESS_LENGTH);
1666 g_array_append_vals(*out_param1, &dev_info,
1667 sizeof(bluetooth_device_info_t));
1668 __bt_free_bond_info(BT_DEVICE_BOND_INFO);
1674 int _bt_unbond_device(bluetooth_device_address_t *device_address,
1675 GArray **out_param1)
1677 int result = OAL_STATUS_SUCCESS;
1678 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1679 bluetooth_device_info_t dev_info;
1682 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1684 _bt_convert_addr_type_to_string(address, device_address->addr);
1686 trigger_unbond_info = g_malloc0(sizeof(bt_bond_data_t));
1687 trigger_unbond_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1688 trigger_unbond_info->addr = g_strdup(address);
1689 trigger_unbond_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1691 /* Check if Bonding is already going on, we should not abruptly remove bonding*/
1692 if (trigger_bond_info && strncmp(trigger_bond_info->addr, trigger_unbond_info->addr, BT_ADDRESS_STRING_SIZE) == 0) {
1693 BT_ERR("Bonding with same device already ongoing");
1694 result = BLUETOOTH_ERROR_PERMISSION_DEINED;
1698 result = device_destroy_bond((bt_address_t *)device_address);
1699 if (result != OAL_STATUS_SUCCESS)
1705 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1706 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1707 trigger_unbond_info->addr);
1709 g_array_append_vals(*out_param1, &dev_info,
1710 sizeof(bluetooth_device_info_t));
1711 __bt_free_bond_info(BT_DEVICE_UNBOND_INFO);
1716 int _bt_cancel_bonding(void)
1718 int result = OAL_STATUS_SUCCESS;
1721 retv_if(trigger_bond_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1723 result = device_stop_bond((bt_address_t *)trigger_bond_info->dev_addr);
1725 if (result == OAL_STATUS_SUCCESS)
1726 trigger_bond_info->is_cancelled_by_user = TRUE;
1731 int _bt_passkey_reply(const char *passkey, gboolean cnfm_reply)
1733 bluetooth_device_address_t device_address;
1734 int ret = OAL_STATUS_SUCCESS;
1735 BT_INFO("reply: %d", cnfm_reply);
1737 retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1738 retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1740 _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1742 if (trigger_pairing_info->is_ssp) {
1744 ret = device_accept_passkey_entry((bt_address_t *)&device_address, atoi(passkey));
1746 ret = device_reject_passkey_entry((bt_address_t *)&device_address);
1747 trigger_pairing_info->is_ssp = FALSE;
1750 ret = device_accept_pin_request((bt_address_t *)&device_address, passkey);
1752 ret = device_reject_pin_request((bt_address_t *)&device_address);
1755 __bt_free_pairing_info(&trigger_pairing_info);
1757 if (ret != OAL_STATUS_SUCCESS) {
1758 BT_ERR("_bt_device_handle_passkey_reply: err [%d]", ret);
1759 return BLUETOOTH_ERROR_INTERNAL;
1763 return BLUETOOTH_ERROR_NONE;
1766 int _bt_passkey_confirmation_reply(gboolean cnfm_reply)
1768 BT_INFO("BT_PASSKEY_CONFIRMATION_REPLY");
1769 bluetooth_device_address_t device_address;
1770 int ret = OAL_STATUS_SUCCESS;
1771 BT_INFO("reply: %d", cnfm_reply);
1773 retv_if(trigger_pairing_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1774 retv_if(trigger_pairing_info->addr == NULL, BLUETOOTH_ERROR_INTERNAL);
1776 _bt_convert_addr_string_to_type(device_address.addr, trigger_pairing_info->addr);
1778 ret = device_reply_passkey_confirmation((bt_address_t *)&device_address, cnfm_reply);
1780 __bt_free_pairing_info(&trigger_pairing_info);
1781 if (ret != OAL_STATUS_SUCCESS) {
1782 BT_ERR("_bt_device_handle_passkey_confirmation_reply: err [%d]", ret);
1783 return BLUETOOTH_ERROR_INTERNAL;
1787 return BLUETOOTH_ERROR_NONE;
1790 gboolean _bt_device_is_pairing(void)
1792 return (trigger_pairing_info) ? TRUE : FALSE;
1795 gboolean _bt_device_is_bonding(void)
1797 return (trigger_bond_info) ? TRUE : FALSE;
1800 gboolean _bt_is_bonding_device_address(const char *address)
1802 if (trigger_bond_info == NULL || trigger_bond_info->addr == NULL)
1805 if (g_strcmp0(trigger_bond_info->addr, address) == 0) {
1806 BT_DBG("[%s] is bonding device", address);
1810 BT_DBG("[%s] is NOT bonding device", address);
1814 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
1816 ret_if(trigger_bond_info == NULL);
1817 trigger_bond_info->is_autopair = is_autopair;
1820 int _bt_search_device(bluetooth_device_address_t *device_address)
1822 int result = OAL_STATUS_SUCCESS;
1825 BT_CHECK_PARAMETER(device_address, return);
1827 if (trigger_bond_info) {
1828 BT_ERR("Bonding in progress");
1829 return BLUETOOTH_ERROR_DEVICE_BUSY;
1832 if (service_search_info) {
1833 BT_ERR("Service searching in progress");
1834 return BLUETOOTH_ERROR_DEVICE_BUSY;
1837 /* allocate user data so that it can be retrieved in callback */
1838 service_search_info = g_malloc0(sizeof(bt_service_search_info_data_t));
1839 service_search_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1840 service_search_info->dev_addr = g_memdup(device_address, sizeof(bluetooth_device_address_t));
1842 _bt_convert_addr_type_to_string(service_search_info->addr,
1843 device_address->addr);
1845 result = device_query_services((bt_address_t *)device_address);
1847 if (result != OAL_STATUS_SUCCESS) {
1848 BT_ERR("Device Service Search Failed..: %d", result);
1849 __bt_free_service_search_info(&service_search_info);
1850 return BLUETOOTH_ERROR_INTERNAL;
1852 return BLUETOOTH_ERROR_NONE;
1855 int _bt_cancel_search_device(void)
1857 int ret = OAL_STATUS_SUCCESS;
1858 retv_if(service_search_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1860 ret = device_stop_query_sevices((bt_address_t *)service_search_info->dev_addr);
1862 if (ret != OAL_STATUS_SUCCESS) {
1863 BT_ERR("SDP Cancel request failed [%d]", ret);
1864 return BLUETOOTH_ERROR_INTERNAL;
1867 __bt_device_handle_pending_requests(BLUETOOTH_ERROR_CANCEL_BY_USER, BT_SEARCH_SERVICE,
1868 service_search_info->addr, BT_ADDRESS_STRING_SIZE);
1870 __bt_free_service_search_info(&service_search_info);
1872 return BLUETOOTH_ERROR_NONE;
1876 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1879 int ret = OAL_STATUS_SUCCESS;
1882 BT_CHECK_PARAMETER(device_address, return);
1883 BT_INFO("Device to be Trusted? [%d]", authorize);
1885 ret = device_set_authorized((bt_address_t*)device_address, authorize);
1886 if (ret != OAL_STATUS_SUCCESS) {
1887 BT_ERR("device_set_authorized: %d", ret);
1888 return BLUETOOTH_ERROR_INTERNAL;
1891 return BLUETOOTH_ERROR_NONE;
1894 gboolean _bt_is_device_connected(bluetooth_device_address_t *device_address, int svc_type)
1896 gboolean is_connected;
1897 oal_service_t svc_id;
1899 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1902 * TODO: While adding support for new profiles, need to add more
1903 * <svc_type, svc_id> mapping here.
1906 case BT_PROFILE_CONN_HID:
1907 svc_id = HID_SERVICE_ID;
1909 case BT_PROFILE_CONN_A2DP:
1910 svc_id = A2DP_SERVICE_ID; /* Remote is A2DP Sink */
1912 case BT_PROFILE_CONN_A2DP_SINK:
1913 svc_id = A2DP_SRC_SERVICE_ID; /* Remote is A2DP Source*/
1915 case BT_PROFILE_CONN_HSP:
1916 svc_id = HFP_HS_SERVICE_ID; /* Remote is HFP HF Unit */
1919 BT_DBG("Unknown svc_type: %d", svc_type);
1923 is_connected = device_get_svc_conn_state((bt_address_t*)device_address, svc_id);
1925 BT_DBG("svc_type: %d, is_connected: %s",
1926 svc_type, is_connected ? "TRUE" : "FALSE");
1928 return is_connected;
1931 int _bt_rfcomm_reply_conn_authorization(char *address, gboolean reply)
1933 bt_address_t bd_addr;
1938 retv_if(NULL == address, BLUETOOTH_ERROR_INVALID_PARAM);
1939 _bt_convert_addr_string_to_type(bd_addr.addr, address);
1940 res = device_reply_auth_request(&bd_addr, 0, reply, FALSE);
1941 if (res != OAL_STATUS_SUCCESS) {
1942 BT_ERR("authorize_response: %d", res);
1943 return BLUETOOTH_ERROR_INTERNAL;
1947 return BLUETOOTH_ERROR_NONE;
1950 int _bt_enable_rssi(bluetooth_device_address_t *addr, int link_type,
1951 int low_threshold, int in_range_threshold, int high_threshold)
1954 bt_address_t bd_addr;
1958 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
1960 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
1961 result = device_enable_rssi_monitoring(&bd_addr, link_type,
1962 low_threshold, in_range_threshold, high_threshold);
1963 if (result != OAL_STATUS_SUCCESS) {
1964 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
1965 return BLUETOOTH_ERROR_INTERNAL;
1969 return BLUETOOTH_ERROR_NONE;
1972 int _bt_get_rssi_strength(bluetooth_device_address_t *addr, int link_type)
1975 bt_address_t bd_addr;
1979 retv_if(!addr, BLUETOOTH_ERROR_INVALID_PARAM);
1981 memcpy(bd_addr.addr, addr, BLUETOOTH_ADDRESS_LENGTH);
1982 result = device_get_connected_link_rssi_strength(&bd_addr, link_type);
1983 if (result != OAL_STATUS_SUCCESS) {
1984 BT_ERR("device_get_connected_link_rssi_strength error: [%d]", result);
1985 return BLUETOOTH_ERROR_INTERNAL;
1989 return BLUETOOTH_ERROR_NONE;
1992 int _bt_set_passkey_notification(const char *sender, gboolean enable)
1996 BT_INFO("Set passkey notification(sender:%s, %s)",
1997 sender, enable ? "Enable" : "Disable");
1999 result = device_enable_gap_auth_notifications(OAL_PASSKEY_DISPLAY,enable);
2000 if (OAL_STATUS_SUCCESS != result) {
2001 BT_ERR("device_enable_gap_auth_notifications error: [%d]", result);
2002 return BLUETOOTH_ERROR_INTERNAL;
2005 g_free(passkey_watcher);
2007 passkey_watcher = g_strdup(sender);
2009 passkey_watcher = NULL;
2011 return BLUETOOTH_ERROR_NONE;
2014 static int __bt_get_device_pin_code(const char *address, char *pin_code)
2018 BT_CHECK_PARAMETER(address, return);
2019 BT_CHECK_PARAMETER(pin_code, return);
2021 for (l = pin_info_list; l != NULL; l = l->next) {
2022 bt_pin_code_info_t *pin_info = l->data;
2024 if (!pin_info || !pin_info->address)
2027 if (g_strcmp0(pin_info->address, address) == 0) {
2028 g_strlcpy(pin_code, pin_info->pin_code,
2029 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2030 return BLUETOOTH_ERROR_NONE;
2034 return BLUETOOTH_ERROR_NOT_FOUND;
2037 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2038 bluetooth_device_pin_code_t *pin_code)
2040 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2041 bt_pin_code_info_t *pin_info = NULL;
2044 BT_CHECK_PARAMETER(device_address, return);
2045 BT_CHECK_PARAMETER(pin_code, return);
2046 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2047 BLUETOOTH_ERROR_NO_RESOURCES);
2049 _bt_convert_addr_type_to_string(address, device_address->addr);
2051 for (l = pin_info_list; l != NULL; l = l->next) {
2054 if (!pin_info || !pin_info->address)
2057 if (g_strcmp0(pin_info->address, address) == 0) {
2058 g_free(pin_info->pin_code);
2059 pin_info->pin_code = g_strdup(pin_code->pin_code);
2060 return BLUETOOTH_ERROR_NONE;
2064 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2065 pin_info->address = g_strdup(address);
2066 pin_info->pin_code = g_strdup(pin_code->pin_code);
2067 pin_info_list = g_slist_append(pin_info_list, pin_info);
2069 return BLUETOOTH_ERROR_NONE;
2072 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2074 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2075 bt_pin_code_info_t *pin_info = NULL;
2080 BT_CHECK_PARAMETER(device_address, return);
2082 _bt_convert_addr_type_to_string(address, device_address->addr);
2084 for (l = pin_info_list; l != NULL; l = l->next) {
2087 if (!pin_info || !pin_info->address)
2090 if (g_strcmp0(pin_info->address, address) == 0) {
2091 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2092 g_free(pin_info->pin_code);
2093 g_free(pin_info->pin_code);
2100 return BLUETOOTH_ERROR_NONE;