coreMQTT v5.0.0
MQTT 5.0 Client Library
 
Loading...
Searching...
No Matches
core_mqtt_serializer.c File Reference

Implements the user-facing functions in core_mqtt_serializer.h. More...

#include <string.h>
#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include "core_mqtt.h"
#include "core_mqtt_serializer.h"
#include "transport_interface.h"
#include "private/core_mqtt_serializer_private.h"
#include "core_mqtt_config_defaults.h"

Data Structures

struct  ConnackSeenFlags_t
 Tracks which CONNACK properties have been seen during deserialization, used to detect duplicates. More...
 

Macros

#define MQTT_PACKET_CONNECT_HEADER_SIZE   ( 10U )
 Size of the fixed and variable header of a CONNECT packet.
 
#define MQTT_PUBLISH_FLAG_RETAIN   ( 0 )
 MQTT PUBLISH retain flag.
 
#define MQTT_PUBLISH_FLAG_QOS1   ( 1 )
 MQTT PUBLISH QoS1 flag.
 
#define MQTT_PUBLISH_FLAG_QOS2   ( 2 )
 MQTT PUBLISH QoS2 flag.
 
#define MQTT_PUBLISH_FLAG_DUP   ( 3 )
 MQTT PUBLISH duplicate flag.
 
#define MQTT_PACKET_PINGREQ_SIZE   ( 2U )
 A PINGREQ packet is always 2 bytes in size, defined by MQTT 5.0 spec.
 
#define MQTT_PACKET_CONNACK_SESSION_PRESENT_MASK   ( ( uint8_t ) 0x01U )
 The "Session Present" bit is always the lowest bit.
 
#define MQTT_PACKET_SIMPLE_ACK_REMAINING_LENGTH   ( ( uint8_t ) 2 )
 Minimum Length of PUBACK, PUBREC, PUBREL, PUBCOMP Packets.
 
#define MQTT_PACKET_PINGRESP_REMAINING_LENGTH   ( 0U )
 A PINGRESP packet always has a "Remaining length" of 0.
 
#define MQTT_PACKET_CONNACK_MINIMUM_SIZE   ( 3U )
 Minimum number of bytes in the CONNACK Packet. CONNECT Acknowledge Flags 0 + 1 = 1 CONNECT Reason Code + 1 = 2 Property Length byte (min) + 1 = 3.
 

Functions

static void serializePublishCommon (const MQTTPublishInfo_t *pPublishInfo, const MQTTPropBuilder_t *pPublishProperties, uint32_t remainingLength, uint16_t packetIdentifier, const MQTTFixedBuffer_t *pFixedBuffer, bool serializePayload)
 Serializes MQTT PUBLISH packet into the buffer provided.
 
static MQTTStatus_t calculatePublishPacketSize (const MQTTPublishInfo_t *pPublishInfo, uint32_t *pRemainingLength, uint32_t *pPacketSize, uint32_t maxPacketSize, uint32_t publishPropertyLength)
 Calculates the packet size and remaining length of an MQTT PUBLISH packet.
 
static MQTTStatus_t calculateSubscriptionPacketSize (const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, uint32_t *pRemainingLength, uint32_t *pPacketSize, uint32_t subscribePropLen, uint32_t maxPacketSize, MQTTSubscriptionType_t subscriptionType)
 Calculates the packet size and remaining length of an MQTT SUBSCRIBE or UNSUBSCRIBE packet.
 
static MQTTStatus_t validateSubscriptionSerializeParams (const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, uint16_t packetId, uint32_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer)
 Validates parameters of MQTT_SerializeSubscribe or MQTT_SerializeUnsubscribe.
 
static void serializeConnectPacket (const MQTTConnectInfo_t *pConnectInfo, const MQTTPublishInfo_t *pWillInfo, const MQTTPropBuilder_t *pConnectProperties, const MQTTPropBuilder_t *pWillProperties, uint32_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer)
 Serialize an MQTT CONNECT packet in the given buffer.
 
static void logConnackResponse (uint8_t responseCode)
 Prints the appropriate message for the CONNACK response code if logs are enabled.
 
static uint32_t remainingLengthEncodedSize (uint32_t length)
 Retrieve the size of the remaining length if it were to be encoded.
 
static uint32_t getRemainingLength (TransportRecv_t recvFunc, NetworkContext_t *pNetworkContext)
 Retrieves and decodes the Remaining Length from the network interface by reading a single byte at a time.
 
static MQTTStatus_t processRemainingLength (const uint8_t *pBuffer, const size_t *pIndex, MQTTPacketInfo_t *pIncomingPacket)
 Retrieves, decodes and stores the Remaining Length from the network interface by reading a single byte at a time.
 
static bool incomingPacketValid (uint8_t packetType)
 Check if an incoming packet type is valid.
 
static MQTTStatus_t checkPublishRemainingLength (uint32_t remainingLength, MQTTQoS_t qos, uint32_t qos0Minimum)
 Check the remaining length of an incoming PUBLISH packet against some value for QoS 0, or for QoS 1 and 2.
 
static MQTTStatus_t processPublishFlags (uint8_t publishFlags, MQTTPublishInfo_t *pPublishInfo)
 Process the flags of an incoming PUBLISH packet.
 
static MQTTStatus_t deserializeConnack (MQTTConnectionProperties_t *pConnackProperties, const MQTTPacketInfo_t *pIncomingPacket, bool *pSessionPresent, MQTTPropBuilder_t *pPropBuffer)
 Deserialize an MQTT CONNACK packet.
 
static MQTTStatus_t readSubackStatus (size_t statusCount, const uint8_t *pStatusStart, MQTTReasonCodeInfo_t *pReasonCodes)
 Decode the status bytes of a SUBACK packet to a MQTTStatus_t.
 
static MQTTStatus_t deserializeSubUnsubAck (const MQTTPacketInfo_t *incomingPacket, uint16_t *pPacketId, MQTTReasonCodeInfo_t *pReasonCodes, MQTTPropBuilder_t *pPropBuffer)
 Deserialize an MQTT SUBACK / UNSUBACK packet.
 
static MQTTStatus_t deserializePublish (const MQTTPacketInfo_t *pIncomingPacket, uint16_t *pPacketId, MQTTPublishInfo_t *pPublishInfo, MQTTPropBuilder_t *pPropBuffer, uint16_t topicAliasMax)
 Deserialize a PUBLISH packet received from the server.
 
static MQTTStatus_t deserializePingresp (const MQTTPacketInfo_t *pPingresp)
 Deserialize a PINGRESP packet.
 
static MQTTStatus_t validateConnackParams (const MQTTPacketInfo_t *pIncomingPacket, bool *pSessionPresent)
 Validate the connack parameters.
 
static MQTTStatus_t deserializeConnackProperties (MQTTConnectionProperties_t *pConnackProperties, uint32_t length, uint8_t *pIndex, MQTTPropBuilder_t *pPropBuffer)
 Validate the length and decode the connack properties.
 
static MQTTStatus_t deserializeSubUnsubAckProperties (MQTTPropBuilder_t *pPropBuffer, uint8_t *pIndex, size_t *pSubackPropertyLength, uint32_t remainingLength)
 Deserialize properties in the SUBACK packet received from the server.
 
static MQTTStatus_t deserializePubAcks (const MQTTPacketInfo_t *pAck, uint16_t *pPacketIdentifier, MQTTReasonCodeInfo_t *pReasonCode, bool requestProblem, MQTTPropBuilder_t *pPropBuffer)
 Deserialize an PUBACK, PUBREC, PUBREL, or PUBCOMP packet.
 
static MQTTStatus_t decodePubAckProperties (MQTTPropBuilder_t *pPropBuffer, uint8_t *pIndex, uint32_t remainingLength)
 Validate the length and decode the publish ack properties.
 
static MQTTStatus_t logAckResponse (MQTTSuccessFailReasonCode_t reasonCode, uint16_t packetIdentifier)
 Prints the appropriate message for the PUBREL, PUBACK response code if logs are enabled.
 
static MQTTStatus_t deserializePublishProperties (MQTTPublishInfo_t *pPublishInfo, MQTTPropBuilder_t *pPropBuffer, uint8_t *pIndex, uint16_t topicAliasMax, uint32_t remainingLength)
 Deserialize properties in the PUBLISH packet received from the server.
 
static MQTTStatus_t validateDisconnectResponse (MQTTSuccessFailReasonCode_t reasonCode, bool incoming)
 Prints and validates the appropriate message for the Disconnect response code if logs are enabled.
 
static MQTTStatus_t validateReasonCodeForAck (uint8_t ackPacketType, MQTTSuccessFailReasonCode_t reasonCode)
 Validates the reason codes for the given ACK packet type.
 
static MQTTStatus_t isValidConnackReasonCode (uint8_t reasonCode)
 Validate if a reason code is valid for CONNACK packets.
 
static MQTTStatus_t validateIncomingDisconnectProperties (uint8_t *pIndex, uint32_t disconnectPropertyLength)
 Validate properties in the DISCONNECT packet received from the server.
 
static MQTTStatus_t deserializeConnackProperty (uint8_t propertyId, MQTTConnectionProperties_t *pConnackProperties, uint32_t *pPropertyLength, uint8_t **ppVariableHeader, MQTTPropBuilder_t *pPropBuffer, ConnackSeenFlags_t *pSeen)
 Decode and validate a single CONNACK property from the incoming packet.
 
static void setConnackPropBit (MQTTPropBuilder_t *pPropBuffer, uint8_t bitPos)
 Set a bit in pPropBuffer->fieldSet if pPropBuffer is not NULL.
 
static MQTTStatus_t validateBoolProp (uint8_t value, const char *pPropName)
 Validate that a uint8 property value is 0 or 1 (boolean).
 
static MQTTStatus_t decodeConnackBoolProp (uint8_t *pDest, uint32_t *pPropertyLength, bool *pSeen, uint8_t **ppIndex, const char *pPropName, MQTTPropBuilder_t *pPropBuffer, uint8_t bitPos)
 Decode a uint8 boolean CONNACK property, validate it, and record it.
 
MQTTStatus_t MQTT_SerializePublishHeaderWithoutTopic (const MQTTPublishInfo_t *pPublishInfo, uint32_t remainingLength, uint8_t *pBuffer, size_t *headerSize)
 Serialize an MQTT PUBLISH packet header without the topic string in the given buffer. This function will add the topic string length to the provided buffer. This helps reduce an unnecessary copy of the topic string into the buffer.
 
MQTTStatus_t updateContextWithConnectProps (const MQTTPropBuilder_t *pPropBuilder, MQTTConnectionProperties_t *pConnectProperties)
 Updates the MQTT context with connect properties from the property builder.
 
MQTTStatus_t MQTT_GetConnectPacketSize (const MQTTConnectInfo_t *pConnectInfo, const MQTTPublishInfo_t *pWillInfo, const MQTTPropBuilder_t *pConnectProperties, const MQTTPropBuilder_t *pWillProperties, uint32_t *pRemainingLength, uint32_t *pPacketSize)
 Get the size and Remaining Length of an MQTT Version 5 CONNECT packet.
 
MQTTStatus_t MQTT_SerializeConnect (const MQTTConnectInfo_t *pConnectInfo, const MQTTPublishInfo_t *pWillInfo, const MQTTPropBuilder_t *pConnectProperties, const MQTTPropBuilder_t *pWillProperties, uint32_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer)
 Serialize an MQTT CONNECT packet in the given fixed buffer pFixedBuffer.
 
MQTTStatus_t MQTT_GetSubscribePacketSize (const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, const MQTTPropBuilder_t *pSubscribeProperties, uint32_t *pRemainingLength, uint32_t *pPacketSize, uint32_t maxPacketSize)
 Get packet size and Remaining Length of an MQTT SUBSCRIBE packet.
 
MQTTStatus_t MQTT_SerializeSubscribe (const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, const MQTTPropBuilder_t *pSubscribeProperties, uint16_t packetId, uint32_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer)
 Serialize an MQTT SUBSCRIBE packet in the given buffer.
 
MQTTStatus_t MQTT_GetUnsubscribePacketSize (const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, const MQTTPropBuilder_t *pUnsubscribeProperties, uint32_t *pRemainingLength, uint32_t *pPacketSize, uint32_t maxPacketSize)
 Get packet size and Remaining Length of an MQTT UNSUBSCRIBE packet.
 
MQTTStatus_t MQTT_ValidateUnsubscribeProperties (const MQTTPropBuilder_t *pPropertyBuilder)
 Validates the properties specified for an MQTT UNSUBSCRIBE packet.
 
MQTTStatus_t MQTT_SerializeUnsubscribe (const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, const MQTTPropBuilder_t *pUnsubscribeProperties, uint16_t packetId, uint32_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer)
 Serialize an MQTT UNSUBSCRIBE packet with properties in the given buffer.
 
MQTTStatus_t MQTT_GetPublishPacketSize (const MQTTPublishInfo_t *pPublishInfo, const MQTTPropBuilder_t *pPublishProperties, uint32_t *pRemainingLength, uint32_t *pPacketSize, uint32_t maxPacketSize)
 Get the packet size and remaining length of an MQTT PUBLISH packet.
 
