in source/core_mqtt_serializer.c [614:706]
static void serializePublishCommon( const MQTTPublishInfo_t * pPublishInfo,
size_t remainingLength,
uint16_t packetIdentifier,
const MQTTFixedBuffer_t * pFixedBuffer,
bool serializePayload )
{
uint8_t * pIndex = NULL;
const uint8_t * pPayloadBuffer = NULL;
/* The first byte of a PUBLISH packet contains the packet type and flags. */
uint8_t publishFlags = MQTT_PACKET_TYPE_PUBLISH;
assert( pPublishInfo != NULL );
assert( pFixedBuffer != NULL );
assert( pFixedBuffer->pBuffer != NULL );
/* Packet Id should be non zero for Qos 1 and Qos 2. */
assert( ( pPublishInfo->qos == MQTTQoS0 ) || ( packetIdentifier != 0U ) );
/* Duplicate flag should be set only for Qos 1 or Qos 2. */
assert( ( pPublishInfo->dup != true ) || ( pPublishInfo->qos != MQTTQoS0 ) );
/* Get the start address of the buffer. */
pIndex = pFixedBuffer->pBuffer;
if( pPublishInfo->qos == MQTTQoS1 )
{
LogDebug( ( "Adding QoS as QoS1 in PUBLISH flags." ) );
UINT8_SET_BIT( publishFlags, MQTT_PUBLISH_FLAG_QOS1 );
}
else if( pPublishInfo->qos == MQTTQoS2 )
{
LogDebug( ( "Adding QoS as QoS2 in PUBLISH flags." ) );
UINT8_SET_BIT( publishFlags, MQTT_PUBLISH_FLAG_QOS2 );
}
else
{
/* Empty else MISRA 15.7 */
}
if( pPublishInfo->retain == true )
{
LogDebug( ( "Adding retain bit in PUBLISH flags." ) );
UINT8_SET_BIT( publishFlags, MQTT_PUBLISH_FLAG_RETAIN );
}
if( pPublishInfo->dup == true )
{
LogDebug( ( "Adding dup bit in PUBLISH flags." ) );
UINT8_SET_BIT( publishFlags, MQTT_PUBLISH_FLAG_DUP );
}
*pIndex = publishFlags;
pIndex++;
/* The "Remaining length" is encoded from the second byte. */
pIndex = encodeRemainingLength( pIndex, remainingLength );
/* The topic name is placed after the "Remaining length". */
pIndex = encodeString( pIndex,
pPublishInfo->pTopicName,
pPublishInfo->topicNameLength );
/* A packet identifier is required for QoS 1 and 2 messages. */
if( pPublishInfo->qos > MQTTQoS0 )
{
LogDebug( ( "Adding packet Id in PUBLISH packet." ) );
/* Place the packet identifier into the PUBLISH packet. */
*pIndex = UINT16_HIGH_BYTE( packetIdentifier );
*( pIndex + 1 ) = UINT16_LOW_BYTE( packetIdentifier );
pIndex += 2;
}
/* The payload is placed after the packet identifier.
* Payload is copied over only if required by the flag serializePayload.
* This will help reduce an unnecessary copy of the payload into the buffer.
*/
if( ( pPublishInfo->payloadLength > 0U ) &&
( serializePayload == true ) )
{
LogDebug( ( "Copying PUBLISH payload of length =%lu to buffer",
( unsigned long ) pPublishInfo->payloadLength ) );
/* Typecast const void * typed payload buffer to const uint8_t *.
* This is to use same type buffers in memcpy. */
pPayloadBuffer = ( const uint8_t * ) pPublishInfo->pPayload;
( void ) memcpy( pIndex, pPayloadBuffer, pPublishInfo->payloadLength );
pIndex += pPublishInfo->payloadLength;
}
/* Ensure that the difference between the end and beginning of the buffer
* is less than the buffer size. */
assert( ( ( size_t ) ( pIndex - pFixedBuffer->pBuffer ) ) <= pFixedBuffer->size );
}