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.
MQTTDeserializePublish.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 "MQTTPacket.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 MQTTString - 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 MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName, 00037 unsigned char** payload, int* payloadlen, unsigned char* buf, int buflen) 00038 { 00039 MQTTHeader header = {0}; 00040 unsigned char* curdata = buf; 00041 unsigned char* enddata = NULL; 00042 int rc = 0; 00043 int mylen = 0; 00044 00045 FUNC_ENTRY; 00046 header.byte = readChar(&curdata); 00047 if (header.bits.type != PUBLISH) 00048 goto exit; 00049 *dup = header.bits.dup; 00050 *qos = header.bits.qos; 00051 *retained = header.bits.retain; 00052 00053 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 00054 enddata = curdata + mylen; 00055 00056 if (!readMQTTLenString(topicName, &curdata, enddata) || 00057 enddata - curdata < 0) {/* do we have enough data to read the protocol version byte? */ 00058 rc = 0; 00059 goto exit; 00060 } 00061 00062 if (*qos > 0) 00063 *packetid = readInt(&curdata); 00064 00065 *payloadlen = enddata - curdata; 00066 *payload = curdata; 00067 rc = 1; 00068 exit: 00069 FUNC_EXIT_RC(rc); 00070 return rc; 00071 } 00072 00073 00074 00075 /** 00076 * Deserializes the supplied (wire) buffer into an ack 00077 * @param packettype returned integer - the MQTT packet type 00078 * @param dup returned integer - the MQTT dup flag 00079 * @param packetid returned integer - the MQTT packet identifier 00080 * @param buf the raw buffer data, of the correct length determined by the remaining length field 00081 * @param buflen the length in bytes of the data in the supplied buffer 00082 * @return error code. 1 is success, 0 is failure 00083 */ 00084 int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen) 00085 { 00086 MQTTHeader header = {0}; 00087 unsigned char* curdata = buf; 00088 unsigned char* enddata = NULL; 00089 int rc = 0; 00090 int mylen; 00091 00092 FUNC_ENTRY; 00093 header.byte = readChar(&curdata); 00094 *dup = header.bits.dup; 00095 *packettype = header.bits.type; 00096 00097 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 00098 enddata = curdata + mylen; 00099 00100 if (enddata - curdata < 2) 00101 goto exit; 00102 *packetid = readInt(&curdata); 00103 00104 rc = 1; 00105 exit: 00106 FUNC_EXIT_RC(rc); 00107 return rc; 00108 } 00109
Generated on Thu Jul 14 2022 12:58:42 by
