Rfcomm Tutorial
Rfcomm Tutorial
RFCOMM
RS-232 serial ports have nine circuits, which can be used for transferring data and sig-
nalling. RFCOMM can emulate the serial cable line settings and status of an RS-232 ser-
ial port. RFCOMM provides multiple concurrent connections by relying on L2CAP to
handle multiplexing over single connections, and to provide connections to multiple de-
vices.
RFCOMM relies on the Bluetooth baseband to provide reliable in-sequence deliv-
ery of byte streams. It does not have any ability to correct errors. RFCOMM’s flow con-
trol also uses the baseband’s capabilities.
RFCOMM data rates will be limited in devices where there is a physical serial port
involved (Type 2 devices). Implementations may optionally pace data on virtual serial
ports (in Type 1 devices). In the absence of pacing, RFCOMM will deliver the highest
possible data rate, although what the highest data rate is can be a complicated issue in the
presence of multiple connections (see Chapter 18).
RFCOMM is a simple, reliable transport protocol with framing, multiplexing, and
the following additional provisions:
172
Typically, serial port transmit and receive data lines are connected to a UART (Universal
Asynchronous Receiver Transmitter). The job of the UART is to convert between the
serial data sent down cables and the parallel data processing which devices use. UARTs
use buffers to convert between serial and parallel data. This allows them to reduce the
load on the processor. Instead of the processor having to be interrupted for every single
bit that is sent down the cables, the UART transfers the bits between the cables and
buffers, then the processor only has to get involved when there is a whole buffer to deal
with.
The signals from a UART are connected, so they appear in the system address map.
Some processors reserve a special range of addresses for I/O; other systems can map them
into any part of normal memory. Because UARTs look like areas of memory to a micro-
processor, it is possible to emulate a serial port in software by taking an area of memory
and setting values as they would appear if they were set by a UART.
The Bluetooth RFCOMM specification talks about emulating the nine circuits of an
RS-232 serial port, and specifies how a serial data stream can be emulated. But because
the serial stream from an RS-232 port is viewed by the microprocessor after it has been
through a UART, software dealing with serial ports is actually handling parallel data.
Similarly, RFCOMM software deals with parallel data delivered by the lower layers of
the Bluetooth stack.
A UART connects to some piece of hardware: wires or buffers. RFCOMM connects
up to the lower layers of the software stack via L2CAP.
A protocol stack for a Type 1 RFCOMM device is shown at the left of Figure 10–1.
The port emulation entity maps a system specific communication interface (API) to the
RFCOMM services. This can be used to connect to legacy applications as shown, or it can
be used to connect to applications specifically written for Bluetooth. A Type 1 device
would usually be the end of a communication path, for example, a PC or printer.
A protocol stack for a Type 2 RFCOMM device is shown at the right of
Figure 10–1. The port proxy entity relays data from RFCOMM to an external
RS-232 interface linked to another device. Type 2 devices are intermediate devices
which sit in the middle of a communication path. A modem is an example of a Type 2
device.
174 RFCOMM
RS-232
Cable
Legacy Application
RFCOMM RFCOMM
L2CAP L2CAP
Radio Radio
SABM, UA, DM, and DISC are “low- level” control frames. RFCOMM uses chan-
nels, each of which has a Data Link Connection Identifier (DLCI). UIH frames on DLCI
= 0 are used to send control messages. UIH frames on DLCIs ≠ 0 are used to send
data.
Connecting and Disconnecting 175
Because RFCOMM frames are carried in the payload of L2CAP packets, an L2CAP con-
nection must be set up before an RFCOMM connection can be set up.
RFCOMM has a reserved Protocol and Service Multiplexor (PSM) value for
L2CAP. This is defined in the Bluetooth core specification as 0x0003. Any L2CAP
frames received with this value in the PSM field will be sent to RFCOMM for processing.
The first frame to be sent on an RFCOMM channel is a SABM frame; this is a Start
Asynchronous Balanced Mode command. If the responding device’s RFCOMM is willing
to connect, it goes into Asynchronous Balanced Mode (ABM), and sends back an UA
frame. If the responding device’s RFCOMM doesn’t want to connect, it refuses the con-
nection by sending a DM frame. Figure 10–2 shows an RFCOMM channel setup being
refused in this way.
RFCOMM has a 60s timer which is started when a command is sent. If an acknowl-
edgement isn’t received when the timer elapses, the connection will be shut down. This is
different from GSM 07.10, which resends the command when the timer elapses. In the
case of RFCOMM, the Bluetooth baseband provides a reliable link, so if the command
wasn’t acknowledged first time, it is not likely to be acknowledged a second time. For the
SABM command, the timeout can be extended because security procedures might mean
that this command takes longer to process than others. If RFCOMM ever times out and
disconnects, it must send a DISC (disconnect) command frame on the same DLCI as the
original SABM frame just in case the other side has come back into range and thinks the
connection is still active. Figure 10–3 shows a channel being closed down because the ini-
tiator timed out.
If the connection succeeds, the responder replies to the SABM frame with a UA (un-
numbered acknowledgement) frame. This is followed by a Parameter Negotiation (PN)
command from the initiator and PN response from the responder, as shown in Figure 10–4.
Responding
For first RFCOMM connection,
SABM Frame (DLCI = 0)
RFCOMM
RFCOMM
Initiating
try to start multiplexor
Once a connection with DLCI = 0 has been set up, this is available for RFCOMM
signalling. To transfer data, other RFCOMM channels must be set up. Figure 10–4 shows
a second RFCOMM channel being set up to transfer data. In this case, the channel re-
quires authentication, so there is a pause for LMP authentication and encryption between
the SABM command frame and the UA response frame. Once the UA frame has been re-
ceived, modem status commands are exchanged to communicate the state of the control
PN Command
Optionally negotiate parameters
for data link connection
Responding
PN Response
RFCOMM
RFCOMM
Initiating
SABM Frame
Open data link connection;
optionally negotiate security
LMP Authenticaton & Encryption
UA Frame
signals. Data can then be transferred immediately as shown, or there could be an ex-
change of PN command and response to configure the new connection’s parameters.
The user data should include MSCs (Modem Status Commands), which communi-
cate the state of serial port control signals.
To shut down an RFCOMM connection, a DISC command is sent. When the last
data link has been shut down, a DISC should be sent on DLCI=0 to shut down the multi-
plexor. Then, whichever device shut down the multiplexor is responsible for disconnect-
ing the L2CAP channel.
RFCOMM borrows its frame structure from the GSM 07.10 standard. Figure 10–5 shows
the frame structure for the GSM 07.10 basic option. (There is also an advanced option,
which lacks the length field; RFCOMM always uses the basic option.)
Figure 10–6 shows the structure of an RFCOMM frame. This is identical to the
GSM07.10 basic option frame except that RFCOMM misses out the start and end flags,
and RFCOMM has to limit the number of bytes in a packet because of the limit on the
size of L2CAP packets.
RFCOMM doesn’t need the start and end flags because each RFCOMM frame is
carried in a single L2CAP packet. There is no need to pick RFCOMM frames out of a
data stream, so there is no need for flag bits to mark where the frames start and end.
0 4 8 12 16 20 24 28 32
Flag =
10011111
Length
Address Control Length
or Data
Flag =
10011111
0 4 8 12 16 20 24 28 32
Length
Address Control Length
or Data
The EA (Extend Address) field can be used to extend an address. If EA=0, then
more address octets follow; if EA=1, then this is the last octet of the address. Since the
Bluetooth specification says that server applications are assigned a server channel number
in the range 1 to 30 and an RFCOMM address frame has 5 bits for the server channel,
there will never be any need to use extended addressing, so the EA bit will always be set
to 1 in an RFCOMM address field.
The C/R (Command/Response) bit says whether the frame is a command or a re-
sponse. Its value depends not only on whether the frame carries a command or a response,
but also on which end of the channel is sending the frame. The device which set up the
connection (by sending a SABM command on DLCI 0) is called the initiator. The device
which responded (by sending the UA response on DLCI 0) is called the responder. As
long as the traffic follows this original pattern, the C/R bit is 1, so commands from the ini-
tiator and responses from the responder have C/R = 1. For exchanges in the opposite di-
rection, the C/R bit is 0, so commands from the responder and responses from the initiator
have C/R = 0. When sending data, the initiator sets C/R = 1 and the responder sets
C/R = 0. Figure 10–8 illustrates how the C/R bit is set.
After the C/R bit comes the Data Link Connection Identifier (DLCI). In GSM
TS0.10, this is one undivided field, but in RFCOMM, it is split up into a direction bit and
a server channel number. The initiator always sets the direction bit to 1 (D=1); the respon-
der always sets the direction bit to 0 (D=0). As with the C/R bits, who is the initiator and
responder is defined by which device sent the SABM frame to start up the connection.
The server channel number has 5 bits; this would give it a range from 0 to 31, but 0
and 31 are reserved, so only 1 to 30 can be allocated as server channel numbers for ser-
vices. Channel 0 is used for sending control information; channel 31 is reserved by TS
07.10. Bluetooth avoids using channels which are reserved by TS 07.10 to preserve com-
patibility with TS 07.10 applications.
0 1 2 3 4 5 6 7 8
Responding
C/R = 1
RFCOMM
Response (C/R = 1)
RFCOMM
Initiating
Command from responder Command (C/R = 0)
Response from initiator
C/R = 0 Response (C/R = 0)
The DLCI is calculated once before the data link connection is established. The
RFCOMM server channel number in the responding device is used for the DLCI. Because
server channel numbers 1 to 30 are available, one device can have up to 30 services using
RFCOMM.
A command with its P bit set to 1 is used when a response or a series of responses is
wanted from the device at the far end of the link. The responding device should send back
its responses with the F bit set to 1. There should only ever be one command frame with P
bit set to 1 waiting for a response.
DM packets are processed regardless of the state of the P/F bit, but SABM or
DISC commands and UA responses are thrown away if the P/F bit is set to zero.
10.5.4 Data
The data field is only present in UIH frames. There must be an integral number of bytes in
the data up to 32767. The size limit is set by the Maximum Transmission Unit (MTU) on
L2CAP packets, so if a system has a smaller L2CAP MTU, the size of RFCOMM data
will also be restricted.
0 2 4 6 8 10 12 14 16
EA
7 bits of Length Field
=1
EA
15 bits of Length Field
=0
Because UIH frames only calculate FCS on the address and control fields, their data
field is not protected by the FCS. This might be a drawback for reliable data transmission,
but it does have the advantage that the FCS patterns can be pre-calculated for all the
DLCIs that are in use. This pre-calculation could be done when the channel is set up.
Multiplexor commands and responses are sent on DLCI = 0. They are used to control the
RFCOMM link. There are seven types of commands or responses:
0 1 2 3 4 5 6 7 8
10–11). The EA bit is 1 because the type field occupies one byte. The C/R bit is used to
indicate whether the message is a command or a response.
The length field in a PN message is always set to 8, and the value field contains 8
bytes as shown in Figure 10–12. These bytes define the parameters which will be used on
a data link connection as follows:
• Six DLCI bits identify the data link connection for which parameters are being ne-
gotiated.
• Two padding bits which are always set to zero follow the DLCI; these are inserted
to avoid splitting parameters across bytes.
• Four I bits give the type of frames used to carry information on the channel. In RF-
COMM UIH frames indicated by the value 0b1000 are used.
0 1 2 3 4 5 6 7 8
DLCI 0b00
P (priority) 0b00
• Four CL bits give the convergence layer to used. RFCOMM uses Type 1 (unstruc-
tured octet stream) = 0b0000 in versions after 1.0b this may also be set to 0x0F to
enable credit based flow control.
• Six P bits assign a priority to the data link connection: 0 is the lowest priority, 63 is
the highest.
• Two padding bits which are always set to zero follow the P bits; these are inserted
to avoid splitting parameters across bytes.
• Eight T bits give the value of the acknowledgement timer, in GSM 07.10, this
would be used to trigger a retransmit; in RFCOMM, if the timer elapses, the con-
nection is closed down. The timer’s value is not negotiable, but is fixed at 60s. This
field is set to 0 to indicate that the timer is not negotiable.
• Sixteen N bits give the maximum size of the frame.
• Eight NA bits give the maximum number of retransmissions. Because the Bluetooth
baseband gives RFCOMM a reliable transport layer, RFCOMM will not retransmit,
so this value is set to zero.
• Three K bits define the window size for error recovery mode. RFCOMM uses basic
mode, so these bits are not interpreted by RFCOMM.
• Five padding bits set to zero fill the rest of the last value field to round up the values
to an integral number of bytes.
All parameters being negotiated are sent with the LSB in bit 1; this is the general
rule for RFCOMM information.
It is worth noting that RFCOMM follows the conventions of the GSM 07.10 stan-
dard and numbers the bits in a frame from 1 for the least-significant bit to 8 for the most
significant bit. This may be confusing to people who are used to seeing the bits in a byte
numbered from 0 to 7.
One device sends a PN message, and the other responds with another PN message.
The response may not change the DLCI, the priority, the convergence layer, or the timer
value. The response may send back a different timer value. In this case, the device which
sent the first PN messages will still use the timer it proposed, but the device at the other
end of the connection will use the value it sent in its message.
The response may have a smaller value for the maximum frame size, but it may not
propose a larger value for this parameter.
In GSM 07.10, PN messages are optional. In RFCOMM, support of the PN message
and its response are compulsory, although if default parameters are satisfactory, PN mes-
sages do not have to be sent. PN messages may be exchanged until the device which sent
the first message is happy with the parameters it gets sent back to it. Once it has a satis-
factory set of parameters in the reply, it can go on to set up the connection.
184 RFCOMM
0 1 2 3 4 5 6 7 8
10.6.2 Test
The test command is used to check the RFCOMM connection. As is normal, the length byte
gives the number of value bytes which follow. The number of value bytes is not fixed, and
is used to hold a test pattern. The remote end of the link echoes the same value bytes back.
The type field for the test command is shown in Figure 10–13. Because only a sin-
gle byte is used, the EA bit is set to 1. The C/R bit is used to indicate whether the message
is a command or a response.
0 1 2 3 4 5 6 7 8
• RTR—Ready To Receive bit, set to 0 when the device cannot receive data and 1
when it can receive data.
• IC—Incoming Call, 1 indicates an incoming call.
• DV—Data Valid, set to 1 to indicate that valid data is being sent.
These values may not seem to make sense when sent in a packet, but that is because
they map onto the lines of an RS-232 interface. It might be obvious when a packet is sent
that it is going to have valid data; who would bother to send a packet with invalid data?
However, when dealing with physical serial port lines, such signals make more sense. A
signal to say that valid data is being sent can be used to activate circuits to handle the
data. The MSC command just mimics the values of the V.24 signals, which would be
used on a wired RS-232 -interface.
The signals from an MSC map onto RS-232 signals as follows:
• RTC maps onto DSR (Data Set Ready) and DTR (Data Terminal Ready).
• RTR maps onto RTS (Request To Send) and CTS (Clear To Send).
• IC maps onto RI (Ring Indication).
• DV maps onto DCD (Data Carrier Detect).
Figure 10–16 shows how the signals are carried in the control field of the command.
The MSC is sent on a connection before any data is sent to establish the state of the
RS-232 control signals. It should also be sent whenever the signals need to be changed.
In the MSC, the state of the signals in the device sending the command is sent. The
response just carries a copy of the signals from the command.
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8
EA 1 DLCI
Baud Rate
XON
XOFF
Parameter Mask
0 1 2 3 4 5 6 7 8
Bit Data Stop Parity XON XOFF Reserved
Parity
Rate Bits Bits Type Char Char = 0b0
Input Output Input Output Input Output
XON/ XON/ Reserved = 0b00
RTR RTR RTC RTC
XOFF XOFF
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8
If the first line status bit is set to 0, then the RLS command is simply reporting that
there are no errors on the line.
When an RLS command is received, a response is sent back with the line status
copied from the command into the response.
RFCOMM implementations must recognise and respond to RLS commands, but
how they deal with the line status information is up to the implementor to decide.
0 1 2 3 4 5 6 7 8
is present. This service also has a text name which can be used to represent it on a user
interface.
For more information, see Chapter 11, Table 11–1, which shows how RFCOMM
information is presented for a headset application. The headset application also uses
RFCOMM services to set up and control headset connections.
10.8 SUMMARY
RFCOMM provides serial port emulation, which can be used to connect to legacy appli-
cations, and is also used for data transfer in several of the Bluetooth profiles.
RFCOMM supports two types of devices: A Type 1 device is the end of a commu-
nications path and supports an application on top of RFCOMM, and a Type 2 device is an
intermediate device and has a physical RS-232 serial port on top of RFCOMM.
To set up an RFCOMM connection, an L2CAP connection must first be set up.
RFCOMM frames are sent in the payload field of L2CAP packets. Once the L2CAP con-
nection is set up, RFCOMM control frames are sent back and forth to establish a sig-
nalling channel with a Data Link Connection Identifier (DLCI) set to 0. After this is set
up, subsequent channels are established for transferring data. Up to 30 data channels can
be set up, so RFCOMM can theoretically support 30 different services at once. (In prac-
tice, most bluetooth devices will not have the resources to support 30 different services.)
RFCOMM is based on the GSM 07.10 standard with a few minor differences to
allow for the differences between a Bluetooth connection and a GSM cellular phone con-
nection.