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 #ifndef MQTTCONNECT_H_
samdanbury 6:37b6d0d56190 18 #define MQTTCONNECT_H_
samdanbury 6:37b6d0d56190 19
samdanbury 6:37b6d0d56190 20 typedef union
samdanbury 6:37b6d0d56190 21 {
samdanbury 6:37b6d0d56190 22 unsigned char all; /**< all connect flags */
samdanbury 6:37b6d0d56190 23 #if defined(REVERSED)
samdanbury 6:37b6d0d56190 24 struct
samdanbury 6:37b6d0d56190 25 {
samdanbury 6:37b6d0d56190 26 unsigned int username : 1; /**< 3.1 user name */
samdanbury 6:37b6d0d56190 27 unsigned int password : 1; /**< 3.1 password */
samdanbury 6:37b6d0d56190 28 unsigned int willRetain : 1; /**< will retain setting */
samdanbury 6:37b6d0d56190 29 unsigned int willQoS : 2; /**< will QoS value */
samdanbury 6:37b6d0d56190 30 unsigned int will : 1; /**< will flag */
samdanbury 6:37b6d0d56190 31 unsigned int cleansession : 1; /**< clean session flag */
samdanbury 6:37b6d0d56190 32 unsigned int : 1; /**< unused */
samdanbury 6:37b6d0d56190 33 } bits;
samdanbury 6:37b6d0d56190 34 #else
samdanbury 6:37b6d0d56190 35 struct
samdanbury 6:37b6d0d56190 36 {
samdanbury 6:37b6d0d56190 37 unsigned int : 1; /**< unused */
samdanbury 6:37b6d0d56190 38 unsigned int cleansession : 1; /**< cleansession flag */
samdanbury 6:37b6d0d56190 39 unsigned int will : 1; /**< will flag */
samdanbury 6:37b6d0d56190 40 unsigned int willQoS : 2; /**< will QoS value */
samdanbury 6:37b6d0d56190 41 unsigned int willRetain : 1; /**< will retain setting */
samdanbury 6:37b6d0d56190 42 unsigned int password : 1; /**< 3.1 password */
samdanbury 6:37b6d0d56190 43 unsigned int username : 1; /**< 3.1 user name */
samdanbury 6:37b6d0d56190 44 } bits;
samdanbury 6:37b6d0d56190 45 #endif
samdanbury 6:37b6d0d56190 46 } MQTTConnectFlags; /**< connect flags byte */
samdanbury 6:37b6d0d56190 47
samdanbury 6:37b6d0d56190 48
samdanbury 6:37b6d0d56190 49
samdanbury 6:37b6d0d56190 50 /**
samdanbury 6:37b6d0d56190 51 * Defines the MQTT "Last Will and Testament" (LWT) settings for
samdanbury 6:37b6d0d56190 52 * the connect packet.
samdanbury 6:37b6d0d56190 53 */
samdanbury 6:37b6d0d56190 54 typedef struct
samdanbury 6:37b6d0d56190 55 {
samdanbury 6:37b6d0d56190 56 /** The eyecatcher for this structure. must be MQTW. */
samdanbury 6:37b6d0d56190 57 char struct_id[4];
samdanbury 6:37b6d0d56190 58 /** The version number of this structure. Must be 0 */
samdanbury 6:37b6d0d56190 59 int struct_version;
samdanbury 6:37b6d0d56190 60 /** The LWT topic to which the LWT message will be published. */
samdanbury 6:37b6d0d56190 61 MQTTString topicName;
samdanbury 6:37b6d0d56190 62 /** The LWT payload. */
samdanbury 6:37b6d0d56190 63 MQTTString message;
samdanbury 6:37b6d0d56190 64 /**
samdanbury 6:37b6d0d56190 65 * The retained flag for the LWT message (see MQTTAsync_message.retained).
samdanbury 6:37b6d0d56190 66 */
samdanbury 6:37b6d0d56190 67 unsigned char retained;
samdanbury 6:37b6d0d56190 68 /**
samdanbury 6:37b6d0d56190 69 * The quality of service setting for the LWT message (see
samdanbury 6:37b6d0d56190 70 * MQTTAsync_message.qos and @ref qos).
samdanbury 6:37b6d0d56190 71 */
samdanbury 6:37b6d0d56190 72 char qos;
samdanbury 6:37b6d0d56190 73 } MQTTPacket_willOptions;
samdanbury 6:37b6d0d56190 74
samdanbury 6:37b6d0d56190 75
samdanbury 6:37b6d0d56190 76 #define MQTTPacket_willOptions_initializer { {'M', 'Q', 'T', 'W'}, 0, {NULL, {0, NULL}}, {NULL, {0, NULL}}, 0, 0 }
samdanbury 6:37b6d0d56190 77
samdanbury 6:37b6d0d56190 78
samdanbury 6:37b6d0d56190 79 typedef struct
samdanbury 6:37b6d0d56190 80 {
samdanbury 6:37b6d0d56190 81 /** The eyecatcher for this structure. must be MQTC. */
samdanbury 6:37b6d0d56190 82 char struct_id[4];
samdanbury 6:37b6d0d56190 83 /** The version number of this structure. Must be 0 */
samdanbury 6:37b6d0d56190 84 int struct_version;
samdanbury 6:37b6d0d56190 85 /** Version of MQTT to be used. 3 = 3.1 4 = 3.1.1
samdanbury 6:37b6d0d56190 86 */
samdanbury 6:37b6d0d56190 87 unsigned char MQTTVersion;
samdanbury 6:37b6d0d56190 88 MQTTString clientID;
samdanbury 6:37b6d0d56190 89 unsigned short keepAliveInterval;
samdanbury 6:37b6d0d56190 90 unsigned char cleansession;
samdanbury 6:37b6d0d56190 91 unsigned char willFlag;
samdanbury 6:37b6d0d56190 92 MQTTPacket_willOptions will;
samdanbury 6:37b6d0d56190 93 MQTTString username;
samdanbury 6:37b6d0d56190 94 MQTTString password;
samdanbury 6:37b6d0d56190 95 } MQTTPacket_connectData;
samdanbury 6:37b6d0d56190 96
samdanbury 6:37b6d0d56190 97 typedef union
samdanbury 6:37b6d0d56190 98 {
samdanbury 6:37b6d0d56190 99 unsigned char all; /**< all connack flags */
samdanbury 6:37b6d0d56190 100 #if defined(REVERSED)
samdanbury 6:37b6d0d56190 101 struct
samdanbury 6:37b6d0d56190 102 {
samdanbury 6:37b6d0d56190 103 unsigned int sessionpresent : 1; /**< session present flag */
samdanbury 6:37b6d0d56190 104 unsigned int : y; /**< unused */
samdanbury 6:37b6d0d56190 105 } bits;
samdanbury 6:37b6d0d56190 106 #else
samdanbury 6:37b6d0d56190 107 struct
samdanbury 6:37b6d0d56190 108 {
samdanbury 6:37b6d0d56190 109 unsigned int : 7; /**< unused */
samdanbury 6:37b6d0d56190 110 unsigned int sessionpresent : 1; /**< session present flag */
samdanbury 6:37b6d0d56190 111 } bits;
samdanbury 6:37b6d0d56190 112 #endif
samdanbury 6:37b6d0d56190 113 } MQTTConnackFlags; /**< connack flags byte */
samdanbury 6:37b6d0d56190 114
samdanbury 6:37b6d0d56190 115 #define MQTTPacket_connectData_initializer { {'M', 'Q', 'T', 'C'}, 0, 4, {NULL, {0, NULL}}, 60, 1, 0, \
samdanbury 6:37b6d0d56190 116 MQTTPacket_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} }
samdanbury 6:37b6d0d56190 117
samdanbury 6:37b6d0d56190 118 int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options);
samdanbury 6:37b6d0d56190 119 int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len);
samdanbury 6:37b6d0d56190 120
samdanbury 6:37b6d0d56190 121 int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent);
samdanbury 6:37b6d0d56190 122 int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen);
samdanbury 6:37b6d0d56190 123
samdanbury 6:37b6d0d56190 124 int MQTTSerialize_disconnect(unsigned char* buf, int buflen);
samdanbury 6:37b6d0d56190 125 int MQTTSerialize_pingreq(unsigned char* buf, int buflen);
samdanbury 6:37b6d0d56190 126
samdanbury 6:37b6d0d56190 127 #endif /* MQTTCONNECT_H_ */