Skip to content

Commit

Permalink
feat(nimble): Add support to allow connection during scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
rahult-github committed Sep 4, 2024
1 parent 4af7646 commit 92e4100
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 31 deletions.
6 changes: 6 additions & 0 deletions components/bt/host/nimble/Kconfig.in
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,12 @@ config BT_NIMBLE_HIGH_DUTY_ADV_ITVL
help
This enable BLE high duty advertising interval feature

config BT_NIMBLE_HOST_ALLOW_CONNECT_WITH_SCAN
bool "Allow Connections with scanning in progress"
depends on BT_NIMBLE_ENABLED
help
This enables support for user to initiate a new connection with scan in progress

config BT_NIMBLE_HOST_QUEUE_CONG_CHECK
bool "BLE queue congestion check"
depends on BT_NIMBLE_ENABLED
Expand Down
2 changes: 1 addition & 1 deletion components/bt/host/nimble/nimble
8 changes: 8 additions & 0 deletions components/bt/host/nimble/port/include/esp_nimble_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,14 @@
#endif
#endif

#ifndef MYNEWT_VAL_BLE_HOST_ALLOW_CONNECT_WITH_SCAN
#ifdef CONFIG_BT_NIMBLE_HOST_ALLOW_CONNECT_WITH_SCAN
#define MYNEWT_VAL_BLE_HOST_ALLOW_CONNECT_WITH_SCAN CONFIG_BT_NIMBLE_HOST_ALLOW_CONNECT_WITH_SCAN
#else
#define MYNEWT_VAL_BLE_HOST_ALLOW_CONNECT_WITH_SCAN (0)
#endif
#endif

#if CONFIG_BT_CONTROLLER_DISABLED && CONFIG_BT_NIMBLE_TRANSPORT_UART
#ifndef MYNEWT_VAL_BLE_TRANSPORT_UART_PORT
#define MYNEWT_VAL_BLE_TRANSPORT_UART_PORT CONFIG_BT_NIMBLE_TRANSPORT_UART_PORT
Expand Down
8 changes: 5 additions & 3 deletions examples/bluetooth/nimble/ble_cts/cts_cent/main/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -303,12 +303,14 @@ ble_cts_cent_connect_if_interesting(void *disc)
}
#endif

#if !(MYNEWT_VAL(BLE_HOST_ALLOW_CONNECT_WITH_SCAN))
/* Scanning must be stopped before a connection can be initiated. */
rc = ble_gap_disc_cancel();
if (rc != 0) {
MODLOG_DFLT(DEBUG, "Failed to cancel scan; rc=%d\n", rc);
return;
}
#endif

/* Figure out address to use for connect (no privacy for now) */
rc = ble_hs_id_infer_auto(0, &own_addr_type);
Expand Down Expand Up @@ -364,7 +366,7 @@ ble_cts_cent_gap_event(struct ble_gap_event *event, void *arg)
return 0;
}

/* An advertisment report was received during GAP discovery. */
/* An advertisement report was received during GAP discovery. */
print_adv_fields(&fields);

/* Try to connect to the advertiser if it looks interesting. */
Expand Down Expand Up @@ -482,7 +484,7 @@ ble_cts_cent_gap_event(struct ble_gap_event *event, void *arg)

#if CONFIG_EXAMPLE_EXTENDED_ADV
case BLE_GAP_EVENT_EXT_DISC:
/* An advertisment report was received during GAP discovery. */
/* An advertisement report was received during GAP discovery. */
ext_print_adv_report(&event->disc);

ble_cts_cent_connect_if_interesting(&event->disc);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
Expand Down Expand Up @@ -358,12 +358,14 @@ enc_adv_data_cent_connect_if_interesting(void *disc)
return;
}