MQTTStatus_t MQTT_SerializePublish (const MQTTPublishInfo_t *pPublishInfo, const MQTTPropBuilder_t *pPublishProperties, uint16_t packetId, uint32_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer)
 Serialize an MQTT PUBLISH packet in the given buffer.
 
MQTTStatus_t MQTT_SerializePublishHeader (const MQTTPublishInfo_t *pPublishInfo, const MQTTPropBuilder_t *pPublishProperties, uint16_t packetId, uint32_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer, size_t *pHeaderSize)
 Serialize an MQTT PUBLISH packet header in the given buffer.
 
static MQTTStatus_t serializeAckWithProperties (const MQTTFixedBuffer_t *pFixedBuffer, uint8_t packetType, uint16_t packetId, const MQTTPropBuilder_t *pAckProperties, const MQTTSuccessFailReasonCode_t *pReasonCode)
 Serialize a publish ACK packet with properties into pFixedBuffer. Called only when pReasonCode != NULL and pAckProperties != NULL.
 
static MQTTStatus_t serializeAckBody (const MQTTFixedBuffer_t *pFixedBuffer, uint8_t packetType, uint16_t packetId, const MQTTPropBuilder_t *pAckProperties, const MQTTSuccessFailReasonCode_t *pReasonCode)
 Validate parameters and serialize a publish ACK packet body. Handles the three cases: no reason code, reason code only, reason code + properties.
 
MQTTStatus_t MQTT_SerializeAck (const MQTTFixedBuffer_t *pFixedBuffer, uint8_t packetType, uint16_t packetId, const MQTTPropBuilder_t *pAckProperties, const MQTTSuccessFailReasonCode_t *pReasonCode)
 Serialize an MQTT PUBACK, PUBREC, PUBREL, or PUBCOMP into the given buffer.
 
MQTTStatus_t MQTT_GetDisconnectPacketSize (const MQTTPropBuilder_t *pDisconnectProperties, uint32_t *pRemainingLength, uint32_t *pPacketSize, uint32_t maxPacketSize, const MQTTSuccessFailReasonCode_t *pReasonCode)
 Get the size of an MQTT DISCONNECT packet.
 
MQTTStatus_t MQTT_SerializeDisconnect (const MQTTPropBuilder_t *pDisconnectProperties, const MQTTSuccessFailReasonCode_t *pReasonCode, uint32_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer)
 Serialize an MQTT DISCONNECT packet into the given buffer.
 
MQTTStatus_t MQTT_GetPingreqPacketSize (uint32_t *pPacketSize)
 Get the size of an MQTT PINGREQ packet.
 
MQTTStatus_t MQTT_SerializePingreq (const MQTTFixedBuffer_t *pFixedBuffer)
 Serialize an MQTT PINGREQ packet into the given buffer.
 
MQTTStatus_t MQTT_DeserializePublish (const MQTTPacketInfo_t *pIncomingPacket, uint16_t *pPacketId, MQTTPublishInfo_t *pPublishInfo, MQTTPropBuilder_t *propBuffer, uint32_t maxPacketSize, uint16_t topicAliasMax)
 Deserialize an MQTT PUBLISH packet.
 
MQTTStatus_t MQTT_DeserializeConnAck (const MQTTPacketInfo_t *pIncomingPacket, bool *pSessionPresent, MQTTPropBuilder_t *pPropBuffer, MQTTConnectionProperties_t *pConnectProperties)
 Deserialize an MQTT CONNACK.
 
MQTTStatus_t MQTT_DeserializeAck (const MQTTPacketInfo_t *pIncomingPacket, uint16_t *pPacketId, MQTTReasonCodeInfo_t *pReasonCode, MQTTPropBuilder_t *pPropBuffer, const MQTTConnectionProperties_t *pConnectProperties)
 Deserialize an MQTT PUBACK, PUBREC, PUBREL, PUBCOMP, SUBACK, UNSUBACK, or PINGRESP.
 
MQTTStatus_t MQTT_GetIncomingPacketTypeAndLength (TransportRecv_t readFunc, NetworkContext_t *pNetworkContext, MQTTPacketInfo_t *pIncomingPacket)
 Extract the MQTT packet type and length from incoming packet.
 
MQTTStatus_t MQTT_UpdateDuplicatePublishFlag (uint8_t *pHeader, bool set)
 Update the duplicate publish flag within the given header of the publish packet.
 
MQTTStatus_t MQTT_ProcessIncomingPacketTypeAndLength (const uint8_t *pBuffer, const size_t *pIndex, MQTTPacketInfo_t *pIncomingPacket)
 Extract the MQTT packet type and length from incoming packet.
 
MQTTStatus_t MQTT_InitConnect (MQTTConnectionProperties_t *pConnectProperties)
 Initialize an MQTTConnectionProperties_t.
 
MQTTStatus_t MQTTPropertyBuilder_Init (MQTTPropBuilder_t *pPropertyBuilder, uint8_t *buffer, size_t length)
 Initialize the property builder.
 
MQTTStatus_t MQTT_ValidateWillProperties (const MQTTPropBuilder_t *pPropertyBuilder)
 Validates the properties specified for WILL Properties in the MQTT CONNECT packet.
 
static MQTTStatus_t checkOnce (uint32_t *pBitMask, uint8_t bitPos, const char *pPropName)
 Check that a property has not been seen before, then decode it. Returns MQTTBadResponse if the property appears more than once.
 
static MQTTStatus_t validateConnectProperty (uint8_t propertyId, uint32_t *pPropertyLength, uint8_t **ppIndex, uint32_t *pBitMask, bool *pIsRequestProblemInfoSet, uint32_t *pPacketMaxSizeValue)
 Decode and validate a single CONNECT property.
 
MQTTStatus_t MQTT_ValidateConnectProperties (const MQTTPropBuilder_t *pPropertyBuilder, bool *isRequestProblemInfoSet, uint32_t *pPacketMaxSizeValue)
 Validate the properties in a CONNECT packet.
 
MQTTStatus_t MQTT_ValidateSubscribeProperties (bool isSubscriptionIdAvailable, const MQTTPropBuilder_t *propBuilder)
 Validates the properties of a SUBSCRIBE packet.
 
MQTTStatus_t MQTT_ValidatePublishProperties (uint16_t serverTopicAliasMax, const MQTTPropBuilder_t *propBuilder, uint16_t *topicAlias)
 Validates the properties of a PUBLISH packet.
 
MQTTStatus_t MQTT_ValidatePublishParams (const MQTTPublishInfo_t *pPublishInfo, uint8_t retainAvailable, uint8_t maxQos, uint16_t topicAlias, uint32_t maxPacketSize)
 Validate the publish parameters present in the given publish structure pPublishInfo.
 
MQTTStatus_t MQTT_ValidatePublishAckProperties (const MQTTPropBuilder_t *pPropertyBuilder)
 Validates the properties specified for an MQTT PUBLISH ACK packet.
 
MQTTStatus_t MQTT_GetAckPacketSize (uint32_t *pRemainingLength, uint32_t *pPacketSize, uint32_t maxPacketSize, size_t ackPropertyLength)
 Get the size of an outgoing PUBLISH ACK packet.
 
MQTTStatus_t MQTT_ValidateDisconnectProperties (uint32_t connectSessionExpiry, const MQTTPropBuilder_t *pPropertyBuilder)
 Validates the properties specified for an MQTT DISCONNECT packet.
 
MQTTStatus_t MQTT_DeserializeDisconnect (const MQTTPacketInfo_t *pPacket, uint32_t maxPacketSize, MQTTReasonCodeInfo_t *pDisconnectInfo, MQTTPropBuilder_t *pPropBuffer)
 Deserialize an MQTT Disconnect packet.
 

Detailed Description

Implements the user-facing functions in core_mqtt_serializer.h.

Function Documentation

◆ serializePublishCommon()

static void serializePublishCommon ( const MQTTPublishInfo_t pPublishInfo,
const MQTTPropBuilder_t pPublishProperties,
uint32_t  remainingLength,
uint16_t  packetIdentifier,
const MQTTFixedBuffer_t pFixedBuffer,
bool  serializePayload 
)
static

Serializes MQTT PUBLISH packet into the buffer provided.

This function serializes MQTT PUBLISH packet into MQTTFixedBuffer_t.pBuffer. Copy of the payload into the buffer is done as part of the serialization only if serializePayload is true.

Parameters
[in]pPublishInfoPublish information containing topic, QoS, payload and other PUBLISH packet fields.
[in]pPublishPropertiesMQTT v5.0 properties for the PUBLISH packet. Can be NULL if no properties are needed.
[in]remainingLengthRemaining length of the PUBLISH packet.
[in]packetIdentifierPacket identifier of PUBLISH packet.
[in,out]pFixedBufferBuffer to which PUBLISH packet will be serialized.
[in]serializePayloadCopy payload to the serialized buffer only if true. Only PUBLISH header will be serialized if false.

◆ calculatePublishPacketSize()

static MQTTStatus_t calculatePublishPacketSize ( const MQTTPublishInfo_t pPublishInfo,
uint32_t *  pRemainingLength,
uint32_t *  pPacketSize,
uint32_t  maxPacketSize,
uint32_t  publishPropertyLength 
)
static

Calculates the packet size and remaining length of an MQTT PUBLISH packet.

Parameters
[in]pPublishInfoMQTT PUBLISH packet parameters.
[out]pRemainingLengthThe Remaining Length of the MQTT PUBLISH packet.
[out]pPacketSizeThe total size of the MQTT PUBLISH packet.
[in]maxPacketSizeMax packet size allowed by the server.
[in]publishPropertyLengthLength of the optional properties in MQTT_PUBLISH
Returns
MQTTBadParameter if the packet would exceed the size allowed by the MQTT spec; MQTTSuccess otherwise.

◆ calculateSubscriptionPacketSize()

static MQTTStatus_t calculateSubscriptionPacketSize ( const MQTTSubscribeInfo_t pSubscriptionList,
size_t  subscriptionCount,
uint32_t *  pRemainingLength,
uint32_t *  pPacketSize,
uint32_t  subscribePropLen,
uint32_t  maxPacketSize,
MQTTSubscriptionType_t  subscriptionType 
)
static

Calculates the packet size and remaining length of an MQTT SUBSCRIBE or UNSUBSCRIBE packet.

Parameters
[in]pSubscriptionListList of MQTT subscription info.
[in]subscriptionCountThe number of elements in pSubscriptionList.
[out]pRemainingLengthThe Remaining Length of the MQTT SUBSCRIBE or UNSUBSCRIBE packet.
[out]pPacketSizeThe total size of the MQTT SUBSCRIBE or MQTT UNSUBSCRIBE packet.
[in]subscribePropLenLength of the optional properties in MQTT_SUBSCRIBE or MQTT_UNSUBSCRIBE
[in]maxPacketSizeMaximum Packet Size allowed by the broker
[in]subscriptionTypeMQTT_TYPE_SUBSCRIBE or MQTT_TYPE_UNSUBSCRIBE.
Returns
MQTTBadParameter if the packet would exceed the size allowed by the MQTT spec or a subscription is empty; MQTTSuccess otherwise.

◆ validateSubscriptionSerializeParams()

static MQTTStatus_t validateSubscriptionSerializeParams ( const MQTTSubscribeInfo_t pSubscriptionList,
size_t  subscriptionCount,
uint16_t  packetId,
uint32_t  remainingLength,
const MQTTFixedBuffer_t pFixedBuffer 
)
static

Validates parameters of MQTT_SerializeSubscribe or MQTT_SerializeUnsubscribe.

Parameters
[in]pSubscriptionListList of MQTT subscription info.
[in]subscriptionCountThe number of elements in pSubscriptionList.
[in]packetIdPacket identifier.
[in]remainingLengthRemaining length of the packet.
[in]pFixedBufferBuffer for packet serialization.
Returns
MQTTNoMemory if pBuffer is too small to hold the MQTT packet; MQTTBadParameter if invalid parameters are passed; MQTTSuccess otherwise.

◆ serializeConnectPacket()

static void serializeConnectPacket ( const MQTTConnectInfo_t pConnectInfo,
const MQTTPublishInfo_t pWillInfo,
const MQTTPropBuilder_t pConnectProperties,
const MQTTPropBuilder_t pWillProperties,
uint32_t  remainingLength,
const MQTTFixedBuffer_t pFixedBuffer 
)
static

Serialize an MQTT CONNECT packet in the given buffer.

Parameters
[in]pConnectInfoMQTT CONNECT packet parameters.
[in]pWillInfoLast Will and Testament. Pass NULL if not used.
[in]pConnectPropertiesMQTT CONNECT properties.
[in]pWillPropertiesMQTT Will properties.
[in]remainingLengthRemaining Length of MQTT CONNECT packet.
[out]pFixedBufferBuffer for packet serialization.

◆ logConnackResponse()

static void logConnackResponse ( uint8_t  responseCode)
static

Prints the appropriate message for the CONNACK response code if logs are enabled.

Parameters
[in]responseCodeMQTT standard CONNACK response code.

◆ remainingLengthEncodedSize()

static uint32_t remainingLengthEncodedSize ( uint32_t  length)
static

