BLE host-guest communication via HCI in Renode
Published:
Topics: Open source tools
Bluetooth Low Energy (BLE) is one of the most popular wireless protocols used in consumer devices including blood pressure monitors, wearables, and smart home appliances. Renode, Antmicro’s embedded and IoT simulation framework, has supported the BLE protocol for a long time, but the typical use scenario would cover setups where all the nodes exist in the simulated world.
There are however some very interesting testing scenarios that can be enabled by connecting nodes simulated in Renode to virtual or physical devices outside of the framework, and to achieve that, we have added support for BLE host-guest communication via the Host Controller Interface, a communication layer between the host and controller elements of the Bluetooth protocol stack. HCI enables the host to manage the behavior of the controller, receive notifications from it, and manage the communication between the two elements.
The Renode HCI integration opens up some exciting advanced testing scenarios, such as connecting any Zephyr-based device emulated in Renode to an application running on an Android Emulator, or connecting to a physical BLE controller for deterministic and repeatable testing of real-world devices in challenging contexts.
Simulating the BLE host stack using Zephyr
Zephyr RTOS is a lightweight, scalable, open-source real-time operating system (RTOS) designed for connected, resource-constrained, and embedded devices. It supports many popular BLE-enabled boards such as nRF52840, with support in Renode including BLE.
Zephyr RTOS implements the HCI protocol as the interface between the host and controller layers of the BLE stack to standardize communication between the layers. The Zephyr HCI interface can be used to connect to the BLE stack of a test host controller, e.g. BlueZ on Linux. Zephyr RTOS also supports single-chip and dual-chip configurations, providing flexibility in the implementation of the BLE host and controller.
To show an example of BLE host-guest communication in Zephyr, we used the peripheral_hr sample which exposes the HR (Heart Rate) GATT service and generates dummy heart rate values. In this scenario we are not using the on-board nRF52840 BLE controller for low-level management of the BLE protocol, but rather rely on an external abstract controller connected over HCI.
Enabling BLE host-guest communication in Renode with Zephyr
Implementing BLE host-guest communication via HCI did not require any complex modifications to Renode itself, as the necessary parts such as support for multi-node simulations and rich support for Zephyr RTOS have been part of Renode for many years now.
To achieve this, we run the hci_uart Zephyr application and include a simple overlay for the nRF52840 DK platform:
/ {
chosen {
zephyr,bt-uart = &arduino_serial;
};
};
&arduino_serial {
status = "okay";
};
The overlay is passed to west build
to assign a selected UART (in this case named arduino_serial
) to the Bluetooth HCI:
west build -p auto -b nrf52840dk_nrf52840 -d hci_peripheral_hr samples/bluetooth/peripheral_hr -- \
-DCONFIG_BT_HCI=y -DCONFIG_BT_CTLR=n -DCONFIG_BT_H4=y \
-DCONFIG_BT_EXT_ADV=n -DCONFIG_BT_HCI_ACL_FLOW_CONTROL=n \
-DDTC_OVERLAY_FILE=$PWD/nrf52840dk_nrf52840_ble_hci_uart.overlay
Once your sample is built, you can use Renode to set up the HCI connection with the host, load your platform and expose the selected UART via the socket terminal:
(machine-0) emulation CreateServerSocketTerminal 3456 "ble_hci_uart" false
(machine-0) connector Connect sysbus.uart1 ble_hci_uart
This will expose a TCP port 3456 on your host, that you can now use to provide the controller side of the connection.
Connecting Renode to Android Emulator
Open source tools like Bumble and BlueZ support connecting a virtual BLE controller to a TCP port exposed by Renode to pass packets between BLE host and controller. You can create a virtual network between your Renode simulation and, for example, Android Emulator using android-netsim which can work in two modes, as a host or as a virtual controller accepting host connections.
This feature allows you to develop Android and Zephyr BLE applications simultaneously by running the entire virtual environment on a remote machine, providing you with detailed logs of your Zephyr application running in Renode. After developing and testing your application entirely in a virtual environment, you can eventually replace both emulated devices with physical counterparts only when you need to perform final testing in a real environment.
You can establish a connection between a Renode simulation and Android Emulator with bumble-hci-bridge
the following way:
bumble-hci-bridge tcp-client:127.0.0.1:3456 android-netsim 0x03:0x0031,0x08:0x013,0x08:0x032,0x08:0x016,0x03:0x035
Connecting physical and external BLE controllers
Renode can be connected to other externally-simulated BLE controllers or even a physical BLE controller. You can use any tool that supports either physical or virtual BLE controllers: btvirt
and btproxy
from BlueZ or bumble-hci-bridge
and bumble-link-relay
from bumble. To establish a connection between the Renode simulation and the USB BLE adapter using bumble-hci-bridge
(the same tool as in the case of Android Emulator), you can run:
bumble-hci-bridge tcp-client:127.0.0.1:3456 usb:0
Simulate and test large-scale Bluetooth networks with Renode
Renode’s ability to integrate easily with various tools, such as Bumble and BlueZ, enables the simulation of large-scale Bluetooth mesh networks in a fully controllable, deterministic environment, making testing and debugging easier. Renode lets you accurately assess the performance, scalability, and reliability of your BLE projects in a virtual environment before deployment to identify and resolve potential issues early in the development cycle, saving time and resources.
If you are interested in Antmicro’s comprehensive engineering services to help you develop your BLE-based products at any stage of development using open source tools such as Renode and Zephyr, contact us at contact@antmicro.com.