Merge branch 'bugfix/update_ble_phy_example' into 'master'

fix(nimble): Updated BLE Phy example

Closes BLERP-17

See merge request espressif/esp-idf!25908
pull/12276/head
Rahul Tank 2023-09-18 15:35:04 +08:00
commit cd2ddd86a7
6 zmienionych plików z 342 dodań i 430 usunięć

Wyświetl plik

@ -59,114 +59,159 @@ See the [Getting Started Guide](https://idf.espressif.com/) for full steps to co
This is the console output on successful connection:
```
I (315) BTDM_INIT: BT controller compile version [05195c9]
I (315) phy_init: phy_version 912,d001756,Jun 2 2022,16:28:07
I (355) system_api: Base MAC address is not set
I (355) system_api: read default base MAC address from EFUSE
I (355) BTDM_INIT: Bluetooth MAC: 84:f7:03:08:4d:8e
I (355) NimBLE_BLE_PHY_CENT: BLE Host Task Started
I (465) NimBLE: Connection established
I (465) NimBLE:
I (465) NimBLE: Prefered LE PHY set to LE_PHY_2M successfully
I (465) NimBLE: GATT procedure initiated: discover all services
I (565) NimBLE: GATT procedure initiated: discover all characteristics;
I (565) NimBLE: start_handle=1 end_handle=5
I (765) NimBLE: GATT procedure initiated: discover all characteristics;
I (765) NimBLE: start_handle=6 end_handle=9
I (965) NimBLE: GATT procedure initiated: discover all characteristics;
I (965) NimBLE: start_handle=10 end_handle=65535
I (1015) NimBLE: LE PHY Update completed; status=0 conn_handle=1 tx_phy=2 rx_phy = 2
I (1165) NimBLE: GATT procedure initiated: discover all descriptors;
I (1165) NimBLE: chr_val_handle=8 end_handle=9
I (1265) NimBLE: GATT procedure initiated: discover all descriptors;
I (1265) NimBLE: chr_val_handle=12 end_handle=65535
I (1365) NimBLE: Service discovery complete; status=0 conn_handle=1
I (1365) NimBLE: GATT procedure initiated: read;
I (1365) NimBLE: att_handle=12
I (1375) NimBLE: GAP procedure initiated: terminate connection; conn_handle=1 hci_reason=19
I (1415) NimBLE: disconnect; reason=534
I (1415) NimBLE:
I (1415) NimBLE: Default LE PHY set successfully; tx_phy = 2, rx_phy = 2
I (1505) NimBLE: Connection established
I (1505) NimBLE:
I (1505) NimBLE: GATT procedure initiated: discover all services
I (1615) NimBLE: GATT procedure initiated: discover all characteristics;
I (1615) NimBLE: start_handle=1 end_handle=5
I (1815) NimBLE: GATT procedure initiated: discover all characteristics;
I (1815) NimBLE: start_handle=6 end_handle=9
I (2015) NimBLE: LE PHY Update completed; status=0 conn_handle=1 tx_phy=2 rx_phy = 2
I (2015) NimBLE: GATT procedure initiated: discover all characteristics;
I (2025) NimBLE: start_handle=10 end_handle=65535
I (2215) NimBLE: GATT procedure initiated: discover all descriptors;
I (2215) NimBLE: chr_val_handle=8 end_handle=9
I (2315) NimBLE: GATT procedure initiated: discover all descriptors;
I (2315) NimBLE: chr_val_handle=12 end_handle=65535
I (2415) NimBLE: Service discovery complete; status=0 conn_handle=1
I (2415) NimBLE: GATT procedure initiated: read;
I (2415) NimBLE: att_handle=12
I (2425) NimBLE: GAP procedure initiated: terminate connection; conn_handle=1 hci_reason=19
I (2465) NimBLE: disconnect; reason=534
I (2465) NimBLE:
I (2465) NimBLE: Default LE PHY set successfully; tx_phy = 4, rx_phy = 4
I (2555) NimBLE: Connection established
I (2555) NimBLE:
I (2555) NimBLE: GATT procedure initiated: discover all services
I (2665) NimBLE: GATT procedure initiated: discover all characteristics;
I (2665) NimBLE: start_handle=1 end_handle=5
I (2865) NimBLE: GATT procedure initiated: discover all characteristics;
I (2865) NimBLE: start_handle=6 end_handle=9
I (3065) NimBLE: LE PHY Update completed; status=0 conn_handle=1 tx_phy=3 rx_phy = 3
I (3065) NimBLE: GATT procedure initiated: discover all characteristics;
I (3075) NimBLE: start_handle=10 end_handle=65535
I (3265) NimBLE: GATT procedure initiated: discover all descriptors;
I (3265) NimBLE: chr_val_handle=8 end_handle=9
I (3365) NimBLE: GATT procedure initiated: discover all descriptors;
I (3365) NimBLE: chr_val_handle=12 end_handle=65535
I (3465) NimBLE: Service discovery complete; status=0 conn_handle=1
I (3465) NimBLE: GATT procedure initiated: read;
I (3465) NimBLE: att_handle=12
I (3475) NimBLE: GAP procedure initiated: terminate connection; conn_handle=1 hci_reason=19
I (3515) NimBLE: disconnect; reason=534
I (3515) NimBLE:
```
I (456) NimBLE_BLE_PHY_CENT: BLE Host Task Started
I (456) NimBLE: Default LE PHY set successfully; tx_phy = 7, rx_phy = 7
I (456) NimBLE: GAP procedure initiated: extended discovery;
I (466) main_task: Returned from app_main()
I (6316) NimBLE: Attempting to connect to : 60 55 f9 f7 3e 23 with type 0
I (6316) NimBLE: GAP procedure initiated: extended connect;
I (6386) NimBLE: Connection established 1M PhY
I (6386) NimBLE:
I (6386) NimBLE: GATT procedure initiated: discover all services
I (6446) NimBLE: GATT procedure initiated: discover all characteristics;
I (6446) NimBLE: start_handle=1 end_handle=5
I (6636) NimBLE: GATT procedure initiated: discover all characteristics;
I (6636) NimBLE: start_handle=6 end_handle=9
I (6836) NimBLE: GATT procedure initiated: discover all characteristics;
I (6836) NimBLE: start_handle=10 end_handle=65535
I (7036) NimBLE: GATT procedure initiated: discover all descriptors;
I (7036) NimBLE: chr_val_handle=8 end_handle=9
I (7136) NimBLE: GATT procedure initiated: discover all descriptors;
I (7136) NimBLE: chr_val_handle=12 end_handle=65535
I (7236) NimBLE: Service discovery complete; status=0 conn_handle=1
I (7236) NimBLE: GATT procedure initiated: read;
I (7236) NimBLE: att_handle=12
I (7336) NimBLE: Read complete; status=261 conn_handle=1
I (7336) NimBLE:
I (7336) NimBLE: GATT procedure initiated: write long;
I (7346) NimBLE: att_handle=12 len=1000
I (7436) NimBLE: Write complete; status=261 conn_handle=1
I (7436) NimBLE: GAP procedure initiated: terminate connection; conn_handle=1 hci_reason=19
I (7446) NimBLE:
I (7486) NimBLE: disconnect; reason=534
I (7486) NimBLE:
I (7486) NimBLE: Attempting to initiate connection on 2M PHY
I (7496) NimBLE: GAP procedure initiated: extended connect;
I (7596) NimBLE: Connection established on 2M Phy
I (7596) NimBLE:
I (7596) NimBLE: GATT procedure initiated: discover all services
I (7696) NimBLE: GATT procedure initiated: discover all characteristics;
I (7706) NimBLE: start_handle=1 end_handle=5
I (7896) NimBLE: GATT procedure initiated: discover all characteristics;
I (7896) NimBLE: start_handle=6 end_handle=9
I (8096) NimBLE: GATT procedure initiated: discover all characteristics;
I (8096) NimBLE: start_handle=10 end_handle=65535
I (8296) NimBLE: GATT procedure initiated: discover all descriptors;
I (8296) NimBLE: chr_val_handle=8 end_handle=9
I (8396) NimBLE: GATT procedure initiated: discover all descriptors;
I (8396) NimBLE: chr_val_handle=12 end_handle=65535
I (8496) NimBLE: Service discovery complete; status=0 conn_handle=1
I (8496) NimBLE: GATT procedure initiated: read;
I (8506) NimBLE: att_handle=12
I (8596) NimBLE: Read complete; status=261 conn_handle=1
I (8596) NimBLE:
I (8606) NimBLE: GATT procedure initiated: write long;
I (8606) NimBLE: att_handle=12 len=1000
I (8696) NimBLE: Write complete; status=261 conn_handle=1
I (8696) NimBLE: GAP procedure initiated: terminate connection; conn_handle=1 hci_reason=19
I (8706) NimBLE:
I (8746) NimBLE: disconnect; reason=534
I (8746) NimBLE:
I (8756) NimBLE: Attempting to initiate connection on Coded PHY
I (8756) NimBLE: GAP procedure initiated: extended connect;
I (8876) NimBLE: Connection established on Coded Phy
I (8876) NimBLE:
I (8876) NimBLE: GATT procedure initiated: discover all services
I (8976) NimBLE: GATT procedure initiated: discover all characteristics;
I (8976) NimBLE: start_handle=1 end_handle=5
I (9176) NimBLE: GATT procedure initiated: discover all characteristics;
I (9176) NimBLE: start_handle=6 end_handle=9
I (9426) NimBLE: GATT procedure initiated: discover all characteristics;
I (9426) NimBLE: start_handle=10 end_handle=65535
I (9626) NimBLE: GATT procedure initiated: discover all descriptors;
I (9626) NimBLE: chr_val_handle=8 end_handle=9
I (9776) NimBLE: GATT procedure initiated: discover all descriptors;
I (9776) NimBLE: chr_val_handle=12 end_handle=65535
I (9876) NimBLE: Service discovery complete; status=0 conn_handle=1
I (9876) NimBLE: GATT procedure initiated: read;
I (9876) NimBLE: att_handle=12
I (9976) NimBLE: Read complete; status=261 conn_handle=1
I (9976) NimBLE:
I (9976) NimBLE: GATT procedure initiated: write long;
I (9976) NimBLE: att_handle=12 len=1000
I (10076) NimBLE: Write complete; status=261 conn_handle=1
I (10076) NimBLE: GAP procedure initiated: terminate connection; conn_handle=1 hci_reason=19
I (10076) NimBLE:
I (10126) NimBLE: disconnect; reason=534
I (10126) NimBLE:
```
## Intiating connection on 2M / Coded Phy
This example also provides a way to initiate connection directly on 2M / Coded PHY. For this, set the peripheral device address in EXAMPLE_PEER_ADDR setting via menuconfig. Also example needs to be modified to use the PHY that user needs to update.
In main.c file, in blecent_on_sync function, set the s_current_phy to the intended phy.
To create direct connection on 2M, set the s_current_phy to BLE_HCI_LE_PHY_2M_PREF_MASK.
To create direct connection on Coded Phy, set the s_current_phy to BLE_HCI_LE_PHY_CODED_PREF_MASK
Recompile application and flash the image.
Note: For this setting to work ,the remote device too should be advertising with either 2M auxillary phy OR Coded phy.
Also, since this mode initiates direct connection, ensure that peripheral is already advertising, before executing this application.
## Troubleshooting

Wyświetl plik

@ -6,14 +6,4 @@ menu "Example Configuration"
help
Enter the peer address in aa:bb:cc:dd:ee:ff form to connect to a specific peripheral
config EXAMPLE_EXTENDED_ADV
bool
depends on SOC_BLE_50_SUPPORTED
default y if SOC_ESP_NIMBLE_CONTROLLER
select BT_NIMBLE_EXT_ADV
prompt "Enable Extended Adv"
help
Use this option to enable extended advertising in the example.
If this option is disabled, ensure config BT_NIMBLE_EXT_ADV is
also disabled from Nimble stack menuconfig
endmenu

Wyświetl plik

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
@ -18,6 +18,8 @@
static const char *tag = "NimBLE_BLE_PHY_CENT";
static int blecent_gap_event(struct ble_gap_event *event, void *arg);
static uint8_t peer_addr[6];
static ble_addr_t conn_addr;
static void blecent_scan(void);
static uint8_t s_current_phy;
@ -45,6 +47,9 @@ blecent_on_write(uint16_t conn_handle,
MODLOG_DFLT(INFO, " attr_handle=%d", attr->handle);
}
/* Terminate the connection once GATT procedure is completed */
ble_gap_terminate(conn_handle, BLE_ERR_REM_USER_CONN_TERM);
MODLOG_DFLT(INFO, "\n");
return 0;
}
@ -162,29 +167,10 @@ blecent_on_disc_complete(const struct peer *peer, int status, void *arg)
*/
blecent_read(peer);
/* Terminate the connection once GATT procedure is completed */
ble_gap_terminate(peer->conn_handle, BLE_ERR_REM_USER_CONN_TERM);
}
/* Set preferred LE PHY after connection is established */
void set_prefered_le_phy_after_conn(uint16_t conn_handle)
{
uint8_t tx_phys_mask = 0, rx_phys_mask = 0;
tx_phys_mask = BLE_HCI_LE_PHY_2M_PREF_MASK;
rx_phys_mask = BLE_HCI_LE_PHY_2M_PREF_MASK;
int rc = ble_gap_set_prefered_le_phy(conn_handle, tx_phys_mask, rx_phys_mask, 0);
if (rc == 0) {
MODLOG_DFLT(INFO, "Prefered LE PHY set to LE_PHY_2M successfully");
} else {
MODLOG_DFLT(ERROR, "Failed to set prefered LE_PHY_2M");
}
}
/* Set default LE PHY before establishing connection */
void set_default_le_phy_before_conn(uint8_t tx_phys_mask, uint8_t rx_phys_mask)
void set_default_le_phy(uint8_t tx_phys_mask, uint8_t rx_phys_mask)
{
int rc = ble_gap_set_prefered_default_le_phy(tx_phys_mask, rx_phys_mask);
if (rc == 0) {
@ -242,7 +228,6 @@ blecent_scan(void)
* advertisement. The function returns a positive result if the device
* advertises connectability and support for the LE PHY service.
*/
#if CONFIG_EXAMPLE_EXTENDED_ADV
static int
ext_blecent_should_connect(const struct ble_gap_ext_disc_desc *disc)
{
@ -253,7 +238,7 @@ ext_blecent_should_connect(const struct ble_gap_ext_disc_desc *disc)
disc->legacy_event_type != BLE_HCI_ADV_RPT_EVTYPE_DIR_IND) {
return 0;
}
if (strlen(CONFIG_EXAMPLE_PEER_ADDR) && (strncmp(CONFIG_EXAMPLE_PEER_ADDR, "ADDR_ANY", strlen ("ADDR_ANY")) != 0)) {
if (strlen(CONFIG_EXAMPLE_PEER_ADDR) && (strncmp(CONFIG_EXAMPLE_PEER_ADDR, "ADDR_ANY", strlen("ADDR_ANY")) != 0)) {
ESP_LOGI(tag, "Peer address from menuconfig: %s", CONFIG_EXAMPLE_PEER_ADDR);
/* Convert string to address */
sscanf(CONFIG_EXAMPLE_PEER_ADDR, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
@ -285,47 +270,6 @@ ext_blecent_should_connect(const struct ble_gap_ext_disc_desc *disc)
} while ( offset < disc->length_data );
return 0;
}
#else
static int
blecent_should_connect(const struct ble_gap_disc_desc *disc)
{
struct ble_hs_adv_fields fields;
int rc;
int i;
/* The device has to be advertising connectability. */
if (disc->event_type != BLE_HCI_ADV_RPT_EVTYPE_ADV_IND &&
disc->event_type != BLE_HCI_ADV_RPT_EVTYPE_DIR_IND) {
return 0;
}
rc = ble_hs_adv_parse_fields(&fields, disc->data, disc->length_data);
if (rc != 0) {
return 0;
}
if (strlen(CONFIG_EXAMPLE_PEER_ADDR) && (strncmp(CONFIG_EXAMPLE_PEER_ADDR, "ADDR_ANY", strlen("ADDR_ANY")) != 0)) {
ESP_LOGI(tag, "Peer address from menuconfig: %s", CONFIG_EXAMPLE_PEER_ADDR);
/* Convert string to address */
sscanf(CONFIG_EXAMPLE_PEER_ADDR, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
&peer_addr[5], &peer_addr[4], &peer_addr[3],
&peer_addr[2], &peer_addr[1], &peer_addr[0]);
if (memcmp(peer_addr, disc->addr.val, sizeof(disc->addr.val)) != 0) {
return 0;
}
}
/* The device has to advertise support for LE PHY UUID (0xABF2)
*/
for (i = 0; i < fields.num_uuids16; i++) {
if (ble_uuid_u16(&fields.uuids16[i].u) == LE_PHY_UUID16) {
return 1;
}
}
return 0;
}
#endif
/**
* Connects to the sender of the specified advertisement of it looks
@ -340,15 +284,9 @@ blecent_connect_if_interesting(void *disc)
ble_addr_t *addr;
/* Don't do anything if we don't care about this advertiser. */
#if CONFIG_EXAMPLE_EXTENDED_ADV
if (!ext_blecent_should_connect((struct ble_gap_ext_disc_desc *)disc)) {
return;
}
#else
if (!blecent_should_connect((struct ble_gap_disc_desc *)disc)) {
return;
}
#endif
/* Scanning must be stopped before a connection can be initiated. */
rc = ble_gap_disc_cancel();
@ -364,21 +302,24 @@ blecent_connect_if_interesting(void *disc)
return;
}
addr = &((struct ble_gap_ext_disc_desc *)disc)->addr;
/* Copy addr information for next connection */
memcpy(&conn_addr, addr, sizeof(conn_addr));
MODLOG_DFLT(INFO, "Attempting to connect to : %x %x %x %x %x %x with type %d \n",
conn_addr.val[5],conn_addr.val[4], conn_addr.val[3], conn_addr.val[2], conn_addr.val[1], conn_addr.val[0],
conn_addr.type);
/* Try to connect the the advertiser. Allow 30 seconds (30000 ms) for
* timeout.
*/
#if CONFIG_EXAMPLE_EXTENDED_ADV
addr = &((struct ble_gap_ext_disc_desc *)disc)->addr;
#else
addr = &((struct ble_gap_disc_desc *)disc)->addr;
#endif
rc = ble_gap_connect(own_addr_type, addr, 30000, NULL,
rc = ble_gap_connect(own_addr_type, &conn_addr, 30000, NULL,
blecent_gap_event, NULL);
if (rc != 0) {
MODLOG_DFLT(ERROR, "Error: Failed to connect to device; addr_type=%d "
"addr=%s; rc=%d\n",
addr->type, addr_str(addr->val), rc);
conn_addr.type, addr_str(conn_addr.val), rc);
return;
}
}
@ -401,29 +342,28 @@ static int
blecent_gap_event(struct ble_gap_event *event, void *arg)
{
struct ble_gap_conn_desc desc;
struct ble_hs_adv_fields fields;
int rc;
switch (event->type) {
case BLE_GAP_EVENT_DISC:
rc = ble_hs_adv_parse_fields(&fields, event->disc.data,
event->disc.length_data);
if (rc != 0) {
return 0;
}
/* An advertisment report was received during GAP discovery. */
print_adv_fields(&fields);
/* Try to connect to the advertiser if it looks interesting. */
blecent_connect_if_interesting(&event->disc);
return 0;
case BLE_GAP_EVENT_CONNECT:
/* A new connection was established or a connection attempt failed. */
if (event->connect.status == 0) {
/* Connection successfully established. */
MODLOG_DFLT(INFO, "Connection established ");
switch (s_current_phy) {
case BLE_HCI_LE_PHY_1M_PREF_MASK:
MODLOG_DFLT(INFO,"Connection established on 1M Phy");
break;
case BLE_HCI_LE_PHY_2M_PREF_MASK:
case BLE_HCI_LE_PHY_1M_PREF_MASK | BLE_HCI_LE_PHY_2M_PREF_MASK:
MODLOG_DFLT(INFO,"Connection established on 2M Phy");
break;
case BLE_HCI_LE_PHY_CODED_PREF_MASK:
MODLOG_DFLT(INFO,"Connection established on Coded Phy");
break;
}
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
assert(rc == 0);
@ -437,10 +377,6 @@ blecent_gap_event(struct ble_gap_event *event, void *arg)
return 0;
}
if (s_current_phy == BLE_HCI_LE_PHY_1M_PREF_MASK) {
/* Update LE PHY from 1M to 2M */
set_prefered_le_phy_after_conn(event->connect.conn_handle);
}
/* Perform service discovery. */
rc = peer_disc_all(event->connect.conn_handle,
blecent_on_disc_complete, NULL);
@ -480,8 +416,20 @@ blecent_gap_event(struct ble_gap_event *event, void *arg)
case BLE_HCI_LE_PHY_CODED_PREF_MASK:
return 0;
}
set_default_le_phy_before_conn(s_current_phy, s_current_phy);
blecent_scan();
vTaskDelay(200);
/* Attempt direct connection on 2M or Coded phy now */
if (s_current_phy == BLE_HCI_LE_PHY_CODED_PREF_MASK) {
MODLOG_DFLT(INFO, " Attempting to initiate connection on Coded PHY \n");
ble_gap_ext_connect(0, &conn_addr, 30000, BLE_HCI_LE_PHY_CODED_PREF_MASK,
NULL, NULL, NULL, blecent_gap_event, NULL);
}
else if (s_current_phy == BLE_HCI_LE_PHY_2M_PREF_MASK) {
MODLOG_DFLT(INFO, " Attempting to initiate connection on 2M PHY \n");
ble_gap_ext_connect(0, &conn_addr, 30000, (BLE_HCI_LE_PHY_1M_PREF_MASK | BLE_HCI_LE_PHY_2M_PREF_MASK),
NULL, NULL, NULL, blecent_gap_event, NULL);
}
return 0;
case BLE_GAP_EVENT_DISC_COMPLETE:
@ -489,21 +437,12 @@ blecent_gap_event(struct ble_gap_event *event, void *arg)
event->disc_complete.reason);
return 0;
#if CONFIG_EXAMPLE_EXTENDED_ADV
case BLE_GAP_EVENT_EXT_DISC:
/* An advertisment report was received during GAP discovery. */
ext_print_adv_report(&event->disc);
blecent_connect_if_interesting(&event->disc);
return 0;
#endif
case BLE_GAP_EVENT_PHY_UPDATE_COMPLETE:
MODLOG_DFLT(INFO, "LE PHY Update completed; status=%d conn_handle=%d tx_phy=%d "
"rx_phy = %d\n", event->phy_updated.status,
event->phy_updated.conn_handle, event->phy_updated.tx_phy,
event->phy_updated.rx_phy);
return 0;
default:
return 0;
@ -519,15 +458,46 @@ blecent_on_reset(int reason)
static void
blecent_on_sync(void)
{
int rc;
int ii, rc;
uint8_t all_phy;
/* Make sure we have proper identity address set (public preferred) */
rc = ble_hs_util_ensure_addr(0);
assert(rc == 0);
s_current_phy = BLE_HCI_LE_PHY_1M_PREF_MASK;
/* Begin scanning for a peripheral to connect to. */
blecent_scan();
all_phy = BLE_HCI_LE_PHY_1M_PREF_MASK | BLE_HCI_LE_PHY_2M_PREF_MASK | BLE_HCI_LE_PHY_CODED_PREF_MASK;
set_default_le_phy(all_phy, all_phy);
if (s_current_phy != BLE_HCI_LE_PHY_1M_PREF_MASK) {
/* Check if peer address is set in EXAMPLE_PEER_ADDR */
if (strlen(CONFIG_EXAMPLE_PEER_ADDR) &&
(strncmp(CONFIG_EXAMPLE_PEER_ADDR, "ADDR_ANY", strlen("ADDR_ANY")) != 0)) {
/* User wants to connect on 2M or coded phy directly */
sscanf(CONFIG_EXAMPLE_PEER_ADDR, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
&peer_addr[5], &peer_addr[4], &peer_addr[3],
&peer_addr[2], &peer_addr[1], &peer_addr[0]);
for(ii = 0 ;ii < 6; ii++)
conn_addr.val[ii] = peer_addr[ii];
conn_addr.type = 0;
vTaskDelay(300);
if (s_current_phy == BLE_HCI_LE_PHY_2M_PREF_MASK)
s_current_phy = BLE_HCI_LE_PHY_1M_PREF_MASK | BLE_HCI_LE_PHY_2M_PREF_MASK ;
ble_gap_ext_connect(0, &conn_addr, 30000, s_current_phy,
NULL, NULL, NULL, blecent_gap_event, NULL);
}
}
else {
/* Begin scanning for a peripheral to connect to. */
blecent_scan();
}
}
void blecent_host_task(void *param)

Wyświetl plik

@ -52,116 +52,118 @@ See the [Getting Started Guide](https://idf.espressif.com/) for full steps to co
This is the console output on successful connection:
```
I (322) BTDM_INIT: BT controller compile version [05195c9]
I (322) phy_init: phy_version 912,d001756,Jun 2 2022,16:28:07
I (362) system_api: Base MAC address is not set
I (362) system_api: read default base MAC address from EFUSE
I (362) BTDM_INIT: Bluetooth MAC: 84:f7:03:05:a5:f6
I (459) NimBLE_BLE_PHY_PRPH: BLE Host Task Started
I (459) NimBLE: Device Address:
I (459) NimBLE: 60:55:f9:f7:3e:23
I (469) NimBLE:
I (362) NimBLE_BLE_PHY_PRPH: BLE Host Task Started
I (372) NimBLE: Device Address:
I (372) NimBLE: 84:f7:03:05:a5:f6
I (382) NimBLE:
I (469) NimBLE: Default LE PHY set successfully
I (479) NimBLE: GAP procedure initiated: extended advertise; instance=1
I (382) uart: queue free spaces: 8
I (4782) NimBLE: connection established; status=0
I (4782) NimBLE: handle=1 our_ota_addr_type=0 our_ota_addr=
I (4782) NimBLE: 84:f7:03:05:a5:f6
I (4782) NimBLE: our_id_addr_type=0 our_id_addr=
I (4792) NimBLE: 84:f7:03:05:a5:f6
I (4792) NimBLE: peer_ota_addr_type=0 peer_ota_addr=
I (4802) NimBLE: 84:f7:03:08:4d:8e
I (4802) NimBLE: peer_id_addr_type=0 peer_id_addr=
I (4812) NimBLE: 84:f7:03:08:4d:8e
I (4812) NimBLE: conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0
I (479) uart: queue free spaces: 8
I (489) main_task: Returned from app_main()
I (599) NimBLE: connection established; status=0
I (599) NimBLE: handle=1 our_ota_addr_type=0 our_ota_addr=
I (599) NimBLE: 60:55:f9:f7:3e:23
I (599) NimBLE: our_id_addr_type=0 our_id_addr=
I (609) NimBLE: 60:55:f9:f7:3e:23
I (609) NimBLE: peer_ota_addr_type=0 peer_ota_addr=
I (619) NimBLE: 60:55:f9:f7:3e:25
I (619) NimBLE: peer_id_addr_type=0 peer_id_addr=
I (619) NimBLE: 60:55:f9:f7:3e:25
I (629) NimBLE: conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0
I (4822) NimBLE:
I (639) NimBLE:
I (4822) NimBLE: advertise complete; reason=0
I (4832) NimBLE: LE PHY Update completed; status=0 conn_handle=1 tx_phy=1 rx_phy = 0
I (639) NimBLE: advertise complete; reason=0
I (1749) NimBLE: disconnect; reason=531
I (1749) NimBLE: handle=1 our_ota_addr_type=0 our_ota_addr=
I (1749) NimBLE: 60:55:f9:f7:3e:23
I (1749) NimBLE: our_id_addr_type=0 our_id_addr=
I (1759) NimBLE: 60:55:f9:f7:3e:23
I (1759) NimBLE: peer_ota_addr_type=0 peer_ota_addr=
I (1759) NimBLE: 60:55:f9:f7:3e:25
I (1769) NimBLE: peer_id_addr_type=0 peer_id_addr=
I (1769) NimBLE: 60:55:f9:f7:3e:25
I (1779) NimBLE: conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0
I (5322) NimBLE: LE PHY Update completed; status=0 conn_handle=1 tx_phy=2 rx_phy = 2
I (1789) NimBLE:
I (5732) NimBLE: disconnect; reason=531
I (5732) NimBLE: handle=1 our_ota_addr_type=0 our_ota_addr=
I (5732) NimBLE: 84:f7:03:05:a5:f6
I (5732) NimBLE: our_id_addr_type=0 our_id_addr=
I (5742) NimBLE: 84:f7:03:05:a5:f6
I (5742) NimBLE: peer_ota_addr_type=0 peer_ota_addr=
I (5752) NimBLE: 84:f7:03:08:4d:8e
I (5752) NimBLE: peer_id_addr_type=0 peer_id_addr=
I (5762) NimBLE: 84:f7:03:08:4d:8e
I (5762) NimBLE: conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0
I (1789) NimBLE: GAP procedure initiated: extended advertise; instance=1
I (5772) NimBLE:
I (1809) NimBLE: connection established; status=0
I (1809) NimBLE: handle=1 our_ota_addr_type=0 our_ota_addr=
I (1809) NimBLE: 60:55:f9:f7:3e:23
I (1819) NimBLE: our_id_addr_type=0 our_id_addr=
I (1819) NimBLE: 60:55:f9:f7:3e:23
I (1819) NimBLE: peer_ota_addr_type=0 peer_ota_addr=
I (1829) NimBLE: 60:55:f9:f7:3e:25
I (1829) NimBLE: peer_id_addr_type=0 peer_id_addr=
I (1839) NimBLE: 60:55:f9:f7:3e:25
I (1839) NimBLE: conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0
I (5772) NimBLE: Default LE PHY set successfully
I (5822) NimBLE: connection established; status=0
I (5822) NimBLE: handle=1 our_ota_addr_type=0 our_ota_addr=
I (5822) NimBLE: 84:f7:03:05:a5:f6
I (5822) NimBLE: our_id_addr_type=0 our_id_addr=
I (5832) NimBLE: 84:f7:03:05:a5:f6
I (5832) NimBLE: peer_ota_addr_type=0 peer_ota_addr=
I (5842) NimBLE: 84:f7:03:08:4d:8e
I (5842) NimBLE: peer_id_addr_type=0 peer_id_addr=
I (5852) NimBLE: 84:f7:03:08:4d:8e
I (5852) NimBLE: conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0
I (1849) NimBLE:
I (5862) NimBLE:
I (1859) NimBLE: advertise complete; reason=0
I (3009) NimBLE: disconnect; reason=531
I (3009) NimBLE: handle=1 our_ota_addr_type=0 our_ota_addr=
I (3009) NimBLE: 60:55:f9:f7:3e:23
I (3009) NimBLE: our_id_addr_type=0 our_id_addr=
I (3019) NimBLE: 60:55:f9:f7:3e:23
I (3019) NimBLE: peer_ota_addr_type=0 peer_ota_addr=
I (3029) NimBLE: 60:55:f9:f7:3e:25
I (3029) NimBLE: peer_id_addr_type=0 peer_id_addr=
I (3039) NimBLE: 60:55:f9:f7:3e:25
I (3039) NimBLE: conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0
I (5862) NimBLE: advertise complete; reason=0
I (5872) NimBLE: LE PHY Update completed; status=0 conn_handle=1 tx_phy=1 rx_phy = 0
I (3049) NimBLE:
I (6322) NimBLE: LE PHY Update completed; status=0 conn_handle=1 tx_phy=2 rx_phy = 2
I (3059) NimBLE: GAP procedure initiated: extended advertise; instance=1
I (6782) NimBLE: disconnect; reason=531
I (6782) NimBLE: handle=1 our_ota_addr_type=0 our_ota_addr=
I (6782) NimBLE: 84:f7:03:05:a5:f6
I (6782) NimBLE: our_id_addr_type=0 our_id_addr=
I (6792) NimBLE: 84:f7:03:05:a5:f6
I (6792) NimBLE: peer_ota_addr_type=0 peer_ota_addr=
I (6802) NimBLE: 84:f7:03:08:4d:8e
I (6802) NimBLE: peer_id_addr_type=0 peer_id_addr=
I (6812) NimBLE: 84:f7:03:08:4d:8e
I (6812) NimBLE: conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0
I (3079) NimBLE: connection established; status=0
I (3079) NimBLE: handle=1 our_ota_addr_type=0 our_ota_addr=
I (3079) NimBLE: 60:55:f9:f7:3e:23
I (3079) NimBLE: our_id_addr_type=0 our_id_addr=
I (3089) NimBLE: 60:55:f9:f7:3e:23
I (3089) NimBLE: peer_ota_addr_type=0 peer_ota_addr=
I (3099) NimBLE: 60:55:f9:f7:3e:25
I (3099) NimBLE: peer_id_addr_type=0 peer_id_addr=
I (3109) NimBLE: 60:55:f9:f7:3e:25
I (3109) NimBLE: conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0
I (6822) NimBLE:
I (3119) NimBLE:
I (6822) NimBLE: Default LE PHY set successfully
I (6872) NimBLE: connection established; status=0
I (6872) NimBLE: handle=1 our_ota_addr_type=0 our_ota_addr=
I (6872) NimBLE: 84:f7:03:05:a5:f6
I (6872) NimBLE: our_id_addr_type=0 our_id_addr=
I (6882) NimBLE: 84:f7:03:05:a5:f6
I (6882) NimBLE: peer_ota_addr_type=0 peer_ota_addr=
I (6892) NimBLE: 84:f7:03:08:4d:8e
I (6892) NimBLE: peer_id_addr_type=0 peer_id_addr=
I (6902) NimBLE: 84:f7:03:08:4d:8e
I (6902) NimBLE: conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0
I (6912) NimBLE:
I (6912) NimBLE: advertise complete; reason=0
I (6922) NimBLE: LE PHY Update completed; status=0 conn_handle=1 tx_phy=1 rx_phy = 0
I (7372) NimBLE: LE PHY Update completed; status=0 conn_handle=1 tx_phy=3 rx_phy = 3
I (7832) NimBLE: disconnect; reason=531
I (7832) NimBLE: handle=1 our_ota_addr_type=0 our_ota_addr=
I (7832) NimBLE: 84:f7:03:05:a5:f6
I (7832) NimBLE: our_id_addr_type=0 our_id_addr=
I (7842) NimBLE: 84:f7:03:05:a5:f6
I (7842) NimBLE: peer_ota_addr_type=0 peer_ota_addr=
I (7852) NimBLE: 84:f7:03:08:4d:8e
I (7852) NimBLE: peer_id_addr_type=0 peer_id_addr=
I (7862) NimBLE: 84:f7:03:08:4d:8e
I (7862) NimBLE: conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0
I (3129) NimBLE: advertise complete; reason=0
I (4379) NimBLE: disconnect; reason=531
I (4379) NimBLE: handle=1 our_ota_addr_type=0 our_ota_addr=
I (4379) NimBLE: 60:55:f9:f7:3e:23
I (4379) NimBLE: our_id_addr_type=0 our_id_addr=
I (4389) NimBLE: 60:55:f9:f7:3e:23
I (4389) NimBLE: peer_ota_addr_type=0 peer_ota_addr=
I (4399) NimBLE: 60:55:f9:f7:3e:25
I (4399) NimBLE: peer_id_addr_type=0 peer_id_addr=
I (4409) NimBLE: 60:55:f9:f7:3e:25
I (4409) NimBLE: conn_itvl=40 conn_latency=0 supervision_timeout=256 encrypted=0 authenticated=0 bonded=0
I (7872) NimBLE:
```
## Accepting connection directly on 2M / Coded Phy
The example can also be configured to advertise directly on 2M auxillary PHY or CODED PHY
In main.c file, in bleprph_on_sync function, set the s_current_phy to the intended phy.
To advertise, using 2M Phy, set the s_current_phy to BLE_HCI_LE_PHY_2M_PREF_MASK.
To advertise, using Coded Phy, set the s_current_phy to BLE_HCI_LE_PHY_CODED_PREF_MASK
Recompile application and flash the image.
Since , in this mode, the central will initiate direct connection, ensure that peripheral is already advertising, before initiating connection from the central device.
## Troubleshooting
For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon.

Wyświetl plik

@ -44,16 +44,4 @@ menu "Example Configuration"
prompt "Use Secure Connection feature"
help
Use this option to enable/disable Security Manager Secure Connection 4.2 feature.
config EXAMPLE_EXTENDED_ADV
bool
depends on SOC_BLE_50_SUPPORTED
default y if SOC_ESP_NIMBLE_CONTROLLER
select BT_NIMBLE_EXT_ADV
prompt "Enable Extended Adv"
help
Use this option to enable extended advertising in the example.
If this option is disabled, ensure config BT_NIMBLE_EXT_ADV is
also disabled from Nimble stack menuconfig
endmenu

Wyświetl plik

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
@ -15,7 +15,6 @@
#include "services/gap/ble_svc_gap.h"
#include "phy_prph.h"
#if CONFIG_EXAMPLE_EXTENDED_ADV
static uint8_t ext_adv_pattern_1M[] = {
0x02, 0x01, 0x06,
0x03, 0x03, 0xab, 0xcd,
@ -37,7 +36,6 @@ static uint8_t ext_adv_pattern_coded[] = {
0x11, 0X09, 'b', 'l', 'e', 'p', 'r', 'p', 'h', '-', 'p', 'h', 'y', '-', 'c', 'o', 'd', 'e',
'd',
};
#endif
static const char *tag = "NimBLE_BLE_PHY_PRPH";
static int bleprph_gap_event(struct ble_gap_event *event, void *arg);
@ -47,7 +45,7 @@ static uint8_t s_current_phy;
void ble_store_config_init(void);
/* Set default LE PHY before establishing connection */
void set_default_le_phy_before_conn(uint8_t tx_phys_mask, uint8_t rx_phys_mask)
void set_default_le_phy(uint8_t tx_phys_mask, uint8_t rx_phys_mask)
{
int rc = ble_gap_set_prefered_default_le_phy(tx_phys_mask, rx_phys_mask);
if (rc == 0) {
@ -84,7 +82,6 @@ bleprph_print_conn_desc(struct ble_gap_conn_desc *desc)
desc->sec_state.bonded);
}
#if CONFIG_EXAMPLE_EXTENDED_ADV
static struct os_mbuf *
ext_get_data(uint8_t ext_adv_pattern[], int size)
{
@ -113,10 +110,13 @@ ext_bleprph_advertise(void)
/* use defaults for non-set params */
memset (&params, 0, sizeof(params));
/* enable connectable advertising */
if (s_current_phy == BLE_HCI_LE_PHY_1M_PREF_MASK) {
params.scannable = 1;
params.legacy_pdu = 1;
}
/*enable connectable advertising for all Phy*/
params.connectable = 1;
params.scannable = 1;
params.legacy_pdu = 1;
/* advertise using random addr */
params.own_addr_type = BLE_OWN_ADDR_PUBLIC;
@ -127,24 +127,24 @@ ext_bleprph_advertise(void)
params.primary_phy = BLE_HCI_LE_PHY_1M;
params.secondary_phy = BLE_HCI_LE_PHY_1M;
data = ext_get_data(ext_adv_pattern_1M, sizeof(ext_adv_pattern_1M));
params.sid = 0;
break;
case BLE_HCI_LE_PHY_2M_PREF_MASK:
params.primary_phy = BLE_HCI_LE_PHY_1M;
params.secondary_phy = BLE_HCI_LE_PHY_2M;
data = ext_get_data(ext_adv_pattern_2M, sizeof(ext_adv_pattern_2M));
params.sid = 1;
break;
case BLE_HCI_LE_PHY_CODED_PREF_MASK:
params.primary_phy = BLE_HCI_LE_PHY_CODED;
params.secondary_phy = BLE_HCI_LE_PHY_CODED;
data = ext_get_data(ext_adv_pattern_coded, sizeof(ext_adv_pattern_coded));
params.sid = 2;
break;
}
//params.tx_power = 127;
params.sid = 1;
params.itvl_min = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
params.itvl_max = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
@ -160,73 +160,7 @@ ext_bleprph_advertise(void)
rc = ble_gap_ext_adv_start(instance, 0, 0);
assert (rc == 0);
}
#else
/**
* Enables advertising with the following parameters:
* o General discoverable mode.
* o Undirected connectable mode.
*/
static void
bleprph_advertise(void)
{
struct ble_gap_adv_params adv_params;
struct ble_hs_adv_fields fields;
const char *name;
int rc;
/**
* Set the advertisement data included in our advertisements:
* o Flags (indicates advertisement type and other general info).
* o Advertising tx power.
* o Device name.
* o 16-bit service UUIDs (alert notifications).
*/
memset(&fields, 0, sizeof fields);
/* Advertise two flags:
* o Discoverability in forthcoming advertisement (general)
* o BLE-only (BR/EDR unsupported).
*/
fields.flags = BLE_HS_ADV_F_DISC_GEN |
BLE_HS_ADV_F_BREDR_UNSUP;
/* Indicate that the TX power level field should be included; have the
* stack fill this value automatically. This is done by assigning the
* special value BLE_HS_ADV_TX_PWR_LVL_AUTO.
*/
fields.tx_pwr_lvl_is_present = 1;
fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
name = ble_svc_gap_device_name();
fields.name = (uint8_t *)name;
fields.name_len = strlen(name);
fields.name_is_complete = 1;
fields.uuids16 = (ble_uuid16_t[]) {
BLE_UUID16_INIT(LE_PHY_UUID16)
};
fields.num_uuids16 = 1;
fields.uuids16_is_complete = 1;
rc = ble_gap_adv_set_fields(&fields);
if (rc != 0) {
MODLOG_DFLT(ERROR, "error setting advertisement data; rc=%d\n", rc);
return;
}
/* Begin advertising. */
memset(&adv_params, 0, sizeof adv_params);
adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;
rc = ble_gap_adv_start(own_addr_type, NULL, BLE_HS_FOREVER,
&adv_params, bleprph_gap_event, NULL);
if (rc != 0) {
MODLOG_DFLT(ERROR, "error enabling advertisement; rc=%d\n", rc);
return;
}
}
#endif
/**
* The nimble host executes this callback when a GAP event occurs. The
* application associates a GAP event callback with each connection that forms.
@ -263,11 +197,7 @@ bleprph_gap_event(struct ble_gap_event *event, void *arg)
if (event->connect.status != 0) {
/* Connection failed; resume advertising. */
#if CONFIG_EXAMPLE_EXTENDED_ADV
ext_bleprph_advertise();
#else
bleprph_advertise();
#endif
}
return 0;
@ -278,7 +208,6 @@ bleprph_gap_event(struct ble_gap_event *event, void *arg)
/* Connection terminated; resume advertising. */
#if CONFIG_EXAMPLE_EXTENDED_ADV
switch (s_current_phy) {
case BLE_HCI_LE_PHY_1M_PREF_MASK:
/* Setting current phy to create connection on 2M PHY */
@ -296,11 +225,8 @@ bleprph_gap_event(struct ble_gap_event *event, void *arg)
default:
return 0;
}
set_default_le_phy_before_conn(s_current_phy, s_current_phy);
ext_bleprph_advertise();
#else
bleprph_advertise();
#endif
return 0;
case BLE_GAP_EVENT_CONN_UPDATE:
@ -316,16 +242,6 @@ bleprph_gap_event(struct ble_gap_event *event, void *arg)
case BLE_GAP_EVENT_ADV_COMPLETE:
MODLOG_DFLT(INFO, "advertise complete; reason=%d",
event->adv_complete.reason);
#if !CONFIG_EXAMPLE_EXTENDED_ADV
bleprph_advertise();
#endif
return 0;
case BLE_GAP_EVENT_PHY_UPDATE_COMPLETE:
MODLOG_DFLT(INFO, "LE PHY Update completed; status=%d conn_handle=%d tx_phy=%d "
"rx_phy = %d\n", event->phy_updated.status,
event->phy_updated.conn_handle, event->phy_updated.tx_phy,
event->phy_updated.rx_phy);
return 0;
}
@ -342,6 +258,7 @@ static void
bleprph_on_sync(void)
{
int rc;
uint8_t all_phy;
/* Make sure we have proper identity address set (public preferred) */
rc = ble_hs_util_ensure_addr(0);
@ -364,12 +281,12 @@ bleprph_on_sync(void)
s_current_phy = BLE_HCI_LE_PHY_1M_PREF_MASK;
all_phy = BLE_HCI_LE_PHY_1M_PREF_MASK | BLE_HCI_LE_PHY_2M_PREF_MASK | BLE_HCI_LE_PHY_CODED_PREF_MASK;
set_default_le_phy(all_phy, all_phy);
/* Begin advertising. */
#if CONFIG_EXAMPLE_EXTENDED_ADV
ext_bleprph_advertise();
#else
bleprph_advertise();
#endif
}
void bleprph_host_task(void *param)