Retrieve the size of the remaining length if it were to be encoded.

Parameters
[in]lengthThe remaining length to be encoded.
Returns
The size of the remaining length if it were to be encoded.

◆ getRemainingLength()

static uint32_t getRemainingLength ( TransportRecv_t  recvFunc,
NetworkContext_t pNetworkContext 
)
static

Retrieves and decodes the Remaining Length from the network interface by reading a single byte at a time.

Parameters
[in]recvFuncNetwork interface receive function.
[in]pNetworkContextNetwork interface context to the receive function.
Returns
The Remaining Length of the incoming packet.

◆ processRemainingLength()

static MQTTStatus_t processRemainingLength ( const uint8_t *  pBuffer,
const size_t *  pIndex,
MQTTPacketInfo_t pIncomingPacket 
)
static

Retrieves, decodes and stores the Remaining Length from the network interface by reading a single byte at a time.

Parameters
[in]pBufferThe buffer holding the raw data to be processed
[in]pIndexPointer to the index within the buffer to marking the end of raw data available.
[in]pIncomingPacketStructure used to hold the fields of the incoming packet.
Returns
MQTTNeedMoreBytes is returned to show that the incoming packet is not yet fully received and decoded. Otherwise, MQTTSuccess shows that processing of the packet was successful.

◆ incomingPacketValid()

static bool incomingPacketValid ( uint8_t  packetType)
static

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.

◆ checkPublishRemainingLength()

static MQTTStatus_t checkPublishRemainingLength ( uint32_t  remainingLength,
MQTTQoS_t  qos,
uint32_t  qos0Minimum 
)
static

Check the remaining length of an incoming PUBLISH packet against some value for QoS 0, or for QoS 1 and 2.

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

Parameters
[in]remainingLengthRemaining length of the PUBLISH packet.
[in]qosThe QoS of the PUBLISH.
[in]qos0MinimumMinimum possible remaining length for a QoS 0 PUBLISH.
Returns
MQTTSuccess or MQTTBadResponse.

◆ processPublishFlags()

static MQTTStatus_t processPublishFlags ( uint8_t  publishFlags,
MQTTPublishInfo_t pPublishInfo 
)
static

Process the flags of an incoming PUBLISH packet.

Parameters
[in]publishFlagsFlags of an incoming PUBLISH.
[in,out]pPublishInfoPointer to MQTTPublishInfo_t struct where output will be written.
Returns
MQTTSuccess or MQTTBadResponse.

◆ deserializeConnack()

static MQTTStatus_t deserializeConnack ( MQTTConnectionProperties_t pConnackProperties,
const MQTTPacketInfo_t pIncomingPacket,
bool *  pSessionPresent,
MQTTPropBuilder_t pPropBuffer 
)
static

Deserialize an MQTT CONNACK packet.

Parameters
[out]pConnackPropertiesTo store the deserialized connack properties.
[in]pIncomingPacketMQTTPacketInfo_t containing the buffer.
[out]pSessionPresentWhether a previous session was present.
[out]pPropBufferMQTTPropBuilder_t to store the deserialized properties.
Returns
MQTTBadParameter, MQTTBadResponse, MQTTSuccess, MQTTServerRefused

◆ readSubackStatus()

static MQTTStatus_t readSubackStatus ( size_t  statusCount,
const uint8_t *  pStatusStart,
MQTTReasonCodeInfo_t pReasonCodes 
)
static

Decode the status bytes of a SUBACK packet to a MQTTStatus_t.

Parameters
[in]statusCountNumber of status bytes in the SUBACK.
[in]pStatusStartThe first status byte in the SUBACK.
[out]pReasonCodesThe MQTTReasonCodeInfo_t to store reason codes for each topic filter.
Returns
MQTTSuccess, MQTTServerRefused, or MQTTBadResponse.

◆ deserializeSubUnsubAck()

static MQTTStatus_t deserializeSubUnsubAck ( const MQTTPacketInfo_t incomingPacket,
uint16_t *  pPacketId,
MQTTReasonCodeInfo_t pReasonCodes,
MQTTPropBuilder_t pPropBuffer 
)
static

Deserialize an MQTT SUBACK / UNSUBACK packet.

Parameters
[in]incomingPacketMQTTPacketInfo_t containing the buffer.
[out]pPacketIdThe packet ID obtained from the buffer.
[out]pReasonCodesStruct to store reason code(s) from the acknowledgment packet. Contains the success/failure status of the corresponding request.
[out]pPropBufferMQTTPropBuilder_t to store the deserialized properties.
Returns
MQTTBadParameter, MQTTBadResponse, MQTTSuccess, MQTTServerRefused

◆ deserializePublish()

static MQTTStatus_t deserializePublish ( const MQTTPacketInfo_t pIncomingPacket,
uint16_t *  pPacketId,
MQTTPublishInfo_t pPublishInfo,
MQTTPropBuilder_t pPropBuffer,
uint16_t  topicAliasMax 
)
static

Deserialize a PUBLISH packet received from the server.

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

Parameters
[in]pIncomingPacketPointer to an MQTT packet struct representing a PUBLISH.
[out]pPacketIdPacket identifier of the PUBLISH.
[out]pPublishInfoPointer to MQTTPublishInfo_t where output is written.
[out]pPropBufferPointer to the property buffer.
[in]topicAliasMaxMaximum allowed Topic Alias.
Returns
MQTTSuccess if PUBLISH is valid; MQTTBadResponse if the PUBLISH packet doesn't follow MQTT spec.

◆ deserializePingresp()

static MQTTStatus_t deserializePingresp ( const MQTTPacketInfo_t pPingresp)
static

Deserialize a PINGRESP packet.

Converts the packet from a stream of bytes to an MQTTStatus_t.

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

◆ validateConnackParams()

static MQTTStatus_t validateConnackParams ( const MQTTPacketInfo_t pIncomingPacket,
bool *  pSessionPresent 
)
static

Validate the connack parameters.

Converts the packet from a stream of bytes to an MQTTStatus_t and extracts the variable header without connack properties.

Parameters
[in]pIncomingPacketPointer to an MQTT packet struct representing a incoming packet.
[out]pSessionPresentWhether a session is present or not.
Returns
MQTTSuccess if connack without connack properties is valid; MQTTServerRefused if the server refused the connection; MQTTBadResponse if the Connack packet doesn't follow MQTT spec.

◆ deserializeConnackProperties()

static MQTTStatus_t deserializeConnackProperties ( MQTTConnectionProperties_t pConnackProperties,
uint32_t  length,
uint8_t *  pIndex,
MQTTPropBuilder_t pPropBuffer 
)
static

Validate the length and decode the connack properties.

Parameters
[out]pConnackPropertiesTo store the decoded property.
[in]lengthLength of the properties.
[in]pIndexPointer to the start of the properties.
[out]pPropBufferPointer to the property buffer.
Returns
MQTTSuccess, MQTTBadResponse

◆ deserializeSubUnsubAckProperties()

static MQTTStatus_t deserializeSubUnsubAckProperties ( MQTTPropBuilder_t pPropBuffer,
uint8_t *  pIndex,
size_t *  pSubackPropertyLength,
uint32_t  remainingLength 
)
static

Deserialize properties in the SUBACK packet received from the server.

Converts the packet from a stream of bytes to an MQTTStatus_t and extracts properties.

Parameters
[out]pPropBufferPointer to the property buffer.
[in]pIndexPointer to the start of the properties.
[out]pSubackPropertyLengthPointer to the length of suback properties
[in]remainingLengthRemaining length of the incoming packet.
Returns
MQTTSuccess if SUBACK is valid; MQTTBadResponse if SUBACK is invalid.

◆ deserializePubAcks()

static MQTTStatus_t deserializePubAcks ( const MQTTPacketInfo_t pAck,
uint16_t *  pPacketIdentifier,
MQTTReasonCodeInfo_t pReasonCode,
bool  requestProblem,
MQTTPropBuilder_t pPropBuffer 
)
static

Deserialize an PUBACK, PUBREC, PUBREL, or PUBCOMP packet.

Converts the packet from a stream of bytes to an MQTTStatus_t and extracts the packet identifier, reason code, properties.

Parameters
[in]pAckPointer to the MQTT packet structure representing the packet.
[out]pPacketIdentifierPacket ID of the ack type packet.
[out]pReasonCodeStructure to store reason code of the ack type packet.
[in]requestProblemTo validate the packet.
[out]pPropBufferPointer to the property buffer.
Returns
MQTTSuccess, MQTTBadResponse, MQTTBadParameter.

◆ decodePubAckProperties()

static MQTTStatus_t decodePubAckProperties ( MQTTPropBuilder_t pPropBuffer,
uint8_t *  pIndex,
uint32_t  remainingLength 
)
static

Validate the length and decode the publish ack properties.

Parameters
[out]pPropBufferTo store the decoded property.
[in]pIndexPointer to the current index of the buffer.
[in]remainingLengthRemaining length of properties in the incoming packet.
Returns
MQTTSuccess, MQTTBadResponse.

◆ logAckResponse()

static MQTTStatus_t logAckResponse ( MQTTSuccessFailReasonCode_t  reasonCode,
uint16_t  packetIdentifier 
)
static

Prints the appropriate message for the PUBREL, PUBACK response code if logs are enabled.

Parameters
[in]reasonCodeMQTT Verion 5 standard PUBREL, PUBACK response code.
[in]packetIdentifierPacket id of the ack packet.
Returns
MQTTSuccess, MQTTServerRefused and MQTTBadResponse.

◆ deserializePublishProperties()

static MQTTStatus_t deserializePublishProperties ( MQTTPublishInfo_t pPublishInfo,
MQTTPropBuilder_t pPropBuffer,
uint8_t *  pIndex,
uint16_t  topicAliasMax,
uint32_t  remainingLength 
)
static

Deserialize properties in the PUBLISH packet received from the server.

Converts the packet from a stream of bytes to an MQTTPublishInfo_t and extracts properties.

Parameters
[out]pPublishInfoPointer to MQTTPublishInfo_t where output is written.
[out]pPropBufferPointer to the property buffer.
[in]pIndexPointer to the start of the properties.
[in]topicAliasMaxMaximum allowed Topic Alias.
[in]remainingLengthRemaining length of the incoming packet.
Returns
MQTTSuccess if PUBLISH is valid; MQTTBadResponse if the PUBLISH packet doesn't follow MQTT spec.

◆ validateDisconnectResponse()

static MQTTStatus_t validateDisconnectResponse ( MQTTSuccessFailReasonCode_t  reasonCode,
bool  incoming 
)
static

Prints and validates the appropriate message for the Disconnect response code if logs are enabled.

Parameters
[in]reasonCodeMQTT Verion 5 standard DISCONNECT response code.
[in]incomingTo differentiate between outgoing and incoming disconnect.
Returns
MQTTSuccess, MQTTBadParameter and MQTTBadResponse.

◆ validateReasonCodeForAck()

static MQTTStatus_t validateReasonCodeForAck ( uint8_t  ackPacketType,
MQTTSuccessFailReasonCode_t  reasonCode 
)
static

Validates the reason codes for the given ACK packet type.

Parameters
[in]ackPacketTypeACK packet type for which the reason code is being added.
[in]reasonCodeThe reason code being added to the ACK packet type.
Returns
MQTTSuccess or MQTTBadParameter.

◆ isValidConnackReasonCode()

static MQTTStatus_t isValidConnackReasonCode ( uint8_t  reasonCode)
inlinestatic

Validate if a reason code is valid for CONNACK packets.

This function checks if the provided reason code is a valid CONNACK reason code according to the MQTT v5 specification.

Parameters
[in]reasonCodeThe reason code to validate.
Returns
MQTTSuccess if the reason code is valid for CONNACK; MQTTServerRefused if the reason code indicates server refusal; MQTTBadResponse if the reason code is invalid.

◆ validateIncomingDisconnectProperties()

static MQTTStatus_t validateIncomingDisconnectProperties ( uint8_t *  pIndex,
uint32_t  disconnectPropertyLength 
)
static

Validate properties in the DISCONNECT packet received from the server.

Note
Incoming properties are different than the ones the client can send.
Parameters
[in]pIndexPointer to the start of the properties.
[in]disconnectPropertyLengthLength of the properties in the DISCONNECT packet.
Returns
MQTTSuccess if DISCONNECT is valid; MQTTBadResponse if the DISCONNECT packet is invalid.

◆ deserializeConnackProperty()

static MQTTStatus_t deserializeConnackProperty ( uint8_t  propertyId,
MQTTConnectionProperties_t pConnackProperties,
uint32_t *  pPropertyLength,
uint8_t **  ppVariableHeader,
MQTTPropBuilder_t pPropBuffer,
ConnackSeenFlags_t pSeen 
)
static

Decode and validate a single CONNACK property from the incoming packet.

Reads one property from the variable header, validates its value, stores it in the appropriate field of pConnackProperties, and sets the corresponding bit in the property buffer's fieldSet if pPropBuffer is not NULL.

