Skip to content

Commit 6bd201a

Browse files
Fixed multiple Sub/Unsub request sending logic (#229)
* Fixed multiple Sub/Unsub request sending logic * Fix memory statistics * Fix memory statistics
1 parent 885db0c commit 6bd201a

File tree

4 files changed

+456
-15
lines changed

4 files changed

+456
-15
lines changed

docs/doxygen/include/size_table.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</tr>
1010
<tr>
1111
<td>core_mqtt.c</td>
12-
<td><center>4.0K</center></td>
12+
<td><center>4.1K</center></td>
1313
<td><center>3.4K</center></td>
1414
</tr>
1515
<tr>
@@ -24,7 +24,7 @@
2424
</tr>
2525
<tr>
2626
<td><b>Total estimates</b></td>
27-
<td><b><center>8.5K</center></b></td>
27+
<td><b><center>8.6K</center></b></td>
2828
<td><b><center>6.9K</center></b></td>
2929
</tr>
3030
</table>

source/core_mqtt.c

+26-12
Original file line numberDiff line numberDiff line change
@@ -1881,13 +1881,14 @@ static MQTTStatus_t sendSubscribeWithoutCopy( MQTTContext_t * pContext,
18811881
uint8_t * pIndex;
18821882
TransportOutVector_t pIoVector[ MQTT_SUB_UNSUB_MAX_VECTORS ];
18831883
TransportOutVector_t * pIterator;
1884-
uint8_t serializedTopicFieldLength[ 2 ];
1884+
uint8_t serializedTopicFieldLength[ MQTT_SUB_UNSUB_MAX_VECTORS ][ 2 ];
18851885
size_t totalPacketLength = 0U;
18861886
size_t ioVectorLength = 0U;
18871887
size_t subscriptionsSent = 0U;
18881888
/* For subscribe, only three vector slots are required per topic string. */
18891889
const size_t subscriptionStringVectorSlots = 3U;
18901890
size_t vectorsAdded;
1891+
size_t topicFieldLengthIndex;
18911892

18921893
/* The vector array should be at least three element long as the topic
18931894
* string needs these many vector elements to be stored. */
@@ -1913,13 +1914,16 @@ static MQTTStatus_t sendSubscribeWithoutCopy( MQTTContext_t * pContext,
19131914

19141915
while( ( status == MQTTSuccess ) && ( subscriptionsSent < subscriptionCount ) )
19151916
{
1917+
/* Reset the index for next iteration. */
1918+
topicFieldLengthIndex = 0;
1919+
19161920
/* Check whether the subscription topic (with QoS) will fit in the
19171921
* given vector. */
19181922
while( ( ioVectorLength <= ( MQTT_SUB_UNSUB_MAX_VECTORS - subscriptionStringVectorSlots ) ) &&
19191923
( subscriptionsSent < subscriptionCount ) )
19201924
{
19211925
/* The topic filter gets sent next. */
1922-
vectorsAdded = addEncodedStringToVector( serializedTopicFieldLength,
1926+
vectorsAdded = addEncodedStringToVector( serializedTopicFieldLength[ topicFieldLengthIndex ],
19231927
pSubscriptionList[ subscriptionsSent ].pTopicFilter,
19241928
pSubscriptionList[ subscriptionsSent ].topicFilterLength,
19251929
pIterator,
@@ -1936,11 +1940,14 @@ static MQTTStatus_t sendSubscribeWithoutCopy( MQTTContext_t * pContext,
19361940
/* Increment the pointer. */
19371941
pIterator++;
19381942

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;
19421946

19431947
subscriptionsSent++;
1948+
1949+
/* The index needs to be updated for next iteration. */
1950+
topicFieldLengthIndex++;
19441951
}
19451952

19461953
if( sendMessageVector( pContext,
@@ -1974,17 +1981,18 @@ static MQTTStatus_t sendUnsubscribeWithoutCopy( MQTTContext_t * pContext,
19741981
uint8_t * pIndex;
19751982
TransportOutVector_t pIoVector[ MQTT_SUB_UNSUB_MAX_VECTORS ];
19761983
TransportOutVector_t * pIterator;
1977-
uint8_t serializedTopicFieldLength[ 2 ];
1984+
uint8_t serializedTopicFieldLength[ MQTT_SUB_UNSUB_MAX_VECTORS ][ 2 ];
19781985
size_t totalPacketLength = 0U;
19791986
size_t unsubscriptionsSent = 0U;
19801987
size_t ioVectorLength = 0U;
19811988
/* For unsubscribe, only two vector slots are required per topic string. */
1982-
const size_t subscriptionStringVectorSlots = 2U;
1989+
const size_t unsubscribeStringVectorSlots = 2U;
19831990
size_t vectorsAdded;
1991+
size_t topicFieldLengthIndex;
19841992

19851993
/* The vector array should be at least three element long as the topic
19861994
* 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 );
19881996

19891997
pIndex = unsubscribeheader;
19901998
pIterator = pIoVector;
@@ -2006,23 +2014,29 @@ static MQTTStatus_t sendUnsubscribeWithoutCopy( MQTTContext_t * pContext,
20062014

20072015
while( ( status == MQTTSuccess ) && ( unsubscriptionsSent < subscriptionCount ) )
20082016
{
2017+
/* Reset the index for next iteration. */
2018+
topicFieldLengthIndex = 0;
2019+
20092020
/* 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 ) ) &&
20112022
( unsubscriptionsSent < subscriptionCount ) )
20122023
{
20132024
/* The topic filter gets sent next. */
2014-
vectorsAdded = addEncodedStringToVector( serializedTopicFieldLength,
2025+
vectorsAdded = addEncodedStringToVector( serializedTopicFieldLength[ topicFieldLengthIndex ],
20152026
pSubscriptionList[ unsubscriptionsSent ].pTopicFilter,
20162027
pSubscriptionList[ unsubscriptionsSent ].topicFilterLength,
20172028
pIterator,
20182029
&totalPacketLength );
20192030

20202031
/* Update the iterator to point to the next empty location. */
20212032
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;
20242035

20252036
unsubscriptionsSent++;
2037+
2038+
/* Update the index for next iteration. */
2039+
topicFieldLengthIndex++;
20262040
}
20272041

20282042
if( sendMessageVector( pContext, pIoVector, ioVectorLength ) != ( int32_t ) totalPacketLength )

test/unit-test/core_mqtt_config.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,6 @@ struct NetworkContext
7575
uint8_t ** buffer;
7676
};
7777

78-
#define MQTT_SUB_UNSUB_MAX_VECTORS ( 8U )
78+
#define MQTT_SUB_UNSUB_MAX_VECTORS ( 6U )
7979

8080
#endif /* ifndef CORE_MQTT_CONFIG_H_ */

0 commit comments

Comments
 (0)