/* Encodes the message length according to the MQTT algorithm  */
int mqtt_packet_encode(unsigned char *buf, int length)
/* Encodes the message length according to the MQTT algorithm  */
int mqtt_packet_encode(unsigned char *buf, int length)
{
	int rc = 0;
	FUNC_ENTRY;
	do
	{
		char d = length % 128;
		length /= 128;
		if (length > 0)
			d |= 0x80;
		buf[rc++] = d;
	} while (length > 0);
	FUNC_EXIT_RC(rc);
	return rc;
}