coreMQTT v2.1.1
MQTT 3.1.1 Client Library
MQTT_ReceiveLoop

Loop to receive packets from the transport interface. Does not handle keep alive.

MQTTStatus_t MQTT_ReceiveLoop(MQTTContext_t *pContext)
Loop to receive packets from the transport interface. Does not handle keep alive.
Definition: core_mqtt.c:3073
MQTTStatus_t
Return codes from MQTT functions.
Definition: core_mqtt_serializer.h:99
A struct representing an MQTT connection.
Definition: core_mqtt.h:162
Note
If a dummy MQTTGetCurrentTimeFunc_t was passed to MQTT_Init, then the MQTT_RECV_POLLING_TIMEOUT_MS and MQTT_SEND_TIMEOUT_MS timeout configurations MUST be set to 0.
Parameters
[in]pContextInitialized and connected MQTT context.
Note
Calling this function blocks the calling context for a time period that depends on the the configuration macros, MQTT_RECV_POLLING_TIMEOUT_MS and MQTT_SEND_TIMEOUT_MS, and the underlying transport interface implementation timeouts, unless an error occurs. The blocking period also depends on the execution time of the MQTTEventCallback_t callback supplied to the library. It is recommended that the supplied MQTTEventCallback_t callback does not contain blocking operations to prevent potential non-deterministic blocking period of the MQTT_ReceiveLoop API call.
Returns
MQTTBadParameter if context is NULL; MQTTRecvFailed if a network error occurs during reception; MQTTSendFailed if a network error occurs while sending an ACK or PINGREQ; MQTTBadResponse if an invalid packet is received; MQTTIllegalState if an incoming QoS 1/2 publish or ack causes an invalid transition for the internal state machine; MQTTNeedMoreBytes if MQTT_ReceiveLoop has received incomplete data; it should be called again (probably after a delay); MQTTSuccess on success.

Example

// Variables used in this example.
MQTTStatus_t status;
uint32_t keepAliveMs = 60 * 1000;
// This context is assumed to be initialized and connected.
MQTTContext_t * pContext;
while( true )
{
status = MQTT_ReceiveLoop( pContext );
if( status != MQTTSuccess && status != MQTTNeedMoreBytes )
{
// Determine the error. It's possible we might need to disconnect
// the underlying transport connection.
}
else
{
// Since this function does not send pings, the application may need
// to in order to comply with keep alive.
if( ( pContext->getTime() - pContext->lastPacketTxTime ) > keepAliveMs )
{
status = MQTT_Ping( pContext );
}
// Other application functions.
}
}
MQTTStatus_t MQTT_Ping(MQTTContext_t *pContext)
Sends an MQTT PINGREQ to broker.
Definition: core_mqtt.c:2850
@ MQTTSuccess
Definition: core_mqtt_serializer.h:100
@ MQTTNeedMoreBytes
Definition: core_mqtt_serializer.h:111
uint32_t lastPacketTxTime
Timestamp of the last packet sent by the library.
Definition: core_mqtt.h:216
MQTTGetCurrentTimeFunc_t getTime
Function used to get millisecond timestamps.
Definition: core_mqtt.h:206