Port to C027 (using AppShield and Ethernet)

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

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