Parameters
[in]propertyIdThe MQTT property identifier byte.
[in,out]pConnackPropertiesConnection properties structure to populate.
[in,out]pPropertyLengthRemaining bytes in the property section; decremented as each property value is consumed.
[in,out]ppVariableHeaderPointer to the current read position in the packet buffer; advanced past the consumed property value.
[in,out]pPropBufferOptional property builder to record which properties were received. May be NULL.
[in,out]pSeenDuplicate-detection flags for each CONNACK property.
Returns
MQTTSuccess if the property was decoded and validated successfully.
MQTTBadResponse if the property value is invalid (e.g., boolean out of range, zero where prohibited, response info sent without being requested) or if an unknown property ID is encountered.

◆ setConnackPropBit()

static void setConnackPropBit ( MQTTPropBuilder_t pPropBuffer,
uint8_t  bitPos 
)
static

Set a bit in pPropBuffer->fieldSet if pPropBuffer is not NULL.

Parameters
[in,out]pPropBufferProperty builder whose fieldSet will be updated. If NULL, this function does nothing.
[in]bitPosBit position to set in fieldSet.

◆ validateBoolProp()

static MQTTStatus_t validateBoolProp ( uint8_t  value,
const char *  pPropName 
)
static

Validate that a uint8 property value is 0 or 1 (boolean).

Parameters
[in]valueThe decoded uint8 property value to validate.
[in]pPropNameHuman-readable property name used in the error log message.
Returns
MQTTSuccess if value is 0 or 1.
MQTTBadResponse if value is greater than 1.

◆ decodeConnackBoolProp()

static MQTTStatus_t decodeConnackBoolProp ( uint8_t *  pDest,
uint32_t *  pPropertyLength,
bool *  pSeen,
uint8_t **  ppIndex,
const char *  pPropName,
MQTTPropBuilder_t pPropBuffer,
uint8_t  bitPos 
)
static

Decode a uint8 boolean CONNACK property, validate it, and record it.

Calls decodeUint8t to read the value, validateBoolProp to check it is 0 or 1, and setConnackPropBit to mark the property as received in the property buffer.

Parameters
[out]pDestDestination to store the decoded uint8 value.
[in,out]pPropertyLengthRemaining bytes in the property section; decremented by the size of the decoded value.
[in,out]pSeenDuplicate-detection flag; set to true after first decode.
[in,out]ppIndexCurrent read position in the packet buffer; advanced past the decoded value.
[in]pPropNameHuman-readable property name used in error log messages.
[in,out]pPropBufferOptional property builder to record the property bit. May be NULL.
[in]bitPosBit position to set in pPropBuffer->fieldSet on success.
Returns
MQTTSuccess if the property was decoded and validated successfully.
MQTTBadResponse if the value is not 0 or 1, or if decoding fails.

◆ MQTT_SerializePublishHeaderWithoutTopic()

MQTTStatus_t MQTT_SerializePublishHeaderWithoutTopic ( const MQTTPublishInfo_t pPublishInfo,
uint32_t  remainingLength,
uint8_t *  pBuffer,
size_t *  headerSize 
)

Serialize an MQTT PUBLISH packet header without the topic string in the given buffer. This function will add the topic string length to the provided buffer. This helps reduce an unnecessary copy of the topic string into the buffer.

Parameters
[in]pPublishInfoMQTT PUBLISH packet parameters.
[in]remainingLengthRemaining Length provided by MQTT_GetPublishPacketSize.
[out]pBufferBuffer for packet serialization.
[out]headerSizeSize of the serialized MQTT PUBLISH header.
Returns
MQTTSuccess if the serialization is successful. Otherwise, MQTTBadParameter.

◆ updateContextWithConnectProps()

MQTTStatus_t updateContextWithConnectProps ( const MQTTPropBuilder_t pPropBuilder,
MQTTConnectionProperties_t pConnectProperties 
)

Updates the MQTT context with connect properties from the property builder.

This function processes the property builder and updates the connect properties in the MQTT context. It handles the conversion and validation of properties from the property builder to the connect properties structure.

Parameters
[in]pPropBuilderPointer to the property builder containing MQTT properties. Must not be NULL.
[out]pConnectPropertiesPointer to the connection properties structure to be updated. Must not be NULL.
Returns
Returns one of the following:

Example

// Variables used in this example.
MQTTStatus_t status;
MQTTPropBuilder_t propBuilder = { 0 };
MQTTConnectionProperties_t connectionProperties = { 0 };
// Initialize property builder with desired properties
// ...
// Update connect properties
status = updateContextWithConnectProps( &propBuilder, &connectionProperties );
if(status == MQTTSuccess)
{
// Properties successfully updated in the context
}
MQTTStatus_t updateContextWithConnectProps(const MQTTPropBuilder_t *pPropBuilder, MQTTConnectionProperties_t *pConnectProperties)
Updates the MQTT context with connect properties from the property builder.
Definition: core_mqtt_serializer.c:2985
MQTTStatus_t
Return codes from MQTT functions.
Definition: core_mqtt_serializer.h:239
@ MQTTSuccess
Definition: core_mqtt_serializer.h:240
Struct to hold connect and connack properties.
Definition: core_mqtt_serializer.h:496
Property builder for MQTT packets.
Definition: core_mqtt_serializer.h:470

This value shall get overwritten if the server sends a sessionExpiry in the CONNACK.

◆ MQTT_GetConnectPacketSize()

MQTTStatus_t MQTT_GetConnectPacketSize ( const MQTTConnectInfo_t pConnectInfo,
const MQTTPublishInfo_t pWillInfo,
const MQTTPropBuilder_t pConnectProperties,
const MQTTPropBuilder_t pWillProperties,
uint32_t *  pRemainingLength,
uint32_t *  pPacketSize 
)

Get the size and Remaining Length of an MQTT Version 5 CONNECT packet.

This function must be called before MQTT_SerializeConnect in order to get the size of the MQTT CONNECT packet that is generated from MQTTConnectInfo_t, MQTTPublishInfo_t and optional MQTTPropBuilder_t. The size of the MQTTFixedBuffer_t supplied to MQTT_SerializeConnect must be at least pPacketSize. The provided pConnectInfo and pWillInfo are valid for serialization with MQTT_SerializeConnect only if this function returns MQTTSuccess. The remaining length returned in pRemainingLength and the packet size returned in pPacketSize are valid only if this function returns MQTTSuccess.

Parameters
[in]pConnectInfoMQTT CONNECT packet parameters.
[in]pWillInfoLast Will and Testament. Pass NULL if not used.
[in]pConnectPropertiesMQTT CONNECT properties builder. Pass NULL if not used.
[in]pWillPropertiesMQTT Will properties builder. Pass NULL if not used.
[out]pRemainingLengthThe Remaining Length of the MQTT CONNECT packet.
[out]pPacketSizeThe total size of the MQTT CONNECT packet.
Returns
MQTTBadParameter if the packet would exceed the size allowed by the MQTT spec; MQTTSuccess otherwise.

Example

// Variables used in this example.
MQTTStatus_t status;
MQTTConnectInfo_t connectInfo = { 0 };
MQTTPublishInfo_t willInfo = { 0 };
MQTTPropBuilder_t connectionProperties = { 0 };
MQTTPropBuilder_t willProperties = { 0 };
size_t remainingLength = 0, packetSize = 0;
// Initialize the connection info, the details are out of scope for this example.
initializeConnectInfo( &connectInfo );
// Initialize the optional will info, the details are out of scope for this example.
initializeWillInfo( &willInfo );
// Initialize connect properties and will properties, the details are out of scope for this example.
initializeConnectProperties( &connectionProperties );
initializeWillProperties( &willProperties );
// Get the size requirement for the connect packet.
&connectInfo,
&willInfo,
&connectionProperties,
&willProperties,
&remainingLength,
&packetSize
);
if( status == MQTTSuccess )
{
// The application should allocate or use a static #MQTTFixedBuffer_t
// of size >= packetSize to serialize the connect request.
}
MQTTStatus_t MQTT_GetConnectPacketSize(const MQTTConnectInfo_t *pConnectInfo, const MQTTPublishInfo_t *pWillInfo, const MQTTPropBuilder_t *pConnectProperties, const MQTTPropBuilder_t *pWillProperties, uint32_t *pRemainingLength, uint32_t *pPacketSize)
Get the size and Remaining Length of an MQTT Version 5 CONNECT packet.
Definition: core_mqtt_serializer.c:3097
MQTT CONNECT packet parameters.
Definition: core_mqtt_serializer.h:294
MQTT PUBLISH packet parameters.
Definition: core_mqtt_serializer.h:395

◆ MQTT_SerializeConnect()

MQTTStatus_t MQTT_SerializeConnect ( const MQTTConnectInfo_t pConnectInfo,
const MQTTPublishInfo_t pWillInfo,
const MQTTPropBuilder_t pConnectProperties,
const MQTTPropBuilder_t pWillProperties,
uint32_t  remainingLength,
const MQTTFixedBuffer_t pFixedBuffer 
)

Serialize an MQTT CONNECT packet in the given fixed buffer pFixedBuffer.

MQTT_GetConnectPacketSize should be called with pConnectInfo, pWillInfo, pConnectProperties, and pWillProperties before invoking this function to get the size of the required MQTTFixedBuffer_t and remainingLength. The remainingLength must be the same as returned by MQTT_GetConnectPacketSize. The MQTTFixedBuffer_t must be at least as large as the size returned by MQTT_GetConnectPacketSize.

Parameters
[in]pConnectInfoMQTT CONNECT packet parameters.
[in]pWillInfoLast Will and Testament. Pass NULL if not used.
[in]pConnectPropertiesMQTT CONNECT properties builder. Pass NULL if not used.
[in]pWillPropertiesMQTT Will properties builder. Pass NULL if not used.
[in]remainingLengthRemaining Length provided by MQTT_GetConnectPacketSize.
[out]pFixedBufferBuffer for packet serialization.
Returns
MQTTNoMemory if pFixedBuffer is too small to hold the MQTT packet; MQTTBadParameter if invalid parameters are passed; MQTTSuccess otherwise.

Example

// Variables used in this example.
MQTTStatus_t status;
MQTTConnectInfo_t connectInfo = { 0 };
MQTTPublishInfo_t willInfo = { 0 };
MQTTPropBuilder_t connectionProperties = { 0 };
MQTTPropBuilder_t willProperties = { 0 };
MQTTFixedBuffer_t fixedBuffer;
uint8_t buffer[ BUFFER_SIZE ];
size_t remainingLength = 0, packetSize = 0;
fixedBuffer.pBuffer = buffer;
fixedBuffer.size = BUFFER_SIZE;
// Assume connectInfo, willInfo, and properties are initialized.
// Get the size requirement for the connect packet.
&connectInfo, &willInfo, &connectionProperties, &willProperties,
&remainingLength, &packetSize
);
assert( status == MQTTSuccess );
assert( packetSize <= BUFFER_SIZE );
// Serialize the connect packet into the fixed buffer.
&connectInfo,
&willInfo,
&connectionProperties,
&willProperties,
remainingLength,
&fixedBuffer
);
if( status == MQTTSuccess )
{
// The connect packet can now be sent to the broker.
}
MQTTStatus_t MQTT_SerializeConnect(const MQTTConnectInfo_t *pConnectInfo, const MQTTPublishInfo_t *pWillInfo, const MQTTPropBuilder_t *pConnectProperties, const MQTTPropBuilder_t *pWillProperties, uint32_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT CONNECT packet in the given fixed buffer pFixedBuffer.
Definition: core_mqtt_serializer.c:3428
Buffer passed to MQTT library.
Definition: core_mqtt_serializer.h:284
size_t size
Size of buffer.
Definition: core_mqtt_serializer.h:286
uint8_t * pBuffer
Pointer to buffer.
Definition: core_mqtt_serializer.h:285

◆ MQTT_GetSubscribePacketSize()

MQTTStatus_t MQTT_GetSubscribePacketSize ( const MQTTSubscribeInfo_t pSubscriptionList,
size_t  subscriptionCount,
const MQTTPropBuilder_t pSubscribeProperties,
uint32_t *  pRemainingLength,
uint32_t *  pPacketSize,
uint32_t  maxPacketSize 
)

Get packet size and Remaining Length of an MQTT SUBSCRIBE packet.

This function must be called before MQTT_SerializeSubscribe in order to get the size of the MQTT SUBSCRIBE packet that is generated from the list of MQTTSubscribeInfo_t and MQTTPropBuilder_t (optional subscribe properties). The size of the MQTTFixedBuffer_t supplied to MQTT_SerializeSubscribe must be at least pPacketSize. The provided pSubscriptionList is valid for serialization with MQTT_SerializeSubscribe only if this function returns MQTTSuccess. The remaining length returned in pRemainingLength and the packet size returned in pPacketSize are valid only if this function returns MQTTSuccess.

Parameters
[in]pSubscriptionListList of MQTT subscription info.
[in]subscriptionCountThe number of elements in pSubscriptionList.
[in]pSubscribePropertiesMQTT SUBSCRIBE properties builder. Pass NULL if not used.
[out]pRemainingLengthThe Remaining Length of the MQTT SUBSCRIBE packet.
[out]pPacketSizeThe total size of the MQTT SUBSCRIBE packet.
[in]maxPacketSizeMaximum packet size.
Returns
MQTTBadParameter if the packet would exceed the size allowed by the MQTT spec; MQTTSuccess otherwise.

Example

// Variables used in this example.
MQTTStatus_t status;
MQTTSubscribeInfo_t subscriptionList[ NUMBER_OF_SUBSCRIPTIONS ] = { 0 };
MQTTPropBuilder_t subscribeProperties = { 0 };
size_t remainingLength = 0, packetSize = 0;
// This is assumed to be a list of filters we want to subscribe to.
const char * filters[ NUMBER_OF_SUBSCRIPTIONS ];
// Set each subscription.
for( int i = 0; i < NUMBER_OF_SUBSCRIPTIONS; i++ )
{
subscriptionList[ i ].qos = MQTTQoS0;
// Each subscription needs a topic filter.
subscriptionList[ i ].pTopicFilter = filters[ i ];
subscriptionList[ i ].topicFilterLength = strlen( filters[ i ] );
subscriptionList[ i ].noLocalOption = false;
subscriptionList[ i ].retainAsPublishedOption = false;
subscriptionList[ i ].retainHandlingOption = retainSendOnSub;
}
// Initialize subscribe properties (if needed)
initializeSubscribeProperties( &subscribeProperties );
// Get the size requirement for the subscribe packet.
&subscriptionList[ 0 ],
NUMBER_OF_SUBSCRIPTIONS,
&subscribeProperties,
&remainingLength,
&packetSize,
maxPacketSize
);
if( status == MQTTSuccess )
{
// The application should allocate or use a static #MQTTFixedBuffer_t
// of size >= packetSize to serialize the subscribe request.
}
MQTTStatus_t MQTT_GetSubscribePacketSize(const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, const MQTTPropBuilder_t *pSubscribeProperties, uint32_t *pRemainingLength, uint32_t *pPacketSize, uint32_t maxPacketSize)
Get packet size and Remaining Length of an MQTT SUBSCRIBE packet.
Definition: core_mqtt_serializer.c:3510
@ retainSendOnSub
Definition: core_mqtt_serializer.h:341
@ MQTTQoS0
Definition: core_mqtt_serializer.h:271
MQTT SUBSCRIBE packet parameters.
Definition: core_mqtt_serializer.h:351
bool retainAsPublishedOption
If true, Application Messages forwarded using this subscription keep the RETAIN flag they were publis...
Definition: core_mqtt_serializer.h:380
size_t topicFilterLength
Length of subscription topic filter - unsigned long.
Definition: core_mqtt_serializer.h:365
MQTTQoS_t qos
Quality of Service for subscription. Include protocol error of qos > 2.
Definition: core_mqtt_serializer.h:355
MQTTRetainHandling_t retainHandlingOption
Specifies whether retained messages are sent when the subscription is established.
Definition: core_mqtt_serializer.h:386
bool noLocalOption
no local option for subscription. Include protocol error if noLocalOption = 1 in a shared subscriptio...
Definition: core_mqtt_serializer.h:374
const char * pTopicFilter
Topic filter to subscribe to.
Definition: core_mqtt_serializer.h:360

