Contains example code to connect the mbed LPC1768 or FRDM-K64F devices to the IBM Internet of Things Cloud service via ethernet.

Dependencies:   C12832 MQTT LM75B MMA7660

Dependents:   MFT_IoT_demo_USB400 IBM_RFID

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 "MQTTPacket.h"
samdanbury 6:37b6d0d56190 18 #include "StackTrace.h"
samdanbury 6:37b6d0d56190 19
samdanbury 6:37b6d0d56190 20 #include <string.h>
samdanbury 6:37b6d0d56190 21
samdanbury 6:37b6d0d56190 22
samdanbury 6:37b6d0d56190 23 /**
samdanbury 6:37b6d0d56190 24 * Deserializes the supplied (wire) buffer into subscribe data
samdanbury 6:37b6d0d56190 25 * @param dup integer returned - the MQTT dup flag
samdanbury 6:37b6d0d56190 26 * @param packetid integer returned - the MQTT packet identifier
samdanbury 6:37b6d0d56190 27 * @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays
samdanbury 6:37b6d0d56190 28 * @param count - number of members in the topicFilters and requestedQoSs arrays
samdanbury 6:37b6d0d56190 29 * @param topicFilters - array of topic filter names
samdanbury 6:37b6d0d56190 30 * @param requestedQoSs - array of requested QoS
samdanbury 6:37b6d0d56190 31 * @param buf the raw buffer data, of the correct length determined by the remaining length field
samdanbury 6:37b6d0d56190 32 * @param buflen the length in bytes of the data in the supplied buffer
samdanbury 6:37b6d0d56190 33 * @return the length of the serialized data. <= 0 indicates error
samdanbury 6:37b6d0d56190 34 */
samdanbury 6:37b6d0d56190 35 int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[],
samdanbury 6:37b6d0d56190 36 int requestedQoSs[], unsigned char* buf, int buflen)
samdanbury 6:37b6d0d56190 37 {
samdanbury 6:37b6d0d56190 38 MQTTHeader header = {0};
samdanbury 6:37b6d0d56190 39 unsigned char* curdata = buf;
samdanbury 6:37b6d0d56190 40 unsigned char* enddata = NULL;
samdanbury 6:37b6d0d56190 41 int rc = -1;
samdanbury 6:37b6d0d56190 42 int mylen = 0;
samdanbury 6:37b6d0d56190 43
samdanbury 6:37b6d0d56190 44 FUNC_ENTRY;
samdanbury 6:37b6d0d56190 45 header.byte = readChar(&curdata);
samdanbury 6:37b6d0d56190 46 if (header.bits.type != SUBSCRIBE)
samdanbury 6:37b6d0d56190 47 goto exit;
samdanbury 6:37b6d0d56190 48 *dup = header.bits.dup;
samdanbury 6:37b6d0d56190 49
samdanbury 6:37b6d0d56190 50 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
samdanbury 6:37b6d0d56190 51 enddata = curdata + mylen;
samdanbury 6:37b6d0d56190 52
samdanbury 6:37b6d0d56190 53 *packetid = readInt(&curdata);
samdanbury 6:37b6d0d56190 54
samdanbury 6:37b6d0d56190 55 *count = 0;
samdanbury 6:37b6d0d56190 56 while (curdata < enddata)
samdanbury 6:37b6d0d56190 57 {
samdanbury 6:37b6d0d56190 58 if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata))
samdanbury 6:37b6d0d56190 59 goto exit;
samdanbury 6:37b6d0d56190 60 if (curdata >= enddata) /* do we have enough data to read the req_qos version byte? */
samdanbury 6:37b6d0d56190 61 goto exit;
samdanbury 6:37b6d0d56190 62 requestedQoSs[*count] = readChar(&curdata);
samdanbury 6:37b6d0d56190 63 (*count)++;
samdanbury 6:37b6d0d56190 64 }
samdanbury 6:37b6d0d56190 65
samdanbury 6:37b6d0d56190 66 rc = 1;
samdanbury 6:37b6d0d56190 67 exit:
samdanbury 6:37b6d0d56190 68 FUNC_EXIT_RC(rc);
samdanbury 6:37b6d0d56190 69 return rc;
samdanbury 6:37b6d0d56190 70 }
samdanbury 6:37b6d0d56190 71
samdanbury 6:37b6d0d56190 72
samdanbury 6:37b6d0d56190 73 /**
samdanbury 6:37b6d0d56190 74 * Serializes the supplied suback data into the supplied buffer, ready for sending
samdanbury 6:37b6d0d56190 75 * @param buf the buffer into which the packet will be serialized
samdanbury 6:37b6d0d56190 76 * @param buflen the length in bytes of the supplied buffer
samdanbury 6:37b6d0d56190 77 * @param packetid integer - the MQTT packet identifier
samdanbury 6:37b6d0d56190 78 * @param count - number of members in the grantedQoSs array
samdanbury 6:37b6d0d56190 79 * @param grantedQoSs - array of granted QoS
samdanbury 6:37b6d0d56190 80 * @return the length of the serialized data. <= 0 indicates error
samdanbury 6:37b6d0d56190 81 */
samdanbury 6:37b6d0d56190 82 int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs)
samdanbury 6:37b6d0d56190 83 {
samdanbury 6:37b6d0d56190 84 MQTTHeader header = {0};
samdanbury 6:37b6d0d56190 85 int rc = -1;
samdanbury 6:37b6d0d56190 86 unsigned char *ptr = buf;
samdanbury 6:37b6d0d56190 87 int i;
samdanbury 6:37b6d0d56190 88
samdanbury 6:37b6d0d56190 89 FUNC_ENTRY;
samdanbury 6:37b6d0d56190 90 if (buflen < 2 + count)
samdanbury 6:37b6d0d56190 91 {
samdanbury 6:37b6d0d56190 92 rc = MQTTPACKET_BUFFER_TOO_SHORT;
samdanbury 6:37b6d0d56190 93 goto exit;
samdanbury 6:37b6d0d56190 94 }
samdanbury 6:37b6d0d56190 95 header.byte = 0;
samdanbury 6:37b6d0d56190 96 header.bits.type = SUBACK;
samdanbury 6:37b6d0d56190 97 writeChar(&ptr, header.byte); /* write header */
samdanbury 6:37b6d0d56190 98
samdanbury 6:37b6d0d56190 99 ptr += MQTTPacket_encode(ptr, 2 + count); /* write remaining length */
samdanbury 6:37b6d0d56190 100
samdanbury 6:37b6d0d56190 101 writeInt(&ptr, packetid);
samdanbury 6:37b6d0d56190 102
samdanbury 6:37b6d0d56190 103 for (i = 0; i < count; ++i)
samdanbury 6:37b6d0d56190 104 writeChar(&ptr, grantedQoSs[i]);
samdanbury 6:37b6d0d56190 105
samdanbury 6:37b6d0d56190 106 rc = ptr - buf;
samdanbury 6:37b6d0d56190 107 exit:
samdanbury 6:37b6d0d56190 108 FUNC_EXIT_RC(rc);
samdanbury 6:37b6d0d56190 109 return rc;
samdanbury 6:37b6d0d56190 110 }
samdanbury 6:37b6d0d56190 111
samdanbury 6:37b6d0d56190 112