3 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
24 * @file iot_mqtt_serialize.h
\r
25 * @brief User-facing functions for serializing MQTT 3.1.1 packets. This header should
\r
26 * be included for building a single threaded light-weight MQTT client bypassing
\r
27 * stateful CSDK MQTT library.
\r
30 #ifndef _IOT_MQTT_SERIALIZE_H_
\r
31 #define _IOT_MQTT_SERIALIZE_H_
\r
33 /* The config header is always included first. */
\r
34 #include "iot_config.h"
\r
36 /* MQTT types include. */
\r
37 #include "types/iot_mqtt_types.h"
\r
39 /*------------------------- MQTT library functions --------------------------*/
\r
42 * @functionspage{mqtt,MQTT library}
\r
43 * - @functionname{mqtt_function_getconnectpacketsize}
\r
44 * - @functionname{mqtt_function_serializeconnect}
\r
45 * - @functionname{mqtt_function_getsubscriptionpacketsize}
\r
46 * - @functionname{mqtt_function_serializesubscribe}
\r
47 * - @functionname{mqtt_function_serializeunsubscribe}
\r
48 * - @functionname{mqtt_function_getpublishpacketsize}
\r
49 * - @functionname{mqtt_function_serializepublish}
\r
50 * - @functionname{mqtt_function_serializedisconnect}
\r
51 * - @functionname{mqtt_function_serializepingreq}
\r
52 * - @functionname{mqtt_function_getincomingmqttpackettypeandlength}
\r
53 * - @functionname{mqtt_function_deserializeresponse}
\r
54 * - @functionname{mqtt_function_deserializepublish}
\r
58 * @functionpage{IotMqtt_GetConnectPacketSize,mqtt,getconnectpacketsize}
\r
59 * @functionpage{IotMqtt_SerializeConnect,mqtt,serializeconnect}
\r
60 * @functionpage{IotMqtt_GetSubscriptionPacketSize,mqtt,getsubscriptionpacketsize}
\r
61 * @functionpage{IotMqtt_SerializeSubscribe,mqtt,serializesubscribe}
\r
62 * @functionpage{IotMqtt_SerializeUnsubscribe,mqtt,serializeunsubscribe}
\r
63 * @functionpage{IotMqtt_GetPublishPacketSize,mqtt,getpublishpacketsize}
\r
64 * @functionpage{IotMqtt_SerializePublish,mqtt,serializepublish}
\r
65 * @functionpage{IotMqtt_SerializeDisconnect,mqtt,serializedisconnect}
\r
66 * @functionpage{IotMqtt_SerializePingreq,mqtt,serializepingreq}
\r
67 * @functionpage{IotMqtt_GetIncomingMQTTPacketTypeAndLength,mqtt,getincomingmqttpackettypeandlength}
\r
68 * @functionpage{IotMqtt_DeserializeResponse,mqtt,deserializeresponse}
\r
69 * @functionpage{IotMqtt_DeserializePublish,mqtt,deserializepublish}
\r
73 * @brief Calculate the size and "Remaining length" of a CONNECT packet generated
\r
74 * from the given parameters.
\r
76 * @param[in] pConnectInfo User-provided CONNECT information struct.
\r
77 * @param[out] pRemainingLength Output for calculated "Remaining length" field.
\r
78 * @param[out] pPacketSize Output for calculated total packet size.
\r
80 * @return IOT_MQTT_SUCCESS if the packet is within the length allowed by MQTT 3.1.1 spec;
\r
81 * IOT_MQTT_BAD_PARAMETER otherwise. If this function returns `IOT_MQTT_BAD_PARAMETER`,
\r
82 * the output parameters should be ignored.
\r
84 * @note This call is part of serializer API used for implementing light-weight MQTT client.
\r
88 * // Example code below shows how IotMqtt_GetConnectPacketSize() should be used to calculate
\r
89 * // the size of connect request.
\r
91 * IotMqttConnectInfo_t xConnectInfo;
\r
92 * size_t xRemainingLength = 0;
\r
93 * size_t xPacketSize = 0;
\r
94 * IotMqttError_t xResult;
\r
96 * // start with everything set to zero
\r
97 * memset( ( void * ) &xConnectInfo, 0x00, sizeof( xConnectInfo ) );
\r
99 * // Initialize connection info, details are out of scope for this example.
\r
100 * _initializeConnectInfo( &xConnectInfo );
\r
101 * // Get size requirement for the connect packet
\r
102 * xResult = IotMqtt_GetConnectPacketSize( &xConnectInfo, &xRemainingLength, &xPacketSize );
\r
103 * IotMqtt_Assert( xResult == IOT_MQTT_SUCCESS );
\r
105 * // Application should allocate buffer with size == xPacketSize or use static buffer
\r
106 * // with size >= xPacketSize to serialize connect request.
\r
109 /* @[declare_mqtt_getconnectpacketsize] */
\r
110 IotMqttError_t IotMqtt_GetConnectPacketSize( const IotMqttConnectInfo_t * pConnectInfo,
\r
111 size_t * pRemainingLength,
\r
112 size_t * pPacketSize );
\r
113 /* @[declare_mqtt_getconnectpacketsize] */
\r
116 * @brief Generate a CONNECT packet from the given parameters.
\r
118 * @param[in] pConnectInfo User-provided CONNECT information.
\r
119 * @param[in] remainingLength remaining length of the packet to be serialized.
\r
120 * @param[in, out] pBuffer User provided buffer where the CONNECT packet is written.
\r
121 * @param[in] bufferSize Size of the buffer pointed to by pBuffer.
\r
123 * @return #IOT_MQTT_SUCCESS or #IOT_MQTT_NO_MEMORY.
\r
125 * @note pBuffer must be allocated by caller. Use @ref mqtt_function_getconnectpacketsize
\r
126 * to determine the required size.
\r
127 * @note This call is part of serializer API used for implementing light-weight MQTT client.
\r
131 * // Example code below shows how IotMqtt_SerializeConnect() should be used to serialize
\r
132 * // MQTT connect packet and send it to MQTT broker.
\r
133 * // Example uses static memory but dynamically allocated memory can be used as well.
\r
134 * // Get size requirement for the connect packet.
\r
136 * #define mqttexampleSHARED_BUFFER_SIZE 100
\r
137 * static ucSharedBuffer[mqttexampleSHARED_BUFFER_SIZE];
\r
138 * void sendConnectPacket( int xMQTTSocket )
\r
140 * IotMqttConnectInfo_t xConnectInfo;
\r
141 * size_t xRemainingLength = 0;
\r
142 * size_t xPacketSize = 0;
\r
143 * IotMqttError_t xResult;
\r
144 * size_t xSentBytes = 0;
\r
145 * // Get size requirement for MQTT connect packet.
\r
146 * xResult = IotMqtt_GetConnectPacketSize( &xConnectInfo, &xRemainingLength, &xPacketSize );
\r
147 * IotMqtt_Assert( xResult == IOT_MQTT_SUCCESS );
\r
148 * // Make sure the packet size is less than static buffer size
\r
149 * IotMqtt_Assert( xPacketSize < mqttexampleSHARED_BUFFER_SIZE );
\r
150 * // Serialize MQTT connect packet into provided buffer
\r
151 * xResult = IotMqtt_SerializeConnect( &xConnectInfo, xRemainingLength, ucSharedBuffer, xPacketSize );
\r
152 * IotMqtt_Assert( xResult == IOT_MQTT_SUCCESS );
\r
153 * // xMQTTSocket here is posix socket created and connected to MQTT broker outside of this function.
\r
154 * xSentBytes = send( xMQTTSocket, ( void * ) ucSharedBuffer, xPacketSize, 0 );
\r
155 * IotMqtt_Assert( xSentBytes == xPacketSize );
\r
159 /* @[declare_mqtt_serializeconnect] */
\r
160 IotMqttError_t IotMqtt_SerializeConnect( const IotMqttConnectInfo_t * pConnectInfo,
\r
161 size_t remainingLength,
\r
163 size_t bufferSize );
\r
164 /* @[declare_mqtt_serializeconnect] */
\r
167 * @brief Calculate the size and "Remaining length" of a SUBSCRIBE or UNSUBSCRIBE
\r
168 * packet generated from the given parameters.
\r
170 * @param[in] type Either IOT_MQTT_SUBSCRIBE or IOT_MQTT_UNSUBSCRIBE.
\r
171 * @param[in] pSubscriptionList User-provided array of subscriptions.
\r
172 * @param[in] subscriptionCount Size of `pSubscriptionList`.
\r
173 * @param[out] pRemainingLength Output for calculated "Remaining length" field.
\r
174 * @param[out] pPacketSize Output for calculated total packet size.
\r
176 * @return IOT_MQTT_SUCCESS if the packet is within the length allowed by MQTT 3.1.1 spec;
\r
177 * IOT_MQTT_BAD_PARAMETER otherwise. If this function returns IOT_MQTT_BAD_PARAMETER,
\r
178 * the output parameters should be ignored.
\r
180 * @note This call is part of serializer API used for implementing light-weight MQTT client.
\r
184 * // Example code below shows how IotMqtt_GetSubscriptionPacketSize() should be used to calculate
\r
185 * // the size of subscribe or unsubscribe request.
\r
187 * IotMqttError_t xResult;
\r
188 * IotMqttSubscription_t xMQTTSubscription[ 1 ];
\r
189 * size_t xRemainingLength = 0;
\r
190 * size_t xPacketSize = 0;
\r
192 * // Initialize Subscribe parameters. Details are out of scope for this example.
\r
193 * // It will involve setting QOS, topic filter and topic filter length.
\r
194 * _initializeSubscribe( xMQTTSubscription );
\r
196 * xResult = IotMqtt_GetSubscriptionPacketSize( IOT_MQTT_SUBSCRIBE,
\r
197 * xMQTTSubscription,
\r
198 * sizeof( xMQTTSubscription ) / sizeof( IotMqttSubscription_t ),
\r
199 * &xRemainingLength, &xPacketSize );
\r
200 * IotMqtt_Assert( xResult == IOT_MQTT_SUCCESS );
\r
202 * // Application should allocate buffer with size == xPacketSize or use static buffer
\r
203 * // with size >= xPacketSize to serialize connect request.
\r
206 /* @[declare_mqtt_getsubscriptionpacketsize] */
\r
207 IotMqttError_t IotMqtt_GetSubscriptionPacketSize( IotMqttOperationType_t type,
\r
208 const IotMqttSubscription_t * pSubscriptionList,
\r
209 size_t subscriptionCount,
\r
210 size_t * pRemainingLength,
\r
211 size_t * pPacketSize );
\r
212 /* @[declare_mqtt_getsubscriptionpacketsize] */
\r
215 * @brief Generate a SUBSCRIBE packet from the given parameters.
\r
217 * @param[in] pSubscriptionList User-provided array of subscriptions.
\r
218 * @param[in] subscriptionCount Size of `pSubscriptionList`.
\r
219 * @param[in] remainingLength remaining length of the packet to be serialized.
\r
220 * @param[out] pPacketIdentifier The packet identifier generated for this SUBSCRIBE.
\r
221 * @param[in, out] pBuffer User provide buffer where the SUBSCRIBE packet is written.
\r
222 * @param[in] bufferSize Size of the buffer pointed to by pBuffer.
\r
224 * @return #IOT_MQTT_SUCCESS or #IOT_MQTT_NO_MEMORY.
\r
226 * @note pBuffer must be allocated by caller.
\r
227 * @note This call is part of serializer API used for implementing light-weight MQTT client.
\r
230 * // Example code below shows how IotMqtt_SerializeSubscribe() should be used to serialize
\r
231 * // MQTT Subscribe packet and send it to MQTT broker.
\r
232 * // Example uses static memory, but dynamically allocated memory can be used as well.
\r
233 * // Get size requirement for the MQTT subscribe packet.
\r
235 * #define mqttexampleSHARED_BUFFER_SIZE 100
\r
236 * static ucSharedBuffer[mqttexampleSHARED_BUFFER_SIZE];
\r
237 * void sendSubscribePacket( int xMQTTSocket )
\r
239 * IotMqttSubscription_t xMQTTSubscription[ 1 ];
\r
240 * size_t xRemainingLength = 0;
\r
241 * size_t xPacketSize = 0;
\r
242 * IotMqttError_t xResult;
\r
243 * size_t xSentBytes = 0;
\r
245 * // Initialize Subscribe parameters. Details are out of scope for this example.
\r
246 * // It will involve setting QOS, topic filter and topic filter length.
\r
247 * _initializeSubscribe( xMQTTSubscription );
\r
248 * // Get size requirement for MQTT Subscribe packet.
\r
249 * xResult = IotMqtt_GetSubscriptionPacketSize( IOT_MQTT_SUBSCRIBE,
\r
250 * xMQTTSubscription,
\r
251 * sizeof( xMQTTSubscription ) / sizeof( IotMqttSubscription_t ),
\r
252 * &xRemainingLength, &xPacketSize );
\r
253 * IotMqtt_Assert( xResult == IOT_MQTT_SUCCESS );
\r
254 * // Make sure the packet size is less than static buffer size.
\r
255 * IotMqtt_Assert( xPacketSize < mqttexampleSHARED_BUFFER_SIZE );
\r
257 * // Serialize subscribe into statically allocated ucSharedBuffer.
\r
258 * xResult = IotMqtt_SerializeSubscribe( xMQTTSubscription,
\r
259 * sizeof( xMQTTSubscription ) / sizeof( IotMqttSubscription_t ),
\r
260 * xRemainingLength,
\r
261 * &usPacketIdentifier,
\r
264 * IotMqtt_Assert( xResult == IOT_MQTT_SUCCESS );
\r
265 * // xMQTTSocket here is posix socket created and connected to MQTT broker outside of this function.
\r
266 * xSentBytes = send( xMQTTSocket, ( void * ) ucSharedBuffer, xPacketSize, 0 );
\r
267 * IotMqtt_Assert( xSentBytes == xPacketSize );
\r
271 /* @[declare_mqtt_serializesubscribe] */
\r
272 IotMqttError_t IotMqtt_SerializeSubscribe( const IotMqttSubscription_t * pSubscriptionList,
\r
273 size_t subscriptionCount,
\r
274 size_t remainingLength,
\r
275 uint16_t * pPacketIdentifier,
\r
277 size_t bufferSize );
\r
278 /* @[declare_mqtt_serializesubscribe] */
\r
281 * @brief Generate a UNSUBSCRIBE packet from the given parameters.
\r
283 * @param[in] pSubscriptionList User-provided array of subscriptions to remove.
\r
284 * @param[in] subscriptionCount Size of `pSubscriptionList`.
\r
285 * @param[in] remainingLength remaining length of the packet to be serialized.
\r
286 * @param[out] pPacketIdentifier The packet identifier generated for this UNSUBSCRIBE.
\r
287 * @param[in, out] pBuffer User provide buffer where the UNSUBSCRIBE packet is written.
\r
288 * @param[in] bufferSize Size of the buffer pointed to by pBuffer.
\r
290 * @return #IOT_MQTT_SUCCESS or #IOT_MQTT_NO_MEMORY.
\r
292 * @note pBuffer must be allocated by caller.
\r
293 * @note This call is part of serializer API used for implementing light-weight MQTT client.
\r
297 * // Example code below shows how IotMqtt_SerializeUnsubscribe() should be used to serialize
\r
298 * // MQTT unsubscribe packet and send it to MQTT broker.
\r
299 * // Example uses static memory, but dynamically allocated memory can be used as well.
\r
300 * // Get size requirement for the Unsubscribe packet.
\r
302 * #define mqttexampleSHARED_BUFFER_SIZE 100
\r
303 * static ucSharedBuffer[mqttexampleSHARED_BUFFER_SIZE];
\r
304 * void sendUnsubscribePacket( int xMQTTSocket )
\r
306 * // Following example shows one topic example.
\r
307 * IotMqttSubscription_t xMQTTSubscription[ 1 ];
\r
308 * size_t xRemainingLength = 0;
\r
309 * size_t xPacketSize = 0;
\r
310 * IotMqttError_t xResult;
\r
311 * size_t xSentBytes = 0;
\r
312 * // Get size requirement for MQTT unsubscribe packet.
\r
313 * xResult = IotMqtt_GetSubscriptionPacketSize( IOT_MQTT_UNSUBSCRIBE,
\r
314 * xMQTTSubscription,
\r
315 * sizeof( xMQTTSubscription ) / sizeof( IotMqttSubscription_t ),
\r
316 * &xRemainingLength, &xPacketSize );
\r
317 * IotMqtt_Assert( xResult == IOT_MQTT_SUCCESS );
\r
318 * // Make sure the packet size is less than static buffer size.
\r
319 * IotMqtt_Assert( xPacketSize < mqttexampleSHARED_BUFFER_SIZE );
\r
320 * // Serialize subscribe into statically allocated ucSharedBuffer.
\r
321 * xResult = IotMqtt_SerializeUnsubscribe( xMQTTSubscription,
\r
322 * sizeof( xMQTTSubscription ) / sizeof( IotMqttSubscription_t ),
\r
323 * xRemainingLength,
\r
324 * &usPacketIdentifier,
\r
327 * IotMqtt_Assert( xResult == IOT_MQTT_SUCCESS );
\r
328 * // xMQTTSocket here is posix socket created and connected to MQTT broker outside of this function.
\r
329 * xSentBytes = send( xMQTTSocket, ( void * ) ucSharedBuffer, xPacketSize, 0 );
\r
330 * IotMqtt_Assert( xSentBytes == xPacketSize );
\r
334 /* @[declare_mqtt_serializeunsubscribe] */
\r
335 IotMqttError_t IotMqtt_SerializeUnsubscribe( const IotMqttSubscription_t * pSubscriptionList,
\r
336 size_t subscriptionCount,
\r
337 size_t remainingLength,
\r
338 uint16_t * pPacketIdentifier,
\r
340 size_t bufferSize );
\r
341 /* @[declare_mqtt_serializeunsubscribe] */
\r
344 * @brief Calculate the size and "Remaining length" of a PUBLISH packet generated
\r
345 * from the given parameters.
\r
347 * @param[in] pPublishInfo User-provided PUBLISH information struct.
\r
348 * @param[out] pRemainingLength Output for calculated "Remaining length" field.
\r
349 * @param[out] pPacketSize Output for calculated total packet size.
\r
351 * @return IOT_MQTT_SUCCESS if the packet is within the length allowed by MQTT 3.1.1 spec;
\r
352 * IOT_MQTT_BAD_PARAMETER otherwise. If this function returns IOT_MQTT_BAD_PARAMETER,
\r
353 * the output parameters should be ignored.
\r
355 * @note This call is part of serializer API used for implementing light-weight MQTT client.
\r
359 * // Example code below shows how IotMqtt_GetPublishPacketSize() should be used to calculate
\r
360 * // the size of MQTT publish request.
\r
362 * IotMqttError_t xResult;
\r
363 * IotMqttPublishInfo_t xMQTTPublishInfo;
\r
364 * size_t xRemainingLength = 0;
\r
365 * size_t xPacketSize = 0;
\r
367 * // Initialize Publish parameters. Details are out of scope for this example.
\r
368 * // It will involve setting QOS, topic filter, topic filter length, payload
\r
369 * // payload length
\r
370 * _initializePublish( &xMQTTPublishInfo );
\r
372 * // Find out length of Publish packet size.
\r
373 * xResult = IotMqtt_GetPublishPacketSize( &xMQTTPublishInfo, &xRemainingLength, &xPacketSize );
\r
374 * IotMqtt_Assert( xResult == IOT_MQTT_SUCCESS );
\r
376 * // Application should allocate buffer with size == xPacketSize or use static buffer
\r
377 * // with size >= xPacketSize to serialize connect request.
\r
380 /* @[declare_mqtt_getpublishpacketsize] */
\r
381 IotMqttError_t IotMqtt_GetPublishPacketSize( IotMqttPublishInfo_t * pPublishInfo,
\r
382 size_t * pRemainingLength,
\r
383 size_t * pPacketSize );
\r
384 /* @[declare_mqtt_getpublishpacketsize] */
\r
387 * @brief Generate a PUBLISH packet from the given parameters.
\r
389 * @param[in] pPublishInfo User-provided PUBLISH information.
\r
390 * @param[in] remainingLength remaining length of the packet to be serialized.
\r
391 * @param[out] pPacketIdentifier The packet identifier generated for this PUBLISH.
\r
392 * @param[out] pPacketIdentifierHigh Where the high byte of the packet identifier
\r
394 * @param[in, out] pBuffer User provide buffer where the PUBLISH packet is written.
\r
395 * @param[in] bufferSize Size of the buffer pointed to by pBuffer.
\r
397 * @return #IOT_MQTT_SUCCESS or #IOT_MQTT_BAD_PARAMETER.
\r
399 * @note pBuffer must be allocated by caller.
\r
400 * @note This call is part of serializer API used for implementing light-weight MQTT client.
\r
404 * // Example code below shows how IotMqtt_SerializePublish() should be used to serialize
\r
405 * // MQTT Publish packet and send it to broker.
\r
406 * // Example uses static memory, but dynamically allocated memory can be used as well.
\r
408 * #define mqttexampleSHARED_BUFFER_SIZE 100
\r
409 * static ucSharedBuffer[mqttexampleSHARED_BUFFER_SIZE];
\r
410 * void sendUnsubscribePacket( int xMQTTSocket )
\r
412 * IotMqttError_t xResult;
\r
413 * IotMqttPublishInfo_t xMQTTPublishInfo;
\r
414 * size_t xRemainingLength = 0;
\r
415 * size_t xPacketSize = 0;
\r
416 * size_t xSentBytes = 0;
\r
417 * uint16_t usPacketIdentifier;
\r
418 * uint8_t * pusPacketIdentifierHigh;
\r
420 * // Initialize Publish parameters. Details are out of scope for this example.
\r
421 * // It will involve setting QOS, topic filter, topic filter length, payload
\r
422 * // payload length.
\r
423 * _initializePublish( &xMQTTPublishInfo );
\r
425 * // Find out length of Publish packet size.
\r
426 * xResult = IotMqtt_GetPublishPacketSize( &xMQTTPublishInfo, &xRemainingLength, &xPacketSize );
\r
427 * IotMqtt_Assert( xResult == IOT_MQTT_SUCCESS );
\r
428 * // Make sure the packet size is less than static buffer size
\r
429 * IotMqtt_Assert( xPacketSize < mqttexampleSHARED_BUFFER_SIZE );
\r
431 * xResult = IotMqtt_SerializePublish( &xMQTTPublishInfo,
\r
432 * xRemainingLength,
\r
433 * &usPacketIdentifier,
\r
434 * &pusPacketIdentifierHigh,
\r
437 * IotMqtt_Assert( xResult == IOT_MQTT_SUCCESS );
\r
439 * // xMQTTSocket here is posix socket created and connected to MQTT broker outside of this function.
\r
440 * xSentBytes = send( xMQTTSocket, ( void * ) ucSharedBuffer, xPacketSize, 0 );
\r
441 * IotMqtt_Assert( xSentBytes == xPacketSize );
\r
445 /* @[declare_mqtt_serializepublish] */
\r
446 IotMqttError_t IotMqtt_SerializePublish( IotMqttPublishInfo_t * pPublishInfo,
\r
447 size_t remainingLength,
\r
448 uint16_t * pPacketIdentifier,
\r
449 uint8_t ** pPacketIdentifierHigh,
\r
451 size_t bufferSize );
\r
452 /* @[declare_mqtt_serializepublish] */
\r
455 * @brief Generate a DISCONNECT packet
\r
457 * @param[in, out] pBuffer User provide buffer where the DISCONNECT packet is written.
\r
458 * @param[in] bufferSize Size of the buffer pointed to by pBuffer.
\r
460 * @return returns #IOT_MQTT_SUCCESS or #IOT_MQTT_BAD_PARAMETER
\r
462 * @note This call is part of serializer API used for implementing light-weight MQTT client.
\r
466 * // Example below shows how IotMqtt_SerializeDisconnect() should be used.
\r
468 * #define mqttexampleSHARED_BUFFER_SIZE 100
\r
469 * static ucSharedBuffer[mqttexampleSHARED_BUFFER_SIZE];
\r
470 * void sendDisconnectRequest( int xMQTTSocket )
\r
472 * size_t xSentBytes = 0;
\r
474 * // Disconnect is fixed length packet, therefore there is no need to calculate the size,
\r
475 * // just makes sure static buffer can accommodate disconnect request.
\r
476 * IotMqtt_Assert( MQTT_PACKET_DISCONNECT_SIZE <= mqttexampleSHARED_BUFFER_SIZE );
\r
478 * // Serialize Disconnect packet into static buffer (dynamically allocated buffer can be used as well)
\r
479 * xResult = IotMqtt_SerializeDisconnect( ucSharedBuffer, MQTT_PACKET_DISCONNECT_SIZE );
\r
480 * IotMqtt_Assert( xResult == IOT_MQTT_SUCCESS );
\r
482 * // xMQTTSocket here is posix socket created and connected to MQTT broker outside of this function.
\r
483 * xSentByte = send( xMQTTSocket, ( void * ) ucSharedBuffer, MQTT_PACKET_DISCONNECT_SIZE, 0 );
\r
484 * IotMqtt_Assert( xSentByte == MQTT_PACKET_DISCONNECT_SIZE );
\r
489 /* @[declare_mqtt_serializedisconnect] */
\r
490 IotMqttError_t IotMqtt_SerializeDisconnect( uint8_t * pBuffer,
\r
491 size_t bufferSize );
\r
492 /* @[declare_mqtt_serializedisconnect] */
\r
495 * @brief Generate a PINGREQ packet.
\r
497 * @param[in, out] pBuffer User provide buffer where the PINGREQ packet is written.
\r
498 * @param[in] bufferSize Size of the buffer pointed to by pBuffer.
\r
500 * @return #IOT_MQTT_SUCCESS or #IOT_MQTT_BAD_PARAMETER.
\r
502 * @note This call is part of serializer API used for implementing light-weight MQTT client.
\r
506 * // Example below shows how IotMqtt_SerializePingReq() should be used.
\r
508 * #define mqttexampleSHARED_BUFFER_SIZE 100
\r
509 * static ucSharedBuffer[mqttexampleSHARED_BUFFER_SIZE];
\r
510 * void sendPingRequest( int xMQTTSocket )
\r
512 * size_t xSentBytes = 0;
\r
514 * // PingReq is fixed length packet, therefore there is no need to calculate the size,
\r
515 * // just makes sure static buffer can accommodate Ping request.
\r
516 * IotMqtt_Assert( MQTT_PACKET_PINGREQ_SIZE <= mqttexampleSHARED_BUFFER_SIZE );
\r
518 * xResult = IotMqtt_SerializePingreq( ucSharedBuffer, MQTT_PACKET_PINGREQ_SIZE );
\r
519 * IotMqtt_Assert( xResult == IOT_MQTT_SUCCESS );
\r
521 * // xMQTTSocket here is posix socket created and connected to MQTT broker outside of this function.
\r
522 * xSentByte = send( xMQTTSocket, ( void * ) ucSharedBuffer, MQTT_PACKET_DISCONNECT_SIZE, 0 );
\r
523 * IotMqtt_Assert( xSentByte == MQTT_PACKET_PINGREQ_SIZE);
\r
527 /* @[declare_mqtt_serializepingreq] */
\r
528 IotMqttError_t IotMqtt_SerializePingreq( uint8_t * pBuffer,
\r
529 size_t bufferSize );
\r
530 /* @[declare_mqtt_serializepingreq] */
\r
533 * @brief Extract MQTT packet type and length from incoming packet
\r
535 * @param[in, out] pIncomingPacket Pointer to IotMqttPacketInfo_t structure
\r
536 * where type, remaining length and packet identifier are stored.
\r
537 * @param[in] getNextByte Pointer to platform specific function which is used
\r
538 * to extract type and length from incoming received stream (see example ).
\r
539 * @param[in] pNetworkConnection Pointer to platform specific network connection
\r
540 * which is used by getNextByte to receive network data
\r
542 * @return #IOT_MQTT_SUCCESS on successful extraction of type and length,
\r
543 * #IOT_MQTT_BAD_RESPONSE on failure.
\r
545 * @note This call is part of serializer API used for implementing light-weight MQTT client.
\r
549 * // Example code below shows how to implement getNetxByte function with posix sockets.
\r
550 * // Note: IotMqttGetNextByte_t typedef IotMqttError_t (* IotMqttGetNextByte_t)( void * pNetworkContext,
\r
551 * // uint8_t * pNextByte );
\r
552 * // Note: It is assumed that socket is already created and connected,
\r
554 * IotMqttError_t getNextByte( void * pContext,
\r
555 * uint8_t * pNextByte )
\r
557 * int socket = ( int ) ( *pvContext );
\r
558 * int receivedBytes;
\r
559 * IotMqttError_t result;
\r
561 * receivedBytes = recv( socket, ( void * ) pNextByte, sizeof( uint8_t ), 0 );
\r
563 * if( receivedBytes == sizeof( uint8_t ) )
\r
565 * result = IOT_MQTT_SUCCESS;
\r
569 * result = IOT_MQTT_TIMEOUT;
\r
575 * // Example below shows how IotMqtt_GetIncomingMQTTPacketTypeAndLength() is used to extract type
\r
576 * // and length from incoming ping response.
\r
577 * // xMQTTSocket here is posix socket created and connected to MQTT broker outside of this function.
\r
578 * void getTypeAndLengthFromIncomingMQTTPingResponse( int xMQTTSocket )
\r
580 * IotMqttPacketInfo_t xIncomingPacket;
\r
581 * IotMqttError_t xResult = IotMqtt_GetIncomingMQTTPacketTypeAndLength( &xIncomingPacket, getNextByte, ( void * ) xMQTTSocket );
\r
582 * IotMqtt_Assert( xResult == IOT_MQTT_SUCCESS );
\r
583 * IotMqtt_Assert( xIncomingPacket.type == MQTT_PACKET_TYPE_PINGRESP );
\r
588 /* @[declare_mqtt_getincomingmqttpackettypeandlength] */
\r
589 IotMqttError_t IotMqtt_GetIncomingMQTTPacketTypeAndLength( IotMqttPacketInfo_t * pIncomingPacket,
\r
590 IotMqttGetNextByte_t getNextByte,
\r
591 void * pNetworkConnection );
\r
592 /* @[declare_mqtt_getincomingmqttpackettypeandlength] */
\r
595 * @brief Deserialize incoming publish packet.
\r
597 * @param[in, out] pMqttPacket The caller of this API sets type, remainingLength and pRemainingData.
\r
598 * On success, packetIdentifier and pubInfo will be set by the function.
\r
600 * @return One of the following:
\r
601 * - #IOT_MQTT_SUCCESS
\r
602 * - #IOT_MQTT_BAD_RESPONSE
\r
603 * - #IOT_MQTT_SERVER_REFUSED
\r
605 * @note This call is part of serializer API used for implementing light-weight MQTT client.
\r
609 * // Example below shows how IotMqtt_DeserializePublish() used to extract contents of incoming
\r
610 * // Publish. xMQTTSocket here is posix socket created and connected to MQTT broker outside of this function.
\r
611 * void processIncomingPublish( int xMQTTSocket )
\r
613 * IotMqttError_t xResult;
\r
614 * IotMqttPacketInfo_t xIncomingPacket;
\r
616 * xResult = IotMqtt_GetIncomingMQTTPacketTypeAndLength( &xIncomingPacket, getNextByte, ( void * ) xMQTTSocket );
\r
617 * IotMqtt_Assert( xResult == IOT_MQTT_SUCCESS );
\r
618 * IotMqtt_Assert( ( xIncomingPacket.type & 0xf0 ) == MQTT_PACKET_TYPE_PUBLISH );
\r
619 * IotMqtt_Assert( xIncomingPacket.remainingLength <= mqttexampleSHARED_BUFFER_SIZE );
\r
621 * // Receive the remaining bytes.
\r
622 * if( recv( xMQTTSocket, ( void * ) ucSharedBuffer, xIncomingPacket.remainingLength, 0 ) == xIncomingPacket.remainingLength )
\r
624 * xIncomingPacket.pRemainingData = ucSharedBuffer;
\r
626 * if( IotMqtt_DeserializePublish( &xIncomingPacket ) != IOT_MQTT_SUCCESS )
\r
628 * xResult = IOT_MQTT_BAD_RESPONSE;
\r
632 * // Process incoming Publish.
\r
633 * IotLogInfo( "Incoming QOS : %d\n", xIncomingPacket.pubInfo.qos );
\r
634 * IotLogInfo( "Incoming Publish Topic Name: %.*s\n", xIncomingPacket.pubInfo.topicNameLength, xIncomingPacket.pubInfo.pTopicName );
\r
635 * IotLogInfo( "Incoming Publish Message : %.*s\n", xIncomingPacket.pubInfo.payloadLength, xIncomingPacket.pubInfo.pPayload );
\r
640 * xResult = IOT_MQTT_NETWORK_ERROR;
\r
643 * IotMqtt_Assert( xResult == IOT_MQTT_SUCCESS );
\r
647 /* @[declare_mqtt_deserializepublish] */
\r
648 IotMqttError_t IotMqtt_DeserializePublish( IotMqttPacketInfo_t * pMqttPacket );
\r
649 /* @[declare_mqtt_deserializepublish] */
\r
652 * @brief Deserialize incoming ack packets.
\r
654 * @param[in, out] pMqttPacket The caller of this API sets type, remainingLength and pRemainingData.
\r
655 * On success, packetIdentifier will be set.
\r
657 * @return One of the following:
\r
658 * - #IOT_MQTT_SUCCESS
\r
659 * - #IOT_MQTT_BAD_RESPONSE
\r
660 * - #IOT_MQTT_SERVER_REFUSED
\r
662 * @note This call is part of serializer API used for implementing light-weight MQTT client.
\r
666 * // Example below shows how IotMqtt_DeserializeResponse() is used to process unsubscribe ack.
\r
667 * // xMQTTSocket here is posix socket created and connected to MQTT broker outside of this function.
\r
668 * void processUnsubscribeAck( int xMQTTSocket )
\r
670 * IotMqttError_t xResult;
\r
671 * IotMqttPacketInfo_t xIncomingPacket;
\r
673 * xResult = IotMqtt_GetIncomingMQTTPacketTypeAndLength( &xIncomingPacket, getNextByte, ( void * ) xMQTTSocket );
\r
674 * IotMqtt_Assert( xResult == IOT_MQTT_SUCCESS );
\r
675 * IotMqtt_Assert( xIncomingPacket.type == MQTT_PACKET_TYPE_UNSUBACK );
\r
676 * IotMqtt_Assert( xIncomingPacket.remainingLength <= sizeof( ucSharedBuffer ) );
\r
678 * // Receive the remaining bytes.
\r
679 * if( recv( xMQTTSocket, ( void * ) ucSharedBuffer, xIncomingPacket.remainingLength, 0 ) == xIncomingPacket.remainingLength )
\r
681 * xIncomingPacket.pRemainingData = ucSharedBuffer;
\r
683 * if( IotMqtt_DeserializeResponse( &xIncomingPacket ) != IOT_MQTT_SUCCESS )
\r
685 * xResult = IOT_MQTT_BAD_RESPONSE;
\r
690 * xResult = IOT_MQTT_NETWORK_ERROR;
\r
692 * IotMqtt_Assert( xResult == IOT_MQTT_SUCCESS );
\r
696 /* @[declare_mqtt_deserializeresponse] */
\r
697 IotMqttError_t IotMqtt_DeserializeResponse( IotMqttPacketInfo_t * pMqttPacket );
\r
698 /* @[declare_mqtt_deserializeresponse] */
\r
702 #endif /* ifndef IOT_MQTT_SERIALIZE_H_ */
\r