AWS IoT Device SDK C: MQTT
MQTT 3.1.1 client library
Return to main page ↑
IotMqtt_SerializeConnect

Generate a CONNECT packet from the given parameters.

size_t remainingLength,
uint8_t * pBuffer,
size_t bufferSize );
Parameters
[in]pConnectInfoUser-provided CONNECT information.
[in]remainingLengthremaining length of the packet to be serialized.
[in,out]pBufferUser provided buffer where the CONNECT packet is written.
[in]bufferSizeSize of the buffer pointed to by pBuffer.
Returns
IOT_MQTT_SUCCESS or IOT_MQTT_NO_MEMORY.
Note
pBuffer must be allocated by caller. Use IotMqtt_GetConnectPacketSize to determine the required size.
This call is part of serializer API used for implementing light-weight MQTT client.

Example

// Example code below shows how IotMqtt_SerializeConnect() should be used to serialize
// MQTT connect packet and send it to MQTT broker.
// Example uses static memory but dynamically allocated memory can be used as well.
// Get size requirement for the connect packet.
#define mqttexampleSHARED_BUFFER_SIZE 100
static ucSharedBuffer[mqttexampleSHARED_BUFFER_SIZE];
void sendConnectPacket( int xMQTTSocket )
{
IotMqttConnectInfo_t xConnectInfo;
size_t xRemainingLength = 0;
size_t xPacketSize = 0;
IotMqttError_t xResult;
size_t xSentBytes = 0;
// Get size requirement for MQTT connect packet.
xResult = IotMqtt_GetConnectPacketSize( &xConnectInfo, &xRemainingLength, &xPacketSize );
// Make sure the packet size is less than static buffer size
IotMqtt_Assert( xPacketSize < mqttexampleSHARED_BUFFER_SIZE );
// Serialize MQTT connect packet into provided buffer
xResult = IotMqtt_SerializeConnect( &xConnectInfo, xRemainingLength, ucSharedBuffer, xPacketSize );
// xMQTTSocket here is posix socket created and connected to MQTT broker outside of this function.
xSentBytes = send( xMQTTSocket, ( void * ) ucSharedBuffer, xPacketSize, 0 );
IotMqtt_Assert( xSentBytes == xPacketSize );
}