Implements the user-facing functions in core_mqtt_serializer.h. More...
#include <string.h>
#include <assert.h>
#include "core_mqtt_serializer.h"
#include "core_mqtt_config_defaults.h"
Macros | |
#define | MQTT_VERSION_3_1_1 ( ( uint8_t ) 4U ) |
MQTT protocol version 3.1.1. | |
#define | MQTT_PACKET_CONNECT_HEADER_SIZE ( 10UL ) |
Size of the fixed and variable header of a CONNECT packet. | |
#define | MQTT_CONNECT_FLAG_CLEAN ( 1 ) |
Clean session. | |
#define | MQTT_CONNECT_FLAG_WILL ( 2 ) |
Will present. | |
#define | MQTT_CONNECT_FLAG_WILL_QOS1 ( 3 ) |
Will QoS 1. | |
#define | MQTT_CONNECT_FLAG_WILL_QOS2 ( 4 ) |
Will QoS 2. | |
#define | MQTT_CONNECT_FLAG_WILL_RETAIN ( 5 ) |
Will retain. | |
#define | MQTT_CONNECT_FLAG_PASSWORD ( 6 ) |
Password present. | |
#define | MQTT_CONNECT_FLAG_USERNAME ( 7 ) |
User name present. | |
#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_DISCONNECT_PACKET_SIZE ( 2UL ) |
The size of MQTT DISCONNECT packets, per MQTT spec. | |
#define | MQTT_PACKET_PINGREQ_SIZE ( 2UL ) |
A PINGREQ packet is always 2 bytes in size, defined by MQTT 3.1.1 spec. | |
#define | MQTT_DISCONNECT_REMAINING_LENGTH ( ( uint8_t ) 0 ) |
The Remaining Length field of MQTT disconnect packets, per MQTT spec. | |
#define | MQTT_PACKET_CONNACK_REMAINING_LENGTH ( ( uint8_t ) 2U ) |
A CONNACK packet always has a "Remaining length" of 2. | |
#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 ) |
PUBACK, PUBREC, PUBREl, PUBCOMP, UNSUBACK Remaining length. | |
#define | MQTT_PACKET_PINGRESP_REMAINING_LENGTH ( 0U ) |
A PINGRESP packet always has a "Remaining length" of 0. | |
#define | MQTT_MAX_REMAINING_LENGTH ( 268435455UL ) |
Per the MQTT 3.1.1 spec, the largest "Remaining Length" of an MQTT packet is this value, 256 MB. | |
#define | UINT8_SET_BIT(x, position) ( ( x ) = ( uint8_t ) ( ( x ) | ( 0x01U << ( position ) ) ) ) |
Set a bit in an 8-bit unsigned integer. | |
#define | UINT8_CHECK_BIT(x, position) ( ( ( x ) & ( 0x01U << ( position ) ) ) == ( 0x01U << ( position ) ) ) |
Macro for checking if a bit is set in a 1-byte unsigned int. | |
#define | UINT16_HIGH_BYTE(x) ( ( uint8_t ) ( ( x ) >> 8 ) ) |
Get the high byte of a 16-bit unsigned integer. | |
#define | UINT16_LOW_BYTE(x) ( ( uint8_t ) ( ( x ) & 0x00ffU ) ) |
Get the low byte of a 16-bit unsigned integer. | |
#define | UINT16_DECODE(ptr) |
Macro for decoding a 2-byte unsigned int from a sequence of bytes. | |
#define | MQTT_REMAINING_LENGTH_INVALID ( ( size_t ) 268435456 ) |
A value that represents an invalid remaining length. | |
#define | MQTT_MIN_PUBLISH_REMAINING_LENGTH_QOS0 ( 3U ) |
The minimum remaining length for a QoS 0 PUBLISH. | |
Enumerations | |
enum | MQTTSubscriptionType_t { MQTT_SUBSCRIBE , MQTT_UNSUBSCRIBE } |
MQTT Subscription packet types. More... | |
Functions | |
static void | serializePublishCommon (const MQTTPublishInfo_t *pPublishInfo, size_t remainingLength, uint16_t packetIdentifier, const MQTTFixedBuffer_t *pFixedBuffer, bool serializePayload) |
Serializes MQTT PUBLISH packet into the buffer provided. | |
static bool | calculatePublishPacketSize (const MQTTPublishInfo_t *pPublishInfo, size_t *pRemainingLength, size_t *pPacketSize) |
Calculates the packet size and remaining length of an MQTT PUBLISH packet. | |
static MQTTStatus_t | calculateSubscriptionPacketSize (const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, size_t *pRemainingLength, size_t *pPacketSize, 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, size_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, size_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 uint8_t * | encodeRemainingLength (uint8_t *pDestination, size_t length) |
Encodes the remaining length of the packet using the variable length encoding scheme provided in the MQTT v3.1.1 specification. | |
static size_t | remainingLengthEncodedSize (size_t length) |
Retrieve the size of the remaining length if it were to be encoded. | |
static uint8_t * | encodeString (uint8_t *pDestination, const char *pSource, uint16_t sourceLength) |
Encode a string whose size is at maximum 16 bits in length. | |
static size_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 (size_t remainingLength, MQTTQoS_t qos, size_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 (const MQTTPacketInfo_t *pConnack, bool *pSessionPresent) |
Deserialize a CONNACK packet. | |
static MQTTStatus_t | readSubackStatus (size_t statusCount, const uint8_t *pStatusStart) |
Decode the status bytes of a SUBACK packet to a MQTTStatus_t. | |
static MQTTStatus_t | deserializeSuback (const MQTTPacketInfo_t *pSuback, uint16_t *pPacketIdentifier) |
Deserialize a SUBACK packet. | |
static MQTTStatus_t | deserializePublish (const MQTTPacketInfo_t *pIncomingPacket, uint16_t *pPacketId, MQTTPublishInfo_t *pPublishInfo) |
Deserialize a PUBLISH packet received from the server. | |
static MQTTStatus_t | deserializeSimpleAck (const MQTTPacketInfo_t *pAck, uint16_t *pPacketIdentifier) |
Deserialize an UNSUBACK, PUBACK, PUBREC, PUBREL, or PUBCOMP packet. | |
static MQTTStatus_t | deserializePingresp (const MQTTPacketInfo_t *pPingresp) |
Deserialize a PINGRESP packet. | |
MQTTStatus_t | MQTT_SerializePublishHeaderWithoutTopic (const MQTTPublishInfo_t *pPublishInfo, size_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. | |
uint8_t * | MQTT_SerializeConnectFixedHeader (uint8_t *pIndex, const MQTTConnectInfo_t *pConnectInfo, const MQTTPublishInfo_t *pWillInfo, size_t remainingLength) |
Serialize the fixed part of the connect packet header. | |
MQTTStatus_t | MQTT_GetConnectPacketSize (const MQTTConnectInfo_t *pConnectInfo, const MQTTPublishInfo_t *pWillInfo, size_t *pRemainingLength, size_t *pPacketSize) |
Get the size and Remaining Length of an MQTT CONNECT packet. | |
MQTTStatus_t | MQTT_SerializeConnect (const MQTTConnectInfo_t *pConnectInfo, const MQTTPublishInfo_t *pWillInfo, size_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, size_t *pRemainingLength, size_t *pPacketSize) |
Get packet size and Remaining Length of an MQTT SUBSCRIBE packet. | |
uint8_t * | MQTT_SerializeSubscribeHeader (size_t remainingLength, uint8_t *pIndex, uint16_t packetId) |
Serialize the fixed part of the subscribe packet header. | |
uint8_t * | MQTT_SerializeUnsubscribeHeader (size_t remainingLength, uint8_t *pIndex, uint16_t packetId) |
Serialize the fixed part of the unsubscribe packet header. | |
MQTTStatus_t | MQTT_SerializeSubscribe (const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, uint16_t packetId, size_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, size_t *pRemainingLength, size_t *pPacketSize) |
Get packet size and Remaining Length of an MQTT UNSUBSCRIBE packet. | |
MQTTStatus_t | MQTT_SerializeUnsubscribe (const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, uint16_t packetId, size_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer) |
Serialize an MQTT UNSUBSCRIBE packet in the given buffer. | |
MQTTStatus_t | MQTT_GetPublishPacketSize (const MQTTPublishInfo_t *pPublishInfo, size_t *pRemainingLength, size_t *pPacketSize) |
Get the packet size and remaining length of an MQTT PUBLISH packet. | |
MQTTStatus_t | MQTT_SerializePublish (const MQTTPublishInfo_t *pPublishInfo, uint16_t packetId, size_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer) |
Serialize an MQTT PUBLISH packet in the given buffer. | |
MQTTStatus_t | MQTT_SerializePublishHeader (const MQTTPublishInfo_t *pPublishInfo, uint16_t packetId, size_t remainingLength, const MQTTFixedBuffer_t *pFixedBuffer, size_t *pHeaderSize) |
Serialize an MQTT PUBLISH packet header in the given buffer. | |
MQTTStatus_t | MQTT_SerializeAck (const MQTTFixedBuffer_t *pFixedBuffer, uint8_t packetType, uint16_t packetId) |
Serialize an MQTT PUBACK, PUBREC, PUBREL, or PUBCOMP into the given buffer. | |
MQTTStatus_t | MQTT_GetDisconnectPacketSize (size_t *pPacketSize) |
Get the size of an MQTT DISCONNECT packet. | |
MQTTStatus_t | MQTT_SerializeDisconnect (const MQTTFixedBuffer_t *pFixedBuffer) |
Serialize an MQTT DISCONNECT packet into the given buffer. | |
MQTTStatus_t | MQTT_GetPingreqPacketSize (size_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) |
Deserialize an MQTT PUBLISH packet. | |
MQTTStatus_t | MQTT_DeserializeAck (const MQTTPacketInfo_t *pIncomingPacket, uint16_t *pPacketId, bool *pSessionPresent) |
Deserialize an MQTT CONNACK, SUBACK, UNSUBACK, PUBACK, PUBREC, PUBREL, PUBCOMP, 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_ProcessIncomingPacketTypeAndLength (const uint8_t *pBuffer, const size_t *pIndex, MQTTPacketInfo_t *pIncomingPacket) |
Extract the MQTT packet type and length from incoming packet. | |
Implements the user-facing functions in core_mqtt_serializer.h.
#define UINT8_CHECK_BIT | ( | x, | |
position | |||
) | ( ( ( x ) & ( 0x01U << ( position ) ) ) == ( 0x01U << ( position ) ) ) |
Macro for checking if a bit is set in a 1-byte unsigned int.
[in] | x | The unsigned int to check. |
[in] | position | Which bit to check. |
#define UINT16_DECODE | ( | ptr | ) |
Macro for decoding a 2-byte unsigned int from a sequence of bytes.
[in] | ptr | A uint8_t* that points to the high byte. |
#define MQTT_REMAINING_LENGTH_INVALID ( ( size_t ) 268435456 ) |
A value that represents an invalid remaining length.
This value is greater than what is allowed by the MQTT specification.
#define MQTT_MIN_PUBLISH_REMAINING_LENGTH_QOS0 ( 3U ) |
The minimum remaining length for a QoS 0 PUBLISH.
Includes two bytes for topic name length and one byte for topic name.
|
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.
param[in] pPublishInfo Publish information.
param[in] remainingLength Remaining length of the PUBLISH packet.
param[in] packetIdentifier Packet identifier of PUBLISH packet.
param[in, out] pFixedBuffer Buffer to which PUBLISH packet will be serialized.
param[in] serializePayload Copy payload to the serialized buffer only if true. Only PUBLISH header will be serialized if false.
|
static |
Calculates the packet size and remaining length of an MQTT PUBLISH packet.
[in] | pPublishInfo | MQTT PUBLISH packet parameters. |
[out] | pRemainingLength | The Remaining Length of the MQTT PUBLISH packet. |
[out] | pPacketSize | The total size of the MQTT PUBLISH packet. |
|
static |
Calculates the packet size and remaining length of an MQTT SUBSCRIBE or UNSUBSCRIBE packet.
[in] | pSubscriptionList | List of MQTT subscription info. |
[in] | subscriptionCount | The number of elements in pSubscriptionList. |
[out] | pRemainingLength | The Remaining Length of the MQTT SUBSCRIBE or UNSUBSCRIBE packet. |
[out] | pPacketSize | The total size of the MQTT MQTT SUBSCRIBE or UNSUBSCRIBE packet. |
[in] | subscriptionType | MQTT_SUBSCRIBE or MQTT_UNSUBSCRIBE. |
MQTTBadParameter if the packet would exceed the size allowed by the MQTT spec or a subscription is empty; MQTTSuccess otherwise.
|
static |
Validates parameters of MQTT_SerializeSubscribe or MQTT_SerializeUnsubscribe.
[in] | pSubscriptionList | List of MQTT subscription info. |
[in] | subscriptionCount | The number of elements in pSubscriptionList. |
[in] | packetId | Packet identifier. |
[in] | remainingLength | Remaining length of the packet. |
[in] | pFixedBuffer | Buffer for packet serialization. |
|
static |
Serialize an MQTT CONNECT packet in the given buffer.
[in] | pConnectInfo | MQTT CONNECT packet parameters. |
[in] | pWillInfo | Last Will and Testament. Pass NULL if not used. |
[in] | remainingLength | Remaining Length of MQTT CONNECT packet. |
[out] | pFixedBuffer | Buffer for packet serialization. |
|
static |
Prints the appropriate message for the CONNACK response code if logs are enabled.
[in] | responseCode | MQTT standard CONNACK response code. |
|
static |
Encodes the remaining length of the packet using the variable length encoding scheme provided in the MQTT v3.1.1 specification.
[out] | pDestination | The destination buffer to store the encoded remaining length. |
[in] | length | The remaining length to encode. |
|
static |
Retrieve the size of the remaining length if it were to be encoded.
[in] | length | The remaining length to be encoded. |
|
static |
Encode a string whose size is at maximum 16 bits in length.
[out] | pDestination | Destination buffer for the encoding. |
[in] | pSource | The source string to encode. |
[in] | sourceLength | The length of the source string to encode. |
|
static |
Retrieves and decodes the Remaining Length from the network interface by reading a single byte at a time.
[in] | recvFunc | Network interface receive function. |
[in] | pNetworkContext | Network interface context to the receive function. |
|
static |
Retrieves, decodes and stores the Remaining Length from the network interface by reading a single byte at a time.
[in] | pBuffer | The buffer holding the raw data to be processed |
[in] | pIndex | Pointer to the index within the buffer to marking the end of raw data available. |
[in] | pIncomingPacket | Structure used to hold the fields of the incoming packet. |
|
static |
Check if an incoming packet type is valid.
[in] | packetType | The packet type to check. |
true
if the packet type is valid; false
otherwise.
|
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.
[in] | remainingLength | Remaining length of the PUBLISH packet. |
[in] | qos | The QoS of the PUBLISH. |
[in] | qos0Minimum | Minimum possible remaining length for a QoS 0 PUBLISH. |
|
static |
Process the flags of an incoming PUBLISH packet.
[in] | publishFlags | Flags of an incoming PUBLISH. |
[in,out] | pPublishInfo | Pointer to MQTTPublishInfo_t struct where output will be written. |
|
static |
Deserialize a CONNACK packet.
Converts the packet from a stream of bytes to an MQTTStatus_t.
[in] | pConnack | Pointer to an MQTT packet struct representing a CONNACK. |
[out] | pSessionPresent | Whether a previous session was present. |
|
static |
Decode the status bytes of a SUBACK packet to a MQTTStatus_t.
[in] | statusCount | Number of status bytes in the SUBACK. |
[in] | pStatusStart | The first status byte in the SUBACK. |
|
static |
Deserialize a SUBACK packet.
Converts the packet from a stream of bytes to an MQTTStatus_t and extracts the packet identifier.
[in] | pSuback | Pointer to an MQTT packet struct representing a SUBACK. |
[out] | pPacketIdentifier | Packet ID of the SUBACK. |
|
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.
[in] | pIncomingPacket | Pointer to an MQTT packet struct representing a PUBLISH. |
[out] | pPacketId | Packet identifier of the PUBLISH. |
[out] | pPublishInfo | Pointer to MQTTPublishInfo_t where output is written. |
|
static |
Deserialize an UNSUBACK, PUBACK, PUBREC, PUBREL, or PUBCOMP packet.
Converts the packet from a stream of bytes to an MQTTStatus_t and extracts the packet identifier.
[in] | pAck | Pointer to the MQTT packet structure representing the packet. |
[out] | pPacketIdentifier | Packet ID of the ack type packet. |
|
static |
Deserialize a PINGRESP packet.
Converts the packet from a stream of bytes to an MQTTStatus_t.
[in] | pPingresp | Pointer to an MQTT packet struct representing a PINGRESP. |
MQTTStatus_t MQTT_SerializePublishHeaderWithoutTopic | ( | const MQTTPublishInfo_t * | pPublishInfo, |
size_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.
[in] | pPublishInfo | MQTT PUBLISH packet parameters. |
[in] | remainingLength | Remaining Length provided by MQTT_GetPublishPacketSize. |
[out] | pBuffer | Buffer for packet serialization. |
[out] | headerSize | Size of the serialized MQTT PUBLISH header. |
uint8_t * MQTT_SerializeConnectFixedHeader | ( | uint8_t * | pIndex, |
const MQTTConnectInfo_t * | pConnectInfo, | ||
const MQTTPublishInfo_t * | pWillInfo, | ||
size_t | remainingLength | ||
) |
Serialize the fixed part of the connect packet header.
[out] | pIndex | Pointer to the buffer where the header is to be serialized. |
[in] | pConnectInfo | The connect information. |
[in] | pWillInfo | The last will and testament information. |
[in] | remainingLength | The remaining length of the packet to be serialized. |
MQTTStatus_t MQTT_GetConnectPacketSize | ( | const MQTTConnectInfo_t * | pConnectInfo, |
const MQTTPublishInfo_t * | pWillInfo, | ||
size_t * | pRemainingLength, | ||
size_t * | pPacketSize | ||
) |
Get the size and Remaining Length of an MQTT 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 and optional MQTTPublishInfo_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.
[in] | pConnectInfo | MQTT CONNECT packet parameters. |
[in] | pWillInfo | Last Will and Testament. Pass NULL if not used. |
[out] | pRemainingLength | The Remaining Length of the MQTT CONNECT packet. |
[out] | pPacketSize | The total size of the MQTT CONNECT packet. |
Example
MQTTStatus_t MQTT_SerializeConnect | ( | const MQTTConnectInfo_t * | pConnectInfo, |
const MQTTPublishInfo_t * | pWillInfo, | ||
size_t | remainingLength, | ||
const MQTTFixedBuffer_t * | pFixedBuffer | ||
) |
Serialize an MQTT CONNECT packet in the given fixed buffer pFixedBuffer
.
MQTT_GetConnectPacketSize should be called with pConnectInfo
and pWillInfo
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.
[in] | pConnectInfo | MQTT CONNECT packet parameters. |
[in] | pWillInfo | Last Will and Testament. Pass NULL if not used. |
[in] | remainingLength | Remaining Length provided by MQTT_GetConnectPacketSize. |
[out] | pFixedBuffer | Buffer for packet serialization. |
Example
MQTTStatus_t MQTT_GetSubscribePacketSize | ( | const MQTTSubscribeInfo_t * | pSubscriptionList, |
size_t | subscriptionCount, | ||
size_t * | pRemainingLength, | ||
size_t * | pPacketSize | ||
) |
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. 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.
[in] | pSubscriptionList | List of MQTT subscription info. |
[in] | subscriptionCount | The number of elements in pSubscriptionList. |
[out] | pRemainingLength | The Remaining Length of the MQTT SUBSCRIBE packet. |
[out] | pPacketSize | The total size of the MQTT SUBSCRIBE packet. |
Example
uint8_t * MQTT_SerializeSubscribeHeader | ( | size_t | remainingLength, |
uint8_t * | pIndex, | ||
uint16_t | packetId | ||
) |
Serialize the fixed part of the subscribe packet header.
[in] | remainingLength | The remaining length of the packet to be serialized. |
[in] | pIndex | Pointer to the buffer where the header is to be serialized. |
[in] | packetId | The packet ID to be serialized. |
uint8_t * MQTT_SerializeUnsubscribeHeader | ( | size_t | remainingLength, |
uint8_t * | pIndex, | ||
uint16_t | packetId | ||
) |
Serialize the fixed part of the unsubscribe packet header.
[in] | remainingLength | The remaining length of the packet to be serialized. |
[in] | pIndex | Pointer to the buffer where the header is to be serialized. |
[in] | packetId | The packet ID to be serialized. |
MQTTStatus_t MQTT_SerializeSubscribe | ( | const MQTTSubscribeInfo_t * | pSubscriptionList, |
size_t | subscriptionCount, | ||
uint16_t | packetId, | ||
size_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.
[in] | pSubscriptionList | List of MQTT subscription info. |
[in] | subscriptionCount | The number of elements in pSubscriptionList. |
[in] | packetId | packet ID generated by MQTT_GetPacketId. |
[in] | remainingLength | Remaining Length provided by MQTT_GetSubscribePacketSize. |
[out] | pFixedBuffer | Buffer for packet serialization. |
Example
MQTTStatus_t MQTT_GetUnsubscribePacketSize | ( | const MQTTSubscribeInfo_t * | pSubscriptionList, |
size_t | subscriptionCount, | ||
size_t * | pRemainingLength, | ||
size_t * | pPacketSize | ||
) |
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. 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.
[in] | pSubscriptionList | List of MQTT subscription info. |
[in] | subscriptionCount | The number of elements in pSubscriptionList. |
[out] | pRemainingLength | The Remaining Length of the MQTT UNSUBSCRIBE packet. |
[out] | pPacketSize | The total size of the MQTT UNSUBSCRIBE packet. |
Example
MQTTStatus_t MQTT_SerializeUnsubscribe | ( | const MQTTSubscribeInfo_t * | pSubscriptionList, |
size_t | subscriptionCount, | ||
uint16_t | packetId, | ||
size_t | remainingLength, | ||
const MQTTFixedBuffer_t * | pFixedBuffer | ||
) |
Serialize an MQTT UNSUBSCRIBE packet in the given buffer.
MQTT_GetUnsubscribePacketSize 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_GetUnsubscribePacketSize. The MQTTFixedBuffer_t must be at least as large as the size returned by MQTT_GetUnsubscribePacketSize.
[in] | pSubscriptionList | List of MQTT subscription info. |
[in] | subscriptionCount | The number of elements in pSubscriptionList. |
[in] | packetId | packet ID generated by MQTT_GetPacketId. |
[in] | remainingLength | Remaining Length provided by MQTT_GetUnsubscribePacketSize. |
[out] | pFixedBuffer | Buffer for packet serialization. |
Example
MQTTStatus_t MQTT_GetPublishPacketSize | ( | const MQTTPublishInfo_t * | pPublishInfo, |
size_t * | pRemainingLength, | ||
size_t * | pPacketSize | ||
) |
Get the packet size and remaining length of an MQTT PUBLISH packet.
This function must be called before MQTT_SerializePublish in order to get the size of the MQTT PUBLISH packet that is generated from MQTTPublishInfo_t. The size of the MQTTFixedBuffer_t supplied to MQTT_SerializePublish must be at least pPacketSize
. The provided pPublishInfo
is valid for serialization with MQTT_SerializePublish 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.
[in] | pPublishInfo | MQTT PUBLISH packet parameters. |
[out] | pRemainingLength | The Remaining Length of the MQTT PUBLISH packet. |
[out] | pPacketSize | The total size of the MQTT PUBLISH packet. |
Example
MQTTStatus_t MQTT_SerializePublish | ( | const MQTTPublishInfo_t * | pPublishInfo, |
uint16_t | packetId, | ||
size_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.
[in] | pPublishInfo | MQTT PUBLISH packet parameters. |
[in] | packetId | packet ID generated by MQTT_GetPacketId. |
[in] | remainingLength | Remaining Length provided by MQTT_GetPublishPacketSize. |
[out] | pFixedBuffer | Buffer for packet serialization. |
Example
MQTTStatus_t MQTT_SerializePublishHeader | ( | const MQTTPublishInfo_t * | pPublishInfo, |
uint16_t | packetId, | ||
size_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.
[in] | pPublishInfo | MQTT PUBLISH packet parameters. |
[in] | packetId | packet ID generated by MQTT_GetPacketId. |
[in] | remainingLength | Remaining Length provided by MQTT_GetPublishPacketSize. |
[out] | pFixedBuffer | Buffer for packet serialization. |
[out] | pHeaderSize | Size of the serialized MQTT PUBLISH header. |
Example
MQTTStatus_t MQTT_SerializeAck | ( | const MQTTFixedBuffer_t * | pFixedBuffer, |
uint8_t | packetType, | ||
uint16_t | packetId | ||
) |
Serialize an MQTT PUBACK, PUBREC, PUBREL, or PUBCOMP into the given buffer.
[out] | pFixedBuffer | Buffer for packet serialization. |
[in] | packetType | Byte of the corresponding packet fixed header per the MQTT spec. |
[in] | packetId | Packet ID of the publish. |
Example
MQTTStatus_t MQTT_GetDisconnectPacketSize | ( | size_t * | pPacketSize | ) |
Get the size of an MQTT DISCONNECT packet.
[out] | pPacketSize | The size of the MQTT DISCONNECT packet. |
pPacketSize
is NULL.Example
MQTTStatus_t MQTT_SerializeDisconnect | ( | 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.
[out] | pFixedBuffer | Buffer for packet serialization. |
Example
MQTTStatus_t MQTT_GetPingreqPacketSize | ( | size_t * | pPacketSize | ) |
Get the size of an MQTT PINGREQ packet.
[out] | pPacketSize | The size of the MQTT PINGREQ packet. |
Example
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.
[out] | pFixedBuffer | Buffer for packet serialization. |
Example
MQTTStatus_t MQTT_DeserializePublish | ( | const MQTTPacketInfo_t * | pIncomingPacket, |
uint16_t * | pPacketId, | ||
MQTTPublishInfo_t * | pPublishInfo | ||
) |
Deserialize an MQTT PUBLISH packet.
[in] | pIncomingPacket | MQTTPacketInfo_t containing the buffer. |
[out] | pPacketId | The packet ID obtained from the buffer. |
[out] | pPublishInfo | Struct containing information about the publish. |
Example
MQTTStatus_t MQTT_DeserializeAck | ( | const MQTTPacketInfo_t * | pIncomingPacket, |
uint16_t * | pPacketId, | ||
bool * | pSessionPresent | ||
) |
Deserialize an MQTT CONNACK, SUBACK, UNSUBACK, PUBACK, PUBREC, PUBREL, PUBCOMP, or PINGRESP.
[in] | pIncomingPacket | MQTTPacketInfo_t containing the buffer. |
[out] | pPacketId | The packet ID of obtained from the buffer. Not used in CONNACK or PINGRESP. |
[out] | pSessionPresent | Boolean flag from a CONNACK indicating present session. |
Example
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.
[in] | readFunc | Transport layer read function pointer. |
[in] | pNetworkContext | The network context pointer provided by the application. |
[out] | pIncomingPacket | Pointer to MQTTPacketInfo_t structure. This is where type, remaining length and packet identifier are stored. |
pIncomingPacket
is invalid, MQTTRecvFailed on transport receive failure, MQTTBadResponse if an invalid packet is read, and MQTTNoDataAvailable if there is nothing to read.Example
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.
[in] | pBuffer | The buffer holding the raw data to be processed |
[in] | pIndex | Pointer to the index within the buffer to marking the end of raw data available. |
[out] | pIncomingPacket | Structure used to hold the fields of the incoming packet. |
pIncomingPacket
is invalid, MQTTBadResponse if an invalid packet is read, and MQTTNoDataAvailable if there is nothing to read.