Basic C library for MQTT packet serialization and deserialization

Dependents:   MQTT MQTT MQTT MQTT ... more

Fork of MQTTPacket by MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTUnsubscribeClient.c Source File

MQTTUnsubscribeClient.c

00001 /*******************************************************************************
00002  * Copyright (c) 2014 IBM Corp.
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Ian Craggs - initial API and implementation and/or initial documentation
00015  *******************************************************************************/
00016 
00017 #include "MQTTPacket.h"
00018 #include "StackTrace.h"
00019 
00020 #include <string.h>
00021 
00022 /**
00023   * Determines the length of the MQTT unsubscribe packet that would be produced using the supplied parameters
00024   * @param count the number of topic filter strings in topicFilters
00025   * @param topicFilters the array of topic filter strings to be used in the publish
00026   * @return the length of buffer needed to contain the serialized version of the packet
00027   */
00028 int MQTTSerialize_unsubscribeLength(int count, MQTTString topicFilters[])
00029 {
00030     int i;
00031     int len = 2; /* packetid */
00032 
00033     for (i = 0; i < count; ++i)
00034         len += 2 + MQTTstrlen(topicFilters[i]); /* length + topic*/
00035     return len;
00036 }
00037 
00038 
00039 /**
00040   * Serializes the supplied unsubscribe data into the supplied buffer, ready for sending
00041   * @param buf the raw buffer data, of the correct length determined by the remaining length field
00042   * @param buflen the length in bytes of the data in the supplied buffer
00043   * @param dup integer - the MQTT dup flag
00044   * @param packetid integer - the MQTT packet identifier
00045   * @param count - number of members in the topicFilters array
00046   * @param topicFilters - array of topic filter names
00047   * @return the length of the serialized data.  <= 0 indicates error
00048   */
00049 int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid,
00050         int count, MQTTString topicFilters[])
00051 {
00052     unsigned char *ptr = buf;
00053     MQTTHeader header = {0};
00054     int rem_len = 0;
00055     int rc = -1;
00056     int i = 0;
00057 
00058     FUNC_ENTRY;
00059     if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters)) > buflen)
00060     {
00061         rc = MQTTPACKET_BUFFER_TOO_SHORT;
00062         goto exit;
00063     }
00064 
00065     header.byte = 0;
00066     header.bits.type = UNSUBSCRIBE;
00067     header.bits.dup = dup;
00068     header.bits.qos = 1;
00069     writeChar(&ptr, header.byte); /* write header */
00070 
00071     ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
00072 
00073     writeInt(&ptr, packetid);
00074 
00075     for (i = 0; i < count; ++i)
00076         writeMQTTString(&ptr, topicFilters[i]);
00077 
00078     rc = ptr - buf;
00079 exit:
00080     FUNC_EXIT_RC(rc);
00081     return rc;
00082 }
00083 
00084 
00085 /**
00086   * Deserializes the supplied (wire) buffer into unsuback data
00087   * @param packetid returned integer - the MQTT packet identifier
00088   * @param buf the raw buffer data, of the correct length determined by the remaining length field
00089   * @param buflen the length in bytes of the data in the supplied buffer
00090   * @return error code.  1 is success, 0 is failure
00091   */
00092 int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen)
00093 {
00094     unsigned char type = 0;
00095     unsigned char dup = 0;
00096     int rc = 0;
00097 
00098     FUNC_ENTRY;
00099     rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen);
00100     if (type == UNSUBACK)
00101         rc = 1;
00102     FUNC_EXIT_RC(rc);
00103     return rc;
00104 }
00105 
00106