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

Files at this revision

API Documentation at this revision

Comitter:
Ian Craggs
Date:
Fri Apr 11 23:44:15 2014 +0100
Parent:
3:4a4f8699f935
Child:
5:eea71419676a
Commit message:
Pings and other fixes

Changed in this revision

MQTTConnect.h Show annotated file Show diff for this revision Revisions of this file
MQTTConnectClient.c Show annotated file Show diff for this revision Revisions of this file
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/MQTTConnect.h	Thu Apr 10 22:54:14 2014 +0000
+++ b/MQTTConnect.h	Fri Apr 11 23:44:15 2014 +0100
@@ -82,7 +82,8 @@
 	char struct_id[4];
 	/** The version number of this structure.  Must be 0 */
 	int struct_version;
-	/** Version of MQTT to be used.  3 = 3.1 4 = 3.1.1 */
+	/** Version of MQTT to be used.  3 = 3.1 4 = 3.1.1
+	  */
 	int MQTTVersion;
 	MQTTString clientID;
 	int keepAliveInterval;
@@ -103,5 +104,6 @@
 int MQTTDeserialize_connack(int* connack_rc, char* buf, int buflen);
 
 int MQTTSerialize_disconnect(char* buf, int buflen);
+int MQTTSerialize_pingreq(char* buf, int buflen);
 
 #endif /* MQTTCONNECT_H_ */
--- 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);
+}
--- a/MQTTPacket.c	Thu Apr 10 22:54:14 2014 +0000
+++ b/MQTTPacket.c	Fri Apr 11 23:44:15 2014 +0100
@@ -174,7 +174,7 @@
  * @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
  * @param string the C string to write
  */
-void writeCString(char** pptr, char* string)
+void writeCString(char** pptr, const char* string)
 {
 	int len = strlen(string);
 	writeInt(pptr, len);
--- a/MQTTPacket.h	Thu Apr 10 22:54:14 2014 +0000
+++ b/MQTTPacket.h	Fri Apr 11 23:44:15 2014 +0100
@@ -81,6 +81,7 @@
 #include "MQTTSubscribe.h"
 #include "MQTTUnsubscribe.h"
 
+int MQTTSerialize_ack(char* buf, int buflen, int type, int dup, int packetid);
 int MQTTDeserialize_ack(int* type, int* dup, int* packetid, char* buf, int buflen);
 
 int MQTTPacket_len(int rem_len);
@@ -94,11 +95,9 @@
 void writeChar(char** pptr, char c);
 void writeInt(char** pptr, int anInt);
 int readMQTTLenString(MQTTString* mqttstring, char** pptr, char* enddata);
-void writeCString(char** pptr, char* string);
+void writeCString(char** pptr, const char* string);
 void writeMQTTString(char** pptr, MQTTString mqttstring);
 
-int MQTTPacket_read(char* buf, int buflen, int (*getfn)(char*, int));
-
 #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
 }
 #endif