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

Implements functions that generate and decode MQTT network packets. More...

#include "iot_config.h"
#include <string.h>
#include <limits.h>
#include "private/iot_mqtt_internal.h"
#include "private/iot_mqtt_helper.h"
#include "platform/iot_threads.h"
#include "iot_atomic.h"

Functions

static IotMqttError_t _decodeSubackStatus (size_t statusCount, const uint8_t *pStatusStart, const _mqttPacket_t *pSuback)
 Decode the status bytes of a SUBACK packet. More...
 
static IotMqttError_t _checkRemainingLength (const _mqttPacket_t *pPublish, IotMqttQos_t qos, size_t qos0Minimum)
 Check the remaining length against some value for QoS 0 or QoS 1/2. More...
 
uint8_t _IotMqtt_GetPacketType (IotNetworkConnection_t pNetworkConnection, const IotNetworkInterface_t *pNetworkInterface)
 Get the MQTT packet type from a stream of bytes off the network. More...
 
size_t _IotMqtt_GetRemainingLength (IotNetworkConnection_t pNetworkConnection, const IotNetworkInterface_t *pNetworkInterface)
 Get the remaining length from a stream of bytes off the network. More...
 
IotMqttError_t _IotMqtt_SerializeConnect (const IotMqttConnectInfo_t *pConnectInfo, uint8_t **pConnectPacket, size_t *pPacketSize)
 Generate a CONNECT packet from the given parameters. More...
 
IotMqttError_t _IotMqtt_DeserializeConnack (_mqttPacket_t *pConnack)
 Deserialize a CONNACK packet. More...
 
IotMqttError_t _IotMqtt_SerializePublish (const IotMqttPublishInfo_t *pPublishInfo, uint8_t **pPublishPacket, size_t *pPacketSize, uint16_t *pPacketIdentifier, uint8_t **pPacketIdentifierHigh)
 Generate a PUBLISH packet from the given parameters. More...
 
void _IotMqtt_PublishSetDup (uint8_t *pPublishPacket, uint8_t *pPacketIdentifierHigh, uint16_t *pNewPacketIdentifier)
 Set the DUP bit in a QoS 1 PUBLISH packet. More...
 
IotMqttError_t _IotMqtt_DeserializePublish (_mqttPacket_t *pPublish)
 Deserialize a PUBLISH packet received from the server. More...
 
IotMqttError_t _IotMqtt_SerializePuback (uint16_t packetIdentifier, uint8_t **pPubackPacket, size_t *pPacketSize)
 Generate a PUBACK packet for the given packet identifier. More...
 
IotMqttError_t _IotMqtt_DeserializePuback (_mqttPacket_t *pPuback)
 Deserialize a PUBACK packet. More...
 
IotMqttError_t _IotMqtt_SerializeSubscribe (const IotMqttSubscription_t *pSubscriptionList, size_t subscriptionCount, uint8_t **pSubscribePacket, size_t *pPacketSize, uint16_t *pPacketIdentifier)
 Generate a SUBSCRIBE packet from the given parameters. More...
 
IotMqttError_t _IotMqtt_DeserializeSuback (_mqttPacket_t *pSuback)
 Deserialize a SUBACK packet. More...
 
IotMqttError_t _IotMqtt_SerializeUnsubscribe (const IotMqttSubscription_t *pSubscriptionList, size_t subscriptionCount, uint8_t **pUnsubscribePacket, size_t *pPacketSize, uint16_t *pPacketIdentifier)
 Generate an UNSUBSCRIBE packet from the given parameters. More...
 
IotMqttError_t _IotMqtt_DeserializeUnsuback (_mqttPacket_t *pUnsuback)
 Deserialize a UNSUBACK packet. More...
 
IotMqttError_t _IotMqtt_SerializePingreq (uint8_t **pPingreqPacket, size_t *pPacketSize)
 Generate a PINGREQ packet. More...
 
IotMqttError_t _IotMqtt_DeserializePingresp (_mqttPacket_t *pPingresp)
 Deserialize a PINGRESP packet. More...
 