◆ MQTT_SerializeSubscribe()

MQTTStatus_t MQTT_SerializeSubscribe ( const MQTTSubscribeInfo_t pSubscriptionList,
size_t  subscriptionCount,
const MQTTPropBuilder_t pSubscribeProperties,
uint16_t  packetId,
uint32_t  remainingLength,
const MQTTFixedBuffer_t pFixedBuffer 
)

Serialize an MQTT SUBSCRIBE packet in the given buffer.

MQTT_GetSubscribePacketSize should be called with pSubscriptionList before invoking this function to get the size of the required MQTTFixedBuffer_t and remainingLength. The remainingLength must be the same as returned by MQTT_GetSubscribePacketSize. The MQTTFixedBuffer_t must be at least as large as the size returned by MQTT_GetSubscribePacketSize.

Parameters
[in]pSubscriptionListList of MQTT subscription info.
[in]subscriptionCountThe number of elements in pSubscriptionList.
[in]pSubscribePropertiesMQTT v5.0 properties for the SUBSCRIBE packet. Can be NULL if no properties are needed.
[in]packetIdpacket ID generated by MQTT_GetPacketId.
[in]remainingLengthRemaining Length provided by MQTT_GetSubscribePacketSize.
[out]pFixedBufferBuffer for packet serialization.
Returns
MQTTNoMemory if pFixedBuffer is too small to hold the MQTT packet; MQTTBadParameter if invalid parameters are passed; MQTTSuccess otherwise.

Example

// Variables used in this example.
MQTTStatus_t status;
MQTTSubscribeInfo_t subscriptionList[ NUMBER_OF_SUBSCRIPTIONS ] = { 0 };
MQTTPropBuilder_t subscribeProperties = { 0 };
MQTTFixedBuffer_t fixedBuffer;
uint8_t buffer[ BUFFER_SIZE ];
size_t remainingLength = 0, packetSize = 0;
uint16_t packetId;
fixedBuffer.pBuffer = buffer;
fixedBuffer.size = BUFFER_SIZE;
// Function to return a valid, unused packet identifier. The details are out of
// scope for this example.
packetId = getNewPacketId();
// Assume subscriptionList and subscribeProperties have been initialized.
Get the subscribe packet size.
&subscriptionList[ 0 ], NUMBER_OF_SUBSCRIPTIONS, &subscribeProperties,
&remainingLength, &packetSize
);
assert( status == MQTTSuccess );
assert( packetSize <= BUFFER_SIZE );
// Serialize the subscribe packet into the fixed buffer.
&subscriptionList[ 0 ],
NUMBER_OF_SUBSCRIPTIONS,
&subscribeProperties,
packetId,
remainingLength,
&fixedBuffer
);
if( status == MQTTSuccess )
{
// The subscribe packet can now be sent to the broker.
}
MQTTStatus_t MQTT_SerializeSubscribe(const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, const MQTTPropBuilder_t *pSubscribeProperties, uint16_t packetId, uint32_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT SUBSCRIBE packet in the given buffer.
Definition: core_mqtt_serializer.c:3572

◆ MQTT_GetUnsubscribePacketSize()

MQTTStatus_t MQTT_GetUnsubscribePacketSize ( const MQTTSubscribeInfo_t pSubscriptionList,
size_t  subscriptionCount,
const MQTTPropBuilder_t pUnsubscribeProperties,
uint32_t *  pRemainingLength,
uint32_t *  pPacketSize,
uint32_t  maxPacketSize 
)

Get packet size and Remaining Length of an MQTT UNSUBSCRIBE packet.

This function must be called before MQTT_SerializeUnsubscribe in order to get the size of the MQTT UNSUBSCRIBE packet that is generated from the list of MQTTSubscribeInfo_t and MQTTPropBuilder_t (optional unsubscribe properties). The size of the MQTTFixedBuffer_t supplied to MQTT_SerializeUnsubscribe must be at least pPacketSize. The provided pSubscriptionList is valid for serialization with MQTT_SerializeUnsubscribe only if this function returns MQTTSuccess. The remaining length returned in pRemainingLength and the packet size returned in pPacketSize are valid only if this function returns MQTTSuccess.

Parameters
[in]pSubscriptionListList of MQTT subscription info.
[in]subscriptionCountThe number of elements in pSubscriptionList.
[in]pUnsubscribePropertiesMQTT UNSUBSCRIBE properties builder. Pass NULL if not used.
[out]pRemainingLengthThe Remaining Length of the MQTT UNSUBSCRIBE packet.
[out]pPacketSizeThe total size of the MQTT UNSUBSCRIBE packet.
[in]maxPacketSizeMaximum packet size.
Returns
MQTTBadParameter if the packet would exceed the size allowed by the MQTT spec; MQTTSuccess otherwise.

Example

// Variables used in this example.
MQTTStatus_t status;
MQTTSubscribeInfo_t subscriptionList[ NUMBER_OF_SUBSCRIPTIONS ] = { 0 };
size_t remainingLength = 0, packetSize = 0;
MQTTPropBuilder_t unsubscribeProperties = { 0 };
size_t maxPacketSize = 0;
// Initialize maxPacketSize. The details are out of scope for this example.
initializeMaxPacketSize( &maxPacketSize );
// Initialize the subscribe info. The details are out of scope for this example.
initializeSubscribeInfo( &subscriptionList[ 0 ] );
//Initialize the property buffer. The details are out of scope for this example.
initializePropertyBuffer( &unsubscribeProperties );
// Get the size requirement for the unsubscribe packet.
&subscriptionList[ 0 ],
NUMBER_OF_SUBSCRIPTIONS,
&unsubscribeProperties,
&remainingLength,
&packetSize,
maxPacketSize);
if( status == MQTTSuccess )
{
// The unsubscribe packet can now be sent to the broker.
}
MQTTStatus_t MQTT_GetUnsubscribePacketSize(const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, const MQTTPropBuilder_t *pUnsubscribeProperties, uint32_t *pRemainingLength, uint32_t *pPacketSize, uint32_t maxPacketSize)
Get packet size and Remaining Length of an MQTT UNSUBSCRIBE packet.
Definition: core_mqtt_serializer.c:3696

◆ MQTT_ValidateUnsubscribeProperties()

MQTTStatus_t MQTT_ValidateUnsubscribeProperties ( const MQTTPropBuilder_t pPropertyBuilder)

Validates the properties specified for an MQTT UNSUBSCRIBE packet.

Parameters
[in]pPropertyBuilderPointer to the property builder structure containing unsubscribe properties.
Returns
Returns one of the following:

◆ MQTT_SerializeUnsubscribe()

MQTTStatus_t MQTT_SerializeUnsubscribe ( const MQTTSubscribeInfo_t pSubscriptionList,
size_t  subscriptionCount,
const MQTTPropBuilder_t pUnsubscribeProperties,
uint16_t  packetId,
uint32_t  remainingLength,
const MQTTFixedBuffer_t pFixedBuffer 
)

Serialize an MQTT UNSUBSCRIBE packet with properties in the given buffer.

MQTT_GetUnsubscribePacketSize should be called with pSubscriptionList and pUnsubscribeProperties before invoking this function to get the size of the required MQTTFixedBuffer_t and remainingLength. The remainingLength must be the same as returned by MQTT_GetUnsubscribePacketSize. The MQTTFixedBuffer_t must be at least as large as the size returned by MQTT_GetUnsubscribePacketSize.

Parameters
[in]pSubscriptionListList of MQTT subscription info to unsubscribe from.
[in]subscriptionCountThe number of elements in pSubscriptionList.
[in]pUnsubscribePropertiesMQTT 5.0 properties for the UNSUBSCRIBE packet. Can be NULL if no properties are needed.
[in]packetIdPacket identifier used for the UNSUBSCRIBE packet.
[in]remainingLengthRemaining Length provided by MQTT_GetUnsubscribePacketSize.
[out]pFixedBufferBuffer where the serialized UNSUBSCRIBE packet will be written.
Returns
MQTTNoMemory if pFixedBuffer is too small to hold the MQTT packet; MQTTBadParameter if any of the parameters are invalid (NULL pSubscriptionList or pFixedBuffer, zero subscriptionCount); MQTTSuccess if the packet was serialized successfully.

Example

