Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Documentation/Plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ AppRuntime's contracts.
| arm64 and x86_64 Linux with glibc (Debian 13, Ubuntu 24.04+) | armv7, musl |
| Native slices linked into the host | wasm2c (AppRuntime's shim, as on Darwin); WasmKit for development |
| Wayland and X11 through SDL3; a headless driver for tests | Multiple windows |
| `Renderer`, Bluetooth `Central` and friends, GL surfaces, web views, files, settings, the built-ins | Location (GeoClue), Device (UPower, sysfs), Networking (libcurl), Formatting (ICU), Motion (iio-sensor-proxy), NFC (neard) |
| `Renderer`, `BluetoothCentral` and friends, GL surfaces, web views, files, settings, the built-ins | Location (GeoClue), Device (UPower, sysfs), Networking (libcurl), Formatting (ICU), Motion (iio-sensor-proxy), NFC (neard) |

## 2. Process model

Expand Down Expand Up @@ -89,7 +89,7 @@ timers for `schedule`.
|---|---|
| `LinuxHost` | `AppHost` (built-ins, modules, `rt_main`), `MainLoop`, `MainLoopClock`, the `Bundle` built-in, and `LinuxFiles` and `LinuxDefaults` (§9); over AppRuntime's `RuntimeHost`, which every host shares: `AppInfo` (the bundle's `Info.plist` keys), the compatibility checks, `HostModule`, the `Permissions` and `Application` built-ins, `HostFiles` and `HostDefaults` |
| `LinuxUI` | The SwiftUI player: DarwinAppRuntime's `DarwinUI`, copied by `Scripts/sync-darwinui.sh`, over Cacao's `SwiftUI` and `Combine` modules; `Host.swift` gives it the logger and bundle it takes from its host |
| `LinuxBluetooth` | `Central`, `Peripheral`, `Service`, `Characteristic`, `Descriptor`, `Advertisement` over BlueZ |
| `LinuxBluetooth` | `BluetoothCentral`, `BluetoothPeripheral`, `BluetoothService`, `BluetoothCharacteristic`, `BluetoothDescriptor`, `BluetoothAdvertisement` over BlueZ |
| `LinuxGraphics` | `Surface` over EGL and GLES 2.0, loaded at run time; the player's `GLSurface` view shows it (§7) |
| `LinuxWebKit` | `WebContent` over WPE WebKit, loaded at run time; the player's `WebView` view shows it (§8) |
| `LinuxCoreModel` | `LinuxModelStore`: the model store over CoreModel-SQLite, through AppRuntime's `HostModelStore` (§9) |
Expand Down Expand Up @@ -159,14 +159,14 @@ contract logic is tested with a fake bus.
| `scan` | `SetDiscoveryFilter({Transport: le, UUIDs, DuplicateData})`, `StartDiscovery`; a device is reported when it appears or its `RSSI`, `ManufacturerData` or `ServiceData` changes, once per scan unless duplicates are allowed; services filtered on the host too, since cached devices skip BlueZ's filter |
| `connect` | `Device1.Connect`; `didConnect` once `Connected` and `ServicesResolved`; the timeout is a main-loop timer that disconnects |
| `cancelConnection` | `Device1.Disconnect`, `didDisconnect` with `0` |
| `retrievePeripherals` | Devices by address (`Peripheral.identifier`) |
| `retrievePeripherals` | Devices by address (`BluetoothPeripheral.identifier`) |
| `retrieveConnectedPeripherals` | Connected devices with any of the services in `UUIDs` |
| `discoverServices`, `discoverCharacteristics`, `discoverDescriptors` | BlueZ resolves the database on connection: the `GattService1`, `GattCharacteristic1` and `GattDescriptor1` objects under the device |
| `readValue`, `writeValue` | `ReadValue({})`, `WriteValue(value, {type: request / command})` |
| `setNotifyValue` | `StartNotify` / `StopNotify`; values arrive as `PropertiesChanged` of `Value` |
| `readRSSI` | `Device1.RSSI` (BlueZ reports it while discovering) |
| `maximumWriteLength` | `GattCharacteristic1.MTU` − 3 without response, 512 with |
| `Advertisement` | `Name`, `ManufacturerData` (company ID first, little-endian), `UUIDs`, `ServiceData`, `TxPower` |
| `BluetoothAdvertisement` | `Name`, `ManufacturerData` (company ID first, little-endian), `UUIDs`, `ServiceData`, `TxPower` |

