Krolyxon
Commit 1b48cfa

Branches

Refs →

budctlcratesbudctl-coresrcstatus.rs

221 lines · 6.8 KB · Rust

1//! What a session reports, in device-neutral terms. Front ends print these, and `--json`
2//! serializes them as they are.
3
4use serde::{Serialize, Serializer};
5
6use crate::bt::BdAddr;
7
8/// Bumped whenever the JSON shape changes incompatibly.
9pub const JSON_SCHEMA: u32 = 1;
10
11#[derive(Debug, Default, Clone, Serialize)]
12pub struct Status {
13 pub battery: Battery,
14 pub noise_control: Option<NoiseControl>,
15 /// Name of the current EQ preset.
16 pub eq: Option<String>,
17 pub bass_level: Option<i8>,
18 /// On/off settings, serialized as `{"game": false, …}`.
19 #[serde(serialize_with = "switches_as_map")]
20 pub switches: Vec<Switch>,
21 /// Audio codec of the link the buds report.
22 pub codec: Option<String>,
23 pub firmware: Option<String>,
24 /// Reads that failed. The rest of the status is still valid.
25 pub warnings: Vec<String>,
26}
27
28#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize)]
29pub struct Battery {
30 pub left: Option<Charge>,
31 pub right: Option<Charge>,
32 pub case: Option<Charge>,
33}
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
36pub struct Charge {
37 /// Percent.
38 pub level: u8,
39 pub charging: bool,
40}
41
42#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
43pub struct NoiseControl {
44 /// `off`, `transparency` or `anc`, or `unknown (…)` for a value the device module cannot name.
45 pub mode: String,
46 /// The noise-cancelling level, when the device reports one.
47 pub level: Option<String>,
48}
49
50/// A noise-control mode to set.
51#[derive(Debug, Clone, Copy, PartialEq, Eq)]
52pub enum NoiseMode {
53 Off,
54 Transparency,
55 Anc(AncLevel),
56}
57
58/// How strongly noise cancelling works.
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60pub enum AncLevel {
61 Light,
62 Medium,
63 Deep,
64}
65
66impl AncLevel {
67 /// The name [`NoiseControl::level`] uses for it.
68 pub fn name(self) -> &'static str {
69 match self {
70 AncLevel::Light => "light",
71 AncLevel::Medium => "medium",
72 AncLevel::Deep => "deep",
73 }
74 }
75}
76
77impl NoiseMode {
78 /// The name [`NoiseControl::mode`] uses for it.
79 pub fn name(self) -> &'static str {
80 match self {
81 NoiseMode::Off => "off",
82 NoiseMode::Transparency => "transparency",
83 NoiseMode::Anc(_) => "anc",
84 }
85 }
86}
87
88#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
89pub struct Switch {
90 /// Stable name used on the command line and in JSON (`game`, `dual`…).
91 pub key: &'static str,
92 /// Human name (`Game mode`).
93 pub label: &'static str,
94 pub on: bool,
95}
96
97#[derive(Debug, Default, Clone, Serialize)]
98pub struct TouchControls {
99 pub gestures: Vec<Gesture>,
100 /// Noise-control modes a hold steps through, when the device has that setting.
101 pub hold_cycle: Option<Vec<String>>,
102 /// The actions each gesture can be given, when the device can change them.
103 pub choices: Vec<GestureChoice>,
104 /// The modes the hold cycle can include, when the device can change it.
105 pub cycle_modes: Vec<String>,
106 pub warnings: Vec<String>,
107}
108
109/// The actions one gesture can be given, as the device offers them, in its order.
110#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
111pub struct GestureChoice {
112 /// `double-tap`, `triple-tap`, `hold`…
113 pub gesture: String,
114 /// `none`, `play-pause`…
115 pub actions: Vec<String>,
116}
117
118#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
119pub struct Gesture {
120 /// `left`, `right` or `both`.
121 pub side: String,
122 /// `double-tap`, `triple-tap`, `hold`…
123 pub gesture: String,
124 /// `play-pause`, `next`, `none`…
125 pub action: String,
126 /// Raw button group, for entries outside the main gestures (calls). `None` for the main group.
127 #[serde(skip_serializing_if = "Option::is_none")]
128 pub group: Option<u8>,
129}
130
131/// The EQ presets the buds have, and which one is in use.
132#[derive(Debug, Default, Clone, Serialize)]
133pub struct EqPresets {
134 /// Name of the preset in use, as in [`Status::eq`].
135 pub current: Option<String>,
136 /// Built-in presets first, then custom ones.
137 pub presets: Vec<EqPreset>,
138 /// Reads that failed. The rest is still valid.
139 pub warnings: Vec<String>,
140}
141
142#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
143pub struct EqPreset {
144 /// Built-in presets: the name `eq <preset>` takes. Custom ones: the name stored on the buds.
145 pub name: String,
146 /// The buds' id for it.
147 pub id: u8,
148 pub custom: bool,
149 /// Custom presets only: gain per band. Empty for built-in presets.
150 pub bands: Vec<EqBand>,
151 /// Custom presets only: the lowest and the highest gain a band takes, dB.
152 #[serde(skip_serializing_if = "Option::is_none")]
153 pub range: Option<(i8, i8)>,
154}
155
156#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
157pub struct EqBand {
158 pub hz: u16,
159 /// dB.
160 pub gain: i8,
161}
162
163/// A phone or computer the buds keep for dual connection.
164#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
165pub struct DualDevice {
166 pub address: BdAddr,
167 /// The name the device gave the buds.
168 pub name: String,
169 pub connected: bool,
170 /// The device that asked: the computer budctl runs on.
171 pub this_computer: bool,
172 /// The device the buds take audio from.
173 pub main_audio: bool,
174 pub playing: bool,
175}
176
177/// A touch-control change: what changes, the write that does it and, once sent, the table the
178/// buds report afterwards.
179#[derive(Debug, Serialize)]
180pub struct TouchChange {
181 /// The entries that change. Empty when every target already has the wanted action.
182 pub changes: Vec<GestureChange>,
183 /// The write that makes the change, or `None` when there is nothing to change.
184 pub write: Option<RawWrite>,
185 /// Whether the write went out. `false` on a dry run and when nothing changes.
186 pub sent: bool,
187 /// The whole table: as read back after the write, or as it would be after a dry run.
188 pub gestures: Vec<Gesture>,
189}
190
191#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
192pub struct GestureChange {
193 pub side: String,
194 pub gesture: String,
195 pub from: String,
196 pub to: String,
197}
198
199/// One command and its payload, as `--dry-run` shows them. The frame around it (sequence byte
200/// and so on) is added when it is sent.
201#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
202pub struct RawWrite {
203 /// `0x0401`.
204 #[serde(serialize_with = "command_as_hex")]
205 pub command: u16,
206 /// `06 01 01 02 01 …`.
207 #[serde(serialize_with = "bytes_as_hex")]
208 pub payload: Vec<u8>,
209}
210
211fn command_as_hex<S: Serializer>(command: &u16, serializer: S) -> Result<S::Ok, S::Error> {
212 serializer.serialize_str(&format!("0x{command:04X}"))
213}
214
215fn bytes_as_hex<S: Serializer>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error> {
216 serializer.serialize_str(&crate::debug::hex(bytes))
217}
218
219fn switches_as_map<S: Serializer>(switches: &[Switch], serializer: S) -> Result<S::Ok, S::Error> {
220 serializer.collect_map(switches.iter().map(|s| (s.key, s.on)))
221}