Fork of my original MQTTGateway

Dependencies:   mbed-http

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTUnsubscribeServer.c Source File

MQTTUnsubscribeServer.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 /**
00024   * Deserializes the supplied (wire) buffer into unsubscribe data
00025   * @param dup integer returned - the MQTT dup flag
00026   * @param packetid integer returned - the MQTT packet identifier
00027   * @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays
00028   * @param count - number of members in the topicFilters and requestedQoSs arrays
00029   * @param topicFilters - array of topic filter names
00030   * @param buf the raw buffer data, of the correct length determined by the remaining length field
00031   * @param buflen the length in bytes of the data in the supplied buffer
00032   * @return the length of the serialized data.  <= 0 indicates error
00033   */
00034 int MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[],
00035         unsigned char* buf, int len)
00036 {
00037     MQTTHeader header = {0};
00038     unsigned char* curdata = buf;
00039     unsigned char* enddata = NULL;
00040     int rc = 0;
00041     int mylen = 0;
00042 
00043     FUNC_ENTRY;
00044     header.byte = readChar(&curdata);
00045     if (header.bits.type != UNSUBSCRIBE)
00046         goto exit;
00047     *dup = header.bits.dup;
00048 
00049     curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
00050     enddata = curdata + mylen;
00051 
00052     *packetid = readInt(&curdata);
00053 
00054     *count = 0;
00055     while (curdata < enddata)
00056     {
00057         if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata))
00058             goto exit;
00059         (*count)++;
00060     }
00061 
00062     rc = 1;
00063 exit:
00064     FUNC_EXIT_RC(rc);
00065     return rc;
00066 }
00067 
00068 
00069 /**
00070   * Serializes the supplied unsuback data into the supplied buffer, ready for sending
00071   * @param buf the buffer into which the packet will be serialized
00072   * @param buflen the length in bytes of the supplied buffer
00073   * @param packetid integer - the MQTT packet identifier
00074   * @return the length of the serialized data.  <= 0 indicates error
00075   */
00076 int MQTTSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid)
00077 {
00078     MQTTHeader header = {0};
00079     int rc = 0;
00080     unsigned char *ptr = buf;
00081 
00082     FUNC_ENTRY;
00083     if (buflen < 2)
00084     {
00085         rc = MQTTPACKET_BUFFER_TOO_SHORT;
00086         goto exit;
00087     }
00088     header.byte = 0;
00089     header.bits.type = UNSUBACK;
00090     writeChar(&ptr, header.byte); /* write header */
00091 
00092     ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
00093 
00094     writeInt(&ptr, packetid);
00095 
00096     rc = ptr - buf;
00097 exit:
00098     FUNC_EXIT_RC(rc);
00099     return rc;
00100 }
00101 
00102