coreMQTT v1.2.0
MQTT 3.1.1 Client Library
core_mqtt_state.h File Reference

Function to keep state of MQTT PUBLISH packet deliveries. More...

#include "core_mqtt.h"

Go to the source code of this file.

Macros

#define MQTT_STATE_CURSOR_INITIALIZER   ( ( size_t ) 0 )
 Initializer value for an MQTTStateCursor_t, indicating a search should start at the beginning of a state record array.
 

Typedefs

typedef size_t MQTTStateCursor_t
 Cursor for iterating through state records.
 

Functions

uint16_t MQTT_PublishToResend (const MQTTContext_t *pMqttContext, MQTTStateCursor_t *pCursor)
 Get the packet ID of next pending publish to be resent. More...
 

Detailed Description

Function to keep state of MQTT PUBLISH packet deliveries.

Function Documentation

◆ MQTT_PublishToResend()

uint16_t MQTT_PublishToResend ( const MQTTContext_t pMqttContext,
MQTTStateCursor_t pCursor 
)

Get the packet ID of next pending publish to be resent.

This function will need to be called to get the packet for which a publish need to be sent when a session is reestablished. Calling this function repeatedly until packet id is 0 will give all the packets for which a publish need to be resent in the correct order.

Parameters
[in]pMqttContextInitialized MQTT context.
[in,out]pCursorIndex at which to start searching.

Example

// For this example assume this function returns an outgoing unacknowledged
// QoS 1 or 2 publish from its packet identifier.
MQTTPublishInfo_t * getPublish( uint16_t packetID );
// Variables used in this example.
MQTTStatus_t status;
bool sessionPresent;
uint16_t packetID;
MQTTPublishInfo_t * pResendPublish = NULL;
MQTTConnectInfo_t connectInfo = { 0 };
// This is assumed to have been initialized before the call to MQTT_Connect().
MQTTContext_t * pContext;
// Set clean session to false to attempt session resumption.
connectInfo.cleanSession = false;
connectInfo.pClientIdentifier = "someClientID";
connectInfo.clientIdentifierLength = strlen( connectInfo.pClientIdentifier );
connectInfo.keepAliveSeconds = 60;
// Optional connect parameters are not relevant to this example.
// Create an MQTT connection. Use 100 milliseconds as a timeout.
status = MQTT_Connect( pContext, &connectInfo, NULL, 100, &sessionPresent );
if( status == MQTTSuccess )
{
if( sessionPresent )
{
// Loop while packet ID is nonzero.
while( ( packetID = MQTT_PublishToResend( pContext, &cursor ) ) != 0 )
{
// Assume this function will succeed.
pResendPublish = getPublish( packetID );
// Set DUP flag.
pResendPublish->dup = true;
status = MQTT_Publish( pContext, pResendPublish, packetID );
if( status != MQTTSuccess )
{
// Application can decide how to handle a failure.
}
}
}
else
{
// The broker did not resume a session, so we can clean up the
// list of outgoing publishes.
}
}
MQTTStatus_t MQTT_Publish(MQTTContext_t *pContext, const MQTTPublishInfo_t *pPublishInfo, uint16_t packetId)
Publishes a message to the given topic name.
Definition: core_mqtt.c:1916
MQTTStatus_t MQTT_Connect(MQTTContext_t *pContext, const MQTTConnectInfo_t *pConnectInfo, const MQTTPublishInfo_t *pWillInfo, uint32_t timeoutMs, bool *pSessionPresent)
Establish an MQTT session.
Definition: core_mqtt.c:1759
uint16_t MQTT_PublishToResend(const MQTTContext_t *pMqttContext, MQTTStateCursor_t *pCursor)
Get the packet ID of next pending publish to be resent.
Definition: core_mqtt_state.c:1033
size_t MQTTStateCursor_t
Cursor for iterating through state records.
Definition: core_mqtt_state.h:49
#define MQTT_STATE_CURSOR_INITIALIZER
Initializer value for an MQTTStateCursor_t, indicating a search should start at the beginning of a st...
Definition: core_mqtt_state.h:43
MQTTStatus_t
Return codes from MQTT functions.
Definition: core_mqtt_serializer.h:97
@ MQTTSuccess
Definition: core_mqtt_serializer.h:98
MQTT CONNECT packet parameters.
Definition: core_mqtt_serializer.h:140
const char * pClientIdentifier
MQTT client identifier. Must be unique per client.
Definition: core_mqtt_serializer.h:154
bool cleanSession
Whether to establish a new, clean session or resume a previous session.
Definition: core_mqtt_serializer.h:144
uint16_t keepAliveSeconds
MQTT keep alive period.
Definition: core_mqtt_serializer.h:149
uint16_t clientIdentifierLength
Length of the client identifier.
Definition: core_mqtt_serializer.h:159
A struct representing an MQTT connection.
Definition: core_mqtt.h:169
MQTT PUBLISH packet parameters.
Definition: core_mqtt_serializer.h:209
bool dup
Whether this is a duplicate publish message.
Definition: core_mqtt_serializer.h:223