User-facing functions for serializing and deserializing MQTT 3.1.1 packets. This header should be included for building a lighter weight MQTT client than the managed CSDK MQTT library API in core_mqtt.h, by using the serializer and de-serializer functions exposed in this file's API. More...
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include "core_mqtt_config.h"
#include "core_mqtt_config_defaults.h"
#include "transport_interface.h"
Go to the source code of this file.
Data Structures | |
struct | MQTTFixedBuffer_t |
Buffer passed to MQTT library. More... | |
struct | MQTTConnectInfo_t |
MQTT CONNECT packet parameters. More... | |
struct | MQTTSubscribeInfo_t |
MQTT SUBSCRIBE packet parameters. More... | |
struct | MQTTPublishInfo_t |
MQTT PUBLISH packet parameters. More... | |
struct | MQTTPacketInfo_t |
MQTT incoming packet parameters. More... | |
Enumerations | |
enum | MQTTStatus_t { MQTTSuccess = 0 , MQTTBadParameter , MQTTNoMemory , MQTTSendFailed , MQTTRecvFailed , MQTTBadResponse , MQTTServerRefused , MQTTNoDataAvailable , MQTTIllegalState , MQTTStateCollision , MQTTKeepAliveTimeout , MQTTNeedMoreBytes } |
Return codes from MQTT functions. More... | |
enum | MQTTQoS_t { MQTTQoS0 = 0 , MQTTQoS1 = 1 , MQTTQoS2 = 2 } |
MQTT Quality of Service values. More... | |
Functions | |
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. More... | |
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 . More... | |
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. More... | |
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. More... | |
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. More... | |
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. More... | |
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. More... | |
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. More... | |
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. More... | |
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. More... | |
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. More... | |
MQTTStatus_t | MQTT_GetDisconnectPacketSize (size_t *pPacketSize) |
Get the size of an MQTT DISCONNECT packet. More... | |
MQTTStatus_t | MQTT_SerializeDisconnect (const MQTTFixedBuffer_t *pFixedBuffer) |
Serialize an MQTT DISCONNECT packet into the given buffer. More... | |
MQTTStatus_t | MQTT_GetPingreqPacketSize (size_t *pPacketSize) |
Get the size of an MQTT PINGREQ packet. More... | |
MQTTStatus_t | MQTT_SerializePingreq (const MQTTFixedBuffer_t *pFixedBuffer) |
Serialize an MQTT PINGREQ packet into the given buffer. More... | |
MQTTStatus_t | MQTT_DeserializePublish (const MQTTPacketInfo_t *pIncomingPacket, uint16_t *pPacketId, MQTTPublishInfo_t *pPublishInfo) |
Deserialize an MQTT PUBLISH packet. More... | |
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. More... | |
MQTTStatus_t | MQTT_GetIncomingPacketTypeAndLength (TransportRecv_t readFunc, NetworkContext_t *pNetworkContext, MQTTPacketInfo_t *pIncomingPacket) |
Extract the MQTT packet type and length from incoming packet. More... | |
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. More... | |
User-facing functions for serializing and deserializing MQTT 3.1.1 packets. This header should be included for building a lighter weight MQTT client than the managed CSDK MQTT library API in core_mqtt.h, by using the serializer and de-serializer functions exposed in this file's API.
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
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_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. |
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.