|
1 |
+ |
//! Bluetooth devices and what state they are actually in: the `alloy bluetooth` verb.
|
|
2 |
+ |
//!
|
|
3 |
+ |
//! docs/STACK.md and `etc/systemd/system-preset/50-alloy.preset` both already
|
|
4 |
+ |
//! say bluez is kept and that what is missing is a surface. This is the
|
|
5 |
+ |
//! surface.
|
|
6 |
+ |
//!
|
|
7 |
+ |
//! # Why this verb exists
|
|
8 |
+ |
//!
|
|
9 |
+ |
//! Not because pairing is hard. `bluetoothctl` pairs fine. It exists because
|
|
10 |
+ |
//! every Bluetooth interface anyone has used collapses five independent facts
|
|
11 |
+ |
//! into one Connected toggle, and then that toggle lies. The state bluez keeps
|
|
12 |
+ |
//! per device is:
|
|
13 |
+ |
//!
|
|
14 |
+ |
//! - **Paired**: keys have been exchanged.
|
|
15 |
+ |
//! - **Bonded**: those keys were written down, so they survive a reboot.
|
|
16 |
+ |
//! - **Trusted**: bluez will accept a connection this device starts, without
|
|
17 |
+ |
//! a human agreeing to it first.
|
|
18 |
+ |
//! - **Blocked**: bluez refuses the device outright.
|
|
19 |
+ |
//! - **Connected**: a link is up right now.
|
|
20 |
+ |
//!
|
|
21 |
+ |
//! They are independent. The one that decides whether a device ever comes back
|
|
22 |
+ |
//! on its own is Trusted, and pairing does not set it. Measured on fw13
|
|
23 |
+ |
//! 2026-08-05: both of this machine's mice report `Paired: yes, Bonded: yes,
|
|
24 |
+ |
//! Trusted: no`, which is the exact combination that reads to a user as
|
|
25 |
+ |
//! "Bluetooth keeps forgetting my mouse". No graphical panel on any operating
|
|
26 |
+ |
//! system shows that field.
|
|
27 |
+ |
//!
|
|
28 |
+ |
//! So this screen shows all five, raw, and then says in a sentence what the
|
|
29 |
+ |
//! combination means and which key changes it. [`Standing`] is that mapping and
|
|
30 |
+ |
//! is the substance of the verb. Everything else here is plumbing to feed it.
|
|
31 |
+ |
//!
|
|
32 |
+ |
//! # Nothing is automatic
|
|
33 |
+ |
//!
|
|
34 |
+ |
//! Nothing on this screen acts without a keypress, and that follows from the
|
|
35 |
+ |
//! same premise. Bluetooth is unpleasant precisely where it tries to be magic:
|
|
36 |
+ |
//! a device that connects itself, moves the audio out from under what is
|
|
37 |
+ |
//! playing, and then does not do it the next time is worse than one that waits
|
|
38 |
+ |
//! to be told. The console offers `d` to hand a connected headset to the
|
|
39 |
+ |
//! default sink; it does not take it.
|
|
40 |
+ |
//!
|
|
41 |
+ |
//! # Not powered has two causes
|
|
42 |
+ |
//!
|
|
43 |
+ |
//! `Powered: no` from bluez and a radio killed by rfkill look identical from
|
|
44 |
+ |
//! the adapter, and they have different fixes: the first is `power on`, the
|
|
45 |
+ |
//! second is a laptop function key the console cannot press. Reading rfkill
|
|
46 |
+ |
//! alongside `show` is the difference between "press w" and a `power on` that
|
|
47 |
+ |
//! fails with a D-Bus error nobody can act on. See [`Radio`].
|
|
48 |
+ |
//!
|
|
49 |
+ |
//! # Scanning suspends rather than polling
|
|
50 |
+ |
//!
|
|
51 |
+ |
//! Discovery is a session held open for as long as a client holds it, so a
|
|
52 |
+ |
//! one-shot `scan on` that exits discovers nothing. The console hands the
|
|
53 |
+ |
//! terminal to `bluetoothctl --timeout` instead, the user watches real
|
|
54 |
+ |
//! discovery output for [`SCAN_SECONDS`], and the list is re-read on the way
|
|
55 |
+ |
//! back. Pairing suspends for the same reason plus a better one: a device that
|
|
56 |
+ |
//! wants a passkey confirmed needs a terminal to ask through, and a captured
|
|
57 |
+ |
//! child has none.
|
|
58 |
+ |
//!
|
|
59 |
+ |
//! <!-- wiki: alloy-console -->
|
|
60 |
+ |
|
|
61 |
+ |
use anyhow::{Context, Result};
|
|
62 |
+ |
use serde::Deserialize;
|
|
63 |
+ |
|
|
64 |
+ |
use alloy_tui::keys::Action;
|
|
65 |
+ |
use alloy_tui::{
|
|
66 |
+ |
AlloyBlock, AlloyList, AlloyTabs, Cursor, FocusRing, Hint, KeyGroup, Severity, Theme, binding,
|
|
67 |
+ |
hint, text, unavailable,
|
|
68 |
+ |
};
|
|
69 |
+ |
use ratatui::Frame;
|
|
70 |
+ |
use ratatui::crossterm::event::{KeyCode, KeyEvent};
|
|
71 |
+ |
use ratatui::layout::{Constraint, Layout, Rect};
|
|
72 |
+ |
use ratatui::text::{Line, Span};
|
|
73 |
+ |
use ratatui::widgets::{Paragraph, Wrap};
|
|
74 |
+ |
|
|
75 |
+ |
use crate::cli::{CommandLog, Invocation};
|
|
76 |
+ |
use crate::shell::{Confirm, Flow, View, block_title, truncate};
|
|
77 |
+ |
|
|
78 |
+ |
/// How long a scan runs before the terminal comes back.
|
|
79 |
+ |
///
|
|
80 |
+ |
/// Long enough for a headset in pairing mode to advertise, short enough that a
|
|
81 |
+ |
/// user who pressed the key by accident is not stuck watching output. Discovery
|
|
82 |
+ |
/// stops when bluetoothctl exits, so this is also how long the adapter is
|
|
83 |
+ |
/// discovering at all.
|
|
84 |
+ |
const SCAN_SECONDS: u32 = 15;
|
|
85 |
+ |
|
|
86 |
+ |
// ---- the model ----
|
|
87 |
+ |
|
|
88 |
+ |
/// The controller, from `bluetoothctl show`.
|
|
89 |
+ |
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
90 |
+ |
pub(crate) struct Adapter {
|
|
91 |
+ |
pub address: String,
|
|
92 |
+ |
/// The controller's name, which is what a phone scanning for this machine
|
|
93 |
+ |
/// sees. `Alias` rather than `Name`: alias is the settable one and is what
|
|
94 |
+ |
/// bluez advertises when the two differ.
|
|
95 |
+ |
pub alias: String,
|
|
96 |
+ |
pub powered: bool,
|
|
97 |
+ |
pub discoverable: bool,
|
|
98 |
+ |
pub pairable: bool,
|
|
99 |
+ |
pub discovering: bool,
|
|
100 |
+ |
}
|
|
101 |
+ |
|
|
102 |
+ |
/// rfkill's verdict on the Bluetooth radio.
|
|
103 |
+ |
///
|
|
104 |
+ |
/// Separate from [`Adapter::powered`] because they answer different questions
|
|
105 |
+ |
/// and only one of them the console can act on. A soft block is software and
|
|
106 |
+ |
/// `rfkill unblock bluetooth` clears it. A hard block is a physical switch or a
|
|
107 |
+ |
/// firmware key, and no command clears it, so saying "press w" there would be
|
|
108 |
+ |
/// advice that cannot work.
|
|
109 |
+ |
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
110 |
+ |
pub(crate) enum Radio {
|
|
111 |
+ |
Unblocked,
|
|
112 |
+ |
Soft,
|
|
113 |
+ |
Hard,
|
|
114 |
+ |
/// rfkill is absent, or lists no Bluetooth line. Reported as unknown rather
|
|
115 |
+ |
/// than assumed unblocked: a missing answer is not a negative one.
|
|
116 |
+ |
Unknown,
|
|
117 |
+ |
}
|
|
118 |
+ |
|
|
119 |
+ |
impl Radio {
|
|
120 |
+ |
/// Why the adapter is down, phrased to be the whole of what the user needs
|
|
121 |
+ |
/// to do next. `None` where the radio is not the reason.
|
|
122 |
+ |
pub(crate) const fn blocker(self) -> Option<&'static str> {
|
|
123 |
+ |
match self {
|
|
124 |
+ |
// Named as a switch rather than as rfkill: the fix is a key on the
|
|
125 |
+ |
// keyboard, and the tool that reported it is not the tool that
|
|
126 |
+ |
// fixes it.
|
|
127 |
+ |
Self::Hard => {
|
|
128 |
+ |
Some("the radio is blocked by a hardware switch, which no command can clear")
|
|
129 |
+ |
}
|
|
130 |
+ |
Self::Soft => Some("the radio is soft-blocked (rfkill unblock bluetooth)"),
|
|
131 |
+ |
Self::Unblocked | Self::Unknown => None,
|
|
132 |
+ |
}
|
|
133 |
+ |
}
|
|
134 |
+ |
}
|
|
135 |
+ |
|
|
136 |
+ |
/// One device bluez knows about, with every state field it keeps.
|
|
137 |
+ |
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
138 |
+ |
pub(crate) struct Device {
|
|
139 |
+ |
pub address: String,
|
|
140 |
+ |
pub name: String,
|
|
141 |
+ |
/// `public` or `random`. Kept because a random address is why a device can
|
|
142 |
+ |
/// appear twice under two addresses, which is otherwise unexplainable.
|
|
143 |
+ |
pub address_type: Option<String>,
|
|
144 |
+ |
/// bluez's `Icon`: `audio-headset`, `input-mouse`, `phone`. Absent on
|
|
145 |
+ |
/// plenty of BLE devices, which is itself worth showing.
|
|
146 |
+ |
pub icon: Option<String>,
|
|
147 |
+ |
pub paired: bool,
|
|
148 |
+ |
pub bonded: bool,
|
|
149 |
+ |
pub trusted: bool,
|
|
150 |
+ |
pub blocked: bool,
|
|
151 |
+ |
pub connected: bool,
|
|
152 |
+ |
/// `Battery Percentage`, which bluez only publishes for a connected device
|
|
153 |
+ |
/// that reports it.
|
|
154 |
+ |
pub battery: Option<u8>,
|
|
155 |
+ |
}
|
|
156 |
+ |
|
|
157 |
+ |
/// What the five booleans add up to, and the whole point of the verb.
|
|
158 |
+ |
///
|
|
159 |
+ |
/// Ordered by what decides the answer rather than by severity: blocked beats
|
|
160 |
+ |
/// everything because nothing else on the screen works while it holds, and
|
|
161 |
+ |
/// connected is reported before paired because it is the more immediate fact.
|
|
162 |
+ |
/// Trusted is never folded away, in either direction, since it is the field the
|
|
163 |
+ |
/// user came here without knowing about.
|
|
164 |
+ |
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
165 |
+ |
pub(crate) enum Standing {
|
|
166 |
+ |
/// bluez refuses this device outright.
|
|
167 |
+ |
Blocked,
|
|
168 |
+ |
/// Connected and trusted. What people mean when they say paired.
|
|
169 |
+ |
Connected,
|
|
170 |
+ |
/// Connected right now, and it will not come back on its own.
|
|
171 |
+ |
ConnectedUntrusted,
|
|
172 |
+ |
/// Keys exchanged and trusted, no link up at the moment.
|
|
173 |
+ |
Trusted,
|
|
174 |
+ |
/// Keys exchanged, not trusted, not connected. The state that reads as
|
|
175 |
+ |
/// Bluetooth having forgotten the device.
|
|
176 |
+ |
Paired,
|
|
177 |
+ |
/// An address bluez has seen and holds no keys for.
|
|
178 |
+ |
Seen,
|
|
179 |
+ |
}
|
|
180 |
+ |
|
|
181 |
+ |
impl Standing {
|
|
182 |
+ |
/// Read the five fields.
|
|
183 |
+ |
pub(crate) const fn of(device: &Device) -> Self {
|
|
184 |
+ |
if device.blocked {
|
|
185 |
+ |
Self::Blocked
|
|
186 |
+ |
} else if device.connected {
|
|
187 |
+ |
if device.trusted {
|
|
188 |
+ |
Self::Connected
|
|
189 |
+ |
} else {
|
|
190 |
+ |
Self::ConnectedUntrusted
|
|
191 |
+ |
}
|
|
192 |
+ |
} else if device.paired {
|
|
193 |
+ |
if device.trusted {
|
|
194 |
+ |
Self::Trusted
|
|
195 |
+ |
} else {
|
|
196 |
+ |
Self::Paired
|
|
197 |
+ |
}
|
|
198 |
+ |
} else {
|
|
199 |
+ |
Self::Seen
|
|
200 |
+ |
}
|
|
201 |
+ |
}
|
|
202 |
+ |
|
|
203 |
+ |
/// The row label. Long enough to carry the trust half, because a row that
|
|
204 |
+ |
/// says only "connected" is the lie this verb exists to stop telling.
|
|
205 |
+ |
pub(crate) const fn label(self) -> &'static str {
|
|
206 |
+ |
match self {
|
|
207 |
+ |
Self::Blocked => "blocked",
|
|
208 |
+ |
Self::Connected => "connected",
|
|
209 |
+ |
Self::ConnectedUntrusted => "connected, untrusted",
|
|
210 |
+ |
Self::Trusted => "trusted, not connected",
|
|
211 |
+ |
Self::Paired => "paired, untrusted",
|
|
212 |
+ |
Self::Seen => "not paired",
|
|
213 |
+ |
}
|
|
214 |
+ |
}
|
|
215 |
+ |
|
|
216 |
+ |
pub(crate) const fn severity(self) -> Severity {
|
|
217 |
+ |
match self {
|
|
218 |
+ |
Self::Blocked => Severity::Error,
|
|
219 |
+ |
Self::Connected => Severity::Healthy,
|
|
220 |
+ |
// Warn rather than Healthy: it works now and will stop working
|
|
221 |
+ |
// later, which is the case a color should catch.
|
|
222 |
+ |
Self::ConnectedUntrusted | Self::Paired => Severity::Warn,
|
|
223 |
+ |
Self::Trusted | Self::Seen => Severity::Info,
|
|
224 |
+ |
}
|
|
225 |
+ |
}
|
|
226 |
+ |
|
|
227 |
+ |
/// What this combination means, and which key changes it.
|
|
228 |
+ |
///
|
|
229 |
+ |
/// The text a user actually came for. Written as plain sentences rather
|
|
230 |
+ |
/// than as field documentation, because the reader is someone whose mouse
|
|
231 |
+ |
/// stopped working and not someone reading the bluez API.
|
|
232 |
+ |
pub(crate) const fn explain(self) -> &'static str {
|
|
233 |
+ |
match self {
|
|
234 |
+ |
Self::Blocked => {
|
|
235 |
+ |
"bluez is refusing this device. Blocked means it will not connect and will not \
|
|
236 |
+ |
pair, and no other key on this screen will change that while it holds. Press b \
|
|
237 |
+ |
to unblock it."
|
|
238 |
+ |
}
|
|
239 |
+ |
Self::Connected => {
|
|
240 |
+ |
"Connected, and trusted, so bluez accepts it back on its own whenever it is in \
|
|
241 |
+ |
range. This is the state most people mean by paired. Press c to disconnect."
|
|
242 |
+ |
}
|
|
243 |
+ |
Self::ConnectedUntrusted => {
|
|
244 |
+ |
"Connected right now, and not trusted. bluez will not accept a connection it did \
|
|
245 |
+ |
not ask for, so once this device goes out of range or the machine reboots, it \
|
|
246 |
+ |
will not come back on its own. Press t to trust it."
|
|
247 |
+ |
}
|
|
248 |
+ |
Self::Trusted => {
|
|
249 |
+ |
"Paired and trusted, so bluez accepts it whenever it is in range. No link is up \
|
|
250 |
+ |
at the moment, which usually means the device is off or out of range. Press c \
|
|
251 |
+ |
to connect it now."
|
|
252 |
+ |
}
|
|
253 |
+ |
Self::Paired => {
|
|
254 |
+ |
"Paired, so the keys are exchanged, and not trusted, so bluez will not accept it \
|
|
255 |
+ |
back on its own. This is the state that reads as Bluetooth having forgotten the \
|
|
256 |
+ |
device. Press t to trust it, or c to connect it this once."
|
|
257 |
+ |
}
|
|
258 |
+ |
Self::Seen => {
|
|
259 |
+ |
"bluez has seen this address and holds no keys for it. Press p to pair, which \
|
|
260 |
+ |
exchanges keys and moves it to the known list."
|
|
261 |
+ |
}
|
|
262 |
+ |
}
|
|
263 |
+ |
}
|
|
264 |
+ |
}
|
|
265 |
+ |
|
|
266 |
+ |
impl Device {
|
|
267 |
+ |
pub(crate) const fn standing(&self) -> Standing {
|
|
268 |
+ |
Standing::of(self)
|
|
269 |
+ |
}
|
|
270 |
+ |
|
|
271 |
+ |
/// A short word for the row, from bluez's icon.
|
|
272 |
+ |
///
|
|
273 |
+ |
/// The icon is a freedesktop name (`audio-headset`, `input-mouse`), which
|
|
274 |
+ |
/// is a fine thing to key off and a poor thing to show. Anything
|
|
275 |
+ |
/// unrecognized is passed through rather than mapped to "device": the raw
|
|
276 |
+ |
/// name is more informative than a shrug.
|
|
277 |
+ |
pub(crate) fn kind(&self) -> &str {
|
|
278 |
+ |
match self.icon.as_deref() {
|
|
279 |
+ |
None => "unknown",
|
|
280 |
+ |
Some("audio-headset" | "audio-headphones") => "headset",
|
|
281 |
+ |
Some("audio-card") => "speaker",
|
|
282 |
+ |
Some("input-mouse") => "mouse",
|
|
283 |
+ |
Some("input-keyboard") => "keyboard",
|
|
284 |
+ |
Some("input-gaming") => "gamepad",
|
|
285 |
+ |
Some("input-tablet") => "tablet",
|
|
286 |
+ |
Some(other) => other,
|
|
287 |
+ |
}
|
|
288 |
+ |
}
|
|
289 |
+ |
|
|
290 |
+ |
/// Whether this is something audio would come out of.
|
|
291 |
+ |
///
|
|
292 |
+ |
/// Gates the default-sink handoff. bluez's icon is the only classification
|
|
293 |
+ |
/// available before connecting, and it is right often enough; a device that
|
|
294 |
+ |
/// is wrong about it still gets the key, and PipeWire's answer (no matching
|
|
295 |
+ |
/// sink) is the honest refusal.
|
|
296 |
+ |
pub(crate) fn is_audio(&self) -> bool {
|
|
297 |
+ |
self.icon
|
|
298 |
+ |
.as_deref()
|
|
299 |
+ |
.is_some_and(|icon| icon.starts_with("audio"))
|
|
300 |
+ |
}
|
|
301 |
+ |
|
|
302 |
+ |
/// The extra fact worth saying when the headline does not cover it.
|
|
303 |
+ |
///
|
|
304 |
+ |
/// Paired without bonded means the keys were never written down, so the
|
|
305 |
+ |
/// pairing does not survive a reboot. It is rare and it is invisible in
|
|
306 |
+ |
/// every other tool, which is exactly the combination that earns a line.
|
|
307 |
+ |
pub(crate) const fn caveat(&self) -> Option<&'static str> {
|
|
308 |
+ |
if self.paired && !self.bonded {
|
|
309 |
+ |
Some(
|
|
310 |
+ |
"Paired but not bonded: the keys were not written to disk, so this pairing does \
|
|
311 |
+ |
not survive a reboot.",
|
|
312 |
+ |
)
|
|
313 |
+ |
} else {
|
|
314 |
+ |
None
|
|
315 |
+ |
}
|
|
316 |
+ |
}
|
|
317 |
+ |
|
|
318 |
+ |
/// The PipeWire sink name prefix this device's audio would arrive under.
|
|
319 |
+ |
///
|
|
320 |
+ |
/// bluez-to-PipeWire naming, measured rather than guessed: a device at
|
|
321 |
+ |
/// `D2:0F:A1:0C:48:3F` becomes `bluez_output.D2_0F_A1_0C_48_3F.1`, with a
|
|
322 |
+ |
/// trailing profile index that is not predictable. So this is a prefix and
|
|
323 |
+ |
/// the caller matches on it.
|
|
324 |
+ |
fn sink_prefix(&self) -> String {
|
|
325 |
+ |
format!("bluez_output.{}", self.address.replace(':', "_"))
|
|
326 |
+ |
}
|
|
327 |
+ |
}
|
|
328 |
+ |
|
|
329 |
+ |
// ---- backends ----
|
|
330 |
+ |
|
|
331 |
+ |
/// Backends build argv and run nothing. The view executes through the log,
|
|
332 |
+ |
/// which is what makes docs/CONSOLE.md's coverage promise structural rather
|
|
333 |
+ |
/// than a habit.
|
|
334 |
+ |
pub(crate) trait Backend {
|
|
335 |
+ |
fn name(&self) -> &'static str;
|
|
336 |
+ |
|
|
337 |
+ |
/// The controller. An error here is the whole screen's error: with no
|
|
338 |
+ |
/// adapter there is nothing to list.
|
|
339 |
+ |
fn adapter(&self, log: &mut CommandLog) -> Result<Adapter>;
|
|
340 |
+ |
|
|
341 |
+ |
/// Every address bluez knows, as `(address, name)`. The state fields come
|
|
342 |
+ |
/// from [`Backend::info`].
|
|
343 |
+ |
fn devices(&self, log: &mut CommandLog) -> Result<Vec<(String, String)>>;
|
|
344 |
+ |
|
|
345 |
+ |
/// Everything bluez keeps about one device.
|
|
346 |
+ |
fn info(&self, address: &str, log: &mut CommandLog) -> Result<Device>;
|
|
347 |
+ |
|
|
348 |
+ |
/// The radio, read without logging. rfkill is the console's own
|
|
349 |
+ |
/// bookkeeping: the user asked about Bluetooth, not about rfkill, and the
|
|
350 |
+ |
/// answer only ever appears as a phrase in the header.
|
|
351 |
+ |
fn radio(&self) -> Radio {
|
|
352 |
+ |
Radio::Unknown
|
|
353 |
+ |
}
|
|
354 |
+ |
|
|
355 |
+ |
fn pair(&self, _device: &Device) -> Option<Invocation> {
|
|
356 |
+ |
None
|
|
357 |
+ |
}
|
|
358 |
+ |
|
|
359 |
+ |
fn connect(&self, _device: &Device) -> Option<Invocation> {
|
|
360 |
+ |
None
|
|
361 |
+ |
}
|
|
362 |
+ |
|
|
363 |
+ |
fn disconnect(&self, _device: &Device) -> Option<Invocation> {
|
|
364 |
+ |
None
|
|
365 |
+ |
}
|
|
366 |
+ |
|
|
367 |
+ |
fn trust(&self, _device: &Device, _trusted: bool) -> Option<Invocation> {
|
|
368 |
+ |
None
|
|
369 |
+ |
}
|
|
370 |
+ |
|
|
371 |
+ |
fn block(&self, _device: &Device, _blocked: bool) -> Option<Invocation> {
|
|
372 |
+ |
None
|
|
373 |
+ |
}
|
|
374 |
+ |
|
|
375 |
+ |
/// Forget the device: drops the keys as well as the entry.
|
|
376 |
+ |
fn remove(&self, _device: &Device) -> Option<Invocation> {
|
|
377 |
+ |
None
|
|
378 |
+ |
}
|
|
379 |
+ |
|
|
380 |
+ |
fn power(&self, _on: bool) -> Option<Invocation> {
|
|
381 |
+ |
None
|
|
382 |
+ |
}
|
|
383 |
+ |
|
|
384 |
+ |
/// A bounded discovery run, handed the terminal rather than captured.
|
|
385 |
+ |
fn scan(&self) -> Option<Invocation> {
|
|
386 |
+ |
None
|
|
387 |
+ |
}
|
|
388 |
+ |
|
|
389 |
+ |
/// List PipeWire's sinks, for finding the one a connected headset landed on.
|
|
390 |
+ |
fn sinks(&self) -> Option<Invocation> {
|
|
391 |
+ |
None
|
|
392 |
+ |
}
|
|
393 |
+ |
|
|
394 |
+ |
fn set_default_sink(&self, _sink: &str) -> Option<Invocation> {
|
|
395 |
+ |
None
|
|
396 |
+ |
}
|
|
397 |
+ |
}
|
|
398 |
+ |
|
|
399 |
+ |
/// Pick a backend: the real one when `bluetoothctl` answers, the mock
|
|
400 |
+ |
/// otherwise.
|
|
401 |
+ |
///
|
|
402 |
+ |
/// A `--version` probe rather than a `which` check, matching every other verb.
|
|
403 |
+ |
/// Note that this probes the client and not the daemon: bluetoothctl answers
|
|
404 |
+ |
/// `--version` with bluetoothd down. That is deliberate, because the daemon
|
|
405 |
+ |
/// being down is a state the screen should report rather than one that should
|
|
406 |
+ |
/// drop it to mock devices, and [`Backend::adapter`] is where it surfaces.
|
|
407 |
+ |
pub(crate) fn detect() -> Box<dyn Backend> {
|
|
408 |
+ |
if Invocation::new("bluetoothctl").arg("--version").probe() {
|
|
409 |
+ |
Box::new(BluetoothCtl {
|
|
410 |
+ |
pactl: Invocation::new("pactl").arg("--version").probe(),
|
|
411 |
+ |
})
|
|
412 |
+ |
} else {
|
|
413 |
+ |
Box::new(Mock)
|
|
414 |
+ |
}
|
|
415 |
+ |
}
|
|
416 |
+ |
|
|
417 |
+ |
pub(crate) struct BluetoothCtl {
|
|
418 |
+ |
/// Whether the audio handoff is offered. Read once at construction, like
|
|
419 |
+ |
/// `disk`'s udisks flag.
|
|
420 |
+ |
pactl: bool,
|
|
421 |
+ |
}
|
|
422 |
+ |
|
|
423 |
+ |
impl BluetoothCtl {
|
|
424 |
+ |
/// The pairing agent capability.
|
|
425 |
+ |
///
|
|
426 |
+ |
/// `NoInputNoOutput` would suppress the passkey prompt entirely and pair
|
|
427 |
+ |
/// silently, which is the wrong trade here: a device asking a human to
|
|
428 |
+ |
/// confirm a number is the one moment Bluetooth's security model is
|
|
429 |
+ |
/// visible, and suppressing it to save a keystroke is how pairing becomes
|
|
430 |
+ |
/// magic. `KeyboardDisplay` says the terminal can both show and answer,
|
|
431 |
+ |
/// which is true, because pairing suspends onto a real one.
|
|
432 |
+ |
const AGENT: &'static str = "KeyboardDisplay";
|
|
433 |
+ |
|
|
434 |
+ |
fn ctl() -> Invocation {
|
|
435 |
+ |
Invocation::new("bluetoothctl")
|
|
436 |
+ |
}
|
|
437 |
+ |
}
|
|
438 |
+ |
|
|
439 |
+ |
impl Backend for BluetoothCtl {
|
|
440 |
+ |
fn name(&self) -> &'static str {
|
|
441 |
+ |
"bluetoothctl"
|
|
442 |
+ |
}
|
|
443 |
+ |
|
|
444 |
+ |
fn adapter(&self, log: &mut CommandLog) -> Result<Adapter> {
|
|
445 |
+ |
parse_show(&Self::ctl().arg("show").run(log)?)
|
|
446 |
+ |
}
|
|
447 |
+ |
|
|
448 |
+ |
fn devices(&self, log: &mut CommandLog) -> Result<Vec<(String, String)>> {
|
|
449 |
+ |
Ok(parse_device_list(&Self::ctl().arg("devices").run(log)?))
|
|
450 |
+ |
}
|
|
451 |
+ |
|
|
452 |
+ |
fn info(&self, address: &str, log: &mut CommandLog) -> Result<Device> {
|
|
453 |
+ |
parse_info(&Self::ctl().args(["info", address]).run(log)?)
|
|
454 |
+ |
}
|
|
455 |
+ |
|
|
456 |
+ |
fn radio(&self) -> Radio {
|
|
457 |
+ |
// `--noheadings --output` rather than the default listing or `--json`:
|
|
458 |
+ |
// util-linux 2.39 accepts `--json` and ignores it, printing the human
|
|
459 |
+ |
// format, so a JSON parser here would fail on the machine it was
|
|
460 |
+ |
// written for. Measured on fw13, 2026-08-05.
|
|
461 |
+ |
Invocation::new("rfkill")
|
|
462 |
+ |
.args([
|
|
463 |
+ |
"--noheadings",
|
|
464 |
+ |
"--output",
|
|
465 |
+ |
"TYPE,SOFT,HARD",
|
|
466 |
+ |
"list",
|
|
467 |
+ |
"bluetooth",
|
|
468 |
+ |
])
|
|
469 |
+ |
.capture_quiet()
|
|
470 |
+ |
.map_or(Radio::Unknown, |raw| parse_rfkill(&raw))
|
|
471 |
+ |
}
|
|
472 |
+ |
|
|
473 |
+ |
fn pair(&self, device: &Device) -> Option<Invocation> {
|
|
474 |
+ |
Some(Self::ctl().args(["--agent", Self::AGENT, "pair", &device.address]))
|
|
475 |
+ |
}
|
|
476 |
+ |
|
|
477 |
+ |
fn connect(&self, device: &Device) -> Option<Invocation> {
|
|
478 |
+ |
Some(Self::ctl().args(["connect", &device.address]))
|
|
479 |
+ |
}
|
|
480 |
+ |
|
|
481 |
+ |
fn disconnect(&self, device: &Device) -> Option<Invocation> {
|
|
482 |
+ |
Some(Self::ctl().args(["disconnect", &device.address]))
|
|
483 |
+ |
}
|
|
484 |
+ |
|
|
485 |
+ |
fn trust(&self, device: &Device, trusted: bool) -> Option<Invocation> {
|
|
486 |
+ |
let verb = if trusted { "trust" } else { "untrust" };
|
|
487 |
+ |
Some(Self::ctl().args([verb, &device.address]))
|
|
488 |
+ |
}
|
|
489 |
+ |
|
|
490 |
+ |
fn block(&self, device: &Device, blocked: bool) -> Option<Invocation> {
|
|
491 |
+ |
let verb = if blocked { "block" } else { "unblock" };
|
|
492 |
+ |
Some(Self::ctl().args([verb, &device.address]))
|
|
493 |
+ |
}
|
|
494 |
+ |
|
|
495 |
+ |
fn remove(&self, device: &Device) -> Option<Invocation> {
|
|
496 |
+ |
Some(Self::ctl().args(["remove", &device.address]))
|
|
497 |
+ |
}
|
|
498 |
+ |
|
|
499 |
+ |
fn power(&self, on: bool) -> Option<Invocation> {
|
|
500 |
+ |
Some(Self::ctl().args(["power", if on { "on" } else { "off" }]))
|