/* Serializes the ack packet into the supplied buffer.  */
int mqtt_serialize_ack(unsigned char *buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
/* Serializes the ack packet into the supplied buffer.  */
int mqtt_serialize_ack(unsigned char *buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
{
	mqtt_header_t 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 == MQTTPACKET_PUBREL) ? 1 : 0;
	mqtt_write_char(&ptr, header.byte);
	ptr += mqtt_packet_encode(ptr, 2);
	mqtt_write_int(&ptr, packetid);
	rc = ptr - buf;
exit:
	FUNC_EXIT_RC(rc);
	return rc;
}