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.
MQTTSNDeserializePublish.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 #include <string.h> 00020 00021 #define min(a, b) ((a < b) ? 1 : 0) 00022 00023 /** 00024 * Deserializes the supplied (wire) buffer into publish data 00025 * @param dup returned integer - the MQTT dup flag 00026 * @param qos returned integer - the MQTT QoS value 00027 * @param retained returned integer - the MQTT retained flag 00028 * @param packetid returned integer - the MQTT packet identifier 00029 * @param topicName returned MQTTSNString - the MQTT topic in the publish 00030 * @param payload returned byte buffer - the MQTT publish payload 00031 * @param payloadlen returned integer - the length of the MQTT payload 00032 * @param buf the raw buffer data, of the correct length determined by the remaining length field 00033 * @param buflen the length in bytes of the data in the supplied buffer 00034 * @return error code. 1 is success 00035 */ 00036 int MQTTSNDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTSN_topicid* topic, 00037 unsigned char** payload, int* payloadlen, unsigned char* buf, int buflen) 00038 { 00039 MQTTSNFlags flags; 00040 unsigned char* curdata = buf; 00041 unsigned char* enddata = NULL; 00042 int rc = 0; 00043 int mylen = 0; 00044 00045 FUNC_ENTRY; 00046 curdata += MQTTSNPacket_decode(curdata, buflen, &mylen); /* read length */ 00047 enddata = buf + mylen; 00048 if (enddata - curdata > buflen) 00049 goto exit; 00050 00051 if (readChar(&curdata) != MQTTSN_PUBLISH) 00052 goto exit; 00053 00054 flags.all = readChar(&curdata); 00055 *dup = flags.bits.dup; 00056 *qos = flags.bits.QoS; 00057 *retained = flags.bits.retain; 00058 00059 topic->type = (MQTTSN_topicTypes)flags.bits.topicIdType; 00060 if (topic->type == MQTTSN_TOPIC_TYPE_NORMAL && *qos == 3) 00061 { 00062 /* special arrangement for long topic names in QoS -1 publishes. The length of the topic is in the topicid field */ 00063 topic->data.long_.len = readInt(&curdata); 00064 } 00065 else if (topic->type == MQTTSN_TOPIC_TYPE_NORMAL || topic->type == MQTTSN_TOPIC_TYPE_PREDEFINED) 00066 topic->data.id = readInt(&curdata); 00067 else 00068 { 00069 topic->data.short_name[0] = readChar(&curdata); 00070 topic->data.short_name[1] = readChar(&curdata); 00071 } 00072 *packetid = readInt(&curdata); 00073 00074 if (topic->type == MQTTSN_TOPIC_TYPE_NORMAL && *qos == 3) 00075 { 00076 topic->data.long_.name = (char*)curdata; 00077 curdata += topic->data.long_.len; 00078 } 00079 00080 *payloadlen = enddata - curdata; 00081 *payload = curdata; 00082 rc = 1; 00083 exit: 00084 FUNC_EXIT_RC(rc); 00085 return rc; 00086 } 00087 00088 00089 int MQTTSNDeserialize_puback(unsigned short* topicid, unsigned short* packetid, 00090 unsigned char* returncode, unsigned char* buf, int buflen) 00091 { 00092 unsigned char* curdata = buf; 00093 unsigned char* enddata = NULL; 00094 int rc = 0; 00095 int mylen = 0; 00096 00097 FUNC_ENTRY; 00098 curdata += MQTTSNPacket_decode(curdata, buflen, &mylen); /* read length */ 00099 enddata = buf + mylen; 00100 if (enddata - curdata > buflen) 00101 goto exit; 00102 00103 if (readChar(&curdata) != MQTTSN_PUBACK) 00104 goto exit; 00105 00106 *topicid = readInt(&curdata); 00107 *packetid = readInt(&curdata); 00108 *returncode = readChar(&curdata); 00109 00110 rc = 1; 00111 exit: 00112 FUNC_EXIT_RC(rc); 00113 return rc; 00114 } 00115 00116 00117 /** 00118 * Deserializes the supplied (wire) buffer into an ack 00119 * @param packettype returned integer - the MQTT packet type 00120 * @param packetid returned integer - the MQTT packet identifier 00121 * @param buf the raw buffer data, of the correct length determined by the remaining length field 00122 * @param buflen the length in bytes of the data in the supplied buffer 00123 * @return error code. 1 is success, 0 is failure 00124 */ 00125 int MQTTSNDeserialize_ack(unsigned char* type, unsigned short* packetid, unsigned char* buf, int buflen) 00126 { 00127 unsigned char* curdata = buf; 00128 unsigned char* enddata = NULL; 00129 int rc = 0; 00130 int mylen = 0; 00131 00132 FUNC_ENTRY; 00133 curdata += MQTTSNPacket_decode(curdata, buflen, &mylen); /* read length */ 00134 enddata = buf + mylen; 00135 if (enddata - curdata > buflen) 00136 goto exit; 00137 00138 *type = readChar(&curdata); 00139 if (*type != MQTTSN_PUBREL && *type != MQTTSN_PUBREC && *type != MQTTSN_PUBCOMP) 00140 goto exit; 00141 00142 *packetid = readInt(&curdata); 00143 00144 rc = 1; 00145 exit: 00146 FUNC_EXIT_RC(rc); 00147 return rc; 00148 } 00149 00150 00151 /** 00152 * Deserializes the supplied (wire) buffer into register data 00153 * @param topicid returned topic id 00154 * @param packetid returned integer - the MQTT packet identifier 00155 * @param topicName returned MQTTSNString - the MQTT topic in the register 00156 * @param buf the raw buffer data, of the correct length determined by the remaining length field 00157 * @param buflen the length in bytes of the data in the supplied buffer 00158 * @return error code. 1 is success 00159 */ 00160 int MQTTSNDeserialize_register(unsigned short* topicid, unsigned short* packetid, MQTTSNString* topicname, 00161 unsigned char* buf, int buflen) 00162 { 00163 unsigned char* curdata = buf; 00164 unsigned char* enddata = NULL; 00165 int rc = 0; 00166 int mylen = 0; 00167 00168 FUNC_ENTRY; 00169 curdata += MQTTSNPacket_decode(curdata, buflen, &mylen); /* read length */ 00170 enddata = buf + mylen; 00171 if (enddata - curdata > buflen) 00172 goto exit; 00173 00174 if (readChar(&curdata) != MQTTSN_REGISTER) 00175 goto exit; 00176 00177 *topicid = readInt(&curdata); 00178 *packetid = readInt(&curdata); 00179 00180 topicname->lenstring.data = (char*)curdata; 00181 topicname->lenstring.len = enddata - curdata; 00182 topicname->cstring = NULL; 00183 00184 rc = 1; 00185 exit: 00186 FUNC_EXIT_RC(rc); 00187 return rc; 00188 } 00189 00190 00191 /** 00192 * Deserializes the supplied (wire) buffer into register data 00193 * @param topicid returned topic id 00194 * @param packetid returned integer - the MQTT packet identifier 00195 * @param return_code returned integer return code 00196 * @param buf the raw buffer data, of the correct length determined by the remaining length field 00197 * @param buflen the length in bytes of the data in the supplied buffer 00198 * @return error code. 1 is success 00199 */ 00200 int MQTTSNDeserialize_regack(unsigned short* topicid, unsigned short* packetid, unsigned char* return_code, 00201 unsigned char* buf, int buflen) 00202 { 00203 unsigned char* curdata = buf; 00204 unsigned char* enddata = NULL; 00205 int rc = 0; 00206 int mylen = 0; 00207 00208 FUNC_ENTRY; 00209 curdata += MQTTSNPacket_decode(curdata, buflen, &mylen); /* read length */ 00210 enddata = buf + mylen; 00211 if (enddata - curdata > buflen) 00212 goto exit; 00213 00214 if (readChar(&curdata) != MQTTSN_REGACK) 00215 goto exit; 00216 00217 *topicid = readInt(&curdata); 00218 *packetid = readInt(&curdata); 00219 *return_code = readChar(&curdata); 00220 00221 rc = 1; 00222 exit: 00223 FUNC_EXIT_RC(rc); 00224 return rc; 00225 }
Generated on Thu Jul 14 2022 12:58:42 by
