sco - Man Page

SCO transport

Synopsis

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

sco_socket = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO);

Description

The SCO logical transport, is a symmetric, point-to-point transport between the Central and a specific Peripheral. The SCO logical transport reserves slots and can therefore be considered as a circuit-switched connection between the Central and the Peripheral.

In addition to the reserved slots, when eSCO is supported, a retransmission window follows immediately after. Together, the reserved slots and the retransmission window form the complete eSCO window.

Socket Address

struct sockaddr_sco {
    sa_family_t     sco_family;
    bdaddr_t        sco_bdaddr;
};

Example:

struct sockaddr_sco addr;

memset(&addr, 0, sizeof(addr));
addr.sco_family = AF_BLUETOOTH;
bacpy(&addr.sco_bdaddr, bdaddr);

Socket Options (Sol_bluetooth)

The socket options listed below can be set by using setsockopt(2) and read with getsockopt(2) with the socket level set to SOL_BLUETOOTH.

BT_SECURITY (since Linux 2.6.30)

Channel security level, possible values:

ValueSecurity LevelLink Key TypeEncryption
BT_SECURITY_SDP0 (SDP Only)NoneNot required
BT_SECURITY_LOW1 (Low)UnauthenticatedNot required
BT_SECURITY_MEDIUM2 (Medium - default)UnauthenticatedDesired
BT_SECURITY_HIGH3 (High)AuthenticatedRequired
BT_SECURITY_FIPS (since Linux 3.15)4 (Secure Only)Authenticated (P-256 based Secure Simple Pairing and Secure Authentication)Required

Example:

int level = BT_SECURITY_HIGH;
int err = setsockopt(sco_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 control if the connection procedure needs to be authorized by userspace before responding which allows authorization at profile level, possible values:

ValueDescriptionAuthorization
0Disable (default)Not required
1EnableRequired

Example:

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

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

struct sockaddr_sco remote_addr = {0};
socklen_t addr_len = sizeof(remote_addr);
int new_socket = accept(sco_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_VOICE (since Linux 3.11)

Transport voice settings, possible values:

struct bt_voice {
    uint16_t setting;
};
DefineValueDescription
BT_VOICE_TRANSPARENT0x0003Transparent output
BT_VOICE_CVSD_16BIT0x0060C-VSD output PCM 16-bit input
BT_VOICE_TRANSPARENT_16BIT0x0063Transparent output PCM 16-bit input

Example:

struct bt_voice voice;

memset(&voice, 0, sizeof(voice));
voice.setting = BT_VOICE_TRANSPARENT;
int err = setsockopt(sco_socket, SOL_BLUETOOTH, BT_VOICE, &voice,
                     sizeof(voice));
if (err == -1) {
    perror("setsockopt");
    return 1;
}

BT_PKT_STATUS (since Linux 5.9)

Enable reporting packet status via BT_SCM_PKT_STATUS CMSG on received packets.  Possible values:

ValueDescription
0Disable (default)
1Enable
BT_SCM_PKT_STATUS

Level SOL_BLUETOOTH CMSG with data:

uint8_t pkt_status;

The values are equal to the "Packet_Status_Flag" defined in Core Specification v6.0 Sec. 5.4.3 pp. 1877:

pkt_statusDescription
0x0Correctly received data
0x1Possibly invalid data
0x2No data received
0x3Data partially lost

BT_PHY (since Linux 5.10)

Transport supported PHY(s), read-only (no setsockopt support). Possible values:

DefineValueDescription
BT_PHY_BR_1M_1SLOTBIT 0BR 1Mbps 1SLOT
BT_PHY_BR_1M_3SLOTBIT 1BR 1Mbps 3SLOT
BT_PHY_BR_2M_1SLOTBIT 3EDR 2Mbps 1SLOT
BT_PHY_BR_2M_3SLOTBIT 4EDR 2Mbps 3SLOT
BT_PHY_BR_3M_1SLOTBIT 6EDR 3Mbps 1SLOT
BT_PHY_BR_3M_3SLOTBIT 7EDR 3Mbps 3SLOT

BT_CODEC (since Linux 5.14)

Transport codec offload, possible values:

struct bt_codec {
    uint8_t id;
    uint16_t cid;
    uint16_t vid;
    uint8_t data_path_id;
    uint8_t num_caps;
    struct codec_caps {
        uint8_t len;
        uint8_t data[];
    } caps[];
} __attribute__((packed));

struct bt_codecs {
    uint8_t num_codecs;
    struct bt_codec codecs[];
} __attribute__((packed));

Example:

char buffer[sizeof(struct bt_codecs) + sizeof(struct bt_codec)];
struct bt_codec *codecs = (void *)buffer;

memset(codecs, 0, sizeof(codecs));
codec->num_codecs = 1;
codecs->codecs[0].id = 0x05;
codecs->codecs[0].data_path_id = 1;

int err = setsockopt(sco_socket, SOL_BLUETOOTH, BT_CODEC, codecs,
                     sizeof(buffer));
if (err == -1) {
    perror("setsockopt");
    return 1;
}

Socket Options (Sol_socket)

SOL_SOCKET level socket options that modify generic socket features (SO_SNDBUF, SO_RCVBUF, etc.) have their usual meaning, see socket(7).

The SOL_SOCKET level SCO socket options that have Bluetooth-specific handling in kernel are listed below.

So_timestamping, So_timestamp, So_timestampns

See  <https://docs.kernel.org/networking/timestamping.html>

For SCO sockets, software RX timestamps are supported.  Software TX timestamps (SOF_TIMESTAMPING_TX_SOFTWARE) are supported since Linux 6.15.

The software RX timestamp is the time when the kernel received the packet from the controller driver.

The SCM_TSTAMP_SND timestamp is emitted when packet is sent to the controller driver.

The SCM_TSTAMP_COMPLETION timestamp is emitted when controller reports the packet completed.  Completion timestamps are only supported on controllers that have SCO flow control.  Other TX timestamp types are not supported.

You can use SIOCETHTOOL to query supported flags.

The timestamps are in CLOCK_REALTIME time.

Example (Enable RX timestamping):

int flags = SOF_TIMESTAMPING_SOFTWARE |
    SOF_TIMESTAMPING_RX_SOFTWARE;
setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING, &flags, sizeof(flags));

Example (Read packet and its RX timestamp):

char data_buf[256];
union {
    char buf[CMSG_SPACE(sizeof(struct scm_timestamping))];
    struct cmsghdr align;
} control;
struct iovec data = {
    .iov_base = data_buf,
    .iov_len = sizeof(data_buf),
};
struct msghdr msg = {
    .msg_iov = &data,
    .msg_iovlen = 1,
    .msg_control = control.buf,
    .msg_controllen = sizeof(control.buf),
};
struct scm_timestamping tss;

res = recvmsg(fd, &msg, MSG_ERRQUEUE | MSG_DONTWAIT);
if (res < 0)
    goto error;

for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
    if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_TIMESTAMPING)
        memcpy(&tss, CMSG_DATA(cmsg), sizeof(tss));
}

