Section 7.5

Host Controller Interface (HCI)

Definition

The Host Controller Interface (HCI), defined in the Bluetooth Core Specification [275], is the standard interface between a Bluetooth device’s Host, its upper protocol layers, and its Controller, the radio and the Link Layer §7.6. HCI has three kinds of packet: commands, which the Host sends to ask the Controller to do something; events, which the Controller sends to report results and occurrences; and data packets, which carry connection data in both directions. HCI transports carry these packets between chips, for example over USB or a UART §6.23 (universal asynchronous receiver-transmitter) serial link.

The idea

A laptop maker buys Bluetooth chips from different suppliers, while the laptop’s operating system contains a single Host. That Host must start advertising, open connections and send data on every chip without knowing how any chip works inside. HCI makes this possible. It is a fixed vocabulary of numbered commands and events, so the bytes that mean “start advertising” are the same on every Controller that follows the specification.

HCI is also asynchronous: the Host sends a command and carries on, and the Controller answers later with an event. Some answers mean “done”. Others mean only “started”: a connection, for example, forms only when the peer is found, which may take seconds. The Host must therefore keep track of which of its requests are still open, and match each event to the request it answers. This is a case of asynchronous completion §9.28, in which the result of an operation arrives separately from the call that started it.

How it works

Packets.

A number longer than one byte is sent least significant byte first.

Transports. When Host and Controller are separate chips, an HCI transport carries the packets between them. On a UART link, one byte before each packet says its kind: 0x01 command, 0x02 ACL data, 0x03 synchronous data, 0x04 event, 0x05 isochronous data. The specification also defines transports for USB and other links. On a single chip, an implementation may pass the same packets through a queue in memory, or replace HCI with direct function calls into the Controller.

Flow control. The Controller has limited buffers. Every Command Complete and Command Status event carries a number, Num_HCI_Command_Packets, that says how many more commands the Controller can accept now. For data, the Host learns how many data packets the Controller can buffer, counts the packets it sends, and waits when the count reaches that number; the Number Of Completed Packets event (code 0x13) reports packets that the Controller has finished with, which frees their places.

Two ways a command finishes.

  1. A command that the Controller can carry out at once is answered by Command Complete, which carries the result. Reset and the command that turns advertising on are examples.

  2. A command that starts a procedure is answered first by Command Status. Status 0 means only that the procedure has started. A later event, specific to the command, reports the outcome. After LE Create Connection, for example, the outcome is an LE Connection Complete event. It arrives when the peer has been found and the connection has formed, which can take any length of time, or, with an error status, when the attempt fails or the Host cancels it.

Worked example

Two commands show the two ways a command finishes. In the first, a sensor’s Host turns advertising on over a UART link.

  1. The Host sends 5 bytes: 01 0A 20 01 01. The first 01 says that a command follows. LE Set Advertising Enable has OGF 0x08 and OCF 0x000A, so its opcode is 1024×8+10=82021024 \times 8 + 10 = 8202, which is 0x200A, sent low byte first as 0A 20. The next 01 says that 1 parameter byte follows, and the last 01 is that parameter: enable.

  2. The Controller answers with 7 bytes: 04 0E 04 01 0A 20 00. 04 says that an event follows; 0E is Command Complete; 04 says that 4 parameter bytes follow; 01 says that the Controller can accept 1 more command; 0A 20 names the command being answered; and 00 is the status, success. Advertising is now on.

In the second, a phone’s Host opens a connection to that sensor.

  1. At time 0 the Host sends LE Create Connection, opcode 1024×8+13=82051024 \times 8 + 13 = 8205, which is 0x200D, with the sensor’s address among its parameters.

  2. At 1 ms the Controller answers 04 0F 04 00 01 0D 20: an event, Command Status, 4 parameter bytes, status 00 (started), room for 1 more command, and the opcode 0x200D. No connection exists yet.

  3. At 800 ms, having received one of the sensor’s advertisements and sent a connection request, the Controller sends an LE Meta event, code 0x3E, with subevent 0x01, LE Connection Complete. It carries the new connection handle and the connection’s parameters.

The command took 1 ms to be accepted and the procedure took 800 ms. A Host that treated the Command Status at 1 ms as “connected” would act 799 ms early, and wrongly if the connection never formed.

Stated precisely

HCI states the following contract.

From this contract follows the matching rule. The Host matches a command result to its command by the opcode, and a connection event to its connection by the handle. An event that answers a request the Host has already abandoned must be discarded; it must not complete a newer request of the same kind.

Limits and common mistakes

Accepted is not done. A Command Status with status 0 says that a procedure started, not that it succeeded. The latency of a procedure is measured to its final event, not to the Command Status.

HCI carries secrets. To start encryption, the Host passes the long-term key §7.28 (LTK) to the Controller inside an HCI command. In a two-chip design, anyone who can probe the wires between the chips, or capture the USB traffic of a Bluetooth adapter, can read the key.

Each side trusts the other. The Controller carries out every valid command from the Host, and the Host usually believes every event from the Controller. A faulty or compromised Controller can send malformed events, so the Host must check the lengths and fields of events as carefully as radio data.

HCI is a local interface. A remote device cannot send HCI commands, but the content of its radio packets reaches the Host inside HCI events, such as advertising reports, and inside data packets, so those carry bytes that a remote device chose.

Where a single-chip product keeps HCI, the interface divides the work but does not isolate memory: both parts run in one address space unless the chip enforces a separation, for example with a memory protection unit §6.8 (MPU).

Supported commands differ between Controllers and between specification versions, so a Host asks the Controller which commands and features it supports instead of assuming them.

Questions answered

Does a single-chip build still preserve logical HCI boundaries?

It can, but it need not, and where it does, the boundary divides the work without isolating the two parts. The Host Controller Interface (HCI) is the set of commands, events and data packets defined between a Bluetooth device’s Host (its upper protocol layers) and its Controller (the radio and the Link Layer). On a single chip, some implementations keep passing HCI packets between the two parts through a queue in memory, so the interface and its contract are preserved exactly. Others call Controller functions directly through a supplier’s own interface; HCI then does not exist inside the chip, although the division of responsibilities between Host and Controller remains.

In either case, unless the chip enforces a separation, for example with a memory protection unit (MPU) that limits which memory each part may access, both parts share one memory and the same access rights, so a memory-corruption defect in one part can damage the other. Which design a given product uses has to be established from its software, not assumed from the specification.

Related entries: BLE Controller and Host (§7.4), Asynchronous completion (§9.28), ABA problem (§9.25), Long-term key (LTK) (§7.28), UART (§6.23). Further reading: [19, 275].