The Cayenne MQTT mbed Library provides functions to easily connect to the Cayenne IoT project builder.

Dependents:   Cayenne-ESP8266Interface Cayenne-WIZnet_Library Cayenne-WIZnetInterface Cayenne-X-NUCLEO-IDW01M1 ... more

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 
00019 #include <string.h>
00020 
00021 /**
00022   * Determines the length of the MQTT unsubscribe packet that would be produced using the supplied parameters
00023   * @param count the number of topic filter strings in topicFilters
00024   * @param topicFilters the array of topic filter strings to be used in the publish
00025   * @return the length of buffer needed to contain the serialized version of the packet
00026   */
00027 size_t MQTTSerialize_unsubscribeLength(int count, MQTTString topicFilters[])
00028 {
00029     int i;
00030     size_t len = 2; /* packetid */
00031 
00032     for (i = 0; i < count; ++i)
00033         len += 2 + MQTTstrlen(topicFilters[i]); /* length + topic*/
00034     return len;
00035 }
00036 
00037 
00038 /**
00039   * Serializes the supplied unsubscribe data into the supplied buffer, ready for sending
00040   * @param buf the raw buffer data, of the correct length determined by the remaining length field
00041   * @param buflen the length in bytes of the data in the supplied buffer
00042   * @param dup integer - the MQTT dup flag
00043   * @param packetid integer - the MQTT packet identifier
00044   * @param count - number of members in the topicFilters array
00045   * @param topicFilters - array of topic filter names
00046   * @return the length of the serialized data.  <= 0 indicates error
00047   */
00048 int MQTTSerialize_unsubscribe(unsigned char* buf, size_t buflen, unsigned char dup, unsigned short packetid,
00049         int count, MQTTString topicFilters[])
00050 {
00051     unsigned char *ptr = buf;
00052     MQTTHeader header = {0};
00053     size_t rem_len = 0;
00054     int rc = -1;
00055     int i = 0;
00056 
00057     if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters)) > buflen)
00058     {
00059         rc = MQTTPACKET_BUFFER_TOO_SHORT;
00060         goto exit;
00061     }
00062 
00063     header.byte = 0;
00064     header.bits.type = UNSUBSCRIBE_MSG;
00065     header.bits.dup = dup;
00066     header.bits.qos = 1;
00067     writeChar(&ptr, header.byte); /* write header */
00068 
00069     ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
00070 
00071     writeInt(&ptr, packetid);
00072 
00073     for (i = 0; i < count; ++i)
00074         writeMQTTString(&ptr, topicFilters[i]);
00075 
00076     rc = (int)(ptr - buf);
00077 exit:
00078     return rc;
00079 }
00080 
00081 
00082 /**
00083   * Deserializes the supplied (wire) buffer into unsuback data
00084   * @param packetid returned integer - the MQTT packet identifier
00085   * @param buf the raw buffer data, of the correct length determined by the remaining length field
00086   * @param buflen the length in bytes of the data in the supplied buffer
00087   * @return error code.  1 is success, 0 is failure
00088   */
00089 int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, size_t buflen)
00090 {
00091     unsigned char type = 0;
00092     unsigned char dup = 0;
00093     int rc = 0;
00094 
00095     rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen);
00096     if (type == UNSUBACK_MSG)
00097         rc = 1;
00098     return rc;
00099 }
00100 
00101