AWS IoT Device SDK C:
MQTT
MQTT 3.1.1 client library
|
Return to main page ↑ |
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... | |
Demonstrates usage of the MQTT library.
#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.
#define KEEP_ALIVE_SECONDS ( 60 ) |
The keep-alive interval used for this demo.
An MQTT ping request will be sent periodically at this interval.
#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.
#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.
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.
[in] | awsIotMqttMode | Specify if this demo is running with the AWS IoT MQTT server. Set this to false if using another MQTT server. |
[in] | pIdentifier | NULL-terminated MQTT client identifier. |
[in] | pNetworkServerInfo | Passed to the MQTT connect function when establishing the MQTT connection. |
[in] | pNetworkCredentialInfo | Passed to the MQTT connect function when establishing the MQTT connection. |
[in] | pNetworkInterface | The network interface to use for this demo. |
EXIT_SUCCESS
if the demo completes successfully; EXIT_FAILURE
otherwise.
|
static |
Called by the MQTT library when an operation completes.
The demo uses this callback to determine the result of PUBLISH operations.
[in] | param1 | The number of the PUBLISH that completed, passed as an intptr_t. |
[in] | pOperation | Information about the completed operation passed by the MQTT library. |
|
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.
[in] | param1 | Counts the total number of received PUBLISH messages. This callback will increment this counter. |
[in] | pPublish | Information about the incoming PUBLISH message passed by the MQTT library. |
|
static |
Initialize the MQTT library.
EXIT_SUCCESS
if all libraries were successfully initialized; EXIT_FAILURE
otherwise.
|
static |
Establish a new connection to the MQTT server.
[in] | awsIotMqttMode | Specify if this demo is running with the AWS IoT MQTT server. Set this to false if using another MQTT server. |
[in] | pIdentifier | NULL-terminated MQTT client identifier. |
[in] | pNetworkServerInfo | Passed to the MQTT connect function when establishing the MQTT connection. |
[in] | pNetworkCredentialInfo | Passed to the MQTT connect function when establishing the MQTT connection. |
[in] | pNetworkInterface | The network interface to use for this demo. |
[out] | pMqttConnection | Set to the handle to the new MQTT connection. |
EXIT_SUCCESS
if the connection is successfully established; EXIT_FAILURE
otherwise.
|
static |
Add or remove subscriptions by either subscribing or unsubscribing.
[in] | mqttConnection | The MQTT connection to use for subscriptions. |
[in] | operation | Either IOT_MQTT_SUBSCRIBE or IOT_MQTT_UNSUBSCRIBE. |
[in] | pTopicFilters | Array of topic filters for subscriptions. |
[in] | pCallbackParameter | The parameter to pass to the subscription callback. |
EXIT_SUCCESS
if the subscription operation succeeded; EXIT_FAILURE
otherwise.
|
static |
Transmit all messages and wait for them to be received on topic filters.
[in] | mqttConnection | The MQTT connection to use for publishing. |
[in] | pTopicNames | Array of topic names for publishing. These were previously subscribed to as topic filters. |
[in] | pPublishReceivedCounter | Counts the number of messages received on topic filters. |
EXIT_SUCCESS
if all messages are published and received; EXIT_FAILURE
otherwise.