ME910 support

Dependents:   5_Dragonfly_Cayenne_Sprint_IKS01A1

Fork of Cayenne-MQTT-mbed-MTSAS by Peter Ferland

Committer:
jburhenn
Date:
Fri Oct 07 17:21:45 2016 +0000
Revision:
0:09ef59d2d0f7
Child:
22:0dbabcc6e7b2
Initial commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jburhenn 0:09ef59d2d0f7 1 /*******************************************************************************
jburhenn 0:09ef59d2d0f7 2 * Copyright (c) 2014 IBM Corp.
jburhenn 0:09ef59d2d0f7 3 *
jburhenn 0:09ef59d2d0f7 4 * All rights reserved. This program and the accompanying materials
jburhenn 0:09ef59d2d0f7 5 * are made available under the terms of the Eclipse Public License v1.0
jburhenn 0:09ef59d2d0f7 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
jburhenn 0:09ef59d2d0f7 7 *
jburhenn 0:09ef59d2d0f7 8 * The Eclipse Public License is available at
jburhenn 0:09ef59d2d0f7 9 * http://www.eclipse.org/legal/epl-v10.html
jburhenn 0:09ef59d2d0f7 10 * and the Eclipse Distribution License is available at
jburhenn 0:09ef59d2d0f7 11 * http://www.eclipse.org/org/documents/edl-v10.php.
jburhenn 0:09ef59d2d0f7 12 *
jburhenn 0:09ef59d2d0f7 13 * Contributors:
jburhenn 0:09ef59d2d0f7 14 * Ian Craggs - initial API and implementation and/or initial documentation
jburhenn 0:09ef59d2d0f7 15 *******************************************************************************/
jburhenn 0:09ef59d2d0f7 16
jburhenn 0:09ef59d2d0f7 17 #include "MQTTPacket.h"
jburhenn 0:09ef59d2d0f7 18 #include <string.h>
jburhenn 0:09ef59d2d0f7 19
jburhenn 0:09ef59d2d0f7 20 #define min(a, b) ((a < b) ? 1 : 0)
jburhenn 0:09ef59d2d0f7 21
jburhenn 0:09ef59d2d0f7 22 /**
jburhenn 0:09ef59d2d0f7 23 * Deserializes the supplied (wire) buffer into publish data
jburhenn 0:09ef59d2d0f7 24 * @param dup returned integer - the MQTT dup flag
jburhenn 0:09ef59d2d0f7 25 * @param qos returned integer - the MQTT QoS value
jburhenn 0:09ef59d2d0f7 26 * @param retained returned integer - the MQTT retained flag
jburhenn 0:09ef59d2d0f7 27 * @param packetid returned integer - the MQTT packet identifier
jburhenn 0:09ef59d2d0f7 28 * @param topicName returned MQTTString - the MQTT topic in the publish
jburhenn 0:09ef59d2d0f7 29 * @param payload returned byte buffer - the MQTT publish payload
jburhenn 0:09ef59d2d0f7 30 * @param payloadlen returned integer - the length of the MQTT payload
jburhenn 0:09ef59d2d0f7 31 * @param buf the raw buffer data, of the correct length determined by the remaining length field
jburhenn 0:09ef59d2d0f7 32 * @param buflen the length in bytes of the data in the supplied buffer
jburhenn 0:09ef59d2d0f7 33 * @return error code. 1 is success
jburhenn 0:09ef59d2d0f7 34 */
jburhenn 0:09ef59d2d0f7 35 int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName,
jburhenn 0:09ef59d2d0f7 36 unsigned char** payload, int* payloadlen, unsigned char* buf, int buflen)
jburhenn 0:09ef59d2d0f7 37 {
jburhenn 0:09ef59d2d0f7 38 MQTTHeader header = {0};
jburhenn 0:09ef59d2d0f7 39 unsigned char* curdata = buf;
jburhenn 0:09ef59d2d0f7 40 unsigned char* enddata = NULL;
jburhenn 0:09ef59d2d0f7 41 int rc = 0;
jburhenn 0:09ef59d2d0f7 42 int mylen = 0;
jburhenn 0:09ef59d2d0f7 43
jburhenn 0:09ef59d2d0f7 44 header.byte = readChar(&curdata);
jburhenn 0:09ef59d2d0f7 45 if (header.bits.type != PUBLISH_MSG)
jburhenn 0:09ef59d2d0f7 46 goto exit;
jburhenn 0:09ef59d2d0f7 47 *dup = header.bits.dup;
jburhenn 0:09ef59d2d0f7 48 *qos = header.bits.qos;
jburhenn 0:09ef59d2d0f7 49 *retained = header.bits.retain;
jburhenn 0:09ef59d2d0f7 50
jburhenn 0:09ef59d2d0f7 51 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
jburhenn 0:09ef59d2d0f7 52 enddata = curdata + mylen;
jburhenn 0:09ef59d2d0f7 53
jburhenn 0:09ef59d2d0f7 54 if (!readMQTTLenString(topicName, &curdata, enddata) ||
jburhenn 0:09ef59d2d0f7 55 enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */
jburhenn 0:09ef59d2d0f7 56 goto exit;
jburhenn 0:09ef59d2d0f7 57
jburhenn 0:09ef59d2d0f7 58 if (*qos > 0)
jburhenn 0:09ef59d2d0f7 59 *packetid = readInt(&curdata);
jburhenn 0:09ef59d2d0f7 60
jburhenn 0:09ef59d2d0f7 61 *payloadlen = enddata - curdata;
jburhenn 0:09ef59d2d0f7 62 *payload = curdata;
jburhenn 0:09ef59d2d0f7 63 rc = 1;
jburhenn 0:09ef59d2d0f7 64 exit:
jburhenn 0:09ef59d2d0f7 65 return rc;
jburhenn 0:09ef59d2d0f7 66 }
jburhenn 0:09ef59d2d0f7 67
jburhenn 0:09ef59d2d0f7 68
jburhenn 0:09ef59d2d0f7 69
jburhenn 0:09ef59d2d0f7 70 /**
jburhenn 0:09ef59d2d0f7 71 * Deserializes the supplied (wire) buffer into an ack
jburhenn 0:09ef59d2d0f7 72 * @param packettype returned integer - the MQTT packet type
jburhenn 0:09ef59d2d0f7 73 * @param dup returned integer - the MQTT dup flag
jburhenn 0:09ef59d2d0f7 74 * @param packetid returned integer - the MQTT packet identifier
jburhenn 0:09ef59d2d0f7 75 * @param buf the raw buffer data, of the correct length determined by the remaining length field
jburhenn 0:09ef59d2d0f7 76 * @param buflen the length in bytes of the data in the supplied buffer
jburhenn 0:09ef59d2d0f7 77 * @return error code. 1 is success, 0 is failure
jburhenn 0:09ef59d2d0f7 78 */
jburhenn 0:09ef59d2d0f7 79 int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen)
jburhenn 0:09ef59d2d0f7 80 {
jburhenn 0:09ef59d2d0f7 81 MQTTHeader header = {0};
jburhenn 0:09ef59d2d0f7 82 unsigned char* curdata = buf;
jburhenn 0:09ef59d2d0f7 83 unsigned char* enddata = NULL;
jburhenn 0:09ef59d2d0f7 84 int rc = 0;
jburhenn 0:09ef59d2d0f7 85 int mylen;
jburhenn 0:09ef59d2d0f7 86
jburhenn 0:09ef59d2d0f7 87 header.byte = readChar(&curdata);
jburhenn 0:09ef59d2d0f7 88 *dup = header.bits.dup;
jburhenn 0:09ef59d2d0f7 89 *packettype = header.bits.type;
jburhenn 0:09ef59d2d0f7 90
jburhenn 0:09ef59d2d0f7 91 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
jburhenn 0:09ef59d2d0f7 92 enddata = curdata + mylen;
jburhenn 0:09ef59d2d0f7 93
jburhenn 0:09ef59d2d0f7 94 if (enddata - curdata < 2)
jburhenn 0:09ef59d2d0f7 95 goto exit;
jburhenn 0:09ef59d2d0f7 96 *packetid = readInt(&curdata);
jburhenn 0:09ef59d2d0f7 97
jburhenn 0:09ef59d2d0f7 98 rc = 1;
jburhenn 0:09ef59d2d0f7 99 exit:
jburhenn 0:09ef59d2d0f7 100 return rc;
jburhenn 0:09ef59d2d0f7 101 }
jburhenn 0:09ef59d2d0f7 102