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 "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 * Determines the length of the MQTT subscribe packet that would be produced using the supplied parameters
samdanbury 6:37b6d0d56190 24 * @param count the number of topic filter strings in topicFilters
samdanbury 6:37b6d0d56190 25 * @param topicFilters the array of topic filter strings to be used in the publish
samdanbury 6:37b6d0d56190 26 * @return the length of buffer needed to contain the serialized version of the packet
samdanbury 6:37b6d0d56190 27 */
samdanbury 6:37b6d0d56190 28 int MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[])
samdanbury 6:37b6d0d56190 29 {
samdanbury 6:37b6d0d56190 30 int i;
samdanbury 6:37b6d0d56190 31 int len = 2; /* packetid */
samdanbury 6:37b6d0d56190 32
samdanbury 6:37b6d0d56190 33 for (i = 0; i < count; ++i)
samdanbury 6:37b6d0d56190 34 len += 2 + MQTTstrlen(topicFilters[i]) + 1; /* length + topic + req_qos */
samdanbury 6:37b6d0d56190 35 return len;
samdanbury 6:37b6d0d56190 36 }
samdanbury 6:37b6d0d56190 37
samdanbury 6:37b6d0d56190 38
samdanbury 6:37b6d0d56190 39 /**
samdanbury 6:37b6d0d56190 40 * Serializes the supplied subscribe data into the supplied buffer, ready for sending
samdanbury 6:37b6d0d56190 41 * @param buf the buffer into which the packet will be serialized
samdanbury 6:37b6d0d56190 42 * @param buflen the length in bytes of the supplied bufferr
samdanbury 6:37b6d0d56190 43 * @param dup integer - the MQTT dup flag
samdanbury 6:37b6d0d56190 44 * @param packetid integer - the MQTT packet identifier
samdanbury 6:37b6d0d56190 45 * @param count - number of members in the topicFilters and reqQos arrays
samdanbury 6:37b6d0d56190 46 * @param topicFilters - array of topic filter names
samdanbury 6:37b6d0d56190 47 * @param requestedQoSs - array of requested QoS
samdanbury 6:37b6d0d56190 48 * @return the length of the serialized data. <= 0 indicates error
samdanbury 6:37b6d0d56190 49 */
samdanbury 6:37b6d0d56190 50 int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, int count,
samdanbury 6:37b6d0d56190 51 MQTTString topicFilters[], int requestedQoSs[])
samdanbury 6:37b6d0d56190 52 {
samdanbury 6:37b6d0d56190 53 unsigned char *ptr = buf;
samdanbury 6:37b6d0d56190 54 MQTTHeader header = {0};
samdanbury 6:37b6d0d56190 55 int rem_len = 0;
samdanbury 6:37b6d0d56190 56 int rc = 0;
samdanbury 6:37b6d0d56190 57 int i = 0;
samdanbury 6:37b6d0d56190 58
samdanbury 6:37b6d0d56190 59 FUNC_ENTRY;
samdanbury 6:37b6d0d56190 60 if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(count, topicFilters)) > buflen)
samdanbury 6:37b6d0d56190 61 {
samdanbury 6:37b6d0d56190 62 rc = MQTTPACKET_BUFFER_TOO_SHORT;
samdanbury 6:37b6d0d56190 63 goto exit;
samdanbury 6:37b6d0d56190 64 }
samdanbury 6:37b6d0d56190 65
samdanbury 6:37b6d0d56190 66 header.byte = 0;
samdanbury 6:37b6d0d56190 67 header.bits.type = SUBSCRIBE;
samdanbury 6:37b6d0d56190 68 header.bits.dup = dup;
samdanbury 6:37b6d0d56190 69 header.bits.qos = 1;
samdanbury 6:37b6d0d56190 70 writeChar(&ptr, header.byte); /* write header */
samdanbury 6:37b6d0d56190 71
samdanbury 6:37b6d0d56190 72 ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
samdanbury 6:37b6d0d56190 73
samdanbury 6:37b6d0d56190 74 writeInt(&ptr, packetid);
samdanbury 6:37b6d0d56190 75
samdanbury 6:37b6d0d56190 76 for (i = 0; i < count; ++i)
samdanbury 6:37b6d0d56190 77 {
samdanbury 6:37b6d0d56190 78 writeMQTTString(&ptr, topicFilters[i]);
samdanbury 6:37b6d0d56190 79 writeChar(&ptr, requestedQoSs[i]);
samdanbury 6:37b6d0d56190 80 }
samdanbury 6:37b6d0d56190 81
samdanbury 6:37b6d0d56190 82 rc = ptr - buf;
samdanbury 6:37b6d0d56190 83 exit:
samdanbury 6:37b6d0d56190 84 FUNC_EXIT_RC(rc);
samdanbury 6:37b6d0d56190 85 return rc;
samdanbury 6:37b6d0d56190 86 }
samdanbury 6:37b6d0d56190 87
samdanbury 6:37b6d0d56190 88
samdanbury 6:37b6d0d56190 89
samdanbury 6:37b6d0d56190 90 /**
samdanbury 6:37b6d0d56190 91 * Deserializes the supplied (wire) buffer into suback data
samdanbury 6:37b6d0d56190 92 * @param packetid returned integer - the MQTT packet identifier
samdanbury 6:37b6d0d56190 93 * @param maxcount - the maximum number of members allowed in the grantedQoSs array
samdanbury 6:37b6d0d56190 94 * @param count returned integer - number of members in the grantedQoSs array
samdanbury 6:37b6d0d56190 95 * @param grantedQoSs returned array of integers - the granted qualities of service
samdanbury 6:37b6d0d56190 96 * @param buf the raw buffer data, of the correct length determined by the remaining length field
samdanbury 6:37b6d0d56190 97 * @param buflen the length in bytes of the data in the supplied buffer
samdanbury 6:37b6d0d56190 98 * @return error code. 1 is success, 0 is failure
samdanbury 6:37b6d0d56190 99 */
samdanbury 6:37b6d0d56190 100 int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int buflen)
samdanbury 6:37b6d0d56190 101 {
samdanbury 6:37b6d0d56190 102 MQTTHeader header = {0};
samdanbury 6:37b6d0d56190 103 unsigned char* curdata = buf;
samdanbury 6:37b6d0d56190 104 unsigned char* enddata = NULL;
samdanbury 6:37b6d0d56190 105 int rc = 0;
samdanbury 6:37b6d0d56190 106 int mylen;
samdanbury 6:37b6d0d56190 107
samdanbury 6:37b6d0d56190 108 FUNC_ENTRY;
samdanbury 6:37b6d0d56190 109 header.byte = readChar(&curdata);
samdanbury 6:37b6d0d56190 110 if (header.bits.type != SUBACK)
samdanbury 6:37b6d0d56190 111 goto exit;
samdanbury 6:37b6d0d56190 112
samdanbury 6:37b6d0d56190 113 curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
samdanbury 6:37b6d0d56190 114 enddata = curdata + mylen;
samdanbury 6:37b6d0d56190 115 if (enddata - curdata < 2)
samdanbury 6:37b6d0d56190 116 goto exit;
samdanbury 6:37b6d0d56190 117
samdanbury 6:37b6d0d56190 118 *packetid = readInt(&curdata);
samdanbury 6:37b6d0d56190 119
samdanbury 6:37b6d0d56190 120 *count = 0;
samdanbury 6:37b6d0d56190 121 while (curdata < enddata)
samdanbury 6:37b6d0d56190 122 {
samdanbury 6:37b6d0d56190 123 if (*count > maxcount)
samdanbury 6:37b6d0d56190 124 {
samdanbury 6:37b6d0d56190 125 rc = -1;
samdanbury 6:37b6d0d56190 126 goto exit;
samdanbury 6:37b6d0d56190 127 }
samdanbury 6:37b6d0d56190 128 grantedQoSs[(*count)++] = readChar(&curdata);
samdanbury 6:37b6d0d56190 129 }
samdanbury 6:37b6d0d56190 130
samdanbury 6:37b6d0d56190 131 rc = 1;
samdanbury 6:37b6d0d56190 132 exit:
samdanbury 6:37b6d0d56190 133 FUNC_EXIT_RC(rc);
samdanbury 6:37b6d0d56190 134 return rc;
samdanbury 6:37b6d0d56190 135 }
samdanbury 6:37b6d0d56190 136
samdanbury 6:37b6d0d56190 137