IotMqttError_t _IotMqtt_SerializeDisconnect (uint8_t **pDisconnectPacket, size_t *pPacketSize)
 Generate a DISCONNECT packet. More...
 
void _IotMqtt_FreePacket (uint8_t *pPacket)
 Free a packet generated by the serializer. More...
 

Detailed Description

Implements functions that generate and decode MQTT network packets.

Function Documentation

◆ _decodeSubackStatus()

static IotMqttError_t _decodeSubackStatus ( size_t  statusCount,
const uint8_t *  pStatusStart,
const _mqttPacket_t pSuback 
)
static

Decode the status bytes of a SUBACK packet.

Parameters
[in]statusCountNumber of status bytes in the SUBACK.
[in]pStatusStartThe first status byte in the SUBACK.
[in]pSubackThe SUBACK packet received from the network.
Returns
IOT_MQTT_SUCCESS, IOT_MQTT_SERVER_REFUSED, or IOT_MQTT_BAD_RESPONSE.

◆ _checkRemainingLength()

static IotMqttError_t _checkRemainingLength ( const _mqttPacket_t pPublish,
IotMqttQos_t  qos,
size_t  qos0Minimum 
)
static

Check the remaining length against some value for QoS 0 or QoS 1/2.

The remaining length for a QoS 1/2 will always be two greater than for a QoS 0.

Parameters
[in]pPublishPointer to an MQTT packet struct representing a PUBLISH.
[in]qosThe QoS of the PUBLISH.
[in]qos0MinimumMinimum possible remaining length for a QoS 0 PUBLISH.
Returns
IOT_MQTT_SUCCESS or IOT_MQTT_BAD_RESPONSE.

◆ _IotMqtt_GetPacketType()

uint8_t _IotMqtt_GetPacketType ( IotNetworkConnection_t  pNetworkConnection,
const IotNetworkInterface_t pNetworkInterface 
)

Get the MQTT packet type from a stream of bytes off the network.

Parameters
[in]pNetworkConnectionReference to the network connection.
[in]pNetworkInterfaceFunction pointers used to interact with the network.
Returns
One of the server-to-client MQTT packet types.
Note
This function is only used for incoming packets, and may not work correctly for outgoing packets.

◆ _IotMqtt_GetRemainingLength()

size_t _IotMqtt_GetRemainingLength ( IotNetworkConnection_t  pNetworkConnection,
const IotNetworkInterface_t pNetworkInterface 
)

Get the remaining length from a stream of bytes off the network.

Parameters
[in]pNetworkConnectionReference to the network connection.
[in]pNetworkInterfaceFunction pointers used to interact with the network.
Returns
The remaining length; MQTT_REMAINING_LENGTH_INVALID on error.

◆ _IotMqtt_SerializeConnect()

IotMqttError_t _IotMqtt_SerializeConnect ( const IotMqttConnectInfo_t pConnectInfo,
uint8_t **  pConnectPacket,
size_t *  pPacketSize 
)

Generate a CONNECT packet from the given parameters.

Parameters
[in]pConnectInfoUser-provided CONNECT information.
[out]pConnectPacketWhere the CONNECT packet is written.
[out]pPacketSizeSize of the packet written to pConnectPacket.
Returns
IOT_MQTT_SUCCESS or IOT_MQTT_NO_MEMORY.

◆ _IotMqtt_DeserializeConnack()

IotMqttError_t _IotMqtt_DeserializeConnack ( _mqttPacket_t pConnack)

Deserialize a CONNACK packet.

Converts the packet from a stream of bytes to an IotMqttError_t. Also prints out debug log messages about the packet.

Parameters
[in,out]pConnackPointer to an MQTT packet struct representing a CONNACK.
Returns
IOT_MQTT_SUCCESS if CONNACK specifies that CONNECT was accepted; IOT_MQTT_SERVER_REFUSED if CONNACK specifies that CONNECT was rejected; IOT_MQTT_BAD_RESPONSE if the CONNACK packet doesn't follow MQTT spec.

◆ _IotMqtt_SerializePublish()

