coreMQTT v2.1.1
MQTT 3.1.1 Client Library
MQTT_GetPublishPacketSize

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

size_t * pRemainingLength,
size_t * pPacketSize );
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.
Definition: core_mqtt_serializer.c:2061
MQTTStatus_t
Return codes from MQTT functions.
Definition: core_mqtt_serializer.h:99
MQTT PUBLISH packet parameters.
Definition: core_mqtt_serializer.h:214

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.

Parameters
[in]pPublishInfoMQTT PUBLISH packet parameters.
[out]pRemainingLengthThe Remaining Length of the MQTT PUBLISH packet.
[out]pPacketSizeThe total size of the MQTT PUBLISH packet.
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 };
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!" );
// Get the size requirement for the publish packet.
&publishInfo, &remainingLength, &packetSize
);
if( status == MQTTSuccess )
{
// The application should allocate or use a static #MQTTFixedBuffer_t
// of size >= packetSize to serialize the publish.
}
@ MQTTSuccess
Definition: core_mqtt_serializer.h:100
@ MQTTQoS0
Definition: core_mqtt_serializer.h:122
MQTTQoS_t qos
Quality of Service for message.
Definition: core_mqtt_serializer.h:218
uint16_t topicNameLength
Length of topic name.
Definition: core_mqtt_serializer.h:238
size_t payloadLength
Message payload length.
Definition: core_mqtt_serializer.h:248
const char * pTopicName
Topic name on which the message is published.
Definition: core_mqtt_serializer.h:233
const void * pPayload
Message payload.
Definition: core_mqtt_serializer.h:243