// Variables used in this example.
MQTTStatus_t status;
MQTTSubscribeInfo_t subscriptionList[2];
MQTTPropBuilder_t unsubscribeProperties;
MQTTFixedBuffer_t fixedBuffer;
uint8_t buffer[100];
size_t remainingLength = 0, packetSize = 0;
uint16_t packetId = 1;
// Initialize the fixed buffer.
fixedBuffer.pBuffer = buffer;
fixedBuffer.size = sizeof( buffer );
// Initialize subscription list.
subscriptionList[0].pTopicFilter = "topic/1";
subscriptionList[0].topicFilterLength = strlen("topic/1");
subscriptionList[1].pTopicFilter = "topic/2";
subscriptionList[1].topicFilterLength = strlen("topic/2");
// Initialize properties (optional)
// Get size requirement for the unsubscribe packet.
subscriptionList,
2,
&unsubscribeProperties,
&remainingLength,
&packetSize
);
if( status == MQTTSuccess )
{
// Serialize unsubscribe packet.
subscriptionList,
2,
&unsubscribeProperties,
packetId,
remainingLength,
&fixedBuffer
);
}
if( status == MQTTSuccess )
{
// The unsubscribe packet has been serialized successfully.
// The serialized packet is now ready to be sent to the broker.
}
MQTTStatus_t MQTT_SerializeUnsubscribe(const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, const MQTTPropBuilder_t *pUnsubscribeProperties, uint16_t packetId, uint32_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT UNSUBSCRIBE packet with properties in the given buffer.
Definition: core_mqtt_serializer.c:3820

◆ MQTT_GetPublishPacketSize()

MQTTStatus_t MQTT_GetPublishPacketSize ( const MQTTPublishInfo_t pPublishInfo,
const MQTTPropBuilder_t pPublishProperties,
uint32_t *  pRemainingLength,
uint32_t *  pPacketSize,
uint32_t  maxPacketSize 
)

Get the packet size and remaining length of an MQTT PUBLISH packet.

MQTT_ValidatePublishParams should be called with pPublishInfo before invoking this function to validate the publish parameters. This function must be called before sendPublishWithoutCopy in order to get the size of the MQTT PUBLISH packet that is generated from MQTTPublishInfo_t and optional publish properties. The remaining length returned in pRemainingLength and the packet size returned in pPacketSize are valid only if this function returns MQTTSuccess.

Parameters
[in]pPublishInfoMQTT PUBLISH packet parameters.
[in]pPublishPropertiesMQTT PUBLISH properties builder. Pass NULL if not used.
[out]pRemainingLengthThe Remaining Length of the MQTT PUBLISH packet.
[out]pPacketSizeThe total size of the MQTT PUBLISH packet.
[in]maxPacketSizeMaximum packet size allowed by the server.
Returns
MQTTBadParameter if the packet would exceed the size allowed by the MQTT spec or if invalid parameters are passed; MQTTSuccess otherwise.

Example

// Variables used in this example.
MQTTStatus_t status;
MQTTPublishInfo_t publishInfo = { 0 };
MQTTPropBuilder_t publishProperties = { 0 };
uint16_t topicAliasMax;
uint8_t retainAvailable;
uint8_t maxQos;
size_t remainingLength = 0, packetSize = 0;
// Initialize the publish info.
publishInfo.qos = MQTTQoS0;
publishInfo.pTopicName = "/some/topic/name";
publishInfo.topicNameLength = strlen( publishInfo.pTopicName );
publishInfo.pPayload = "Hello World!";
publishInfo.payloadLength = strlen( "Hello World!" );
// Initialize publish properties (if needed)
initializePublishProperties( &publishProperties );
// Validate publish parameters
status = MQTT_ValidatePublishParams(&publishInfo, topicAliasMax, retainAvailable, maxQos);
// Get the size requirement for the publish packet.
&publishInfo,
&publishProperties,
&remainingLength,
&packetSize,
maxPacketSize
);
if( status == MQTTSuccess )
{
// The publish packet can now be sent to the broker.
}
MQTTStatus_t MQTT_GetPublishPacketSize(const MQTTPublishInfo_t *pPublishInfo, const MQTTPropBuilder_t *pPublishProperties, uint32_t *pRemainingLength, uint32_t *pPacketSize, uint32_t maxPacketSize)
Get the packet size and remaining length of an MQTT PUBLISH packet.
Definition: core_mqtt_serializer.c:3889
MQTTStatus_t MQTT_ValidatePublishParams(const MQTTPublishInfo_t *pPublishInfo, uint8_t retainAvailable, uint8_t maxQos, uint16_t topicAlias, uint32_t maxPacketSize)
Validate the publish parameters present in the given publish structure pPublishInfo.
Definition: core_mqtt_serializer.c:5654
MQTTQoS_t qos
Quality of Service for message.
Definition: core_mqtt_serializer.h:399
size_t payloadLength
Message payload length.
Definition: core_mqtt_serializer.h:429
size_t topicNameLength
Length of topic name.
Definition: core_mqtt_serializer.h:419
const char * pTopicName
Topic name on which the message is published.
Definition: core_mqtt_serializer.h:414
const void * pPayload
Message payload.
Definition: core_mqtt_serializer.h:424

◆ MQTT_SerializePublish()

MQTTStatus_t MQTT_SerializePublish ( const MQTTPublishInfo_t pPublishInfo,
const MQTTPropBuilder_t pPublishProperties,
uint16_t  packetId,
uint32_t  remainingLength,
const MQTTFixedBuffer_t pFixedBuffer 
)

Serialize an MQTT PUBLISH packet in the given buffer.

This function will serialize complete MQTT PUBLISH packet into the given buffer. If the PUBLISH payload can be sent separately, consider using MQTT_SerializePublishHeader, which will serialize only the PUBLISH header into the buffer.

MQTT_GetPublishPacketSize should be called with pPublishInfo before invoking this function to get the size of the required MQTTFixedBuffer_t and remainingLength. The remainingLength must be the same as returned by MQTT_GetPublishPacketSize. The MQTTFixedBuffer_t must be at least as large as the size returned by MQTT_GetPublishPacketSize.

Parameters
[in]pPublishInfoMQTT PUBLISH packet parameters.
[in]pPublishPropertiesMQTT v5.0 properties for the PUBLISH packet. Can be NULL if no properties are needed.
[in]packetIdpacket ID generated by MQTT_GetPacketId.
[in]remainingLengthRemaining Length provided by MQTT_GetPublishPacketSize.
[out]pFixedBufferBuffer for packet serialization.
Returns
MQTTNoMemory if pFixedBuffer is too small to hold the MQTT packet; MQTTBadParameter if invalid parameters are passed; MQTTSuccess otherwise.

Example

// Variables used in this example.
MQTTStatus_t status;
MQTTPublishInfo_t publishInfo = { 0 };
MQTTPropBuilder_t publishProperties = { 0 };
MQTTFixedBuffer_t fixedBuffer;
uint8_t buffer[ BUFFER_SIZE ];
size_t remainingLength = 0, packetSize = 0;
uint16_t packetId;
fixedBuffer.pBuffer = buffer;
fixedBuffer.size = BUFFER_SIZE;
// A packet identifier is unused for QoS 0 publishes. Otherwise, a valid, unused packet
// identifier must be used.
packetId = 0;
// Assume publishInfo and publishProperties have been initialized. Get publish packet size.
&publishInfo, &publishProperties, &remainingLength, &packetSize
);
assert( status == MQTTSuccess );
assert( packetSize <= BUFFER_SIZE );
// Serialize the publish packet into the fixed buffer.
&publishInfo,
&publishProperties,
packetId,
remainingLength,
&fixedBuffer
);
if( status == MQTTSuccess )
{
// The publish packet can now be sent to the broker.
}
MQTTStatus_t MQTT_SerializePublish(const MQTTPublishInfo_t *pPublishInfo, const MQTTPropBuilder_t *pPublishProperties, uint16_t packetId, uint32_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT PUBLISH packet in the given buffer.
Definition: core_mqtt_serializer.c:3949

◆ MQTT_SerializePublishHeader()

MQTTStatus_t MQTT_SerializePublishHeader ( const MQTTPublishInfo_t pPublishInfo,
const MQTTPropBuilder_t pPublishProperties,
uint16_t  packetId,
uint32_t  remainingLength,
const MQTTFixedBuffer_t pFixedBuffer,
size_t *  pHeaderSize 
)

Serialize an MQTT PUBLISH packet header in the given buffer.

This function serializes PUBLISH header in to the given buffer. The payload for PUBLISH will not be copied over to the buffer. This will help reduce the memory needed for the buffer and avoid an unwanted copy operation of the PUBLISH payload into the buffer. If the payload also would need to be part of the serialized buffer, consider using MQTT_SerializePublish.

MQTT_GetPublishPacketSize should be called with pPublishInfo before invoking this function to get the size of the required MQTTFixedBuffer_t and remainingLength. The remainingLength must be the same as returned by MQTT_GetPublishPacketSize. The MQTTFixedBuffer_t must be at least as large as the size returned by MQTT_GetPublishPacketSize.

Parameters
[in]pPublishInfoMQTT PUBLISH packet parameters.
[in]pPublishPropertiesMQTT v5.0 properties for the PUBLISH packet. Can be NULL if no properties are needed.
[in]packetIdpacket ID generated by MQTT_GetPacketId.
[in]remainingLengthRemaining Length provided by MQTT_GetPublishPacketSize.
[out]pFixedBufferBuffer for packet serialization.
[out]pHeaderSizeSize of the serialized MQTT PUBLISH header.
Returns
MQTTNoMemory if pFixedBuffer is too small to hold the MQTT packet; MQTTBadParameter if invalid parameters are passed; MQTTSuccess otherwise.

Example

// Variables used in this example.
MQTTStatus_t status;
MQTTPublishInfo_t publishInfo = { 0 };
MQTTPropBuilder_t publishProperties ;
MQTTFixedBuffer_t fixedBuffer;
uint8_t buffer[ BUFFER_SIZE ];
size_t remainingLength = 0, packetSize = 0, headerSize = 0;
uint16_t packetId;
int32_t bytesSent;
uint32_t maxPacketSize = pContext->connectionProperties.serverMaxPacketSize;
fixedBuffer.pBuffer = buffer;
fixedBuffer.size = BUFFER_SIZE;
// A packet identifier is unused for QoS 0 publishes. Otherwise, a valid, unused packet
// identifier must be used.
packetId = 0;
// Assume publishInfo and publishProperties have been initialized. Get the publish packet size.
&publishInfo, &publishProperties, &remainingLength, &packetSize, maxPacketSize );
assert( status == MQTTSuccess );
// The payload will not be serialized, so the the fixed buffer does not need to hold it.
assert( ( packetSize - publishInfo.payloadLength ) <= BUFFER_SIZE );
// Serialize the publish packet header into the fixed buffer.
&publishInfo,
&publishProperties,
packetId,
remainingLength,
&fixedBuffer,
&headerSize
);
if( status == MQTTSuccess )
{
// The publish header and payload can now be sent to the broker.
// mqttSocket here is a socket descriptor created and connected to the MQTT
// broker outside of this function.
bytesSent = send( mqttSocket, ( void * ) fixedBuffer.pBuffer, headerSize, 0 );
assert( bytesSent == headerSize );
bytesSent = send( mqttSocket, publishInfo.pPayload, publishInfo.payloadLength, 0 );
assert( bytesSent == publishInfo.payloadLength );
}
MQTTStatus_t MQTT_SerializePublishHeader(const MQTTPublishInfo_t *pPublishInfo, const MQTTPropBuilder_t *pPublishProperties, uint16_t packetId, uint32_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer, size_t *pHeaderSize)
Serialize an MQTT PUBLISH packet header in the given buffer.
Definition: core_mqtt_serializer.c:4054

◆ MQTT_SerializeAck()

MQTTStatus_t MQTT_SerializeAck ( const MQTTFixedBuffer_t pFixedBuffer,
uint8_t  packetType,
uint16_t  packetId,
const MQTTPropBuilder_t pAckProperties,
const MQTTSuccessFailReasonCode_t pReasonCode 
)

Serialize an MQTT PUBACK, PUBREC, PUBREL, or PUBCOMP into the given buffer.

Parameters
[out]pFixedBufferBuffer for packet serialization.
[in]packetTypeByte of the corresponding packet fixed header per the MQTT spec.
[in]packetIdPacket ID of the publish.
[in]pAckPropertiesOptional properties to be added to the ACK packet.
[in]pReasonCodeOptional reason code to be added to the ACK packet.
Note
If any properties are provided to the function to be added to the ack packet, then a reason code must be provided as well.
Returns
MQTTBadParameter, MQTTNoMemory, or MQTTSuccess.

Example

// Variables used in this example.
MQTTStatus_t status;
MQTTFixedBuffer_t fixedBuffer;
uint8_t buffer[ BUFFER_SIZE ];
uint16_t packetId;
uint8_t packetType;
fixedBuffer.pBuffer = buffer;
fixedBuffer.size = BUFFER_SIZE;
// The fixed buffer must be large enough to hold 4 bytes.
assert( BUFFER_SIZE >= MQTT_PUBLISH_ACK_PACKET_SIZE );
// The packet ID must be the same as the original publish packet.
packetId = publishPacketId;
// The byte representing a packet of type ACK. This function accepts PUBACK, PUBREC, PUBREL, or PUBCOMP.
// Serialize the publish acknowledgment into the fixed buffer without any properties or reason code.
status = MQTT_SerializeAck( &fixedBuffer, packetType, packetId, NULL, NULL );
if( status == MQTTSuccess )
{
// The publish acknowledgment can now be sent to the broker.
}
MQTTStatus_t MQTT_SerializeAck(const MQTTFixedBuffer_t *pFixedBuffer, uint8_t packetType, uint16_t packetId, const MQTTPropBuilder_t *pAckProperties, const MQTTSuccessFailReasonCode_t *pReasonCode)
Serialize an MQTT PUBACK, PUBREC, PUBREL, or PUBCOMP into the given buffer.
Definition: core_mqtt_serializer.c:4263
#define MQTT_PUBLISH_ACK_PACKET_SIZE
The size of MQTT PUBACK, PUBREC, PUBREL, and PUBCOMP packets, per MQTT spec.
Definition: core_mqtt_serializer.h:74
#define MQTT_PACKET_TYPE_PUBACK
PUBACK (bidirectional).
Definition: core_mqtt_serializer.h:56

◆ MQTT_GetDisconnectPacketSize()

MQTTStatus_t MQTT_GetDisconnectPacketSize ( const MQTTPropBuilder_t pDisconnectProperties,
uint32_t *  pRemainingLength,
uint32_t *  pPacketSize,
uint32_t  maxPacketSize,
const MQTTSuccessFailReasonCode_t pReasonCode 
)

Get the size of an MQTT DISCONNECT packet.