IotMqttError_t _IotMqtt_SerializePublish ( const IotMqttPublishInfo_t pPublishInfo,
uint8_t **  pPublishPacket,
size_t *  pPacketSize,
uint16_t *  pPacketIdentifier,
uint8_t **  pPacketIdentifierHigh 
)

Generate a PUBLISH packet from the given parameters.

Parameters
[in]pPublishInfoUser-provided PUBLISH information.
[out]pPublishPacketWhere the PUBLISH packet is written.
[out]pPacketSizeSize of the packet written to pPublishPacket.
[out]pPacketIdentifierThe packet identifier generated for this PUBLISH.
[out]pPacketIdentifierHighWhere the high byte of the packet identifier is written.
Returns
IOT_MQTT_SUCCESS or IOT_MQTT_NO_MEMORY.

◆ _IotMqtt_PublishSetDup()

void _IotMqtt_PublishSetDup ( uint8_t *  pPublishPacket,
uint8_t *  pPacketIdentifierHigh,
uint16_t *  pNewPacketIdentifier 
)

Set the DUP bit in a QoS 1 PUBLISH packet.

Parameters
[in]pPublishPacketPointer to the PUBLISH packet to modify.
[in]pPacketIdentifierHighThe high byte of any packet identifier to modify.
[out]pNewPacketIdentifierSince AWS IoT does not support the DUP flag, a new packet identifier is generated and should be written here. This parameter is only used when connected to an AWS IoT MQTT server.
Note
See IotMqttPublishInfo_t for caveats with retransmission to the AWS IoT MQTT server.

◆ _IotMqtt_DeserializePublish()

IotMqttError_t _IotMqtt_DeserializePublish ( _mqttPacket_t pPublish)

Deserialize a PUBLISH packet received from the server.

Converts the packet from a stream of bytes to an IotMqttPublishInfo_t and extracts the packet identifier. Also prints out debug log messages about the packet.

Parameters
[in,out]pPublishPointer to an MQTT packet struct representing a PUBLISH.
Returns
IOT_MQTT_SUCCESS if PUBLISH is valid; IOT_MQTT_BAD_RESPONSE if the PUBLISH packet doesn't follow MQTT spec.

◆ _IotMqtt_SerializePuback()

IotMqttError_t _IotMqtt_SerializePuback ( uint16_t  packetIdentifier,
uint8_t **  pPubackPacket,
size_t *  pPacketSize 
)

Generate a PUBACK packet for the given packet identifier.

Parameters
[in]packetIdentifierThe packet identifier to place in PUBACK.
[out]pPubackPacketWhere the PUBACK packet is written.
[out]pPacketSizeSize of the packet written to pPubackPacket.
Returns
IOT_MQTT_SUCCESS or IOT_MQTT_NO_MEMORY.

◆ _IotMqtt_DeserializePuback()

IotMqttError_t _IotMqtt_DeserializePuback ( _mqttPacket_t pPuback)

Deserialize a PUBACK packet.

Converts the packet from a stream of bytes to an IotMqttError_t and extracts the packet identifier. Also prints out debug log messages about the packet.

Parameters
[in,out]pPubackPointer to an MQTT packet struct representing a PUBACK.
Returns
IOT_MQTT_SUCCESS if PUBACK is valid; IOT_MQTT_BAD_RESPONSE if the PUBACK packet doesn't follow MQTT spec.

◆ _IotMqtt_SerializeSubscribe()

IotMqttError_t _IotMqtt_SerializeSubscribe ( const IotMqttSubscription_t pSubscriptionList,
size_t  subscriptionCount,
uint8_t **  pSubscribePacket,
size_t *  pPacketSize,
uint16_t *  pPacketIdentifier 
)

Generate a SUBSCRIBE packet from the given parameters.

Parameters
[in]pSubscriptionListUser-provided array of subscriptions.
[in]subscriptionCountSize of pSubscriptionList.
[out]pSubscribePacketWhere the SUBSCRIBE packet is written.
[out]pPacketSizeSize of the packet written to pSubscribePacket.
[out]pPacketIdentifierThe packet identifier generated for this SUBSCRIBE.
Returns
IOT_MQTT_SUCCESS or IOT_MQTT_NO_MEMORY.

