156156
157157/*-----------------------------------------------------------*/
158158
159- /**
160- * @brief Packet Identifier updated when an ACK packet is received.
161- *
162- * It is used to match an expected ACK for a transmitted packet.
163- */
164- static uint16_t globalAckPacketIdentifier = 0U ;
165-
166159/**
167160 * @brief Packet Identifier generated when Subscribe request was sent to the broker;
168161 * it is used to match received Subscribe ACK to the transmitted subscribe.
@@ -354,21 +347,17 @@ static void eventCallback( MQTTContext_t * pContext,
354347 MQTTDeserializedInfo_t * pDeserializedInfo );
355348
356349/**
357- * @brief Wait for an expected ACK packet to be received.
358- *
359- * This function handles waiting for an expected ACK packet by calling
360- * #MQTT_ProcessLoop and waiting for #mqttCallback to set the global ACK
361- * packet identifier to the expected ACK packet identifier.
350+ * @brief Call #MQTT_ProcessLoop in a loop for the duration of a timeout or
351+ * #MQTT_ProcessLoop returns a failure.
362352 *
363353 * @param[in] pMqttContext MQTT context pointer.
364- * @param[in] usPacketIdentifier Packet identifier for expected ACK packet.
365- * @param[in] ulTimeout Maximum duration to wait for expected ACK packet.
354+ * @param[in] ulTimeoutMs Duration to call #MQTT_ProcessLoop for.
366355 *
367- * @return true if the expected ACK packet was received, false otherwise.
356+ * @return Returns the return value of the last call to #MQTT_ProcessLoop unless
357+ * the last call returned MQTTNeedMoreBytes -in that case it returns MQTTSuccess.
368358 */
369- static bool waitForPacketAck ( MQTTContext_t * pMqttContext ,
370- uint16_t usPacketIdentifier ,
371- uint32_t ulTimeout );
359+ static MQTTStatus_t processLoopWithTimeout ( MQTTContext_t * pMqttContext ,
360+ uint32_t ulTimeoutMs );
372361
373362/*-----------------------------------------------------------*/
374363
@@ -550,9 +539,6 @@ static void eventCallback( MQTTContext_t * pContext,
550539 packetIdentifier ) );
551540 /* Make sure ACK packet identifier matches with Request packet identifier. */
552541 TEST_ASSERT_EQUAL ( globalSubscribePacketIdentifier , packetIdentifier );
553-
554- /* Update the global ACK packet identifier. */
555- globalAckPacketIdentifier = packetIdentifier ;
556542 break ;
557543
558544 case MQTT_PACKET_TYPE_PINGRESP :
@@ -570,9 +556,6 @@ static void eventCallback( MQTTContext_t * pContext,
570556 packetIdentifier ) );
571557 /* Make sure ACK packet identifier matches with Request packet identifier. */
572558 TEST_ASSERT_EQUAL ( globalUnsubscribePacketIdentifier , packetIdentifier );
573-
574- /* Update the global ACK packet identifier. */
575- globalAckPacketIdentifier = packetIdentifier ;
576559 break ;
577560
578561 case MQTT_PACKET_TYPE_PUBACK :
@@ -582,9 +565,6 @@ static void eventCallback( MQTTContext_t * pContext,
582565 /* Make sure ACK packet identifier matches with Request packet identifier. */
583566 TEST_ASSERT_EQUAL ( globalPublishPacketIdentifier , packetIdentifier );
584567
585- /* Update the global ACK packet identifier. */
586- globalAckPacketIdentifier = packetIdentifier ;
587-
588568 LogDebug ( ( "Received PUBACK: PacketID=%u" ,
589569 packetIdentifier ) );
590570 break ;
@@ -662,7 +642,7 @@ static MQTTStatus_t subscribeToTopic( MQTTContext_t * pContext,
662642
663643 if ( mqttStatus == MQTTSuccess )
664644 {
665- TEST_ASSERT_TRUE ( waitForPacketAck ( pContext , globalSubscribePacketIdentifier , MQTT_PROCESS_LOOP_TIMEOUT_MS ) );
645+ mqttStatus = processLoopWithTimeout ( pContext , MQTT_PROCESS_LOOP_TIMEOUT_MS );
666646 }
667647
668648 if ( mqttStatus == MQTTSuccess )
@@ -705,7 +685,7 @@ static MQTTStatus_t unsubscribeFromTopic( MQTTContext_t * pContext,
705685
706686 if ( mqttStatus == MQTTSuccess )
707687 {
708- TEST_ASSERT_TRUE ( waitForPacketAck ( pContext , globalUnsubscribePacketIdentifier , MQTT_PROCESS_LOOP_TIMEOUT_MS ) );
688+ mqttStatus = processLoopWithTimeout ( pContext , MQTT_PROCESS_LOOP_TIMEOUT_MS );
709689 }
710690
711691 if ( mqttStatus == MQTTSuccess )
@@ -746,7 +726,7 @@ static MQTTStatus_t publishToTopic( MQTTContext_t * pContext,
746726
747727 if ( mqttStatus == MQTTSuccess )
748728 {
749- TEST_ASSERT_TRUE ( waitForPacketAck ( pContext , globalPublishPacketIdentifier , MQTT_PROCESS_LOOP_TIMEOUT_MS ) );
729+ mqttStatus = processLoopWithTimeout ( pContext , MQTT_PROCESS_LOOP_TIMEOUT_MS );
750730 }
751731
752732 if ( mqttStatus == MQTTSuccess )
@@ -900,50 +880,32 @@ static void testSequence( const char * pShadowName,
900880 MQTTQoS0 ) );
901881}
902882
903- static bool waitForPacketAck ( MQTTContext_t * pMqttContext ,
904- uint16_t usPacketIdentifier ,
905- uint32_t ulTimeout )
883+ static MQTTStatus_t processLoopWithTimeout ( MQTTContext_t * pMqttContext ,
884+ uint32_t ulTimeoutMs )
906885{
907- uint32_t ulMqttProcessLoopEntryTime ;
908886 uint32_t ulMqttProcessLoopTimeoutTime ;
909887 uint32_t ulCurrentTime ;
910888
911889 MQTTStatus_t eMqttStatus = MQTTSuccess ;
912- bool returnStatus = false;
913-
914- /* Reset the ACK packet identifier being received. */
915- globalAckPacketIdentifier = 0U ;
916890
917891 ulCurrentTime = pMqttContext -> getTime ();
918- ulMqttProcessLoopEntryTime = ulCurrentTime ;
919- ulMqttProcessLoopTimeoutTime = ulCurrentTime + ulTimeout ;
892+ ulMqttProcessLoopTimeoutTime = ulCurrentTime + ulTimeoutMs ;
920893
921- /* Call MQTT_ProcessLoop multiple times until the expected packet ACK
922- * is received, a timeout happens, or MQTT_ProcessLoop fails. */
923- while ( ( globalAckPacketIdentifier != usPacketIdentifier ) &&
924- ( ulCurrentTime < ulMqttProcessLoopTimeoutTime ) &&
894+ /* Call MQTT_ProcessLoop multiple times a timeout happens, or
895+ * MQTT_ProcessLoop fails. */
896+ while ( ( ulCurrentTime < ulMqttProcessLoopTimeoutTime ) &&
925897 ( eMqttStatus == MQTTSuccess || eMqttStatus == MQTTNeedMoreBytes ) )
926898 {
927- /* Event callback will set #globalAckPacketIdentifier when receiving
928- * appropriate packet. */
929899 eMqttStatus = MQTT_ProcessLoop ( pMqttContext );
930900 ulCurrentTime = pMqttContext -> getTime ();
931901 }
932902
933- if ( ( ( eMqttStatus != MQTTSuccess ) && ( eMqttStatus != MQTTNeedMoreBytes ) ) ||
934- ( globalAckPacketIdentifier != usPacketIdentifier ) )
935- {
936- LogError ( ( "MQTT_ProcessLoop failed to receive ACK packet: Expected ACK Packet ID=%02X, LoopDuration=%u, Status=%s" ,
937- usPacketIdentifier ,
938- ( ulCurrentTime - ulMqttProcessLoopEntryTime ),
939- MQTT_Status_strerror ( eMqttStatus ) ) );
940- }
941- else
903+ if ( eMqttStatus == MQTTNeedMoreBytes )
942904 {
943- returnStatus = true ;
905+ eMqttStatus = MQTTSuccess ;
944906 }
945907
946- return returnStatus ;
908+ return eMqttStatus ;
947909}
948910
949911/* ============================ UNITY FIXTURES ============================ */
0 commit comments