coreMQTT  v1.1.2
MQTT 3.1.1 Client Library
MQTT_SerializePublishHeader

Serialize an MQTT PUBLISH packet header in the given buffer.

uint16_t packetId,
size_t remainingLength,
const MQTTFixedBuffer_t * pFixedBuffer,
size_t * pHeaderSize );

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]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 };
MQTTFixedBuffer_t fixedBuffer;
uint8_t buffer[ BUFFER_SIZE ];
size_t remainingLength = 0, packetSize = 0, headerSize = 0;
uint16_t packetId;
int32_t bytesSent;
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 has been initialized. Get the publish packet size.
&publishInfo, &remainingLength, &packetSize
);
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,
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 );
}
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
MQTTFixedBuffer_t::pBuffer
uint8_t * pBuffer
Pointer to buffer.
Definition: core_mqtt_serializer.h:131
MQTTFixedBuffer_t
Buffer passed to MQTT library.
Definition: core_mqtt_serializer.h:130
MQTTPublishInfo_t::pPayload
const void * pPayload
Message payload.
Definition: core_mqtt_serializer.h:238
MQTTFixedBuffer_t::size
size_t size
Size of buffer.
Definition: core_mqtt_serializer.h:132
MQTTStatus_t
MQTTStatus_t
Return codes from MQTT functions.
Definition: core_mqtt_serializer.h:97
MQTT_SerializePublishHeader
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.
Definition: core_mqtt_serializer.c:1973
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