AWS IoT Device SDK C: MQTT
MQTT 3.1.1 client library
Return to main page ↑
iot_mqtt_helper.c File Reference

Implements helper functions for the MQTT library. More...

#include "iot_config.h"
#include <string.h>
#include <limits.h>
#include <assert.h>
#include <stdlib.h>
#include "iot_atomic.h"
#include "private/iot_mqtt_helper.h"
#include "iot_logging_setup.h"

Macros

#define IotMqtt_Assert(expression)
 Assertion macro for the MQTT library. More...
 
#define AWS_IOT_METRICS_USERNAME   "?SDK=C&Version=4.0.0"
 Specify C SDK and version.
 
#define AWS_IOT_METRICS_USERNAME_LENGTH   ( ( uint16_t ) sizeof( AWS_IOT_METRICS_USERNAME ) - 1U )
 The length of AWS_IOT_METRICS_USERNAME.
 

Functions

static uint8_t * _encodeUserNameAndMetrics (uint8_t *pDestination, const IotMqttConnectInfo_t *pConnectInfo, bool *pEncodedUserName)
 Encode both connection and metrics username into a buffer, if they will fit. More...
 
static uint8_t * _encodeUserName (uint8_t *pDestination, const IotMqttConnectInfo_t *pConnectInfo)
 Encode a username into a CONNECT packet, if necessary. More...
 
static uint8_t * _encodeString (uint8_t *pDestination, const char *source, uint16_t sourceLength)
 Encode a C string as a UTF-8 string, per MQTT 3.1.1 spec. More...
 
static uint8_t * _encodeRemainingLength (uint8_t *pDestination, size_t length)
 Encode the "Remaining length" field per MQTT spec. More...
 
size_t _IotMqtt_RemainingLengthEncodedSize (size_t length)
 Calculate the number of bytes required to encode an MQTT "Remaining length" field. More...
 
uint16_t _IotMqtt_NextPacketIdentifier (void)
 Generate and return a 2-byte packet identifier. More...
 
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. More...
 
void _IotMqtt_SerializeConnectCommon (const IotMqttConnectInfo_t *pConnectInfo, size_t remainingLength, uint8_t *pPacket, size_t connectPacketSize)
 Generate a CONNECT packet from the given parameters. More...
 
bool _IotMqtt_IncomingPacketValid (uint8_t packetType)
 Check if an incoming packet type is valid. More...
 
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. More...
 
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 given parameters. More...
 
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. More...
 
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. More...
 
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. More...
 
IotMqttError_t _IotMqtt_ProcessPublishFlags (uint8_t publishFlags, IotMqttPublishInfo_t *pOutput)
 Process incoming publish flags. More...
 

Detailed Description

Implements helper functions for the MQTT library.

Implements internal helper functions for the MQTT library.

Macro Definition Documentation

◆ IotMqtt_Assert

#define IotMqtt_Assert (   expression)

Assertion macro for the MQTT library.

Set IOT_MQTT_ENABLE_ASSERTS to 1 to enable assertions in the MQTT library.

Parameters
[in]expressionExpression to be evaluated.

Function Documentation

◆ _encodeUserNameAndMetrics()

static uint8_t * _encodeUserNameAndMetrics ( uint8_t *  pDestination,
const IotMqttConnectInfo_t pConnectInfo,
bool *  pEncodedUserName 
)
static

Encode both connection and metrics username into a buffer, if they will fit.

Parameters
[in]pDestinationBuffer to write username into.
[in]pConnectInfoUser-provided CONNECT information.
[out]pEncodedUserNameWhether the username was written into the buffer.
Returns
Pointer to the end of encoded string, which will be identical to pDestination if nothing was encoded.
Warning
This function does not check the size of pDestination! Ensure that pDestination is large enough to hold pConnectInfo->userNameLength + AWS_IOT_METRICS_USERNAME_LENGTH bytes to avoid a buffer overflow.

◆ _encodeUserName()

static uint8_t * _encodeUserName ( uint8_t *  pDestination,
const IotMqttConnectInfo_t pConnectInfo 
)
static

Encode a username into a CONNECT packet, if necessary.

Parameters
[out]pDestinationBuffer for the CONNECT packet.
[in]pConnectInfoUser-provided CONNECT information.
Returns
Pointer to the end of the encoded string, which will be identical to pDestination if nothing was encoded.
Warning
This function does not check the size of pDestination! To avoid a buffer overflow, ensure that pDestination is large enough to hold pConnectInfo->userNameLength bytes if a username is supplied, and/or AWS_IOT_METRICS_USERNAME_LENGTH bytes if metrics are enabled.

◆ _encodeString()

static uint8_t * _encodeString ( uint8_t *  pDestination,
const char *  source,
uint16_t  sourceLength 
)
static

Encode a C string as a UTF-8 string, per MQTT 3.1.1 spec.

Parameters
[out]pDestinationWhere to write the encoded string.
[in]sourceThe string to encode.
[in]sourceLengthThe length of source.
Returns
Pointer to the end of the encoded string, which is sourceLength+2 bytes greater than pDestination.
Warning
This function does not check the size of pDestination! Ensure that pDestination is large enough to hold sourceLength+2 bytes to avoid a buffer overflow.

◆ _encodeRemainingLength()

static uint8_t * _encodeRemainingLength ( uint8_t *  pDestination,
size_t  length 
)
static

