coreMQTT v2.1.1
MQTT 3.1.1 Client Library
MQTT_GetSubscribePacketSize

Get packet size and Remaining Length of an MQTT SUBSCRIBE packet.

size_t subscriptionCount,
size_t * pRemainingLength,
size_t * pPacketSize );
MQTTStatus_t MQTT_GetSubscribePacketSize(const MQTTSubscribeInfo_t *pSubscriptionList, size_t subscriptionCount, size_t *pRemainingLength, size_t *pPacketSize)
Get packet size and Remaining Length of an MQTT SUBSCRIBE packet.
Definition: core_mqtt_serializer.c:1852
MQTTStatus_t
Return codes from MQTT functions.
Definition: core_mqtt_serializer.h:99
MQTT SUBSCRIBE packet parameters.
Definition: core_mqtt_serializer.h:192

This function must be called before MQTT_SerializeSubscribe in order to get the size of the MQTT SUBSCRIBE packet that is generated from the list of MQTTSubscribeInfo_t. The size of the MQTTFixedBuffer_t supplied to MQTT_SerializeSubscribe must be at least pPacketSize. The provided pSubscriptionList is valid for serialization with MQTT_SerializeSubscribe only if this function returns MQTTSuccess. The remaining length returned in pRemainingLength and the packet size returned in pPacketSize are valid only if this function returns MQTTSuccess.

Parameters
[in]pSubscriptionListList of MQTT subscription info.
[in]subscriptionCountThe number of elements in pSubscriptionList.
[out]pRemainingLengthThe Remaining Length of the MQTT SUBSCRIBE packet.
[out]pPacketSizeThe total size of the MQTT SUBSCRIBE packet.
Returns
MQTTBadParameter if the packet would exceed the size allowed by the MQTT spec; MQTTSuccess otherwise.

Example

// Variables used in this example.
MQTTStatus_t status;
MQTTSubscribeInfo_t subscriptionList[ NUMBER_OF_SUBSCRIPTIONS ] = { 0 };
size_t remainingLength = 0, packetSize = 0;
// This is assumed to be a list of filters we want to subscribe to.
const char * filters[ NUMBER_OF_SUBSCRIPTIONS ];
// Set each subscription.
for( int i = 0; i < NUMBER_OF_SUBSCRIPTIONS; i++ )
{
subscriptionList[ i ].qos = MQTTQoS0;
// Each subscription needs a topic filter.
subscriptionList[ i ].pTopicFilter = filters[ i ];
subscriptionList[ i ].topicFilterLength = strlen( filters[ i ] );
}
// Get the size requirement for the subscribe packet.
&subscriptionList[ 0 ], NUMBER_OF_SUBSCRIPTIONS, &remainingLength, &packetSize
);
if( status == MQTTSuccess )
{
// The application should allocate or use a static #MQTTFixedBuffer_t
// of size >= packetSize to serialize the subscribe request.
}
@ MQTTSuccess
Definition: core_mqtt_serializer.h:100
@ MQTTQoS0
Definition: core_mqtt_serializer.h:122
MQTTQoS_t qos
Quality of Service for subscription.
Definition: core_mqtt_serializer.h:196
uint16_t topicFilterLength
Length of subscription topic filter.
Definition: core_mqtt_serializer.h:206
const char * pTopicFilter
Topic filter to subscribe to.
Definition: core_mqtt_serializer.h:201