/* Helper function to read packet data from some source into a buffer  */
int MQTTPacket_read(unsigned char *buf, int buflen, int(*getfn)(unsigned char *, int))
/* Helper function to read packet data from some source into a buffer  */
int MQTTPacket_read(unsigned char *buf, int buflen, int(*getfn)(unsigned char *, int))
{
	int rc = -1;
	MQTTHeader header = {0};
	int len = 0;
	int rem_len = 0;
	if ((*getfn)(buf, 1) != 1)
		goto exit;
	len = 1;
	MQTTPacket_decode(getfn, &rem_len);
	len += MQTTPacket_encode(buf + 1, rem_len);
	if((rem_len + len) > buflen)
		goto exit;
	if ((*getfn)(buf + len, rem_len) != rem_len)
		goto exit;
	header.byte = buf[0];
	rc = header.bits.type;
exit:
	return rc;
}