From b7556a0a72064cd44fb6c3ad4d39fe068162ae7f Mon Sep 17 00:00:00 2001 From: Kai Cheng Date: Wed, 22 Oct 2025 21:26:25 +0800 Subject: [PATCH] Bluetooth: Conn: add connection type helper functions Introduce dedicated helper functions for connection type checking: - bt_conn_is_br() for BR/EDR connections - bt_conn_is_le() for LE connections - bt_conn_is_iso() for ISO connections - bt_conn_is_sco() for SCO connections Replace direct conn->type comparisons with these new helper functions throughout the connection management code. This improves code readability, maintainability, and provides proper configuration checks for each connection type. Signed-off-by: Kai Cheng --- subsys/bluetooth/host/conn.c | 144 ++++++++++++-------------- subsys/bluetooth/host/conn_internal.h | 35 ++++++- 2 files changed, 97 insertions(+), 82 deletions(-) diff --git a/subsys/bluetooth/host/conn.c b/subsys/bluetooth/host/conn.c index 9b6c961cfae6f..e12df2b63aeb0 100644 --- a/subsys/bluetooth/host/conn.c +++ b/subsys/bluetooth/host/conn.c @@ -217,7 +217,7 @@ int bt_conn_iso_init(void) struct k_sem *bt_conn_get_pkts(struct bt_conn *conn) { #if defined(CONFIG_BT_CLASSIC) - if (conn->type == BT_CONN_TYPE_BR || !bt_dev.le.acl_mtu) { + if (bt_conn_is_br(conn) || !bt_dev.le.acl_mtu) { return &bt_dev.br.pkts; } #endif /* CONFIG_BT_CLASSIC */ @@ -226,7 +226,7 @@ struct k_sem *bt_conn_get_pkts(struct bt_conn *conn) /* Use ISO pkts semaphore if LE Read Buffer Size command returned * dedicated ISO buffers. */ - if (conn->type == BT_CONN_TYPE_ISO) { + if (bt_conn_is_iso(conn)) { if (bt_dev.le.iso_mtu && bt_dev.le.iso_limit != 0) { return &bt_dev.le.iso_pkts; } @@ -470,7 +470,7 @@ static void bt_acl_recv(struct bt_conn *conn, struct net_buf *buf, return; } - if ((conn->type != BT_CONN_TYPE_BR) && (conn->rx->len > acl_total_len)) { + if (!bt_conn_is_br(conn) && (conn->rx->len > acl_total_len)) { LOG_ERR("ACL len mismatch (%u > %u)", conn->rx->len, acl_total_len); bt_conn_reset_rx_state(conn); return; @@ -481,7 +481,7 @@ static void bt_acl_recv(struct bt_conn *conn, struct net_buf *buf, conn->rx = NULL; LOG_DBG("Successfully parsed %u byte L2CAP packet", buf->len); - if (IS_ENABLED(CONFIG_BT_CLASSIC) && (conn->type == BT_CONN_TYPE_BR)) { + if (bt_conn_is_br(conn)) { bt_br_acl_recv(conn, buf, true); } else { bt_l2cap_recv(conn, buf, true); @@ -500,7 +500,7 @@ void bt_conn_recv(struct bt_conn *conn, struct net_buf *buf, uint8_t flags) LOG_DBG("handle %u len %u flags %02x", conn->handle, buf->len, flags); - if (IS_ENABLED(CONFIG_BT_ISO_RX) && conn->type == BT_CONN_TYPE_ISO) { + if (IS_ENABLED(CONFIG_BT_ISO_RX) && bt_conn_is_iso(conn)) { bt_iso_recv(conn, buf, flags); return; } else if (IS_ENABLED(CONFIG_BT_CONN)) { @@ -618,13 +618,12 @@ static int send_iso(struct bt_conn *conn, struct net_buf *buf, uint8_t flags) static inline uint16_t conn_mtu(struct bt_conn *conn) { #if defined(CONFIG_BT_CLASSIC) - if (conn->type == BT_CONN_TYPE_BR || - (conn->type != BT_CONN_TYPE_ISO && !bt_dev.le.acl_mtu)) { + if (bt_conn_is_br(conn) || (!bt_conn_is_iso(conn) && !bt_dev.le.acl_mtu)) { return bt_dev.br.mtu; } #endif /* CONFIG_BT_CLASSIC */ #if defined(CONFIG_BT_ISO) - if (conn->type == BT_CONN_TYPE_ISO) { + if (bt_conn_is_iso(conn)) { return bt_dev.le.iso_mtu; } #endif /* CONFIG_BT_ISO */ @@ -635,26 +634,14 @@ static inline uint16_t conn_mtu(struct bt_conn *conn) #endif /* CONFIG_BT_CONN */ } -static bool is_classic_conn(struct bt_conn *conn) -{ - return (IS_ENABLED(CONFIG_BT_CLASSIC) && - conn->type == BT_CONN_TYPE_BR); -} - static bool is_iso_tx_conn(struct bt_conn *conn) { - return IS_ENABLED(CONFIG_BT_ISO_TX) && - conn->type == BT_CONN_TYPE_ISO; -} - -static bool is_le_conn(struct bt_conn *conn) -{ - return IS_ENABLED(CONFIG_BT_CONN) && conn->type == BT_CONN_TYPE_LE; + return IS_ENABLED(CONFIG_BT_ISO_TX) && bt_conn_is_iso(conn); } static bool is_acl_conn(struct bt_conn *conn) { - return is_le_conn(conn) || is_classic_conn(conn); + return bt_conn_is_le(conn) || bt_conn_is_br(conn); } static int send_buf(struct bt_conn *conn, struct net_buf *buf, @@ -1175,13 +1162,12 @@ void bt_conn_set_state(struct bt_conn *conn, bt_conn_state_t state) * bt_conn_add_le() and keep it until reaching DISCONNECTED * again. */ - if (conn->type != BT_CONN_TYPE_ISO) { + if (!bt_conn_is_iso(conn)) { bt_conn_ref(conn); } break; case BT_CONN_INITIATING: - if (IS_ENABLED(CONFIG_BT_CENTRAL) && - conn->type == BT_CONN_TYPE_LE) { + if (IS_ENABLED(CONFIG_BT_CENTRAL) && bt_conn_is_le(conn)) { k_work_cancel_delayable(&conn->deferred_work); } break; @@ -1192,7 +1178,7 @@ void bt_conn_set_state(struct bt_conn *conn, bt_conn_state_t state) /* Actions needed for entering the new state */ switch (conn->state) { case BT_CONN_CONNECTED: - if (conn->type == BT_CONN_TYPE_SCO) { + if (bt_conn_is_sco(conn)) { if (IS_ENABLED(CONFIG_BT_CLASSIC)) { bt_sco_connected(conn); } @@ -1200,8 +1186,7 @@ void bt_conn_set_state(struct bt_conn *conn, bt_conn_state_t state) } k_poll_signal_raise(&conn_change, 0); - if (IS_ENABLED(CONFIG_BT_ISO) && - conn->type == BT_CONN_TYPE_ISO) { + if (bt_conn_is_iso(conn)) { bt_iso_connected(conn); break; } @@ -1213,7 +1198,7 @@ void bt_conn_set_state(struct bt_conn *conn, bt_conn_state_t state) conn->role == BT_CONN_ROLE_PERIPHERAL) { #if defined(CONFIG_BT_GAP_AUTO_UPDATE_CONN_PARAMS) - if (conn->type == BT_CONN_TYPE_LE) { + if (bt_conn_is_le(conn)) { conn->le.conn_param_retry_countdown = CONFIG_BT_CONN_PARAM_RETRY_COUNT; } @@ -1227,7 +1212,7 @@ void bt_conn_set_state(struct bt_conn *conn, bt_conn_state_t state) break; case BT_CONN_DISCONNECTED: #if defined(CONFIG_BT_CONN) - if (conn->type == BT_CONN_TYPE_SCO) { + if (bt_conn_is_sco(conn)) { if (IS_ENABLED(CONFIG_BT_CLASSIC)) { bt_sco_disconnected(conn); } @@ -1324,15 +1309,14 @@ void bt_conn_set_state(struct bt_conn *conn, bt_conn_state_t state) case BT_CONN_ADV_DIR_CONNECTABLE: break; case BT_CONN_INITIATING: - if (conn->type == BT_CONN_TYPE_SCO) { + if (bt_conn_is_sco(conn)) { break; } /* * Timer is needed only for LE. For other link types controller * will handle connection timeout. */ - if (IS_ENABLED(CONFIG_BT_CENTRAL) && - conn->type == BT_CONN_TYPE_LE && + if (IS_ENABLED(CONFIG_BT_CENTRAL) && bt_conn_is_le(conn) && bt_dev.create_param.timeout != 0) { k_work_schedule(&conn->deferred_work, K_MSEC(10 * bt_dev.create_param.timeout)); @@ -1930,21 +1914,21 @@ int bt_conn_disconnect(struct bt_conn *conn, uint8_t reason) } return 0; case BT_CONN_INITIATING: - if (conn->type == BT_CONN_TYPE_LE) { + if (bt_conn_is_le(conn)) { if (IS_ENABLED(CONFIG_BT_CENTRAL)) { k_work_cancel_delayable(&conn->deferred_work); return bt_le_create_conn_cancel(); } } #if defined(CONFIG_BT_ISO) - else if (conn->type == BT_CONN_TYPE_ISO) { + else if (bt_conn_is_iso(conn)) { return conn_disconnect(conn, reason); } #endif /* CONFIG_BT_ISO */ #if defined(CONFIG_BT_CLASSIC) - else if (conn->type == BT_CONN_TYPE_BR) { + else if (bt_conn_is_br(conn)) { return bt_hci_connect_br_cancel(conn); - } else if (conn->type == BT_CONN_TYPE_SCO) { + } else if (bt_conn_is_sco(conn)) { /* There is no HCI cmd to cancel SCO connecting from spec */ return -EPROTONOSUPPORT; } @@ -2225,7 +2209,7 @@ static void deferred_work(struct k_work *work) #if defined(CONFIG_BT_ISO_UNICAST) struct bt_conn *iso; - if (conn->type == BT_CONN_TYPE_ISO) { + if (bt_conn_is_iso(conn)) { /* bt_iso_disconnected is responsible for unref'ing the * connection pointer, as it is conditional on whether * the connection is a central or peripheral. @@ -2285,7 +2269,7 @@ static void deferred_work(struct k_work *work) return; } - if (conn->type != BT_CONN_TYPE_LE) { + if (!bt_conn_is_le(conn)) { return; } @@ -2369,7 +2353,7 @@ struct bt_conn *bt_conn_lookup_addr_sco(const bt_addr_t *peer) continue; } - if (conn->type != BT_CONN_TYPE_SCO) { + if (!bt_conn_is_sco(conn)) { bt_conn_unref(conn); continue; } @@ -2401,7 +2385,7 @@ struct bt_conn *bt_conn_lookup_addr_br(const bt_addr_t *peer) continue; } - if (conn->type != BT_CONN_TYPE_BR) { + if (!bt_conn_is_br(conn)) { bt_conn_unref(conn); continue; } @@ -2544,7 +2528,7 @@ int bt_conn_le_start_encryption(struct bt_conn *conn, uint8_t rand[8], #if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_CLASSIC) uint8_t bt_conn_enc_key_size(const struct bt_conn *conn) { - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE | BT_CONN_TYPE_BR)) { + if (!bt_conn_is_le(conn) && !bt_conn_is_br(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return 0; } @@ -2554,7 +2538,7 @@ uint8_t bt_conn_enc_key_size(const struct bt_conn *conn) } #if defined(CONFIG_BT_CLASSIC) - if (conn->type == BT_CONN_TYPE_BR) { + if (bt_conn_is_br(conn)) { return conn->br.link_key ? conn->br.link_key->enc_key_size : 0; } #endif /* CONFIG_BT_CLASSIC */ @@ -2569,7 +2553,7 @@ uint8_t bt_conn_enc_key_size(const struct bt_conn *conn) static void reset_pairing(struct bt_conn *conn) { #if defined(CONFIG_BT_CLASSIC) - if (conn->type == BT_CONN_TYPE_BR) { + if (bt_conn_is_br(conn)) { atomic_clear_bit(conn->flags, BT_CONN_BR_PAIRING); atomic_clear_bit(conn->flags, BT_CONN_BR_PAIRED); atomic_clear_bit(conn->flags, BT_CONN_BR_PAIRING_INITIATOR); @@ -2605,12 +2589,12 @@ void bt_conn_security_changed(struct bt_conn *conn, uint8_t hci_err, #if defined(CONFIG_BT_KEYS_OVERWRITE_OLDEST) if (!err && conn->sec_level >= BT_SECURITY_L2) { - if (conn->type == BT_CONN_TYPE_LE) { + if (bt_conn_is_le(conn)) { bt_keys_update_usage(conn->id, bt_conn_get_dst(conn)); } #if defined(CONFIG_BT_CLASSIC) - if (conn->type == BT_CONN_TYPE_BR) { + if (bt_conn_is_br(conn)) { bt_keys_link_key_update_usage(&conn->br.dst); } #endif /* CONFIG_BT_CLASSIC */ @@ -2621,7 +2605,7 @@ void bt_conn_security_changed(struct bt_conn *conn, uint8_t hci_err, static int start_security(struct bt_conn *conn) { - if (IS_ENABLED(CONFIG_BT_CLASSIC) && conn->type == BT_CONN_TYPE_BR) { + if (bt_conn_is_br(conn)) { return bt_ssp_start_security(conn); } @@ -2637,7 +2621,7 @@ int bt_conn_set_security(struct bt_conn *conn, bt_security_t sec) bool force_pair; int err; - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE | BT_CONN_TYPE_BR)) { + if (!bt_conn_is_le(conn) && !bt_conn_is_br(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return -EINVAL; } @@ -2677,7 +2661,7 @@ int bt_conn_set_security(struct bt_conn *conn, bt_security_t sec) bt_security_t bt_conn_get_security(const struct bt_conn *conn) { - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE | BT_CONN_TYPE_BR)) { + if (!bt_conn_is_le(conn) && !bt_conn_is_br(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return BT_SECURITY_L0; } @@ -2795,7 +2779,7 @@ struct bt_conn *bt_conn_lookup_addr_le(uint8_t id, const bt_addr_le_t *peer) continue; } - if (conn->type != BT_CONN_TYPE_LE) { + if (!bt_conn_is_le(conn)) { bt_conn_unref(conn); continue; } @@ -2823,7 +2807,7 @@ struct bt_conn *bt_conn_lookup_state_le(uint8_t id, const bt_addr_le_t *peer, continue; } - if (conn->type != BT_CONN_TYPE_LE) { + if (!bt_conn_is_le(conn)) { bt_conn_unref(conn); continue; } @@ -2846,7 +2830,7 @@ struct bt_conn *bt_conn_lookup_state_le(uint8_t id, const bt_addr_le_t *peer, const bt_addr_le_t *bt_conn_get_dst(const struct bt_conn *conn) { - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) { + if (!bt_conn_is_le(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return NULL; } @@ -2961,7 +2945,7 @@ bool bt_conn_is_type(const struct bt_conn *conn, enum bt_conn_type type) int bt_conn_get_remote_info(const struct bt_conn *conn, struct bt_conn_remote_info *remote_info) { - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE | BT_CONN_TYPE_BR)) { + if (!bt_conn_is_le(conn) && !bt_conn_is_br(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return -EINVAL; } @@ -3056,7 +3040,7 @@ int bt_conn_le_enhanced_get_tx_power_level(struct bt_conn *conn, struct bt_hci_cp_le_read_tx_power_level *cp; struct net_buf *buf; - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) { + if (!bt_conn_is_le(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return -EINVAL; } @@ -3094,7 +3078,7 @@ int bt_conn_le_get_remote_tx_power_level(struct bt_conn *conn, struct bt_hci_cp_le_read_tx_power_level *cp; struct net_buf *buf; - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) { + if (!bt_conn_is_le(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return -EINVAL; } @@ -3122,7 +3106,7 @@ int bt_conn_le_set_tx_power_report_enable(struct bt_conn *conn, struct bt_hci_cp_le_set_tx_power_report_enable *cp; struct net_buf *buf; - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) { + if (!bt_conn_is_le(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return -EINVAL; } @@ -3148,7 +3132,7 @@ int bt_conn_le_get_tx_power_level(struct bt_conn *conn, { int err; - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) { + if (!bt_conn_is_le(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return -EINVAL; } @@ -3196,7 +3180,7 @@ int bt_conn_le_set_path_loss_mon_param(struct bt_conn *conn, struct bt_hci_cp_le_set_path_loss_reporting_parameters *cp; struct net_buf *buf; - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) { + if (!bt_conn_is_le(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return -EINVAL; } @@ -3222,7 +3206,7 @@ int bt_conn_le_set_path_loss_mon_enable(struct bt_conn *conn, bool reporting_ena struct bt_hci_cp_le_set_path_loss_reporting_enable *cp; struct net_buf *buf; - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) { + if (!bt_conn_is_le(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return -EINVAL; } @@ -3321,7 +3305,7 @@ int bt_conn_le_subrate_request(struct bt_conn *conn, struct bt_hci_cp_le_subrate_request *cp; struct net_buf *buf; - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) { + if (!bt_conn_is_le(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return -EINVAL; } @@ -3370,7 +3354,7 @@ int bt_conn_le_read_all_remote_features(struct bt_conn *conn, uint8_t pages_requ struct bt_hci_cp_le_read_all_remote_features *cp; struct net_buf *buf; - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) { + if (!bt_conn_is_le(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return -EINVAL; } @@ -3439,7 +3423,7 @@ int bt_conn_le_frame_space_update(struct bt_conn *conn, struct bt_hci_cp_le_frame_space_update *cp; struct net_buf *buf; - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) { + if (!bt_conn_is_le(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return -EINVAL; } @@ -3581,7 +3565,7 @@ void bt_conn_notify_cs_subevent_result(struct bt_conn *conn, int bt_conn_le_param_update(struct bt_conn *conn, const struct bt_le_conn_param *param) { - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) { + if (!bt_conn_is_le(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return -EINVAL; } @@ -3615,7 +3599,7 @@ int bt_conn_le_param_update(struct bt_conn *conn, int bt_conn_le_data_len_update(struct bt_conn *conn, const struct bt_conn_le_data_len_param *param) { - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) { + if (!bt_conn_is_le(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return -EINVAL; } @@ -3635,7 +3619,7 @@ int bt_conn_le_phy_update(struct bt_conn *conn, { uint8_t phy_opts, all_phys; - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) { + if (!bt_conn_is_le(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return -EINVAL; } @@ -4069,7 +4053,7 @@ int bt_conn_auth_cb_register(const struct bt_conn_auth_cb *cb) #if defined(CONFIG_BT_SMP) int bt_conn_auth_cb_overlay(struct bt_conn *conn, const struct bt_conn_auth_cb *cb) { - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE | BT_CONN_TYPE_BR)) { + if (!bt_conn_is_le(conn) && !bt_conn_is_br(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return -EINVAL; } @@ -4083,7 +4067,7 @@ int bt_conn_auth_cb_overlay(struct bt_conn *conn, const struct bt_conn_auth_cb * return -EINVAL; } - if (conn->type == BT_CONN_TYPE_LE) { + if (bt_conn_is_le(conn)) { return bt_smp_auth_cb_overlay(conn, cb); } @@ -4121,16 +4105,16 @@ int bt_conn_auth_info_cb_unregister(struct bt_conn_auth_info_cb *cb) int bt_conn_auth_passkey_entry(struct bt_conn *conn, unsigned int passkey) { - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE | BT_CONN_TYPE_BR)) { + if (!bt_conn_is_le(conn) && !bt_conn_is_br(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return -EINVAL; } - if (IS_ENABLED(CONFIG_BT_SMP) && conn->type == BT_CONN_TYPE_LE) { + if (IS_ENABLED(CONFIG_BT_SMP) && bt_conn_is_le(conn)) { return bt_smp_auth_passkey_entry(conn, passkey); } - if (IS_ENABLED(CONFIG_BT_CLASSIC) && conn->type == BT_CONN_TYPE_BR) { + if (bt_conn_is_br(conn)) { if (!bt_auth) { return -EINVAL; } @@ -4145,7 +4129,7 @@ int bt_conn_auth_passkey_entry(struct bt_conn *conn, unsigned int passkey) int bt_conn_auth_keypress_notify(struct bt_conn *conn, enum bt_conn_auth_keypress type) { - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE)) { + if (!bt_conn_is_le(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return -EINVAL; } @@ -4161,16 +4145,16 @@ int bt_conn_auth_keypress_notify(struct bt_conn *conn, int bt_conn_auth_passkey_confirm(struct bt_conn *conn) { - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE | BT_CONN_TYPE_BR)) { + if (!bt_conn_is_le(conn) && !bt_conn_is_br(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return -EINVAL; } - if (IS_ENABLED(CONFIG_BT_SMP) && conn->type == BT_CONN_TYPE_LE) { + if (IS_ENABLED(CONFIG_BT_SMP) && bt_conn_is_le(conn)) { return bt_smp_auth_passkey_confirm(conn); } - if (IS_ENABLED(CONFIG_BT_CLASSIC) && conn->type == BT_CONN_TYPE_BR) { + if (bt_conn_is_br(conn)) { if (!bt_auth) { return -EINVAL; } @@ -4183,16 +4167,16 @@ int bt_conn_auth_passkey_confirm(struct bt_conn *conn) int bt_conn_auth_cancel(struct bt_conn *conn) { - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE | BT_CONN_TYPE_BR)) { + if (!bt_conn_is_le(conn) && !bt_conn_is_br(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return -EINVAL; } - if (IS_ENABLED(CONFIG_BT_SMP) && conn->type == BT_CONN_TYPE_LE) { + if (IS_ENABLED(CONFIG_BT_SMP) && bt_conn_is_le(conn)) { return bt_smp_auth_cancel(conn); } - if (IS_ENABLED(CONFIG_BT_CLASSIC) && conn->type == BT_CONN_TYPE_BR) { + if (bt_conn_is_br(conn)) { if (!bt_auth) { return -EINVAL; } @@ -4205,16 +4189,16 @@ int bt_conn_auth_cancel(struct bt_conn *conn) int bt_conn_auth_pairing_confirm(struct bt_conn *conn) { - if (!bt_conn_is_type(conn, BT_CONN_TYPE_LE | BT_CONN_TYPE_BR)) { + if (!bt_conn_is_le(conn) && !bt_conn_is_br(conn)) { LOG_DBG("Invalid connection type: %u for %p", conn->type, conn); return -EINVAL; } - if (IS_ENABLED(CONFIG_BT_SMP) && conn->type == BT_CONN_TYPE_LE) { + if (IS_ENABLED(CONFIG_BT_SMP) && bt_conn_is_le(conn)) { return bt_smp_auth_pairing_confirm(conn); } - if (IS_ENABLED(CONFIG_BT_CLASSIC) && conn->type == BT_CONN_TYPE_BR) { + if (bt_conn_is_br(conn)) { if (!bt_auth) { return -EINVAL; } diff --git a/subsys/bluetooth/host/conn_internal.h b/subsys/bluetooth/host/conn_internal.h index 87393922a93b6..f4401f42c8e84 100644 --- a/subsys/bluetooth/host/conn_internal.h +++ b/subsys/bluetooth/host/conn_internal.h @@ -368,6 +368,38 @@ static inline void *closure_data(void *storage) return ((struct closure *)storage)->data; } +#if defined(CONFIG_BT_CLASSIC) +static inline bool bt_conn_is_br(const struct bt_conn *conn) +{ + return conn->type == BT_CONN_TYPE_BR; +} +#else +#define bt_conn_is_br(conn) (false) +#endif + +static inline bool bt_conn_is_le(const struct bt_conn *conn) +{ + return conn->type == BT_CONN_TYPE_LE; +} + +#if defined(CONFIG_BT_ISO) +static inline bool bt_conn_is_iso(const struct bt_conn *conn) +{ + return conn->type == BT_CONN_TYPE_ISO; +} +#else +#define bt_conn_is_iso(conn) (false) +#endif + +#if defined(CONFIG_BT_CLASSIC) +static inline bool bt_conn_is_sco(const struct bt_conn *conn) +{ + return conn->type == BT_CONN_TYPE_SCO; +} +#else +#define bt_conn_is_sco(conn) (false) +#endif + void bt_conn_tx_notify(struct bt_conn *conn, bool wait_for_completion); void bt_conn_reset_rx_state(struct bt_conn *conn); @@ -447,8 +479,7 @@ static inline bool bt_conn_is_handle_valid(struct bt_conn *conn) return true; case BT_CONN_INITIATING: /* ISO connection handle assigned at connect state */ - if (IS_ENABLED(CONFIG_BT_ISO) && - conn->type == BT_CONN_TYPE_ISO) { + if (bt_conn_is_iso(conn)) { return true; } __fallthrough;