Section 7.9

ATT

Full name: Attribute Protocol.

Definition

ATT, the Attribute Protocol, is the Bluetooth Low Energy (BLE) §7.1 protocol with which one device, the client, reads and writes small data items, called attributes, that another device, the server, keeps in a table. Each attribute has a handle, a 16-bit number that serves as its address in the table, a type, and a value of up to 512 bytes. ATT defines the messages for reading, writing and pushing values, and which of them get an answer. The Bluetooth Core Specification defines it.

The idea

A small sensor must offer a few values, such as a temperature and a sampling period, to a phone, with little memory and a slow radio link. ATT gives it the simplest possible remote access: every value lives in a numbered slot, and every message names a slot by its handle, so reading the slot with handle 0x0012 takes a 3-byte request. ATT knows nothing about what the slots mean. GATT §7.10 (Generic Attribute Profile), built on top of ATT, gives the table its structure.

A wall of numbered post-office boxes is a fair picture: a customer asks the clerk for the contents of box 18, or hands in an envelope for it. The picture differs in two ways. Some boxes open only for customers with the right credentials, and the rules are not posted, so customers find out by trying. And the server can push a box’s new contents to the client unasked. In practice, the detail that matters most is which messages get an answer: of the six kinds of ATT message, only requests and indications are answered.

How it works

Transport. ATT messages travel on fixed channel 0x0004 of L2CAP §7.15 (Logical Link Control and Adaptation Protocol), the layer that carries Host messages over a BLE connection. Client and server are ATT roles, independent of which device created the connection, and one device can be both.

An attribute has four parts:

Message format. Every ATT message starts with a 1-byte opcode §2.8 that names the operation, followed by parameters such as a handle and a value. Numbers longer than one byte are sent least significant byte first. The largest message the two devices exchange is ATT_MTU, the ATT maximum transmission unit §7.14 (MTU). It starts at 23 bytes; the client can raise it with an Exchange MTU Request, the server answers with its own limit, and both then use the smaller value.

Six kinds of message.

Bit 6 of the opcode (value 0x40) marks a command, so the Write Command, opcode 0x52, is the Write Request, opcode 0x12, with that bit set.

One question at a time. A client sends its next request on an ATT channel only after the response to the previous one has arrived, and a server likewise has at most one indication outstanding. Commands and notifications may be sent at any time and have no flow control. A request or indication not answered within 30 seconds has failed, and no further ATT message may then be sent on that channel; in practice the devices must disconnect and reconnect.

Server processing. For a request, the server decodes the opcode and handle, checks that the attribute exists, checks the permissions against the current state of the link, and performs the operation. It then sends the response, or an Error Response that gives the request’s opcode, the handle and an error code, such as 0x01 Invalid Handle, 0x03 Write Not Permitted, 0x05 Insufficient Authentication or 0x0F Insufficient Encryption. For a command, the server performs the same checks, but if a check fails, or the server has no buffer space for the command, it discards the command silently.

Worked example

A client writes the 2-byte value 0x0001 to the attribute with handle 0x002A, while ATT_MTU is still 23 bytes. The Write Request is 5 bytes:

12 2A 00 01 00

Here 12 is the opcode of a Write Request, 2A 00 is the handle 0x002A with its low byte first, and 01 00 is the value 0x0001, also low byte first. If the write succeeds, the server answers with the 1-byte Write Response 13. If no attribute has handle 0x002A, it answers with the 5-byte Error Response 01 12 2A 00 01: opcode 01 (Error Response), the opcode 12 of the failed request, the handle 2A 00, and error code 01, Invalid Handle.

The same write sent as a Write Command is 52 2A 00 01 00, and the server sends nothing back, whether it wrote the value or discarded the command. In either form the value can be at most 23−3=2023 - 3 = 20 bytes, because the opcode and the handle take 3 of the 23 bytes.

An indication runs the other way: the server sends 1D 2C 00 followed by the new value of the attribute with handle 0x002C, and the client answers with the 1-byte Handle Value Confirmation 1E. A notification, opcode 1B, has the same layout and gets no answer. Below ATT, each message travels with a 4-byte L2CAP header, so the Write Request above fills 5+4=95 + 4 = 9 bytes of a radio packet’s payload, and the Link Layer §7.6, the part of each device that controls the radio, acknowledges that packet whatever kind of ATT message it carries.

Stated precisely

The Bluetooth Core Specification states the acknowledgments of ATT as follows.

Every ATT message travels in radio packets that the peer’s Link Layer acknowledges and that are sent again until received, for as long as the connection lasts. So a command or notification is seldom lost on the radio; it is lost when a receiver discards it, or when the connection ends before it is delivered.

Limits and common mistakes

A response is not proof of a lasting effect. Suppose a server’s write handler copies the new value into a queue in RAM at 2 ms and returns success, the server sends the Write Response at 3 ms, and a separate task stores the value in flash memory at 20 ms. If power fails at 10 ms, the client holds a truthful response and the server holds no stored value. An application that needs proof of a stored result must define its own receipt, for example a notification sent after the value has been stored.

A confirmation shows only that the client’s ATT layer received the indication, not that the client’s application has processed the value.

A missing response proves nothing either way. If the connection drops or the 30-second limit passes, the client cannot tell whether the server performed the request.

ATT does not remove duplicates that the application creates. A client that sends a request again after reconnecting may cause the same write twice, so operations that matter should be safe to repeat, the property called idempotency §8.31.

ATT has almost no message security of its own. The exception, the rarely used Signed Write Command, appends a 12-byte authentication §1.6 signature made of a counter and a message authentication code §5.7 computed with a key shared during pairing §7.17; despite its name, it is not a digital signature §5.14. Confidentiality §1.8 and integrity §1.9 normally come from encryption in the Link Layer, and permissions check the state of the link, not who composed the bytes.

Answers are paced by the radio schedule. A response travels in the same connection event, the regular radio meeting of the two devices, as its request only if that event is still open when the response is ready; otherwise it waits for the next event, one connection interval §7.7 later.

Questions answered

Which operations receive a protocol acknowledgment?

In ATT, the Attribute Protocol of Bluetooth Low Energy (BLE), only requests and indications receive a protocol acknowledgment. Every request from client to server, such as a Read Request, a Write Request, a Prepare Write Request, an Execute Write Request or an Exchange MTU Request, is answered by the matching response or by an Error Response that states why it failed. Every indication from server to client is answered by a Handle Value Confirmation. Commands, namely the Write Command and the Signed Write Command, and notifications receive no ATT answer, so the sender never learns whether the receiver used or discarded them.

Two limits apply. All these messages, answered or not, travel in radio packets that the peer’s Link Layer acknowledges, which shows only that the bytes reached the peer’s radio software. And an ATT acknowledgment shows only what the specification says: a Write Response means that the server considers the value written, and a confirmation means that the client received the indication. Neither proves that an application finished acting on the value or stored it permanently; that needs an application-level receipt.

Related entries: Notification versus indication (§7.12), Write request versus write command (§7.13), Asynchronous completion (§9.28), GATT (§7.10), MTU and fragmentation (§7.14). Further reading: [91].