◆ _IotMqtt_DeserializeSuback()

IotMqttError_t _IotMqtt_DeserializeSuback ( _mqttPacket_t pSuback)

Deserialize a SUBACK packet.

Converts the packet from a stream of bytes to an IotMqttError_t and extracts the packet identifier. Also prints out debug log messages about the packet.

Parameters
[in,out]pSubackPointer to an MQTT packet struct representing a SUBACK.
Returns
IOT_MQTT_SUCCESS if SUBACK is valid; IOT_MQTT_BAD_RESPONSE if the SUBACK packet doesn't follow MQTT spec.

◆ _IotMqtt_SerializeUnsubscribe()

IotMqttError_t _IotMqtt_SerializeUnsubscribe ( const IotMqttSubscription_t pSubscriptionList,
size_t  subscriptionCount,
uint8_t **  pUnsubscribePacket,
size_t *  pPacketSize,
uint16_t *  pPacketIdentifier 
)

Generate an UNSUBSCRIBE packet from the given parameters.

Parameters
[in]pSubscriptionListUser-provided array of subscriptions to remove.
[in]subscriptionCountSize of pSubscriptionList.
[out]pUnsubscribePacketWhere the UNSUBSCRIBE packet is written.
[out]pPacketSizeSize of the packet written to pUnsubscribePacket.
[out]pPacketIdentifierThe packet identifier generated for this UNSUBSCRIBE.
Returns
IOT_MQTT_SUCCESS or IOT_MQTT_NO_MEMORY.

◆ _IotMqtt_DeserializeUnsuback()

IotMqttError_t _IotMqtt_DeserializeUnsuback ( _mqttPacket_t pUnsuback)

Deserialize a UNSUBACK packet.

Converts the packet from a stream of bytes to an IotMqttError_t and extracts the packet identifier. Also prints out debug log messages about the packet.

Parameters
[in,out]pUnsubackPointer to an MQTT packet struct representing an UNSUBACK.
Returns
IOT_MQTT_SUCCESS if UNSUBACK is valid; IOT_MQTT_BAD_RESPONSE if the UNSUBACK packet doesn't follow MQTT spec.

◆ _IotMqtt_SerializePingreq()

IotMqttError_t _IotMqtt_SerializePingreq ( uint8_t **  pPingreqPacket,
size_t *  pPacketSize 
)

Generate a PINGREQ packet.

Parameters
[out]pPingreqPacketWhere the PINGREQ packet is written.
[out]pPacketSizeSize of the packet written to pPingreqPacket.
Returns
Always returns IOT_MQTT_SUCCESS.

◆ _IotMqtt_DeserializePingresp()

IotMqttError_t _IotMqtt_DeserializePingresp ( _mqttPacket_t pPingresp)

Deserialize a PINGRESP packet.

Converts the packet from a stream of bytes to an IotMqttError_t. Also prints out debug log messages about the packet.

Parameters
[in,out]pPingrespPointer to an MQTT packet struct representing a PINGRESP.
Returns
IOT_MQTT_SUCCESS if PINGRESP is valid; IOT_MQTT_BAD_RESPONSE if the PINGRESP packet doesn't follow MQTT spec.

◆ _IotMqtt_SerializeDisconnect()

IotMqttError_t _IotMqtt_SerializeDisconnect ( uint8_t **  pDisconnectPacket,
size_t *  pPacketSize 
)

Generate a DISCONNECT packet.

Parameters
[out]pDisconnectPacketWhere the DISCONNECT packet is written.
[out]pPacketSizeSize of the packet written to pDisconnectPacket.
Returns
Always returns IOT_MQTT_SUCCESS.

◆ _IotMqtt_FreePacket()

void _IotMqtt_FreePacket ( uint8_t *  pPacket)

Free a packet generated by the serializer.

Parameters
[in]pPacketThe packet to free.