coreMQTT v5.0.0
MQTT 5.0 Client Library
 
Loading...
Searching...
No Matches
core_mqtt_serializer_private.h
Go to the documentation of this file.
1/*
2 * coreMQTT
3 * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
35#ifndef CORE_MQTT_SERIALIZER_PRIVATE_H
36#define CORE_MQTT_SERIALIZER_PRIVATE_H
37
38#include <stdint.h>
39
41
57#define MQTT_SUBSCRIPTION_ID_POS ( 1 )
58
63#define MQTT_SESSION_EXPIRY_INTERVAL_POS ( 2 )
64
69#define MQTT_RECEIVE_MAXIMUM_POS ( 3 )
70
75#define MQTT_MAX_PACKET_SIZE_POS ( 4 )
76
81#define MQTT_TOPIC_ALIAS_MAX_POS ( 5 )
82
87#define MQTT_REQUEST_RESPONSE_INFO_POS ( 6 )
88
93#define MQTT_REQUEST_PROBLEM_INFO_POS ( 7 )
94
99#define MQTT_AUTHENTICATION_METHOD_POS ( 9 )
100
105#define MQTT_AUTHENTICATION_DATA_POS ( 10 )
106
111#define MQTT_PAYLOAD_FORMAT_INDICATOR_POS ( 11 )
112
117#define MQTT_MESSAGE_EXPIRY_INTERVAL_POS ( 12 )
118
123#define MQTT_TOPIC_ALIAS_POS ( 13 )
124
129#define MQTT_RESPONSE_TOPIC_POS ( 14 )
130
135#define MQTT_CORRELATION_DATA_POS ( 15 )
136
141#define MQTT_CONTENT_TYPE_POS ( 16 )
142
147#define MQTT_REASON_STRING_POS ( 17 )
148
153#define MQTT_WILL_DELAY_POS ( 18 )
154
159#define MQTT_ASSIGNED_CLIENT_ID_POS ( 19 )
160
165#define MQTT_SERVER_KEEP_ALIVE_POS ( 20 )
166
171#define MQTT_RESPONSE_INFORMATION_POS ( 21 )
172
177#define MQTT_SERVER_REFERENCE_POS ( 22 )
178
183#define MQTT_MAX_QOS_POS ( 23 )
184
189#define MQTT_RETAIN_AVAILABLE_POS ( 24 )
190
195#define MQTT_WILDCARD_SUBSCRIPTION_AVAILABLE_POS ( 25 )
196
201#define MQTT_SUBSCRIPTION_ID_AVAILABLE_POS ( 26 )
202
207#define MQTT_SHARED_SUBSCRIPTION_AVAILABLE_POS ( 27 )
208
213#define MQTT_USER_PROP_POS ( 28 )
214
215/* MQTT CONNECT flags. */
216#define MQTT_CONNECT_FLAG_CLEAN ( 1 )
217#define MQTT_CONNECT_FLAG_WILL ( 2 )
218#define MQTT_CONNECT_FLAG_WILL_QOS1 ( 3 )
219#define MQTT_CONNECT_FLAG_WILL_QOS2 ( 4 )
220#define MQTT_CONNECT_FLAG_WILL_RETAIN ( 5 )
221#define MQTT_CONNECT_FLAG_PASSWORD ( 6 )
222#define MQTT_CONNECT_FLAG_USERNAME ( 7 )
229#define UINT32_DECODE( ptr ) \
230 ( uint32_t ) ( ( ( ( uint32_t ) ptr[ 0 ] ) << 24 ) | \
231 ( ( ( uint32_t ) ptr[ 1 ] ) << 16 ) | \
232 ( ( ( uint32_t ) ptr[ 2 ] ) << 8 ) | \
233 ( ( uint32_t ) ptr[ 3 ] ) )
234
239#define WRITE_UINT32( addr, val ) \
240 { \
241 ( addr )[ 3 ] = ( uint8_t ) ( ( ( val ) >> 0 ) & 0xFFU ); \
242 ( addr )[ 2 ] = ( uint8_t ) ( ( ( val ) >> 8 ) & 0xFFU ); \
243 ( addr )[ 1 ] = ( uint8_t ) ( ( ( val ) >> 16 ) & 0xFFU ); \
244 ( addr )[ 0 ] = ( uint8_t ) ( ( ( val ) >> 24 ) & 0xFFU ); \
245 }
246
250#define UINT8_SET_BIT( x, position ) ( ( x ) = ( uint8_t ) ( ( x ) | ( 0x01U << ( position ) ) ) )
251
255#define UINT8_CLEAR_BIT( x, position ) ( ( x ) = ( uint8_t ) ( ( x ) & ( ~( 0x01U << ( position ) ) ) ) )
256
263#define UINT8_CHECK_BIT( x, position ) ( ( ( x ) & ( 0x01U << ( position ) ) ) == ( 0x01U << ( position ) ) )
264
268#define UINT16_HIGH_BYTE( x ) ( ( uint8_t ) ( ( x ) >> 8 ) )
269
273#define UINT16_LOW_BYTE( x ) ( ( uint8_t ) ( ( x ) & 0x00ffU ) )
274
280#define UINT16_DECODE( ptr ) \
281 ( uint16_t ) ( ( ( ( uint16_t ) ptr[ 0 ] ) << 8 ) | \
282 ( ( uint16_t ) ptr[ 1 ] ) )
283
287#define UINT32_SET_BIT( x, position ) \
288 ( ( x ) = ( uint32_t ) ( ( x ) | ( ( uint32_t ) 0x01U << ( position ) ) ) )
289
296#define UINT32_CHECK_BIT( x, position ) \
297 ( ( ( uint32_t ) ( x ) & ( ( uint32_t ) 0x01U << ( position ) ) ) == ( ( uint32_t ) 0x01U << ( position ) ) )
298
303#define MQTT_MAX_REMAINING_LENGTH ( 268435455U )
304
310#define MQTT_REMAINING_LENGTH_INVALID ( ( uint32_t ) MQTT_MAX_REMAINING_LENGTH + 1U )
311
318#define MQTT_MAX_PACKET_SIZE ( MQTT_MAX_REMAINING_LENGTH + 5U )
319
323#define MQTT_MAX_UTF8_STR_LENGTH ( ( uint32_t ) 65535 )
324
329#define MAX_VARIABLE_LENGTH_INT_VALUE ( ( uint32_t ) 268435455U )
330
337#if ( SIZE_MAX >= UINT32_MAX )
338 #define CHECK_U32T_OVERFLOWS_SIZE_T( x ) false
339#else
340 #define CHECK_U32T_OVERFLOWS_SIZE_T( x ) ( ( x ) > SIZE_MAX )
341#endif
342
349#if ( 65535U >= SIZE_MAX )
350 #define CHECK_SIZE_T_OVERFLOWS_16BIT( x ) false
351#else
352 #define CHECK_SIZE_T_OVERFLOWS_16BIT( x ) ( ( x ) > 65535U )
353#endif
354
361#if ( 0xFFFFFFFFU >= SIZE_MAX )
362 #define CHECK_SIZE_T_OVERFLOWS_32BIT( x ) false
363#else
364 #define CHECK_SIZE_T_OVERFLOWS_32BIT( x ) ( ( x ) > 0xFFFFFFFFU )
365#endif
366
372#define ADDITION_WILL_OVERFLOW_U32( x, y ) \
373 ( ( x ) > ( UINT32_MAX - ( y ) ) )
374
380#define ADDITION_WILL_OVERFLOW_SIZE_T( x, y ) \
381 ( ( x ) > ( SIZE_MAX - ( y ) ) )
382
399uint32_t variableLengthEncodedSize( uint32_t length );
418uint8_t * encodeString( uint8_t * pDestination,
419 const char * pSource,
420 uint16_t sourceLength );
442MQTTStatus_t decodeUserProp( const char ** pPropertyKey,
443 size_t * pPropertyKeyLen,
444 const char ** pPropertyValue,
445 size_t * pPropertyValueLen,
446 uint32_t * pPropertyLength,
447 uint8_t ** pIndex );
467MQTTStatus_t decodeUint32t( uint32_t * pProperty,
468 uint32_t * pPropertyLength,
469 bool * pUsed,
470 uint8_t ** pIndex );
490MQTTStatus_t decodeUint16t( uint16_t * pProperty,
491 uint32_t * pPropertyLength,
492 bool * pUsed,
493 uint8_t ** pIndex );
513MQTTStatus_t decodeUint8t( uint8_t * pProperty,
514 uint32_t * pPropertyLength,
515 bool * pUsed,
516 uint8_t ** pIndex );
536uint8_t * encodeVariableLength( uint8_t * pDestination,
537 uint32_t length );
558MQTTStatus_t decodeUtf8( const char ** pProperty,
559 size_t * pLength,
560 uint32_t * pPropertyLength,
561 bool * pUsed,
562 uint8_t ** pIndex );
583MQTTStatus_t decodeVariableLength( const uint8_t * pBuffer,
584 size_t bufferLength,
585 uint32_t * pLength );
607uint8_t * serializeAckFixed( uint8_t * pIndex,
608 uint8_t packetType,
609 uint16_t packetId,
610 uint32_t remainingLength,
611 MQTTSuccessFailReasonCode_t reasonCode );
631MQTTStatus_t decodeSubackPropertyLength( const uint8_t * pIndex,
632 uint32_t remainingLength,
633 uint32_t * subackPropertyLength );
652uint8_t * serializeDisconnectFixed( uint8_t * pIndex,
653 const MQTTSuccessFailReasonCode_t * pReasonCode,
654 uint32_t remainingLength );
675uint8_t * serializeConnectFixedHeader( uint8_t * pIndex,
676 const MQTTConnectInfo_t * pConnectInfo,
677 const MQTTPublishInfo_t * pWillInfo,
678 uint32_t remainingLength );
698uint8_t * serializeSubscribeHeader( uint32_t remainingLength,
699 uint8_t * pIndex,
700 uint16_t packetId );
720uint8_t * serializeUnsubscribeHeader( uint32_t remainingLength,
721 uint8_t * pIndex,
722 uint16_t packetId );
725#endif /* ifndef CORE_MQTT_SERIALIZER_PRIVATE_H */
User-facing functions for serializing and deserializing MQTT 5.0 packets. This header should be inclu...
uint8_t * encodeVariableLength(uint8_t *pDestination, uint32_t length)
Encodes the remaining length of the packet using the variable length encoding scheme provided in the ...
Definition: core_mqtt_serializer_private.c:415
uint8_t * serializeUnsubscribeHeader(uint32_t remainingLength, uint8_t *pIndex, uint16_t packetId)
Serialize the fixed part of the unsubscribe packet header.
Definition: core_mqtt_serializer_private.c:580
MQTTStatus_t decodeUint32t(uint32_t *pProperty, uint32_t *pPropertyLength, bool *pUsed, uint8_t **pIndex)
Validate the length and decode a 4 byte value.
Definition: core_mqtt_serializer_private.c:164
MQTTStatus_t decodeUtf8(const char **pProperty, size_t *pLength, uint32_t *pPropertyLength, bool *pUsed, uint8_t **pIndex)
Validate the length and decode a utf 8 string.
Definition: core_mqtt_serializer_private.c:287
MQTTStatus_t decodeUint16t(uint16_t *pProperty, uint32_t *pPropertyLength, bool *pUsed, uint8_t **pIndex)
Validate the length and decode a 2 byte value.
Definition: core_mqtt_serializer_private.c:205
uint32_t variableLengthEncodedSize(uint32_t length)
Retrieve the size of the remaining length if it were to be encoded.
Definition: core_mqtt_serializer_private.c:52
uint8_t * serializeSubscribeHeader(uint32_t remainingLength, uint8_t *pIndex, uint16_t packetId)
Serialize the fixed part of the subscribe packet header.
Definition: core_mqtt_serializer_private.c:556
MQTTStatus_t decodeUint8t(uint8_t *pProperty, uint32_t *pPropertyLength, bool *pUsed, uint8_t **pIndex)
Validate the length and decode a 1 byte value.
Definition: core_mqtt_serializer_private.c:246
uint8_t * serializeDisconnectFixed(uint8_t *pIndex, const MQTTSuccessFailReasonCode_t *pReasonCode, uint32_t remainingLength)
Serialize the fixed size part of the disconnect packet header.
Definition: core_mqtt_serializer_private.c:604
uint8_t * encodeString(uint8_t *pDestination, const char *pSource, uint16_t sourceLength)
Encode a string whose size is at maximum 16 bits in length.
Definition: core_mqtt_serializer_private.c:91
uint8_t * serializeAckFixed(uint8_t *pIndex, uint8_t packetType, uint16_t packetId, uint32_t remainingLength, MQTTSuccessFailReasonCode_t reasonCode)
Serialize the fixed size part of the ack packet header.
Definition: core_mqtt_serializer_private.c:449
uint8_t * serializeConnectFixedHeader(uint8_t *pIndex, const MQTTConnectInfo_t *pConnectInfo, const MQTTPublishInfo_t *pWillInfo, uint32_t remainingLength)
Serialize the fixed part of the connect packet header.
Definition: core_mqtt_serializer_private.c:474
MQTTStatus_t decodeVariableLength(const uint8_t *pBuffer, size_t bufferLength, uint32_t *pLength)
Decodes the variable length by reading a single byte at a time.
Definition: core_mqtt_serializer_private.c:341
MQTTStatus_t decodeSubackPropertyLength(const uint8_t *pIndex, uint32_t remainingLength, uint32_t *subackPropertyLength)
Decodes the property length field in a SUBACK packet.
Definition: core_mqtt_serializer_private.c:630
MQTTStatus_t decodeUserProp(const char **pPropertyKey, size_t *pPropertyKeyLen, const char **pPropertyValue, size_t *pPropertyValueLen, uint32_t *pPropertyLength, uint8_t **pIndex)
Validate the length and decode a user property.
Definition: core_mqtt_serializer_private.c:123
MQTTSuccessFailReasonCode_t
MQTT reason codes.
Definition: core_mqtt_serializer.h:583
MQTTStatus_t
Return codes from MQTT functions.
Definition: core_mqtt_serializer.h:239
MQTT CONNECT packet parameters.
Definition: core_mqtt_serializer.h:294
MQTT PUBLISH packet parameters.
Definition: core_mqtt_serializer.h:395