Bluetooth HCI (Host Controller Interface)
The Bluetooth specification includes the definition of an interface (the Host Controller Interface) for the hardware of a Bluetooth module. It defines the interface between the HCI Driver (on Micro-controller) and the Host Controller firmware (on the Bluetooth module).HCI Driver is also known as the HCI Host.
The physical link for transmitting HCI packets between an HCI Driver and Host Controller firmware, via a Host Controller Transport Layer, are:
- USB (Universal Serial Bus).
- RS232 (standard serial port), with error detection and recovery.
- Generic UART, assuming no errors.
Commands are processed asynchronously, so the completion of a command (and its return data) is reported by a Command Complete event. Commands may be processed in parallel, so a later command may complete before an earlier command.
- HCI Command Packets: commands are issued by the HCI Driver to the Host Controller:
- Packet indicator (for UART interfaces) of 1.
- Op-code (16 bits): identifies the command:
OGF (Op-code Group Field, most significant 6 bits);
OCF (Op-code Command Field, least significant 10 bits).- Parameter length (8-bit): total length of all parameters in bytes.
- Command parameters: the number of parameters and their length is command specific.
- HCI Data Packets for ACL data:
- Packet indicator (for UART interfaces) of 2.
- Control information (16 bits):
Broadcast flag (most significant 2 bits):00 = point-to-point packet (no broadcast);Packet boundary flag (2 bits):
01 = Active Slave Broadcast;
10 = Parked Slave Broadcast.01 = continuing packet of a higher level message;Connection handle (least significant 12 bits).
10 = first packet of a higher level message.- Data length (16 bits): total length of data in bytes.
- Data.
- HCI Data Packets for SCO data:
- Packet indicator (for UART interfaces) of 3.
- Connection handle (12 bits).
- Unused (4 bits).
- Data length (8 bits): total length of data in bytes.
- Data.
- HCI Event Packets: the Host Controller notifies the HCI Driver of events:
- Packet indicator (for UART interfaces) of 4.
- Event code (8 bits): identifies the event.
- Parameter length (8-bit): total length of all parameters in bytes.
- Event parameters: the number of parameters and their length is event specific.
HCI ERROR CODE DESCRIPTIONS
HCI error code descriptions can be implementation-dependent; whether the error should be returned using a Command Status event or the event associated with the issued command (following a Command Status event with Status=0x00). In these cases, the command can not start executing because of the error, and it is therefore recommended to use the Command Status event. The reason for this suggested course of action is that it is not possible to use the Command Status event in all software architectures.
The Bluetooth module firmware will handle the Link Management Protocol (LMP). If local sends LMP_au_rand and remote returns LM_not_accepted, the Bluetooth module will send a HCI event to inform the HCI Driver, with the error code of 0x05. See below:
AUTHENTICATION FAILURE (0x05)
The’Authentication Failure’ error code is returned by the Host Controller in the Status parameter in a Connection Complete event or Authentication Complete event when pairing or authentication fails due to incorrect results in the pairing/ authentication calculations (because of incorrect PIN code or link key).
"The 'Authentication Failure' error code can also be used as a value for the Reason parameter in the Disconnect command (as a reason code). The error code will then be sent over the air so that it is returned in the Reason parameter of a Disconnection Complete event on the remote side. In the Disconnection Complete event following a Command Status event (where Status=0x00) on the local side on which the Disconnect command has been issued, the Reason parameter will however contain the reason code 'Connection Terminated By Local Host'.
Example of Authentication Failure in Connection Complete:
> HCI Event: Connect Request (0x04) plen 10
bdaddr 00:0D:44:2F:8C:C0 class 0x20040c type ACL
< HCI Command: Accept Connection Request (0x01|0x0009) plen 7
bdaddr 00:0D:44:2F:8C:C0 role 0x01
Role: Slave
> HCI Event: Command Status (0x0f) plen 4
Accept Connection Request (0x01|0x0009) status 0x00 ncmd 1
> HCI Event: Link Key Request (0x17) plen 6
bdaddr 00:0D:44:2F:8C:C0
< HCI Command: Link Key Request Reply (0x01|0x000b) plen 22
bdaddr 00:0D:44:2F:8C:C0 key C9295D90AF0463360C848B55881C2A11
> HCI Event: Command Complete (0x0e) plen 10
Link Key Request Reply (0x01|0x000b) ncmd 1
status 0x00 bdaddr 00:0D:44:2F:8C:C0
> HCI Event: Connect Complete (0x03) plen 11
status 0x05 handle 1 bdaddr 00:0D:44:2F:8C:C0 type ACL encrypt 0x00
Error: Authentication Failure