Krolyxon
Commit 904ab9a

Branches

Refs →

budctlcratesbudctl-coresrcstatus.rs

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