AWS IoT Device SDK C: MQTT
MQTT 3.1.1 client library
Return to main page ↑
iot_mqtt_helper.h
1 /*
2  * IoT MQTT V2.1.0
3  * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
28 #ifndef IOT_MQTT_HELPER_H_
29 #define IOT_MQTT_HELPER_H_
30 
31 /* Standard includes. */
32 #include <string.h>
33 #include <limits.h>
34 /* Default assert and memory allocation functions. */
35 #include <assert.h>
36 #include <stdlib.h>
37 
38 /* MQTT Protocol Specific defines */
39 #include "iot_mqtt_protocol.h"
40 /* MQTT types include. */
41 #include "types/iot_mqtt_types.h"
42 
43 /*-----------------------------------------------------------*/
44 
45 /*
46  * Macros for reading the high and low byte of a 2-byte unsigned int.
47  */
48 #define UINT16_HIGH_BYTE( x ) ( ( uint8_t ) ( ( x ) >> 8 ) )
49 #define UINT16_LOW_BYTE( x ) ( ( uint8_t ) ( ( x ) & 0x00ffU ) )
56 #define UINT16_DECODE( ptr ) \
57  ( uint16_t ) ( ( ( ( uint16_t ) ( *( ptr ) ) ) << 8 ) | \
58  ( ( uint16_t ) ( *( ( ptr ) + 1 ) ) ) )
59 
66 #define UINT8_SET_BIT( x, position ) ( ( x ) = ( uint8_t ) ( ( x ) | ( 0x01U << ( position ) ) ) )
67 
74 #define UINT8_CHECK_BIT( x, position ) ( ( ( x ) &( 0x01U << ( position ) ) ) == ( 0x01U << ( position ) ) )
75 
82 #ifndef AWS_IOT_MQTT_ENABLE_METRICS
83  #define AWS_IOT_MQTT_ENABLE_METRICS ( 1 )
84 #endif
85 
87 /*-----------------------------------------------------------*/
88 
97 size_t _IotMqtt_RemainingLengthEncodedSize( size_t length );
98 
110 bool _IotMqtt_ConnectPacketSize( const IotMqttConnectInfo_t * pConnectInfo,
111  size_t * pRemainingLength,
112  size_t * pPacketSize );
113 
114 
124 void _IotMqtt_SerializeConnectCommon( const IotMqttConnectInfo_t * pConnectInfo,
125  size_t remainingLength,
126  uint8_t * pPacket,
127  size_t connectPacketSize );
128 
143  const IotMqttSubscription_t * pSubscriptionList,
144  size_t subscriptionCount,
145  size_t * pRemainingLength,
146  size_t * pPacketSize );
147 
159 void _IotMqtt_SerializeSubscribeCommon( const IotMqttSubscription_t * pSubscriptionList,
160  size_t subscriptionCount,
161  size_t remainingLength,
162  uint16_t * pPacketIdentifier,
163  uint8_t * pPacket,
164  size_t subscribePacketSize );
165 
177 void _IotMqtt_SerializeUnsubscribeCommon( const IotMqttSubscription_t * pSubscriptionList,
178  size_t subscriptionCount,
179  size_t remainingLength,
180  uint16_t * pPacketIdentifier,
181  uint8_t * pPacket,
182  size_t unsubscribePacketSize );
183 
195 bool _IotMqtt_PublishPacketSize( const IotMqttPublishInfo_t * pPublishInfo,
196  size_t * pRemainingLength,
197  size_t * pPacketSize );
198 
211 void _IotMqtt_SerializePublishCommon( const IotMqttPublishInfo_t * pPublishInfo,
212  size_t remainingLength,
213  uint16_t * pPacketIdentifier,
214  uint8_t ** pPacketIdentifierHigh,
215  uint8_t * pPacket,
216  size_t publishPacketSize );
217 
225 bool _IotMqtt_IncomingPacketValid( uint8_t packetType );
226 
234 uint16_t _IotMqtt_NextPacketIdentifier( void );
235 
246 IotMqttError_t _IotMqtt_ProcessPublishFlags( uint8_t publishFlags,
247  IotMqttPublishInfo_t * pOutput );
248 
249 #endif /* ifndef IOT_MQTT_HELPER_H_ */
IotMqttError_t
Return codes of MQTT functions.
Definition: iot_mqtt_types.h:106
This file contains MQTT 3.1.1 specific defines. This is a common header to be included for building a...
IotMqttError_t _IotMqtt_ProcessPublishFlags(uint8_t publishFlags, IotMqttPublishInfo_t *pOutput)
Process incoming publish flags.
Definition: iot_mqtt_helper.c:918
size_t _IotMqtt_RemainingLengthEncodedSize(size_t length)
Calculate the number of bytes required to encode an MQTT "Remaining length" field.
Definition: iot_mqtt_helper.c:336
uint16_t _IotMqtt_NextPacketIdentifier(void)
Generate and return a 2-byte packet identifier.
Definition: iot_mqtt_helper.c:372
MQTT library types.
void _IotMqtt_SerializeUnsubscribeCommon(const IotMqttSubscription_t *pSubscriptionList, size_t subscriptionCount, size_t remainingLength, uint16_t *pPacketIdentifier, uint8_t *pPacket, size_t unsubscribePacketSize)
Generate an UNSUBSCRIBE packet from the given parameters.
Definition: iot_mqtt_helper.c:868
bool _IotMqtt_IncomingPacketValid(uint8_t packetType)
Check if an incoming packet type is valid.
Definition: iot_mqtt_helper.c:588
bool _IotMqtt_ConnectPacketSize(const IotMqttConnectInfo_t *pConnectInfo, size_t *pRemainingLength, size_t *pPacketSize)
Calculate the size and "Remaining length" of a CONNECT packet generated from the given parameters...
Definition: iot_mqtt_helper.c:386
Information on a PUBLISH message.
Definition: iot_mqtt_types.h:395
MQTT subscription.
Definition: iot_mqtt_types.h:553
void _IotMqtt_SerializeConnectCommon(const IotMqttConnectInfo_t *pConnectInfo, size_t remainingLength, uint8_t *pPacket, size_t connectPacketSize)
Generate a CONNECT packet from the given parameters.
Definition: iot_mqtt_helper.c:456
MQTT connection details.
Definition: iot_mqtt_types.h:589
bool _IotMqtt_PublishPacketSize(const IotMqttPublishInfo_t *pPublishInfo, size_t *pRemainingLength, size_t *pPacketSize)
Calculate the size and "Remaining length" of a PUBLISH packet generated from the given parameters...
Definition: iot_mqtt_helper.c:722
void _IotMqtt_SerializeSubscribeCommon(const IotMqttSubscription_t *pSubscriptionList, size_t subscriptionCount, size_t remainingLength, uint16_t *pPacketIdentifier, uint8_t *pPacket, size_t subscribePacketSize)
Generate a SUBSCRIBE packet from the given parameters.
Definition: iot_mqtt_helper.c:615
bool _IotMqtt_SubscriptionPacketSize(IotMqttOperationType_t type, const IotMqttSubscription_t *pSubscriptionList, size_t subscriptionCount, size_t *pRemainingLength, size_t *pPacketSize)
Calculate the size and "Remaining length" of a SUBSCRIBE or UNSUBSCRIBE packet generated from the giv...
Definition: iot_mqtt_helper.c:669
IotMqttOperationType_t
Types of MQTT operations.
Definition: iot_mqtt_types.h:280
void _IotMqtt_SerializePublishCommon(const IotMqttPublishInfo_t *pPublishInfo, size_t remainingLength, uint16_t *pPacketIdentifier, uint8_t **pPacketIdentifierHigh, uint8_t *pPacket, size_t publishPacketSize)
Generate a PUBLISH packet from the given parameters.
Definition: iot_mqtt_helper.c:780