Basic C library for MQTT packet serialization and deserialization

Dependents:   MQTT MQTT MQTT MQTT ... more

Fork of MQTTPacket by MQTT

This library is part of the EclipseTM Paho project; specifically the embedded client.

A basic MQTT library in C for packet serialization and deserialization

Revision:
4:c502573c6016
Parent:
0:7734401cc1b4
Child:
9:3893bc7343f4
--- a/MQTTConnectClient.c	Thu Apr 10 22:54:14 2014 +0000
+++ b/MQTTConnectClient.c	Fri Apr 11 23:44:15 2014 +0100
@@ -157,13 +157,15 @@
 }
 
 
+
 /**
-  * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket
+  * Serializes a 0-length packet into the supplied buffer, ready for writing to a socket
   * @param buf the buffer into which the packet will be serialized
   * @param buflen the length in bytes of the supplied buffer, to avoid overruns
+  * @param type the message type
   * @return serialized length, or error if 0
   */
-int MQTTSerialize_disconnect(char* buf, int buflen)
+int MQTTSerialize_zero(char* buf, int buflen, int type)
 {
 	MQTTHeader header;
 	int rc = -1;
@@ -176,7 +178,7 @@
 		goto exit;
 	}
 	header.byte = 0;
-	header.bits.type = DISCONNECT;
+	header.bits.type = type;
 	writeChar(&ptr, header.byte); /* write header */
 
 	ptr += MQTTPacket_encode(ptr, 0); /* write remaining length */
@@ -185,3 +187,27 @@
 	FUNC_EXIT_RC(rc);
 	return rc;
 }
+
+
+/**
+  * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket
+  * @param buf the buffer into which the packet will be serialized
+  * @param buflen the length in bytes of the supplied buffer, to avoid overruns
+  * @return serialized length, or error if 0
+  */
+int MQTTSerialize_disconnect(char* buf, int buflen)
+{
+	return MQTTSerialize_zero(buf, buflen, DISCONNECT);
+}
+
+
+/**
+  * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket
+  * @param buf the buffer into which the packet will be serialized
+  * @param buflen the length in bytes of the supplied buffer, to avoid overruns
+  * @return serialized length, or error if 0
+  */
+int MQTTSerialize_pingreq(char* buf, int buflen)
+{
+	return MQTTSerialize_zero(buf, buflen, PINGREQ);
+}