Parameters
[in]pDisconnectPropertiesMQTT DISCONNECT properties builder. Pass NULL if not used.
[out]pRemainingLengthThe Remaining Length of the MQTT DISCONNECT packet.
[out]pPacketSizeThe size of the MQTT DISCONNECT packet.
[in]maxPacketSizeMaximum packet size allowed by the server.
[in]pReasonCodeThe reason code for the disconnect. Pass NULL if not used - only valid if the properties are NULL too.
Returns
MQTTSuccess, or MQTTBadParameter if parameters are invalid

Example

// Variables used in this example.
MQTTStatus_t status;
uint32_t remainingLength = 0;
uint32_t packetSize = 0;
uint32_t maxPacketSize;
MQTTPropBuilder_t disconnectProperties ;
//Set property builder. The details are out of scope for this example.
initializePropertyBuilder( &disconnectProperties );
//Set the parameters.
// Get the size requirement for the disconnect packet.
status = MQTT_GetDisconnectPacketSize( &disconnectProperties, &remainingLength, &packetSize, maxPacketSize, &reasonCode );
if( status == MQTTSuccess )
{
// Send the disconnect packet.
}
MQTTStatus_t MQTT_GetDisconnectPacketSize(const MQTTPropBuilder_t *pDisconnectProperties, uint32_t *pRemainingLength, uint32_t *pPacketSize, uint32_t maxPacketSize, const MQTTSuccessFailReasonCode_t *pReasonCode)
Get the size of an MQTT DISCONNECT packet.
Definition: core_mqtt_serializer.c:4333
MQTTSuccessFailReasonCode_t
MQTT reason codes.
Definition: core_mqtt_serializer.h:583
@ MQTT_REASON_DISCONNECT_NORMAL_DISCONNECTION
Definition: core_mqtt_serializer.h:662

◆ MQTT_SerializeDisconnect()

MQTTStatus_t MQTT_SerializeDisconnect ( const MQTTPropBuilder_t pDisconnectProperties,
const MQTTSuccessFailReasonCode_t pReasonCode,
uint32_t  remainingLength,
const MQTTFixedBuffer_t pFixedBuffer 
)

Serialize an MQTT DISCONNECT packet into the given buffer.

The input MQTTFixedBuffer_t.size must be at least as large as the size returned by MQTT_GetDisconnectPacketSize. This function should only be called after MQTT_GetDisconnectPacketSize to ensure proper buffer sizing.

Parameters
[in]pDisconnectPropertiesMQTT v5.0 properties for the DISCONNECT packet. Can be NULL if no properties are needed.
[in]pReasonCodeThe reason code for the disconnect. For MQTT v5.0, this indicates why the connection is being terminated. If this is NULL, then the pDisconnectProperties must be NULL as well.
[in]remainingLengthRemaining Length provided by MQTT_GetDisconnectPacketSize.
[out]pFixedBufferBuffer for packet serialization.
Returns
MQTTNoMemory if pFixedBuffer is too small to hold the MQTT packet; MQTTBadParameter if invalid parameters are passed; MQTTSuccess otherwise.

Example

// Variables used in this example.
MQTTStatus_t status;
MQTTFixedBuffer_t fixedBuffer;
MQTTPropBuilder_t disconnectProperties = { 0 };
uint8_t buffer[ BUFFER_SIZE ];
uint32_t remainingLength = 0, packetSize = 0;
fixedBuffer.pBuffer = buffer;
fixedBuffer.size = BUFFER_SIZE;
// Get the disconnect packet size.
status = MQTT_GetDisconnectPacketSize( &disconnectProperties,
&remainingLength,
&packetSize,
&reasonCode );
assert( status == MQTTSuccess );
assert( packetSize <= BUFFER_SIZE );
// Serialize the disconnect into the fixed buffer.
status = MQTT_SerializeDisconnect( &disconnectProperties,
&reasonCode,
remainingLength,
&fixedBuffer );
if( status == MQTTSuccess )
{
// The disconnect packet can now be sent to the broker.
}
MQTTStatus_t MQTT_SerializeDisconnect(const MQTTPropBuilder_t *pDisconnectProperties, const MQTTSuccessFailReasonCode_t *pReasonCode, uint32_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT DISCONNECT packet into the given buffer.
Definition: core_mqtt_serializer.c:4444
#define MQTT_MAX_REMAINING_LENGTH
Per the MQTT spec, the largest "Remaining Length" of an MQTT packet is this value,...
Definition: core_mqtt_serializer_private.h:303

◆ MQTT_GetPingreqPacketSize()

MQTTStatus_t MQTT_GetPingreqPacketSize ( uint32_t *  pPacketSize)

Get the size of an MQTT PINGREQ packet.

Parameters
[out]pPacketSizeThe size of the MQTT PINGREQ packet.
Returns
MQTTSuccess or MQTTBadParameter if pPacketSize is NULL.

Example

// Variables used in this example.
MQTTStatus_t status;
uint32_t packetSize = 0;
// Get the size requirement for the ping request packet.
status = MQTT_GetPingreqPacketSize( &packetSize );
assert( status == MQTTSuccess );
assert( packetSize == 2 );
// The application should allocate or use a static #MQTTFixedBuffer_t of
// size >= 2 to serialize the ping request.
MQTTStatus_t MQTT_GetPingreqPacketSize(uint32_t *pPacketSize)
Get the size of an MQTT PINGREQ packet.
Definition: core_mqtt_serializer.c:4530

◆ MQTT_SerializePingreq()

MQTTStatus_t MQTT_SerializePingreq ( const MQTTFixedBuffer_t pFixedBuffer)

Serialize an MQTT PINGREQ packet into the given buffer.

The input MQTTFixedBuffer_t.size must be at least as large as the size returned by MQTT_GetPingreqPacketSize.

Parameters
[out]pFixedBufferBuffer for packet serialization.
Returns
MQTTNoMemory if pFixedBuffer is too small to hold the MQTT packet; MQTTBadParameter if invalid parameters are passed; MQTTSuccess otherwise.

Example

// Variables used in this example.
MQTTStatus_t status;
MQTTFixedBuffer_t fixedBuffer;
uint8_t buffer[ BUFFER_SIZE ];
fixedBuffer.pBuffer = buffer;
fixedBuffer.size = BUFFER_SIZE;
// Get the ping request packet size.
status = MQTT_GetPingreqPacketSize( &packetSize );
assert( status == MQTTSuccess );
assert( packetSize <= BUFFER_SIZE );
// Serialize the ping request into the fixed buffer.
status = MQTT_SerializePingreq( &fixedBuffer );
if( status == MQTTSuccess )
{
// The ping request can now be sent to the broker.
}
MQTTStatus_t MQTT_SerializePingreq(const MQTTFixedBuffer_t *pFixedBuffer)
Serialize an MQTT PINGREQ packet into the given buffer.
Definition: core_mqtt_serializer.c:4550

◆ MQTT_DeserializePublish()

MQTTStatus_t MQTT_DeserializePublish ( const MQTTPacketInfo_t pIncomingPacket,
uint16_t *  pPacketId,
MQTTPublishInfo_t pPublishInfo,
MQTTPropBuilder_t propBuffer,
uint32_t  maxPacketSize,
uint16_t  topicAliasMax 
)

Deserialize an MQTT PUBLISH packet.

Parameters
[in]pIncomingPacketMQTTPacketInfo_t containing the buffer.
[out]pPacketIdThe packet ID obtained from the buffer.
[out]pPublishInfoStruct containing information about the publish.
[in]propBufferBuffer to hold the properties.
[in]maxPacketSizeMaximum packet size.
[in]topicAliasMaxMaximum topic alias specified in the CONNECT packet.
Returns

Example

// TransportRecv_t function for reading from the network.
int32_t socket_recv(
NetworkContext_t * pNetworkContext,
void * pBuffer,
size_t bytesToRecv
);
// Some context to be used with the above transport receive function.
NetworkContext_t networkContext;
// Other variables used in this example.
MQTTStatus_t status;
MQTTPacketInfo_t incomingPacket;
MQTTPublishInfo_t publishInfo = { 0 };
MQTTPropBuilder_t propBuffer ;
uint16_t packetId;
uint32_t maxPacketSize = pContext->connectionProperties.maxPacketSize;
uint16_t topicAliasMax = pContext->connectionProperties.topicAliasMax;
int32_t bytesRecvd;
// A buffer to hold remaining data of the incoming packet.
uint8_t buffer[ BUFFER_SIZE ];
// Populate all fields of the incoming packet.
socket_recv,
&networkContext,
&incomingPacket
);
assert( status == MQTTSuccess );
assert( incomingPacket.remainingLength <= BUFFER_SIZE );
bytesRecvd = socket_recv(
&networkContext,
( void * ) buffer,
incomingPacket.remainingLength
);
incomingPacket.pRemainingData = buffer;
// Deserialize the publish information if the incoming packet is a publish.
if( ( incomingPacket.type & 0xF0 ) == MQTT_PACKET_TYPE_PUBLISH )
{
status = MQTT_DeserializePublish( &incomingPacket, &packetId, &publishInfo,
&propBuffer, maxPacketSize, topicAliasMax );
if( status == MQTTSuccess )
{
// The deserialized publish information can now be used from `publishInfo`.
}
}
MQTTStatus_t MQTT_DeserializePublish(const MQTTPacketInfo_t *pIncomingPacket, uint16_t *pPacketId, MQTTPublishInfo_t *pPublishInfo, MQTTPropBuilder_t *propBuffer, uint32_t maxPacketSize, uint16_t topicAliasMax)
Deserialize an MQTT PUBLISH packet.
Definition: core_mqtt_serializer.c:4593
MQTTStatus_t MQTT_GetIncomingPacketTypeAndLength(TransportRecv_t readFunc, NetworkContext_t *pNetworkContext, MQTTPacketInfo_t *pIncomingPacket)
Extract the MQTT packet type and length from incoming packet.
Definition: core_mqtt_serializer.c:4827
#define MQTT_PACKET_TYPE_PUBLISH
PUBLISH (bidirectional).
Definition: core_mqtt_serializer.h:55
struct NetworkContext NetworkContext_t
The NetworkContext is an incomplete type. An implementation of this interface must define struct Netw...
Definition: transport_interface.h:192
MQTT incoming packet parameters.
Definition: core_mqtt_serializer.h:443
uint32_t remainingLength
Length of remaining serialized data.
Definition: core_mqtt_serializer.h:457
uint8_t type
Type of incoming MQTT packet.
Definition: core_mqtt_serializer.h:447
uint8_t * pRemainingData
Remaining serialized data in the MQTT packet.
Definition: core_mqtt_serializer.h:452

◆ MQTT_DeserializeConnAck()

MQTTStatus_t MQTT_DeserializeConnAck ( const MQTTPacketInfo_t pIncomingPacket,
bool *  pSessionPresent,
MQTTPropBuilder_t pPropBuffer,
MQTTConnectionProperties_t pConnectProperties 
)

Deserialize an MQTT CONNACK.

Parameters
[in]pIncomingPacketMQTTPacketInfo_t containing the buffer.
[out]pSessionPresentBoolean flag from a CONNACK indicating present session.
[out]pPropBufferStruct to store the deserialized acknowledgment properties. Will contain any MQTT v5.0 properties included in the ack packet.
[in,out]pConnectPropertiesStruct to store the deserialized connect/connack properties. This parameter cannot be NULL.
Returns
MQTTBadParameter, MQTTBadResponse, MQTTServerRefused, or MQTTSuccess.

◆ MQTT_DeserializeAck()

MQTTStatus_t MQTT_DeserializeAck ( const MQTTPacketInfo_t pIncomingPacket,
uint16_t *  pPacketId,
MQTTReasonCodeInfo_t pReasonCode,
MQTTPropBuilder_t pPropBuffer,
const MQTTConnectionProperties_t pConnectProperties 
)

Deserialize an MQTT PUBACK, PUBREC, PUBREL, PUBCOMP, SUBACK, UNSUBACK, or PINGRESP.

Parameters
[in]pIncomingPacketMQTTPacketInfo_t containing the buffer.
[out]pPacketIdThe packet ID obtained from the buffer.
[out]pReasonCodeStruct to store reason code(s) from the acknowledgment packet. Contains the success/failure status of the corresponding request.
[out]pPropBufferStruct to store the deserialized acknowledgment properties. Will contain any MQTT v5.0 properties included in the ack packet.
[in,out]pConnectPropertiesStruct to store the deserialized connect/connack properties.
Returns
Returns one of the following:

Example

// Variables used in this example.
MQTTStatus_t status;
MQTTPacketInfo_t incomingPacket;
uint16_t packetId;
MQTTReasonCodeInfo_t reasonCode ; // Can be set to NULL if the incoming packet is CONNACK or PINGRESP
MQTTPropBuilder_t propBuffer; // Can be set to NULL if the user does not want any incoming properties.
MQTTConnectionProperties_t connectionProperties = pContext->connectionProperties; // Cannot be set to NULL.
// Receive an incoming packet and populate all fields. The details are out of scope
// for this example.
receiveIncomingPacket(&incomingPacket);
// Deserialize ack information if the incoming packet is a publish ack.
status = MQTT_DeserializeAck( &incomingPacket,
&packetId,
&reasonCode,
&propBuffer,
&connectionProperties );
if(status == MQTTSuccess)
{
// Ack information is now available.
}
MQTTStatus_t MQTT_DeserializeAck(const MQTTPacketInfo_t *pIncomingPacket, uint16_t *pPacketId, MQTTReasonCodeInfo_t *pReasonCode, MQTTPropBuilder_t *pPropBuffer, const MQTTConnectionProperties_t *pConnectProperties)
Deserialize an MQTT PUBACK, PUBREC, PUBREL, PUBCOMP, SUBACK, UNSUBACK, or PINGRESP.
Definition: core_mqtt_serializer.c:4715
Struct to hold reason codes.
Definition: core_mqtt_serializer.h:482

