RFCOMM(7)

πŸ’– RFCOMM(7) πŸ’–

okay so this is the rfcomm page from Linux System Administration β€” RFCOMM protocol edition, and yes bestie, we’re getting into the Bluetooth serial-port emulation lore. πŸ¦‹


🌸 NAME 🌸

rfcomm β€” RFCOMM protocol


✨ SYNOPSIS ✨

#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>

rfcomm_socket = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);

πŸ’« DESCRIPTION πŸ’«

The RFCOMM protocol gives you serial port emulation over the L2CAP(7) protocol. It’s based on the ETSI standard TS 07.10. No cap, that’s literally the foundation. πŸŽ€

RFCOMM is a simple transport protocol, but it also has extra provisions for emulating the 9 circuits of RS-232 (EIATIA-232-E) serial ports. It’s giving β€œserial ports but make it Bluetooth.” πŸ’…


πŸ“¬ SOCKET ADDRESS πŸ“¬

struct sockaddr_rc {
    sa_family_t rc_family;
    unsigned short rc_bdaddr;
    unsigned char rc_channel;
};

Example:

struct sockaddr_rc addr;

memset(&addr, 0, sizeof(addr));
addr.rc_family = AF_BLUETOOTH;
bacpy(&addr.rc_bdaddr, bdaddr);
addr.rc_channel = channel;

πŸ› οΈ SOCKET OPTIONS πŸ› οΈ

The socket options listed below can be set with setsockopt(2) and read with getsockopt(2), with the socket level set to SOL_BLUETOOTH. Slay. πŸ”₯

πŸ” BT_SECURITY (since Linux 2.6.30) πŸ”

Channel security level β€” possible values, bestie:

  • BT_SECURITY_SDP β€” Security Level: 0 (SDP Only); Link Key Type: None; Encryption: Not required.
  • BT_SECURITY_LOW β€” Security Level: 1 (Low); Link Key Type: Unauthenticated; Encryption: Not required.
  • BT_SECURITY_MEDIUM β€” Security Level: 2 (Medium - default); Link Key Type: Unauthenticated; Encryption: Desired.
  • BT_SECURITY_HIGH β€” Security Level: 3 (High); Link Key Type: Authenticated; Encryption: Required.
  • BT_SECURITY_FIPS (since Linux 3.15) β€” Security Level: 4 (Secure Only); Link Key Type: Authenticated (P-256 based Secure Simple Pairing and Secure Authentication); Encryption: Required.

Example:

int level = BT_SECURITY_HIGH;
int err = setsockopt(rfcomm_socket, SOL_BLUETOOTH, BT_SECURITY, &level,
                     sizeof(level));
if (err == -1) {
    perror("setsockopt");
    return 1;
}

πŸ›‘οΈ BT_DEFER_SETUP (since Linux 2.6.30) πŸ›‘οΈ

Channel defer connection setup β€” this controls whether the connection procedure needs to be authorized by userspace before responding, which allows authorization at profile level. Possible values:

  • Value 0 β€” Description: Disable (default); Authorization: Not required.
  • Value 1 β€” Description: Enable; Authorization: Required.

Example:

int defer_setup = 1;
int err = setsockopt(rfcomm_socket, SOL_BLUETOOTH, BT_DEFER_SETUP,
                     &defer_setup, sizeof(defer_setup));
if (err == -1) {
    perror("setsockopt");
    return err;
}

err = listen(rfcomm_socket, 5);
if (err) {
    perror("listen");
    return err;
}

struct sockaddr_rc remote_addr = {0};
socklen_t addr_len = sizeof(remote_addr);
int new_socket = accept(rfcomm_socket, (struct sockaddr*)&remote_addr,
                        &addr_len);
if (new_socket < 0) {
    perror("accept");
    return new_socket;
}

/* To complete the connection setup of new_socket read 1 byte */
char c;
struct pollfd pfd;

memset(&pfd, 0, sizeof(pfd));
pfd.fd = new_socket;
pfd.events = POLLOUT;

err = poll(&pfd, 1, 0);
if (err) {
    perror("poll");
    return err;
}

if (!(pfd.revents & POLLOUT)) {
    err = read(sk, &c, 1);
    if (err < 0) {
        perror("read");
        return err;
    }
}

🌊 BT_FLUSHABLE (since Linux 2.6.39)

Okay so this one's the channel flushable flag β€” it literally just controls whether the channel's data can be flushed or not. That's it, that's the whole vibe. Possible values:

  • BT_FLUSHABLE_OFF β€” value 0x00, and it's the default. Do not flush data.
  • BT_FLUSHABLE_ON β€” value 0x01. Flush data. 🚿

⚑ BT_CHANNEL_POLICY (since Linux 3.10)

This is the high-speed (AMP) channel policy bestie, possible values:

  • BT_CHANNEL_POLICY_BREDR_ONLY β€” value 0 (the default). BR/EDR only.
  • BT_CHANNEL_POLICY_BREDR_PREFERRED β€” value 1. BR/EDR Preferred.
  • BT_CHANNEL_POLICY_BREDR_PREFERRED β€” value 2. AMP Preferred.
🫢 ngl, values 1 and 2 both shipping with the same define name is exactly what the page says, so I'm not touching it β€” I'm just the messenger πŸ’…

πŸ¦‹ BT_PHY (since Linux 5.10)

Channel supported PHY(s), possible values β€” and yes, these are bit flags, so you can stack them:

  • BT_PHY_BR_1M_1SLOT β€” BIT 0 β€” BR 1Mbps 1SLOT
  • BT_PHY_BR_1M_3SLOT β€” BIT 1 β€” BR 1Mbps 3SLOT
  • BT_PHY_BR_1M_5SLOT β€” BIT 2 β€” BR 1Mbps 5SLOT
  • BT_PHY_BR_2M_1SLOT β€” BIT 3 β€” EDR 2Mbps 1SLOT
  • BT_PHY_BR_2M_3SLOT β€” BIT 4 β€” EDR 2Mbps 3SLOT
  • BT_PHY_BR_2M_5SLOT β€” BIT 5 β€” EDR 2Mbps 5SLOT
  • BT_PHY_BR_3M_1SLOT β€” BIT 6 β€” EDR 3Mbps 1SLOT
  • BT_PHY_BR_3M_3SLOT β€” BIT 7 β€” EDR 3Mbps 3SLOT
  • BT_PHY_BR_3M_5SLOT β€” BIT 8 β€” EDR 3Mbps 5SLOT

Nine PHYs, all iconic in their own way πŸ‘‘


🌈 RESOURCES

http://www.bluez.org

That's the mothership, go say hi ✨


πŸ› REPORTING BUGS

linux-bluetooth@vger.kernel.org

Found something cursed? Email the list. They're chill, fr fr πŸ™ˆ


πŸ‘€ SEE ALSO


Free use of this software is granted under the terms of the GNU Lesser General Public Licenses (LGPL).


That's the whole page bestie, you ate that πŸ’…βœ¨ This little masterpiece is brought to you by BlueZ β€” dated May 2024 β€” and answers to the name RFCOMM(7) πŸŽ€πŸ¦‹ Go forth and bind those sockets, mwah πŸ’–

πŸ“¦ where this page comes from