#if !(MYNEWT_VAL(BLE_HOST_ALLOW_CONNECT_WITH_SCAN))
/* Scanning must be stopped before a connection can be initiated. */
rc = ble_gap_disc_cancel();
if (rc != 0) {
MODLOG_DFLT(DEBUG, "Failed to cancel scan; rc=%d\n", rc);
return;
}
#endif

/* Figure out address to use for connect (no privacy for now) */
rc = ble_hs_id_infer_auto(0, &own_addr_type);
Expand Down Expand Up @@ -416,7 +418,7 @@ enc_adv_data_cent_gap_event(struct ble_gap_event *event, void *arg)
return 0;
}

/* An advertisment report was received during GAP discovery. */
/* An advertisement report was received during GAP discovery. */
print_adv_fields(&fields);

/* Try to connect to the advertiser if it looks interesting. */
Expand Down Expand Up @@ -514,7 +516,7 @@ enc_adv_data_cent_gap_event(struct ble_gap_event *event, void *arg)
return 0;

case BLE_GAP_EVENT_EXT_DISC:
/* An advertisment report was received during GAP discovery. */
/* An advertisement report was received during GAP discovery. */
ext_print_adv_report(&event->disc);

enc_adv_data_cent_connect_if_interesting(&event->disc);
Expand Down
14 changes: 8 additions & 6 deletions examples/bluetooth/nimble/ble_htp/htp_cent/main/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -189,7 +189,7 @@ ble_htp_cent_on_read(uint16_t conn_handle,

/**
* Performs three GATT operations against the specified peer:
* 1. Reads the HTP temparature type characteristic.
* 1. Reads the HTP temperature type characteristic.
* 2. After read is completed, writes the HTP temperature measurement interval characteristic.
* 3. After write is completed, subscribes to notifications for the HTP intermediate temperature
* and temperature measurement characteristic.
Expand All @@ -205,12 +205,12 @@ ble_htp_cent_read_write_subscribe(const struct peer *peer)
const struct peer_chr *chr;
int rc;

/* Read the Temparature Type characteristic. */
/* Read the Temperature Type characteristic. */
chr = peer_chr_find_uuid(peer,
BLE_UUID16_DECLARE(BLE_SVC_HTP_UUID16),
BLE_UUID16_DECLARE(BLE_SVC_HTP_CHR_UUID16_TEMP_TYPE));
if (chr == NULL) {
MODLOG_DFLT(ERROR, "Error: Peer doesn't support the Temparature Type"
MODLOG_DFLT(ERROR, "Error: Peer doesn't support the Temperature Type"
" characteristic\n");
goto err;
}
Expand Down Expand Up @@ -418,12 +418,14 @@ ble_htp_cent_connect_if_interesting(void *disc)
}
#endif

#if !(MYNEWT_VAL(BLE_HOST_ALLOW_CONNECT_WITH_SCAN))
/* Scanning must be stopped before a connection can be initiated. */
rc = ble_gap_disc_cancel();
if (rc != 0) {
MODLOG_DFLT(DEBUG, "Failed to cancel scan; rc=%d\n", rc);
return;
}
#endif

/* Figure out address to use for connect (no privacy for now) */
rc = ble_hs_id_infer_auto(0, &own_addr_type);
Expand Down Expand Up @@ -479,7 +481,7 @@ ble_htp_cent_gap_event(struct ble_gap_event *event, void *arg)
return 0;
}

/* An advertisment report was received during GAP discovery. */
/* An advertisement report was received during GAP discovery. */
print_adv_fields(&fields);

/* Try to connect to the advertiser if it looks interesting. */
Expand Down Expand Up @@ -613,7 +615,7 @@ ble_htp_cent_gap_event(struct ble_gap_event *event, void *arg)

#if CONFIG_EXAMPLE_EXTENDED_ADV
case BLE_GAP_EVENT_EXT_DISC:
/* An advertisment report was received during GAP discovery. */
/* An advertisement report was received during GAP discovery. */
ext_print_adv_report(&event->disc);

