-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathbluetooth.rs
300 lines (278 loc) · 10.9 KB
/
bluetooth.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use crate::debug::UnwrapLog;
use crate::hidreport::HidReport;
use crate::keyboard::Keyboard;
use crate::led::Led;
use crate::protocol::{BleOp, KeyboardOp, LedOp, MacroOp, Message, MsgType, SystemOp};
use crate::serial::bluetooth_usart::BluetoothUsart;
use crate::serial::{DmaUsart, Serial, Transfer};
use core::convert::Infallible;
use core::marker::Unsize;
#[derive(Copy, Clone, PartialEq)]
pub enum BluetoothMode {
Unknown,
Legacy,
Ble,
}
pub struct Bluetooth<BUFFER: 'static + Unsize<[u8]>> {
pub serial: Serial<BluetoothUsart, BUFFER>,
pub rx_transfer: Option<Transfer<BUFFER>>,
mode: BluetoothMode,
/// 4-bit bitfield, indicating whether the BT chip has a host
/// saved in that slot. TODO: investigate high bits (issue #37)
saved_hosts: u8,
/// The currently connected slot (1-4), or disconnected (0), or
/// the current host is not saved (12)
connected_host: u8,
}
impl<BUFFER> Bluetooth<BUFFER>
where
BUFFER: Unsize<[u8]>,
{
pub fn new(
mut serial: Serial<BluetoothUsart, BUFFER>,
rx_buffer: &'static mut BUFFER,
) -> Bluetooth<BUFFER> {
let rx_transfer = serial.receive(rx_buffer);
Bluetooth {
serial,
rx_transfer: Some(rx_transfer),
mode: BluetoothMode::Unknown,
saved_hosts: 0,
connected_host: 0,
}
}
pub fn on(&mut self) -> nb::Result<(), Infallible> {
self.serial.send(MsgType::Ble, BleOp::On as u8, &[])
}
pub fn off(&mut self) -> nb::Result<(), Infallible> {
self.serial.send(MsgType::Ble, BleOp::Off as u8, &[])
}
pub fn save_host(&mut self, host: u8) -> nb::Result<(), Infallible> {
// TODO: host < 4?
self.serial
.send(MsgType::Ble, BleOp::SaveHost as u8, &[host])
}
pub fn connect_host(&mut self, host: u8) -> nb::Result<(), Infallible> {
self.serial
.send(MsgType::Ble, BleOp::ConnectHost as u8, &[host])
}
pub fn delete_host(&mut self, host: u8) -> nb::Result<(), Infallible> {
self.serial
.send(MsgType::Ble, BleOp::DeleteHost as u8, &[host])
}
pub fn broadcast(&mut self) -> nb::Result<(), Infallible> {
self.serial.send(MsgType::Ble, BleOp::Broadcast as u8, &[])
}
pub fn enable_legacy_mode(&mut self, enabled: bool) -> nb::Result<(), Infallible> {
let on = if enabled { 1 } else { 0 };
self.serial
.send(MsgType::Ble, BleOp::LegacyMode as u8, &[on])
}
pub fn toggle_legacy_mode(&mut self) -> nb::Result<(), Infallible> {
let enabled: bool = self.mode == BluetoothMode::Ble;
self.enable_legacy_mode(enabled)
}
pub fn host_list_query(&mut self) -> nb::Result<(), Infallible> {
self.serial
.send(MsgType::Ble, BleOp::HostListQuery as u8, &[])
}
pub fn send_report(&mut self, report: &HidReport) -> nb::Result<(), Infallible> {
self.serial.send(
MsgType::Keyboard,
KeyboardOp::KeyReport as u8,
report.as_bytes(),
)
}
pub fn update_led(
&self,
led: &mut Led<BUFFER>,
keyboard_send_usb_report: bool,
) -> nb::Result<(), Infallible> {
led.bluetooth_mode(
self.saved_hosts,
self.connected_host,
self.mode,
keyboard_send_usb_report,
)
}
pub fn handle_message(
&mut self,
message: &Message<'_>,
led: &mut Led<BUFFER>,
keyboard: &mut Keyboard,
) {
match message.msg_type {
MsgType::System => {
match SystemOp::from(message.operation) {
SystemOp::GetId => {
const DEVICE_TYPE_KEYBOARD: u8 = 1;
const DEVICE_MODEL_ANNE_PRO: u8 = 2;
//const DEVICE_ID = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
// send two packets
// nblock = 2
// [datalen, nblock, iblock = 0, data...]
// [datalen, nblock, iblock = 1, data...]
let data1 = [
10,
2,
0,
DEVICE_TYPE_KEYBOARD,
DEVICE_MODEL_ANNE_PRO,
1,
2,
3,
4,
5,
6,
];
let data2 = [8, 2, 1, 7, 8, 9, 10, 11, 12];
self.serial
.send(MsgType::System, SystemOp::AckGetId as u8, &data1)
.log_error();
self.serial
.send(MsgType::System, SystemOp::AckGetId as u8, &data2)
.log_error();
}
SystemOp::IsSyncCode => {
self.serial
.send(MsgType::System, SystemOp::AckIsSyncCode as u8, &[1])
.log_error();
}
SystemOp::SetSyncCode => {
self.serial
.send(MsgType::System, SystemOp::AckIsSyncCode as u8, &[])
.log_error();
}
_ => {
crate::heprintln!("msg: System {} {:?}", message.operation, message.data)
.ok();
}
}
}
MsgType::Ble => {
match BleOp::from(message.operation) {
BleOp::AckWakeup => {
// nothing to do here, this message only only lets us know
// that we can now safely send
}
BleOp::AckOn => {
// data = [0]
// TODO: always getting a [0] too much?
//crate::heprintln!("bt ack on: {:?}", message.data).ok();
}
BleOp::AckOff => {
// data = [0]
//crate::heprintln!("bt ack off: {:?}", message.data).ok();
}
BleOp::AckLegacyMode => {
// data = [0]
//crate::heprintln!("bt ack legacy mode: {:?}", message.data).ok();
}
BleOp::AckDeleteHost => {
// data = [0]
//crate::heprintln!("bt ack delete host: {:?}", message.data).ok();
}
BleOp::Pair => {
crate::heprintln!("bt pair").ok();
keyboard.disable_bluetooth_mode();
led.bluetooth_pin_mode().log_error();
}
BleOp::Disconnect => {
// check this? sent after off, 14
crate::heprintln!("bt disconnect").ok();
}
BleOp::AckHostListQuery => {
if message.data.len() == 3 {
self.saved_hosts = message.data[0];
self.connected_host = message.data[1];
self.mode = match message.data[2] {
0 => BluetoothMode::Ble,
1 => BluetoothMode::Legacy,
_ => BluetoothMode::Unknown,
};
}
if keyboard.bluetooth_mode_enabled() {
self.update_led(led, keyboard.send_usb_report).log_error();
}
}
_ => {
crate::heprintln!("msg: Ble {} {:?}", message.operation, message.data).ok();
}
}
}
MsgType::Led => match LedOp::from(message.operation) {
LedOp::ThemeMode => {
led.set_theme(message.data[0]).log_error();
}
LedOp::GetUserStaticTheme => {
crate::heprintln!("TODO: Theme Sync").ok();
// [data_length, num_blocks, block_i]
//let data = [2 + 4, 1, 0, 1, 2, 3, 4];
//self.serial
//.send(MsgType::Led, LedOp::AckGetUserStaticTheme as u8, &data)
//.log_error();
}
_ => {
crate::heprintln!("msg: Led {} {:?}", message.operation, message.data).ok();
}
},
MsgType::Keyboard => match KeyboardOp::from(message.operation) {
KeyboardOp::UpUserLayout => {
crate::heprintln!("TODO: Keyboard Sync").ok();
}
_ => {
crate::heprintln!("msg: Keyboard {} {:?}", message.operation, message.data)
.ok();
}
},
MsgType::Macro => match MacroOp::from(message.operation) {
MacroOp::SyncMacro => {
crate::heprintln!("TODO: Macro Sync").ok();
}
_ => {
crate::heprintln!("msg: macro {} {:?}", message.operation, message.data).ok();
}
},
_ => {
crate::heprintln!(
"msg: {:?} {} {:?}",
message.msg_type,
message.operation,
message.data
)
.ok();
}
}
}
pub fn poll(&mut self, led: &mut Led<BUFFER>, keyboard: &mut Keyboard) {
let result = self
.rx_transfer
.as_mut()
.unwrap()
.poll(&mut self.serial.usart);
match result {
Err(nb::Error::WouldBlock) => {}
Err(_) => unreachable!(),
Ok(()) => {
let buffer = self.rx_transfer.take().unwrap().finish();
{
let buffer: &mut [u8] = buffer;
let message = Message {
msg_type: MsgType::from(buffer[0]),
operation: buffer[2],
data: &buffer[3..3 + buffer[1] as usize - 1],
};
self.handle_message(&message, led, keyboard);
if let (MsgType::Ble, BleOp::AckWakeup) =
(message.msg_type, message.operation.into())
{
// Wakeup acknowledged, send data
self.serial.usart.ack_wakeup();
self.serial.send_buffer_pos = 0;
}
}
self.rx_transfer = Some(self.serial.receive(buffer));
}
}
}
}