in source/core_mqtt_serializer.c [1393:1508]
static void serializeConnectPacket( const MQTTConnectInfo_t * pConnectInfo,
const MQTTPublishInfo_t * pWillInfo,
size_t remainingLength,
const MQTTFixedBuffer_t * pFixedBuffer )
{
uint8_t connectFlags = 0U;
uint8_t * pIndex = NULL;
assert( pConnectInfo != NULL );
assert( pFixedBuffer != NULL );
assert( pFixedBuffer->pBuffer != NULL );
pIndex = pFixedBuffer->pBuffer;
/* The first byte in the CONNECT packet is the control packet type. */
*pIndex = MQTT_PACKET_TYPE_CONNECT;
pIndex++;
/* The remaining length of the CONNECT packet is encoded starting from the
* second byte. The remaining length does not include the length of the fixed
* header or the encoding of the remaining length. */
pIndex = encodeRemainingLength( pIndex, remainingLength );
/* The string "MQTT" is placed at the beginning of the CONNECT packet's variable
* header. This string is 4 bytes long. */
pIndex = encodeString( pIndex, "MQTT", 4 );
/* The MQTT protocol version is the second field of the variable header. */
*pIndex = MQTT_VERSION_3_1_1;
pIndex++;
/* Set the clean session flag if needed. */
if( pConnectInfo->cleanSession == true )
{
UINT8_SET_BIT( connectFlags, MQTT_CONNECT_FLAG_CLEAN );
}
/* Set the flags for username and password if provided. */
if( pConnectInfo->pUserName != NULL )
{
UINT8_SET_BIT( connectFlags, MQTT_CONNECT_FLAG_USERNAME );
}
if( pConnectInfo->pPassword != NULL )
{
UINT8_SET_BIT( connectFlags, MQTT_CONNECT_FLAG_PASSWORD );
}
/* Set will flag if a Last Will and Testament is provided. */
if( pWillInfo != NULL )
{
UINT8_SET_BIT( connectFlags, MQTT_CONNECT_FLAG_WILL );
/* Flags only need to be changed for Will QoS 1 or 2. */
if( pWillInfo->qos == MQTTQoS1 )
{
UINT8_SET_BIT( connectFlags, MQTT_CONNECT_FLAG_WILL_QOS1 );
}
else if( pWillInfo->qos == MQTTQoS2 )
{
UINT8_SET_BIT( connectFlags, MQTT_CONNECT_FLAG_WILL_QOS2 );
}
else
{
/* Empty else MISRA 15.7 */
}
if( pWillInfo->retain == true )
{
UINT8_SET_BIT( connectFlags, MQTT_CONNECT_FLAG_WILL_RETAIN );
}
}
*pIndex = connectFlags;
pIndex++;
/* Write the 2 bytes of the keep alive interval into the CONNECT packet. */
*pIndex = UINT16_HIGH_BYTE( pConnectInfo->keepAliveSeconds );
*( pIndex + 1 ) = UINT16_LOW_BYTE( pConnectInfo->keepAliveSeconds );
pIndex += 2;
/* Write the client identifier into the CONNECT packet. */
pIndex = encodeString( pIndex,
pConnectInfo->pClientIdentifier,
pConnectInfo->clientIdentifierLength );
/* Write the will topic name and message into the CONNECT packet if provided. */
if( pWillInfo != NULL )
{
pIndex = encodeString( pIndex,
pWillInfo->pTopicName,
pWillInfo->topicNameLength );
pIndex = encodeString( pIndex,
pWillInfo->pPayload,
( uint16_t ) pWillInfo->payloadLength );
}
/* Encode the user name if provided. */
if( pConnectInfo->pUserName != NULL )
{
pIndex = encodeString( pIndex, pConnectInfo->pUserName, pConnectInfo->userNameLength );
}
/* Encode the password if provided. */
if( pConnectInfo->pPassword != NULL )
{
pIndex = encodeString( pIndex, pConnectInfo->pPassword, pConnectInfo->passwordLength );
}
LogDebug( ( "Length of serialized CONNECT packet is %lu.",
( ( unsigned long ) ( pIndex - pFixedBuffer->pBuffer ) ) ) );
/* 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 );
}