Skip to content

Support for Bluetooth LE L2CAP Connection-oriented channels through AF_BLUETOOTH/BTPROTO_L2CAP socket #129288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
themadinventor opened this issue Jan 25, 2025 · 2 comments
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement

Comments

@themadinventor
Copy link
Contributor

themadinventor commented Jan 25, 2025

Feature or enhancement

Proposal:

As has been discussed previously (69242, 70145), "new" fields have been added in sockaddr_l2, but are still not supported in CPython's socket implementation.

  • The lack of l2_bdaddr_type prevents opening L2CAP connection-oriented channels to Bluetooth LE devices.
  • Likewise, the missing l2_cid prevents raw access to e.g ATT.

My suggestion is to add support for a third and fourth optional element in the address tuple for l2_cid and l2_bdaddr_type respectively.

  • Only one of psm and cid can be non-zero. This is enforced by the kernel.
  • l2_bdaddr_type can take the value of BDADDR_LE_PUBLIC or BDADDR_LE_RANDOM. BDADDR_BREDR is implied if the element is missing, preserving compatibility with existing code.

I.e for LE CoC connection to PSM 0x80

sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)
sock.connect((bdaddr, 0x80, 0, BDADDR_LE_RANDOM))

For a raw LE ATT connection:

CID_ATT = 0x04
sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)
s.bind((BDADDR_ANY, 0, CID_ATT, BDADDR_LE_RANDOM))
s.connect((bdaddr, 0, CID_ATT, BDADDR_LE_RANDOM))

while keeping the existing format for classic BR/EDR L2CAP.

sock.connect((bdaddr, 0x80))

I have a working implementation of this locally (tested on Linux 6.11) and will file a PR for review as soon as I've got this issue number assigned.

This is my first attempt at contributing to CPython, so I apologize in advance for any mistakes in the process.

Edit 2025-01-26: Added CID field to proposal. PR to be updated.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

#69242
#70145

Linked PRs

@themadinventor themadinventor added the type-feature A feature request or enhancement label Jan 25, 2025
themadinventor added a commit to themadinventor/cpython that referenced this issue Jan 25, 2025
…ress tuple

To be able to connect L2CAP sockets to Bluetooth LE devices, the l2_bdaddr_type must be set to BDADDR_LE_PUBLIC or BDADDR_LE_RANDOM.
This change adds support for providing the l2_bdaddr_type as an optional, traliing element in the address tuple passed to connect()
themadinventor added a commit to themadinventor/cpython that referenced this issue Jan 25, 2025
… socket.BDADDR_RANDOM only if defined for target

Add check that BDADDR_BREDR is defined as Windows build may set USE_BLUETOOTH but not BDADDR_*
themadinventor added a commit to themadinventor/cpython that referenced this issue Jan 26, 2025
…ss tuple

This change adds support for the CID field in the socket address tuple. This allows e.g raw LE ATT connections.
themadinventor added a commit to themadinventor/cpython that referenced this issue Jan 26, 2025
@picnixz picnixz added the extension-modules C modules in the Modules dir label Jan 27, 2025
themadinventor added a commit to themadinventor/cpython that referenced this issue Jan 27, 2025
themadinventor added a commit to themadinventor/cpython that referenced this issue Feb 23, 2025
…address fields

Update documentation to mention the new cid and bdaddr_type fields for BTPROTO_L2CAP connections.
themadinventor added a commit to themadinventor/cpython that referenced this issue Feb 23, 2025
@themadinventor
Copy link
Contributor Author

Kindly reminding about this PR, which I hope is uncontroversial and quick to review :)

(The docs says it's ok to ping after one month of no feedback, to make sure it's not forgotten [1].)

[1] https://devguide.python.org/getting-started/pull-request-lifecycle/#reviewing

themadinventor added a commit to themadinventor/cpython that referenced this issue Feb 25, 2025
themadinventor added a commit to themadinventor/cpython that referenced this issue Feb 25, 2025
themadinventor added a commit to themadinventor/cpython that referenced this issue Feb 25, 2025
themadinventor added a commit to themadinventor/cpython that referenced this issue Feb 25, 2025
BTPROTO_L2CAP extenstion: Expand the docs & provide link targets for the constants
themadinventor added a commit to themadinventor/cpython that referenced this issue Feb 25, 2025
themadinventor added a commit to themadinventor/cpython that referenced this issue Feb 26, 2025
themadinventor added a commit to themadinventor/cpython that referenced this issue Feb 26, 2025
encukou pushed a commit that referenced this issue Feb 27, 2025
…cket address tuple (#129293)

Add two optional, traling elements in the AF_BLUETOOTH socket address tuple:

- l2_cid, to allow e.g raw LE ATT connections
- l2_bdaddr_type. To be able to connect L2CAP sockets to Bluetooth LE devices,
  the l2_bdaddr_type must be set to BDADDR_LE_PUBLIC or BDADDR_LE_RANDOM.
@encukou
Copy link
Member

encukou commented Feb 27, 2025

Thank you for the addition!

@encukou encukou closed this as completed Feb 27, 2025
seehwan pushed a commit to seehwan/cpython that referenced this issue Apr 16, 2025
…CAP socket address tuple (python#129293)

Add two optional, traling elements in the AF_BLUETOOTH socket address tuple:

- l2_cid, to allow e.g raw LE ATT connections
- l2_bdaddr_type. To be able to connect L2CAP sockets to Bluetooth LE devices,
  the l2_bdaddr_type must be set to BDADDR_LE_PUBLIC or BDADDR_LE_RANDOM.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants