Demo application for using the AT&T IoT Starter Kit Powered by AWS.
Dependencies: SDFileSystem
Fork of ATT_AWS_IoT_demo by
MQTTDeserializePublish.cpp
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 /** 00022 * Deserializes the supplied (wire) buffer into publish data 00023 * @param dup returned integer - the MQTT dup flag 00024 * @param qos returned integer - the MQTT QoS value 00025 * @param retained returned integer - the MQTT retained flag 00026 * @param packetid returned integer - the MQTT packet identifier 00027 * @param topicName returned MQTTString - the MQTT topic in the publish 00028 * @param payload returned byte buffer - the MQTT publish payload 00029 * @param payloadlen returned integer - the length of the MQTT payload 00030 * @param buf the raw buffer data, of the correct length determined by the remaining length field 00031 * @param buflen the length in bytes of the data in the supplied buffer 00032 * @return error code. 1 is success 00033 */ 00034 MQTTReturnCode MQTTDeserialize_publish(unsigned char *dup, QoS *qos, 00035 unsigned char *retained, uint16_t *packetid, 00036 MQTTString* topicName, unsigned char **payload, 00037 uint32_t *payloadlen, unsigned char *buf, size_t buflen) { 00038 MQTTHeader header = {0}; 00039 unsigned char *curdata = buf; 00040 unsigned char *enddata = NULL; 00041 MQTTReturnCode rc = FAILURE; 00042 uint32_t decodedLen = 0; 00043 uint32_t readBytesLen = 0; 00044 00045 FUNC_ENTRY; 00046 if(NULL == dup || NULL == qos || NULL == retained || NULL == packetid) { 00047 FUNC_EXIT_RC(FAILURE); 00048 return FAILURE; 00049 } 00050 00051 /* Publish header size is at least four bytes. 00052 * Fixed header is two bytes. 00053 * Variable header size depends on QoS And Topic Name. 00054 * QoS level 0 doesn't have a message identifier (0 - 2 bytes) 00055 * Topic Name length fields decide size of topic name field (at least 2 bytes) 00056 * MQTT v3.1.1 Specification 3.3.1 */ 00057 if(4 > buflen) { 00058 FUNC_EXIT_RC(MQTTPACKET_BUFFER_TOO_SHORT); 00059 return MQTTPACKET_BUFFER_TOO_SHORT; 00060 } 00061 00062 header.byte = readChar(&curdata); 00063 if(PUBLISH != header.bits.type) { 00064 FUNC_EXIT_RC(FAILURE); 00065 return FAILURE; 00066 } 00067 00068 *dup = header.bits.dup; 00069 *qos = (QoS)header.bits.qos; 00070 *retained = header.bits.retain; 00071 00072 /* read remaining length */ 00073 rc = MQTTPacket_decodeBuf(curdata, &decodedLen, &readBytesLen); 00074 if(SUCCESS != rc) { 00075 FUNC_EXIT_RC(rc); 00076 return rc; 00077 } 00078 curdata += (readBytesLen); 00079 enddata = curdata + decodedLen; 00080 00081 /* do we have enough data to read the protocol version byte? */ 00082 if(SUCCESS != readMQTTLenString(topicName, &curdata, enddata) || (0 > (enddata - curdata))) { 00083 FUNC_EXIT_RC(FAILURE); 00084 return FAILURE; 00085 } 00086 00087 if(QOS0 != *qos) { 00088 *packetid = readPacketId(&curdata); 00089 } 00090 00091 *payloadlen = (uint32_t)(enddata - curdata); 00092 *payload = curdata; 00093 00094 FUNC_EXIT_RC(SUCCESS); 00095 return SUCCESS; 00096 } 00097 00098 /** 00099 * Deserializes the supplied (wire) buffer into an ack 00100 * @param packettype returned integer - the MQTT packet type 00101 * @param dup returned integer - the MQTT dup flag 00102 * @param packetid returned integer - the MQTT packet identifier 00103 * @param buf the raw buffer data, of the correct length determined by the remaining length field 00104 * @param buflen the length in bytes of the data in the supplied buffer 00105 * @return error code. 1 is success, 0 is failure 00106 */ 00107 MQTTReturnCode MQTTDeserialize_ack(unsigned char *packettype, unsigned char *dup, 00108 uint16_t *packetid, unsigned char *buf, 00109 size_t buflen) { 00110 MQTTReturnCode rc = FAILURE; 00111 MQTTHeader header = {0}; 00112 unsigned char *curdata = buf; 00113 unsigned char *enddata = NULL; 00114 uint32_t decodedLen = 0; 00115 uint32_t readBytesLen = 0; 00116 FUNC_ENTRY; 00117 if(NULL == packettype || NULL == dup || NULL == packetid || NULL == buf) { 00118 FUNC_EXIT_RC(MQTT_NULL_VALUE_ERROR); 00119 return MQTT_NULL_VALUE_ERROR; 00120 } 00121 00122 /* PUBACK fixed header size is two bytes, variable header is 2 bytes, MQTT v3.1.1 Specification 3.4.1 */ 00123 if(4 > buflen) { 00124 FUNC_EXIT_RC(MQTTPACKET_BUFFER_TOO_SHORT); 00125 return MQTTPACKET_BUFFER_TOO_SHORT; 00126 } 00127 00128 header.byte = readChar(&curdata); 00129 *dup = header.bits.dup; 00130 *packettype = header.bits.type; 00131 00132 /* read remaining length */ 00133 rc = MQTTPacket_decodeBuf(curdata, &decodedLen, &readBytesLen); 00134 if(SUCCESS != rc) { 00135 FUNC_EXIT_RC(rc); 00136 return rc; 00137 } 00138 curdata += (readBytesLen); 00139 enddata = curdata + decodedLen; 00140 00141 if(enddata - curdata < 2) { 00142 FUNC_EXIT_RC(FAILURE); 00143 return FAILURE; 00144 } 00145 00146 *packetid = readPacketId(&curdata); 00147 00148 FUNC_EXIT_RC(SUCCESS); 00149 return SUCCESS; 00150 } 00151 00152 00153
Generated on Tue Jul 12 2022 22:13:21 by 1.7.2