]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS-Plus/Demo/FreeRTOS_IoT_Libraries/mqtt/DemoTasks/SimpleMQTTExamples.c
Fix spelling issues.
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_IoT_Libraries / mqtt / DemoTasks / SimpleMQTTExamples.c
index 648999eb6654a5a9d7a714d33626c0bbd7d69713..80a04169e78208d8e5223203d12369920199a592 100644 (file)
 /**\r
  * @brief The timeout for MQTT operations in this example.\r
  */\r
-#define mqttexampleMQTT_TIMEOUT_MS                     ( 5000 )\r
+#define mqttexampleMQTT_TIMEOUT_MS                     ( 50000 )\r
 \r
 /**\r
  * @brief The MQTT client identifier used in this example.\r
  */\r
 #define mqttexampleCLIENT_IDENTIFIER           "mqttexampleclient"\r
 \r
+const char *pcClientIdentifiers[] = { "AAA" };//, "BBB", "CCC", "DDD", "EEE", "FFF", "GGG", "HHH", "III", "JJJ" };\r
+\r
 /**\r
  * @brief Details of the MQTT broker to connect to.\r
  *\r
 #define mqttexampleMESSAGE_RECEIVED_BIT                ( 1UL << 1UL )\r
 /*-----------------------------------------------------------*/\r
 \r
-/**\r
- * @brief The MQTT connection handle used in this example.\r
- */\r
-static IotMqttConnection_t xMQTTConnection = IOT_MQTT_CONNECTION_INITIALIZER;\r
-\r
 /**\r
  * @brief Parameters used to create the system task pool.\r
  */\r
@@ -140,58 +137,58 @@ static void prvMQTTDemoTask( void *pvParameters );
 /**\r
  * @brief The callback invoked by the MQTT library when the MQTT connection gets\r
  * disconnected.\r
- * \r
+ *\r
  * @param[in] pvCallbackContext Callback context as provided at the time of\r
  * connect.\r
  * @param[in] pxCallbackParams Contains the reason why the MQTT connection was\r
  * disconnected.\r
  */\r
-static void prvExample_DisconnectCallback( void * pvCallbackContext,\r
-                                                                                  IotMqttCallbackParam_t * pxCallbackParams );\r
+static void prvExample_OnDisconnect( void * pvCallbackContext,\r
+                                                                        IotMqttCallbackParam_t * pxCallbackParams );\r
 \r
 /**\r
  * @brief The callback invoked by the MQTT library when a message is received on\r
  * a subscribed topic from the MQTT broker.\r
- * \r
+ *\r
  * @param[in] pvCallbackContext Callback context as provided at the time of\r
  * subscribe.\r
- * @param[in] pxCallbackParams Contain the details about the received message - \r
+ * @param[in] pxCallbackParams Contain the details about the received message -\r
  * topic on which the message was received, the received message.\r
  */\r
-static void prvExample_PublishCallback( void * pvCallbackContext,\r
-                                                                               IotMqttCallbackParam_t * pxCallbackParams );\r
+static void prvExample_OnMessageReceived( void * pvCallbackContext,\r
+                                                                                 IotMqttCallbackParam_t * pxCallbackParams );\r
 \r
 /**\r
  * @brief Connects to the MQTT broker as specified in mqttexampleMQTT_BROKER_ENDPOINT\r
  * and mqttexampleMQTT_BROKER_PORT.\r
- * \r
+ *\r
  * @note This example does not use TLS and therefore will not work with MQTT.\r
  */\r
-static void prvMQTTConnect( void );\r
+static void prvMQTTConnect( IotMqttConnection_t *xMQTTConnection, const char *pcClientID );\r
 \r
 /**\r
- * @brief Subscribes to the topic as specified in mqttexampleTOPIC.\r
+ * @brief Subscribes to pcTopicString.\r
  */\r
-static void prvMQTTSubscribe( void );\r
+static void prvMQTTSubscribe( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString );\r
 \r
 /**\r
  * @brief Publishes a messages mqttexampleMESSAGE on mqttexampleTOPIC topic.\r
  */\r
