Section 7.11

GATT characteristic

Full name: Generic Attribute Profile (GATT) characteristic.

Definition

A GATT characteristic is one named data value that a Bluetooth Low Energy (BLE) §7.1 device offers to other devices, such as a temperature or a setting. GATT §7.10, the Generic Attribute Profile, builds each characteristic from attributes, the numbered entries of the device’s attribute table: a declaration, which states the characteristic’s type and the operations it supports; a value, a byte string of up to 512 bytes; and optional descriptors, which add information or per-client settings. Clients read, write or subscribe to the value.

The idea

A characteristic gives an application a slot of bytes that a remote client can write, and nothing more. Suppose an application message, for example a 600-byte configuration record, is larger than one write can carry. The client must split it, and every write reaches the server’s application as a separate call of its write handler, with no mark that says “first part of three”. GATT does not know where the application’s messages begin or end, so the application must define that itself; until it does, the server cannot tell a complete message from a fragment.

A mail slot that takes envelopes of limited size is a fair picture. A long letter is split over several envelopes, each marked with its page number and the total number of pages, and the office acts only once every page is in and the letter has been checked. The picture differs in one respect: GATT also offers its own long write, in which the Bluetooth software collects the pieces and applies them together. That mechanism is limited to 512 bytes per characteristic and holds the pieces in the Bluetooth software’s memory, so many applications define their own fragment format instead.

How it works

The attributes of one characteristic. The declaration holds the properties byte, the handle of the value attribute, and the characteristic’s UUID (universally unique identifier), the 16-bit or 128-bit name of its type. The value attribute holds the data. Descriptors follow, for example the Client Characteristic Configuration Descriptor, with which each client switches notifications or indications on. The properties byte lists the kinds of write the characteristic offers; the permissions, kept privately by the server, decide which writes succeed.

One ordinary write. A Write Request or a Write Command, two messages of ATT §7.9 (Attribute Protocol), carries the value handle and up to ATT_MTU minus 3 bytes of data, where ATT_MTU is the largest ATT message the two devices have agreed on and 3 bytes go to the opcode §2.8 and the handle. The server’s Bluetooth software calls the application’s write handler once per write, with the bytes and an offset of 0. The bytes of one write always arrive complete: if the Link Layer §7.6, which controls the radio, carried the message in several radio packets, L2CAP §7.15 (Logical Link Control and Adaptation Protocol) joined them before ATT saw them. A partial ATT message never reaches the handler; a partial application message can.

The long write. For a value longer than one write allows, GATT defines the Write Long Characteristic Values procedure:

  1. The client sends Prepare Write Requests (opcode 0x16), each with the handle, a value offset giving the position of this part within the value, and up to ATT_MTU minus 5 bytes. The server checks the handle and the write permission, adds the part to a prepare queue that it keeps for this client, and echoes handle, offset and part in its Prepare Write Response; if the queue is full, it answers with the error Prepare Queue Full (0x09).

  2. The client sends an Execute Write Request (opcode 0x18). With flag 0x01, the server writes all queued parts in the order they arrived, as one indivisible operation; with flag 0x00, it discards them. Offsets and total length are checked only now, and the total cannot exceed the 512-byte limit of an attribute value.

In the related Reliable Writes procedure, the client compares each echo with what it sent and cancels if any part came back different. A server that does not support queued writes refuses Prepare Write Requests with an error such as Request Not Supported (0x06).

Application-level fragments. When a message can exceed 512 bytes, or the server refuses queued writes, the application defines its own fragment format: each write carries a small header and one part of the message. A workable header names the message, gives the part’s offset within it, and gives the message’s total length; announcing the total length first is the idea of length-prefix framing §4.30. The server then:

  1. checks the header: the total length is within the largest message the server accepts, and the part lies inside the message;

  2. on the first part, reserves a buffer for the whole message, or rejects the message if it cannot;

  3. copies the part into the buffer at its offset and records which byte positions are now covered;

  4. when every position from 0 to the total length minus 1 is covered, and not before, parses the message, verifies its authenticity and checks its content, and only then acts on it;

  5. discards an incomplete message when the connection ends, when a new message replaces it, or after a time limit.

A write handler cannot report the result of a whole message, so the server usually reports it separately, for example in a notification that carries the message’s name and a status code. These rules form a small protocol state machine §7.37.

Worked example

The two devices have agreed on an ATT_MTU of 247 bytes, a common value because it fills the largest radio-packet payload of 251 bytes after the 4-byte L2CAP header: 251−4=247251 - 4 = 247. One Write Request then carries at most 247−3=244247 - 3 = 244 bytes.

A 600-byte message. The long write cannot carry it, because 600 is more than 512. Assume one message at a time per connection, so the application’s header needs no message name: a 2-byte offset and a 2-byte total length, which leave 244−4=240244 - 4 = 240 bytes of message per write. Three writes are needed: offset 0 with 240 bytes, covering positions 0 to 239, written as the half-open interval §14.12 [0,240)[0, 240), which includes 0 and excludes 240; offset 240 with 240 bytes, covering [240,480)[240, 480); and offset 480 with 120 bytes, covering [480,600)[480, 600). Together they cover 240+240+120=600240 + 240 + 120 = 600 positions, the whole message, and only then does the server parse and check it.