tstamp_clock_realtime = tss.ts[0];

Example (Enable TX timestamping):

int flags = SOF_TIMESTAMPING_SOFTWARE |
    SOF_TIMESTAMPING_TX_SOFTWARE |
    SOF_TIMESTAMPING_OPT_ID;
setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING, &flags, sizeof(flags));

Example (Read TX timestamps):

union {
    char buf[CMSG_SPACE(sizeof(struct scm_timestamping))];
    struct cmsghdr align;
} control;
struct iovec data = {
    .iov_base = NULL,
    .iov_len = 0
};
struct msghdr msg = {
    .msg_iov = &data,
    .msg_iovlen = 1,
    .msg_control = control.buf,
    .msg_controllen = sizeof(control.buf),
};
struct cmsghdr *cmsg;
struct scm_timestamping tss;
struct sock_extended_err serr;
int res;

res = recvmsg(fd, &msg, MSG_ERRQUEUE | MSG_DONTWAIT);
if (res < 0)
    goto error;

for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
    if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_TIMESTAMPING)
        memcpy(&tss, CMSG_DATA(cmsg), sizeof(tss));
    else if (cmsg->cmsg_level == SOL_BLUETOOTH && cmsg->cmsg_type == BT_SCM_ERROR)
        memcpy(&serr, CMSG_DATA(cmsg), sizeof(serr));
}

tstamp_clock_realtime = tss.ts[0];
tstamp_type = serr->ee_info;      /* SCM_TSTAMP_SND or SCM_TSTAMP_COMPLETION */
tstamp_seqnum = serr->ee_data;

Ioctls

The following ioctls with operation specific for SCO sockets are available.

SIOCETHTOOL (since Linux 6.16-rc1)

Supports only command ETHTOOL_GET_TS_INFO, which may be used to query supported SOF_TIMESTAMPING_* flags.  The SOF_TIMESTAMPING_OPT_* flags are always available as applicable.

Example:

#include <linux/ethtool.h>
#include <linux/sockios.h>
#include <net/if.h>
#include <sys/socket.h>
#include <sys/ioctl.h>

...

struct ifreq ifr = {};
struct ethtool_ts_info cmd = {};
int sk;

snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "hci0");
ifr.ifr_data = (void *)&cmd;
cmd.cmd = ETHTOOL_GET_TS_INFO;

sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO);
if (sk < 0)
    goto error;
if (ioctl(sk, SIOCETHTOOL, &ifr))
    goto error;

sof_available = cmd.so_timestamping;

Resources

<http://www.bluez.org>

Reporting Bugs

<linux-bluetooth@vger.kernel.org>

See Also

socket(7), scotest(1)

Info

March 2025 BlueZ Linux System Administration