/* Serializes the supplied suback data into the supplied buffer, ready for sending  */
int MQTTSerialize_suback(unsigned char *buf, int buflen, unsigned short packetid, int count, int *grantedQoSs)
/* Serializes the supplied suback data into the supplied buffer, ready for sending  */
int MQTTSerialize_suback(unsigned char *buf, int buflen, unsigned short packetid, int count, int *grantedQoSs)
{
    MQTTHeader header = {0};
    int rc = -1;
    unsigned char* ptr = buf;
    int i;
    FUNC_ENTRY;
    if (buflen < 2 + count) {
        rc = MQTTPACKET_BUFFER_TOO_SHORT;
        goto exit;
    }
    header.byte = 0;
    header.bits.type = SUBACK;
    writeChar(&ptr, header.byte);
    ptr += MQTTPacket_encode(ptr, 2 + count);
    writeInt(&ptr, packetid);
    for (i = 0; i < count; ++i)
        writeChar(&ptr, grantedQoSs[i]);
    rc = ptr - buf;
exit:
    FUNC_EXIT_RC(rc);
    return rc;
}