GATT operations are queued per peripheral, and every call gets exactly one
callback; a disconnect fails what's queued with `notConnected`. BlueZ
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ added to its database between launches:
|---|---|
| `LinuxHost` | `AppHost` (built-ins, modules, `rt_main`), `MainLoop` and its clock, the `Bundle` built-in, `LinuxFiles` and `LinuxDefaults`, the app's files and settings in XDG's directories or a container ([plan](Documentation/Plan.md) §9), over AppRuntime's shared `RuntimeHost` (`AppInfo`, compatibility checks, `HostModule`, `Permissions`, `Application`, `HostFiles`, `HostDefaults`) |
| `LinuxUI` | The SwiftUI player: DarwinAppRuntime's `DarwinUI`, copied by `Scripts/sync-darwinui.sh`, over Cacao's `SwiftUI` |
| `LinuxBluetooth` | `Central`, `Peripheral`, `Service`, `Characteristic`, `Descriptor` and `Advertisement` over BlueZ, through `wendylabsinc/dbus` |
| `LinuxBluetooth` | `BluetoothCentral`, `BluetoothPeripheral`, `BluetoothService`, `BluetoothCharacteristic`, `BluetoothDescriptor` and `BluetoothAdvertisement` over BlueZ, through `wendylabsinc/dbus` |
| `LinuxGraphics` | `Surface`: GLES 2.0 drawn in EGL pbuffers, loaded at run time, and read back into the player's `GLSurface` view ([plan](Documentation/Plan.md) §7) |
| `LinuxWebKit` | `WebContent`: WPE WebKit pages, loaded at run time, drawn into shared memory and shown by the player's `WebView` view ([plan](Documentation/Plan.md) §8) |
| `LinuxCoreModel` | `LinuxModelStore`: the app's CoreModel stores in SQLite, through AppRuntime's `HostModelStore` and CoreModel-SQLite ([plan](Documentation/Plan.md) §9) |
Expand Down
18 changes: 9 additions & 9 deletions Sources/LinuxBluetooth/LinuxAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import DynamicBluetooth
import DynamicRuntime

