1use core::fmt::{Display, Formatter};
26
27#[derive(Debug, PartialEq)]
28#[cfg_attr(feature = "defmt", derive(defmt::Format))]
29pub enum ReasonCode {
30 Success,
31 GrantedQoS1,
32 GrantedQoS2,
33 DisconnectWithWillMessage,
34 NoMatchingSubscribers,
35 NoSubscriptionExisted,
36 ContinueAuth,
37 ReAuthenticate,
38 UnspecifiedError,
39 MalformedPacket,
40 ProtocolError,
41 ImplementationSpecificError,
42 UnsupportedProtocolVersion,
43 ClientIdNotValid,
44 BadUserNameOrPassword,
45 NotAuthorized,
46 ServerUnavailable,
47 ServerBusy,
48 Banned,
49 ServerShuttingDown,
50 BadAuthMethod,
51 KeepAliveTimeout,
52 SessionTakeOver,
53 TopicFilterInvalid,
54 TopicNameInvalid,
55 PacketIdentifierInUse,
56 PacketIdentifierNotFound,
57 ReceiveMaximumExceeded,
58 TopicAliasInvalid,
59 PacketTooLarge,
60 MessageRateTooHigh,
61 QuotaExceeded,
62 AdministrativeAction,
63 PayloadFormatInvalid,
64 RetainNotSupported,
65 QoSNotSupported,
66 UseAnotherServer,
67 ServerMoved,
68 SharedSubscriptionNotSupported,
69 ConnectionRateExceeded,
70 MaximumConnectTime,
71 SubscriptionIdentifiersNotSupported,
72 WildcardSubscriptionNotSupported,
73 TimerNotSupported,
74 BuffError,
75 NetworkError,
76}
77
78impl From<ReasonCode> for u8 {
79 fn from(value: ReasonCode) -> Self {
80 match value {
81 ReasonCode::Success => 0x00,
82 ReasonCode::GrantedQoS1 => 0x01,
83 ReasonCode::GrantedQoS2 => 0x02,
84 ReasonCode::DisconnectWithWillMessage => 0x04,
85 ReasonCode::NoMatchingSubscribers => 0x10,
86 ReasonCode::NoSubscriptionExisted => 0x11,
87 ReasonCode::ContinueAuth => 0x18,
88 ReasonCode::ReAuthenticate => 0x19,
89 ReasonCode::UnspecifiedError => 0x80,
90 ReasonCode::MalformedPacket => 0x81,
91 ReasonCode::ProtocolError => 0x82,
92 ReasonCode::ImplementationSpecificError => 0x83,
93 ReasonCode::UnsupportedProtocolVersion => 0x84,
94 ReasonCode::ClientIdNotValid => 0x85,
95 ReasonCode::BadUserNameOrPassword => 0x86,
96 ReasonCode::NotAuthorized => 0x87,
97 ReasonCode::ServerUnavailable => 0x88,
98 ReasonCode::ServerBusy => 0x89,
99 ReasonCode::Banned => 0x8A,
100 ReasonCode::ServerShuttingDown => 0x8B,
101 ReasonCode::BadAuthMethod => 0x8C,
102 ReasonCode::KeepAliveTimeout => 0x8D,
103 ReasonCode::SessionTakeOver => 0x8E,
104 ReasonCode::TopicFilterInvalid => 0x8F,
105 ReasonCode::TopicNameInvalid => 0x90,
106 ReasonCode::PacketIdentifierInUse => 0x91,
107 ReasonCode::PacketIdentifierNotFound => 0x92,
108 ReasonCode::ReceiveMaximumExceeded => 0x93,
109 ReasonCode::TopicAliasInvalid => 0x94,
110 ReasonCode::PacketTooLarge => 0x95,
111 ReasonCode::MessageRateTooHigh => 0x96,
112 ReasonCode::QuotaExceeded => 0x97,
113 ReasonCode::AdministrativeAction => 0x98,
114 ReasonCode::PayloadFormatInvalid => 0x99,
115 ReasonCode::RetainNotSupported => 0x9A,
116 ReasonCode::QoSNotSupported => 0x9B,
117 ReasonCode::UseAnotherServer => 0x9C,
118 ReasonCode::ServerMoved => 0x9D,
119 ReasonCode::SharedSubscriptionNotSupported => 0x9E,
120 ReasonCode::ConnectionRateExceeded => 0x9F,
121 ReasonCode::MaximumConnectTime => 0xA0,
122 ReasonCode::SubscriptionIdentifiersNotSupported => 0xA1,
123 ReasonCode::WildcardSubscriptionNotSupported => 0xA2,
124 ReasonCode::TimerNotSupported => 0xFD,
125 ReasonCode::BuffError => 0xFE,
126 ReasonCode::NetworkError => 0xFF,
127 }
128 }
129}
130
131impl From<u8> for ReasonCode {
132 fn from(orig: u8) -> Self {
133 match orig {
134 0x00 => ReasonCode::Success,
135 0x01 => ReasonCode::GrantedQoS1,
136 0x02 => ReasonCode::GrantedQoS2,
137 0x04 => ReasonCode::DisconnectWithWillMessage,
138 0x10 => ReasonCode::NoMatchingSubscribers,
139 0x11 => ReasonCode::NoSubscriptionExisted,
140 0x18 => ReasonCode::ContinueAuth,
141 0x19 => ReasonCode::ReAuthenticate,
142 0x80 => ReasonCode::UnspecifiedError,
143 0x81 => ReasonCode::MalformedPacket,
144 0x82 => ReasonCode::ProtocolError,
145 0x83 => ReasonCode::ImplementationSpecificError,
146 0x84 => ReasonCode::UnsupportedProtocolVersion,
147 0x85 => ReasonCode::ClientIdNotValid,
148 0x86 => ReasonCode::BadUserNameOrPassword,
149 0x87 => ReasonCode::NotAuthorized,
150 0x88 => ReasonCode::ServerUnavailable,
151 0x89 => ReasonCode::ServerBusy,
152 0x8A => ReasonCode::Banned,
153 0x8B => ReasonCode::ServerShuttingDown,
154 0x8C => ReasonCode::BadAuthMethod,
155 0x8D => ReasonCode::KeepAliveTimeout,
156 0x8E => ReasonCode::SessionTakeOver,
157 0x8F => ReasonCode::TopicFilterInvalid,
158 0x90 => ReasonCode::TopicNameInvalid,
159 0x91 => ReasonCode::PacketIdentifierInUse,
160 0x92 => ReasonCode::PacketIdentifierNotFound,
161 0x93 => ReasonCode::ReceiveMaximumExceeded,
162 0x94 => ReasonCode::TopicAliasInvalid,
163 0x95 => ReasonCode::PacketTooLarge,
164 0x96 => ReasonCode::MessageRateTooHigh,
165 0x97 => ReasonCode::QuotaExceeded,
166 0x98 => ReasonCode::AdministrativeAction,
167 0x99 => ReasonCode::PayloadFormatInvalid,
168 0x9A => ReasonCode::RetainNotSupported,
169 0x9B => ReasonCode::QoSNotSupported,
170 0x9C => ReasonCode::UseAnotherServer,
171 0x9D => ReasonCode::ServerMoved,
172 0x9E => ReasonCode::SharedSubscriptionNotSupported,
173 0xA0 => ReasonCode::MaximumConnectTime,
174 0xA1 => ReasonCode::SubscriptionIdentifiersNotSupported,
175 0xA2 => ReasonCode::WildcardSubscriptionNotSupported,
176 0xFD => ReasonCode::TimerNotSupported,
177 0xFE => ReasonCode::BuffError,
178 _ => ReasonCode::NetworkError,
179 }
180 }
181}
182
183impl Display for ReasonCode {
184 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
185 match *self {
186 ReasonCode::Success => write!(f, "Operation was successful!"),
187 ReasonCode::GrantedQoS1 => write!(f, "Granted QoS level 1!"),
188 ReasonCode::GrantedQoS2 => write!(f, "Granted QoS level 2!"),
189 ReasonCode::DisconnectWithWillMessage => write!(f, "Disconnected with Will message!"),
190 ReasonCode::NoMatchingSubscribers => write!(f, "No matching subscribers on broker!"),
191 ReasonCode::NoSubscriptionExisted => write!(f, "Subscription not exist!"),
192 ReasonCode::ContinueAuth => write!(f, "Broker asks for more AUTH packets!"),
193 ReasonCode::ReAuthenticate => write!(f, "Broker requires re-authentication!"),
194 ReasonCode::UnspecifiedError => write!(f, "Unspecified error!"),
195 ReasonCode::MalformedPacket => write!(f, "Malformed packet sent!"),
196 ReasonCode::ProtocolError => write!(f, "Protocol specific error!"),
197 ReasonCode::ImplementationSpecificError => write!(f, "Implementation specific error!"),
198 ReasonCode::UnsupportedProtocolVersion => write!(f, "Unsupported protocol version!"),
199 ReasonCode::ClientIdNotValid => write!(f, "Client sent not valid identification"),
200 ReasonCode::BadUserNameOrPassword => {
201 write!(f, "Authentication error, username of password not valid!")
202 }
203 ReasonCode::NotAuthorized => write!(f, "Client not authorized!"),
204 ReasonCode::ServerUnavailable => write!(f, "Server unavailable!"),
205 ReasonCode::ServerBusy => write!(f, "Server is busy!"),
206 ReasonCode::Banned => write!(f, "Client is banned on broker!"),
207 ReasonCode::ServerShuttingDown => write!(f, "Server is shutting down!"),
208 ReasonCode::BadAuthMethod => write!(f, "Provided bad authentication method!"),
209 ReasonCode::KeepAliveTimeout => write!(f, "Client reached timeout"),
210 ReasonCode::SessionTakeOver => write!(f, "Took over session!"),
211 ReasonCode::TopicFilterInvalid => write!(f, "Topic filter is not valid!"),
212 ReasonCode::TopicNameInvalid => write!(f, "Topic name is not valid!"),
213 ReasonCode::PacketIdentifierInUse => write!(f, "Packet identifier is already in use!"),
214 ReasonCode::PacketIdentifierNotFound => write!(f, "Packet identifier not found!"),
215 ReasonCode::ReceiveMaximumExceeded => write!(f, "Maximum receive amount exceeded!"),
216 ReasonCode::TopicAliasInvalid => write!(f, "Invalid topic alias!"),
217 ReasonCode::PacketTooLarge => write!(f, "Sent packet was too large!"),
218 ReasonCode::MessageRateTooHigh => write!(f, "Message rate is too high!"),
219 ReasonCode::QuotaExceeded => write!(f, "Quota exceeded!"),
220 ReasonCode::AdministrativeAction => write!(f, "Administrative action!"),
221 ReasonCode::PayloadFormatInvalid => write!(f, "Invalid payload format!"),
222 ReasonCode::RetainNotSupported => write!(f, "Message retain not supported!"),
223 ReasonCode::QoSNotSupported => write!(f, "Used QoS is not supported!"),
224 ReasonCode::UseAnotherServer => write!(f, "Use another server!"),
225 ReasonCode::ServerMoved => write!(f, "Server moved!"),
226 ReasonCode::SharedSubscriptionNotSupported => {
227 write!(f, "Shared subscription is not supported")
228 }
229 ReasonCode::ConnectionRateExceeded => write!(f, "Connection rate exceeded!"),
230 ReasonCode::MaximumConnectTime => write!(f, "Maximum connect time exceeded!"),
231 ReasonCode::SubscriptionIdentifiersNotSupported => {
232 write!(f, "Subscription identifier not supported!")
233 }
234 ReasonCode::WildcardSubscriptionNotSupported => {
235 write!(f, "Wildcard subscription not supported!")
236 }
237 ReasonCode::TimerNotSupported => write!(f, "Timer implementation is not provided"),
238 ReasonCode::BuffError => write!(f, "Error encountered during write / read from packet"),
239 ReasonCode::NetworkError => write!(f, "Unknown error!"),
240 }
241 }
242}