Encode the "Remaining length" field per MQTT spec.

Parameters
[out]pDestinationWhere to write the encoded "Remaining length".
[in]lengthThe "Remaining length" to encode.
Returns
Pointer to the end of the encoded "Remaining length", which is 1-4 bytes greater than pDestination.
Warning
This function does not check the size of pDestination! Ensure that pDestination is large enough to hold the encoded "Remaining length" using the function _IotMqtt_RemainingLengthEncodedSize to avoid buffer overflows.

◆ _IotMqtt_RemainingLengthEncodedSize()

size_t _IotMqtt_RemainingLengthEncodedSize ( size_t  length)

Calculate the number of bytes required to encode an MQTT "Remaining length" field.

Parameters
[in]lengthThe value of the "Remaining length" to encode.
Returns
The size of the encoding of length. This is always 1, 2, 3, or 4.

◆ _IotMqtt_NextPacketIdentifier()

uint16_t _IotMqtt_NextPacketIdentifier ( void  )

Generate and return a 2-byte packet identifier.

This packet identifier will be nonzero.

Returns
The packet identifier.

◆ _IotMqtt_ConnectPacketSize()

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.

Parameters
[in]pConnectInfoUser-provided CONNECT information struct.
[out]pRemainingLengthOutput for calculated "Remaining length" field.
[out]pPacketSizeOutput for calculated total packet size.
Returns
true if the packet is within the length allowed by MQTT 3.1.1 spec; false otherwise. If this function returns false, the output parameters should be ignored.

◆ _IotMqtt_SerializeConnectCommon()

void _IotMqtt_SerializeConnectCommon ( const IotMqttConnectInfo_t pConnectInfo,
size_t  remainingLength,
uint8_t *  pPacket,
size_t  connectPacketSize 
)

Generate a CONNECT packet from the given parameters.

Parameters
[in]pConnectInfoUser-provided CONNECT information.
[in]remainingLengthUser provided remaining length.
[in,out]pPacketUser provided buffer where the CONNECT packet is written.
[in]connectPacketSizeSize of the buffer pointed to by pPacket.

◆ _IotMqtt_IncomingPacketValid()

bool _IotMqtt_IncomingPacketValid ( uint8_t  packetType)

Check if an incoming packet type is valid.

Parameters
[in]packetTypeThe packet type to check.
Returns
true if the packet type is valid; false otherwise.

◆ _IotMqtt_SerializeSubscribeCommon()

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.

Parameters
[in]pSubscriptionListUser-provided array of subscriptions.
[in]subscriptionCountSize of pSubscriptionList.
[in]remainingLengthUser provided remaining length.
[out]pPacketIdentifierThe packet identifier generated for this SUBSCRIBE.
[in,out]pPacketUser provided buffer where the SUBSCRIBE packet is written.
[in]subscribePacketSizeSize of the buffer pointed to by pPacket.

◆ _IotMqtt_SubscriptionPacketSize()

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 given parameters.

Parameters
[in]typeEither IOT_MQTT_SUBSCRIBE or IOT_MQTT_UNSUBSCRIBE.
[in]pSubscriptionListUser-provided array of subscriptions.
[in]subscriptionCountSize of pSubscriptionList.
[out]pRemainingLengthOutput for calculated "Remaining length" field.
[out]pPacketSizeOutput for calculated total packet size.
Returns
true if the packet is within the length allowed by MQTT 3.1.1 spec; false otherwise. If this function returns false, the output parameters should be ignored.

◆ _IotMqtt_PublishPacketSize()

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.

Parameters
[in]pPublishInfoUser-provided PUBLISH information struct.
[out]pRemainingLengthOutput for calculated "Remaining length" field.
[out]pPacketSizeOutput for calculated total packet size.
Returns
true if the packet is within the length allowed by MQTT 3.1.1 spec; false otherwise. If this function returns false, the output parameters should be ignored.

◆ _IotMqtt_SerializePublishCommon()

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.

Parameters
[in]pPublishInfoUser-provided PUBLISH information.
[in]remainingLengthUser provided remaining length.
[out]pPacketIdentifierThe packet identifier generated for this PUBLISH.
[out]pPacketIdentifierHighWhere the high byte of the packet identifier is written.
[in,out]pPacketUser provided buffer where the PUBLISH packet is written.
[in]publishPacketSizeSize of buffer pointed to by pPacket.

◆ _IotMqtt_SerializeUnsubscribeCommon()

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.

Parameters
[in]pSubscriptionListUser-provided array of subscriptions to remove.
[in]subscriptionCountSize of pSubscriptionList.
[in]remainingLengthUser provided remaining length.
[out]pPacketIdentifierThe packet identifier generated for this UNSUBSCRIBE.
[in,out]pPacketUser provided buffer where the UNSUBSCRIBE packet is written.
[in]unsubscribePacketSizesize of the buffer pointed to by pPacket.

◆ _IotMqtt_ProcessPublishFlags()

IotMqttError_t _IotMqtt_ProcessPublishFlags ( uint8_t  publishFlags,
IotMqttPublishInfo_t pOutput 
)

Process incoming publish flags.

Parameters
[in]publishFlagsIncoming publish flags.
[in,out]pOutputPointer to IotMqttPublishInfo_t struct. where output will be written.
Returns
IOT_MQTT_SUCCESS, IOT_MQTT_BAD_RESPONSE.