AWS IoT Device SDK C: MQTT
MQTT 3.1.1 client library
Return to main page ↑
iot_demo_mqtt.c File Reference

Demonstrates usage of the MQTT library. More...

#include "iot_config.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "iot_demo_logging.h"
#include "platform/iot_clock.h"
#include "platform/iot_threads.h"
#include "iot_mqtt.h"

Macros

#define CLIENT_IDENTIFIER_PREFIX   "iotdemo"
 The first characters in the client identifier. A timestamp is appended to this prefix to create a unique client identifer. More...
 
#define CLIENT_IDENTIFIER_MAX_LENGTH   ( 24 )
 The longest client identifier that an MQTT server must accept (as defined by the MQTT 3.1.1 spec) is 23 characters. Add 1 to include the length of the NULL terminator.
 
#define KEEP_ALIVE_SECONDS   ( 60 )
 The keep-alive interval used for this demo. More...
 
#define MQTT_TIMEOUT_MS   ( 5000 )
 The timeout for MQTT operations in this demo.
 
#define WILL_TOPIC_NAME   IOT_DEMO_MQTT_TOPIC_PREFIX "/will"
 The Last Will and Testament topic name in this demo. More...
 
#define WILL_TOPIC_NAME_LENGTH   ( ( uint16_t ) ( sizeof( WILL_TOPIC_NAME ) - 1 ) )
 The length of WILL_TOPIC_NAME.
 
#define WILL_MESSAGE   "MQTT demo unexpectedly disconnected."
 The message to publish to WILL_TOPIC_NAME.
 
#define WILL_MESSAGE_LENGTH   ( ( size_t ) ( sizeof( WILL_MESSAGE ) - 1 ) )
 The length of WILL_MESSAGE.
 
#define TOPIC_FILTER_COUNT   ( 4 )
 How many topic filters will be used in this demo.
 
#define TOPIC_FILTER_LENGTH   ( ( uint16_t ) ( sizeof( IOT_DEMO_MQTT_TOPIC_PREFIX "/topic/1" ) - 1 ) )
 The length of each topic filter. More...
 
#define PUBLISH_PAYLOAD_FORMAT   "Hello world %d!"
 Format string of the PUBLISH messages in this demo.
 
#define PUBLISH_PAYLOAD_BUFFER_LENGTH   ( sizeof( PUBLISH_PAYLOAD_FORMAT ) + 2 )
 Size of the buffer that holds the PUBLISH messages in this demo.
 
#define PUBLISH_RETRY_LIMIT   ( 10 )
 The maximum number of times each PUBLISH in this demo will be retried.
 
#define PUBLISH_RETRY_MS   ( 1000 )
 A PUBLISH message is retried if no response is received within this time.
 
#define ACKNOWLEDGEMENT_TOPIC_NAME   IOT_DEMO_MQTT_TOPIC_PREFIX "/acknowledgements"
 The topic name on which acknowledgement messages for incoming publishes should be published.
 
#define ACKNOWLEDGEMENT_TOPIC_NAME_LENGTH   ( ( uint16_t ) ( sizeof( ACKNOWLEDGEMENT_TOPIC_NAME ) - 1 ) )
 The length of ACKNOWLEDGEMENT_TOPIC_NAME.
 
#define ACKNOWLEDGEMENT_MESSAGE_FORMAT   "Client has received PUBLISH %.*s from server."
 Format string of PUBLISH acknowledgement messages in this demo.
 
#define ACKNOWLEDGEMENT_MESSAGE_BUFFER_LENGTH   ( sizeof( ACKNOWLEDGEMENT_MESSAGE_FORMAT ) + 2 )
 Size of the buffers that hold acknowledgement messages in this demo.
 

Functions

int RunMqttDemo (bool awsIotMqttMode, const char *pIdentifier, void *pNetworkServerInfo, void *pNetworkCredentialInfo, const IotNetworkInterface_t *pNetworkInterface)
 The function that runs the MQTT demo, called by the demo runner. More...
 
static void _operationCompleteCallback (void *param1, IotMqttCallbackParam_t *const pOperation)
 Called by the MQTT library when an operation completes. More...
 
static void _mqttSubscriptionCallback (void *param1, IotMqttCallbackParam_t *const pPublish)
 Called by the MQTT library when an incoming PUBLISH message is received. More...
 
static int _initializeDemo (void)
 Initialize the MQTT library. More...
 
static void _cleanupDemo (void)
 Clean up the MQTT library.
 
static int _establishMqttConnection (bool awsIotMqttMode, const char *pIdentifier, void *pNetworkServerInfo, void *pNetworkCredentialInfo, const IotNetworkInterface_t *pNetworkInterface, IotMqttConnection_t *pMqttConnection)
 Establish a new connection to the MQTT server. More...
 
static int _modifySubscriptions (IotMqttConnection_t mqttConnection, IotMqttOperationType_t operation, const char **pTopicFilters, void *pCallbackParameter)
 Add or remove subscriptions by either subscribing or unsubscribing. More...
 
static int _publishAllMessages (IotMqttConnection_t mqttConnection, const char **pTopicNames, IotSemaphore_t *pPublishReceivedCounter)
 Transmit all messages and wait for them to be received on topic filters. More...
 

Detailed Description

Demonstrates usage of the MQTT library.

Macro Definition Documentation

◆ CLIENT_IDENTIFIER_PREFIX

#define CLIENT_IDENTIFIER_PREFIX   "iotdemo"

