Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion subsys/bluetooth/gatt.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ static struct bt_gatt_state {
const struct bt_conn_auto_setup_cb *cb;
struct bt_conn_auto_discovery *discovery;
uint8_t preferred_phy;
bool connect_cb_run;
#endif /* CONFIG_BT_GATT_CLIENT */
} state[CONFIG_BT_MAX_CONN];

Expand Down Expand Up @@ -65,6 +66,7 @@ void bt_conn_le_auto_setup(struct bt_conn *conn, struct bt_conn_auto_discovery *
{
struct bt_gatt_state *s = &state[bt_conn_index(conn)];

s->connect_cb_run = false;
s->discovery = discovery;
s->cb = callbacks;
s->preferred_phy = preferred_phy;
Expand All @@ -74,6 +76,9 @@ static void connection_done(struct bt_conn *conn)
{
struct bt_gatt_state *s = &state[bt_conn_index(conn)];

/* We will call `conn_setup_cb` */
s->connect_cb_run = true;

/* Run user callback */
s->cb->conn_setup_cb(conn, 0, s->cb->user_data);
}
Expand All @@ -84,6 +89,9 @@ static void connection_error(struct bt_conn *conn, int err)

LOG_ERR("Connection setup failed (%d)", err);

/* We will call `conn_setup_cb` */
s->connect_cb_run = true;

/* Run user callback */
s->cb->conn_setup_cb(conn, err, s->cb->user_data);

Expand Down Expand Up @@ -519,7 +527,14 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)
#endif /* CONFIG_BT_CONN_AUTO_RSSI */
#ifdef CONFIG_BT_GATT_CLIENT
if (s->cb) {
if (s->cb->conn_terminated_cb) {
/* Have we run `conn_setup_cb` yet?
* We can expect this to happen with a PHY update procedure, which
* doesn't have a result callback that can fail, and hence there is
* nowhere to run `connection_error` from.
*/
if (!s->connect_cb_run) {
s->cb->conn_setup_cb(conn, reason, s->cb->user_data);
} else if (s->cb->conn_terminated_cb) {
s->cb->conn_terminated_cb(conn, reason, s->cb->user_data);
}
s->cb = NULL;
Expand Down
2 changes: 1 addition & 1 deletion tests/bsim/bluetooth/epacket/src/terminator.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static void main_epacket_conn_refuser(void)

LOG_INF("Starting connection terminator send");

for (int i = 0; i < 18; i++) {
for (int i = 0; i < 36; i++) {
k_sleep(K_MSEC(500));
announce.uptime = k_uptime_seconds();
TDF_DATA_LOGGER_LOG(TDF_DATA_LOGGER_BT_ADV | TDF_DATA_LOGGER_BT_PERIPHERAL,
Expand Down
23 changes: 20 additions & 3 deletions tests/bsim/bluetooth/gatt/src/gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ static void main_connect_phy(void)
PASS("Connect preferred PHY passed\n\n");
}

static void main_connect_terminator(void)
static void run_connect_terminator(uint8_t phy)
{
struct k_poll_signal sig;
struct bt_gatt_remote_char remote_info[3] = {0};
Expand Down Expand Up @@ -956,10 +956,10 @@ static void main_connect_terminator(void)
FAIL("Failed to initiate connection\n");
return;
}
bt_conn_le_auto_setup(conn, &discovery, &callbacks, BT_GAP_LE_PHY_NONE);
bt_conn_le_auto_setup(conn, &discovery, &callbacks, phy);

/* Wait for connection process to complete */
rc = k_poll(events, ARRAY_SIZE(events), K_SECONDS(3));
rc = k_poll(events, ARRAY_SIZE(events), K_SECONDS(5));
k_poll_signal_check(&sig, &signaled, &conn_rc);
if (signaled != 1) {
FAIL("Result not signaled\n");
Expand All @@ -981,6 +981,16 @@ static void main_connect_terminator(void)
PASS("Connect terminator passed\n\n");
}

static void main_connect_terminator(void)
{
run_connect_terminator(BT_GAP_LE_PHY_NONE);
}

static void main_connect_terminator_phy(void)
{
run_connect_terminator(BT_GAP_LE_PHY_1M);
}

static const struct bst_test_instance gatt_gateway[] = {
{
.test_id = "gatt_connect_nonexistant",
Expand Down Expand Up @@ -1052,6 +1062,13 @@ static const struct bst_test_instance gatt_gateway[] = {
.test_tick_f = test_tick,
.test_main_f = main_connect_terminator,
},
{
.test_id = "gatt_connect_terminator_phy",
.test_descr = "Connect to device that keeps disconnecting with a preferred PHY",
.test_pre_init_f = test_init,
.test_tick_f = test_tick,
.test_main_f = main_connect_terminator_phy,
},
BSTEST_END_MARKER};

struct bst_test_list *test_gatt_bt_gateway(struct bst_test_list *tests)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_gatt_prj_conf \
-testid=gatt_connect_terminator -rs=6

Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \
-D=2 -sim_length=10e6 $@
-D=2 -sim_length=20e6 $@

wait_for_background_jobs
23 changes: 23 additions & 0 deletions tests/bsim/bluetooth/gatt/tests_scripts/connect_terminator_phy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
# Copyright (c) 2024 Embeint Inc
# SPDX-License-Identifier: Apache-2.0

source ${ZEPHYR_BASE}/tests/bsim/sh_common.source

simulation_id="connect_terminator_phy"
verbosity_level=2

cd ${BSIM_OUT_PATH}/bin

Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_epacket_prj_conn_terminator_conf \
-v=${verbosity_level} -s=${simulation_id} -d=0 -RealEncryption=0 \
-testid=epacket_bt_conn_terminator -rs=23

Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_gatt_prj_conf \
-v=${verbosity_level} -s=${simulation_id} -d=1 -RealEncryption=0 \
-testid=gatt_connect_terminator_phy -rs=6

Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \
-D=2 -sim_length=20e6 $@

wait_for_background_jobs