1.. _bluetooth-tools:
2
3Tools
4#####
5
6This page lists and describes tools that can be used to assist during Bluetooth
7stack or application development in order to help, simplify and speed up the
8development process.
9
10.. contents::
11    :local:
12    :depth: 2
13
14.. _bluetooth-mobile-apps:
15
16Mobile applications
17*******************
18
19It is often useful to make use of existing mobile applications to interact with
20hardware running Zephyr, to test functionality without having to write any
21additional code or requiring extra hardware.
22
23The recommended mobile applications for interacting with Zephyr are:
24
25* Android:
26
27  * `nRF Connect for Android`_
28  * `nRF Mesh for Android`_
29  * `LightBlue for Android`_
30
31* iOS:
32
33  * `nRF Connect for iOS`_
34  * `nRF Mesh for iOS`_
35  * `LightBlue for iOS`_
36
37.. _bluetooth_bluez:
38
39Using BlueZ with Zephyr
40***********************
41
42The Linux Bluetooth Protocol Stack, BlueZ, comes with a very useful set of
43tools that can be used to debug and interact with Zephyr's BLE Host and
44Controller. In order to benefit from these tools you will need to make sure
45that you are running a recent version of the Linux Kernel and BlueZ:
46
47* Linux Kernel 4.10+
48* BlueZ 4.45+
49
50Additionally, some of the BlueZ tools might not be bundled by default by your
51Linux distribution. If you need to build BlueZ from scratch to update to a
52recent version or to obtain all of its tools you can follow the steps below:
53
54.. code-block:: console
55
56   git clone git://git.kernel.org/pub/scm/bluetooth/bluez.git
57   cd bluez
58   ./bootstrap-configure --disable-android --disable-midi
59   make
60
61You can then find :file:`btattach`, :file:`btmgt` and :file:`btproxy` in the
62:file:`tools/` folder and :file:`btmon` in the :file:`monitor/` folder.
63
64You'll need to enable BlueZ's experimental features so you can access its
65most recent BLE functionality. Do this by editing the file
66:file:`/lib/systemd/system/bluetooth.service`
67and making sure to include the :literal:`-E` option in the daemon's execution
68start line:
69
70.. code-block:: console
71
72   ExecStart=/usr/libexec/bluetooth/bluetoothd -E
73
74Finally, reload and restart the daemon:
75
76.. code-block:: console
77
78   sudo systemctl daemon-reload
79   sudo systemctl restart bluetooth
80
81.. _bluetooth_qemu_native:
82
83Running on QEMU or native_sim
84*****************************
85
86It's possible to run Bluetooth applications using either the :ref:`QEMU
87emulator<application_run_qemu>` or :ref:`native_sim <native_sim>`.
88
89In either case, a Bluetooth controller needs to be exported from
90the host OS (Linux) to the emulator. For this purpose you will need some tools
91described in the :ref:`bluetooth_bluez` section.
92
93Using the Host System Bluetooth Controller
94==========================================
95
96The host OS's Bluetooth controller is connected in the following manner:
97
98* To the second QEMU serial line using a UNIX socket. This socket gets used
99  with the help of the QEMU option :literal:`-serial unix:/tmp/bt-server-bredr`.
100  This option gets passed to QEMU through :makevar:`QEMU_EXTRA_FLAGS`
101  automatically whenever an application has enabled Bluetooth support.
102* To :ref:`native_sim's BT User Channel driver <nsim_bt_host_cont>` through the use of a
103  command-line option passed to the native_sim executable: ``--bt-dev=hci0``
104
105On the host side, BlueZ allows you to export its Bluetooth controller
106through a so-called user channel for QEMU and :ref:`native_sim <native_sim>` to use.
107
108.. note::
109   You only need to run ``btproxy`` when using QEMU. native_sim handles
110   the UNIX socket proxying automatically
111
112If you are using QEMU, in order to make the Controller available you will need
113one additional step using ``btproxy``:
114
115#. Make sure that the Bluetooth controller is down
116
117#. Use the btproxy tool to open the listening UNIX socket, type:
118
119   .. code-block:: console
120
121      sudo tools/btproxy -u -i 0
122      Listening on /tmp/bt-server-bredr
123
124   You might need to replace :literal:`-i 0` with the index of the Controller
125   you wish to proxy.
126
127   If you see ``Received unknown host packet type 0x00`` when running QEMU, then
128   add :literal:`-z` to the ``btproxy`` command line to ignore any null bytes
129   transmitted at startup.
130
131Once the hardware is connected and ready to use, you can then proceed to
132building and running a sample:
133
134* Choose one of the Bluetooth sample applications located in
135  :literal:`samples/bluetooth`.
136
137* To run a Bluetooth application in QEMU, type:
138
139  .. zephyr-app-commands::
140     :zephyr-app: samples/bluetooth/<sample>
141     :host-os: unix
142     :board: qemu_x86
143     :goals: run
144     :compact:
145
146  Running QEMU now results in a connection with the second serial line to
147  the :literal:`bt-server-bredr` UNIX socket, letting the application
148  access the Bluetooth controller.
149
150* To run a Bluetooth application in :ref:`native_sim <native_sim>`, first build it:
151
152  .. zephyr-app-commands::
153     :zephyr-app: samples/bluetooth/<sample>
154     :host-os: unix
155     :board: native_sim
156     :goals: build
157     :compact:
158
159  And then run it with::
160
161     $ sudo ./build/zephyr/zephyr.exe --bt-dev=hci0
162
163Using a Zephyr-based BLE Controller
164===================================
165
166Depending on which hardware you have available, you can choose between two
167transports when building a single-mode, Zephyr-based BLE Controller:
168
169* UART: Use the :zephyr:code-sample:`bluetooth_hci_uart` sample and follow
170  the instructions in :ref:`bluetooth-hci-uart-qemu-posix`.
171* USB: Use the :zephyr:code-sample:`bluetooth_hci_usb` sample and then
172  treat it as a Host System Bluetooth Controller (see previous section)
173
174.. _bluetooth-hci-tracing:
175
176HCI Tracing
177===========
178
179When running the Host on a computer connected to an external Controller, it
180is very useful to be able to see the full log of exchanges between the two,
181in the format of a :ref:`bluetooth-hci` log.
182In order to see those logs, you can use the built-in ``btmon`` tool from BlueZ:
183
184.. code-block:: console
185
186   $ btmon
187
188The output looks like this::
189
190   = New Index: 00:00:00:00:00:00 (Primary,Virtual,Control)                     0.274200
191   = Open Index: 00:00:00:00:00:00                                              0.274500
192   < HCI Command: Reset (0x03|0x0003) plen 0                                 #1 0.274600
193   > HCI Event: Command Complete (0x0e) plen 4                               #2 0.274700
194         Reset (0x03|0x0003) ncmd 1
195         Status: Success (0x00)
196   < HCI Command: Read Local Supported Features (0x04|0x0003) plen 0         #3 0.274800
197   > HCI Event: Command Complete (0x0e) plen 12                              #4 0.274900
198         Read Local Supported Features (0x04|0x0003) ncmd 1
199         Status: Success (0x00)
200         Features: 0x00 0x00 0x00 0x00 0x60 0x00 0x00 0x00
201            BR/EDR Not Supported
202            LE Supported (Controller)
203
204.. _bluetooth-embedded-hci-tracing:
205
206Embedded HCI tracing
207--------------------
208
209When running both Host and Controller in actual Integrated Circuits, you will
210only see normal log messages on the console by default, without any way of
211accessing the HCI traffic between the Host and the Controller.  However, there
212is a special Bluetooth logging mode that converts the console to use a binary
213protocol that interleaves both normal log messages as well as the HCI traffic.
214
215Set the following Kconfig options to enable this protocol before building your
216application:
217
218.. code-block:: cfg
219
220   CONFIG_BT_DEBUG_MONITOR_UART=y
221   CONFIG_UART_CONSOLE=n
222
223- Setting :kconfig:option:`CONFIG_BT_DEBUG_MONITOR_UART` activates the formatting
224- Clearing :kconfig:option:`CONFIG_UART_CONSOLE` makes the UART unavailable for
225  the system console. E.g. for ``printk`` and the :kconfig:option:`boot banner
226  <CONFIG_BOOT_BANNER>`
227
228To decode the binary protocol that will now be sent to the console UART you need
229to use the btmon tool from :ref:`BlueZ <bluetooth_bluez>`:
230
231.. code-block:: console
232
233   $ btmon --tty <console TTY> --tty-speed 115200
234
235If UART is not available (or you still want non-binary logs), you can set
236:kconfig:option:`CONFIG_BT_DEBUG_MONITOR_RTT` instead, which will use Segger
237RTT. For example, if trying to connect to a nRF52840DK with S/N 683578642:
238
239.. code-block:: console
240
241   $ btmon --jlink nRF52840_xxAA,683578642
242
243.. _bluetooth_virtual_posix:
244
245Running on a Virtual Controller and native_sim
246**********************************************
247
248An alternative to a Bluetooth physical controller is the use of a virtual
249controller. This controller can be connected over an HCI TCP server.
250This TCP server must support the HCI H4 protocol. In comparison to the physical controller
251variant, the virtual controller allows to test a Zephyr application running on the native
252boards without a physical Bluetooth controller.
253
254The main use case for a virtual controller is to do Bluetooth connectivity tests without
255the need of Bluetooth hardware. This allows to automate Bluetooth integration tests with
256external applications such as a Bluetooth gateway or a mobile application.
257
258To demonstrate this functionality an example is given to interact with a virtual controller.
259For this purpose, the experimental python module `Bumble`_ from Google is used as it allows to create
260a TCP Bluetooth virtual controller and connect with the Zephyr Bluetooth host. To install
261bumble follow the `Bumble Getting Started Guide`_.
262
263.. note::
264   If your Zephyr application requires the use of the HCI LE Set extended commands, install
265   the branch ``controller-extended-advertising`` from Bumble.
266
267Android Emulator
268=================
269
270You can test the virtual controller by connecting a Bluetooth Zephyr application
271to the `Android Emulator`_.
272
273To connect your application to the Android Emulator follow the next steps:
274
275    #. Build your Zephyr application and disable the HCI ACL flow
276       control (i.e. ``CONFIG_BT_HCI_ACL_FLOW_CONTROL=n``) as the
277       virtual controller from android does not support it at the moment.
278
279    #. Install Android Emulator version >= 33.1.4.0. The easiest way to do this is by installing
280       the latest `Android Studio Preview`_ version.
281
282    #. Create a new Android Virtual Device (AVD) with the `Android Device Manager`_. The AVD should use at least SDK API 34.
283
284    #. Run the Android Emulator via terminal as follows:
285
286       ``emulator avd YOUR_AVD -packet-streamer-endpoint default``
287
288    #. Create a Bluetooth bridge between the Zephyr application and
289       the virtual controller from Android Emulator with the `Bumble`_ utility ``hci-bridge``.
290
291       ``bumble-hci-bridge tcp-server:_:1234 android-netsim``
292
293       This command will create a TCP server bridge on the local host IP address ``127.0.0.1``
294       and port number ``1234``.
295
296    #. Run the Zephyr application and connect to the TCP server created in the last step.
297
298       ``./zephyr.exe --bt-dev=127.0.0.1:1234``
299
300After following these steps the Zephyr application will be available to the Android Emulator
301over the virtual Bluetooth controller that was bridged with Bumble. You can verify that the
302Zephyr application can communicate over Bluetooth by opening the Bluetooth settings in your
303AVD and scanning for your Zephyr application device. To test this you can build the Bluetooth
304peripheral samples such as :zephyr:code-sample:`ble_peripheral_hr` or
305:zephyr:code-sample:`ble_peripheral_dis`.
306
307.. _bluetooth_ctlr_bluez:
308
309Using Zephyr-based Controllers with BlueZ
310*****************************************
311
312If you want to test a Zephyr-powered BLE Controller using BlueZ's Bluetooth
313Host, you will need a few tools described in the :ref:`bluetooth_bluez` section.
314Once you have installed the tools you can then use them to interact with your
315Zephyr-based controller:
316
317   .. code-block:: console
318
319      sudo tools/btmgmt --index 0
320      [hci0]# auto-power
321      [hci0]# find -l
322
323You might need to replace :literal:`--index 0` with the index of the Controller
324you wish to manage.
325Additional information about :file:`btmgmt` can be found in its manual pages.
326
327
328.. _nRF Connect for Android: https://play.google.com/store/apps/details?id=no.nordicsemi.android.mcp&hl=en
329.. _nRF Connect for iOS: https://itunes.apple.com/us/app/nrf-connect/id1054362403
330.. _LightBlue for Android: https://play.google.com/store/apps/details?id=com.punchthrough.lightblueexplorer&hl=en_US
331.. _LightBlue for iOS: https://itunes.apple.com/us/app/lightblue-explorer/id557428110
332.. _nRF Mesh for Android: https://play.google.com/store/apps/details?id=no.nordicsemi.android.nrfmeshprovisioner&hl=en
333.. _nRF Mesh for iOS: https://itunes.apple.com/us/app/nrf-mesh/id1380726771
334.. _Bumble: https://github.com/google/bumble
335.. _Bumble Getting Started Guide: https://google.github.io/bumble/getting_started.html
336.. _Android Emulator: https://developer.android.com/studio/run/emulator
337.. _Android Device Manager: https://developer.android.com/studio/run/managing-avds
338.. _Android Studio Preview: https://developer.android.com/studio/preview
339