View Javadoc
1   /*
2    * Copyright 2023 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty.handler.codec.mqtt;
18  
19  /**
20   * Provides a set of enumeration that exposes standard MQTT 5 reason codes used by various messages.
21   * */
22  public final class MqttReasonCodes {
23  
24      private MqttReasonCodes() {
25      }
26  
27      /**
28       * @return the corresponding enum value to the hex value.
29       * */
30      private static <E> E valueOfHelper(byte b, E[] values) {
31          try {
32              return values[b & 0xFF];
33          } catch (ArrayIndexOutOfBoundsException e) {
34              throw new IllegalArgumentException("unknown reason code: " + b);
35          }
36      }
37  
38      /**
39       * Reason codes for MQTT Disconnect message.
40       */
41      public enum Disconnect {
42          NORMAL_DISCONNECT((byte) 0x00), //sent by: client or server
43          DISCONNECT_WITH_WILL_MESSAGE((byte) 0x04), //sent by: client
44          UNSPECIFIED_ERROR((byte) 0x80), //sent by: client or server
45          MALFORMED_PACKET((byte) 0x81), //sent by: client or server
46          PROTOCOL_ERROR((byte) 0x82), //sent by: client or server
47          IMPLEMENTATION_SPECIFIC_ERROR((byte) 0x83), //sent by: client or server
48          NOT_AUTHORIZED((byte) 0x87), //sent by: server
49          SERVER_BUSY((byte) 0x89), //sent by: server
50          SERVER_SHUTTING_DOWN((byte) 0x8B), //sent by: server
51          KEEP_ALIVE_TIMEOUT((byte) 0x8D), //sent by: Server
52          SESSION_TAKEN_OVER((byte) 0x8E), //sent by: Server
53          TOPIC_FILTER_INVALID((byte) 0x8F), //sent by: Server
54          TOPIC_NAME_INVALID((byte) 0x90), //sent by: Client or Server
55          RECEIVE_MAXIMUM_EXCEEDED((byte) 0x93), //sent by: Client or Server
56          TOPIC_ALIAS_INVALID((byte) 0x94), //sent by: Client or Server
57          PACKET_TOO_LARGE((byte) 0x95), //sent by: Client or Server
58          MESSAGE_RATE_TOO_HIGH((byte) 0x96), //sent by: Client or Server
59          QUOTA_EXCEEDED((byte) 0x97), //sent by: Client or Server
60          ADMINISTRATIVE_ACTION((byte) 0x98), //sent by: Client or Server
61          PAYLOAD_FORMAT_INVALID((byte) 0x99), //sent by: Client or Server
62          RETAIN_NOT_SUPPORTED((byte) 0x9A), //sent by: Server
63          QOS_NOT_SUPPORTED((byte) 0x9B), //sent by: Server
64          USE_ANOTHER_SERVER((byte) 0x9C), //sent by: Server
65          SERVER_MOVED((byte) 0x9D), //sent by: Server
66          SHARED_SUBSCRIPTIONS_NOT_SUPPORTED((byte) 0x9E), //sent by: Server
67          CONNECTION_RATE_EXCEEDED((byte) 0x9F), //sent by: Server
68          MAXIMUM_CONNECT_TIME((byte) 0xA0), //sent by: Server
69          SUBSCRIPTION_IDENTIFIERS_NOT_SUPPORTED((byte) 0xA1), //sent by: Server
70          WILDCARD_SUBSCRIPTIONS_NOT_SUPPORTED((byte) 0xA2); //sent by: Server
71  
72          protected static final Disconnect[] VALUES;
73  
74          static {
75              Disconnect[] values = values();
76              VALUES = new Disconnect[163];
77              for (Disconnect code : values) {
78                  final int unsignedByte = code.byteValue & 0xFF;
79                  // Suppress a warning about out of bounds access since the enum contains only correct values
80                  VALUES[unsignedByte] = code;    //  [java/index-out-of-bounds]
81              }
82          }
83  
84          private final byte byteValue;
85  
86          Disconnect(byte byteValue) {
87              this.byteValue = byteValue;
88          }
89  
90          /**
91           * @return the value number corresponding to the constant.
92           * */
93          public byte byteValue() {
94              return byteValue;
95          }
96  
97          /**
98           * @param b the number to decode.
99           * @return the enum value corresponding to the number.
100          * */
101         public static Disconnect valueOf(byte b) {
102             return valueOfHelper(b, VALUES);
103         }
104     }
105 
106     /**
107      * Reason codes for MQTT Auth message.
108      */
109     public enum Auth {
110         SUCCESS((byte) 0x00), //sent by: Server
111         CONTINUE_AUTHENTICATION((byte) 0x18), //sent by: Client or Server
112         REAUTHENTICATE((byte) 0x19); //sent by: Client
113 
114         private static final Auth[] VALUES;
115 
116         static {
117             Auth[] values = values();
118             VALUES = new Auth[26];
119             for (Auth code : values) {
120                 final int unsignedByte = code.byteValue & 0xFF;
121                 // Suppress a warning about out of bounds access since the enum contains only correct values
122                 VALUES[unsignedByte] = code;    //  [java/index-out-of-bounds]
123             }
124         }
125 
126         private final byte byteValue;
127 
128         Auth(byte byteValue) {
129             this.byteValue = byteValue;
130         }
131 
132         /**
133          * @return the value number corresponding to the constant.
134          * */
135         public byte byteValue() {
136             return byteValue;
137         }
138 
139         /**
140          * @param b the number to decode.
141          * @return the enum value corresponding to the number.
142          * */
143         public static Auth valueOf(byte b) {
144             return valueOfHelper(b, VALUES);
145         }
146     }
147 
148     /**
149      * Reason codes for MQTT PubAck message.
150      */
151     public enum PubAck {
152         SUCCESS((byte) 0x00),
153         NO_MATCHING_SUBSCRIBERS((byte) 0x10),
154         UNSPECIFIED_ERROR((byte) 0x80),
155         IMPLEMENTATION_SPECIFIC_ERROR((byte) 0x83),
156         NOT_AUTHORIZED((byte) 0x87),
157         TOPIC_NAME_INVALID((byte) 0x90),
158         PACKET_IDENTIFIER_IN_USE((byte) 0x91),
159         QUOTA_EXCEEDED((byte) 0x97),
160         PAYLOAD_FORMAT_INVALID((byte) 0x99);
161 
162         private static final PubAck[] VALUES;
163 
164         static {
165             PubAck[] values = values();
166             VALUES = new PubAck[154];
167             for (PubAck code : values) {
168                 final int unsignedByte = code.byteValue & 0xFF;
169                 // Suppress a warning about out of bounds access since the enum contains only correct values
170                 VALUES[unsignedByte] = code;    //  [java/index-out-of-bounds]
171             }
172         }
173 
174         private final byte byteValue;
175 
176         PubAck(byte byteValue) {
177             this.byteValue = byteValue;
178         }
179 
180         /**
181          * @return the value number corresponding to the constant.
182          * */
183         public byte byteValue() {
184             return byteValue;
185         }
186 
187         /**
188          * @param b the number to decode.
189          * @return the enum value corresponding to the number.
190          * */
191         public static PubAck valueOf(byte b) {
192             return valueOfHelper(b, VALUES);
193         }
194     }
195 
196     /**
197      * Reason codes for MQTT PubRec message.
198      */
199     public enum PubRec {
200         SUCCESS((byte) 0x00),
201         NO_MATCHING_SUBSCRIBERS((byte) 0x10),
202         UNSPECIFIED_ERROR((byte) 0x80),
203         IMPLEMENTATION_SPECIFIC_ERROR((byte) 0x83),
204         NOT_AUTHORIZED((byte) 0x87),
205         TOPIC_NAME_INVALID((byte) 0x90),
206         PACKET_IDENTIFIER_IN_USE((byte) 0x91),
207         QUOTA_EXCEEDED((byte) 0x97),
208         PAYLOAD_FORMAT_INVALID((byte) 0x99);
209 
210         private static final PubRec[] VALUES;
211 
212         static {
213             PubRec[] values = values();
214             VALUES = new PubRec[154];
215             for (PubRec code : values) {
216                 final int unsignedByte = code.byteValue & 0xFF;
217                 // Suppress a warning about out of bounds access since the enum contains only correct values
218                 VALUES[unsignedByte] = code;    //  [java/index-out-of-bounds]
219             }
220         }
221 
222         private final byte byteValue;
223 
224         PubRec(byte byteValue) {
225             this.byteValue = byteValue;
226         }
227 
228         /**
229          * @return the value number corresponding to the constant.
230          * */
231         public byte byteValue() {
232             return byteValue;
233         }
234 
235         /**
236          * @param b the number to decode.
237          * @return the enum value corresponding to the number.
238          * */
239         public static PubRec valueOf(byte b) {
240             return valueOfHelper(b, VALUES);
241         }
242     }
243 
244     /**
245      * Reason codes for MQTT PubRel message.
246      */
247     public enum PubRel {
248         SUCCESS((byte) 0x00),
249         PACKET_IDENTIFIER_NOT_FOUND((byte) 0x92);
250 
251         private static final PubRel[] VALUES;
252 
253         static {
254             PubRel[] values = values();
255             VALUES = new PubRel[147];
256             for (PubRel code : values) {
257                 final int unsignedByte = code.byteValue & 0xFF;
258                 // Suppress a warning about out of bounds access since the enum contains only correct values
259                 VALUES[unsignedByte] = code;    //  [java/index-out-of-bounds]
260             }
261         }
262 
263         private final byte byteValue;
264 
265         PubRel(byte byteValue) {
266             this.byteValue = byteValue;
267         }
268 
269         /**
270          * @return the value number corresponding to the constant.
271          * */
272         public byte byteValue() {
273             return byteValue;
274         }
275 
276         /**
277          * @param b the number to decode.
278          * @return the enum value corresponding to the number.
279          * */
280         public static PubRel valueOf(byte b) {
281             return valueOfHelper(b, VALUES);
282         }
283     }
284 
285     /**
286      * Reason codes for MQTT PubComp message.
287      */
288     public enum PubComp {
289         SUCCESS((byte) 0x00),
290         PACKET_IDENTIFIER_NOT_FOUND((byte) 0x92);
291 
292         private static final PubComp[] VALUES;
293 
294         static {
295             PubComp[] values = values();
296             VALUES = new PubComp[147];
297             for (PubComp code : values) {
298                 final int unsignedByte = code.byteValue & 0xFF;
299                 // Suppress a warning about out of bounds access since the enum contains only correct values
300                 VALUES[unsignedByte] = code;    //  [java/index-out-of-bounds]
301             }
302         }
303 
304         private final byte byteValue;
305 
306         PubComp(byte byteValue) {
307             this.byteValue = byteValue;
308         }
309 
310         /**
311          * @return the value number corresponding to the constant.
312          * */
313         public byte byteValue() {
314             return byteValue;
315         }
316 
317         /**
318          * @param b the number to decode.
319          * @return the enum value corresponding to the number.
320          * */
321         public static PubComp valueOf(byte b) {
322             return valueOfHelper(b, VALUES);
323         }
324     }
325 
326     /**
327      * Reason codes for MQTT SubAck message.
328      */
329     public enum SubAck {
330         GRANTED_QOS_0((byte) 0x00),
331         GRANTED_QOS_1((byte) 0x01),
332         GRANTED_QOS_2((byte) 0x02),
333         UNSPECIFIED_ERROR((byte) 0x80),
334         IMPLEMENTATION_SPECIFIC_ERROR((byte) 0x83),
335         NOT_AUTHORIZED((byte) 0x87),
336         TOPIC_FILTER_INVALID((byte) 0x8F),
337         PACKET_IDENTIFIER_IN_USE((byte) 0x91),
338         QUOTA_EXCEEDED((byte) 0x97),
339         SHARED_SUBSCRIPTIONS_NOT_SUPPORTED((byte) 0x9E),
340         SUBSCRIPTION_IDENTIFIERS_NOT_SUPPORTED((byte) 0xA1),
341         WILDCARD_SUBSCRIPTIONS_NOT_SUPPORTED((byte) 0xA2);
342 
343         private static final SubAck[] VALUES;
344 
345         static {
346             SubAck[] values = values();
347             VALUES = new SubAck[163];
348             for (SubAck code : values) {
349                 final int unsignedByte = code.byteValue & 0xFF;
350                 // Suppress a warning about out of bounds access since the enum contains only correct values
351                 VALUES[unsignedByte] = code;    //  [java/index-out-of-bounds]
352             }
353         }
354 
355         private final byte byteValue;
356 
357         SubAck(byte byteValue) {
358             this.byteValue = byteValue;
359         }
360 
361         /**
362          * @return the value number corresponding to the constant.
363          * */
364         public byte byteValue() {
365             return byteValue;
366         }
367 
368         /**
369          * @param b the number to decode.
370          * @return the enum value corresponding to the number.
371          * */
372         public static SubAck valueOf(byte b) {
373             return valueOfHelper(b, VALUES);
374         }
375     }
376 
377     /**
378      * Reason codes for MQTT UnsubAck message.
379      */
380     public enum UnsubAck {
381         SUCCESS((byte) 0x00),
382         NO_SUBSCRIPTION_EXISTED((byte) 0x11),
383         UNSPECIFIED_ERROR((byte) 0x80),
384         IMPLEMENTATION_SPECIFIC_ERROR((byte) 0x83),
385         NOT_AUTHORIZED((byte) 0x87),
386         TOPIC_FILTER_INVALID((byte) 0x8F),
387         PACKET_IDENTIFIER_IN_USE((byte) 0x91);
388 
389         private static final UnsubAck[] VALUES;
390 
391         static {
392             UnsubAck[] values = values();
393             VALUES = new UnsubAck[146];
394             for (UnsubAck code : values) {
395                 final int unsignedByte = code.byteValue & 0xFF;
396                 // Suppress a warning about out of bounds access since the enum contains only correct values
397                 VALUES[unsignedByte] = code;    //  [java/index-out-of-bounds]
398             }
399         }
400 
401         private final byte byteValue;
402 
403         UnsubAck(byte byteValue) {
404             this.byteValue = byteValue;
405         }
406 
407         /**
408          * @return the value number corresponding to the constant.
409          * */
410         public byte byteValue() {
411             return byteValue;
412         }
413 
414         /**
415          * @param b the number to decode.
416          * @return the enum value corresponding to the number.
417          * */
418         public static UnsubAck valueOf(byte b) {
419             return valueOfHelper(b, VALUES);
420         }
421     }
422 }