Krolyxon
Commit e9c4031

Branches

Refs →

budctlcratesbudctl-clisrcmain.rs

133 lines · 3.8 KB · Rust

1//! `budctl`: control wireless earbuds from Linux.
2//!
3//! A thin layer over budctl-core: parse the command line, run one command, print the result,
4//! turn errors into the exit codes listed in CLAUDE.md.
5
6mod output;
7
8use std::io::{self, Write};
9use std::process::ExitCode;
10
11use budctl_core::bt::BdAddr;
12use budctl_core::status::NoiseMode;
13use budctl_core::{Error, Kind};
14use clap::{Parser, Subcommand, ValueEnum};
15
16/// Control wireless earbuds from Linux, without the vendor app.
17#[derive(Parser)]
18#[command(name = "budctl", version)]
19struct Cli {
20 /// The buds to use, when more than one supported pair is paired.
21 #[arg(long, global = true, value_name = "MAC")]
22 device: Option<BdAddr>,
23
24 /// Machine-readable output (JSON with "schema": 1).
25 #[arg(long, global = true)]
26 json: bool,
27
28 /// Hex-dump every frame sent and received on stderr.
29 #[arg(long, global = true)]
30 debug: bool,
31
32 #[command(subcommand)]
33 command: Command,
34}
35
36#[derive(Subcommand)]
37enum Command {
38 /// Paired devices and whether budctl recognizes them.
39 List,
40 /// Battery, noise control, EQ and switches.
41 Status,
42 /// Touch controls (gesture table) of each bud.
43 Touch,
44 /// Set noise control, then read it back.
45 Anc {
46 #[arg(value_enum)]
47 mode: AncArg,
48 },
49}
50
51#[derive(Clone, Copy, ValueEnum)]
52enum AncArg {
53 Off,
54 On,
55 Transparency,
56}
57
58impl From<AncArg> for NoiseMode {
59 fn from(arg: AncArg) -> Self {
60 match arg {
61 AncArg::Off => NoiseMode::Off,
62 AncArg::On => NoiseMode::Anc,
63 AncArg::Transparency => NoiseMode::Transparency,
64 }
65 }
66}
67
68fn main() -> ExitCode {
69 let cli = match Cli::try_parse() {
70 Ok(cli) => cli,
71 Err(error) => {
72 // Help and version are not errors; everything else is a usage error (exit 1).
73 let code = if error.use_stderr() { 1 } else { 0 };
74 // Nothing useful can be done if printing the usage message fails.
75 let _ = error.print();
76 return ExitCode::from(code);
77 }
78 };
79 if cli.debug {
80 budctl_core::debug::enable();
81 }
82
83 let mut stdout = io::stdout().lock();
84 let result = run(&cli, &mut stdout).and_then(|()| stdout.flush().map_err(output::Failure::Io));
85 match result {
86 Ok(()) => ExitCode::SUCCESS,
87 // `budctl status | head -1` is fine.
88 Err(output::Failure::Io(e)) if e.kind() == io::ErrorKind::BrokenPipe => ExitCode::SUCCESS,
89 Err(output::Failure::Io(e)) => {
90 eprintln!("budctl: cannot write output: {e}");
91 ExitCode::from(1)
92 }
93 Err(output::Failure::Budctl(e)) => {
94 eprintln!("budctl: {e}");
95 ExitCode::from(exit_code(&e))
96 }
97 }
98}
99
100fn run(cli: &Cli, out: &mut impl Write) -> Result<(), output::Failure> {
101 let found = budctl_core::scan()?;
102 if let Command::List = cli.command {
103 return output::list(out, &found, cli.json);
104 }
105
106 let (device, model) = budctl_core::select(found, cli.device)?;
107 let mut session = budctl_core::connect(&device, model)?;
108 match cli.command {
109 Command::List => Ok(()),
110 Command::Status => {
111 let status = session.status()?;
112 output::status(out, &device, model, &status, cli.json)
113 }
114 Command::Touch => {
115 let touch = session.touch_controls()?;
116 output::touch(out, &device, model, &touch, cli.json)
117 }
118 Command::Anc { mode } => {
119 let now = session.set_noise_control(mode.into())?;
120 output::noise_control(out, &device, model, &now, cli.json)
121 }
122 }
123}
124
125/// Exit codes from CLAUDE.md: 1 usage, 2 not found, 3 busy, 4 rejected or timed out.
126fn exit_code(error: &Error) -> u8 {
127 match error.kind() {
128 Kind::Usage => 1,
129 Kind::NotFound => 2,
130 Kind::Busy => 3,
131 Kind::Failed => 4,
132 }
133}