coreMQTT v2.1.1
MQTT 3.1.1 Client Library
MQTT_GetIncomingPacketTypeAndLength

Extract the MQTT packet type and length from incoming packet.

NetworkContext_t * pNetworkContext,
MQTTPacketInfo_t * pIncomingPacket );
MQTTStatus_t MQTT_GetIncomingPacketTypeAndLength(TransportRecv_t readFunc, NetworkContext_t *pNetworkContext, MQTTPacketInfo_t *pIncomingPacket)
Extract the MQTT packet type and length from incoming packet.
Definition: core_mqtt_serializer.c:2565
int32_t(* TransportRecv_t)(NetworkContext_t *pNetworkContext, void *pBuffer, size_t bytesToRecv)
Transport interface for receiving data on the network.
Definition: transport_interface.h:221
MQTTStatus_t
Return codes from MQTT functions.
Definition: core_mqtt_serializer.h:99
struct NetworkContext NetworkContext_t
The NetworkContext is an incomplete type. An implementation of this interface must define struct Netw...
Definition: transport_interface.h:191
MQTT incoming packet parameters.
Definition: core_mqtt_serializer.h:256

This function must be called for every incoming packet to retrieve the MQTTPacketInfo_t.type and MQTTPacketInfo_t.remainingLength. A MQTTPacketInfo_t is not valid until this routine has been invoked.

Parameters
[in]readFuncTransport layer read function pointer.
[in]pNetworkContextThe network context pointer provided by the application.
[out]pIncomingPacketPointer to MQTTPacketInfo_t structure. This is where type, remaining length and packet identifier are stored.
Returns
MQTTSuccess on successful extraction of type and length, MQTTBadParameter if pIncomingPacket is invalid, MQTTRecvFailed on transport receive failure, MQTTBadResponse if an invalid packet is read, and MQTTNoDataAvailable if there is nothing to read.

Example

// TransportRecv_t function for reading from the network.
int32_t socket_recv(
NetworkContext_t * pNetworkContext,
void * pBuffer,
size_t bytesToRecv
);
// Some context to be used with above transport receive function.
NetworkContext_t networkContext;
// Struct to hold the incoming packet information.
MQTTPacketInfo_t incomingPacket;
int32_t bytesRecvd;
// Buffer to hold the remaining data of the incoming packet.
uint8_t buffer[ BUFFER_SIZE ];
// Loop until data is available to be received.
do{
socket_recv,
&networkContext,
&incomingPacket
);
} while( status == MQTTNoDataAvailable );
assert( status == MQTTSuccess );
// Receive the rest of the incoming packet.
assert( incomingPacket.remainingLength <= BUFFER_SIZE );
bytesRecvd = socket_recv(
&networkContext,
( void * ) buffer,
incomingPacket.remainingLength
);
// Set the remaining data field.
incomingPacket.pRemainingData = buffer;
@ MQTTSuccess
Definition: core_mqtt_serializer.h:100
@ MQTTNoDataAvailable
Definition: core_mqtt_serializer.h:107
size_t remainingLength
Length of remaining serialized data.
Definition: core_mqtt_serializer.h:270
uint8_t * pRemainingData
Remaining serialized data in the MQTT packet.
Definition: core_mqtt_serializer.h:265