Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MQTTSNSubscribeClient.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_subscribeLength(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 /** 00042 * Serializes the supplied subscribe data into the supplied buffer, ready for sending 00043 * @param buf the buffer into which the packet will be serialized 00044 * @param buflen the length in bytes of the supplied buffer 00045 * @param dup integer - the MQTT-SN dup flag 00046 * @param qos integer - the MQTT-SN QoS value 00047 * @param packetid integer - the MQTT-SN packet identifier 00048 * @param topic MQTTSN_topicid - the MQTT-SN topic in the subscribe 00049 * @return the length of the serialized data. <= 0 indicates error 00050 */ 00051 int MQTTSNSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned short packetid, 00052 MQTTSN_topicid* topicFilter) 00053 { 00054 unsigned char *ptr = buf; 00055 MQTTSNFlags flags; 00056 int len = 0; 00057 int rc = 0; 00058 00059 FUNC_ENTRY; 00060 if ((len = MQTTSNPacket_len(MQTTSNSerialize_subscribeLength(topicFilter))) > buflen) 00061 { 00062 rc = MQTTSNPACKET_BUFFER_TOO_SHORT; 00063 goto exit; 00064 } 00065 ptr += MQTTSNPacket_encode(ptr, len); /* write length */ 00066 writeChar(&ptr, MQTTSN_SUBSCRIBE); /* write message type */ 00067 00068 flags.all = 0; 00069 flags.bits.dup = dup; 00070 flags.bits.QoS = qos; 00071 flags.bits.topicIdType = topicFilter->type; 00072 writeChar(&ptr, flags.all); 00073 00074 writeInt(&ptr, packetid); 00075 00076 /* now the topic id or name */ 00077 if (topicFilter->type == MQTTSN_TOPIC_TYPE_NORMAL) /* means long topic name */ 00078 { 00079 memcpy(ptr, topicFilter->data.long_.name, topicFilter->data.long_.len); 00080 ptr += topicFilter->data.long_.len; 00081 } 00082 else if (topicFilter->type == MQTTSN_TOPIC_TYPE_PREDEFINED) 00083 writeInt(&ptr, topicFilter->data.id); 00084 else if (topicFilter->type == MQTTSN_TOPIC_TYPE_SHORT) 00085 { 00086 writeChar(&ptr, topicFilter->data.short_name[0]); 00087 writeChar(&ptr, topicFilter->data.short_name[1]); 00088 } 00089 00090 rc = ptr - buf; 00091 exit: 00092 FUNC_EXIT_RC(rc); 00093 return rc; 00094 00095 } 00096 00097 00098 /** 00099 * Deserializes the supplied (wire) buffer into suback data 00100 * @param qos the returned qos 00101 * @param topicid returned if "accepted" the value which will be used by the gateway in subsequent PUBLISH packets 00102 * @param packetid returned - the same value as the one contained in the corresponding SUBSCRIBE 00103 * @param returncode returned - "accepted" or rejection reason 00104 * @param buf the raw buffer data, of the correct length determined by the remaining length field 00105 * @param buflen the length in bytes of the data in the supplied buffer 00106 * @return error code. 1 is success 00107 */ 00108 int MQTTSNDeserialize_suback(int* qos, unsigned short* topicid, unsigned short* packetid, 00109 unsigned char* returncode, unsigned char* buf, int buflen) 00110 { 00111 MQTTSNFlags flags; 00112 unsigned char* curdata = buf; 00113 unsigned char* enddata = NULL; 00114 int rc = 0; 00115 int mylen = 0; 00116 00117 FUNC_ENTRY; 00118 curdata += MQTTSNPacket_decode(curdata, buflen, &mylen); /* read length */ 00119 enddata = buf + mylen; 00120 if (enddata - curdata > buflen) 00121 goto exit; 00122 00123 if (readChar(&curdata) != MQTTSN_SUBACK) 00124 goto exit; 00125 00126 flags.all = readChar(&curdata); 00127 *qos = flags.bits.QoS; 00128 00129 *topicid = readInt(&curdata); 00130 *packetid = readInt(&curdata); 00131 *returncode = readChar(&curdata); 00132 00133 rc = 1; 00134 exit: 00135 FUNC_EXIT_RC(rc); 00136 return rc; 00137 } 00138 00139 00140
Generated on Thu Jul 14 2022 12:58:42 by
