MQTT API Reference
MQTT 3.1.1 client library
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 );
}
IotMqtt_SerializeConnect
IotMqttError_t IotMqtt_SerializeConnect(const IotMqttConnectInfo_t *pConnectInfo, size_t remainingLength, uint8_t *pBuffer, size_t bufferSize)
Generate a CONNECT packet from the given parameters.
Definition: iot_mqtt_serialize.c:2181
IotMqttError_t
IotMqttError_t
Return codes of MQTT functions.
Definition: iot_mqtt_types.h:103
IOT_MQTT_SUCCESS
MQTT operation completed successfully.
Definition: iot_mqtt_types.h:119
IotMqtt_GetConnectPacketSize
IotMqttError_t IotMqtt_GetConnectPacketSize(const IotMqttConnectInfo_t *pConnectInfo, size_t *pRemainingLength, size_t *pPacketSize)
Calculate the size and "Remaining length" of a CONNECT packet generated from the given parameters.
Definition: iot_mqtt_serialize.c:2134
IotMqtt_Assert
#define IotMqtt_Assert(expression)
Assertion macro for the MQTT library.
Definition: iot_mqtt_internal.h:62
IotMqttConnectInfo_t
MQTT connection details.
Definition: iot_mqtt_types.h:576