/* Serializes the ack packet into the supplied buffer.  */
int MQTTSerialize_ack(unsigned char *buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
/* Serializes the ack packet into the supplied buffer.  */
int MQTTSerialize_ack(unsigned char *buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
{
    MQTTHeader header = {0};
    int rc = 0;
    unsigned char* ptr = buf;
    FUNC_ENTRY;
    if (buflen < 4) {
        rc = MQTTPACKET_BUFFER_TOO_SHORT;
        goto exit;
    }
    header.bits.type = packettype;
    header.bits.dup = dup;
    header.bits.qos = (packettype == PUBREL) ? 1 : 0;
    writeChar(&ptr, header.byte);
    ptr += MQTTPacket_encode(ptr, 2);
    writeInt(&ptr, packetid);
    rc = ptr - buf;
exit:
    FUNC_EXIT_RC(rc);
    return rc;
}