The first characters in the client identifier. A timestamp is appended to this prefix to create a unique client identifer.

This prefix is also used to generate topic names and topic filters used in this demo.

◆ KEEP_ALIVE_SECONDS

#define KEEP_ALIVE_SECONDS   ( 60 )

The keep-alive interval used for this demo.

An MQTT ping request will be sent periodically at this interval.

◆ WILL_TOPIC_NAME

#define WILL_TOPIC_NAME   IOT_DEMO_MQTT_TOPIC_PREFIX "/will"

The Last Will and Testament topic name in this demo.

The MQTT server will publish a message to this topic name if this client is unexpectedly disconnected.

◆ TOPIC_FILTER_LENGTH

#define TOPIC_FILTER_LENGTH   ( ( uint16_t ) ( sizeof( IOT_DEMO_MQTT_TOPIC_PREFIX "/topic/1" ) - 1 ) )

The length of each topic filter.

For convenience, all topic filters are the same length.

Function Documentation

◆ RunMqttDemo()

int RunMqttDemo ( bool  awsIotMqttMode,
const char *  pIdentifier,
void *  pNetworkServerInfo,
void *  pNetworkCredentialInfo,
const IotNetworkInterface_t pNetworkInterface 
)

The function that runs the MQTT demo, called by the demo runner.

Parameters
[in]awsIotMqttModeSpecify if this demo is running with the AWS IoT MQTT server. Set this to false if using another MQTT server.
[in]pIdentifierNULL-terminated MQTT client identifier.
[in]pNetworkServerInfoPassed to the MQTT connect function when establishing the MQTT connection.
[in]pNetworkCredentialInfoPassed to the MQTT connect function when establishing the MQTT connection.
[in]pNetworkInterfaceThe network interface to use for this demo.
Returns
EXIT_SUCCESS if the demo completes successfully; EXIT_FAILURE otherwise.

◆ _operationCompleteCallback()

static void _operationCompleteCallback ( void *  param1,
IotMqttCallbackParam_t *const  pOperation 
)
static

Called by the MQTT library when an operation completes.

The demo uses this callback to determine the result of PUBLISH operations.

Parameters
[in]param1The number of the PUBLISH that completed, passed as an intptr_t.
[in]pOperationInformation about the completed operation passed by the MQTT library.

◆ _mqttSubscriptionCallback()

static void _mqttSubscriptionCallback ( void *  param1,
IotMqttCallbackParam_t *const  pPublish 
)
static

Called by the MQTT library when an incoming PUBLISH message is received.

The demo uses this callback to handle incoming PUBLISH messages. This callback prints the contents of an incoming message and publishes an acknowledgement to the MQTT server.

Parameters
[in]param1Counts the total number of received PUBLISH messages. This callback will increment this counter.
[in]pPublishInformation about the incoming PUBLISH message passed by the MQTT library.

◆ _initializeDemo()

static int _initializeDemo ( void  )
static

Initialize the MQTT library.

Returns
EXIT_SUCCESS if all libraries were successfully initialized; EXIT_FAILURE otherwise.

◆ _establishMqttConnection()

static int _establishMqttConnection ( bool  awsIotMqttMode,
const char *  pIdentifier,
void *  pNetworkServerInfo,
void *  pNetworkCredentialInfo,
const IotNetworkInterface_t pNetworkInterface,
IotMqttConnection_t pMqttConnection 
)
static

Establish a new connection to the MQTT server.

Parameters
[in]awsIotMqttModeSpecify if this demo is running with the AWS IoT MQTT server. Set this to false if using another MQTT server.
[in]pIdentifierNULL-terminated MQTT client identifier.
[in]pNetworkServerInfoPassed to the MQTT connect function when establishing the MQTT connection.
[in]pNetworkCredentialInfoPassed to the MQTT connect function when establishing the MQTT connection.
[in]pNetworkInterfaceThe network interface to use for this demo.
[out]pMqttConnectionSet to the handle to the new MQTT connection.
Returns
EXIT_SUCCESS if the connection is successfully established; EXIT_FAILURE otherwise.

◆ _modifySubscriptions()

static int _modifySubscriptions ( IotMqttConnection_t  mqttConnection,
IotMqttOperationType_t  operation,
const char **  pTopicFilters,
void *  pCallbackParameter 
)
static

Add or remove subscriptions by either subscribing or unsubscribing.

Parameters
[in]mqttConnectionThe MQTT connection to use for subscriptions.
[in]operationEither IOT_MQTT_SUBSCRIBE or IOT_MQTT_UNSUBSCRIBE.
[in]pTopicFiltersArray of topic filters for subscriptions.
[in]pCallbackParameterThe parameter to pass to the subscription callback.
Returns
EXIT_SUCCESS if the subscription operation succeeded; EXIT_FAILURE otherwise.

◆ _publishAllMessages()

static int _publishAllMessages ( IotMqttConnection_t  mqttConnection,
const char **  pTopicNames,
IotSemaphore_t pPublishReceivedCounter 
)
static

Transmit all messages and wait for them to be received on topic filters.

Parameters
[in]mqttConnectionThe MQTT connection to use for publishing.
[in]pTopicNamesArray of topic names for publishing. These were previously subscribed to as topic filters.
[in]pPublishReceivedCounterCounts the number of messages received on topic filters.
Returns
EXIT_SUCCESS if all messages are published and received; EXIT_FAILURE otherwise.