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