diff --git a/include/zephyr/bluetooth/conn.h b/include/zephyr/bluetooth/conn.h index b4cf1c5c6bab212..5ef3628172690fc 100644 --- a/include/zephyr/bluetooth/conn.h +++ b/include/zephyr/bluetooth/conn.h @@ -74,9 +74,9 @@ struct bt_le_conn_param { * Latency: 0 * Timeout: 4 s */ -#define BT_LE_CONN_PARAM_DEFAULT BT_LE_CONN_PARAM(BT_GAP_INIT_CONN_INT_MIN, \ - BT_GAP_INIT_CONN_INT_MAX, \ - 0, 400) +#define BT_LE_CONN_PARAM_DEFAULT \ + BT_LE_CONN_PARAM(BT_GAP_INIT_CONN_INT_MIN, BT_GAP_INIT_CONN_INT_MAX, 0, \ + BT_GAP_MS_TO_CONN_TIMEOUT(4000)) /** Connection PHY information for LE connections */ struct bt_conn_le_phy_info { diff --git a/include/zephyr/bluetooth/gap.h b/include/zephyr/bluetooth/gap.h index f1c8093b5b539e7..6bdffcbe1660d03 100644 --- a/include/zephyr/bluetooth/gap.h +++ b/include/zephyr/bluetooth/gap.h @@ -825,12 +825,93 @@ enum { /** Maximum Periodic Advertising Interval (N * 1.25 ms) */ #define BT_GAP_PER_ADV_MAX_INTERVAL 0xFFFF /* 81.91875 s */ +/** + * @brief Convert periodic advertising interval (N * 0.625 ms) to milliseconds + * + * @note When intervals cannot be represented in milliseconds, this will round down. + * For example BT_GAP_ADV_INTERVAL_TO_MS(6) will become 7 ms instead of 7.5 ms + */ +#define BT_GAP_ADV_INTERVAL_TO_MS(_interval) ((_interval) * 5U / 8U) + +/** + * @brief Convert isochronous interval (N * 1.25 ms) to milliseconds + * + * @note When intervals cannot be represented in milliseconds, this will round down. + * For example BT_GAP_ISO_INTERVAL_TO_MS(6) will become 7 ms instead of 7.5 ms + */ +#define BT_GAP_ISO_INTERVAL_TO_MS(_interval) ((_interval) * 5U / 4U) + /** * @brief Convert periodic advertising interval (N * 1.25 ms) to milliseconds * - * 5 / 4 represents 1.25 ms unit. + * @note When intervals cannot be represented in milliseconds, this will round down. + * For example BT_GAP_PER_ADV_INTERVAL_TO_MS(6) will become 7 ms instead of 7.5 ms + */ +#define BT_GAP_PER_ADV_INTERVAL_TO_MS(_interval) ((_interval) * 5U / 4U) + +/** + * @brief Convert milliseconds to advertising interval units (0.625 ms) + * + * @note If @p _interval is not a multiple of the unit, it will round down to nearest. + * For example BT_GAP_MS_TO_ADV_INTERVAL(23) will become 22.5 milliseconds + */ +#define BT_GAP_MS_TO_ADV_INTERVAL(_interval) ((uint16_t)((_interval) * 8U / 5U)) + +/** + * @brief Convert milliseconds to periodic advertising interval units (1.25 ms) + * + * @note If @p _interval is not a multiple of the unit, it will round down to nearest. + * For example BT_GAP_MS_TO_PER_ADV_INTERVAL(23) will become 22.5 milliseconds + */ +#define BT_GAP_MS_TO_PER_ADV_INTERVAL(_interval) ((uint16_t)((_interval) * 4U / 5U)) + +/** + * @brief Convert milliseconds to periodic advertising sync timeout (10 ms) + * + * @note If @p _timeout is not a multiple of the unit, it will round down to nearest. + * For example BT_GAP_MS_TO_PER_ADV_SYNC_TIMEOUT(4005) will become 4000 milliseconds + */ +#define BT_GAP_MS_TO_PER_ADV_SYNC_TIMEOUT(_timeout) ((uint16_t)((_timeout) / 10U)) + +/** + * @brief Convert milliseconds to scan interval units (0.625 ms) + * + * @note If @p _interval is not a multiple of the unit, it will round down to nearest. + * For example BT_GAP_MS_TO_SCAN_INTERVAL(23) will become 22.5 milliseconds + */ +#define BT_GAP_MS_TO_SCAN_INTERVAL(_interval) ((uint16_t)((_interval) * 8U / 5U)) + +/** + * @brief Convert milliseconds to scan window units (0.625 ms) + * + * @note If @p _window is not a multiple of the unit, it will round down to nearest. + * For example BT_GAP_MS_TO_SCAN_WINDOW(23) will become 22.5 milliseconds + */ +#define BT_GAP_MS_TO_SCAN_WINDOW(_window) ((uint16_t)((_window) * 8U / 5U)) + +/** + * @brief Convert milliseconds to connection interval units (1.25 ms) + * + * @note If @p _interval is not a multiple of the unit, it will round down to nearest. + * For example BT_GAP_MS_TO_CONN_INTERVAL(23) will become 22.5 milliseconds + */ +#define BT_GAP_MS_TO_CONN_INTERVAL(_interval) ((uint16_t)((_interval) * 4U / 5U)) + +/** + * @brief Convert milliseconds to connection supervision timeout units (10 ms) + * + * @note If @p _timeout is not a multiple of the unit, it will round down to nearest. + * For example BT_GAP_MS_TO_CONN_TIMEOUT(4005) will become 4000 milliseconds + */ +#define BT_GAP_MS_TO_CONN_TIMEOUT(_timeout) ((uint16_t)((_timeout) / 10U)) + +/** + * @brief Convert milliseconds to connection event length units (0.625) + * + * @note If @p _event_len is not a multiple of the unit, it will round down to nearest. + * For example BT_GAP_MS_TO_CONN_EVENT_LEN(23) will become 22.5 milliseconds */ -#define BT_GAP_PER_ADV_INTERVAL_TO_MS(interval) ((interval) * 5 / 4) +#define BT_GAP_MS_TO_CONN_EVENT_LEN(_event_len) ((uint16_t)((_event_len) * 8U / 5U)) /** Constant Tone Extension (CTE) types */ enum { diff --git a/samples/bluetooth/bap_broadcast_source/src/main.c b/samples/bluetooth/bap_broadcast_source/src/main.c index 9d3fc8759e12fa5..b0238ade6bd02c1 100644 --- a/samples/bluetooth/bap_broadcast_source/src/main.c +++ b/samples/bluetooth/bap_broadcast_source/src/main.c @@ -39,7 +39,9 @@ BUILD_ASSERT(strlen(CONFIG_BROADCAST_CODE) <= BT_ISO_BROADCAST_CODE_SIZE, "Inval * And, for 10 ms ISO interval, can use 90 ms minus 10 ms ==> 80 ms advertising * interval. */ -#define BT_LE_EXT_ADV_CUSTOM BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV, 0x0080, 0x0080, NULL) +#define BT_LE_EXT_ADV_CUSTOM \ + BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV, BT_GAP_MS_TO_ADV_INTERVAL(80), \ + BT_GAP_MS_TO_ADV_INTERVAL(80), NULL) /* When BROADCAST_ENQUEUE_COUNT > 1 we can enqueue enough buffers to ensure that * the controller is never idle diff --git a/samples/bluetooth/hci_pwr_ctrl/src/main.c b/samples/bluetooth/hci_pwr_ctrl/src/main.c index 3b08d45b8a00912..7595326bd131cd4 100644 --- a/samples/bluetooth/hci_pwr_ctrl/src/main.c +++ b/samples/bluetooth/hci_pwr_ctrl/src/main.c @@ -6,6 +6,7 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include #include #include #include @@ -42,8 +43,8 @@ static K_THREAD_STACK_DEFINE(pwr_thread_stack, 512); static const int8_t txpower[DEVICE_BEACON_TXPOWER_NUM] = {4, 0, -3, -8, -15, -18, -23, -30}; -static const struct bt_le_adv_param *param = - BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONN, 0x0020, 0x0020, NULL); +static const struct bt_le_adv_param *param = BT_LE_ADV_PARAM( + BT_LE_ADV_OPT_CONN, BT_GAP_MS_TO_ADV_INTERVAL(20), BT_GAP_MS_TO_ADV_INTERVAL(20), NULL); static void read_conn_rssi(uint16_t handle, int8_t *rssi) { diff --git a/tests/bluetooth/tester/src/btp_gap.c b/tests/bluetooth/tester/src/btp_gap.c index d4cfa91ca68489b..f08269570e51ccd 100644 --- a/tests/bluetooth/tester/src/btp_gap.c +++ b/tests/bluetooth/tester/src/btp_gap.c @@ -989,7 +989,8 @@ static uint8_t connect(const void *cmd, uint16_t cmd_len, void *rsp, uint16_t *rsp_len) { const struct bt_le_conn_param *conn_param = - BT_LE_CONN_PARAM(BT_GAP_INIT_CONN_INT_MIN, BT_GAP_INIT_CONN_INT_MIN, 0, 400); + BT_LE_CONN_PARAM(BT_GAP_INIT_CONN_INT_MIN, BT_GAP_INIT_CONN_INT_MIN, 0, + BT_GAP_MS_TO_CONN_TIMEOUT(4000)); const struct btp_gap_connect_cmd *cp = cmd; int err; diff --git a/tests/bsim/bluetooth/audio/src/cap_commander_test.c b/tests/bsim/bluetooth/audio/src/cap_commander_test.c index 43e57a2e6f5b726..f88aea0d079d85c 100644 --- a/tests/bsim/bluetooth/audio/src/cap_commander_test.c +++ b/tests/bsim/bluetooth/audio/src/cap_commander_test.c @@ -586,10 +586,10 @@ static void cap_device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type return; } - err = bt_conn_le_create( - addr, BT_CONN_LE_CREATE_CONN, - BT_LE_CONN_PARAM(BT_GAP_INIT_CONN_INT_MIN, BT_GAP_INIT_CONN_INT_MIN, 0, 400), - &connected_conns[connected_conn_cnt]); + err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, + BT_LE_CONN_PARAM(BT_GAP_INIT_CONN_INT_MIN, BT_GAP_INIT_CONN_INT_MIN, + 0, BT_GAP_MS_TO_CONN_TIMEOUT(4000)), + &connected_conns[connected_conn_cnt]); if (err) { FAIL("Could not connect to peer: %d", err); } diff --git a/tests/bsim/bluetooth/audio/src/cap_initiator_broadcast_test.c b/tests/bsim/bluetooth/audio/src/cap_initiator_broadcast_test.c index 9b9ad2d335490e4..b81b1cfe18d4ff6 100644 --- a/tests/bsim/bluetooth/audio/src/cap_initiator_broadcast_test.c +++ b/tests/bsim/bluetooth/audio/src/cap_initiator_broadcast_test.c @@ -38,14 +38,13 @@ * required to place the AUX_ADV_IND PDUs in a non-overlapping interval with the * Broadcast ISO radio events. */ -#define BT_LE_EXT_ADV_CUSTOM \ - BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV, \ - 0x0080, 0x0080, NULL) - -#define BT_LE_PER_ADV_CUSTOM \ - BT_LE_PER_ADV_PARAM(0x0048, \ - 0x0048, \ - BT_LE_PER_ADV_OPT_NONE) +#define BT_LE_EXT_ADV_CUSTOM \ + BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV, BT_GAP_MS_TO_ADV_INTERVAL(80), \ + BT_GAP_MS_TO_ADV_INTERVAL(80), NULL) + +#define BT_LE_PER_ADV_CUSTOM \ + BT_LE_PER_ADV_PARAM(BT_GAP_MS_TO_PER_ADV_INTERVAL(90), BT_GAP_MS_TO_PER_ADV_INTERVAL(90), \ + BT_LE_PER_ADV_OPT_NONE) #define BROADCAST_STREMT_CNT CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT #define BROADCAST_ENQUEUE_COUNT 2U diff --git a/tests/bsim/bluetooth/audio/src/cap_initiator_unicast_test.c b/tests/bsim/bluetooth/audio/src/cap_initiator_unicast_test.c index bd98e6a5c09281c..920d4b663177879 100644 --- a/tests/bsim/bluetooth/audio/src/cap_initiator_unicast_test.c +++ b/tests/bsim/bluetooth/audio/src/cap_initiator_unicast_test.c @@ -438,10 +438,10 @@ static void cap_device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type return; } - err = bt_conn_le_create( - addr, BT_CONN_LE_CREATE_CONN, - BT_LE_CONN_PARAM(BT_GAP_INIT_CONN_INT_MIN, BT_GAP_INIT_CONN_INT_MIN, 0, 400), - &connected_conns[connected_conn_cnt]); + err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, + BT_LE_CONN_PARAM(BT_GAP_INIT_CONN_INT_MIN, BT_GAP_INIT_CONN_INT_MIN, + 0, BT_GAP_MS_TO_CONN_TIMEOUT(4000)), + &connected_conns[connected_conn_cnt]); if (err) { FAIL("Could not connect to peer: %d", err); } diff --git a/tests/bsim/bluetooth/audio/src/gmap_ugg_test.c b/tests/bsim/bluetooth/audio/src/gmap_ugg_test.c index c50f5104f0ddf41..a15129361288554 100644 --- a/tests/bsim/bluetooth/audio/src/gmap_ugg_test.c +++ b/tests/bsim/bluetooth/audio/src/gmap_ugg_test.c @@ -44,14 +44,13 @@ * required to place the AUX_ADV_IND PDUs in a non-overlapping interval with the * Broadcast ISO radio events. */ -#define BT_LE_EXT_ADV_CUSTOM \ - BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV, \ - 0x0080, 0x0080, NULL) +#define BT_LE_EXT_ADV_CUSTOM \ + BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV, BT_GAP_MS_TO_ADV_INTERVAL(80), \ + BT_GAP_MS_TO_ADV_INTERVAL(80), NULL) -#define BT_LE_PER_ADV_CUSTOM \ - BT_LE_PER_ADV_PARAM(0x0048, \ - 0x0048, \ - BT_LE_PER_ADV_OPT_NONE) +#define BT_LE_PER_ADV_CUSTOM \ + BT_LE_PER_ADV_PARAM(BT_GAP_MS_TO_PER_ADV_INTERVAL(90), BT_GAP_MS_TO_PER_ADV_INTERVAL(90), \ + BT_LE_PER_ADV_OPT_NONE) #define UNICAST_SINK_SUPPORTED (CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT > 0) #define UNICAST_SRC_SUPPORTED (CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT > 0) @@ -546,10 +545,10 @@ static void gmap_device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t typ return; } - err = bt_conn_le_create( - addr, BT_CONN_LE_CREATE_CONN, - BT_LE_CONN_PARAM(BT_GAP_INIT_CONN_INT_MIN, BT_GAP_INIT_CONN_INT_MIN, 0, 400), - &connected_conns[connected_conn_cnt]); + err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, + BT_LE_CONN_PARAM(BT_GAP_INIT_CONN_INT_MIN, BT_GAP_INIT_CONN_INT_MIN, + 0, BT_GAP_MS_TO_CONN_TIMEOUT(4000)), + &connected_conns[connected_conn_cnt]); if (err) { FAIL("Could not connect to peer: %d", err); } diff --git a/tests/bsim/bluetooth/audio/src/mcc_test.c b/tests/bsim/bluetooth/audio/src/mcc_test.c index b6c0fedf5228b93..ecd9d4eef26fc14 100644 --- a/tests/bsim/bluetooth/audio/src/mcc_test.c +++ b/tests/bsim/bluetooth/audio/src/mcc_test.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -2462,7 +2463,10 @@ void test_main(void) bt_addr_le_to_str(bt_conn_get_dst(default_conn), addr, sizeof(addr)); printk("Connected: %s\n", addr); - bt_conn_le_param_update(default_conn, BT_LE_CONN_PARAM(0x06U, 0x10U, 0U, 400U)); + bt_conn_le_param_update(default_conn, + BT_LE_CONN_PARAM(BT_GAP_MS_TO_CONN_INTERVAL(7.5), + BT_GAP_MS_TO_CONN_INTERVAL(20), 0U, + BT_GAP_MS_TO_CONN_TIMEOUT(4000U))); WAIT_FOR_FLAG(flag_conn_updated); test_discover(); diff --git a/tests/bsim/bluetooth/ll/cis/src/main.c b/tests/bsim/bluetooth/ll/cis/src/main.c index 2e5e8eeebf394f2..564b9acf5f71753 100644 --- a/tests/bsim/bluetooth/ll/cis/src/main.c +++ b/tests/bsim/bluetooth/ll/cis/src/main.c @@ -6,6 +6,7 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include #include #include #include @@ -14,6 +15,8 @@ #include #include #include +#include +#include #include "bs_types.h" #include "bs_tracing.h" @@ -49,22 +52,23 @@ static bt_addr_le_t peer_addr; #define CREATE_CONN_WINDOW 0x0010 #define ISO_INTERVAL_US 10000U +#define ISO_INTERVAL_MS (ISO_INTERVAL_US / USEC_PER_MSEC) #define ISO_LATENCY_MS DIV_ROUND_UP(ISO_INTERVAL_US, USEC_PER_MSEC) #define ISO_LATENCY_FT_MS 20U -#define BT_CONN_US_TO_INTERVAL(t) ((uint16_t)((t) * 4U / 5U / USEC_PER_MSEC)) - #if (CONFIG_BT_CTLR_CENTRAL_SPACING == 0) -#define CONN_INTERVAL_MIN BT_CONN_US_TO_INTERVAL(ISO_INTERVAL_US) +#define CONN_INTERVAL_MIN_MS ISO_INTERVAL_MS #else /* CONFIG_BT_CTLR_CENTRAL_SPACING > 0 */ -#define CONN_INTERVAL_MIN BT_CONN_US_TO_INTERVAL(ISO_INTERVAL_US * CONFIG_BT_MAX_CONN) +#define CONN_INTERVAL_MIN_MS (ISO_INTERVAL_MS * CONFIG_BT_MAX_CONN) #endif /* CONFIG_BT_CTLR_CENTRAL_SPACING > 0 */ +#define CONN_INTERVAL_MAX_MS CONN_INTERVAL_MIN_MS -#define CONN_INTERVAL_MAX CONN_INTERVAL_MIN -#define CONN_TIMEOUT MAX((BT_CONN_INTERVAL_TO_MS(CONN_INTERVAL_MAX) * 6U / 10U), 10U) +#define CONN_INTERVAL_MIN BT_GAP_MS_TO_CONN_INTERVAL(CONN_INTERVAL_MIN_MS) +#define CONN_INTERVAL_MAX BT_GAP_MS_TO_CONN_INTERVAL(CONN_INTERVAL_MAX_MS) +#define CONN_TIMEOUT BT_GAP_MS_TO_CONN_TIMEOUT(MAX((CONN_INTERVAL_MAX_MS * 6U), 100U)) -#define ADV_INTERVAL_MIN 0x0020 -#define ADV_INTERVAL_MAX 0x0020 +#define ADV_INTERVAL_MIN BT_GAP_MS_TO_ADV_INTERVAL(20) +#define ADV_INTERVAL_MAX BT_GAP_MS_TO_ADV_INTERVAL(20) #define BT_CONN_LE_CREATE_CONN_CUSTOM \ BT_CONN_LE_CREATE_PARAM(BT_CONN_LE_OPT_NONE, \