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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
use core::fmt;
use core::fmt::{Debug, Formatter};
use core::task::Poll;
use futures::ready;
use crate::bit_ops::{get_bit, get_bit_range};
use crate::encoding::decode_high_byte;
use crate::Error;
macro_rules! incoming_packages {
(
$(
$(#[$outer:meta])*
$code:literal => |$bytes:ident: [u8; $length:literal]| $(#[$outer2:meta])* $name:ident {
$(
$(#[$field_meta:meta])*
$field_vis:vis $field_name:ident: $field_type:ty = $field_const:expr
),*$(,)?
}
),*$(,)?
) => {
#[derive(Debug)]
pub enum IncomingPackage {
$(
$(#[$outer])*
$name($name),
)*
}
$(
$(#[$outer])*
$(#[$outer2])*
pub struct $name {
$(
$(#[$field_meta])*
$field_vis $field_name: $field_type,
)*
}
impl $name {
pub(super) fn from_bytes($bytes: [u8; $length]) -> Self {
$name {
$($field_name: $field_const,)*
}
}
}
)*
pub enum IncomingStateMachine {
None,
$(
$(#[$outer])*
$name {
buffer: [u8; ($length+1)],
received_bytes: usize
},
)*
}
impl IncomingStateMachine {
pub fn resume<
#[cfg(feature = "std")] E: snafu::AsErrorSource,
#[cfg(not(feature = "std"))] E,
>(
&mut self,
mut read: impl FnMut(&mut [u8]) -> Poll<core::result::Result<usize, E>>
) -> Poll<$crate::Result<IncomingPackage, E>> {
loop {
match self {
IncomingStateMachine::None => {
let mut code = [0u8];
let count = ready!(read(&mut code))?;
if count == 0 {
return Err(Error::DeviceReadZero).into();
}
if count > 1 {
return Err(
Error::DeviceReadTooMuch { requested: 1, reported: count }
).into();
}
match code[0] {
$(
$code => *self = IncomingStateMachine::$name {
buffer: [0; ($length + 1)],
received_bytes: 0
},
)*
code => return Err(Error::UnknownTypeCode{ code }).into(),
}
},
$(
IncomingStateMachine::$name {
ref mut buffer,
ref mut received_bytes
} => {
let slice = &mut buffer[*received_bytes..($length + 1)];
let count = ready!(read(slice))?;
if count == 0 {
return Err(Error::DeviceReadZero).into();
}
if count > slice.len() {
return Err(Error::DeviceReadTooMuch {
requested: slice.len(),
reported: count
}).into();
}
*received_bytes += count;
if *received_bytes == ($length + 1) {
let [high_byte, data @ ..] = *buffer;
let decoded = match decode_high_byte((high_byte, data)){
Ok(decoded) => decoded,
Err(invalid_index) => {
let mut bytes = [0; 8];
bytes[..$length+1].copy_from_slice(buffer);
return Err(Error::InvalidPackageData {
code: $code,
bytes,
length: $length+1,
invalid_index
}).into();
}
};
let data = $name::from_bytes(decoded);
*self = IncomingStateMachine::None;
return Poll::Ready(Ok(IncomingPackage::$name(data)))
}
},
)*
}
}
}
}
};
}
incoming_packages! {
0x01 => |bytes: [u8; 7]| #[derive(Debug, Copy, Clone)] RealTimeData {
pub signal_strength: u8 = get_bit_range(bytes[0], 0..=3),
pub searching_time_too_long: bool = get_bit(bytes[0], 4),
pub low_spo2: bool = get_bit(bytes[0], 5),
pub pulse_beep: bool = get_bit(bytes[0], 6),
pub probe_errors: bool = get_bit(bytes[0], 7),
pub pulse_waveform: u8 = get_bit_range(bytes[1], 0..=6),
pub searching_pulse: bool = get_bit(bytes[1], 7),
pub bar_graph: u8 = get_bit_range(bytes[2], 0..=3),
pub pi_invalid: bool = get_bit(bytes[2], 4),
pub pulse_rate: u8 = bytes[3],
pub spo2: u8 = bytes[4],
pub pi: u16 = (bytes[5] as u16) + ((bytes[6] as u16) << 8)
},
0x04 => |bytes: [u8; 7]| #[derive(Debug, Copy, Clone)] DeviceIdentifier {
pub identifier: [u8; 7] = bytes,
},
0x05 => |bytes: [u8; 7]| #[derive(Debug, Copy, Clone)] UserInformation {
pub user_index: u8 = bytes[0],
pub user_info: [u8; 6] = [bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6]]
},
0x07 => |bytes: [u8; 6]| #[derive(Debug, Copy, Clone)] StorageStartTimeDate {
pub user_index: u8 = bytes[0],
pub storage_segment: u8 = bytes[1],
pub year: u16 = (bytes[2] as u16) + ((bytes[3] as u16) << 8),
pub month: u8 = bytes[4],
pub day: u8 = bytes[5],
},
0x12 => |bytes: [u8; 6]| #[derive(Debug, Copy, Clone)] StorageStartTimeTime {
pub user_index: u8 = bytes[0],
pub storage_segment: u8 = bytes[1],
pub hour: u8 = bytes[2],
pub minute: u8 = bytes[3],
pub second: u8 = bytes[4],
},
0x08 => |bytes: [u8; 6]| #[derive(Debug, Copy, Clone)] StorageDataLength {
pub user_index: u8 = bytes[0],
pub data_segment: u8 = bytes[1],
pub length: u32 =
(bytes[2] as u32) + ((bytes[3] as u32) << 8) + ((bytes[4] as u32) << 16) + ((bytes[5] as u32) << 24),
},
0x09 => |bytes: [u8; 4]| #[derive(Debug, Copy, Clone)] StorageDataWithPI {
pub spo2: u8 = bytes[0],
pub pulse_rate: u8 = bytes[1],
pub pi: u16 = (bytes[2] as u16) + ((bytes[3] as u16) << 8),
},
0x0A => |bytes: [u8; 2]| #[derive(Debug, Copy, Clone)] StorageDataSegmentAmount {
pub user_index: u8 = bytes[0],
pub segment_amount: u8 = bytes[1],
},
0x0B => |bytes: [u8; 2]| CommandFeedback {
pub command: u8 = bytes[0],
pub code: u8 = bytes[1],
},
0x0C => |_bytes: [u8; 0]| #[derive(Debug, Copy, Clone)] FreeFeedback {},
0x0D => |bytes: [u8; 1]| #[derive(Debug, Copy, Clone)] DisconnectNotice {
pub reason: u8 = bytes[0],
},
0x0E => |bytes: [u8; 1]| #[derive(Debug, Copy, Clone)] PIIdentifiers {
pub pi_support: u8 = bytes[0],
},
0x0F => |bytes: [u8; 6]| #[derive(Debug, Copy, Clone)] StorageData {
pub spo2_1: u8 = bytes[0],
pub pulse_rate_1: u8 = bytes[1],
pub spo2_2: u8 = bytes[2],
pub pulse_rate_2: u8 = bytes[3],
pub spo2_3: u8 = bytes[4],
pub pulse_rate_3: u8 = bytes[5],
},
0x10 => |bytes: [u8; 1]| #[derive(Debug, Copy, Clone)] UserAmount {
pub total_user: u8 = bytes[0],
},
0x11 => |bytes: [u8; 7]| #[derive(Debug, Copy, Clone)] DeviceNotice {
pub device_notice: u8 = bytes[0],
pub device_info: [u8; 6] = [bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6]],
},
0x15 => |bytes: [u8; 7]| #[derive(Debug, Copy, Clone)] StorageDataIdentifiers {
pub user_index: u8 = bytes[0],
pub data_segment: u8 = bytes[1],
pub pi_identifiers: u8 = bytes[2],
pub retention: [u8; 4] = [bytes[3], bytes[4], bytes[5], bytes[6]],
},
}
impl CommandFeedback {
pub fn message(&self) -> &str {
match self.code {
0x00 => "Completed operation",
0x01 => "Shutdown device",
0x02 => "Exchange users",
0x03 => "Recording",
0x04 => "Failure to delete the storage data",
0x05 => "Not supported",
_ => "Unknown reason",
}
}
}
impl Debug for CommandFeedback {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("CommandFeedback")
.field("command", &format_args!("{:#04X}", self.command))
.field("reason_code", &format_args!("{:#04X}", self.code))
.field("message", &format_args!("'{}'", self.message()))
.finish()
}
}