Skip to content

Conversation

@chengkai15
Copy link
Contributor

@chengkai15 chengkai15 commented Oct 22, 2025

Bluetooth: Add connection type helper functions and refactor type checks

Summary

This PR introduces dedicated helper functions for connection type checking and refactors the entire Bluetooth host stack to use these new functions instead of direct conn->type comparisons. This improves code consistency, readability, and maintainability.

Changes

  • New Helper Functions: Added four inline helper functions for connection type checking:

    • bt_conn_is_br() - Check for BR/EDR connections
    • bt_conn_is_le() - Check for LE connections
    • bt_conn_is_iso() - Check for ISO connections
    • bt_conn_is_sco() - Check for SCO connections
  • Comprehensive Refactoring: Updated connection type checks throughout the Bluetooth host stack:

    • Connection management core (conn.c)
    • Internal connection headers (conn_internal.h)
    • Packet handling and state management
    • Security and encryption operations
    • Connection lookup functions

Technical Details

  • Helper functions provide proper configuration checks using IS_ENABLED() macros
  • When features are disabled (e.g., CONFIG_BT_CLASSIC=n), functions return false at compile time
  • Eliminates redundant IS_ENABLED() checks scattered throughout the code
  • Improves compiler optimization opportunities through inline functions

Benefits

  • Code Consistency: Uniform connection type checking across entire host stack
  • Maintainability: Easier to modify and extend connection type logic
  • Readability: Clearer intent when checking connection types
  • Configuration Safety: Proper handling of disabled features
  • Compiler Optimization: Better dead code elimination when features are disabled

Impact

  • No functional changes - purely a code refactoring
  • Improves long-term maintainability of Bluetooth host stack
  • Sets consistent pattern for future connection type checks
  • Minimal risk change with significant code quality benefits

Testing

  • Verified compilation for all supported Bluetooth configurations
  • Confirmed no functional changes in connection establishment and management
  • Tested BR/EDR, LE, ISO, and SCO connection scenarios
  • Validated security procedures and packet handling

This refactoring establishes a clean, consistent pattern for connection type verification that will make future Bluetooth feature development more straightforward and less error-prone.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors connection type checks throughout the Bluetooth host stack by replacing direct conn->type comparisons with the bt_conn_is_type() macro. The changes maintain identical functionality while improving code consistency and maintainability across all Bluetooth subsystems.

Key changes:

  • Replaces direct conn->type == BT_CONN_TYPE_* comparisons with bt_conn_is_type(conn, BT_CONN_TYPE_*) macro calls
  • Moves the bt_conn_is_type() function definition earlier in conn.c (from line 2950 to line 217) to support its use throughout the file
  • Updates connection type checks across connection management, L2CAP, SMP, and HCI core modules

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
subsys/bluetooth/host/conn.c Moves bt_conn_is_type() definition and refactors type checks in connection management, security operations, and lookup functions
subsys/bluetooth/host/conn_internal.h Updates ISO connection handle validation to use the macro
subsys/bluetooth/host/hci_core.c Refactors type checks in HCI event handlers for disconnection, encryption, and security operations
subsys/bluetooth/host/l2cap.c Updates L2CAP layer type checks for BR/EDR and LE connection differentiation
subsys/bluetooth/host/smp.c Refactors SMP security manager type checks for LE and BR/EDR connections

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@chengkai15 chengkai15 force-pushed the conn_type branch 4 times, most recently from bcc3df8 to 6b26eb5 Compare October 22, 2025 15:29
@chengkai15 chengkai15 requested a review from jhedberg October 22, 2025 15:44
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 <[email protected]>
@@ -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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!is_acl_conn(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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!is_acl_conn(conn))

Copy link
Contributor

@gzh-terry gzh-terry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good to me.

@sonarqubecloud
Copy link

@kartben kartben merged commit 038523c into zephyrproject-rtos:main Oct 24, 2025
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: Bluetooth Host Bluetooth Host (excluding BR/EDR) area: Bluetooth

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants