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
use byteorder::{ByteOrder, LittleEndian};
use core::time::Duration;
#[derive(Clone, Debug)]
pub struct ExpectedConnectionLength {
range: (Duration, Duration),
}
impl ExpectedConnectionLength {
pub fn new(
min: Duration,
max: Duration,
) -> Result<ExpectedConnectionLength, ExpectedConnectionLengthError> {
if min > max {
return Err(ExpectedConnectionLengthError::Inverted(min, max));
}
const ABSOLUTE_MAX: Duration = Duration::from_micros(40_959_375);
assert_eq!(Self::as_u16(ABSOLUTE_MAX), 0xFFFF);
if max > ABSOLUTE_MAX {
return Err(ExpectedConnectionLengthError::TooLong(max));
}
Ok(ExpectedConnectionLength { range: (min, max) })
}
pub fn into_bytes(&self, bytes: &mut [u8]) {
assert!(bytes.len() >= 4);
LittleEndian::write_u16(&mut bytes[0..2], Self::as_u16(self.range.0));
LittleEndian::write_u16(&mut bytes[2..4], Self::as_u16(self.range.1));
}
fn as_u16(d: Duration) -> u16 {
(1600 * d.as_secs() as u32 + (d.subsec_micros() / 625)) as u16
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum ExpectedConnectionLengthError {
TooLong(Duration),
Inverted(Duration, Duration),
}