Andrew Reed / Mbed OS CITY1082-i2c_master_wifi_mqtt
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTSNUnsubscribeClient.c Source File

MQTTSNUnsubscribeClient.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 "MQTTSNPacket.h"
00018 #include "StackTrace.h"
00019 
00020 #include <string.h>
00021 
00022 /**
00023   * Determines the length of the MQTTSN subscribe packet that would be produced using the supplied parameters, 
00024   * excluding length
00025   * @param topicName the topic name to be used in the publish  
00026   * @return the length of buffer needed to contain the serialized version of the packet
00027   */
00028 int MQTTSNSerialize_unsubscribeLength(MQTTSN_topicid* topicFilter)
00029 {
00030     int len = 4;
00031 
00032     if (topicFilter->type == MQTTSN_TOPIC_TYPE_NORMAL)
00033         len += topicFilter->data.long_.len;
00034     else if (topicFilter->type == MQTTSN_TOPIC_TYPE_SHORT || topicFilter->type == MQTTSN_TOPIC_TYPE_PREDEFINED)
00035         len += 2;
00036 
00037     return len;
00038 }
00039 
00040 
00041 int MQTTSNSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned short packetid, MQTTSN_topicid* topicFilter)
00042 {
00043     unsigned char *ptr = buf;
00044     MQTTSNFlags flags;
00045     int len = 0;
00046     int rc = 0;
00047 
00048     FUNC_ENTRY;
00049     if ((len = MQTTSNPacket_len(MQTTSNSerialize_unsubscribeLength(topicFilter))) > buflen)
00050     {
00051         rc = MQTTSNPACKET_BUFFER_TOO_SHORT;
00052         goto exit;
00053     }
00054     ptr += MQTTSNPacket_encode(ptr, len);   /* write length */
00055     writeChar(&ptr, MQTTSN_UNSUBSCRIBE);      /* write message type */
00056 
00057     flags.all = 0;
00058     flags.bits.topicIdType = topicFilter->type;
00059     writeChar(&ptr, flags.all);
00060 
00061     writeInt(&ptr, packetid);
00062 
00063     /* now the topic id or name */
00064     if (topicFilter->type == MQTTSN_TOPIC_TYPE_NORMAL) /* means long topic name */
00065     {
00066         memcpy(ptr, topicFilter->data.long_.name, topicFilter->data.long_.len);
00067         ptr += topicFilter->data.long_.len;
00068     }
00069     else if (topicFilter->type == MQTTSN_TOPIC_TYPE_PREDEFINED)
00070         writeInt(&ptr, topicFilter->data.id);
00071     else if (topicFilter->type == MQTTSN_TOPIC_TYPE_SHORT)
00072     {
00073         writeChar(&ptr, topicFilter->data.short_name[0]);
00074         writeChar(&ptr, topicFilter->data.short_name[1]);
00075     }
00076 
00077     rc = ptr - buf;
00078 exit:
00079     FUNC_EXIT_RC(rc);
00080     return rc;
00081 
00082 }
00083 
00084 
00085 /**
00086   * Deserializes the supplied (wire) buffer into unsuback data
00087   * @param packetid returned - the same value as the one contained in the corresponding SUBSCRIBE
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
00091   */
00092 int MQTTSNDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen)
00093 {
00094     unsigned char* curdata = buf;
00095     unsigned char* enddata = NULL;
00096     int rc = 0;
00097     int mylen = 0;
00098 
00099     FUNC_ENTRY;
00100     curdata += MQTTSNPacket_decode(curdata, buflen, &mylen); /* read length */
00101     enddata = buf + mylen;
00102     if (enddata - curdata > buflen)
00103         goto exit;
00104 
00105     if (readChar(&curdata) != MQTTSN_UNSUBACK)
00106         goto exit;
00107 
00108     *packetid = readInt(&curdata);
00109 
00110     rc = 1;
00111 exit:
00112     FUNC_EXIT_RC(rc);
00113     return rc;
00114 }
00115 
00116 
00117