bluedroid Bluedroid Bluetooth host stack (ESP-IDF)

Bluedroid is the Bluetooth host stack of Espressif’s ESP-IDF, written in C under the Apache License 2.0. It covers Classic Bluetooth (BR/EDR) and Bluetooth Low Energy: GAP, SDP, L2CAP, RFCOMM, A2DP source and sink with the SBC codec, AVRCP, HID host, GATT client and server, and the Security Manager (pairing and bonding).

This package builds it for NuttX as a host only: it runs in user space and drives an external controller through a character device that carries H4 packets. Applications use the API of ESP-IDF (esp_bluedroid_init(), esp_bt_gap_*(), esp_a2d_*(), esp_ble_gap_*(), esp_ble_gattc_*() …), so programs written for Bluedroid on ESP-IDF need few changes.

It cannot be enabled together with NuttX’s own Bluetooth host (CONFIG_WIRELESS_BLUETOOTH_HOST) or with NimBLE.

Source

The sources are not part of nuttx-apps. The first build fetches, from ESP-IDF’s repository, the commit named by CONFIG_BLUEDROID_VERSION (v6.1.1 by default), and only components/bt/host/bluedroid, components/bt/common and components/bt/ble_profiles/common of it: a shallow, sparse, blob-less git clone of about 24 MB, which needs git 2.25 or later and network access. It is placed in wireless/bluetooth/bluedroid/esp-idf and patched there with patches/*.patch. A tree already at that place (one cloned by hand, for instance) is used as it is; make distclean removes it.

Espressif builds the stack for its own 32-bit chips and controllers. The patches make it right on 64-bit targets, with other controllers and on several CPUs:

  • 0001: the timer entries keep pointers in a 32-bit field.

  • 0002: a call to a function that exists only with background connections (BLE_GATT_BGCONN).

  • 0003: the arguments of BTC messages are misaligned for their pointers.

  • 0004: the GATT database misaligns the attributes that follow an attribute value.

  • 0005: the HCI layer frees its receive queues while an external HCI driver may still be handing it a packet.

  • 0006: LE Add Device To Resolving List is sent to controllers without a resolving list.

  • 0007, 0008: A2DP and AVDTP events are copied as whole unions from smaller variables.

  • 0009: the 32-bit types of the SBC codec are long.

  • 0010: the A2DP init is reported before the profile is registered, and a connection requested at once is lost.

  • 0011: the API of the duplicate scan exceptional list is declared only for LE 4.2, but built for LE 5.0 too.

The stack’s own sources are otherwise built unchanged: the package provides what they expect from ESP-IDF, on POSIX interfaces (port/): the FreeRTOS tasks, semaphores and queues (on POSIX threads), esp_timer (one thread runs the timers’ callbacks), the NVS key/value store (on files) and the log (on syslog). Every symbol of the port is named bluedroid_*, and the generic names of the stack’s osi layer (list_new(), config_get_int() …) get the same prefix.

Controller

The stack talks H4 with the controller through the character device CONFIG_BLUEDROID_HCI_DEVICE:

  • /dev/ttyHCIn, which NuttX’s Bluetooth UART driver registers (CONFIG_UART_BTH4), with the board’s support for the controller (firmware download, UART speed);

  • or a serial port wired to the controller (/dev/ttySn): it is put in raw mode, and its speed and flow control are left as they are. The controller must then be ready (its firmware loaded, at the speed of the port) before the stack starts.

The stack resets the controller when it starts and reads its version, address and capabilities.

Configuration

CONFIG_BLUEDROID

The package. It selects CONFIG_DEV_URANDOM unless the board has CONFIG_DEV_RANDOM: keys and LE random addresses are drawn from there.

CONFIG_BLUEDROID_VERSION

The ESP-IDF commit to fetch (full SHA-1). The patches are made for the default.

CONFIG_BLUEDROID_HCI_DEVICE

The controller’s device, /dev/ttyHCI0 by default.

CONFIG_BLUEDROID_STORAGE_PATH

Where the stack keeps its settings and the bonds (link keys), /data/bluedroid by default. It must be on a writable file system; it is created if needed. Each NVS namespace is a directory there and each key a file, replaced as a whole when written.

CONFIG_BLUEDROID_PRIORITY, CONFIG_BLUEDROID_STACKSIZE_MIN

The priority of the thread that reads the controller; the stack’s own threads run a few levels below it, in the order ESP-IDF gives them. Their stacks are those of ESP-IDF scaled by the size of a pointer, and at least CONFIG_BLUEDROID_STACKSIZE_MIN bytes.

CONFIG_BLUEDROID_TRACE_LEVEL

The level of the stack’s messages, written to the system log: 0 none, 1 errors, 2 warnings (the default), 3 API calls, 4 events, 5 debug, 6 verbose.

CONFIG_BLUEDROID_CLASSIC

Classic Bluetooth, with CONFIG_BLUEDROID_A2DP (A2DP and AVRCP) and CONFIG_BLUEDROID_HID_HOST.

CONFIG_BLUEDROID_BLE

Bluetooth Low Energy, with CONFIG_BLUEDROID_GATTS, CONFIG_BLUEDROID_GATTC, CONFIG_BLUEDROID_BLE_SMP and the choice of the legacy (CONFIG_BLUEDROID_BLE_42, the default) or the extended (CONFIG_BLUEDROID_BLE_50) advertising and scanning commands.

The Security Manager uses TinyCrypt (crypto/tinycrypt), selected with it.

Using the stack

Applications see the stack’s API headers and bluedroid_hci.h: the package’s Make.defs adds their directories to CFLAGS; with CMake, an application takes them from the bluedroid library:

nuttx_add_application(
  NAME myapp
  SRCS myapp_main.c
  INCLUDE_DIRECTORIES
    $<GENEX_EVAL:$<TARGET_PROPERTY:bluedroid,INTERFACE_INCLUDE_DIRECTORIES>>
  DEPENDS bluedroid)

The transport is attached before the stack starts and detached after it stops:

#include "esp_bt_main.h"
#include "esp_gap_bt_api.h"
#include "bluedroid_hci.h"

esp_bluedroid_config_t cfg = BT_BLUEDROID_INIT_CONFIG_DEFAULT();

bluedroid_hci_attach(NULL);          /* CONFIG_BLUEDROID_HCI_DEVICE */
esp_bluedroid_init_with_cfg(&cfg);
esp_bluedroid_enable();