-static void prvMQTTPublish( void );\r
+static void prvMQTTPublish( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString );\r
 \r
 /**\r
  * @brief Unsubscribes from the mqttexampleTOPIC topic.\r
  */\r
-static void prvMQTTUnsubscribe( void );\r
+static void prvMQTTUnsubscribe( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString );\r
 \r
 /**\r
  * @brief Disconnects from the MQTT broker gracefully by sending an MQTT\r
  * DISCONNECT message.\r
  */\r
-static void prvMQTTDisconnect( void );\r
+static void prvMQTTDisconnect( IotMqttConnection_t xMQTTConnection );\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvExample_DisconnectCallback( void * pvCallbackContext,\r
+static void prvExample_OnDisconnect( void * pvCallbackContext,\r
                                                                                   IotMqttCallbackParam_t * pxCallbackParams )\r
 {\r
 TaskHandle_t xDemoTaskHandle = ( TaskHandle_t ) pvCallbackContext;\r
@@ -207,16 +204,16 @@ TaskHandle_t xDemoTaskHandle = ( TaskHandle_t ) pvCallbackContext;
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvExample_PublishCallback( void * pvCallbackContext,\r
+static void prvExample_OnMessageReceived( void * pvCallbackContext,\r
                                                                                IotMqttCallbackParam_t * pxCallbackParams )\r
 {\r
 TaskHandle_t xDemoTaskHandle = ( TaskHandle_t ) pvCallbackContext;\r
 \r
        /* Ensure that the message is received on the expected topic. */\r
-       configASSERT( pxCallbackParams->u.message.info.topicNameLength == strlen( mqttexampleTOPIC ) );\r
-       configASSERT( strncmp( pxCallbackParams->u.message.info.pTopicName,\r
-                                                  mqttexampleTOPIC,\r
-                                                  strlen( mqttexampleTOPIC ) ) == 0 );\r
+//     configASSERT( pxCallbackParams->u.message.info.topicNameLength == strlen( mqttexampleTOPIC ) );\r
+//     configASSERT( strncmp( pxCallbackParams->u.message.info.pTopicName,\r
+//                                                mqttexampleTOPIC,\r
+//                                                strlen( mqttexampleTOPIC ) ) == 0 );\r
 \r
        /* Ensure that the expected message is received. */\r
        configASSERT( pxCallbackParams->u.message.info.payloadLength == strlen( mqttexampleMESSAGE ) );\r
@@ -227,33 +224,50 @@ TaskHandle_t xDemoTaskHandle = ( TaskHandle_t ) pvCallbackContext;
        /* Ensure that the message QoS is as expected. */\r
        configASSERT( pxCallbackParams->u.message.info.qos == IOT_MQTT_QOS_1 );\r
 \r
+       /* Although this print uses the constants rather than the data from the\r
+        * message payload the asserts above have already checked the message\r
+        * payload equals the constants, and it is more efficient not to have to\r
+        * worry about lengths in the print. */\r
+       configPRINTF( ( "Received %s on the topic %s\r\n", mqttexampleMESSAGE, mqttexampleTOPIC ) );\r
+\r
        /* Inform the demo task about the message received from the MQTT broker. */\r
        xTaskNotify( xDemoTaskHandle,\r
-                               mqttexampleMESSAGE_RECEIVED_BIT,\r
-                               eSetBits /* Set the mqttexampleMESSAGE_RECEIVED_BIT in the demo task's notification value. */\r
+                                mqttexampleMESSAGE_RECEIVED_BIT,\r
+                                eSetBits /* Set the mqttexampleMESSAGE_RECEIVED_BIT in the demo task's notification value. */\r
                                );\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
 void vStartSimpleMQTTDemo( void )\r
 {\r
+uint32_t x;\r
+const uint32_t ulMax_x = sizeof( pcClientIdentifiers ) / sizeof( char * );\r
+\r
        /* This example uses a single application task, which in turn is used to\r
         * connect, subscribe, publish, unsubscribe and disconnect from the MQTT\r
         * broker. */\r
+for( x = 0; x < ulMax_x; x++ )\r
+{\r
        xTaskCreate( prvMQTTDemoTask,                   /* Function that implements the task. */\r
                                 "MQTTDemo",                            /* Text name for the task - only used for debugging. */\r
                                 configMINIMAL_STACK_SIZE,      /* Size of stack (in words, not bytes) to allocate for the task. */\r
-                                NULL,                                          /* Task parameter - not used in this case. */\r
+                                ( void * ) x,                                          /* Task parameter - not used in this case. */\r
                                 tskIDLE_PRIORITY,                      /* Task priority, must be between 0 and configMAX_PRIORITIES - 1. */\r
                                 NULL );                                        /* Used to pass out a handle to the created task - not used in this case. */\r
 }\r
+}\r
 /*-----------------------------------------------------------*/\r
 \r
 static void prvMQTTDemoTask( void *pvParameters )\r
 {\r
 IotMqttError_t xResult;\r
-uint32_t ulNotificationValue = 0;\r
-const TickType_t xNoDelay = ( TickType_t ) 0;\r
+uint32_t ulNotificationValue = 0, ulPublishCount;\r
+uint32_t ulMaxPublishCount = 0UL;\r
+const TickType_t xNoDelay = ( TickType_t ) 1;\r
+IotMqttConnection_t xMQTTConnection = IOT_MQTT_CONNECTION_INITIALIZER;\r
+uint32_t ulTaskNumber = ( uint32_t ) pvParameters, x;\r
+char cTopicString[ sizeof( mqttexampleTOPIC ) + 5 ];//_RB_ Access by other tasks so must be persistant and will cause memory faults on memory protected systems.\r
+#pragma message ("Access by other tasks so must be persistant and will cause memory faults on memory protected systems.")\r
 \r
        /* Remove compiler warnings about unused parameters. */\r
        ( void ) pvParameters;\r
@@ -267,77 +281,117 @@ const TickType_t xNoDelay = ( TickType_t ) 0;
        xResult = IotMqtt_Init();\r
        configASSERT( xResult == IOT_MQTT_SUCCESS );\r
 \r
+       /* Create a topic string that is unique to the MQTT connection created by\r
+       this task. */\r
+       snprintf( cTopicString, sizeof( cTopicString ), "%s/%s", mqttexampleTOPIC, pcClientIdentifiers[ ulTaskNumber ] );\r
+\r
        for( ; ; )\r
        {\r
                /* Don't expect any notifications to be pending yet. */\r
-               configASSERT( ulTaskNotifyTake( pdTRUE, xNoDelay ) == 0 );\r
+               ulNotificationValue = ulTaskNotifyTake( pdTRUE, xNoDelay );\r
+               configASSERT( ulNotificationValue == 0 );\r
+\r
+\r
+               /****************************** Connect. ******************************/\r
 \r
                /* Establish a connection to the MQTT broker. This example connects to\r
                 * the MQTT broker as specified in mqttexampleMQTT_BROKER_ENDPOINT and\r
                 * mqttexampleMQTT_BROKER_PORT at the top of this file. Please change\r
                 * it to the MQTT broker you want to connect to. Note that this example\r
                 * does not use TLS and therefore will not work with AWS IoT. */\r
-               prvMQTTConnect();\r
-\r
-               /* Subscribe to the topic as specified in mqttexampleTOPIC at the top\r
-                * of this file. */\r
-               prvMQTTSubscribe();\r
-\r
-               /* Publish a message on the mqttexampleTOPIC topic as specified at the\r
-                * top of this file. */\r
-               prvMQTTPublish();\r
-\r
-               /* Since we are subscribed on the same topic, we will get the same\r
-                * message back from the MQTT broker. Wait for the message to be\r
-                * received which is informed to us by the publish callback\r
-                * (prvExample_PublishCallback) by setting the mqttexampleMESSAGE_RECEIVED_BIT\r
-                * in this task's notification value. */\r
-               xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */\r
-                                                0UL, /* Don't clear any bits on exit. */\r
-                                                &( ulNotificationValue ), /* Obtain the notification value. */\r
-                                                pdMS_TO_TICKS( mqttexampleMQTT_TIMEOUT_MS ) );\r
-               configASSERT( ( ulNotificationValue & mqttexampleMESSAGE_RECEIVED_BIT ) == mqttexampleMESSAGE_RECEIVED_BIT );\r
-\r
-               /* Unsubscribe from the topic mqttexampleTOPIC. */\r
-               prvMQTTUnsubscribe();\r
-\r
-               /* Gracefully disconnect from the MQTT broker by sending an MQTT\r
-                * DISCONNECT message. */\r
-               prvMQTTDisconnect();\r
+               prvMQTTConnect( &xMQTTConnection, pcClientIdentifiers[ ulTaskNumber ] );\r
+               configPRINTF( ( "Connected to %s\r\n", mqttexampleMQTT_BROKER_ENDPOINT ) );\r
+\r
+\r
+               /**************************** Subscribe. ******************************/\r
+\r
+               /* The client is now connected to the broker. Subscribe to the topic\r
+                * as specified in mqttexampleTOPIC at the top of this file.  This\r
+                * client will then publish to the same topic it subscribed to, so will\r
+                * expect all the messages it sends to the broker to be sent back to it\r
+                * from the broker. */\r
+               prvMQTTSubscribe( xMQTTConnection, cTopicString );\r
+               configPRINTF( ( "Subscribed to the topic %s\r\n", cTopicString ) );\r
+\r
+\r
+               /*********************** Publish 5 messages. **************************/\r
+\r
+               /* Publish a few messages while connected. */\r
+               for( x = 0; x < ( ulTaskNumber + 1UL ); x++ )\r
+               {\r
+                       ulMaxPublishCount = uxRand();\r
+               }\r
+\r
+               /* Cap ulMaxPublishCount but ensure it is not zero. */\r
+               ulMaxPublishCount %= 10UL;\r
+               ulMaxPublishCount++;\r
+\r
+               for( ulPublishCount = 0; ulPublishCount < ulMaxPublishCount; ulPublishCount++ )\r
+               {\r
+                       /* Publish a message on the mqttexampleTOPIC topic as specified at\r
+                        * the top of this file. */\r
+                       prvMQTTPublish( xMQTTConnection, cTopicString );\r
+                       configPRINTF( ( "Published %s on the topic %s\r\n", mqttexampleMESSAGE, cTopicString ) );\r
+\r
+                       /* Since we are subscribed to the same topic as we published on, we\r
+                        * will get the same message back from the MQTT broker. Wait for the\r
+                        * message to be received which is informed to us by the publish\r
+                        * callback (prvExample_OnMessageReceived) by setting the\r
+                        * mqttexampleMESSAGE_RECEIVED_BIT in this task's notification\r
+                        * value. Note that the bit is cleared in the task's notification\r
+                        * value to ensure that it is ready for next message. */\r
+                       xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */\r
+                                                        mqttexampleMESSAGE_RECEIVED_BIT, /* Clear bit on exit. */\r
+                                                        &( ulNotificationValue ), /* Obtain the notification value. */\r
+                                                        pdMS_TO_TICKS( mqttexampleMQTT_TIMEOUT_MS ) );\r
+                       configASSERT( ( ulNotificationValue & mqttexampleMESSAGE_RECEIVED_BIT ) == mqttexampleMESSAGE_RECEIVED_BIT );\r
+               }\r
+\r
+\r
+               /******************* Unsubscribe and Disconnect. **********************/\r
+\r
+               /* Unsubscribe from the topic mqttexampleTOPIC and disconnect\r
+                * gracefully. */\r
+               prvMQTTUnsubscribe( xMQTTConnection, cTopicString );\r
+               prvMQTTDisconnect( xMQTTConnection );\r
+               configPRINTF( ( "Disconnected from %s\r\n\r\n", mqttexampleMQTT_BROKER_ENDPOINT ) );\r
 \r
                /* Wait for the disconnect operation to complete which is informed to us\r
-                * by the disconnect callback (prvExample_DisconnectCallback)by setting\r
+                * by the disconnect callback (prvExample_OnDisconnect)by setting\r
                 * the mqttexampleDISCONNECTED_BIT in this task's notification value.\r
-                * Note that all bits are cleared in the task's notification value to\r
+                * Note that the bit is cleared in the task's notification value to\r
                 * ensure that it is ready for the next run. */\r
                xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */\r
-                                                portMAX_DELAY, /* Clear all bits on exit - portMAX_DELAY is used as it is a portable way of having all bits set. */\r
+                                                mqttexampleDISCONNECTED_BIT, /* Clear bit on exit. */\r
                                                 &( ulNotificationValue ), /* Obtain the notification value. */\r
                                                 pdMS_TO_TICKS( mqttexampleMQTT_TIMEOUT_MS ) );\r
                configASSERT( ( ulNotificationValue & mqttexampleDISCONNECTED_BIT ) == mqttexampleDISCONNECTED_BIT );\r
 \r
-               printf( "prvMQTTDemoTask() completed an iteration without hitting an assert.\r\n" );\r
-               fflush( stdout );\r
-\r
                /* Wait for some time between two iterations to ensure that we do not\r
                 * bombard the public test mosquitto broker. */\r
-               vTaskDelay( pdMS_TO_TICKS( 5000 ) );\r
+               configPRINTF( ( "prvMQTTDemoTask() completed an iteration without hitting an assert. Total free heap is %u\r\n\r\n", xPortGetFreeHeapSize() ) );\r
+//             vTaskDelay( pdMS_TO_TICKS( 5000 ) );\r
        }\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvMQTTConnect( void )\r
+static void prvMQTTConnect( IotMqttConnection_t *xMQTTConnection, const char *pcClientID )\r
 {\r
 IotMqttError_t xResult;\r
 IotNetworkServerInfo_t xMQTTBrokerInfo;\r
 IotMqttNetworkInfo_t xNetworkInfo = IOT_MQTT_NETWORK_INFO_INITIALIZER;\r
 IotMqttConnectInfo_t xConnectInfo = IOT_MQTT_CONNECT_INFO_INITIALIZER;\r
+static char c[ 10 ];\r
+static int id = 0;\r
 \r
        /******************* Broker information setup. **********************/\r
+\r
        xMQTTBrokerInfo.pHostName = mqttexampleMQTT_BROKER_ENDPOINT;\r
        xMQTTBrokerInfo.port = mqttexampleMQTT_BROKER_PORT;\r
 \r
+\r
        /******************* Network information setup. **********************/\r
+\r
        /* No connection to the MQTT broker has been established yet and we want to\r
         * establish a new connection. */\r
        xNetworkInfo.createNetworkConnection = true;\r
@@ -348,14 +402,19 @@ IotMqttConnectInfo_t xConnectInfo = IOT_MQTT_CONNECT_INFO_INITIALIZER;
        xNetworkInfo.u.setup.pNetworkCredentialInfo = NULL;\r
 \r
        /* Use FreeRTOS+TCP network. */\r
-       xNetworkInfo.pNetworkInterface = IOT_NETWORK_INTERFACE_AFR;\r
+       xNetworkInfo.pNetworkInterface = IOT_NETWORK_INTERFACE_FREERTOS;\r
 \r
-       /* Setup the callback which is called when the MQTT connection is disconnected. */\r
+       /* Setup the callback which is called when the MQTT connection is\r
+        * disconnected. The task handle is passed as the callback context which\r
+        * is used by the callback to send a task notification to this task.*/\r
        xNetworkInfo.disconnectCallback.pCallbackContext = ( void * ) xTaskGetCurrentTaskHandle();\r
-       xNetworkInfo.disconnectCallback.function = prvExample_DisconnectCallback;\r
+       xNetworkInfo.disconnectCallback.function = prvExample_OnDisconnect;\r
+\r
 \r
        /****************** MQTT Connection information setup. ********************/\r
-       /* This example does not use TLS and therefore won't work with AWS IoT. */\r
+\r
+       /* Set this flag to true if connecting to the AWS IoT MQTT broker. This\r
+        * example does not use TLS and therefore won't work with AWS IoT. */\r
        xConnectInfo.awsIotMqttMode = false;\r
 \r
        /* Start with a clean session i.e. direct the MQTT broker to discard any\r
@@ -373,13 +432,15 @@ IotMqttConnectInfo_t xConnectInfo = IOT_MQTT_CONNECT_INFO_INITIALIZER;
         * client gets disconnected. */\r
        xConnectInfo.pWillInfo = NULL;\r
 \r
-       /* Send an MQTT PING request every minute. */\r
+       /* Send an MQTT PING request every minute to keep the connection open if\r
+       there is no other MQTT traffic. */\r
        xConnectInfo.keepAliveSeconds = mqttexampleKEEP_ALIVE_SECONDS;\r
 \r
        /* The client identifier is used to uniquely identify this MQTT client to\r
-        * the MQTT broker. */\r
-       xConnectInfo.pClientIdentifier = mqttexampleCLIENT_IDENTIFIER;\r
-       xConnectInfo.clientIdentifierLength = ( uint16_t ) strlen( mqttexampleCLIENT_IDENTIFIER );\r
+        * the MQTT broker.  In a production device the identifier can be something\r
+        * unique, such as a device serial number. */\r
+       xConnectInfo.pClientIdentifier = pcClientID;\r
+       xConnectInfo.clientIdentifierLength = ( uint16_t ) strlen( pcClientID );\r
 \r
        /* This example does not use any authentication and therefore username and\r
         * password fields are not used. */\r
@@ -389,29 +450,31 @@ IotMqttConnectInfo_t xConnectInfo = IOT_MQTT_CONNECT_INFO_INITIALIZER;
        xConnectInfo.passwordLength = 0;\r
 \r
        /* Establish the connection to the MQTT broker - It is a blocking call and\r
-       will return only when connection is complete. */\r
+       will return only when connection is complete or a timeout occurs. */\r
        xResult = IotMqtt_Connect( &( xNetworkInfo ),\r
                                                           &( xConnectInfo ),\r
                                                           mqttexampleMQTT_TIMEOUT_MS,\r
-                                                          &( xMQTTConnection ) );\r
+                                                          xMQTTConnection );\r
        configASSERT( xResult == IOT_MQTT_SUCCESS );\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvMQTTSubscribe( void )\r
+static void prvMQTTSubscribe( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString )\r
 {\r
 IotMqttError_t xResult;\r
 IotMqttSubscription_t xMQTTSubscription;\r
 \r
-       /* Subscribe to the mqttexampleTOPIC topic filter. */\r
+       /* Subscribe to the mqttexampleTOPIC topic filter. The task handle is passed\r
+        * as the callback context which is used by the callback to send a task\r
+        * notification to this task.*/\r
        xMQTTSubscription.qos = IOT_MQTT_QOS_1;\r
-       xMQTTSubscription.pTopicFilter = mqttexampleTOPIC;\r
-       xMQTTSubscription.topicFilterLength = ( uint16_t ) strlen( mqttexampleTOPIC );\r
+       xMQTTSubscription.pTopicFilter = pcTopicString;\r
+       xMQTTSubscription.topicFilterLength = ( uint16_t ) strlen( pcTopicString );\r
        xMQTTSubscription.callback.pCallbackContext = ( void * ) xTaskGetCurrentTaskHandle();\r
-       xMQTTSubscription.callback.function = prvExample_PublishCallback;\r
+       xMQTTSubscription.callback.function = prvExample_OnMessageReceived;\r
 \r
        /* Use the synchronous API to subscribe - It is a blocking call and only\r
-        * returns when the subscribe operation is complete. */\r
+        * returns when the subscribe operation is complete or a timeout occurs. */\r
        xResult = IotMqtt_TimedSubscribe( xMQTTConnection,\r
                                                                          &( xMQTTSubscription ),\r
                                                                          1, /* We are subscribing to one topic filter. */\r
@@ -421,7 +484,7 @@ IotMqttSubscription_t xMQTTSubscription;
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvMQTTPublish( void )\r
+static void prvMQTTPublish( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString )\r
 {\r
 IotMqttError_t xResult;\r
 IotMqttPublishInfo_t xMQTTPublishInfo;\r
@@ -431,15 +494,15 @@ IotMqttPublishInfo_t xMQTTPublishInfo;
         * back to us. It is verified in the publish callback. */\r
        xMQTTPublishInfo.qos = IOT_MQTT_QOS_1;\r
        xMQTTPublishInfo.retain = false;\r
-       xMQTTPublishInfo.pTopicName = mqttexampleTOPIC;\r
-       xMQTTPublishInfo.topicNameLength = ( uint16_t ) strlen( mqttexampleTOPIC );\r
+       xMQTTPublishInfo.pTopicName = pcTopicString;\r
+       xMQTTPublishInfo.topicNameLength = ( uint16_t ) strlen( pcTopicString );\r
        xMQTTPublishInfo.pPayload = mqttexampleMESSAGE;\r
        xMQTTPublishInfo.payloadLength = strlen( mqttexampleMESSAGE );\r
        xMQTTPublishInfo.retryMs = mqttexamplePUBLISH_RETRY_MS;\r
        xMQTTPublishInfo.retryLimit = mqttexamplePUBLISH_RETRY_LIMIT;\r
 \r
        /* Use the synchronous API to publish - It is a blocking call and only\r
-        * returns when the publish operation is complete. */\r
+        * returns when the publish operation is complete or a timeout occurs. */\r
        xResult = IotMqtt_TimedPublish( xMQTTConnection,\r
                                                                        &( xMQTTPublishInfo ),\r
                                                                        0, /* flags - currently ignored. */\r
@@ -448,14 +511,14 @@ IotMqttPublishInfo_t xMQTTPublishInfo;
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvMQTTUnsubscribe( void )\r
+static void prvMQTTUnsubscribe( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString )\r
 {\r
 IotMqttError_t xResult;\r
 IotMqttSubscription_t xMQTTSubscription;\r
 \r
        /* Unsubscribe from the mqttexampleTOPIC topic filter. */\r
-       xMQTTSubscription.pTopicFilter = mqttexampleTOPIC;\r
-       xMQTTSubscription.topicFilterLength = ( uint16_t ) strlen( mqttexampleTOPIC );\r
+       xMQTTSubscription.pTopicFilter = pcTopicString;\r
+       xMQTTSubscription.topicFilterLength = ( uint16_t ) strlen( pcTopicString );\r
        /* The following members of the IotMqttSubscription_t are ignored by the\r
         * unsubscribe operation. Just initialize them to avoid "use of uninitialized\r
         * variable" warnings. */\r
@@ -464,7 +527,7 @@ IotMqttSubscription_t xMQTTSubscription;
        xMQTTSubscription.callback.function = NULL;\r
 \r
        /* Use the synchronous API to unsubscribe - It is a blocking call and only\r
-        * returns when the unsubscribe operation is complete. */\r
+        * returns when the unsubscribe operation is complete or a timeout occurs. */\r
        xResult = IotMqtt_TimedUnsubscribe( xMQTTConnection,\r
                                                                                &( xMQTTSubscription ),\r
                                                                                1, /* We are unsubscribing from one topic filter. */\r
@@ -474,7 +537,7 @@ IotMqttSubscription_t xMQTTSubscription;
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvMQTTDisconnect( void )\r
+static void prvMQTTDisconnect( IotMqttConnection_t xMQTTConnection )\r
 {\r
        /* Send a MQTT DISCONNECT packet to the MQTT broker to do a graceful\r
         * disconnect. */\r