coreMQTT  v1.1.2
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 );

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.
}
MQTTPublishInfo_t::topicNameLength
uint16_t topicNameLength
Length of topic name.
Definition: core_mqtt_serializer.h:233
MQTT_GetPublishPacketSize
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:1847
MQTTPublishInfo_t::pPayload
const void * pPayload
Message payload.
Definition: core_mqtt_serializer.h:238
MQTTStatus_t
MQTTStatus_t
Return codes from MQTT functions.
Definition: core_mqtt_serializer.h:97
MQTTPublishInfo_t::pTopicName
const char * pTopicName
Topic name on which the message is published.
Definition: core_mqtt_serializer.h:228
MQTTPublishInfo_t::qos
MQTTQoS_t qos
Quality of Service for message.
Definition: core_mqtt_serializer.h:213
MQTTSuccess
@ MQTTSuccess
Definition: core_mqtt_serializer.h:98
MQTTPublishInfo_t
MQTT PUBLISH packet parameters.
Definition: core_mqtt_serializer.h:209
MQTTPublishInfo_t::payloadLength
size_t payloadLength
Message payload length.
Definition: core_mqtt_serializer.h:243
MQTTQoS0
@ MQTTQoS0
Definition: core_mqtt_serializer.h:117