1use crate::{
2 eio::{Read, Write},
3 io::{
4 err::{ReadError, WriteError},
5 read::Readable,
6 write::Writable,
7 },
8};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
16#[cfg_attr(feature = "defmt", derive(defmt::Format))]
17#[repr(u8)]
18pub enum ReasonCode {
19 Success = 0x00,
23
24 GrantedQoS1 = 0x01,
26
27 GrantedQoS2 = 0x02,
29
30 DisconnectWithWillMessage = 0x04,
32
33 NoMatchingSubscribers = 0x10,
35
36 NoSubscriptionExisted = 0x11,
38
39 ContinueAuthentication = 0x18,
41
42 ReAuthenticate = 0x19,
44
45 #[default]
46 UnspecifiedError = 0x80,
48
49 MalformedPacket = 0x81,
51
52 ProtocolError = 0x82,
54
55 ImplementationSpecificError = 0x83,
57
58 UnsupportedProtocolVersion = 0x84,
60
61 ClientIdentifierNotValid = 0x85,
63
64 BadUserNameOrPassword = 0x86,
66
67 NotAuthorized = 0x87,
69
70 ServerUnavailable = 0x88,
72
73 ServerBusy = 0x89,
75
76 Banned = 0x8A,
78
79 ServerShuttingDown = 0x8B,
81
82 BadAuthenticationMethod = 0x8C,
84
85 KeepAliveTimeout = 0x8D,
87
88 SessionTakenOver = 0x8E,
90
91 TopicFilterInvalid = 0x8F,
93
94 TopicNameInvalid = 0x90,
96
97 PacketIdentifierInUse = 0x91,
99
100 PacketIdentifierNotFound = 0x92,
102
103 ReceiveMaximumExceeded = 0x93,
105
106 TopicAliasInvalid = 0x94,
108
109 PacketTooLarge = 0x95,
111
112 MessageRateTooHigh = 0x96,
114
115 QuotaExceeded = 0x97,
117
118 AdministrativeAction = 0x98,
120
121 PayloadFormatInvalid = 0x99,
123
124 RetainNotSupported = 0x9A,
126
127 QoSNotSupported = 0x9B,
129
130 UseAnotherServer = 0x9C,
132
133 ServerMoved = 0x9D,
135
136 SharedSubscriptionsNotSupported = 0x9E,
138
139 ConnectionRateExceeded = 0x9F,
141
142 MaximumConnectTime = 0xA0,
144
145 SubscriptionIdentifiersNotSupported = 0xA1,
147
148 WildcardSubscriptionsNotSupported = 0xA2,
150}
151
152impl ReasonCode {
153 #[must_use]
155 pub const fn value(self) -> u8 {
156 self as u8
157 }
158
159 #[must_use]
162 pub const fn is_success(&self) -> bool {
163 self.value() < 0x80
164 }
165
166 #[must_use]
169 pub const fn is_erroneous(&self) -> bool {
170 self.value() >= 0x80
171 }
172}
173
174impl<R: Read> Readable<R> for ReasonCode {
175 async fn read(net: &mut R) -> Result<Self, ReadError<R::Error>> {
176 let value = u8::read(net).await?;
177 Ok(match value {
178 0x00 => Self::Success, 0x01 => Self::GrantedQoS1,
180 0x02 => Self::GrantedQoS2,
181 0x04 => Self::DisconnectWithWillMessage,
182 0x10 => Self::NoMatchingSubscribers,
183 0x11 => Self::NoSubscriptionExisted,
184 0x18 => Self::ContinueAuthentication,
185 0x19 => Self::ReAuthenticate,
186 0x80 => Self::UnspecifiedError,
187 0x81 => Self::MalformedPacket,
188 0x82 => Self::ProtocolError,
189 0x83 => Self::ImplementationSpecificError,
190 0x84 => Self::UnsupportedProtocolVersion,
191 0x85 => Self::ClientIdentifierNotValid,
192 0x86 => Self::BadUserNameOrPassword,
193 0x87 => Self::NotAuthorized,
194 0x88 => Self::ServerUnavailable,
195 0x89 => Self::ServerBusy,
196 0x8A => Self::Banned,
197 0x8B => Self::ServerShuttingDown,
198 0x8C => Self::BadAuthenticationMethod,
199 0x8D => Self::KeepAliveTimeout,
200 0x8E => Self::SessionTakenOver,
201 0x8F => Self::TopicFilterInvalid,
202 0x90 => Self::TopicNameInvalid,
203 0x91 => Self::PacketIdentifierInUse,
204 0x92 => Self::PacketIdentifierNotFound,
205 0x93 => Self::ReceiveMaximumExceeded,
206 0x94 => Self::TopicAliasInvalid,
207 0x95 => Self::PacketTooLarge,
208 0x96 => Self::MessageRateTooHigh,
209 0x97 => Self::QuotaExceeded,
210 0x98 => Self::AdministrativeAction,
211 0x99 => Self::PayloadFormatInvalid,
212 0x9A => Self::RetainNotSupported,
213 0x9B => Self::QoSNotSupported,
214 0x9C => Self::UseAnotherServer,
215 0x9D => Self::ServerMoved,
216 0x9E => Self::SharedSubscriptionsNotSupported,
217 0x9F => Self::ConnectionRateExceeded,
218 0xA0 => Self::MaximumConnectTime,
219 0xA1 => Self::SubscriptionIdentifiersNotSupported,
220 0xA2 => Self::WildcardSubscriptionsNotSupported,
221 _ => return Err(ReadError::ProtocolError),
222 })
223 }
224}
225
226impl Writable for ReasonCode {
227 fn written_len(&self) -> usize {
228 1
229 }
230
231 async fn write<W: Write>(&self, write: &mut W) -> Result<(), WriteError<W::Error>> {
232 self.value().write(write).await
233 }
234}