aboutsummaryrefslogtreecommitdiffstats
path: root/net/bluetooth/hci_conn.c
diff options
context:
space:
mode:
authorJohan Hedberg <johan.hedberg@intel.com>2014-03-25 04:30:49 -0400
committerMarcel Holtmann <marcel@holtmann.org>2014-03-26 12:31:38 -0400
commit3c857757ef6e5a4e472bd3e5c934709c2eb482af (patch)
tree62b46afba2b9348bbc6e0495d9a3ccb5d186bfce /net/bluetooth/hci_conn.c
parent5d2e9fadf43e87e690bfbe607313bf9be47867e4 (diff)
Bluetooth: Add directed advertising support through connect()
When we're in peripheral mode (HCI_ADVERTISING flag is set) the most natural mapping of connect() is to perform directed advertising to the peer device. This patch does the necessary changes to enable directed advertising and keeps the hci_conn state as BT_CONNECT in a similar way as is done for central or BR/EDR connection initiation. Signed-off-by: Johan Hedberg <johan.hedberg@intel.com> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
Diffstat (limited to 'net/bluetooth/hci_conn.c')
-rw-r--r--net/bluetooth/hci_conn.c77
1 files changed, 69 insertions, 8 deletions
diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c
index 129c22a85ccf..55a174317925 100644
--- a/net/bluetooth/hci_conn.c
+++ b/net/bluetooth/hci_conn.c
@@ -367,9 +367,23 @@ static void le_conn_timeout(struct work_struct *work)
367{ 367{
368 struct hci_conn *conn = container_of(work, struct hci_conn, 368 struct hci_conn *conn = container_of(work, struct hci_conn,
369 le_conn_timeout.work); 369 le_conn_timeout.work);
370 struct hci_dev *hdev = conn->hdev;
370 371
371 BT_DBG(""); 372 BT_DBG("");
372 373
374 /* We could end up here due to having done directed advertising,
375 * so clean up the state if necessary. This should however only
376 * happen with broken hardware or if low duty cycle was used
377 * (which doesn't have a timeout of its own).
378 */
379 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) {
380 u8 enable = 0x00;
381 hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable),
382 &enable);
383 hci_le_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT);
384 return;
385 }
386
373 hci_le_create_connection_cancel(conn); 387 hci_le_create_connection_cancel(conn);
374} 388}
375 389
@@ -549,6 +563,11 @@ void hci_le_conn_failed(struct hci_conn *conn, u8 status)
549 * favor of connection establishment, we should restart it. 563 * favor of connection establishment, we should restart it.
550 */ 564 */
551 hci_update_background_scan(hdev); 565 hci_update_background_scan(hdev);
566
567 /* Re-enable advertising in case this was a failed connection
568 * attempt as a peripheral.
569 */
570 mgmt_reenable_advertising(hdev);
552} 571}
553 572
554static void create_le_conn_complete(struct hci_dev *hdev, u8 status) 573static void create_le_conn_complete(struct hci_dev *hdev, u8 status)
@@ -609,6 +628,45 @@ static void hci_req_add_le_create_conn(struct hci_request *req,
609 conn->state = BT_CONNECT; 628 conn->state = BT_CONNECT;
610} 629}
611 630
631static void hci_req_directed_advertising(struct hci_request *req,
632 struct hci_conn *conn)
633{
634 struct hci_dev *hdev = req->hdev;
635 struct hci_cp_le_set_adv_param cp;
636 u8 own_addr_type;
637 u8 enable;
638
639 enable = 0x00;
640 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
641
642 /* Clear the HCI_ADVERTISING bit temporarily so that the
643 * hci_update_random_address knows that it's safe to go ahead
644 * and write a new random address. The flag will be set back on
645 * as soon as the SET_ADV_ENABLE HCI command completes.
646 */
647 clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
648
649 /* Set require_privacy to false so that the remote device has a
650 * chance of identifying us.
651 */
652 if (hci_update_random_address(req, false, &own_addr_type) < 0)
653 return;
654
655 memset(&cp, 0, sizeof(cp));
656 cp.type = LE_ADV_DIRECT_IND;
657 cp.own_address_type = own_addr_type;
658 cp.direct_addr_type = conn->dst_type;
659 bacpy(&cp.direct_addr, &conn->dst);
660 cp.channel_map = hdev->le_adv_channel_map;
661
662 hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);
663
664 enable = 0x01;
665 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
666
667 conn->state = BT_CONNECT;
668}
669
612struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst, 670struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
613 u8 dst_type, u8 sec_level, u8 auth_type) 671 u8 dst_type, u8 sec_level, u8 auth_type)
614{ 672{
@@ -618,9 +676,6 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
618 struct hci_request req; 676 struct hci_request req;
619 int err; 677 int err;
620 678
621 if (test_bit(HCI_ADVERTISING, &hdev->flags))
622 return ERR_PTR(-ENOTSUPP);
623
624 /* Some devices send ATT messages as soon as the physical link is 679 /* Some devices send ATT messages as soon as the physical link is
625 * established. To be able to handle these ATT messages, the user- 680 * established. To be able to handle these ATT messages, the user-
626 * space first establishes the connection and then starts the pairing 681 * space first establishes the connection and then starts the pairing
@@ -668,13 +723,20 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
668 return ERR_PTR(-ENOMEM); 723 return ERR_PTR(-ENOMEM);
669 724
670 conn->dst_type = dst_type; 725 conn->dst_type = dst_type;
671
672 conn->out = true;
673 conn->link_mode |= HCI_LM_MASTER;
674 conn->sec_level = BT_SECURITY_LOW; 726 conn->sec_level = BT_SECURITY_LOW;
675 conn->pending_sec_level = sec_level; 727 conn->pending_sec_level = sec_level;
676 conn->auth_type = auth_type; 728 conn->auth_type = auth_type;
677 729
730 hci_req_init(&req, hdev);
731
732 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) {
733 hci_req_directed_advertising(&req, conn);
734 goto create_conn;
735 }
736
737 conn->out = true;
738 conn->link_mode |= HCI_LM_MASTER;
739
678 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type); 740 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
679 if (params) { 741 if (params) {
680 conn->le_conn_min_interval = params->conn_min_interval; 742 conn->le_conn_min_interval = params->conn_min_interval;
@@ -684,8 +746,6 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
684 conn->le_conn_max_interval = hdev->le_conn_max_interval; 746 conn->le_conn_max_interval = hdev->le_conn_max_interval;
685 } 747 }
686 748
687 hci_req_init(&req, hdev);
688
689 /* If controller is scanning, we stop it since some controllers are 749 /* If controller is scanning, we stop it since some controllers are
690 * not able to scan and connect at the same time. Also set the 750 * not able to scan and connect at the same time. Also set the
691 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete 751 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
@@ -699,6 +759,7 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
699 759
700 hci_req_add_le_create_conn(&req, conn); 760 hci_req_add_le_create_conn(&req, conn);
701 761
762create_conn:
702 err = hci_req_run(&req, create_le_conn_complete); 763 err = hci_req_run(&req, create_le_conn_complete);
703 if (err) { 764 if (err) {
704 hci_conn_del(conn); 765 hci_conn_del(conn);