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:
16:d0b3886ada32
Parent:
14:c2052aee81de
Child:
17:c5bd28cc139a
--- a/MQTTConnectServer.c	Fri Aug 01 16:27:19 2014 +0000
+++ b/MQTTConnectServer.c	Fri Aug 01 16:58:18 2014 +0000
@@ -61,6 +61,8 @@
 
 	FUNC_ENTRY;
 	header.byte = readChar(&curdata);
+	if (header.bits.type != CONNECT)
+		goto exit;
 
 	curdata += MQTTPacket_decodeBuf(curdata, &mylen); /* read remaining length */
 
@@ -111,13 +113,15 @@
   * @param buf the buffer into which the packet will be serialized
   * @param buflen the length in bytes of the supplied buffer
   * @param connack_rc the integer connack return code to be used 
+  * @param sessionPresent the MQTT 3.1.1 sessionPresent flag
   * @return serialized length, or error if 0
   */
-int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc)
+int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent)
 {
 	MQTTHeader header;
 	int rc = 0;
 	unsigned char *ptr = buf;
+	MQTTConnackFlags flags;
 
 	FUNC_ENTRY;
 	if (buflen < 2)
@@ -131,7 +135,9 @@
 
 	ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
 
-	writeChar(&ptr, 0); /* compression byte - not used */
+	flags.all = 0;
+	flags.bits.sessionpresent = sessionPresent;
+	writeChar(&ptr, flags.all); 
 	writeChar(&ptr, connack_rc);
 
 	rc = ptr - buf;