- OCaml 93%
- C 4%
- Python 3%
- Shell <1%
bluetooth #
A Bluetooth Low Energy (BLE) host stack in pure OCaml.
A BLE radio is a separate chip, the controller, which a computer drives over the Host Controller Interface (HCI). Everything above HCI is the host, and it is usually the C stack of the operating system: BlueZ on Linux, CoreBluetooth on macOS. The host has four layers. L2CAP multiplexes logical channels over one radio connection and reassembles packets the link split. The Attribute Protocol (ATT) is a request-response protocol for reading and writing numbered attributes (a handle, a type UUID and a value) on the peer. GATT organises those attributes into services and characteristics, such as a battery service with a level characteristic. GAP defines the roles, advertising, scanning, connection and pairing procedures.
Neither BlueZ nor CoreBluetooth exists in a unikernel. bluetooth
implements the host in OCaml, with no I/O of its own: it takes the H4 frames
a controller sends, and returns the frames to write back, so the same code
runs over a serial port, a Linux HCI socket or a simulated controller. H4 is
the simplest HCI transport, made for a serial line: each HCI packet is sent
as is, preceded by one byte giving its type (0x01 command, 0x02 ACL
data, 0x03 synchronous data, 0x04 event), and the packet's own length
field delimits it.
The package is split by protocol layer, each an I/O-free sublibrary built on
the wire codec library:
bluetooth.types: 16, 32 and 128-bit UUIDs, device addresses (BD_ADDR) and the advertising-data codec.bluetooth.hci: HCI packets, commands, events and the controller state.bluetooth.l2cap: channel identifiers, frames and fragment reassembly.bluetooth.att: Attribute Protocol messages and the client transaction machine.bluetooth.gatt: service discovery and a GATT server.bluetooth.smp: the Security Manager, with pairing, bonds, privacy and its cryptographic toolbox.bluetooth: the GAP layer that ties them together, as a Central (scan, connect, discover, pair) or a Peripheral (advertise and serve).
The bluetooth-eio package runs the host over an Eio flow, with a reader
fiber and a timer fiber. It references no Unix, so it builds for a Solo5
unikernel; bluetooth-eio.host opens a serial controller or a Linux HCI user
channel on a normal operating system. The blth package is a command-line
tool with scan, gatt, pair and serve commands; example/ shows how
to run it against a simulated controller.
The controller is always one the operating system does not already drive.
macOS gives no HCI access to its built-in radio, so on a Mac the controller
is an external one on a serial line, such as an nRF52840 running Zephyr's
hci_uart sample (blth --device /dev/cu.usbmodem... --baud 1000000), or a
simulated one reached with --tcp. On Linux, --hci N takes over
controller hciN through the HCI user channel, after
hciconfig hciN down has detached it from BlueZ.
Installation #
Install with opam:
$ opam install bluetooth
If opam cannot find the package, it has probably not reached the public
opam-repository yet. Add the overlay repository, then install it:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install bluetooth
Example #
A 16-bit UUID expands to a full 128-bit value through the Bluetooth Base
UUID, 00000000-0000-1000-8000-00805F9B34FB: the 16-bit value goes into
bytes 2 and 3, so 0x180F (the battery service) becomes
0000180F-0000-1000-8000-00805F9B34FB, and a 32-bit value fills the first
four bytes the same way (Core Specification, Vol 3, Part B, 2.5.1):
# let u = Bluetooth_types.Uuid.of_uuid16 0x180F in
String.length (Bluetooth_types.Uuid.to_uuid128 u)
- : int = 16
Scope #
BLE only, in both roles (Central and Peripheral). Classic Bluetooth (SDP, RFCOMM and the audio profiles) is out of scope. Pairing runs in Secure Connections Only Mode (Core Spec Vol 3 Part C 10.2.4): LE Secure Connections with Just Works, Numeric Comparison and Passkey Entry. A peer without Secure Connections is refused with Authentication Requirements, and out-of-band data is refused.
Licence #
ISC