1.. _bluetooth-arch: 2 3Stack Architecture 4################## 5 6Overview 7******** 8 9This page describes the software architecture of Zephyr's Bluetooth protocol 10stack. 11 12.. note:: 13 Zephyr supports mainly Bluetooth Low Energy (LE), the low-power 14 version of the Bluetooth specification. Zephyr also has limited support 15 for portions of the BR/EDR Host. 16 17.. _bluetooth-layers: 18 19Bluetooth LE Layers 20=================== 21 22There are 3 main layers that together constitute a full Bluetooth Low Energy 23protocol stack: 24 25* **Host**: This layer sits right below the application, and is comprised of 26 multiple (non real-time) network and transport protocols enabling 27 applications to communicate with peer devices in a standard and interoperable 28 way. 29* **Controller**: The Controller implements the Link Layer (LE LL), the 30 low-level, real-time protocol which provides, in conjunction with the Radio 31 Hardware, standard-interoperable over-the-air communication. The LL schedules 32 packet reception and transmission, guarantees the delivery of data, and 33 handles all the LL control procedures. 34* **Radio Hardware**: Hardware implements the required analog and digital 35 baseband functional blocks that permit the Link Layer firmware to send and 36 receive in the 2.4GHz band of the spectrum. 37 38.. _bluetooth-hci: 39 40Host Controller Interface 41========================= 42 43The `Bluetooth Specification`_ describes the format in which a Host must 44communicate with a Controller. This is called the Host Controller Interface 45(HCI) protocol. HCI can be implemented over a range of different physical 46transports like UART, SPI, or USB. This protocol defines the commands that a Host 47can send to a Controller and the events that it can expect in return, and also 48the format for user and protocol data that needs to go over the air. The HCI 49ensures that different Host and Controller implementations can communicate 50in a standard way making it possible to combine Hosts and Controllers from 51different vendors. 52 53.. _bluetooth-configs: 54 55Configurations 56============== 57 58The three separate layers of the protocol and the standardized interface make 59it possible to implement the Host and Controller on different platforms. The two 60following configurations are commonly used: 61 62* **Single-chip configuration**: In this configuration, a single microcontroller 63 implements all three layers and the application itself. This can also be called a 64 system-on-chip (SoC) implementation. In this case the Bluetooth Host and the Bluetooth 65 Controller communicate directly through function calls and queues in RAM. The 66 Bluetooth specification does not specify how HCI is implemented in this 67 single-chip configuration and so how HCI commands, events, and data flows between 68 the two can be implementation-specific. This configuration is well suited for 69 those applications and designs that require a small footprint and the lowest 70 possible power consumption, since everything runs on a single IC. 71* **Dual-chip configuration**: This configuration uses two separate ICs, 72 one running the Application and the Host, and a second one with the Controller 73 and the Radio Hardware. This is sometimes also called a connectivity-chip 74 configuration. This configuration allows for a wider variety of combinations of 75 Hosts when using the Zephyr OS as a Controller. Since HCI ensures 76 interoperability among Host and Controller implementations, including of course 77 Zephyr's very own Bluetooth Host and Controller, users of the Zephyr Controller can 78 choose to use whatever Host running on any platform they prefer. For example, 79 the host can be the Linux Bluetooth Host stack (BlueZ) running on any processor 80 capable of supporting Linux. The Host processor may of course also run Zephyr 81 and the Zephyr OS Bluetooth Host. Conversely, combining an IC running the Zephyr 82 Host with an external Controller that does not run Zephyr is also supported. 83 84.. _bluetooth-build-types: 85 86Build Types 87=========== 88 89The Zephyr software stack as an RTOS is highly configurable, and in particular, 90the Bluetooth subsystem can be configured in multiple ways during the build process to 91include only the features and layers that are required to reduce RAM and ROM 92footprint as well as power consumption. Here's a short list of the different 93Bluetooth-enabled builds that can be produced from the Zephyr project codebase: 94 95* **Controller-only build**: When built as a Bluetooth Controller, Zephyr includes 96 the Link Layer and a special application. This application is different 97 depending on the physical transport chosen for HCI: 98 99 * :zephyr:code-sample:`bluetooth_hci_uart` 100 * :zephyr:code-sample:`bluetooth_hci_usb` 101 * :zephyr:code-sample:`bluetooth_hci_spi` 102 103 This application acts as a bridge between the UART, SPI or USB peripherals and 104 the Controller subsystem, listening for HCI commands, sending application data 105 and responding with events and received data. A build of this type sets the 106 following Kconfig option values: 107 108 * :kconfig:option:`CONFIG_BT` ``=y`` 109 * :kconfig:option:`CONFIG_BT_HCI` ``=y`` 110 * :kconfig:option:`CONFIG_BT_HCI_RAW` ``=y`` 111 112 The controller itself needs to be enabled as well, typically by making sure the 113 corresponding device tree node is enabled. 114 115* **Host-only build**: A Zephyr OS Host build will contain the Application and 116 the Bluetooth Host, along with an HCI driver (UART or SPI) to interface with an 117 external Controller chip. 118 A build of this type sets the following Kconfig option values: 119 120 * :kconfig:option:`CONFIG_BT` ``=y`` 121 * :kconfig:option:`CONFIG_BT_HCI` ``=y`` 122 123 Additionally, if the platform supports also a local controller, it needs to be 124 disabled, typically by disabling the corresponding device tree node. This is done 125 together with enabling the device tree node for some other HCI driver and making 126 sure that the ``zephyr,bt-hci`` device tree chosen property points at it. 127 128 All of the samples located in ``samples/bluetooth`` except for the ones 129 used for Controller-only builds can be built as Host-only 130 131* **Combined build**: This includes the Application, the Host and the 132 Controller, and it is used exclusively for single-chip (SoC) configurations. 133 A build of this type sets the following Kconfig option values: 134 135 * :kconfig:option:`CONFIG_BT` ``=y`` 136 * :kconfig:option:`CONFIG_BT_HCI` ``=y`` 137 138 The controller itself needs to be enabled as well, typically by making sure the 139 corresponding device tree node is enabled. 140 141 All of the samples located in ``samples/bluetooth`` except for the ones 142 used for Controller-only builds can be built as Combined 143 144The picture below shows the SoC or single-chip configuration when using a Zephyr 145combined build (a build that includes both a Bluetooth Host and a Controller in the 146same firmware image that is programmed onto the chip): 147 148.. figure:: img/ble_cfg_single.png 149 :align: center 150 :alt: Bluetooth Combined build on a single chip 151 152 A Combined build on a Single-Chip configuration 153 154When using connectivity or dual-chip configurations, several Host and Controller 155combinations are possible, some of which are depicted below: 156 157.. figure:: img/ble_cfg_dual.png 158 :align: center 159 :alt: Bluetooth dual-chip configuration builds 160 161 Host-only and Controller-only builds on dual-chip configurations 162 163When using a Zephyr Host (left side of image), two instances of Zephyr OS 164must be built with different configurations, yielding two separate images that 165must be programmed into each of the chips respectively. The Host build image 166contains the application, the Bluetooth Host and the selected HCI driver (UART or 167SPI), while the Controller build runs either the 168:zephyr:code-sample:`bluetooth_hci_uart`, or the 169:zephyr:code-sample:`bluetooth_hci_spi` app to provide an interface to 170the Bluetooth Controller. 171 172This configuration is not limited to using a Zephyr OS Host, as the right side 173of the image shows. One can indeed take one of the many existing GNU/Linux 174distributions, most of which include Linux's own Bluetooth Host (BlueZ), to connect it 175via UART or USB to one or more instances of the Zephyr OS Controller build. 176BlueZ as a Host supports multiple Controllers simultaneously for applications 177that require more than one Bluetooth radio operating at the same time but sharing the 178same Host stack. 179 180Source tree layout 181****************** 182 183The stack is split up as follows in the source tree: 184 185``subsys/bluetooth/host`` 186 :ref:`The host stack <bluetooth_le_host>`. This is where the HCI command and 187 event handling as well as connection tracking happens. The implementation of 188 the core protocols such as L2CAP, ATT, and SMP is also here. 189 190``subsys/bluetooth/controller`` 191 :ref:`Bluetooth LE Controller <bluetooth-ctlr-arch>` implementation. 192 Implements the controller-side of HCI, the Link Layer as well as access to the 193 radio transceiver. 194 195``include/bluetooth/`` 196 :ref:`Public API <bluetooth_api>` header files. These are the header files 197 applications need to include in order to use Bluetooth functionality. 198 199``drivers/bluetooth/`` 200 HCI transport drivers. Every HCI transport needs its own driver. For example, 201 the two common types of UART transport protocols (3-Wire and 5-Wire) 202 have their own drivers. 203 204``samples/bluetooth/`` 205 :zephyr:code-sample-category:`Sample Bluetooth code <bluetooth>`. This is a good reference to 206 get started with Bluetooth application development. 207 208``tests/bluetooth/`` 209 Test applications. These applications are used to verify the 210 functionality of the Bluetooth stack, but are not necessary the best 211 source for sample code (see ``samples/bluetooth`` instead). 212 213``doc/connectivity/bluetooth/`` 214 Extra documentation, such as PICS documents. 215 216.. _Bluetooth Specification: https://www.bluetooth.com/specifications/bluetooth-core-specification 217