doc/connectivity/bluetooth/bluetooth-arch.rst
.. _bluetooth-arch:
Stack Architecture ##################
Overview
This page describes the software architecture of Zephyr's Bluetooth protocol stack.
.. note:: Zephyr supports mainly Bluetooth Low Energy (LE), the low-power version of the Bluetooth specification. Zephyr also has limited support for portions of the BR/EDR Host.
.. _bluetooth-layers:
There are 3 main layers that together constitute a full Bluetooth Low Energy protocol stack:
.. _bluetooth-hci:
The Bluetooth Specification_ describes the format in which a Host must
communicate with a Controller. This is called the Host Controller Interface
(HCI) protocol. HCI can be implemented over a range of different physical
transports like UART, SPI, or USB. This protocol defines the commands that a Host
can send to a Controller and the events that it can expect in return, and also
the format for user and protocol data that needs to go over the air. The HCI
ensures that different Host and Controller implementations can communicate
in a standard way making it possible to combine Hosts and Controllers from
different vendors.
.. _bluetooth-configs:
The three separate layers of the protocol and the standardized interface make it possible to implement the Host and Controller on different platforms. The two following configurations are commonly used:
.. _bluetooth-build-types:
The Zephyr software stack as an RTOS is highly configurable, and in particular, the Bluetooth subsystem can be configured in multiple ways during the build process to include only the features and layers that are required to reduce RAM and ROM footprint as well as power consumption. Here's a short list of the different Bluetooth-enabled builds that can be produced from the Zephyr project codebase:
Controller-only build: When built as a Bluetooth Controller, Zephyr includes the Link Layer and a special application. This application is different depending on the physical transport chosen for HCI:
bluetooth_hci_uartbluetooth_hci_usbbluetooth_hci_spiThis application acts as a bridge between the UART, SPI or USB peripherals and the Controller subsystem, listening for HCI commands, sending application data and responding with events and received data. A build of this type sets the following Kconfig option values:
CONFIG_BT =yCONFIG_BT_HCI =yCONFIG_BT_HCI_RAW =yThe controller itself needs to be enabled as well, typically by making sure the corresponding device tree node is enabled.
Host-only build: A Zephyr OS Host build will contain the Application and the Bluetooth Host, along with an HCI driver (UART or SPI) to interface with an external Controller chip. A build of this type sets the following Kconfig option values:
CONFIG_BT =yCONFIG_BT_HCI =yAdditionally, if the platform supports also a local controller, it needs to be
disabled, typically by disabling the corresponding device tree node. This is done
together with enabling the device tree node for some other HCI driver and making
sure that the zephyr,bt-hci device tree chosen property points at it.
All of the samples located in samples/bluetooth except for the ones
used for Controller-only builds can be built as Host-only
Combined build: This includes the Application, the Host and the Controller, and it is used exclusively for single-chip (SoC) configurations. A build of this type sets the following Kconfig option values:
CONFIG_BT =yCONFIG_BT_HCI =yThe controller itself needs to be enabled as well, typically by making sure the corresponding device tree node is enabled.
All of the samples located in samples/bluetooth except for the ones
used for Controller-only builds can be built as Combined
The picture below shows the SoC or single-chip configuration when using a Zephyr combined build (a build that includes both a Bluetooth Host and a Controller in the same firmware image that is programmed onto the chip):
.. figure:: img/ble_cfg_single.png :align: center :alt: Bluetooth Combined build on a single chip
A Combined build on a Single-Chip configuration
When using connectivity or dual-chip configurations, several Host and Controller combinations are possible, some of which are depicted below:
.. figure:: img/ble_cfg_dual.png :align: center :alt: Bluetooth dual-chip configuration builds
Host-only and Controller-only builds on dual-chip configurations
When using a Zephyr Host (left side of image), two instances of Zephyr OS
must be built with different configurations, yielding two separate images that
must be programmed into each of the chips respectively. The Host build image
contains the application, the Bluetooth Host and the selected HCI driver (UART or
SPI), while the Controller build runs either the
:zephyr:code-sample:bluetooth_hci_uart, or the
:zephyr:code-sample:bluetooth_hci_spi app to provide an interface to
the Bluetooth Controller.
This configuration is not limited to using a Zephyr OS Host, as the right side of the image shows. One can indeed take one of the many existing GNU/Linux distributions, most of which include Linux's own Bluetooth Host (BlueZ), to connect it via UART or USB to one or more instances of the Zephyr OS Controller build. BlueZ as a Host supports multiple Controllers simultaneously for applications that require more than one Bluetooth radio operating at the same time but sharing the same Host stack.
Source tree layout
The stack is split up as follows in the source tree:
:zephyr_file:subsys/bluetooth/host
:ref:The host stack <bluetooth_le_host>. This is where the HCI command and
event handling as well as connection tracking happens. The implementation of
the core protocols such as L2CAP, ATT, and SMP is also here.
:zephyr_file:subsys/bluetooth/controller
:ref:Bluetooth LE Controller <bluetooth-ctlr-arch> implementation.
Implements the controller-side of HCI, the Link Layer as well as access to the
radio transceiver.
:zephyr_file:include/zephyr/bluetooth/
:ref:Public API <bluetooth_api> header files. These are the header files
applications need to include in order to use Bluetooth functionality.
:zephyr_file:drivers/bluetooth/
HCI transport drivers. Every HCI transport needs its own driver. For example,
the two common types of UART transport protocols (3-Wire and 5-Wire)
have their own drivers.
:zephyr_file:samples/bluetooth/
:zephyr:code-sample-category:Sample Bluetooth code <bluetooth>. This is a good reference to
get started with Bluetooth application development.
:zephyr_file:tests/bluetooth/
Test applications. These applications are used to verify the
functionality of the Bluetooth stack, but are not necessary the best
source for sample code (see :zephyr_file:samples/bluetooth instead).
:zephyr_file:doc/connectivity/bluetooth/
Extra documentation, such as PICS documents.
.. _Bluetooth Specification: https://www.bluetooth.com/specifications/bluetooth-core-specification