@@ -1881,13 +1881,14 @@ static MQTTStatus_t sendSubscribeWithoutCopy( MQTTContext_t * pContext,
1881
1881
uint8_t * pIndex ;
1882
1882
TransportOutVector_t pIoVector [ MQTT_SUB_UNSUB_MAX_VECTORS ];
1883
1883
TransportOutVector_t * pIterator ;
1884
- uint8_t serializedTopicFieldLength [ 2 ];
1884
+ uint8_t serializedTopicFieldLength [ MQTT_SUB_UNSUB_MAX_VECTORS ][ 2 ];
1885
1885
size_t totalPacketLength = 0U ;
1886
1886
size_t ioVectorLength = 0U ;
1887
1887
size_t subscriptionsSent = 0U ;
1888
1888
/* For subscribe, only three vector slots are required per topic string. */
1889
1889
const size_t subscriptionStringVectorSlots = 3U ;
1890
1890
size_t vectorsAdded ;
1891
+ size_t topicFieldLengthIndex ;
1891
1892
1892
1893
/* The vector array should be at least three element long as the topic
1893
1894
* string needs these many vector elements to be stored. */
@@ -1913,13 +1914,16 @@ static MQTTStatus_t sendSubscribeWithoutCopy( MQTTContext_t * pContext,
1913
1914
1914
1915
while ( ( status == MQTTSuccess ) && ( subscriptionsSent < subscriptionCount ) )
1915
1916
{
1917
+ /* Reset the index for next iteration. */
1918
+ topicFieldLengthIndex = 0 ;
1919
+
1916
1920
/* Check whether the subscription topic (with QoS) will fit in the
1917
1921
* given vector. */
1918
1922
while ( ( ioVectorLength <= ( MQTT_SUB_UNSUB_MAX_VECTORS - subscriptionStringVectorSlots ) ) &&
1919
1923
( subscriptionsSent < subscriptionCount ) )
1920
1924
{
1921
1925
/* The topic filter gets sent next. */
1922
- vectorsAdded = addEncodedStringToVector ( serializedTopicFieldLength ,
1926
+ vectorsAdded = addEncodedStringToVector ( serializedTopicFieldLength [ topicFieldLengthIndex ] ,
1923
1927
pSubscriptionList [ subscriptionsSent ].pTopicFilter ,
1924
1928
pSubscriptionList [ subscriptionsSent ].topicFilterLength ,
1925
1929
pIterator ,
@@ -1936,11 +1940,14 @@ static MQTTStatus_t sendSubscribeWithoutCopy( MQTTContext_t * pContext,
1936
1940
/* Increment the pointer. */
1937
1941
pIterator ++ ;
1938
1942
1939
- /* Two slots get used by the topic string length and topic string. And
1940
- * one slot gets used by the quality of service. */
1941
- ioVectorLength += subscriptionStringVectorSlots ;
1943
+ /* Two slots get used by the topic string length and topic string.
1944
+ * One slot gets used by the quality of service. */
1945
+ ioVectorLength += vectorsAdded + 1U ;
1942
1946
1943
1947
subscriptionsSent ++ ;
1948
+
1949
+ /* The index needs to be updated for next iteration. */
1950
+ topicFieldLengthIndex ++ ;
1944
1951
}
1945
1952
1946
1953
if ( sendMessageVector ( pContext ,
@@ -1974,17 +1981,18 @@ static MQTTStatus_t sendUnsubscribeWithoutCopy( MQTTContext_t * pContext,
1974
1981
uint8_t * pIndex ;
1975
1982
TransportOutVector_t pIoVector [ MQTT_SUB_UNSUB_MAX_VECTORS ];
1976
1983
TransportOutVector_t * pIterator ;
1977
- uint8_t serializedTopicFieldLength [ 2 ];
1984
+ uint8_t serializedTopicFieldLength [ MQTT_SUB_UNSUB_MAX_VECTORS ][ 2 ];
1978
1985
size_t totalPacketLength = 0U ;
1979
1986
size_t unsubscriptionsSent = 0U ;
1980
1987
size_t ioVectorLength = 0U ;
1981
1988
/* For unsubscribe, only two vector slots are required per topic string. */
1982
- const size_t subscriptionStringVectorSlots = 2U ;
1989
+ const size_t unsubscribeStringVectorSlots = 2U ;
1983
1990
size_t vectorsAdded ;
1991
+ size_t topicFieldLengthIndex ;
1984
1992
1985
1993
/* The vector array should be at least three element long as the topic
1986
1994
* string needs these many vector elements to be stored. */
1987
- assert ( MQTT_SUB_UNSUB_MAX_VECTORS >= subscriptionStringVectorSlots );
1995
+ assert ( MQTT_SUB_UNSUB_MAX_VECTORS >= unsubscribeStringVectorSlots );
1988
1996
1989
1997
pIndex = unsubscribeheader ;
1990
1998
pIterator = pIoVector ;
@@ -2006,23 +2014,29 @@ static MQTTStatus_t sendUnsubscribeWithoutCopy( MQTTContext_t * pContext,
2006
2014
2007
2015
while ( ( status == MQTTSuccess ) && ( unsubscriptionsSent < subscriptionCount ) )
2008
2016
{
2017
+ /* Reset the index for next iteration. */
2018
+ topicFieldLengthIndex = 0 ;
2019
+
2009
2020
/* Check whether the subscription topic will fit in the given vector. */
2010
- while ( ( ioVectorLength <= ( MQTT_SUB_UNSUB_MAX_VECTORS - subscriptionStringVectorSlots ) ) &&
2021
+ while ( ( ioVectorLength <= ( MQTT_SUB_UNSUB_MAX_VECTORS - unsubscribeStringVectorSlots ) ) &&
2011
2022
( unsubscriptionsSent < subscriptionCount ) )
2012
2023
{
2013
2024
/* The topic filter gets sent next. */
2014
- vectorsAdded = addEncodedStringToVector ( serializedTopicFieldLength ,
2025
+ vectorsAdded = addEncodedStringToVector ( serializedTopicFieldLength [ topicFieldLengthIndex ] ,
2015
2026
pSubscriptionList [ unsubscriptionsSent ].pTopicFilter ,
2016
2027
pSubscriptionList [ unsubscriptionsSent ].topicFilterLength ,
2017
2028
pIterator ,
2018
2029
& totalPacketLength );
2019
2030
2020
2031
/* Update the iterator to point to the next empty location. */
2021
2032
pIterator = & pIterator [ vectorsAdded ];
2022
- /* Two slots get used by the topic string length and topic string . */
2023
- ioVectorLength += subscriptionStringVectorSlots ;
2033
+ /* Update the total count based on how many vectors were added . */
2034
+ ioVectorLength += vectorsAdded ;
2024
2035
2025
2036
unsubscriptionsSent ++ ;
2037
+
2038
+ /* Update the index for next iteration. */
2039
+ topicFieldLengthIndex ++ ;
2026
2040
}
2027
2041
2028
2042
if ( sendMessageVector ( pContext , pIoVector , ioVectorLength ) != ( int32_t ) totalPacketLength )
0 commit comments