From 0e002d3263e95c0e6d7715f155d22c2a467cd0b7 Mon Sep 17 00:00:00 2001 From: SarenaAWS <6563840+sarenameas@users.noreply.github.com> Date: Mon, 5 Oct 2020 13:23:55 -0700 Subject: [PATCH] Check the return value of MQTT_Disconnect in the demo loops (#318) * Check the return value of MQTT_Disconnect and fix comment line lengths. --- .../DemoTasks/KeepAliveMQTTExample.c | 43 ++++++++++------- .../DemoTasks/MutualAuthMQTTExample.c | 48 +++++++++++-------- .../DemoTasks/PlaintextMQTTExample.c | 32 +++++++------ .../DemoTasks/MutualAuthMQTTExample.c | 30 +++++++----- 4 files changed, 88 insertions(+), 65 deletions(-) diff --git a/FreeRTOS-Plus/Demo/FreeRTOS-IoT-Libraries-LTS-Beta2/mqtt/mqtt_keep_alive/DemoTasks/KeepAliveMQTTExample.c b/FreeRTOS-Plus/Demo/FreeRTOS-IoT-Libraries-LTS-Beta2/mqtt/mqtt_keep_alive/DemoTasks/KeepAliveMQTTExample.c index b80f2b2510..64a264ea18 100644 --- a/FreeRTOS-Plus/Demo/FreeRTOS-IoT-Libraries-LTS-Beta2/mqtt/mqtt_keep_alive/DemoTasks/KeepAliveMQTTExample.c +++ b/FreeRTOS-Plus/Demo/FreeRTOS-IoT-Libraries-LTS-Beta2/mqtt/mqtt_keep_alive/DemoTasks/KeepAliveMQTTExample.c @@ -431,7 +431,8 @@ static void prvMQTTDemoTask( void * pvParameters ) ulGlobalEntryTimeMs = prvGetTimeMs(); - /* Serialize a PINGREQ packet to send upon invoking the keep-alive timer callback. */ + /* Serialize a PINGREQ packet to send upon invoking the keep-alive timer + * callback. */ xMQTTStatus = MQTT_SerializePingreq( &xPingReqBuffer ); configASSERT( xMQTTStatus == MQTTSuccess ); @@ -442,8 +443,8 @@ static void prvMQTTDemoTask( void * pvParameters ) /* Attempt to connect to the MQTT broker. If connection fails, retry after * a timeout. Timeout value will be exponentially increased until the maximum * number of attempts are reached or the maximum timeout value is reached. - * The function returns a failure status if the TCP connection cannot be established - * to the broker after the configured number of attempts. */ + * The function returns a failure status if the TCP connection cannot be + * established to the broker after the configured number of attempts. */ xNetworkStatus = prvConnectToServerWithBackoffRetries( &xNetworkContext ); configASSERT( xNetworkStatus == PLAINTEXT_TRANSPORT_SUCCESS ); @@ -467,20 +468,21 @@ static void prvMQTTDemoTask( void * pvParameters ) /**************************** Subscribe. ******************************/ - /* If server rejected the subscription request, attempt to resubscribe to topic. - * Attempts are made according to the exponential backoff retry strategy - * implemented in retryUtils. */ + /* If server rejected the subscription request, attempt to resubscribe to + * topic. Attempts are made according to the exponential backoff retry + * strategy implemented in retryUtils. */ prvMQTTSubscribeWithBackoffRetries( &xMQTTContext ); - /**************************** Publish and Receive Loop. ******************************/ + /********************* Publish and Receive Loop. **********************/ /* Publish messages with QOS0, send and process Keep alive messages. */ for( ulPublishCount = 0; ulPublishCount < ulMaxPublishCount; ulPublishCount++ ) { LogInfo( ( "Publish to the MQTT topic %s.\r\n", mqttexampleTOPIC ) ); prvMQTTPublishToTopic( &xMQTTContext ); - /* Process incoming publish echo, since application subscribed to the same - * topic the broker will send publish message back to the application. */ + /* Process incoming publish echo, since application subscribed to the + * same topic the broker will send publish message back to the + * application. */ LogInfo( ( "Attempt to receive publish message from broker.\r\n" ) ); xMQTTStatus = MQTT_ReceiveLoop( &xMQTTContext, mqttexampleRECEIVE_LOOP_TIMEOUT_MS ); configASSERT( xMQTTStatus == MQTTSuccess ); @@ -490,7 +492,7 @@ static void prvMQTTDemoTask( void * pvParameters ) vTaskDelay( mqttexampleDELAY_BETWEEN_PUBLISHES ); } - /************************ Unsubscribe from the topic. **************************/ + /******************** Unsubscribe from the topic. *********************/ LogInfo( ( "Unsubscribe from the MQTT topic %s.\r\n", mqttexampleTOPIC ) ); prvMQTTUnsubscribeFromTopic( &xMQTTContext ); @@ -498,13 +500,15 @@ static void prvMQTTDemoTask( void * pvParameters ) xMQTTStatus = MQTT_ReceiveLoop( &xMQTTContext, mqttexampleRECEIVE_LOOP_TIMEOUT_MS ); configASSERT( xMQTTStatus == MQTTSuccess ); - /**************************** Disconnect. ******************************/ + /**************************** Disconnect. *****************************/ /* Send an MQTT Disconnect packet over the already connected TCP socket. - * There is no corresponding response for the disconnect packet. After sending - * disconnect, client must close the network connection. */ - LogInfo( ( "Disconnecting the MQTT connection with %s.\r\n", democonfigMQTT_BROKER_ENDPOINT ) ); - MQTT_Disconnect( &xMQTTContext ); + * There is no corresponding response for the disconnect packet. After + * sending disconnect, client must close the network connection. */ + LogInfo( ( "Disconnecting the MQTT connection with %s.\r\n", + democonfigMQTT_BROKER_ENDPOINT ) ); + xMQTTStatus = MQTT_Disconnect( &xMQTTContext ); + configASSERT( xMQTTStatus == MQTTSuccess ); /* Stop the keep-alive timer for the next iteration. */ xTimerStatus = xTimerStop( xKeepAliveTimer, 0 ); @@ -514,15 +518,18 @@ static void prvMQTTDemoTask( void * pvParameters ) xNetworkStatus = Plaintext_FreeRTOS_Disconnect( &xNetworkContext ); configASSERT( xNetworkStatus == PLAINTEXT_TRANSPORT_SUCCESS ); - /* Reset SUBACK status for each topic filter after completion of subscription request cycle. */ + /* Reset SUBACK status for each topic filter after completion of subscription + * request cycle. */ for( ulTopicCount = 0; ulTopicCount < mqttexampleTOPIC_COUNT; ulTopicCount++ ) { xTopicFilterContext[ ulTopicCount ].xSubAckStatus = MQTTSubAckFailure; } /* Wait for some time between two iterations to ensure that we do not - * bombard the public test mosquitto broker. */ - LogInfo( ( "prvMQTTDemoTask() completed an iteration successfully. Total free heap is %u.\r\n", xPortGetFreeHeapSize() ) ); + * bombard the broker. */ + LogInfo( ( "prvMQTTDemoTask() completed an iteration successfully. " + "Total free heap is %u.\r\n", + xPortGetFreeHeapSize() ) ); LogInfo( ( "Demo completed successfully.\r\n" ) ); LogInfo( ( "Short delay before starting the next iteration.... \r\n\r\n" ) ); vTaskDelay( mqttexampleDELAY_BETWEEN_DEMO_ITERATIONS ); diff --git a/FreeRTOS-Plus/Demo/FreeRTOS-IoT-Libraries-LTS-Beta2/mqtt/mqtt_mutual_auth/DemoTasks/MutualAuthMQTTExample.c b/FreeRTOS-Plus/Demo/FreeRTOS-IoT-Libraries-LTS-Beta2/mqtt/mqtt_mutual_auth/DemoTasks/MutualAuthMQTTExample.c index 584f4d5143..0a4a77c8cc 100644 --- a/FreeRTOS-Plus/Demo/FreeRTOS-IoT-Libraries-LTS-Beta2/mqtt/mqtt_mutual_auth/DemoTasks/MutualAuthMQTTExample.c +++ b/FreeRTOS-Plus/Demo/FreeRTOS-IoT-Libraries-LTS-Beta2/mqtt/mqtt_mutual_auth/DemoTasks/MutualAuthMQTTExample.c @@ -394,10 +394,11 @@ static void prvMQTTDemoTask( void * pvParameters ) /****************************** Connect. ******************************/ /* Attempt to establish TLS session with MQTT broker. If connection fails, - * retry after a timeout. Timeout value will be exponentially increased until - * the maximum number of attempts are reached or the maximum timeout value is reached. - * The function returns a failure status if the TCP connection cannot be established - * to the broker after the configured number of attempts. */ + * retry after a timeout. Timeout value will be exponentially increased + * until the maximum number of attempts are reached or the maximum timeout + * value is reached. The function returns a failure status if the TCP + * connection cannot be established to the broker after the configured + * number of attempts. */ xNetworkStatus = prvConnectToServerWithBackoffRetries( &xNetworkCredentials, &xNetworkContext ); configASSERT( xNetworkStatus == TLS_TRANSPORT_SUCCESS ); @@ -409,9 +410,9 @@ static void prvMQTTDemoTask( void * pvParameters ) /**************************** Subscribe. ******************************/ - /* If server rejected the subscription request, attempt to resubscribe to topic. - * Attempts are made according to the exponential backoff retry strategy - * implemented in retryUtils. */ + /* If server rejected the subscription request, attempt to resubscribe to + * topic. Attempts are made according to the exponential backoff retry + * strategy implemented in retryUtils. */ prvMQTTSubscribeWithBackoffRetries( &xMQTTContext ); /* Process incoming packet from the broker. After sending the subscribe, the @@ -424,15 +425,16 @@ static void prvMQTTDemoTask( void * pvParameters ) xMQTTStatus = MQTT_ProcessLoop( &xMQTTContext, mqttexamplePROCESS_LOOP_TIMEOUT_MS ); configASSERT( xMQTTStatus == MQTTSuccess ); - /**************************** Publish and Keep Alive Loop. ******************************/ + /****************** Publish and Keep Alive Loop. **********************/ /* Publish messages with QoS1, send and process Keep alive messages. */ for( ulPublishCount = 0; ulPublishCount < ulMaxPublishCount; ulPublishCount++ ) { LogInfo( ( "Publish to the MQTT topic %s.\r\n", mqttexampleTOPIC ) ); prvMQTTPublishToTopic( &xMQTTContext ); - /* Process incoming publish echo, since application subscribed to the same - * topic, the broker will send publish message back to the application. */ + /* Process incoming publish echo, since application subscribed to the + * same topic, the broker will send publish message back to the + * application. */ LogInfo( ( "Attempt to receive publish message from broker.\r\n" ) ); xMQTTStatus = MQTT_ProcessLoop( &xMQTTContext, mqttexamplePROCESS_LOOP_TIMEOUT_MS ); configASSERT( xMQTTStatus == MQTTSuccess ); @@ -442,7 +444,7 @@ static void prvMQTTDemoTask( void * pvParameters ) vTaskDelay( mqttexampleDELAY_BETWEEN_PUBLISHES_TICKS ); } - /************************ Unsubscribe from the topic. **************************/ + /******************** Unsubscribe from the topic. *********************/ LogInfo( ( "Unsubscribe from the MQTT topic %s.\r\n", mqttexampleTOPIC ) ); prvMQTTUnsubscribeFromTopic( &xMQTTContext ); @@ -450,26 +452,32 @@ static void prvMQTTDemoTask( void * pvParameters ) xMQTTStatus = MQTT_ProcessLoop( &xMQTTContext, mqttexamplePROCESS_LOOP_TIMEOUT_MS ); configASSERT( xMQTTStatus == MQTTSuccess ); - /**************************** Disconnect. ******************************/ + /**************************** Disconnect. *****************************/ - /* Send an MQTT Disconnect packet over the already connected TLS over TCP connection. - * There is no corresponding response for the disconnect packet. After sending - * disconnect, client must close the network connection. */ - LogInfo( ( "Disconnecting the MQTT connection with %s.\r\n", democonfigMQTT_BROKER_ENDPOINT ) ); - MQTT_Disconnect( &xMQTTContext ); + /* Send an MQTT Disconnect packet over the already connected TLS over + * TCP connection. There is no corresponding response for the disconnect + * packet. After sending disconnect, client must close the network + * connection. */ + LogInfo( ( "Disconnecting the MQTT connection with %s.\r\n", + democonfigMQTT_BROKER_ENDPOINT ) ); + xMQTTStatus = MQTT_Disconnect( &xMQTTContext ); + configASSERT( xMQTTStatus == MQTTSuccess ); /* Close the network connection. */ TLS_FreeRTOS_Disconnect( &xNetworkContext ); - /* Reset SUBACK status for each topic filter after completion of subscription request cycle. */ + /* Reset SUBACK status for each topic filter after completion of + * subscription request cycle. */ for( ulTopicCount = 0; ulTopicCount < mqttexampleTOPIC_COUNT; ulTopicCount++ ) { xTopicFilterContext[ ulTopicCount ].xSubAckStatus = MQTTSubAckFailure; } /* Wait for some time between two iterations to ensure that we do not - * the broker. */ - LogInfo( ( "prvMQTTDemoTask() completed an iteration successfully. Total free heap is %u.\r\n", xPortGetFreeHeapSize() ) ); + * bombard the broker. */ + LogInfo( ( "prvMQTTDemoTask() completed an iteration successfully. " + "Total free heap is %u.\r\n", + xPortGetFreeHeapSize() ) ); LogInfo( ( "Demo completed successfully.\r\n" ) ); LogInfo( ( "Short delay before starting the next iteration.... \r\n\r\n" ) ); vTaskDelay( mqttexampleDELAY_BETWEEN_DEMO_ITERATIONS_TICKS ); diff --git a/FreeRTOS-Plus/Demo/FreeRTOS-IoT-Libraries-LTS-Beta2/mqtt/mqtt_plain_text/DemoTasks/PlaintextMQTTExample.c b/FreeRTOS-Plus/Demo/FreeRTOS-IoT-Libraries-LTS-Beta2/mqtt/mqtt_plain_text/DemoTasks/PlaintextMQTTExample.c index e68dc775e3..51050c6353 100644 --- a/FreeRTOS-Plus/Demo/FreeRTOS-IoT-Libraries-LTS-Beta2/mqtt/mqtt_plain_text/DemoTasks/PlaintextMQTTExample.c +++ b/FreeRTOS-Plus/Demo/FreeRTOS-IoT-Libraries-LTS-Beta2/mqtt/mqtt_plain_text/DemoTasks/PlaintextMQTTExample.c @@ -354,8 +354,8 @@ static void prvMQTTDemoTask( void * pvParameters ) /* Attempt to connect to the MQTT broker. If connection fails, retry after * a timeout. Timeout value will be exponentially increased until the maximum * number of attempts are reached or the maximum timeout value is reached. - * The function returns a failure status if the TCP connection cannot be established - * to the broker after the configured number of attempts. */ + * The function returns a failure status if the TCP connection cannot be + * established to the broker after the configured number of attempts. */ xNetworkStatus = prvConnectToServerWithBackoffRetries( &xNetworkContext ); configASSERT( xNetworkStatus == PLAINTEXT_TRANSPORT_SUCCESS ); @@ -366,12 +366,12 @@ static void prvMQTTDemoTask( void * pvParameters ) /**************************** Subscribe. ******************************/ - /* If server rejected the subscription request, attempt to resubscribe to topic. - * Attempts are made according to the exponential backoff retry strategy - * implemented in retryUtils. */ + /* If server rejected the subscription request, attempt to resubscribe to + * topic. Attempts are made according to the exponential backoff retry + * strategy implemented in retryUtils. */ prvMQTTSubscribeWithBackoffRetries( &xMQTTContext ); - /**************************** Publish and Keep Alive Loop. ******************************/ + /******************* Publish and Keep Alive Loop. *********************/ /* Publish messages with QoS0, send and process Keep alive messages. */ for( ulPublishCount = 0; ulPublishCount < ulMaxPublishCount; ulPublishCount++ ) { @@ -389,7 +389,7 @@ static void prvMQTTDemoTask( void * pvParameters ) vTaskDelay( mqttexampleDELAY_BETWEEN_PUBLISHES ); } - /************************ Unsubscribe from the topic. **************************/ + /******************** Unsubscribe from the topic. *********************/ LogInfo( ( "Unsubscribe from the MQTT topic %s.\r\n", mqttexampleTOPIC ) ); prvMQTTUnsubscribeFromTopic( &xMQTTContext ); @@ -397,13 +397,15 @@ static void prvMQTTDemoTask( void * pvParameters ) xMQTTStatus = MQTT_ProcessLoop( &xMQTTContext, mqttexamplePROCESS_LOOP_TIMEOUT_MS ); configASSERT( xMQTTStatus == MQTTSuccess ); - /**************************** Disconnect. ******************************/ + /**************************** Disconnect. *****************************/ /* Send an MQTT Disconnect packet over the already connected TCP socket. - * There is no corresponding response for the disconnect packet. After sending - * disconnect, client must close the network connection. */ - LogInfo( ( "Disconnecting the MQTT connection with %s.\r\n", democonfigMQTT_BROKER_ENDPOINT ) ); - MQTT_Disconnect( &xMQTTContext ); + * There is no corresponding response for the disconnect packet. After + * sending disconnect, client must close the network connection. */ + LogInfo( ( "Disconnecting the MQTT connection with %s.\r\n", + democonfigMQTT_BROKER_ENDPOINT ) ); + xMQTTStatus = MQTT_Disconnect( &xMQTTContext ); + configASSERT( xMQTTStatus == MQTTSuccess ); /* Close the network connection. */ xNetworkStatus = Plaintext_FreeRTOS_Disconnect( &xNetworkContext ); @@ -416,8 +418,10 @@ static void prvMQTTDemoTask( void * pvParameters ) } /* Wait for some time between two iterations to ensure that we do not - * bombard the public test mosquitto broker. */ - LogInfo( ( "prvMQTTDemoTask() completed an iteration successfully. Total free heap is %u.\r\n", xPortGetFreeHeapSize() ) ); + * bombard the broker. */ + LogInfo( ( "prvMQTTDemoTask() completed an iteration successfully. " + "Total free heap is %u.\r\n", + xPortGetFreeHeapSize() ) ); LogInfo( ( "Demo completed successfully.\r\n" ) ); LogInfo( ( "Short delay before starting the next iteration.... \r\n\r\n" ) ); vTaskDelay( mqttexampleDELAY_BETWEEN_DEMO_ITERATIONS ); diff --git a/FreeRTOS-Plus/Demo/FreeRTOS-IoT-Libraries-LTS-Beta2/pkcs11/mqtt_mutal_auth_with_pkcs11/DemoTasks/MutualAuthMQTTExample.c b/FreeRTOS-Plus/Demo/FreeRTOS-IoT-Libraries-LTS-Beta2/pkcs11/mqtt_mutal_auth_with_pkcs11/DemoTasks/MutualAuthMQTTExample.c index 940daa4c12..d86843bd5f 100644 --- a/FreeRTOS-Plus/Demo/FreeRTOS-IoT-Libraries-LTS-Beta2/pkcs11/mqtt_mutal_auth_with_pkcs11/DemoTasks/MutualAuthMQTTExample.c +++ b/FreeRTOS-Plus/Demo/FreeRTOS-IoT-Libraries-LTS-Beta2/pkcs11/mqtt_mutal_auth_with_pkcs11/DemoTasks/MutualAuthMQTTExample.c @@ -341,8 +341,8 @@ static void prvMQTTDemoTask( void * pvParameters ) { /****************************** Connect. ******************************/ - /* Establish a TLS connection with the MQTT broker. This example connects to - * the MQTT broker as specified by democonfigMQTT_BROKER_ENDPOINT and + /* Establish a TLS connection with the MQTT broker. This example connects + * to the MQTT broker as specified by democonfigMQTT_BROKER_ENDPOINT and * democonfigMQTT_BROKER_PORT in the demo_config.h file. */ LogInfo( ( "Creating a TLS connection to %s:%u.\r\n", democonfigMQTT_BROKER_ENDPOINT, @@ -378,15 +378,15 @@ static void prvMQTTDemoTask( void * pvParameters ) xMQTTStatus = MQTT_ProcessLoop( &xMQTTContext, mqttexamplePROCESS_LOOP_TIMEOUT_MS ); configASSERT( xMQTTStatus == MQTTSuccess ); - /**************************** Publish and Keep Alive Loop. ******************************/ + /******************* Publish and Keep Alive Loop. *********************/ /* Publish messages with QoS1, send and process Keep alive messages. */ for( ulPublishCount = 0; ulPublishCount < ulMaxPublishCount; ulPublishCount++ ) { LogInfo( ( "Publish to the MQTT topic %s.\r\n", mqttexampleTOPIC ) ); prvMQTTPublishToTopic( &xMQTTContext ); - /* Process incoming publish echo, since application subscribed to the same - * topic, the broker will send publish message back to the application. */ + /* Process incoming publish echo, since application subscribed to the + * same topic, the broker will send publish message back to the application. */ LogInfo( ( "Attempt to receive publish message from broker.\r\n" ) ); xMQTTStatus = MQTT_ProcessLoop( &xMQTTContext, mqttexamplePROCESS_LOOP_TIMEOUT_MS ); configASSERT( xMQTTStatus == MQTTSuccess ); @@ -396,7 +396,7 @@ static void prvMQTTDemoTask( void * pvParameters ) vTaskDelay( mqttexampleDELAY_BETWEEN_PUBLISHES_TICKS ); } - /************************ Unsubscribe from the topic. **************************/ + /******************** Unsubscribe from the topic. *********************/ LogInfo( ( "Unsubscribe from the MQTT topic %s.\r\n", mqttexampleTOPIC ) ); prvMQTTUnsubscribeFromTopic( &xMQTTContext ); @@ -406,18 +406,22 @@ static void prvMQTTDemoTask( void * pvParameters ) /**************************** Disconnect. ******************************/ - /* Send an MQTT Disconnect packet over the already connected TLS over TCP connection. - * There is no corresponding response for the disconnect packet. After sending - * disconnect, client must close the network connection. */ - LogInfo( ( "Disconnecting the MQTT connection with %s.\r\n", democonfigMQTT_BROKER_ENDPOINT ) ); - MQTT_Disconnect( &xMQTTContext ); + /* Send an MQTT Disconnect packet over the already connected TLS over TCP + * connection. There is no corresponding response for the disconnect packet. + * After sending disconnect, client must close the network connection. */ + LogInfo( ( "Disconnecting the MQTT connection with %s.\r\n", + democonfigMQTT_BROKER_ENDPOINT ) ); + xMQTTStatus = MQTT_Disconnect( &xMQTTContext ); + configASSERT( xMQTTStatus == MQTTSuccess ); /* Close the network connection. */ TLS_FreeRTOS_Disconnect( &xNetworkContext ); /* Wait for some time between two iterations to ensure that we do not - * the broker. */ - LogInfo( ( "prvMQTTDemoTask() completed an iteration successfully. Total free heap is %u.\r\n", xPortGetFreeHeapSize() ) ); + * bombard the broker. */ + LogInfo( ( "prvMQTTDemoTask() completed an iteration successfully. " + "Total free heap is %u.\r\n", + xPortGetFreeHeapSize() ) ); LogInfo( ( "Demo completed successfully.\r\n" ) ); LogInfo( ( "Short delay before starting the next iteration.... \r\n\r\n" ) ); vTaskDelay( mqttexampleDELAY_BETWEEN_DEMO_ITERATIONS_TICKS );