Something went wrong. Try again.
Bluetooth Low Energy host stack in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106#!/usr/bin/env python3"""A Bumble virtual HCI controller for exercising blth without hardware.
Builds a simulated RF link with two controllers on it:
- a connectable peripheral, "FakeSensor", with one GATT service holding a readable/writable characteristic, and pairing enabled (Just Works, LE SC); - a bare controller exposed over TCP (H4), which the OCaml blth host drives.
Because both controllers share one LocalLink, the TCP-side controller reachesthe peripheral. Run this, then (see README.md for why the address flags):
blth scan --tcp 127.0.0.1:9000 blth gatt --tcp 127.0.0.1:9000 --peer F0:F1:F2:F3:F4:F5 --peer-random blth pair --tcp 127.0.0.1:9000 --peer F0:F1:F2:F3:F4:F5 --peer-random \\ --random C0:11:22:33:44:55"""
import asyncio
from bumble.controller import Controllerfrom bumble.link import LocalLinkfrom bumble.device import Devicefrom bumble.host import Hostfrom bumble.hci import Addressfrom bumble.core import AdvertisingDatafrom bumble.gatt import Service, Characteristicfrom bumble.pairing import PairingConfig, PairingDelegatefrom bumble.transport import open_transport
PERIPHERAL_ADDRESS = "F0:F1:F2:F3:F4:F5"SERVICE_UUID = "1815" # Automation IOCHAR_UUID = "2A56" # Digital
async def main(): link = LocalLink()
# The peripheral: its own controller on the link, driven by a Bumble host. # Its address is random (a static random address: top two bits set), because # Bumble's LocalLink sources LE data from the controller's random address -- # so the peer must be reached as a random-address device (blth # --peer-random). adv_controller = Controller( "adv", link=link, public_address=PERIPHERAL_ADDRESS ) adv_host = Host( controller_source=adv_controller, controller_sink=adv_controller ) adv_device = Device( name="FakeSensor", address=Address(PERIPHERAL_ADDRESS, Address.RANDOM_DEVICE_ADDRESS), host=adv_host, ) adv_device.add_service( Service( SERVICE_UUID, [ Characteristic( CHAR_UUID, Characteristic.Properties.READ | Characteristic.Properties.WRITE, Characteristic.READABLE | Characteristic.WRITEABLE, value=b"hello from bumble", ) ], ) ) adv_device.advertising_data = bytes( AdvertisingData([(AdvertisingData.COMPLETE_LOCAL_NAME, b"FakeSensor")]) ) # Accept Just Works LE Secure Connections pairing (no user interaction). adv_device.pairing_config_factory = lambda _conn: PairingConfig( sc=True, mitm=False, bonding=True, delegate=PairingDelegate( io_capability=PairingDelegate.NO_OUTPUT_NO_INPUT ), ) await adv_device.power_on() # auto_restart re-advertises after each disconnect so the commands can run # back to back. await adv_device.start_advertising(auto_restart=True)
# The controller blth drives over TCP. Same link -> it reaches the peer. Its # address is left at the default 00:00:00:00:00:00; blth reads it via Read # BD_ADDR (or overrides it with --random) for the pairing DHKey check. transport = await open_transport("tcp-server:127.0.0.1:9000") Controller( "host", host_source=transport.source, host_sink=transport.sink, link=link, )
print( "Bumble virtual controller on 127.0.0.1:9000; " f"FakeSensor ({PERIPHERAL_ADDRESS}) advertising, connectable, pairable.", flush=True, ) await asyncio.Event().wait()
asyncio.run(main())