Skip to main content

Module l2cap

Module l2cap 

Source
Expand description

L2CAP connection-oriented channels, as Bluetooth sockets.

Linux exposes L2CAP directly through the socket API — AF_BLUETOOTH with BTPROTO_L2CAP — so this needs neither BlueZ nor D-Bus. That is a much shorter path than the Apple side, where the same thing arrives as a pair of runloop-driven NSStreams.

  socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)
  connect(&sockaddr_l2 { psm, bdaddr, bdaddr_type })
  read / write

One detail that silently breaks everything if missed: a Bluetooth address goes on the wire least-significant byte first, so AA:BB:CC:DD:EE:FF is the bytes FF EE DD CC BB AA. And an LE channel must name the peer’s address type — connecting to a random address as though it were public simply times out.

And the socket is SOCK_SEQPACKET: each write is one SDU, which may not exceed the peer’s MTU (BT_SNDMTU; EMSGSIZE otherwise), and each read returns one SDU, cut short if the buffer is smaller. So writes are split at the send MTU and the reader’s buffer is at least the receive MTU. The portable channel is a byte stream; SDU boundaries mean nothing to callers.

Structs§

Channel
An open L2CAP channel.
L2capListener
Listener for inbound LE L2CAP connection-oriented channels.
L2capListenerHandle
Handle used to stop a listener whose stream is being routed elsewhere.

Enums§

Closed
Why a channel closed.

Traits§

ChannelSink
Where a channel’s inbound bytes go.

Functions§

listen
Bind and listen on an LE L2CAP PSM. Pass zero to let the kernel allocate it (from the dynamic range, 0x80–0xFF). With encryption_required, a channel only opens on an encrypted link; the security level is set on the listener and accepted sockets inherit it.
open
Open a channel to address on psm.

Type Aliases§

L2capChannel
This platform’s channel, wrapped in the portable one.
Psm
A PSM — the L2CAP equivalent of a port number.