Bluetooth: Simplify usage of the enable_advertising function
By adding support for disabling advertising when necessary and doing the checks for existing LE connections inside the enable_advertising function we can simplify the calling code quite a lot. Signed-off-by: Johan Hedberg <johan.hedberg@intel.com> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
This commit is contained in:
parent
5ce194c4a7
commit
0ec5ae8438
@ -1039,6 +1039,13 @@ static bool get_connectable(struct hci_dev *hdev)
|
|||||||
return test_bit(HCI_CONNECTABLE, &hdev->dev_flags);
|
return test_bit(HCI_CONNECTABLE, &hdev->dev_flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void disable_advertising(struct hci_request *req)
|
||||||
|
{
|
||||||
|
u8 enable = 0x00;
|
||||||
|
|
||||||
|
hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
|
||||||
|
}
|
||||||
|
|
||||||
static void enable_advertising(struct hci_request *req)
|
static void enable_advertising(struct hci_request *req)
|
||||||
{
|
{
|
||||||
struct hci_dev *hdev = req->hdev;
|
struct hci_dev *hdev = req->hdev;
|
||||||
@ -1046,6 +1053,12 @@ static void enable_advertising(struct hci_request *req)
|
|||||||
u8 own_addr_type, enable = 0x01;
|
u8 own_addr_type, enable = 0x01;
|
||||||
bool connectable;
|
bool connectable;
|
||||||
|
|
||||||
|
if (hci_conn_num(hdev, LE_LINK) > 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (test_bit(HCI_LE_ADV, &hdev->dev_flags))
|
||||||
|
disable_advertising(req);
|
||||||
|
|
||||||
/* Clear the HCI_LE_ADV bit temporarily so that the
|
/* Clear the HCI_LE_ADV bit temporarily so that the
|
||||||
* hci_update_random_address knows that it's safe to go ahead
|
* hci_update_random_address knows that it's safe to go ahead
|
||||||
* and write a new random address. The flag will be set back on
|
* and write a new random address. The flag will be set back on
|
||||||
@ -1074,13 +1087,6 @@ static void enable_advertising(struct hci_request *req)
|
|||||||
hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
|
hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void disable_advertising(struct hci_request *req)
|
|
||||||
{
|
|
||||||
u8 enable = 0x00;
|
|
||||||
|
|
||||||
hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void service_cache_off(struct work_struct *work)
|
static void service_cache_off(struct work_struct *work)
|
||||||
{
|
{
|
||||||
struct hci_dev *hdev = container_of(work, struct hci_dev,
|
struct hci_dev *hdev = container_of(work, struct hci_dev,
|
||||||
@ -1112,19 +1118,14 @@ static void rpa_expired(struct work_struct *work)
|
|||||||
|
|
||||||
set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
|
set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
|
||||||
|
|
||||||
if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags) ||
|
if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags))
|
||||||
hci_conn_num(hdev, LE_LINK) > 0)
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/* The generation of a new RPA and programming it into the
|
/* The generation of a new RPA and programming it into the
|
||||||
* controller happens in the enable_advertising() function.
|
* controller happens in the enable_advertising() function.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
hci_req_init(&req, hdev);
|
hci_req_init(&req, hdev);
|
||||||
|
|
||||||
disable_advertising(&req);
|
|
||||||
enable_advertising(&req);
|
enable_advertising(&req);
|
||||||
|
|
||||||
hci_req_run(&req, NULL);
|
hci_req_run(&req, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1864,10 +1865,8 @@ static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
|
|||||||
write_fast_connectable(&req, false);
|
write_fast_connectable(&req, false);
|
||||||
|
|
||||||
if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) &&
|
if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) &&
|
||||||
hci_conn_num(hdev, LE_LINK) == 0) {
|
!test_bit(HCI_LE_ADV, &hdev->dev_flags))
|
||||||
disable_advertising(&req);
|
|
||||||
enable_advertising(&req);
|
enable_advertising(&req);
|
||||||
}
|
|
||||||
|
|
||||||
err = hci_req_run(&req, set_connectable_complete);
|
err = hci_req_run(&req, set_connectable_complete);
|
||||||
if (err < 0) {
|
if (err < 0) {
|
||||||
@ -6809,32 +6808,16 @@ void mgmt_discovering(struct hci_dev *hdev, u8 discovering)
|
|||||||
static void adv_enable_complete(struct hci_dev *hdev, u8 status)
|
static void adv_enable_complete(struct hci_dev *hdev, u8 status)
|
||||||
{
|
{
|
||||||
BT_DBG("%s status %u", hdev->name, status);
|
BT_DBG("%s status %u", hdev->name, status);
|
||||||
|
|
||||||
/* Clear the advertising mgmt setting if we failed to re-enable it */
|
|
||||||
if (status) {
|
|
||||||
clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
|
|
||||||
new_settings(hdev, NULL);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void mgmt_reenable_advertising(struct hci_dev *hdev)
|
void mgmt_reenable_advertising(struct hci_dev *hdev)
|
||||||
{
|
{
|
||||||
struct hci_request req;
|
struct hci_request req;
|
||||||
|
|
||||||
if (hci_conn_num(hdev, LE_LINK) > 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags))
|
if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
hci_req_init(&req, hdev);
|
hci_req_init(&req, hdev);
|
||||||
enable_advertising(&req);
|
enable_advertising(&req);
|
||||||
|
hci_req_run(&req, adv_enable_complete);
|
||||||
/* If this fails we have no option but to let user space know
|
|
||||||
* that we've disabled advertising.
|
|
||||||
*/
|
|
||||||
if (hci_req_run(&req, adv_enable_complete) < 0) {
|
|
||||||
clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
|
|
||||||
new_settings(hdev, NULL);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user