/* Serializes the supplied unsubscribe data into the supplied buffer, ready for sending  */
int MQTTSerialize_unsubscribe(unsigned char *buf, int buflen, unsigned char dup, unsigned short packetid, int count, MQTTString topicFilters[])
/* Serializes the supplied unsubscribe data into the supplied buffer, ready for sending  */
int MQTTSerialize_unsubscribe(unsigned char *buf, int buflen, unsigned char dup, unsigned short packetid, int count, MQTTString topicFilters[])
{
    unsigned char* ptr = buf;
    MQTTHeader header = {0};
    int rem_len = 0;
    int rc = -1;
    int i = 0;
    FUNC_ENTRY;
    if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(
                           count, topicFilters)) > buflen) {
        rc = MQTTPACKET_BUFFER_TOO_SHORT;
        goto exit;
    }
    header.byte = 0;
    header.bits.type = UNSUBSCRIBE;
    header.bits.dup = dup;
    header.bits.qos = 1;
    writeChar(&ptr, header.byte);
    ptr += MQTTPacket_encode(ptr, rem_len);
    ;
    writeInt(&ptr, packetid);
    for (i = 0; i < count; ++i)
        writeMQTTString(&ptr, topicFilters[i]);
    rc = ptr - buf;
exit:
    FUNC_EXIT_RC(rc);
    return rc;
}