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 * Determines the length of the MQTT publish packet that would be produced using the supplied parameters
samdanbury 6:37b6d0d56190 25 * @param qos the MQTT QoS of the publish (packetid is omitted for QoS 0)
samdanbury 6:37b6d0d56190 26 * @param topicName the topic name to be used in the publish
samdanbury 6:37b6d0d56190 27 * @param payloadlen the length of the payload to be sent
samdanbury 6:37b6d0d56190 28 * @return the length of buffer needed to contain the serialized version of the packet
samdanbury 6:37b6d0d56190 29 */
samdanbury 6:37b6d0d56190 30 int MQTTSerialize_publishLength(int qos, MQTTString topicName, int payloadlen)
samdanbury 6:37b6d0d56190 31 {
samdanbury 6:37b6d0d56190 32 int len = 0;
samdanbury 6:37b6d0d56190 33
samdanbury 6:37b6d0d56190 34 len += 2 + MQTTstrlen(topicName) + payloadlen;
samdanbury 6:37b6d0d56190 35 if (qos > 0)
samdanbury 6:37b6d0d56190 36 len += 2; /* packetid */
samdanbury 6:37b6d0d56190 37 return len;
samdanbury 6:37b6d0d56190 38 }
samdanbury 6:37b6d0d56190 39
samdanbury 6:37b6d0d56190 40
samdanbury 6:37b6d0d56190 41 /**
samdanbury 6:37b6d0d56190 42 * Serializes the supplied publish data into the supplied buffer, ready for sending
samdanbury 6:37b6d0d56190 43 * @param buf the buffer into which the packet will be serialized
samdanbury 6:37b6d0d56190 44 * @param buflen the length in bytes of the supplied buffer
samdanbury 6:37b6d0d56190 45 * @param dup integer - the MQTT dup flag
samdanbury 6:37b6d0d56190 46 * @param qos integer - the MQTT QoS value
samdanbury 6:37b6d0d56190 47 * @param retained integer - the MQTT retained flag
samdanbury 6:37b6d0d56190 48 * @param packetid integer - the MQTT packet identifier
samdanbury 6:37b6d0d56190 49 * @param topicName MQTTString - the MQTT topic in the publish
samdanbury 6:37b6d0d56190 50 * @param payload byte buffer - the MQTT publish payload
samdanbury 6:37b6d0d56190 51 * @param payloadlen integer - the length of the MQTT payload
samdanbury 6:37b6d0d56190 52 * @return the length of the serialized data. <= 0 indicates error
samdanbury 6:37b6d0d56190 53 */
samdanbury 6:37b6d0d56190 54 int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid,
samdanbury 6:37b6d0d56190 55 MQTTString topicName, unsigned char* payload, int payloadlen)
samdanbury 6:37b6d0d56190 56 {
samdanbury 6:37b6d0d56190 57 unsigned char *ptr = buf;
samdanbury 6:37b6d0d56190 58 MQTTHeader header = {0};
samdanbury 6:37b6d0d56190 59 int rem_len = 0;
samdanbury 6:37b6d0d56190 60 int rc = 0;
samdanbury 6:37b6d0d56190 61
samdanbury 6:37b6d0d56190 62 FUNC_ENTRY;
samdanbury 6:37b6d0d56190 63 if (MQTTPacket_len(rem_len = MQTTSerialize_publishLength(qos, topicName, payloadlen)) > buflen)
samdanbury 6:37b6d0d56190 64 {
samdanbury 6:37b6d0d56190 65 rc = MQTTPACKET_BUFFER_TOO_SHORT;
samdanbury 6:37b6d0d56190 66 goto exit;
samdanbury 6:37b6d0d56190 67 }
samdanbury 6:37b6d0d56190 68
samdanbury 6:37b6d0d56190 69 header.bits.type = PUBLISH;
samdanbury 6:37b6d0d56190 70 header.bits.dup = dup;
samdanbury 6:37b6d0d56190 71 header.bits.qos = qos;
samdanbury 6:37b6d0d56190 72 header.bits.retain = retained;
samdanbury 6:37b6d0d56190 73 writeChar(&ptr, header.byte); /* write header */
samdanbury 6:37b6d0d56190 74
samdanbury 6:37b6d0d56190 75 ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
samdanbury 6:37b6d0d56190 76
samdanbury 6:37b6d0d56190 77 writeMQTTString(&ptr, topicName);
samdanbury 6:37b6d0d56190 78
samdanbury 6:37b6d0d56190 79 if (qos > 0)
samdanbury 6:37b6d0d56190 80 writeInt(&ptr, packetid);
samdanbury 6:37b6d0d56190 81
samdanbury 6:37b6d0d56190 82 memcpy(ptr, payload, payloadlen);
samdanbury 6:37b6d0d56190 83 ptr += payloadlen;
samdanbury 6:37b6d0d56190 84
samdanbury 6:37b6d0d56190 85 rc = ptr - buf;
samdanbury 6:37b6d0d56190 86
samdanbury 6:37b6d0d56190 87 exit:
samdanbury 6:37b6d0d56190 88 FUNC_EXIT_RC(rc);
samdanbury 6:37b6d0d56190 89 return rc;
samdanbury 6:37b6d0d56190 90 }
samdanbury 6:37b6d0d56190 91
samdanbury 6:37b6d0d56190 92
samdanbury 6:37b6d0d56190 93
samdanbury 6:37b6d0d56190 94 /**
samdanbury 6:37b6d0d56190 95 * Serializes the ack packet into the supplied buffer.
samdanbury 6:37b6d0d56190 96 * @param buf the buffer into which the packet will be serialized
samdanbury 6:37b6d0d56190 97 * @param buflen the length in bytes of the supplied buffer
samdanbury 6:37b6d0d56190 98 * @param type the MQTT packet type
samdanbury 6:37b6d0d56190 99 * @param dup the MQTT dup flag
samdanbury 6:37b6d0d56190 100 * @param packetid the MQTT packet identifier
samdanbury 6:37b6d0d56190 101 * @return serialized length, or error if 0
samdanbury 6:37b6d0d56190 102 */
samdanbury 6:37b6d0d56190 103 int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
samdanbury 6:37b6d0d56190 104 {
samdanbury 6:37b6d0d56190 105 MQTTHeader header = {0};
samdanbury 6:37b6d0d56190 106 int rc = 0;
samdanbury 6:37b6d0d56190 107 unsigned char *ptr = buf;
samdanbury 6:37b6d0d56190 108
samdanbury 6:37b6d0d56190 109 FUNC_ENTRY;
samdanbury 6:37b6d0d56190 110 if (buflen < 4)
samdanbury 6:37b6d0d56190 111 {
samdanbury 6:37b6d0d56190 112 rc = MQTTPACKET_BUFFER_TOO_SHORT;
samdanbury 6:37b6d0d56190 113 goto exit;
samdanbury 6:37b6d0d56190 114 }
samdanbury 6:37b6d0d56190 115 header.bits.type = packettype;
samdanbury 6:37b6d0d56190 116 header.bits.dup = dup;
samdanbury 6:37b6d0d56190 117 header.bits.qos = 0;
samdanbury 6:37b6d0d56190 118 writeChar(&ptr, header.byte); /* write header */
samdanbury 6:37b6d0d56190 119
samdanbury 6:37b6d0d56190 120 ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
samdanbury 6:37b6d0d56190 121 writeInt(&ptr, packetid);
samdanbury 6:37b6d0d56190 122 rc = ptr - buf;
samdanbury 6:37b6d0d56190 123 exit:
samdanbury 6:37b6d0d56190 124 FUNC_EXIT_RC(rc);
samdanbury 6:37b6d0d56190 125 return rc;
samdanbury 6:37b6d0d56190 126 }
samdanbury 6:37b6d0d56190 127
samdanbury 6:37b6d0d56190 128
samdanbury 6:37b6d0d56190 129 /**
samdanbury 6:37b6d0d56190 130 * Serializes a puback packet into the supplied buffer.
samdanbury 6:37b6d0d56190 131 * @param buf the buffer into which the packet will be serialized
samdanbury 6:37b6d0d56190 132 * @param buflen the length in bytes of the supplied buffer
samdanbury 6:37b6d0d56190 133 * @param packetid integer - the MQTT packet identifier
samdanbury 6:37b6d0d56190 134 * @return serialized length, or error if 0
samdanbury 6:37b6d0d56190 135 */
samdanbury 6:37b6d0d56190 136 int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid)
samdanbury 6:37b6d0d56190 137 {
samdanbury 6:37b6d0d56190 138 return MQTTSerialize_ack(buf, buflen, PUBACK, packetid, 0);
samdanbury 6:37b6d0d56190 139 }
samdanbury 6:37b6d0d56190 140
samdanbury 6:37b6d0d56190 141
samdanbury 6:37b6d0d56190 142 /**
samdanbury 6:37b6d0d56190 143 * Serializes a pubrel packet into the supplied buffer.
samdanbury 6:37b6d0d56190 144 * @param buf the buffer into which the packet will be serialized
samdanbury 6:37b6d0d56190 145 * @param buflen the length in bytes of the supplied buffer
samdanbury 6:37b6d0d56190 146 * @param dup integer - the MQTT dup flag
samdanbury 6:37b6d0d56190 147 * @param packetid integer - the MQTT packet identifier
samdanbury 6:37b6d0d56190 148 * @return serialized length, or error if 0
samdanbury 6:37b6d0d56190 149 */
samdanbury 6:37b6d0d56190 150 int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid)
samdanbury 6:37b6d0d56190 151 {
samdanbury 6:37b6d0d56190 152 return MQTTSerialize_ack(buf, buflen, PUBREL, packetid, dup);
samdanbury 6:37b6d0d56190 153 }
samdanbury 6:37b6d0d56190 154
samdanbury 6:37b6d0d56190 155
samdanbury 6:37b6d0d56190 156 /**
samdanbury 6:37b6d0d56190 157 * Serializes a pubrel packet into the supplied buffer.
samdanbury 6:37b6d0d56190 158 * @param buf the buffer into which the packet will be serialized
samdanbury 6:37b6d0d56190 159 * @param buflen the length in bytes of the supplied buffer
samdanbury 6:37b6d0d56190 160 * @param packetid integer - the MQTT packet identifier
samdanbury 6:37b6d0d56190 161 * @return serialized length, or error if 0
samdanbury 6:37b6d0d56190 162 */
samdanbury 6:37b6d0d56190 163 int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid)
samdanbury 6:37b6d0d56190 164 {
samdanbury 6:37b6d0d56190 165 return MQTTSerialize_ack(buf, buflen, PUBCOMP, packetid, 0);
samdanbury 6:37b6d0d56190 166 }
samdanbury 6:37b6d0d56190 167
samdanbury 6:37b6d0d56190 168