MQTTPacket library (clone of https://os.mbed.com/teams/mqtt/code/MQTTPacket/)
Embed:
(wiki syntax)
Show/hide line numbers
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 goto exit; 00059 00060 if (*qos > 0) 00061 *packetid = readInt(&curdata); 00062 00063 *payloadlen = enddata - curdata; 00064 *payload = curdata; 00065 rc = 1; 00066 exit: 00067 FUNC_EXIT_RC(rc); 00068 return rc; 00069 } 00070 00071 00072 00073 /** 00074 * Deserializes the supplied (wire) buffer into an ack 00075 * @param packettype returned integer - the MQTT packet type 00076 * @param dup returned integer - the MQTT dup flag 00077 * @param packetid returned integer - the MQTT packet identifier 00078 * @param buf the raw buffer data, of the correct length determined by the remaining length field 00079 * @param buflen the length in bytes of the data in the supplied buffer 00080 * @return error code. 1 is success, 0 is failure 00081 */ 00082 int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen) 00083 { 00084 MQTTHeader header = {0}; 00085 unsigned char* curdata = buf; 00086 unsigned char* enddata = NULL; 00087 int rc = 0; 00088 int mylen; 00089 00090 FUNC_ENTRY; 00091 header.byte = readChar(&curdata); 00092 *dup = header.bits.dup; 00093 *packettype = header.bits.type; 00094 00095 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 00096 enddata = curdata + mylen; 00097 00098 if (enddata - curdata < 2) 00099 goto exit; 00100 *packetid = readInt(&curdata); 00101 00102 rc = 1; 00103 exit: 00104 FUNC_EXIT_RC(rc); 00105 return rc; 00106 } 00107
Generated on Thu Jul 14 2022 11:12:53 by 1.7.2