/* Register callbacks and use the profiles as on ESP-IDF:
 * esp_bt_gap_register_callback(), esp_a2d_source_init() ...
 */

esp_bluedroid_disable();
esp_bluedroid_deinit();
bluedroid_hci_detach();

bluedroid_hci_version() gives the controller’s version, which the stack reads when it starts but does not offer in its API.

ESP-IDF’s nvs_flash_init() is accepted and does nothing; there is no controller to set up (esp_bt_controller_*()) since the controller is external.

The bluedroid command

CONFIG_BLUEDROID_DEMO builds a command that starts the stack, shows the controller’s version and address and the stack’s local name, and stops the stack. With play, it also connects to an A2DP sink (headphones, a speaker), pairing with it if needed (Secure Simple Pairing is accepted as it comes, a PIN is 0000), plays a short melody to it and disconnects:

nsh> bluedroid play 00:11:22:33:44:55 5
HCI version 9 revision 0x017e, LMP version 9 subversion 0x6119, manufacturer 0x0131
Address: 11:22:33:44:55:66
Name: ESP32
A2DP: connecting to: 00:11:22:33:44:55
A2DP: connection state 1
Pairing: authenticated with ...
A2DP: codec type 0 configured
A2DP: connection state 2
A2DP: audio state 1
A2DP: playing for 5 s
A2DP: audio state 0
A2DP: 220416 samples sent
A2DP: connection state 3
A2DP: connection state 0

-d <device> uses another device than CONFIG_BLUEDROID_HCI_DEVICE.

The configuration sim:bluedroid runs it in the simulator, on a controller outside it (see the simulator’s board documentation).

Limitations

  • SPP and the Classic L2CAP API are not built: they need ESP-IDF’s virtual file system. BluFi (Wi-Fi provisioning over BLE), LE Audio, ISO channels and the controller-specific features of ESP-IDF are not built either.

  • In a flat build the stack’s state lives in the program’s static data, which outlives it: a program must stop the stack (esp_bluedroid_deinit(), bluedroid_hci_detach()) before it exits.

  • One controller, used by one program at a time. bluedroid_hci_attach() locks the device with flock(): while one program has it, another gets -EBUSY. A kernel without file locks (CONFIG_FS_LOCK_BUCKET_SIZE 0) gives no such check.