-
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathbluetooth.rs
More file actions
215 lines (190 loc) Β· 7.42 KB
/
bluetooth.rs
File metadata and controls
215 lines (190 loc) Β· 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use std::sync::{Arc, atomic::AtomicBool};
use bluer::{Adapter, Address, Session};
use bluer::Device as BTDevice;
use crate::app::AppResult;
/// Fallback: read battery percentage from /sys/class/power_supply/ for devices
/// that report battery via kernel drivers (e.g. PS4/PS5 controllers, Nintendo
/// Switch Pro Controllers) instead of the Bluetooth Battery Service (BAS) profile
/// exposed through BlueZ's Battery1 D-Bus interface.
///
/// First tries to read `capacity` (exact percentage). If unavailable, falls back
/// to `capacity_level` which provides a rough estimate using kernel-defined levels:
/// Unknown, Critical, Low, Normal, High, Full.
/// See: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/include/linux/power_supply.h
fn read_battery_from_sysfs(addr: &Address) -> Option<u8> {
let addr_str = addr.to_string().to_lowercase();
let power_supply_dir = std::path::Path::new("/sys/class/power_supply");
if let Ok(entries) = std::fs::read_dir(power_supply_dir) {
for entry in entries.flatten() {
let name = entry.file_name();
let name_str = name.to_string_lossy();
if name_str.contains(&addr_str) {
let dir = entry.path();
// Try exact percentage first
if let Ok(content) = std::fs::read_to_string(dir.join("capacity")) {
if let Ok(val) = content.trim().parse::<u8>() {
return Some(val);
}
}
// Fall back to capacity_level (e.g. Nintendo Switch Pro Controller)
if let Ok(level) = std::fs::read_to_string(dir.join("capacity_level")) {
return match level.trim().to_lowercase().as_str() {
"full" => Some(100),
"high" => Some(75),
"normal" => Some(50),
"low" => Some(25),
"critical" => Some(5),
_ => None,
};
}
}
}
}
None
}
#[derive(Debug, Clone)]
pub struct Controller {
pub adapter: Arc<Adapter>,
pub name: String,
pub alias: String,
pub is_powered: bool,
pub is_pairable: bool,
pub is_discoverable: bool,
pub is_scanning: Arc<AtomicBool>,
pub paired_devices: Vec<Device>,
pub new_devices: Vec<Device>,
}
#[derive(Debug, Clone)]
pub struct Device {
device: BTDevice,
pub addr: Address,
pub icon: &'static str,
pub alias: String,
pub is_paired: bool,
pub is_favorite: bool,
pub is_trusted: bool,
pub is_connected: bool,
pub battery_percentage: Option<u8>,
}
impl Device {
pub async fn set_alias(&self, alias: String) -> AppResult<()> {
self.device.set_alias(alias).await?;
Ok(())
}
// https://specifications.freedesktop.org/icon-naming/latest/
pub fn get_icon(name: &str) -> &'static str {
match name {
"audio-card" => "σ° ",
"audio-input-microphone" => "ο° ",
"audio-headphones" | "audio-headset" => "σ° ",
"battery" => "σ° ",
"camera-photo" => "σ°» ",
"computer" => "ο ",
"input-keyboard" => "σ° ",
"input-mouse" => "σ°½ ",
"input-gaming" => "σ°΄ ",
"phone" => "σ°² ",
_ => "σ°Ύ° ",
}
}
}
impl Controller {
pub async fn get_all(
session: Arc<Session>,
favorite_devices: &[Address],
) -> AppResult<Vec<Controller>> {
let mut controllers: Vec<Controller> = Vec::new();
// let session = bluer::Session::new().await?;
let adapter_names = session.adapter_names().await?;
for adapter_name in adapter_names {
if let Ok(adapter) = session.adapter(&adapter_name) {
let name = adapter.name().to_owned();
let alias = adapter.alias().await?;
let is_powered = adapter.is_powered().await?;
let is_pairable = adapter.is_pairable().await?;
let is_discoverable = adapter.is_discoverable().await?;
let is_scanning = adapter.is_discovering().await?;
let (paired_devices, new_devices) =
Controller::get_all_devices(&adapter, favorite_devices).await?;
let controller = Controller {
adapter: Arc::new(adapter),
name,
alias,
is_powered,
is_pairable,
is_discoverable,
is_scanning: Arc::new(AtomicBool::new(is_scanning)),
paired_devices,
new_devices,
};
controllers.push(controller);
}
}
Ok(controllers)
}
pub async fn get_all_devices(
adapter: &Adapter,
favorite_devices: &[Address],
) -> AppResult<(Vec<Device>, Vec<Device>)> {
let mut paired_devices: Vec<Device> = Vec::new();
let mut new_devices: Vec<Device> = Vec::new();
let mut devices_without_aliases: Vec<Device> = Vec::new();
let connected_devices_addresses = adapter.device_addresses().await?;
for addr in connected_devices_addresses {
let device = adapter.device(addr)?;
let alias = device.alias().await?;
let icon = Device::get_icon(device.icon().await?.unwrap_or("-".to_string()).as_str());
let is_paired = device.is_paired().await?;
let is_trusted = device.is_trusted().await?;
let is_connected = device.is_connected().await?;
let is_favorite = favorite_devices.contains(&addr);
let battery_percentage =
device.battery_percentage().await?.or_else(|| read_battery_from_sysfs(&addr));
let dev = Device {
device,
addr,
alias,
icon,
is_paired,
is_trusted,
is_connected,
is_favorite,
battery_percentage,
};
if dev.is_paired {
paired_devices.push(dev);
} else {
match is_mac_addr(&dev.alias) {
// most device names without aliases may default to their mac addresses, but we should not
// assume that to be 100% the case
true => devices_without_aliases.push(dev),
false => new_devices.push(dev),
}
}
}
paired_devices.sort_by_key(|i| (!i.is_favorite, i.addr));
new_devices.sort_by(|a, b| a.alias.cmp(&b.alias));
devices_without_aliases.sort_by_key(|i| i.addr);
new_devices.extend(devices_without_aliases);
Ok((paired_devices, new_devices))
}
}
fn is_mac_addr(s: &str) -> bool {
if s.len() != 17 {
return false;
}
let mut chars = s.chars();
for _ in 0..5 {
// Matches [A-Fa-f0-9][A-Fa-f0-9]-
if !(matches!(chars.next(), Some(c) if c.is_ascii_hexdigit())
&& matches!(chars.next(), Some(c) if c.is_ascii_hexdigit())
&& matches!(chars.next(), Some('-')))
{
return false;
}
}
// Matches [A-Fa-f0-9][A-Fa-f0-9]$
matches!(chars.next(), Some(c) if c.is_ascii_hexdigit())
&& matches!(chars.next(), Some(c) if c.is_ascii_hexdigit())
&& chars.next().is_none()
}