◆ MQTT_GetIncomingPacketTypeAndLength()

MQTTStatus_t MQTT_GetIncomingPacketTypeAndLength ( TransportRecv_t  readFunc,
NetworkContext_t pNetworkContext,
MQTTPacketInfo_t pIncomingPacket 
)

Extract the MQTT packet type and length from incoming packet.

This function must be called for every incoming packet to retrieve the MQTTPacketInfo_t.type and MQTTPacketInfo_t.remainingLength. A MQTTPacketInfo_t is not valid until this routine has been invoked.

Parameters
[in]readFuncTransport layer read function pointer.
[in]pNetworkContextThe network context pointer provided by the application.
[out]pIncomingPacketPointer to MQTTPacketInfo_t structure. This is where type, remaining length and packet identifier are stored.
Returns
MQTTSuccess on successful extraction of type and length, MQTTBadParameter if pIncomingPacket is invalid, MQTTRecvFailed on transport receive failure, MQTTBadResponse if an invalid packet is read, and MQTTNoDataAvailable if there is nothing to read.

Example

// TransportRecv_t function for reading from the network.
int32_t socket_recv(
NetworkContext_t * pNetworkContext,
void * pBuffer,
size_t bytesToRecv
);
// Some context to be used with above transport receive function.
NetworkContext_t networkContext;
// Struct to hold the incoming packet information.
MQTTPacketInfo_t incomingPacket;
int32_t bytesRecvd;
// Buffer to hold the remaining data of the incoming packet.
uint8_t buffer[ BUFFER_SIZE ];
// Loop until data is available to be received.
do{
socket_recv,
&networkContext,
&incomingPacket
);
} while( status == MQTTNoDataAvailable );
assert( status == MQTTSuccess );
// Receive the rest of the incoming packet.
assert( incomingPacket.remainingLength <= BUFFER_SIZE );
bytesRecvd = socket_recv(
&networkContext,
( void * ) buffer,
incomingPacket.remainingLength
);
// Set the remaining data field.
incomingPacket.pRemainingData = buffer;
@ MQTTNoDataAvailable
Definition: core_mqtt_serializer.h:247

◆ MQTT_UpdateDuplicatePublishFlag()

MQTTStatus_t MQTT_UpdateDuplicatePublishFlag ( uint8_t *  pHeader,
bool  set 
)

Update the duplicate publish flag within the given header of the publish packet.

Parameters
[in]pHeaderThe buffer holding the header content
[in]setIf true then the flag will be set else cleared
Returns
MQTTSuccess on successful setting of the duplicate flag, MQTTBadParameter for invalid parameters

◆ MQTT_ProcessIncomingPacketTypeAndLength()

MQTTStatus_t MQTT_ProcessIncomingPacketTypeAndLength ( const uint8_t *  pBuffer,
const size_t *  pIndex,
MQTTPacketInfo_t pIncomingPacket 
)

Extract the MQTT packet type and length from incoming packet.

This function must be called for every incoming packet to retrieve the MQTTPacketInfo_t.type and MQTTPacketInfo_t.remainingLength. A MQTTPacketInfo_t is not valid until this routine has been invoked.

Parameters
[in]pBufferThe buffer holding the raw data to be processed
[in]pIndexPointer to the index within the buffer to marking the end of raw data available.
[out]pIncomingPacketStructure used to hold the fields of the incoming packet.
Returns
MQTTSuccess on successful extraction of type and length, MQTTBadParameter if pIncomingPacket is invalid, MQTTBadResponse if an invalid packet is read, and MQTTNoDataAvailable if there is nothing to read.

◆ MQTT_InitConnect()

MQTTStatus_t MQTT_InitConnect ( MQTTConnectionProperties_t pConnectProperties)

Initialize an MQTTConnectionProperties_t.

Note
This function initializes the connect properties to default values. This function should only be used if using only serializer functions throughout the connection. It is also important to only call this function before sending the connect packet.
Parameters
[in]pConnectPropertiesThe connect properties to initialize.
Returns

◆ MQTTPropertyBuilder_Init()

MQTTStatus_t MQTTPropertyBuilder_Init ( MQTTPropBuilder_t pPropertyBuilder,
uint8_t *  buffer,
size_t  length 
)

Initialize the property builder.

Parameters
[out]pPropertyBuilderProperty builder to initialize.
[in]bufferBuffer to store the properties.
[in]lengthLength of the buffer.
Returns

◆ MQTT_ValidateWillProperties()

MQTTStatus_t MQTT_ValidateWillProperties ( const MQTTPropBuilder_t pPropertyBuilder)

Validates the properties specified for WILL Properties in the MQTT CONNECT packet.

Parameters
[in]pPropertyBuilderPointer to the property builder structure containing will properties.
Returns
Returns one of the following:

◆ MQTT_ValidateConnectProperties()

MQTTStatus_t MQTT_ValidateConnectProperties ( const MQTTPropBuilder_t pPropertyBuilder,
bool *  isRequestProblemInfoSet,
uint32_t *  pPacketMaxSizeValue 
)

Validate the properties in a CONNECT packet.

Parameters
[in]pPropertyBuilderPointer to the property builder structure containing connect packet properties.
[out]isRequestProblemInfoSetWhether the request problem info field is set in the properties.
[out]pPacketMaxSizeValueOptional pointer to get the Maximum Packet Size from the properties. If not required, NULL can be passed.
Returns
Returns one of the following:

◆ MQTT_ValidateSubscribeProperties()

MQTTStatus_t MQTT_ValidateSubscribeProperties ( bool  isSubscriptionIdAvailable,
const MQTTPropBuilder_t propBuilder 
)

Validates the properties of a SUBSCRIBE packet.

This function validates the properties in the property builder for a SUBSCRIBE packet.

Parameters
[in]isSubscriptionIdAvailableBoolean indicating if subscription identifiers are supported.
[in]propBuilderPointer to the property builder structure.
Returns
Returns one of the following:

◆ MQTT_ValidatePublishProperties()

MQTTStatus_t MQTT_ValidatePublishProperties ( uint16_t  serverTopicAliasMax,
const MQTTPropBuilder_t propBuilder,
uint16_t *  topicAlias 
)

Validates the properties of a PUBLISH packet.

This function validates the properties in the property builder for a PUBLISH packet.

Parameters
[in]serverTopicAliasMaxMaximum topic alias value allowed by the server.
[in]propBuilderPointer to the property builder structure.
[out]topicAliasPointer to store the topic alias value if present.
Returns
Returns one of the following:

◆ MQTT_ValidatePublishParams()

MQTTStatus_t MQTT_ValidatePublishParams ( const MQTTPublishInfo_t pPublishInfo,
uint8_t  retainAvailable,
uint8_t  maxQos,
uint16_t  topicAlias,
uint32_t  maxPacketSize 
)

Validate the publish parameters present in the given publish structure pPublishInfo.

This function must be called before MQTT_GetPublishPacketSize in order to validate the publish parameters.

Parameters
[in]pPublishInfoMQTT publish packet parameters.
[in]retainAvailableWhether server allows retain or not.
[in]maxQosMaximum QoS supported by the server.
[in]topicAliasTopic alias in the PUBLISH packet.
[in]maxPacketSizeMaximum packet size allowed by the server.
Returns
MQTTBadParameter if invalid parameters are passed; MQTTSuccess otherwise.

Example

// Variables used in this example.
MQTTStatus_t status;
MQTTPublishInfo_t publishInfo = {0};
uint16_t topicAlias;
uint8_t retainAvailable;
uint8_t maxQos;
// Set in the CONNACK packet.
uint32_t maxPacketSize ;
//Set the publish info parameters.
//Validate the publish packet
status = MQTT_ValidatePublishParams(&publishInfo, retainAvailable, maxQos, topicAlias, maxPacketSize);
if( status == MQTTSuccess )
{
// Get the packet size and serialize the publish packet.
}

◆ MQTT_ValidatePublishAckProperties()

MQTTStatus_t MQTT_ValidatePublishAckProperties ( const MQTTPropBuilder_t pPropertyBuilder)

Validates the properties specified for an MQTT PUBLISH ACK packet.

Parameters
[in]pPropertyBuilderPointer to the property builder structure containing unsubscribe properties.
Returns
Returns one of the following:

◆ MQTT_GetAckPacketSize()

MQTTStatus_t MQTT_GetAckPacketSize ( uint32_t *  pRemainingLength,
uint32_t *  pPacketSize,
uint32_t  maxPacketSize,
size_t  ackPropertyLength 
)

Get the size of an outgoing PUBLISH ACK packet.

Note
If no reason code is sent and property length is zero then MQTT_SerializeAck can be used directly.
Parameters
[out]pRemainingLengthThe remaining length of the packet to be serialized.
[out]pPacketSizeThe size of the packet to be serialized.
[in]maxPacketSizeMaximum packet size allowed by the server.
[in]ackPropertyLengthThe length of the properties.
Returns
MQTTBadParameter if invalid parameters are passed; MQTTSuccess otherwise.

Example

// Variables used in this example.
MQTTStatus_t status;
MQTTFixedBuffer_t fixedBuffer;
uint8_t buffer[ BUFFER_SIZE ];
MQTTAckInfo_t ackInfo;
uint16_t sessionExpiry;
fixedBuffer.pBuffer = buffer;
fixedBuffer.size = BUFFER_SIZE;
// Variables used in this example.
MQTTStatus_t status;
size_t remainingLength =0;
size_t packetSize = 0;
size_t ackPropertyLength = 0;
uint32_t maxPacketSize;
//set the parameters.
// Get the size requirement for the ack packet.
status = MQTT_GetAckPacketSize(&remainingLength,&packetSize,maxPacketSize, ackPropertyLength);
}
MQTTStatus_t MQTT_GetAckPacketSize(uint32_t *pRemainingLength, uint32_t *pPacketSize, uint32_t maxPacketSize, size_t ackPropertyLength)
Get the size of an outgoing PUBLISH ACK packet.
Definition: core_mqtt_serializer.c:5775

◆ MQTT_ValidateDisconnectProperties()

MQTTStatus_t MQTT_ValidateDisconnectProperties ( uint32_t  connectSessionExpiry,
const MQTTPropBuilder_t pPropertyBuilder 
)

Validates the properties specified for an MQTT DISCONNECT packet.

Parameters
[in]connectSessionExpiryThe session expiry interval that was specified in the CONNECT packet. Used to validate that the DISCONNECT session expiry is not non-zero while connectSessionExpiry is zero.
[in]pPropertyBuilderPointer to the property builder structure containing subscribe properties.
Returns
Returns one of the following:

◆ MQTT_DeserializeDisconnect()

MQTTStatus_t MQTT_DeserializeDisconnect ( const MQTTPacketInfo_t pPacket,
uint32_t  maxPacketSize,
MQTTReasonCodeInfo_t pDisconnectInfo,
MQTTPropBuilder_t pPropBuffer 
)

Deserialize an MQTT Disconnect packet.

Parameters
[in]pPacketMQTTPacketInfo_t containing the buffer.
[in]maxPacketSizeMaximum packet size allowed by the client.
[out]pDisconnectInfoStruct containing disconnect reason code
[out]pPropBufferMQTTPropBuilder_t to store the deserialized properties.
Returns
MQTTBadParameter, MQTTBadResponse or MQTTSuccess.

Example

// Variables used in this example.
MQTTStatus_t status;
MQTTPacketInfo_t incomingPacket;
MQTTReasonCodeInfo_t disconnectInfo;
uint32_t maxPacketSize;
MQTTPropBuilder_t propBuffer; // Assume this is initialized properly
// Receive an incoming packet and populate all fields. The details are out of scope
// for this example.
receiveIncomingPacket( &incomingPacket );
// Deserialize disconnect information.
if( ( incomingPacket.type) == MQTT_PACKET_TYPE_DISCONNECT )
{
status = MQTT_DeserializeDisconnect(&incomingPacket,
maxPacketSize,
&disconnectInfo,
&propBuffer);
if( status == MQTTSuccess )
{
// Disconnect information is available.
}
}
MQTTStatus_t MQTT_DeserializeDisconnect(const MQTTPacketInfo_t *pPacket, uint32_t maxPacketSize, MQTTReasonCodeInfo_t *pDisconnectInfo, MQTTPropBuilder_t *pPropBuffer)
Deserialize an MQTT Disconnect packet.
Definition: core_mqtt_serializer.c:5919
#define MQTT_PACKET_TYPE_DISCONNECT
DISCONNECT (client-to-server).
Definition: core_mqtt_serializer.h:66