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  * :ref:`hci_uart <bluetooth-hci-uart-sample>`
101  * :ref:`hci_usb <bluetooth-hci-usb-sample>`
102  * :ref:`hci_spi <bluetooth-hci-spi-sample>`
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  * :kconfig:option:`CONFIG_BT_CTLR` ``=y``
113  * :kconfig:option:`CONFIG_BT_LL_SW_SPLIT` ``=y`` (if using the open source Link Layer)
114
115* **Host-only build**: A Zephyr OS Host build will contain the Application and
116  the BLE 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  * :kconfig:option:`CONFIG_BT_CTLR` ``=n``
123
124  All of the samples located in ``samples/bluetooth`` except for the ones
125  used for Controller-only builds can be built as Host-only
126
127* **Combined build**: This includes the Application, the Host and the
128  Controller, and it is used exclusively for single-chip (SoC) configurations.
129  A build of this type sets the following Kconfig option values:
130
131  * :kconfig:option:`CONFIG_BT` ``=y``
132  * :kconfig:option:`CONFIG_BT_HCI` ``=y``
133  * :kconfig:option:`CONFIG_BT_CTLR` ``=y``
134  * :kconfig:option:`CONFIG_BT_LL_SW_SPLIT` ``=y`` (if using the open source Link Layer)
135
136  All of the samples located in ``samples/bluetooth`` except for the ones
137  used for Controller-only builds can be built as Combined
138
139The picture below shows the SoC or single-chip configuration when using a Zephyr
140combined build (a build that includes both a BLE Host and a Controller in the
141same firmware image that is programmed onto the chip):
142
143.. figure:: img/ble_cfg_single.png
144   :align: center
145   :alt: BLE Combined build on a single chip
146
147   A Combined build on a Single-Chip configuration
148
149When using connectivity or dual-chip configurations, several Host and Controller
150combinations are possible, some of which are depicted below:
151
152.. figure:: img/ble_cfg_dual.png
153   :align: center
154   :alt: BLE dual-chip configuration builds
155
156   Host-only and Controller-only builds on dual-chip configurations
157
158When using a Zephyr Host (left side of image), two instances of Zephyr OS
159must be built with different configurations, yielding two separate images that
160must be programmed into each of the chips respectively. The Host build image
161contains the application, the BLE Host and the selected HCI driver (UART or
162SPI), while the Controller build runs either the
163:ref:`hci_uart <bluetooth-hci-uart-sample>`, or the
164:ref:`hci_spi <bluetooth-hci-spi-sample>` app to provide an interface to
165the BLE Controller.
166
167This configuration is not limited to using a Zephyr OS Host, as the right side
168of the image shows. One can indeed take one of the many existing GNU/Linux
169distributions, most of which include Linux's own BLE Host (BlueZ), to connect it
170via UART or USB to one or more instances of the Zephyr OS Controller build.
171BlueZ as a Host supports multiple Controllers simultaneously for applications
172that require more than one BLE radio operating at the same time but sharing the
173same Host stack.
174
175Source tree layout
176******************
177
178The stack is split up as follows in the source tree:
179
180``subsys/bluetooth/host``
181  :ref:`The host stack <bluetooth_le_host>`. This is where the HCI command and
182  event handling as well as connection tracking happens. The implementation of
183  the core protocols such as L2CAP, ATT, and SMP is also here.
184
185``subsys/bluetooth/controller``
186  :ref:`Bluetooth LE Controller <bluetooth-ctlr-arch>` implementation.
187  Implements the controller-side of HCI, the Link Layer as well as access to the
188  radio transceiver.
189
190``include/bluetooth/``
191  :ref:`Public API <bluetooth_api>` header files. These are the header files
192  applications need to include in order to use Bluetooth functionality.
193
194``drivers/bluetooth/``
195  HCI transport drivers. Every HCI transport needs its own driver. For example,
196  the two common types of UART transport protocols (3-Wire and 5-Wire)
197  have their own drivers.
198
199``samples/bluetooth/``
200  :ref:`Sample Bluetooth code <bluetooth-samples>`. This is a good reference to
201  get started with Bluetooth application development.
202
203``tests/bluetooth/``
204  Test applications. These applications are used to verify the
205  functionality of the Bluetooth stack, but are not necessary the best
206  source for sample code (see ``samples/bluetooth`` instead).
207
208``doc/connectivity/bluetooth/``
209  Extra documentation, such as PICS documents.
210
211.. _Bluetooth Specification: https://www.bluetooth.com/specifications/bluetooth-core-specification
212