|
| 1 | +# SPDX-License-Identifier: MIT |
| 2 | +# Copyright (c) 2024 Victor Chavez |
| 3 | +"""Bumble backend.""" |
| 4 | +import os |
| 5 | +from enum import Enum |
| 6 | +from typing import Dict, Final, Optional |
| 7 | + |
| 8 | +from bumble.controller import Controller |
| 9 | +from bumble.link import LocalLink |
| 10 | +from bumble.transport import Transport, open_transport |
| 11 | + |
| 12 | +transports: Dict[str, Transport] = {} |
| 13 | +_link: Final = LocalLink() |
| 14 | +_scheme_delimiter: Final = ":" |
| 15 | + |
| 16 | +_env_transport_cfg: Final = os.getenv("BLEAK_BUMBLE") |
| 17 | +_env_host_mode: Final = os.getenv("BLEAK_BUMBLE_HOST") |
| 18 | + |
| 19 | + |
| 20 | +class TransportScheme(Enum): |
| 21 | + """The transport schemes supported by bumble. |
| 22 | +
|
| 23 | + https://google.github.io/bumble/transports |
| 24 | + """ |
| 25 | + |
| 26 | + SERIAL = "serial" |
| 27 | + """: The serial transport implements sending/receiving HCI |
| 28 | + packets over a UART (a.k.a serial port). |
| 29 | + """ |
| 30 | + UDP = "udp" |
| 31 | + """: The UDP transport is a UDP socket, receiving packets on a specified port number, |
| 32 | + and sending packets to a specified host and port number. |
| 33 | + """ |
| 34 | + TCP_CLIENT = "tcp-client" |
| 35 | + """: The TCP Client transport uses an outgoing TCP connection. |
| 36 | + """ |
| 37 | + TCP_SERVER = "tcp-server" |
| 38 | + """: The TCP Server transport uses an incoming TCP connection. |
| 39 | + """ |
| 40 | + WS_CLIENT = "ws-client" |
| 41 | + """: The WebSocket Client transport is WebSocket connection |
| 42 | + to a WebSocket server over which HCI packets are sent and received. |
| 43 | + """ |
| 44 | + WS_SERVER = "ws-server" |
| 45 | + """: The WebSocket Server transport is WebSocket server that accepts |
| 46 | + connections from a WebSocket client. HCI packets are sent and received over the connection. |
| 47 | + """ |
| 48 | + PTY = "pty" |
| 49 | + """: The PTY transport uses a Unix pseudo-terminal device to communicate |
| 50 | + with another process on the host, as if it were over a serial port. |
| 51 | + """ |
| 52 | + FILE = "file" |
| 53 | + """: The File transport allows opening any named entry on a filesystem |
| 54 | + and use it for HCI transport I/O. This is typically used to open a PTY, |
| 55 | + or unix driver, not for real files. |
| 56 | + """ |
| 57 | + VHCI = "vhci" |
| 58 | + """: The VHCI transport allows attaching a virtual controller |
| 59 | + to the Bluetooth stack on operating systems that offer a |
| 60 | + VHCI driver (Linux, if enabled, maybe others). |
| 61 | + """ |
| 62 | + HCI_SOCKET = "hci-socket" |
| 63 | + """: An HCI Socket can send/receive HCI packets to/from a |
| 64 | + Bluetooth HCI controller managed by the host OS. |
| 65 | + This is only supported on some platforms (currently only tested on Linux). |
| 66 | + """ |
| 67 | + USB = "usb" |
| 68 | + """: The USB transport interfaces with a local Bluetooth USB dongle. |
| 69 | + """ |
| 70 | + ANDROID_NETSIM = "android-netsim" |
| 71 | + """: The Android "netsim" transport either connects, as a host, to a |
| 72 | + Netsim virtual controller ("host" mode), or acts as a virtual |
| 73 | + controller itself ("controller" mode) accepting host connections. |
| 74 | + """ |
| 75 | + |
| 76 | + @classmethod |
| 77 | + def from_string(cls, value: str) -> "TransportScheme": |
| 78 | + try: |
| 79 | + return cls(value) |
| 80 | + except ValueError: |
| 81 | + raise ValueError(f"'{value}' is not a valid TransportScheme") |
| 82 | + |
| 83 | + |
| 84 | +class BumbleTransportCfg: |
| 85 | + """Transport configuration for bumble. |
| 86 | +
|
| 87 | + Args: |
| 88 | + scheme (TransportScheme): The transport scheme supported by bumble. |
| 89 | + args (Optional[str]): The arguments used to initialize the transport. |
| 90 | + See https://google.github.io/bumble/transports/index.html |
| 91 | + """ |
| 92 | + |
| 93 | + def __init__(self, scheme: TransportScheme, args: Optional[str] = None): |
| 94 | + self.scheme: Final = scheme |
| 95 | + self.args: Final = args |
| 96 | + |
| 97 | + def __str__(self): |
| 98 | + return f"{self.scheme.value}:{self.args}" if self.args else self.scheme.value |
| 99 | + |
| 100 | + |
| 101 | +def get_default_transport_cfg() -> BumbleTransportCfg: |
| 102 | + if _env_transport_cfg: |
| 103 | + scheme_val, *args = _env_transport_cfg.split(_scheme_delimiter, 1) |
| 104 | + return BumbleTransportCfg( |
| 105 | + TransportScheme.from_string(scheme_val), args[0] if args else None |
| 106 | + ) |
| 107 | + |
| 108 | + return BumbleTransportCfg(TransportScheme.TCP_SERVER, "127.0.0.1:1234") |
| 109 | + |
| 110 | + |
| 111 | +def get_default_host_mode() -> bool: |
| 112 | + return True if _env_host_mode else False |
| 113 | + |
| 114 | + |
| 115 | +async def start_transport( |
| 116 | + cfg: BumbleTransportCfg, host_mode: bool = get_default_host_mode() |
| 117 | +) -> Transport: |
| 118 | + transport_cmd = str(cfg) |
| 119 | + if transport_cmd not in transports.keys(): |
| 120 | + transports[transport_cmd] = await open_transport(transport_cmd) |
| 121 | + if not host_mode: |
| 122 | + Controller( |
| 123 | + "ext", |
| 124 | + host_source=transports[transport_cmd].source, |
| 125 | + host_sink=transports[transport_cmd].sink, |
| 126 | + link=_link, |
| 127 | + ) |
| 128 | + return transports[transport_cmd] |
| 129 | + |
| 130 | + |
| 131 | +def get_link(): |
| 132 | + # Assume all transports are linked |
| 133 | + return _link |
0 commit comments