1#![no_std]
4
5extern crate alloc;
6
7mod packets;
8mod read;
9mod topic;
10
11use bytes::Buf;
12use core::fmt;
13use core::fmt::{Display, Formatter};
14pub use packets::*;
15pub use read::*;
16pub use topic::*;
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub enum Error {
21 InvalidConnectReturnCode(u8),
22 InvalidProtocol,
23 InvalidProtocolLevel(u8),
24 IncorrectPacketFormat,
25 InvalidPacketType(u8),
26 InvalidQoS(u8),
27 PacketIdZero,
28 PayloadSizeIncorrect,
29 PayloadTooLong,
30 PayloadSizeLimitExceeded,
31 PayloadRequired,
32 TopicNotUtf8,
33 BoundaryCrossed,
34 MalformedRemainingLength,
35 InsufficientBytes(usize),
36}
37
38#[derive(Debug, Clone, PartialEq)]
40pub enum Packet {
41 Connect(Connect),
42 ConnAck(ConnAck),
43 Publish(Publish),
44 PubAck(PubAck),
45 PubRec(PubRec),
46 PubRel(PubRel),
47 PubComp(PubComp),
48 Subscribe(Subscribe),
49 SubAck(SubAck),
50 Unsubscribe(Unsubscribe),
51 UnsubAck(UnsubAck),
52 PingReq,
53 PingResp,
54 Disconnect,
55}
56
57#[repr(u8)]
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60pub enum PacketType {
61 Connect = 1,
62 ConnAck,
63 Publish,
64 PubAck,
65 PubRec,
66 PubRel,
67 PubComp,
68 Subscribe,
69 SubAck,
70 Unsubscribe,
71 UnsubAck,
72 PingReq,
73 PingResp,
74 Disconnect,
75}
76
77#[derive(Debug, Clone, Copy, PartialEq, Eq)]
79pub enum Protocol {
80 MQTT(u8),
81}
82
83#[repr(u8)]
85#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd)]
86pub enum QoS {
87 AtMostOnce = 0,
88 AtLeastOnce = 1,
89 ExactlyOnce = 2,
90}
91
92pub struct FixedHeader {
105 byte1: u8,
108 fixed_len: usize,
112 remaining_len: usize,
115}
116
117impl FixedHeader {
118 pub fn new(byte1: u8, remaining_len_len: usize, remaining_len: usize) -> FixedHeader {
119 FixedHeader {
120 byte1,
121 fixed_len: remaining_len_len + 1,
122 remaining_len,
123 }
124 }
125
126 pub fn packet_type(&self) -> Result<PacketType, Error> {
127 let num = self.byte1 >> 4;
128 match num {
129 1 => Ok(PacketType::Connect),
130 2 => Ok(PacketType::ConnAck),
131 3 => Ok(PacketType::Publish),
132 4 => Ok(PacketType::PubAck),
133 5 => Ok(PacketType::PubRec),
134 6 => Ok(PacketType::PubRel),
135 7 => Ok(PacketType::PubComp),
136 8 => Ok(PacketType::Subscribe),
137 9 => Ok(PacketType::SubAck),
138 10 => Ok(PacketType::Unsubscribe),
139 11 => Ok(PacketType::UnsubAck),
140 12 => Ok(PacketType::PingReq),
141 13 => Ok(PacketType::PingResp),
142 14 => Ok(PacketType::Disconnect),
143 _ => Err(Error::InvalidPacketType(num)),
144 }
145 }
146
147 pub fn frame_length(&self) -> usize {
150 self.fixed_len + self.remaining_len
151 }
152}
153
154pub fn qos(num: u8) -> Result<QoS, Error> {
156 match num {
157 0 => Ok(QoS::AtMostOnce),
158 1 => Ok(QoS::AtLeastOnce),
159 2 => Ok(QoS::ExactlyOnce),
160 qos => Err(Error::InvalidQoS(qos)),
161 }
162}
163
164impl Display for Error {
165 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
166 write!(f, "Error = {:?}", self)
167 }
168}