-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.rs
253 lines (236 loc) · 8.81 KB
/
server.rs
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#![allow(unused_crate_dependencies)]
#![allow(clippy::print_stdout)]
#![allow(clippy::print_stderr)]
use std::io::BufRead;
use std::sync::Arc;
use std::time::Duration;
use anyhow::Result;
use clap::Parser;
use futures_core::future::BoxFuture;
use sscanf::sscanf;
use tracing::info;
use burble::att::Access;
use burble::gap::Appearance;
use burble::gatt::service::hids::{HidService, KeyboardMouse};
use burble::gatt::service::{bas, dis, gaps};
use burble::gatt::Db;
use burble::hci::AdvEvent;
use burble::*;
use burble_const::Service;
use burble_crypto::NumCompare;
use burble_hid::kbd::Keyboard;
use burble_hid::mouse::{Button, Mouse};
#[derive(Clone, Copy, Debug, clap::Parser)]
struct Args {
/// Vendor ID of the Bluetooth USB device.
#[arg(short, long, value_parser=hex16)]
vid: Option<u16>,
/// Product ID of the Bluetooth USB device.
#[arg(short, long, value_parser=hex16)]
pid: Option<u16>,
/// Use legacy advertising.
#[arg(short, long)]
legacy: bool,
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
let args = Args::parse();
let usb = host::Usb::new()?;
let mut ctlr = if let (Some(vid), Some(pid)) = (args.vid, args.pid) {
usb.open_first(vid, pid)?
} else {
println!("Available controllers (pass 'ID <VID>:<PID>' to '--vid' and '--pid' options):");
for ctlr in usb.controllers()? {
println!("{ctlr}");
}
return Ok(());
};
ctlr.init()?;
let mut host = hci::Host::new(Arc::new(ctlr));
let event_loop = host.event_loop();
host.init(&hci::EventMask::default()).await?;
host.le_set_default_phy(Some(hci::PhyMask::LE_2M), Some(hci::PhyMask::LE_2M))
.await?;
let r = serve(args, host).await;
event_loop.stop().await?;
r
}
fn read_input(hid: HidService<KeyboardMouse>) -> tokio::task::JoinHandle<()> {
let (tx, mut rx) = tokio::sync::mpsc::channel(1);
std::thread::spawn(move || {
// https://github.com/tokio-rs/tokio/issues/2466
for ln in std::io::BufReader::new(std::io::stdin()).lines() {
tx.blocking_send(ln?)?;
}
Ok::<_, anyhow::Error>(())
});
tokio::task::spawn(async move {
loop {
let ln: String = tokio::select! {
ln = rx.recv() => match ln {
None => return,
Some(ln) => ln,
},
_ = tokio::signal::ctrl_c() => return,
};
let mut tok = ln.split_ascii_whitespace();
let Some(cmd) = tok.next() else { continue };
match (cmd, tok.collect::<Vec<&str>>().join(" ")) {
("click" | "c", params) => {
if params.is_empty() {
hid.exec(|km| km.mouse().click(Button::PRIMARY)).await;
} else {
let btn = match sscanf!(params, "{u8}") {
Ok(v) if v < 8 => Button::from_bits_retain(1 << v),
_ => continue,
};
hid.exec(|km| km.mouse().click(btn)).await;
}
}
("move" | "m", params) => {
let Ok(v) = sscanf!(params, "{i32} {i32}") else { continue };
hid.exec(|km| km.mouse().move_rel(v.0, v.1)).await;
}
_ => {}
}
}
})
}
async fn serve(args: Args, host: hci::Host) -> Result<()> {
// [HOGP] Section 6.1
const SEC: Access = Access::READ.authn().encrypt();
let mut db = Db::build();
gatt::Server::define_service(&mut db);
gaps::GapService::new("Burble", Appearance::GenericHumanInterfaceDevice).define(&mut db);
dis::DeviceInfoService::new()
.with_manufacturer_name("Blackrock Neurotech")
// [HOGP] Section 3.3.2
.with_pnp_id(dis::PnpId::new(dis::VendorId::USB(0x1209), 0x0001, (1, 0, 0)).unwrap())
.define(&mut db, SEC);
bas::BatteryService::new().define(&mut db, SEC);
let hid = HidService::new(KeyboardMouse::new(Keyboard::us(1), Mouse::new(2, 400)));
//#[cfg(debug_assertions)]
//db.morph_next();
hid.define(&mut db);
let mut input_task = read_input(hid);
let srv = gatt::Server::new(db, Arc::new(fs::GattServerStore::per_user("burble")));
srv.db().dump();
let key_store: Arc<smp::KeyStore> = Arc::new(fs::KeyStore::per_user("burble"));
let mut secdb = smp::SecDb::new(host.clone(), Arc::clone(&key_store));
tokio::task::spawn(async move { secdb.event_loop().await });
// TODO: Redesign ChanManager for easier integration with advertisements
let mut cm = l2cap::ChanManager::new(&host).await?;
let mut adv_task = None;
let mut srv_task = None;
let mut conn = None;
loop {
if srv_task.is_none() && adv_task.is_none() {
info!("Enabling advertisements");
adv_task = Some(tokio::task::spawn(advertise(args, host.clone())));
}
tokio::select! {
_ = &mut input_task => return Ok(()),
adv = async { adv_task.as_mut().unwrap().await }, if adv_task.is_some() => {
info!("Advertisement result: {:?}", adv);
adv_task = None;
if matches!(adv??, AdvEvent::Term(_)) {
continue;
}
let mut conn = match conn.take() {
Some(conn) => conn,
None => cm.next().await?,
};
info!("Serving {}", conn.link());
let mut smp = conn.smp_peripheral().unwrap();
let key_store = Arc::clone(&key_store);
tokio::task::spawn(async move {
let mut dev = smp::Device::new().with_display(Box::new(Dev)).with_confirm(Box::new(Dev));
smp.respond(&mut dev, key_store.as_ref()).await
});
let br = conn.att_bearer().unwrap();
srv_task = Some(tokio::task::spawn(srv.attach(&br).serve(br)));
}
srv = async { srv_task.as_mut().unwrap().await }, if srv_task.is_some() => {
info!("GATT server terminated: {:?}", srv);
srv_task = None;
}
r = cm.next() => {
assert!(conn.is_none());
conn = Some(r?);
}
}
}
}
async fn advertise(args: Args, host: hci::Host) -> hci::Result<AdvEvent> {
let mut adv = hci::Advertiser::new(&host).await?;
let params = if args.legacy {
hci::AdvParams {
props: hci::AdvProp::CONNECTABLE | hci::AdvProp::SCANNABLE | hci::AdvProp::LEGACY,
pri_interval: (Duration::from_millis(20), Duration::from_millis(25)),
..hci::AdvParams::default()
}
} else {
hci::AdvParams {
props: hci::AdvProp::CONNECTABLE | hci::AdvProp::INCLUDE_TX_POWER,
pri_interval: (Duration::from_millis(20), Duration::from_millis(25)),
sec_phy: hci::Phy::Le2M,
..hci::AdvParams::default()
}
};
let (h, power) = adv.create(params).await?;
let mut data = gap::ResponseDataMut::new();
data.flags(gap::AdvFlag::LE_GENERAL | gap::AdvFlag::NO_BREDR)
// [HOGP] Section 3.1.3
.service(false, [Service::HumanInterfaceDevice])
// [HOGP] Section 3.1.4
.local_name(true, "Burble")
// [HOGP] Section 3.1.5
.appearance(Appearance::GenericHumanInterfaceDevice)
.tx_power(power);
adv.set_data(h, data.get()).await?;
let enable_params = hci::AdvEnableParams {
handle: h,
duration: Duration::from_secs(20),
max_events: 0,
};
let adv_set = adv.enable(enable_params).await?;
let r = adv_set.await;
adv.remove_all().await?;
r
}
#[derive(Debug)]
struct Dev;
impl smp::Display for Dev {
fn show(&mut self, n: NumCompare) -> BoxFuture<bool> {
println!("Numeric comparison: {n}");
Box::pin(std::future::ready(true))
}
}
impl smp::Confirm for Dev {
fn confirm(&mut self) -> BoxFuture<bool> {
Box::pin(std::future::ready(true))
/*
use tokio::io::AsyncBufReadExt;
Box::pin(async {
let buf = tokio::io::BufReader::new(tokio::io::stdin());
let mut lines = buf.lines();
loop {
print!("Match? (yes/no) ");
let Ok(Some(ln)) = lines.next_line().await else { return false };
match ln.to_ascii_lowercase().as_str() {
"yes" | "y" => return true,
"no" | "n" => return false,
_ => {}
}
}
})
*/
}
}
pub fn hex16(mut s: &str) -> Result<u16, String> {
if s.starts_with("0x") || s.starts_with("0X") {
s = &s[2..];
}
u16::from_str_radix(s, 16).map_err(|e| format!("{e}"))
}