Skip to content

Commit faa766f

Browse files
Update MQTT_GetSubAckStatusCodes
1 parent 8ce6d7b commit faa766f

File tree

5 files changed

+85
-16
lines changed

5 files changed

+85
-16
lines changed

docs/doxygen/include/size_table.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</tr>
2525
<tr>
2626
<td>core_mqtt_serializer_private.c</td>
27-
<td><center>0.7K</center></td>
27+
<td><center>0.8K</center></td>
2828
<td><center>0.7K</center></td>
2929
</tr>
3030
<tr>
@@ -39,7 +39,7 @@
3939
</tr>
4040
<tr>
4141
<td><b>Total estimates</b></td>
42-
<td><b><center>20.8K</center></b></td>
42+
<td><b><center>20.9K</center></b></td>
4343
<td><b><center>17.5K</center></b></td>
4444
</tr>
4545
</table>

source/core_mqtt.c

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4918,6 +4918,7 @@ MQTTStatus_t MQTT_GetSubAckStatusCodes( const MQTTPacketInfo_t * pSubackPacket,
49184918
size_t * pPayloadSize )
49194919
{
49204920
MQTTStatus_t status = MQTTSuccess;
4921+
uint32_t propertyLength = 0;
49214922

49224923
if( pSubackPacket == NULL )
49234924
{
@@ -4948,23 +4949,39 @@ MQTTStatus_t MQTT_GetSubAckStatusCodes( const MQTTPacketInfo_t * pSubackPacket,
49484949
status = MQTTBadParameter;
49494950
}
49504951

4951-
/* A SUBACK must have a remaining length of at least 3 to accommodate the
4952-
* packet identifier and at least 1 return code. */
4953-
else if( pSubackPacket->remainingLength < 3U )
4952+
/* A SUBACK must have a remaining length of at least 4 to accommodate the
4953+
* packet identifier, atleast 1 byte for the property length and at least 1 return code. */
4954+
else if( pSubackPacket->remainingLength < 4U )
49544955
{
49554956
LogError( ( "Invalid parameter: Packet remaining length is invalid: "
4956-
"Should be greater than 2 for SUBACK packet: InputRemainingLength=%lu",
4957+
"Should be greater than or equal to 4 for SUBACK packet: InputRemainingLength=%lu",
49574958
( unsigned long ) pSubackPacket->remainingLength ) );
49584959
status = MQTTBadParameter;
49594960
}
49604961
else
49614962
{
4962-
/* According to the MQTT 3.1.1 protocol specification, the "Remaining Length" field is a
4963-
* length of the variable header (2 bytes) plus the length of the payload.
4964-
* Therefore, we add 2 positions for the starting address of the payload, and
4965-
* subtract 2 bytes from the remaining length for the length of the payload. */
4966-
*pPayloadStart = &pSubackPacket->pRemainingData[ sizeof( uint16_t ) ];
4967-
*pPayloadSize = pSubackPacket->remainingLength - sizeof( uint16_t );
4963+
/* According to the MQTT 5.0 specification, the "Remaining Length" field represents the
4964+
* combined length of the variable header and the payload. In a SUBACK packet, the variable
4965+
* header consists of the Packet Identifier (2 bytes) followed by the properties.
4966+
*
4967+
* To locate the start of the payload:
4968+
* - Skip the 2-byte Packet Identifier.
4969+
* - Then skip the properties, whose total length is decoded using the
4970+
* decodeSubackPropertyLength() function.
4971+
*
4972+
* The payload starts immediately after the properties.
4973+
* Its size is calculated by subtracting the size of the variable header
4974+
* (2 bytes for Packet ID + property length) from the remaining length.
4975+
*/
4976+
status = decodeSubackPropertyLength( &pSubackPacket->pRemainingData[ sizeof( uint16_t ) ],
4977+
pSubackPacket->remainingLength,
4978+
&propertyLength );
4979+
4980+
if( status == MQTTSuccess )
4981+
{
4982+
*pPayloadStart = &pSubackPacket->pRemainingData[ sizeof( uint16_t ) + propertyLength ];
4983+
*pPayloadSize = pSubackPacket->remainingLength - sizeof( uint16_t ) - propertyLength;
4984+
}
49684985
}
49694986

49704987
return status;

source/core_mqtt_serializer_private.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,3 +585,28 @@ uint8_t * serializeDisconnectFixed( uint8_t * pIndex,
585585
}
586586

587587
/*-----------------------------------------------------------*/
588+
589+
MQTTStatus_t decodeSubackPropertyLength( const uint8_t * pIndex,
590+
uint32_t remainingLength,
591+
uint32_t * subackPropertyLength )
592+
{
593+
MQTTStatus_t status;
594+
const uint8_t * pLocalIndex = pIndex;
595+
uint32_t propertyLength = 0U;
596+
597+
status = decodeVariableLength( pLocalIndex, remainingLength - sizeof( uint16_t ), &propertyLength );
598+
599+
if( status == MQTTSuccess )
600+
{
601+
*subackPropertyLength = ( propertyLength + variableLengthEncodedSize( propertyLength ) );
602+
603+
if( *subackPropertyLength > ( remainingLength - sizeof( uint16_t ) ) )
604+
{
605+
status = MQTTBadResponse;
606+
}
607+
}
608+
609+
return status;
610+
}
611+
612+
/*-----------------------------------------------------------*/

source/include/core_mqtt.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,17 @@ MQTTStatus_t MQTT_MatchTopic( const char * pTopicName,
13611361
* - 0x00 - Success - Maximum QoS 0
13621362
* - 0x01 - Success - Maximum QoS 1
13631363
* - 0x02 - Success - Maximum QoS 2
1364-
* - 0x80 - Failure
1364+
* These are the reason codes when the server refuses the request-
1365+
* - 0x80 - Topic Filter Refused
1366+
* - 0x83 - Implementation specific error.
1367+
* - 0x87 - Not authorized.
1368+
* - 0x8F - Invalid Topic Filter.
1369+
* - 0x91 - Packet identifier in use.
1370+
* - 0x97 - Quota exceeded.
1371+
* - 0x9E - Shared subscriptions not supported.
1372+
* - 0xA1 - Subscription identifiers not supported.
1373+
* - 0xA2 - Wildcard subscriptions not supported.
1374+
*
13651375
* Refer to #MQTTSubAckStatus_t for the status codes.
13661376
*
13671377
* @param[in] pSubackPacket The SUBACK packet whose payload is to be parsed.
@@ -1372,8 +1382,8 @@ MQTTStatus_t MQTT_MatchTopic( const char * pTopicName,
13721382
* SUBACK status is present in the packet.
13731383
*
13741384
* @return Returns one of the following:
1375-
* - #MQTTBadParameter if the input SUBACK packet is invalid.
1376-
* - #MQTTSuccess if parsing the payload was successful.
1385+
* - #MQTTBadParameter if the input SUBACK packet is invalid.<br>
1386+
* - #MQTTSuccess if parsing the payload was successful.<br>
13771387
*
13781388
* <b>Example</b>
13791389
* @code{c}
@@ -1384,10 +1394,13 @@ MQTTStatus_t MQTT_MatchTopic( const char * pTopicName,
13841394
*
13851395
* // MQTT_GetSubAckStatusCodes is intended to be used from the application
13861396
* // callback that is called by the library in MQTT_ProcessLoop or MQTT_ReceiveLoop.
1387-
* void eventCallback(
1397+
* bool eventCallback(
13881398
* MQTTContext_t * pContext,
13891399
* MQTTPacketInfo_t * pPacketInfo,
13901400
* MQTTDeserializedInfo_t * pDeserializedInfo
1401+
* MQTTSuccessFailReasonCode_t * pReasonCode,
1402+
* MQTTPropBuilder_t * pSendPropsBuffer,
1403+
* MQTTPropBuilder_t * pGetPropsBuffer
13911404
* )
13921405
* {
13931406
* MQTTStatus_t status = MQTTSuccess;

source/include/private/core_mqtt_serializer_private.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,20 @@ uint8_t * serializeAckFixed( uint8_t * pIndex,
446446
size_t remainingLength,
447447
MQTTSuccessFailReasonCode_t reasonCode );
448448

449+
/**
450+
* @brief Decodes the property length field in a SUBACK packet.
451+
*
452+
* @param[in] pIndex Pointer to the start of the properties in the SUBACK packet.
453+
* @param[in] remainingLength The remaining length of the MQTT packet being parsed, without Packet ID.
454+
* @param[out] subackPropertyLength The decoded property length including the size of its encoded representation.
455+
*
456+
* @return #MQTTSuccess if the property length is successfully decoded;
457+
* #MQTTBadResponse if the decoded property length is greater than the remaining length.
458+
*/
459+
MQTTStatus_t decodeSubackPropertyLength( const uint8_t * pIndex,
460+
uint32_t remainingLength,
461+
uint32_t * subackPropertyLength );
462+
449463
/**
450464
* @brief Serialize the fixed size part of the disconnect packet header.
451465
*

0 commit comments

Comments
 (0)