A repeated part. The client’s software, unsure whether the second part arrived, sends it again, and the third part is never sent. Counting received bytes gives 240+240+240=720240 + 240 + 240 = 720, at least 600, and would wrongly declare the message complete. The covered positions are [0,480)[0, 480), which misses [480,600)[480, 600), so tracking coverage gives the right answer: incomplete.

A part that does not fit. Let oo be a part’s offset, nn its length and LL the total length. A part with o=480o = 480 and n=240n = 240 claims [480,720)[480, 720), but only 600−480=120600 - 480 = 120 positions remain, and 240 is more than 120, so the server rejects it. After checking o≤Lo \le L, the server tests n≤L−on \le L - o, not o+n≤Lo + n \le L, because in 16-bit arithmetic o+no + n can wrap around: offset 0xFFF0 plus 32 bytes gives 0x10010, which a 16-bit variable stores as 0x0010, that is 16, and 16 would pass a test against 600. This is an overflow-safe range check §4.17.

500 bytes by long write. A 500-byte value fits the 512-byte limit. Each Prepare Write Request carries at most 247−5=242247 - 5 = 242 bytes, because opcode, handle and offset take 5 bytes. The parts are 242, 242 and 16 bytes long, since 500−242−242=16500 - 242 - 242 = 16, at offsets 0, 242 and 484. With the Execute Write Request, that makes four requests, each waiting for its response.

Stated precisely

Let LmaxL_{\max} be the largest message the server accepts and LL the declared total length of one message, with 1≤L≤Lmax1 \le L \le L_{\max}. A part with offset oo and length nn is accepted only if n≥1n \ge 1, o≤Lo \le L and n≤L−on \le L - o; together these say that the part lies inside [0,L)[0, L), and in this form the test cannot overflow. Let CC be the set of covered positions, empty at the start. Accepting the part sets CC to C∪[o,o+n)C \cup [o, o + n). If the part overlaps positions already in CC, the server either rejects it or accepts it only when the overlapping bytes equal the stored ones, and it applies one of these rules consistently. The message is complete exactly when C=[0,L)C = [0, L), that is, when CC contains LL positions.

The message may change the server’s state only if it is complete, its format is valid, its authenticity has been verified over all LL bytes, and its content is acceptable. The partial state is discarded on disconnection, on the start of a different message, or when a time limit expires. For the GATT long write, the specification fixes the corresponding rules: each part carries at most ATT_MTU minus 5 bytes, the total is at most 512 bytes, offsets and total length are checked when the Execute Write Request arrives, and all parts are written together.

Limits and common mistakes

One write is not one message. Acting on the first part, for example by parsing it as if it were a whole message, treats a fragment as a complete command.

Write Commands give no feedback. A server discards a command it cannot process, for example when its buffers are full, without telling the client, so a client that sends parts as Write Commands cannot know which parts arrived. A final status report from the server, or the use of Write Requests, closes that gap.

Repeated parts do not come from the radio. Within one connection, the Link Layer already removes radio-level repeats and keeps packets in order; repeated parts come from client software that sends again, typically after a reconnection.

Partial state is a resource. A client that announces large messages and never finishes them can exhaust the memory of a server that reserves a buffer for each, a form of denial of service §1.28. Limit the total length, allow one incomplete message per connection, and discard it on disconnection or timeout §9.34.

A stale part must not complete a new message. If the parts of an abandoned message and of a new one cannot be told apart, the server can assemble one message from both. A message name in every header, or a rule that discards the incomplete message whenever a new one starts, prevents this.

Complete is not authentic. Full coverage shows only that every position holds some byte. The server must verify authenticity over the complete message, with the total length and the message name among the authenticated bytes; deciding which bytes a check protects is the question of authentication coverage §5.41. A message authentication code §5.7 over each part alone would not stop parts of different messages from being combined.

Queued writes have their own costs: the prepare queue sits in the Bluetooth software, is limited in size, and is validated only when the Execute Write Request arrives. A server that must check every part as it arrives, or strictly bound its memory per client, therefore often refuses Prepare Write Requests and uses its own fragment format.

Questions answered

What happens if a characteristic write contains a partial application message?

A write that contains only part of an application message reaches the server’s write handler exactly like any other write, because a GATT (Generic Attribute Profile) characteristic carries bytes and knows nothing about the application’s message boundaries; nothing in the write marks it as partial. What happens next is decided entirely by the application’s own protocol. If that protocol says one write carries one complete message, the fragment fails the length or format check and the server rejects it. If the protocol defines fragments, the server checks the fragment’s header, stores its bytes at the stated offset in a buffer of bounded size, records which positions are covered, and waits. It acts only when every byte has arrived and the complete message has passed its format, authenticity and content checks, and it discards the partial state if the connection ends, a different message starts or a time limit expires.

For values of at most 512 bytes, the client can instead use the long write of GATT, in which the server queues the parts sent in Prepare Write Requests and applies them together when an Execute Write Request arrives. In no case may the server act on the first fragment alone.

Related entries: Length-prefix framing (§4.30), Overflow-safe range check (§4.17), Protocol state machine (§7.37), Staging versus commit versus activation (§8.22), ATT (§7.9), GATT (§7.10), Write request versus write command (§7.13), Authentication coverage (§5.41). Further reading: [91].