MQTTPacket fixes

Fork of MQTTPacket by MQTT

Files at this revision

API Documentation at this revision

Comitter:
icraggs
Date:
Mon Sep 29 11:31:13 2014 +0000
Parent:
17:c5bd28cc139a
Child:
19:99773f597e90
Child:
20:63c71eeb4183
Commit message:
Add packet formatting

Changed in this revision

MQTTPacket.c Show annotated file Show diff for this revision Revisions of this file
MQTTPacket.h Show annotated file Show diff for this revision Revisions of this file
--- a/MQTTPacket.c	Fri Aug 01 17:25:52 2014 +0000
+++ b/MQTTPacket.c	Mon Sep 29 11:31:13 2014 +0000
@@ -311,3 +311,143 @@
 	return rc;
 }
 
+
+const char* MQTTPacket_names[] =
+{
+	"RESERVED", "CONNECT", "CONNACK", "PUBLISH", "PUBACK", "PUBREC", "PUBREL",
+	"PUBCOMP", "SUBSCRIBE", "SUBACK", "UNSUBSCRIBE", "UNSUBACK",
+	"PINGREQ", "PINGRESP", "DISCONNECT"
+};
+
+
+char* MQTTPacket_toString(char* strbuf, int strbuflen, unsigned char* buf, int buflen)
+{
+	int index = 0;
+	int rem_length = 0;
+	MQTTHeader header = {0};
+	int strindex = 0;
+
+	header.byte = buf[index++];
+	index += MQTTPacket_decodeBuf(&buf[index], &rem_length);
+
+	switch (header.bits.type)
+	{
+	case CONNECT:
+	{
+		MQTTPacket_connectData data;
+		if (MQTTDeserialize_connect(&data, buf, buflen) == 1)
+		{
+			strindex = snprintf(strbuf, strbuflen,
+				"CONNECT MQTT version %d, client id %.*s, clean session %d, keep alive %hd",
+				(int)data.MQTTVersion, data.clientID.lenstring.len, data.clientID.lenstring.data,
+				(int)data.cleansession, data.keepAliveInterval);
+			if (data.willFlag)
+				strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
+				", will QoS %d, will retain %d, will topic %.*s, will message %.*s",
+				data.will.qos, data.will.retained,
+				data.will.topicName.lenstring.len, data.will.topicName.lenstring.data,
+				data.will.message.lenstring.len, data.will.message.lenstring.data);
+			if (data.username.lenstring.data && data.username.lenstring.len > 0)
+			{
+				printf("user name\n");
+				strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
+				", user name %.*s", data.username.lenstring.len, data.username.lenstring.data);
+			}
+			if (data.password.lenstring.data && data.password.lenstring.len > 0)
+				strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
+				", password %.*s", data.password.lenstring.len, data.password.lenstring.data);
+		}
+	}
+	break;
+	case CONNACK:
+	{
+		unsigned char sessionPresent, connack_rc;
+		if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) == 1)
+			strindex = snprintf(strbuf, strbuflen,
+			"CONNACK session present %d, rc %d", sessionPresent, connack_rc);
+	}
+	break;
+	case PUBLISH:
+	{
+		unsigned char dup, retained, *payload;
+		unsigned short packetid;
+		int qos, payloadlen;
+		MQTTString topicName = MQTTString_initializer;
+		if (MQTTDeserialize_publish(&dup, &qos, &retained, &packetid, &topicName,
+				&payload, &payloadlen, buf, buflen) == 1)
+			strindex = snprintf(strbuf, strbuflen,
+				"PUBLISH dup %d, QoS %d, retained %d, packet id %d, topic %.*s, payload length %d, payload %.*s",
+				dup, qos, retained, packetid,
+				(topicName.lenstring.len < 20) ? topicName.lenstring.len : 20, topicName.lenstring.data,
+				payloadlen, (payloadlen < 20) ? payloadlen : 20, payload);
+	}
+	break;
+	case PUBACK:
+	case PUBREC:
+	case PUBREL:
+	case PUBCOMP:
+	{
+		unsigned char packettype, dup;
+		unsigned short packetid;
+		if (MQTTDeserialize_ack(&packettype, &dup, &packetid, buf, buflen) == 1)
+			strindex = snprintf(strbuf, strbuflen,
+				"%s dup %d, packet id %d",
+				MQTTPacket_names[packettype], dup, packetid);
+	}
+	break;
+	case SUBSCRIBE:
+	{
+		unsigned char dup;
+		unsigned short packetid;
+		int maxcount = 1, count = 0;
+		MQTTString topicFilters[1];
+		int requestedQoSs[1];
+		if (MQTTDeserialize_subscribe(&dup, &packetid, maxcount, &count,
+				topicFilters, requestedQoSs, buf, buflen) == 1)
+			strindex = snprintf(strbuf, strbuflen,
+				"SUBSCRIBE dup %d, packet id %d count %d topic %.*s qos %d",
+				dup, packetid, count,
+				topicFilters[0].lenstring.len, topicFilters[0].lenstring.data,
+				requestedQoSs[0]);
+	}
+	break;
+	case SUBACK:
+	{
+		unsigned short packetid;
+		int maxcount = 1, count = 0;
+		int grantedQoSs[1];
+		if (MQTTDeserialize_suback(&packetid, maxcount, &count, grantedQoSs, buf, buflen) == 1)
+			strindex = snprintf(strbuf, strbuflen,
+				"SUBACK packet id %d count %d topic %.*s granted qos %d",
+				packetid, count, grantedQoSs[0]);
+	}
+	break;
+	case UNSUBSCRIBE:
+	{
+		unsigned char dup;
+		unsigned short packetid;
+		int maxcount = 1, count = 0;
+		MQTTString topicFilters[1];
+		if (MQTTDeserialize_unsubscribe(&dup, &packetid, maxcount, &count, topicFilters, buf, buflen) == 1)
+			strindex = snprintf(strbuf, strbuflen,
+				"UNSUBSCRIBE dup %d, packet id %d count %d topic %.*s",
+				dup, packetid, count,
+				topicFilters[0].lenstring.len, topicFilters[0].lenstring.data);
+	}
+	break;
+	case UNSUBACK:
+	{
+		unsigned short packetid;
+		if (MQTTDeserialize_unsuback(&packetid, buf, buflen) == 1)
+			strindex = snprintf(strbuf, strbuflen,
+				"UNSUBACK packet id %d", packetid);
+	}
+	break;
+	case PINGREQ:
+	case PINGRESP:
+	case DISCONNECT:
+		strindex = snprintf(strbuf, strbuflen, "%s", MQTTPacket_names[header.bits.type]);
+		break;
+	}
+	return strbuf;
+}
--- a/MQTTPacket.h	Fri Aug 01 17:25:52 2014 +0000
+++ b/MQTTPacket.h	Mon Sep 29 11:31:13 2014 +0000
@@ -101,6 +101,8 @@
 
 int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int));
 
+char* MQTTPacket_toString(char* strbuf, int strbuflen, unsigned char* buf, int buflen);
+
 #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
 }
 #endif