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

« Back to documentation index

Show/hide line numbers MQTTSNUnsubscribeServer.c Source File

MQTTSNUnsubscribeServer.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 "StackTrace.h"
00018 #include "MQTTSNPacket.h"
00019 
00020 int MQTTSNDeserialize_unsubscribe(unsigned short* packetid, MQTTSN_topicid* topicFilter,
00021         unsigned char* buf, int buflen)
00022 {
00023     MQTTSNFlags flags;
00024     unsigned char* curdata = buf;
00025     unsigned char* enddata = NULL;
00026     int rc = 0;
00027     int mylen = 0;
00028 
00029     FUNC_ENTRY;
00030     curdata += MQTTSNPacket_decode(curdata, buflen, &mylen); /* read length */
00031     enddata = buf + mylen;
00032     if (enddata - curdata > buflen)
00033         goto exit;
00034 
00035     if (readChar(&curdata) != MQTTSN_UNSUBSCRIBE)
00036         goto exit;
00037 
00038     flags.all = readChar(&curdata);
00039     *packetid = readInt(&curdata);
00040 
00041     topicFilter->type = (MQTTSN_topicTypes)flags.bits.topicIdType;
00042     if (topicFilter->type == MQTTSN_TOPIC_TYPE_NORMAL)
00043     {
00044         topicFilter->data.long_.len = enddata - curdata;
00045         topicFilter->data.long_.name = (char*)curdata;
00046     }
00047     else if (topicFilter->type == MQTTSN_TOPIC_TYPE_PREDEFINED)
00048         topicFilter->data.id = readInt(&curdata);
00049     else if (topicFilter->type == MQTTSN_TOPIC_TYPE_SHORT)
00050     {
00051         topicFilter->data.short_name[0] = readChar(&curdata);
00052         topicFilter->data.short_name[1] = readChar(&curdata);
00053     }
00054 
00055     rc = 1;
00056 exit:
00057     FUNC_EXIT_RC(rc);
00058     return rc;
00059 }
00060 
00061 
00062 int MQTTSNSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid)
00063 {
00064     unsigned char *ptr = buf;
00065     int len = 0;
00066     int rc = 0;
00067 
00068     FUNC_ENTRY;
00069     if ((len = MQTTSNPacket_len(3)) > buflen)
00070     {
00071         rc = MQTTSNPACKET_BUFFER_TOO_SHORT;
00072         goto exit;
00073     }
00074     ptr += MQTTSNPacket_encode(ptr, len); /* write length */
00075     writeChar(&ptr, MQTTSN_UNSUBACK);      /* write message type */
00076 
00077     writeInt(&ptr, packetid);
00078 
00079     rc = ptr - buf;
00080 exit:
00081     FUNC_EXIT_RC(rc);
00082     return rc;
00083 }
00084 
00085