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 * Determines the length of the MQTT unsubscribe 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_unsubscribeLength(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]); /* length + topic*/
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 unsubscribe data into the supplied buffer, ready for sending
samdanbury 6:37b6d0d56190 41 * @param buf the raw buffer data, of the correct length determined by the remaining length field
samdanbury 6:37b6d0d56190 42 * @param buflen the length in bytes of the data in the supplied buffer
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 array
samdanbury 6:37b6d0d56190 46 * @param topicFilters - array of topic filter names
samdanbury 6:37b6d0d56190 47 * @return the length of the serialized data. <= 0 indicates error
samdanbury 6:37b6d0d56190 48 */
samdanbury 6:37b6d0d56190 49 int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid,
samdanbury 6:37b6d0d56190 50 int count, MQTTString topicFilters[])
samdanbury 6:37b6d0d56190 51 {
samdanbury 6:37b6d0d56190 52 unsigned char *ptr = buf;
samdanbury 6:37b6d0d56190 53 MQTTHeader header = {0};
samdanbury 6:37b6d0d56190 54 int rem_len = 0;
samdanbury 6:37b6d0d56190 55 int rc = -1;
samdanbury 6:37b6d0d56190 56 int i = 0;
samdanbury 6:37b6d0d56190 57
samdanbury 6:37b6d0d56190 58 FUNC_ENTRY;
samdanbury 6:37b6d0d56190 59 if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters)) > buflen)
samdanbury 6:37b6d0d56190 60 {
samdanbury 6:37b6d0d56190 61 rc = MQTTPACKET_BUFFER_TOO_SHORT;
samdanbury 6:37b6d0d56190 62 goto exit;
samdanbury 6:37b6d0d56190 63 }
samdanbury 6:37b6d0d56190 64
samdanbury 6:37b6d0d56190 65 header.byte = 0;
samdanbury 6:37b6d0d56190 66 header.bits.type = UNSUBSCRIBE;
samdanbury 6:37b6d0d56190 67 header.bits.dup = dup;
samdanbury 6:37b6d0d56190 68 header.bits.qos = 1;
samdanbury 6:37b6d0d56190 69 writeChar(&ptr, header.byte); /* write header */
samdanbury 6:37b6d0d56190 70
samdanbury 6:37b6d0d56190 71 ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
samdanbury 6:37b6d0d56190 72
samdanbury 6:37b6d0d56190 73 writeInt(&ptr, packetid);
samdanbury 6:37b6d0d56190 74
samdanbury 6:37b6d0d56190 75 for (i = 0; i < count; ++i)
samdanbury 6:37b6d0d56190 76 writeMQTTString(&ptr, topicFilters[i]);
samdanbury 6:37b6d0d56190 77
samdanbury 6:37b6d0d56190 78 rc = ptr - buf;
samdanbury 6:37b6d0d56190 79 exit:
samdanbury 6:37b6d0d56190 80 FUNC_EXIT_RC(rc);
samdanbury 6:37b6d0d56190 81 return rc;
samdanbury 6:37b6d0d56190 82 }
samdanbury 6:37b6d0d56190 83
samdanbury 6:37b6d0d56190 84
samdanbury 6:37b6d0d56190 85 /**
samdanbury 6:37b6d0d56190 86 * Deserializes the supplied (wire) buffer into unsuback data
samdanbury 6:37b6d0d56190 87 * @param packetid returned integer - the MQTT packet identifier
samdanbury 6:37b6d0d56190 88 * @param buf the raw buffer data, of the correct length determined by the remaining length field
samdanbury 6:37b6d0d56190 89 * @param buflen the length in bytes of the data in the supplied buffer
samdanbury 6:37b6d0d56190 90 * @return error code. 1 is success, 0 is failure
samdanbury 6:37b6d0d56190 91 */
samdanbury 6:37b6d0d56190 92 int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen)
samdanbury 6:37b6d0d56190 93 {
samdanbury 6:37b6d0d56190 94 unsigned char type = 0;
samdanbury 6:37b6d0d56190 95 unsigned char dup = 0;
samdanbury 6:37b6d0d56190 96 int rc = 0;
samdanbury 6:37b6d0d56190 97
samdanbury 6:37b6d0d56190 98 FUNC_ENTRY;
samdanbury 6:37b6d0d56190 99 rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen);
samdanbury 6:37b6d0d56190 100 if (type == UNSUBACK)
samdanbury 6:37b6d0d56190 101 rc = 1;
samdanbury 6:37b6d0d56190 102 FUNC_EXIT_RC(rc);
samdanbury 6:37b6d0d56190 103 return rc;
samdanbury 6:37b6d0d56190 104 }
samdanbury 6:37b6d0d56190 105
samdanbury 6:37b6d0d56190 106