ble_htp_cent_connect_if_interesting(&event->disc);
Expand Down
10 changes: 6 additions & 4 deletions examples/bluetooth/nimble/ble_l2cap_coc/coc_blecent/main/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
Expand Down Expand Up @@ -77,7 +77,7 @@ blecent_l2cap_coc_send_data(struct ble_l2cap_chan *chan)
}

/**
* After connetion is established on GAP layer, service discovery is performed. On
* After connection is established on GAP layer, service discovery is performed. On
* it's completion, this API is called for making a connection is on L2CAP layer.
*/
static void
Expand Down Expand Up @@ -340,12 +340,14 @@ blecent_connect_if_interesting(void *disc)
}
#endif

#if !(MYNEWT_VAL(BLE_HOST_ALLOW_CONNECT_WITH_SCAN))
/* Scanning must be stopped before a connection can be initiated. */
rc = ble_gap_disc_cancel();
if (rc != 0) {
MODLOG_DFLT(DEBUG, "Failed to cancel scan; rc=%d\n", rc);
return;
}
#endif

/* Figure out address to use for connect (no privacy for now) */
rc = ble_hs_id_infer_auto(0, &own_addr_type);
Expand Down Expand Up @@ -403,7 +405,7 @@ blecent_gap_event(struct ble_gap_event *event, void *arg)
return 0;
}

/* An advertisment report was received during GAP discovery. */
/* An advertisement report was received during GAP discovery. */
print_adv_fields(&fields);

/* Try to connect to the advertiser if it looks interesting. */
Expand Down Expand Up @@ -470,7 +472,7 @@ blecent_gap_event(struct ble_gap_event *event, void *arg)

#if CONFIG_EXAMPLE_EXTENDED_ADV
case BLE_GAP_EVENT_EXT_DISC:
/* An advertisment report was received during GAP discovery. */
/* An advertisement report was received during GAP discovery. */
ext_print_adv_report(&event->disc);