/// A `GattService1` object.
public final class LinuxService: Service {
public final class LinuxService: BluetoothService {

let path: String

Expand All @@ -31,11 +31,11 @@ public final class LinuxService: Service {
object["Primary"]?.boolValue ?? true
}

public func characteristics() -> [any Characteristic] {
public func characteristics() -> [any BluetoothCharacteristic] {
discovered
}

public func peripheral() -> any Peripheral {
public func peripheral() -> any BluetoothPeripheral {
hub.peripheral(object["Device"]?.stringValue ?? Self.parent(of: path))
}

Expand All @@ -45,7 +45,7 @@ public final class LinuxService: Service {
}

/// A `GattCharacteristic1` object.
public final class LinuxCharacteristic: Characteristic {
public final class LinuxCharacteristic: BluetoothCharacteristic {

let path: String

Expand Down Expand Up @@ -89,11 +89,11 @@ public final class LinuxCharacteristic: Characteristic {
notifying ?? object["Notifying"]?.boolValue ?? false
}

public func descriptors() -> [any Descriptor] {
public func descriptors() -> [any BluetoothDescriptor] {
discovered
}

public func service() -> any Service {
public func service() -> any BluetoothService {
hub.service(object["Service"]?.stringValue ?? LinuxService.parent(of: path))
}

Expand All @@ -107,7 +107,7 @@ public final class LinuxCharacteristic: Characteristic {
}

/// A `GattDescriptor1` object.
public final class LinuxDescriptor: Descriptor {
public final class LinuxDescriptor: BluetoothDescriptor {

let path: String

Expand All @@ -134,13 +134,13 @@ public final class LinuxDescriptor: Descriptor {
lastValue ?? object["Value"]?.bytes
}

public func characteristic() -> any Characteristic {
public func characteristic() -> any BluetoothCharacteristic {
hub.characteristic(object["Characteristic"]?.stringValue ?? LinuxService.parent(of: path))
}
}

/// What a device advertised, as BlueZ has it when it's reported.
public final class LinuxAdvertisement: Advertisement {
public final class LinuxAdvertisement: BluetoothAdvertisement {

let device: [String: DBusValue]

Expand Down
32 changes: 16 additions & 16 deletions Sources/LinuxBluetooth/LinuxBluetooth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ public enum LinuxBluetooth: HostModule {

public static var contracts: [ContractDescriptor] {
[
CentralProxy.descriptor,
CentralDelegateProxy.descriptor,
PeripheralProxy.descriptor,
PeripheralDelegateProxy.descriptor,
ServiceProxy.descriptor,
CharacteristicProxy.descriptor,
DescriptorProxy.descriptor,
AdvertisementProxy.descriptor,
BluetoothCentralProxy.descriptor,
BluetoothCentralDelegateProxy.descriptor,
BluetoothPeripheralProxy.descriptor,
BluetoothPeripheralDelegateProxy.descriptor,
BluetoothServiceProxy.descriptor,
BluetoothCharacteristicProxy.descriptor,
BluetoothDescriptorProxy.descriptor,
BluetoothAdvertisementProxy.descriptor,
]
}

public static func register(in registrar: some ContractRegistrar) {
CentralProxy.register(LinuxCentral.self, in: registrar)
PeripheralProxy.register(LinuxPeripheral.self, in: registrar)
ServiceProxy.register(LinuxService.self, in: registrar)
CharacteristicProxy.register(LinuxCharacteristic.self, in: registrar)
DescriptorProxy.register(LinuxDescriptor.self, in: registrar)
AdvertisementProxy.register(LinuxAdvertisement.self, in: registrar)
CentralDelegateProxy.registerInterface(in: registrar)
PeripheralDelegateProxy.registerInterface(in: registrar)
BluetoothCentralProxy.register(LinuxCentral.self, in: registrar)
BluetoothPeripheralProxy.register(LinuxPeripheral.self, in: registrar)
BluetoothServiceProxy.register(LinuxService.self, in: registrar)
BluetoothCharacteristicProxy.register(LinuxCharacteristic.self, in: registrar)
BluetoothDescriptorProxy.register(LinuxDescriptor.self, in: registrar)
BluetoothAdvertisementProxy.register(LinuxAdvertisement.self, in: registrar)
BluetoothCentralDelegateProxy.registerInterface(in: registrar)
BluetoothPeripheralDelegateProxy.registerInterface(in: registrar)
}

/// Desktop Linux has no Bluetooth prompt: a declared `bluetooth`
Expand Down
20 changes: 10 additions & 10 deletions Sources/LinuxBluetooth/LinuxCentral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import DBUS
import DynamicBluetooth
import LinuxHost

/// The `Central` contract over BlueZ (`Plan.md` §6).
/// The `BluetoothCentral` contract over BlueZ (`Plan.md` §6).
///
/// - Authorization is always `allowed`: desktop Linux has no prompt, and
/// the sandbox's `bluetooth` capability gates the contract itself.
Expand All @@ -14,14 +14,14 @@ import LinuxHost
/// - Connection timeouts are main-loop timers; BlueZ's own is long.
///
/// Every call that reports through the delegate reports exactly once.
public final class LinuxCentral: Central {
public final class LinuxCentral: BluetoothCentral {

struct ScanRequest {
var services: [BluetoothUUID]
var allowDuplicates: Bool
}

let delegate: any CentralDelegate
let delegate: any BluetoothCentralDelegate

let hub: BlueZHub

Expand All @@ -39,7 +39,7 @@ public final class LinuxCentral: Central {

private var reportedState: CentralState?

init(delegate: any CentralDelegate, hub: BlueZHub) {
init(delegate: any BluetoothCentralDelegate, hub: BlueZHub) {
self.delegate = delegate
self.hub = hub
}
Expand All @@ -59,11 +59,11 @@ public final class LinuxCentral: Central {
BluetoothAuthorization.allowed.rawValue
}

public static func make(_ delegate: any CentralDelegate) -> LinuxCentral {
public static func make(_ delegate: any BluetoothCentralDelegate) -> LinuxCentral {
make(delegate, hub: .shared)
}

static func make(_ delegate: any CentralDelegate, hub: BlueZHub) -> LinuxCentral {
static func make(_ delegate: any BluetoothCentralDelegate, hub: BlueZHub) -> LinuxCentral {
let central = LinuxCentral(delegate: delegate, hub: hub)
hub.add(central)
LinuxBluetooth.post { [weak central] in
Expand Down Expand Up @@ -145,7 +145,7 @@ public final class LinuxCentral: Central {

// MARK: Connections

public func connect(_ peripheral: any Peripheral, timeout: Int32) {
public func connect(_ peripheral: any BluetoothPeripheral, timeout: Int32) {
guard let peripheral = LinuxBluetooth.resolve(peripheral, as: LinuxPeripheral.self) else {
HostLog.warning("connect: not a peripheral of this host")
return
Expand Down Expand Up @@ -188,7 +188,7 @@ public final class LinuxCentral: Central {
}
}

public func cancelConnection(_ peripheral: any Peripheral) {
public func cancelConnection(_ peripheral: any BluetoothPeripheral) {
guard let peripheral = LinuxBluetooth.resolve(peripheral, as: LinuxPeripheral.self) else { return }
let path = peripheral.path
if let attempt = connecting.removeValue(forKey: path) {
Expand Down Expand Up @@ -217,11 +217,11 @@ public final class LinuxCentral: Central {
}
}

public func retrievePeripherals(_ identifiers: [String]) -> [any Peripheral] {
public func retrievePeripherals(_ identifiers: [String]) -> [any BluetoothPeripheral] {
identifiers.compactMap { hub.devicePath(address: $0).map(hub.peripheral) }
}

public func retrieveConnectedPeripherals(_ services: [BluetoothUUID]) -> [any Peripheral] {
public func retrieveConnectedPeripherals(_ services: [BluetoothUUID]) -> [any BluetoothPeripheral] {
let wanted = Set(services.map(BlueZMapping.bluezUUID))
return hub.devicePaths.filter { path in
guard let device = hub.properties(path, BlueZInterface.device), device["Connected"]?.boolValue == true else {
Expand Down
24 changes: 12 additions & 12 deletions Sources/LinuxBluetooth/LinuxPeripheral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ import DBUS
import DynamicBluetooth
import LinuxHost

/// The `Peripheral` contract over a BlueZ device (`Plan.md` §6).
/// The `BluetoothPeripheral` contract over a BlueZ device (`Plan.md` §6).
///
/// BlueZ resolves the whole GATT database when it connects, so discovery
/// reads `GattService1`, `GattCharacteristic1` and `GattDescriptor1`
/// objects under the device. Operations are queued and run one at a time;
/// each reports exactly once, and a disconnect fails what's queued with
/// `notConnected`.
public final class LinuxPeripheral: Peripheral {
public final class LinuxPeripheral: BluetoothPeripheral {

let path: String

/// Process-wide in the app; strong, so objects the guest keeps never
/// outlive it.
let hub: BlueZHub

private(set) var delegate: (any PeripheralDelegate)?
private(set) var delegate: (any BluetoothPeripheralDelegate)?

/// Services discovered so far.
private var discovered: [LinuxService] = []
Expand Down Expand Up @@ -81,7 +81,7 @@ public final class LinuxPeripheral: Peripheral {
return state.rawValue
}

public func setDelegate(_ delegate: (any PeripheralDelegate)?) {
public func setDelegate(_ delegate: (any BluetoothPeripheralDelegate)?) {
self.delegate = delegate
}

Expand All @@ -106,11 +106,11 @@ public final class LinuxPeripheral: Peripheral {
}
}

public func services() -> [any Service] {
public func services() -> [any BluetoothService] {
discovered
}

public func discoverCharacteristics(_ uuids: [BluetoothUUID], for service: any Service) {
public func discoverCharacteristics(_ uuids: [BluetoothUUID], for service: any BluetoothService) {
guard let service = LinuxBluetooth.resolve(service, as: LinuxService.self) else { return }
let wanted = Set(uuids.map(BlueZMapping.bluezUUID))
enqueue { [self] done in
Expand All @@ -124,7 +124,7 @@ public final class LinuxPeripheral: Peripheral {
}
}

public func discoverDescriptors(for characteristic: any Characteristic) {
public func discoverDescriptors(for characteristic: any BluetoothCharacteristic) {
guard let characteristic = LinuxBluetooth.resolve(characteristic, as: LinuxCharacteristic.self) else { return }
enqueue { [self] done in
characteristic.discovered = hub.children(of: characteristic.path, interface: BlueZInterface.descriptor, parentKey: "Characteristic")
Expand All @@ -136,7 +136,7 @@ public final class LinuxPeripheral: Peripheral {
}
}

public func readValue(_ characteristic: any Characteristic) {
public func readValue(_ characteristic: any BluetoothCharacteristic) {
guard let characteristic = LinuxBluetooth.resolve(characteristic, as: LinuxCharacteristic.self) else { return }
enqueue { [self] done in
characteristic.reading = true
Expand All @@ -157,7 +157,7 @@ public final class LinuxPeripheral: Peripheral {
}
}

public func writeValue(_ value: [UInt8], for characteristic: any Characteristic, type: Int32) {
public func writeValue(_ value: [UInt8], for characteristic: any BluetoothCharacteristic, type: Int32) {
guard let characteristic = LinuxBluetooth.resolve(characteristic, as: LinuxCharacteristic.self) else { return }
let withResponse = type != WriteType.withoutResponse.rawValue
enqueue { [self] done in
Expand All @@ -178,7 +178,7 @@ public final class LinuxPeripheral: Peripheral {
}
}

public func setNotifyValue(_ enabled: Bool, for characteristic: any Characteristic) {
public func setNotifyValue(_ enabled: Bool, for characteristic: any BluetoothCharacteristic) {
guard let characteristic = LinuxBluetooth.resolve(characteristic, as: LinuxCharacteristic.self) else { return }
enqueue { [self] done in
gatt(characteristic.path, BlueZInterface.characteristic, enabled ? "StartNotify" : "StopNotify", [], nil) { [self] result in
Expand All @@ -200,7 +200,7 @@ public final class LinuxPeripheral: Peripheral {
}
}

public func readDescriptor(_ descriptor: any Descriptor) {
public func readDescriptor(_ descriptor: any BluetoothDescriptor) {
guard let descriptor = LinuxBluetooth.resolve(descriptor, as: LinuxDescriptor.self) else { return }
enqueue { [self] done in
gatt(descriptor.path, BlueZInterface.descriptor, "ReadValue", [.options([:])], "a{sv}") { [self] result in
Expand All @@ -219,7 +219,7 @@ public final class LinuxPeripheral: Peripheral {
}
}

public func writeDescriptor(_ value: [UInt8], for descriptor: any Descriptor) {
public func writeDescriptor(_ value: [UInt8], for descriptor: any BluetoothDescriptor) {
guard let descriptor = LinuxBluetooth.resolve(descriptor, as: LinuxDescriptor.self) else { return }
enqueue { [self] done in
gatt(descriptor.path, BlueZInterface.descriptor, "WriteValue", [.byteArray(value), .options([:])], "aya{sv}") { [self] result in
Expand Down
Loading
Loading