MQTT API Reference
MQTT 3.1.1 client library
IotMqtt_SerializeSubscribe

Generate a SUBSCRIBE packet from the given parameters.

size_t subscriptionCount,
size_t remainingLength,
uint16_t * pPacketIdentifier,
uint8_t * pBuffer,
size_t bufferSize );
Parameters
[in]pSubscriptionListUser-provided array of subscriptions.
[in]subscriptionCountSize of pSubscriptionList.
[in]remainingLengthremaining length of the packet to be serialized.
[out]pPacketIdentifierThe packet identifier generated for this SUBSCRIBE.
[in,out]pBufferUser provide buffer where the SUBSCRIBE packet is written.
[in]bufferSizeSize of the buffer pointed to by pBuffer.
Returns
IOT_MQTT_SUCCESS or IOT_MQTT_NO_MEMORY.
Note
pBuffer must be allocated by caller.
This call is part of serializer API used for implementing light-weight MQTT client. Example
// Example code below shows how IotMqtt_SerializeSubscribe() should be used to serialize
// MQTT Subscribe packet and send it to MQTT broker.
// Example uses static memory, but dynamically allocated memory can be used as well.
// Get size requirement for the MQTT subscribe packet.
#define mqttexampleSHARED_BUFFER_SIZE 100
static ucSharedBuffer[mqttexampleSHARED_BUFFER_SIZE];
void sendSubscribePacket( int xMQTTSocket )
{
IotMqttSubscription_t xMQTTSubscription[ 1 ];
size_t xRemainingLength = 0;
size_t xPacketSize = 0;
IotMqttError_t xResult;
size_t xSentBytes = 0;
// Initialize Subscribe parameters. Details are out of scope for this example.
// It will involve setting QOS, topic filter and topic filter length.
_initializeSubscribe( xMQTTSubscription );
// Get size requirement for MQTT Subscribe packet.
xMQTTSubscription,
sizeof( xMQTTSubscription ) / sizeof( IotMqttSubscription_t ),
&xRemainingLength, &xPacketSize );
// Make sure the packet size is less than static buffer size.
IotMqtt_Assert( xPacketSize < mqttexampleSHARED_BUFFER_SIZE );
// Serialize subscribe into statically allocated ucSharedBuffer.
xResult = IotMqtt_SerializeSubscribe( xMQTTSubscription,
sizeof( xMQTTSubscription ) / sizeof( IotMqttSubscription_t ),
xRemainingLength,
&usPacketIdentifier,
ucSharedBuffer,
xPacketSize );
// xMQTTSocket here is posix socket created and connected to MQTT broker outside of this function.
xSentBytes = send( xMQTTSocket, ( void * ) ucSharedBuffer, xPacketSize, 0 );
IotMqtt_Assert( xSentBytes == xPacketSize );
}
IOT_MQTT_SUBSCRIBE
Definition: iot_mqtt_types.h:282
IotMqttError_t
IotMqttError_t
Return codes of MQTT functions.
Definition: iot_mqtt_types.h:103
IOT_MQTT_SUCCESS
MQTT operation completed successfully.
Definition: iot_mqtt_types.h:119
IotMqtt_GetSubscriptionPacketSize
IotMqttError_t IotMqtt_GetSubscriptionPacketSize(IotMqttOperationType_t type, const IotMqttSubscription_t *pSubscriptionList, size_t subscriptionCount, size_t *pRemainingLength, size_t *pPacketSize)
Calculate the size and "Remaining length" of a SUBSCRIBE or UNSUBSCRIBE packet generated from the giv...
Definition: iot_mqtt_serialize.c:2217
IotMqttSubscription_t
MQTT subscription.
Definition: iot_mqtt_types.h:541
IotMqtt_SerializeSubscribe
IotMqttError_t IotMqtt_SerializeSubscribe(const IotMqttSubscription_t *pSubscriptionList, size_t subscriptionCount, size_t remainingLength, uint16_t *pPacketIdentifier, uint8_t *pBuffer, size_t bufferSize)
Generate a SUBSCRIBE packet from the given parameters.
Definition: iot_mqtt_serialize.c:2273
IotMqtt_Assert
#define IotMqtt_Assert(expression)
Assertion macro for the MQTT library.
Definition: iot_mqtt_internal.h:62