blecent_connect_if_interesting(&event->disc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ ble_cent_client_gap_event(struct ble_gap_event *event, void *arg)
case BLE_GAP_EVENT_EXT_DISC:
rc = ble_hs_adv_parse_fields(&fields, event->ext_disc.data, event->ext_disc.length_data);

/* An advertisment report was received during GAP discovery. */
/* An advertisement report was received during GAP discovery. */
if ((rc == 0) && fields.name && (fields.name_len >= strlen(BLE_PEER_NAME)) &&
!strncmp((const char *)fields.name, BLE_PEER_NAME, strlen(BLE_PEER_NAME))) {
ble_cent_connect(&event->ext_disc);
Expand Down Expand Up @@ -319,12 +319,14 @@ ble_cent_connect(void *disc)
return;
}

#if !(MYNEWT_VAL(BLE_HOST_ALLOW_CONNECT_WITH_SCAN))
/* Scanning must be stopped before a connection can be initiated. */
rc = ble_gap_disc_cancel();
if (rc != 0) {
ESP_LOGE(TAG, "Failed to cancel scan; rc=%d\n", rc);
return;
}
#endif

/* We won't connect to the same device. Change our static random address to simulate
* multi-connection with only one central and one peripheral.
Expand Down
4 changes: 3 additions & 1 deletion examples/bluetooth/nimble/ble_phy/phy_cent/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,14 @@ blecent_connect_if_interesting(void *disc)
return;
}

#if !(MYNEWT_VAL(BLE_HOST_ALLOW_CONNECT_WITH_SCAN))
/* Scanning must be stopped before a connection can be initiated. */
rc = ble_gap_disc_cancel();
if (rc != 0) {
MODLOG_DFLT(DEBUG, "Failed to cancel scan; rc=%d\n", rc);
return;
}
#endif

/* Figure out address to use for connect (no privacy for now) */
rc = ble_hs_id_infer_auto(0, &own_addr_type);
Expand Down Expand Up @@ -438,7 +440,7 @@ blecent_gap_event(struct ble_gap_event *event, void *arg)
return 0;

case BLE_GAP_EVENT_EXT_DISC:
/* An advertisment report was received during GAP discovery. */
/* An advertisement report was received during GAP discovery. */
ext_print_adv_report(&event->disc);

blecent_connect_if_interesting(&event->disc);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -25,7 +25,7 @@ static struct ble_prox_cent_link_lost_peer disconn_peer[MYNEWT_VAL(BLE_MAX_CONNE
/* Note: Path loss is calculated using formula : threshold - RSSI value
* by default threshold is kept -128 as per the spec
* high_threshold and low_threshold are hardcoded after testing and noting
* RSSI values when distance betweeen devices are less and more.
* RSSI values when distance between devices are less and more.
*/
static int8_t high_threshold = -70;
static int8_t low_threshold = -100;
Expand Down Expand Up @@ -335,12 +335,14 @@ ble_prox_cent_connect_if_interesting(void *disc)
}
#endif

#if !(MYNEWT_VAL(BLE_HOST_ALLOW_CONNECT_WITH_SCAN))
/* Scanning must be stopped before a connection can be initiated. */
rc = ble_gap_disc_cancel();
if (rc != 0) {
MODLOG_DFLT(DEBUG, "Failed to cancel scan; rc=%d\n", rc);
return;
}
#endif

/* Figure out address to use for connect (no privacy for now) */
rc = ble_hs_id_infer_auto(0, &own_addr_type);
Expand Down Expand Up @@ -396,7 +398,7 @@ ble_prox_cent_gap_event(struct ble_gap_event *event, void *arg)
return 0;
}

/* An advertisment report was received during GAP discovery. */
/* An advertisement report was received during GAP discovery. */
print_adv_fields(&fields);

/* Try to connect to the advertiser if it looks interesting. */
Expand Down Expand Up @@ -561,7 +563,7 @@ ble_prox_cent_gap_event(struct ble_gap_event *event, void *arg)

#if CONFIG_EXAMPLE_EXTENDED_ADV
case BLE_GAP_EVENT_EXT_DISC:
/* An advertisment report was received during GAP discovery. */
/* An advertisement report was received during GAP discovery. */
ext_print_adv_report(&event->disc);

ble_prox_cent_connect_if_interesting(&event->disc);
Expand Down
8 changes: 5 additions & 3 deletions examples/bluetooth/nimble/ble_spp/spp_client/main/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
Expand Down Expand Up @@ -164,12 +164,14 @@ ble_spp_client_connect_if_interesting(const struct ble_gap_disc_desc *disc)
return;
}

#if !(MYNEWT_VAL(BLE_HOST_ALLOW_CONNECT_WITH_SCAN))
/* Scanning must be stopped before a connection can be initiated. */
rc = ble_gap_disc_cancel();
if (rc != 0) {
MODLOG_DFLT(DEBUG, "Failed to cancel scan; rc=%d\n", rc);
return;
}
#endif

/* Figure out address to use for connect (no privacy for now) */
rc = ble_hs_id_infer_auto(0, &own_addr_type);
Expand Down Expand Up @@ -221,7 +223,7 @@ ble_spp_client_gap_event(struct ble_gap_event *event, void *arg)
return 0;
}

/* An advertisment report was received during GAP discovery. */
/* An advertisement report was received during GAP discovery. */
print_adv_fields(&fields);

/* Try to connect to the advertiser if it looks interesting. */
Expand Down Expand Up @@ -347,7 +349,7 @@ void ble_client_uart_task(void *pvParameters)
//Waiting for UART event.
if (xQueueReceive(spp_common_uart_queue, (void * )&event, (TickType_t)portMAX_DELAY)) {
switch (event.type) {
//Event of UART receving data
//Event of UART receiving data
case UART_DATA:
if (event.size) {

Expand Down
Loading

0 comments on commit 92e4100

Please sign in to comment.