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

Extract MQTT packet type and length from incoming packet.

Parameters
[in,out]pIncomingPacketPointer to IotMqttPacketInfo_t structure where type, remaining length and packet identifier are stored.
[in]getNextBytePointer to platform specific function which is used to extract type and length from incoming received stream (see example ).
[in]pNetworkConnectionPointer to platform specific network connection which is used by getNextByte to receive network data
Returns
IOT_MQTT_SUCCESS on successful extraction of type and length, IOT_MQTT_BAD_RESPONSE on failure.
Note
This call is part of serializer API used for implementing light-weight MQTT client.

Example

// Example code below shows how to implement getNextByte function with posix sockets.
// Note: IotMqttGetNextByte_t typedef IotMqttError_t (* IotMqttGetNextByte_t)( IotNetworkConnection_t pNetworkContext,
// uint8_t * pNextByte );
// Note: It is assumed that socket is already created and connected,
IotMqttError_t getNextByte( IotNetworkConnection_t pContext,
uint8_t * pNextByte )
{
int socket = ( int ) ( *pvContext );
int receivedBytes;
receivedBytes = recv( socket, ( void * ) pNextByte, sizeof( uint8_t ), 0 );
if( receivedBytes == sizeof( uint8_t ) )
{
result = IOT_MQTT_SUCCESS;
}
else
{
result = IOT_MQTT_TIMEOUT;
}
return result;
}
// Example below shows how IotMqtt_GetIncomingMQTTPacketTypeAndLength() is used to extract type
// and length from incoming ping response.
// xMQTTSocket here is posix socket created and connected to MQTT broker outside of this function.
void getTypeAndLengthFromIncomingMQTTPingResponse( int xMQTTSocket )
{
IotMqttPacketInfo_t xIncomingPacket;
IotMqttError_t xResult = IotMqtt_GetIncomingMQTTPacketTypeAndLength( &xIncomingPacket, getNextByte, ( IotNetworkConnection_t ) xMQTTSocket );
}