Skip to content

Commit 3c85775

Browse files
jhedbergholtmann
authored andcommitted
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>
1 parent 5d2e9fa commit 3c85775

3 files changed

Lines changed: 87 additions & 10 deletions

File tree

‎include/net/bluetooth/hci.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ enum {
367367
#define HCI_ERROR_REMOTE_POWER_OFF 0x15
368368
#define HCI_ERROR_LOCAL_HOST_TERM 0x16
369369
#define HCI_ERROR_PAIRING_NOT_ALLOWED 0x18
370+
#define HCI_ERROR_ADVERTISING_TIMEOUT 0x3c
370371

371372
/* Flow control modes */
372373
#define HCI_FLOW_CTL_MODE_PACKET_BASED 0x00

‎net/bluetooth/hci_conn.c‎

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,23 @@ static void le_conn_timeout(struct work_struct *work)
367367
{
368368
struct hci_conn *conn = container_of(work, struct hci_conn,
369369
le_conn_timeout.work);
370+
struct hci_dev *hdev = conn->hdev;
370371

371372
BT_DBG("");
372373

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+
373387
hci_le_create_connection_cancel(conn);
374388
}
375389

@@ -549,6 +563,11 @@ void hci_le_conn_failed(struct hci_conn *conn, u8 status)
549563
* favor of connection establishment, we should restart it.
550564
*/
551565
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);
552571
}
553572

554573
static 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,
609628
conn->state = BT_CONNECT;
610629
}
611630

631+
static 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+
612670
struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
613671
u8 dst_type, u8 sec_level, u8 auth_type)
614672
{
@@ -618,9 +676,6 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
618676
struct hci_request req;
619677
int err;
620678

621-
if (test_bit(HCI_ADVERTISING, &hdev->flags))
622-
return ERR_PTR(-ENOTSUPP);
623-
624679
/* Some devices send ATT messages as soon as the physical link is
625680
* established. To be able to handle these ATT messages, the user-
626681
* 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,
668723
return ERR_PTR(-ENOMEM);
669724

670725
conn->dst_type = dst_type;
671-
672-
conn->out = true;
673-
conn->link_mode |= HCI_LM_MASTER;
674726
conn->sec_level = BT_SECURITY_LOW;
675727
conn->pending_sec_level = sec_level;
676728
conn->auth_type = auth_type;
677729

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+
678740
params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
679741
if (params) {
680742
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,
684746
conn->le_conn_max_interval = hdev->le_conn_max_interval;
685747
}
686748

687-
hci_req_init(&req, hdev);
688-
689749
/* If controller is scanning, we stop it since some controllers are
690750
* not able to scan and connect at the same time. Also set the
691751
* 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,
699759

700760
hci_req_add_le_create_conn(&req, conn);
701761

762+
create_conn:
702763
err = hci_req_run(&req, create_le_conn_complete);
703764
if (err) {
704765
hci_conn_del(conn);

‎net/bluetooth/hci_event.c‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,10 +991,25 @@ static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
991991
if (!sent)
992992
return;
993993

994+
if (status)
995+
return;
996+
994997
hci_dev_lock(hdev);
995998

996-
if (!status)
997-
mgmt_advertising(hdev, *sent);
999+
/* If we're doing connection initation as peripheral. Set a
1000+
* timeout in case something goes wrong.
1001+
*/
1002+
if (*sent) {
1003+
struct hci_conn *conn;
1004+
1005+
conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
1006+
if (conn)
1007+
queue_delayed_work(hdev->workqueue,
1008+
&conn->le_conn_timeout,
1009+
HCI_LE_CONN_TIMEOUT);
1010+
}
1011+
1012+
mgmt_advertising(hdev, *sent);
9981013

9991014
hci_dev_unlock(hdev);
10001015
}

0 commit comments

Comments
 (0)