Debian Codegraph @

crt/aws-c-mqtt/include/aws/mqtt/private/packets.h

raw symbols SCIP
1 #ifndef AWS_MQTT_PRIVATE_PACKETS_H
2 #define AWS_MQTT_PRIVATE_PACKETS_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 #include <aws/mqtt/mqtt.h>
10 #include <aws/mqtt/private/fixed_header.h>
11
12 #include <aws/common/array_list.h>
13 #include <aws/common/byte_buf.h>
14 #include <aws/common/string.h>
15
16 /*
17 * General MQTT Control Packet Format [MQTT-2]:
18 * 1. Fixed header, present in all packets
19 * 2. Variable header, present in some packets
20 * 3. Payload, preset in some packets
21 */
22
23 /* Struct used internally for representing subscriptions */
24 struct aws_mqtt_subscription {
25 /* Topic filter to subscribe to [MQTT-4.7]. */
26 struct aws_byte_cursor topic_filter;
27 /* Maximum QoS of messages to receive [MQTT-4.3]. */
28 enum aws_mqtt_qos qos;
29 };
30
31 /**
32 * Used to represent the following MQTT packets:
33 * - PUBACK
34 * - PUBREC
35 * - PUBREL
36 * - PUBCOMP
37 * - UNSUBACK
38 */
39 struct aws_mqtt_packet_ack {
40 /* Fixed header */
41 struct aws_mqtt_fixed_header fixed_header;
42
43 /* Variable header */
44 uint16_t packet_identifier;
45 };
46
47 /**
48 * Represents the MQTT SUBACK packet
49 */
50 struct aws_mqtt_packet_suback {
51 /* Fixed header */
52 struct aws_mqtt_fixed_header fixed_header;
53
54 /* Variable header */
55 uint16_t packet_identifier;
56
57 /* Payload */
58 /* List of uint8_t return code */
59 struct aws_array_list return_codes;
60 };
61
62 /* Represents the MQTT CONNECT packet */
63 struct aws_mqtt_packet_connect {
64 /* Fixed header */
65 struct aws_mqtt_fixed_header fixed_header;
66
67 /* Variable header */
68 bool clean_session;
69 bool has_will;
70 bool will_retain;
71 bool has_password;
72 bool has_username;
73 uint16_t keep_alive_timeout;
74 enum aws_mqtt_qos will_qos;
75 struct aws_byte_cursor client_identifier;
76
77 /* Payload */
78 struct aws_byte_cursor will_topic;
79 struct aws_byte_cursor will_message;
80 struct aws_byte_cursor username;
81 struct aws_byte_cursor password;
82 };
83
84 /* Represents the MQTT CONNACK packet */
85 struct aws_mqtt_packet_connack {
86 /* Fixed header */
87 struct aws_mqtt_fixed_header fixed_header;
88
89 /* Variable header */
90 bool session_present;
91 uint8_t connect_return_code;
92 };
93
94 /* Represents the MQTT PUBLISH packet */
95 struct aws_mqtt_packet_publish {
96 struct aws_mqtt_fixed_header fixed_header;
97
98 /* Variable header */
99 uint16_t packet_identifier;
100 struct aws_byte_cursor topic_name;
101
102 /* Payload */
103 struct aws_byte_cursor payload;
104 };
105
106 /* Represents the MQTT SUBSCRIBE packet */
107 struct aws_mqtt_packet_subscribe {
108 /* Fixed header */
109 struct aws_mqtt_fixed_header fixed_header;
110
111 /* Variable header */
112 uint16_t packet_identifier;
113
114 /* Payload */
115 /* List of aws_mqtt_subscription */
116 struct aws_array_list topic_filters;
117 };
118
119 /* Represents the MQTT UNSUBSCRIBE packet */
120 struct aws_mqtt_packet_unsubscribe {
121 /* Fixed header */
122 struct aws_mqtt_fixed_header fixed_header;
123
124 /* Variable header */
125 uint16_t packet_identifier;
126
127 /* Payload */
128 /* List of aws_byte_cursors */
129 struct aws_array_list topic_filters;
130 };
131 /**
132 * Used to represent the following MQTT packets:
133 * - PINGREQ
134 * - PINGRESP
135 * - DISCONNECT
136 */
137 struct aws_mqtt_packet_connection {
138 /* Fixed header */
139 struct aws_mqtt_fixed_header fixed_header;
140 };
141
142 #ifdef __cplusplus
143 extern "C" {
144 #endif
145
146 /*****************************************************************************/
147 /* Ack */
148
149 AWS_MQTT_API
150 int aws_mqtt_packet_ack_encode(struct aws_byte_buf *buf, const struct aws_mqtt_packet_ack *packet);
151
152 AWS_MQTT_API
153 int aws_mqtt_packet_ack_decode(struct aws_byte_cursor *cur, struct aws_mqtt_packet_ack *packet);
154
155 /*****************************************************************************/
156 /* Connect */
157
158 AWS_MQTT_API
159 int aws_mqtt_packet_connect_init(
160 struct aws_mqtt_packet_connect *packet,
161 struct aws_byte_cursor client_identifier,
162 bool clean_session,
163 uint16_t keep_alive);
164
165 AWS_MQTT_API
166 int aws_mqtt_packet_connect_add_will(
167 struct aws_mqtt_packet_connect *packet,
168 struct aws_byte_cursor topic,
169 enum aws_mqtt_qos qos,
170 bool retain,
171 struct aws_byte_cursor payload);
172
173 AWS_MQTT_API
174 int aws_mqtt_packet_connect_add_credentials(
175 struct aws_mqtt_packet_connect *packet,
176 struct aws_byte_cursor username,
177 struct aws_byte_cursor password);
178
179 AWS_MQTT_API
180 int aws_mqtt_packet_connect_encode(struct aws_byte_buf *buf, const struct aws_mqtt_packet_connect *packet);
181
182 AWS_MQTT_API
183 int aws_mqtt_packet_connect_decode(struct aws_byte_cursor *cur, struct aws_mqtt_packet_connect *packet);
184
185 /*****************************************************************************/
186 /* Connack */
187
188 AWS_MQTT_API
189 int aws_mqtt_packet_connack_init(
190 struct aws_mqtt_packet_connack *packet,
191 bool session_present,
192 enum aws_mqtt_connect_return_code return_code);
193
194 AWS_MQTT_API
195 int aws_mqtt_packet_connack_encode(struct aws_byte_buf *buf, const struct aws_mqtt_packet_connack *packet);
196
197 AWS_MQTT_API
198 int aws_mqtt_packet_connack_decode(struct aws_byte_cursor *cur, struct aws_mqtt_packet_connack *packet);
199
200 /*****************************************************************************/
201 /* Publish */
202
203 AWS_MQTT_API
204 int aws_mqtt_packet_publish_init(
205 struct aws_mqtt_packet_publish *packet,
206 bool retain,
207 enum aws_mqtt_qos qos,
208 bool dup,
209 struct aws_byte_cursor topic_name,
210 uint16_t packet_identifier,
211 struct aws_byte_cursor payload);
212
213 AWS_MQTT_API
214 int aws_mqtt_packet_publish_encode(struct aws_byte_buf *buf, const struct aws_mqtt_packet_publish *packet);
215
216 AWS_MQTT_API
217 int aws_mqtt_packet_publish_encode_headers(struct aws_byte_buf *buf, const struct aws_mqtt_packet_publish *packet);
218
219 AWS_MQTT_API
220 int aws_mqtt_packet_publish_decode(struct aws_byte_cursor *cur, struct aws_mqtt_packet_publish *packet);
221
222 AWS_MQTT_API
223 void aws_mqtt_packet_publish_set_dup(struct aws_mqtt_packet_publish *packet);
224
225 AWS_MQTT_API
226 bool aws_mqtt_packet_publish_get_dup(const struct aws_mqtt_packet_publish *packet);
227
228 AWS_MQTT_API
229 enum aws_mqtt_qos aws_mqtt_packet_publish_get_qos(const struct aws_mqtt_packet_publish *packet);
230
231 AWS_MQTT_API
232 bool aws_mqtt_packet_publish_get_retain(const struct aws_mqtt_packet_publish *packet);
233
234 /*****************************************************************************/
235 /* Puback */
236
237 AWS_MQTT_API
238 int aws_mqtt_packet_puback_init(struct aws_mqtt_packet_ack *packet, uint16_t packet_identifier);
239
240 /*****************************************************************************/
241 /* Pubrec */
242
243 AWS_MQTT_API
244 int aws_mqtt_packet_pubrec_init(struct aws_mqtt_packet_ack *packet, uint16_t packet_identifier);
245
246 /*****************************************************************************/
247 /* Pubrel */
248
249 AWS_MQTT_API
250 int aws_mqtt_packet_pubrel_init(struct aws_mqtt_packet_ack *packet, uint16_t packet_identifier);
251
252 /*****************************************************************************/
253 /* Pubcomp */
254
255 AWS_MQTT_API
256 int aws_mqtt_packet_pubcomp_init(struct aws_mqtt_packet_ack *packet, uint16_t packet_identifier);
257
258 /*****************************************************************************/
259 /* Subscribe */
260
261 AWS_MQTT_API
262 int aws_mqtt_packet_subscribe_init(
263 struct aws_mqtt_packet_subscribe *packet,
264 struct aws_allocator *allocator,
265 uint16_t packet_identifier);
266
267 AWS_MQTT_API
268 void aws_mqtt_packet_subscribe_clean_up(struct aws_mqtt_packet_subscribe *packet);
269
270 AWS_MQTT_API
271 int aws_mqtt_packet_subscribe_add_topic(
272 struct aws_mqtt_packet_subscribe *packet,
273 struct aws_byte_cursor topic_filter,
274 enum aws_mqtt_qos qos);
275
276 AWS_MQTT_API
277 int aws_mqtt_packet_subscribe_encode(struct aws_byte_buf *buf, const struct aws_mqtt_packet_subscribe *packet);
278
279 AWS_MQTT_API
280 int aws_mqtt_packet_subscribe_decode(struct aws_byte_cursor *cur, struct aws_mqtt_packet_subscribe *packet);
281
282 /*****************************************************************************/
283 /* Suback */
284
285 AWS_MQTT_API
286 int aws_mqtt_packet_suback_init(
287 struct aws_mqtt_packet_suback *packet,
288 struct aws_allocator *allocator,
289 uint16_t packet_identifier);
290
291 AWS_MQTT_API
292 void aws_mqtt_packet_suback_clean_up(struct aws_mqtt_packet_suback *packet);
293
294 AWS_MQTT_API
295 int aws_mqtt_packet_suback_add_return_code(struct aws_mqtt_packet_suback *packet, uint8_t return_code);
296
297 AWS_MQTT_API
298 int aws_mqtt_packet_suback_encode(struct aws_byte_buf *buf, const struct aws_mqtt_packet_suback *packet);
299
300 AWS_MQTT_API
301 int aws_mqtt_packet_suback_decode(struct aws_byte_cursor *cur, struct aws_mqtt_packet_suback *packet);
302
303 /*****************************************************************************/
304 /* Unsubscribe */
305
306 AWS_MQTT_API
307 int aws_mqtt_packet_unsubscribe_init(
308 struct aws_mqtt_packet_unsubscribe *packet,
309 struct aws_allocator *allocator,
310 uint16_t packet_identifier);
311
312 AWS_MQTT_API
313 void aws_mqtt_packet_unsubscribe_clean_up(struct aws_mqtt_packet_unsubscribe *packet);
314
315 AWS_MQTT_API
316 int aws_mqtt_packet_unsubscribe_add_topic(
317 struct aws_mqtt_packet_unsubscribe *packet,
318 struct aws_byte_cursor topic_filter);
319
320 AWS_MQTT_API
321 int aws_mqtt_packet_unsubscribe_encode(struct aws_byte_buf *buf, const struct aws_mqtt_packet_unsubscribe *packet);
322
323 AWS_MQTT_API
324 int aws_mqtt_packet_unsubscribe_decode(struct aws_byte_cursor *cur, struct aws_mqtt_packet_unsubscribe *packet);
325
326 /*****************************************************************************/
327 /* Unsuback */
328
329 AWS_MQTT_API
330 int aws_mqtt_packet_unsuback_init(struct aws_mqtt_packet_ack *packet, uint16_t packet_identifier);
331
332 /*****************************************************************************/
333 /* Ping request/response, disconnect */
334
335 AWS_MQTT_API
336 int aws_mqtt_packet_pingreq_init(struct aws_mqtt_packet_connection *packet);
337
338 AWS_MQTT_API
339 int aws_mqtt_packet_pingresp_init(struct aws_mqtt_packet_connection *packet);
340
341 AWS_MQTT_API
342 int aws_mqtt_packet_disconnect_init(struct aws_mqtt_packet_connection *packet);
343
344 AWS_MQTT_API
345 int aws_mqtt_packet_connection_encode(struct aws_byte_buf *buf, const struct aws_mqtt_packet_connection *packet);
346
347 AWS_MQTT_API
348 int aws_mqtt_packet_connection_decode(struct aws_byte_cursor *cur, struct aws_mqtt_packet_connection *packet);
349
350 #ifdef __cplusplus
351 }
352 #endif
353
354 #endif /* AWS_MQTT_PRIVATE_PACKETS_H */