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
|
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
//! Everything around parsing the fixed MQTT Header
use winnow::binary::bits::bits;
use winnow::error::ErrMode;
use winnow::error::FromExternalError;
use winnow::error::InputError;
use winnow::error::ParserError;
use winnow::Bytes;
use winnow::Parser;
use super::MResult;
#[derive(num_enum::TryFromPrimitive, num_enum::IntoPrimitive)]
#[repr(u8)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum QualityOfService {
AtMostOnce = 0,
AtLeastOnce = 1,
ExactlyOnce = 2,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum PacketType {
Connect,
Connack,
Publish {
dup: bool,
qos: QualityOfService,
retain: bool,
},
Puback,
Pubrec,
Pubrel,
Pubcomp,
Subscribe,
Suback,
Unsubscribe,
Unsuback,
Pingreq,
Pingresp,
Disconnect,
Auth,
}
#[derive(Debug, PartialEq)]
pub struct MFixedHeader {
pub packet_type: PacketType,
}
impl MFixedHeader {
pub fn parse(input: &mut &Bytes) -> MResult<MFixedHeader> {
let (packet_type, packet_flags): (u8, u8) = bits::<_, _, InputError<(_, usize)>, _, _>((
winnow::binary::bits::take(4usize),
winnow::binary::bits::take(4usize),
))
.parse_next(input)
.map_err(|_: ErrMode<InputError<_>>| {
ErrMode::from_error_kind(input, winnow::error::ErrorKind::Slice)
})?;
let packet_type = match (packet_type, packet_flags) {
(0, _) => {
return Err(ErrMode::from_error_kind(
input,
winnow::error::ErrorKind::Verify,
))
}
(1, 0) => PacketType::Connect,
(2, 0) => PacketType::Connack,
(3, flags) => PacketType::Publish {
dup: (0b1000 & flags) != 0,
qos: QualityOfService::try_from((flags & 0b0110) >> 1).map_err(|e| {
ErrMode::from_external_error(input, winnow::error::ErrorKind::Verify, e)
})?,
retain: (0b0001 & flags) != 0,
},
(4, 0) => PacketType::Puback,
(5, 0) => PacketType::Pubrec,
(6, 0b0010) => PacketType::Pubrel,
(7, 0) => PacketType::Pubcomp,
(8, 0b0010) => PacketType::Subscribe,
(9, 0) => PacketType::Suback,
(10, 0b0010) => PacketType::Unsubscribe,
(11, 0) => PacketType::Unsuback,
(12, 0) => PacketType::Pingreq,
(13, 0) => PacketType::Pingresp,
(14, 0) => PacketType::Disconnect,
(15, 0) => PacketType::Auth,
_ => {
return Err(ErrMode::from_error_kind(
input,
winnow::error::ErrorKind::Verify,
))
}
};
Ok(MFixedHeader { packet_type })
}
}
#[cfg(test)]
mod tests {
use winnow::Bytes;
use crate::v5::fixed_header::MFixedHeader;
#[test]
fn check_fixed_header() {
let input = &[0b0011_1010];
assert_eq!(
MFixedHeader::parse(&mut Bytes::new(&input)).unwrap(),
MFixedHeader {
packet_type: crate::v5::fixed_header::PacketType::Publish {
dup: true,
qos: crate::v5::fixed_header::QualityOfService::AtLeastOnce,
retain: false
},
}
)
}
}
|