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 MQTTPACKET_H_
samdanbury 6:37b6d0d56190 18 #define MQTTPACKET_H_
samdanbury 6:37b6d0d56190 19
samdanbury 6:37b6d0d56190 20 #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
samdanbury 6:37b6d0d56190 21 extern "C" {
samdanbury 6:37b6d0d56190 22 #endif
samdanbury 6:37b6d0d56190 23
samdanbury 6:37b6d0d56190 24 enum errors
samdanbury 6:37b6d0d56190 25 {
samdanbury 6:37b6d0d56190 26 MQTTPACKET_BUFFER_TOO_SHORT = -2,
samdanbury 6:37b6d0d56190 27 MQTTPACKET_READ_ERROR = -1,
samdanbury 6:37b6d0d56190 28 MQTTPACKET_READ_COMPLETE,
samdanbury 6:37b6d0d56190 29 };
samdanbury 6:37b6d0d56190 30
samdanbury 6:37b6d0d56190 31 enum msgTypes
samdanbury 6:37b6d0d56190 32 {
samdanbury 6:37b6d0d56190 33 CONNECT = 1, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL,
samdanbury 6:37b6d0d56190 34 PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK,
samdanbury 6:37b6d0d56190 35 PINGREQ, PINGRESP, DISCONNECT
samdanbury 6:37b6d0d56190 36 };
samdanbury 6:37b6d0d56190 37
samdanbury 6:37b6d0d56190 38 /**
samdanbury 6:37b6d0d56190 39 * Bitfields for the MQTT header byte.
samdanbury 6:37b6d0d56190 40 */
samdanbury 6:37b6d0d56190 41 typedef union
samdanbury 6:37b6d0d56190 42 {
samdanbury 6:37b6d0d56190 43 unsigned char byte; /**< the whole byte */
samdanbury 6:37b6d0d56190 44 #if defined(REVERSED)
samdanbury 6:37b6d0d56190 45 struct
samdanbury 6:37b6d0d56190 46 {
samdanbury 6:37b6d0d56190 47 unsigned int type : 4; /**< message type nibble */
samdanbury 6:37b6d0d56190 48 unsigned int dup : 1; /**< DUP flag bit */
samdanbury 6:37b6d0d56190 49 unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
samdanbury 6:37b6d0d56190 50 unsigned int retain : 1; /**< retained flag bit */
samdanbury 6:37b6d0d56190 51 } bits;
samdanbury 6:37b6d0d56190 52 #else
samdanbury 6:37b6d0d56190 53 struct
samdanbury 6:37b6d0d56190 54 {
samdanbury 6:37b6d0d56190 55 unsigned int retain : 1; /**< retained flag bit */
samdanbury 6:37b6d0d56190 56 unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
samdanbury 6:37b6d0d56190 57 unsigned int dup : 1; /**< DUP flag bit */
samdanbury 6:37b6d0d56190 58 unsigned int type : 4; /**< message type nibble */
samdanbury 6:37b6d0d56190 59 } bits;
samdanbury 6:37b6d0d56190 60 #endif
samdanbury 6:37b6d0d56190 61 } MQTTHeader;
samdanbury 6:37b6d0d56190 62
samdanbury 6:37b6d0d56190 63 typedef struct
samdanbury 6:37b6d0d56190 64 {
samdanbury 6:37b6d0d56190 65 int len;
samdanbury 6:37b6d0d56190 66 char* data;
samdanbury 6:37b6d0d56190 67 } MQTTLenString;
samdanbury 6:37b6d0d56190 68
samdanbury 6:37b6d0d56190 69 typedef struct
samdanbury 6:37b6d0d56190 70 {
samdanbury 6:37b6d0d56190 71 char* cstring;
samdanbury 6:37b6d0d56190 72 MQTTLenString lenstring;
samdanbury 6:37b6d0d56190 73 } MQTTString;
samdanbury 6:37b6d0d56190 74
samdanbury 6:37b6d0d56190 75 #define MQTTString_initializer {NULL, {0, NULL}}
samdanbury 6:37b6d0d56190 76
samdanbury 6:37b6d0d56190 77 int MQTTstrlen(MQTTString mqttstring);
samdanbury 6:37b6d0d56190 78
samdanbury 6:37b6d0d56190 79 #include "MQTTConnect.h"
samdanbury 6:37b6d0d56190 80 #include "MQTTPublish.h"
samdanbury 6:37b6d0d56190 81 #include "MQTTSubscribe.h"
samdanbury 6:37b6d0d56190 82 #include "MQTTUnsubscribe.h"
samdanbury 6:37b6d0d56190 83
samdanbury 6:37b6d0d56190 84 int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char type, unsigned char dup, unsigned short packetid);
samdanbury 6:37b6d0d56190 85 int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen);
samdanbury 6:37b6d0d56190 86
samdanbury 6:37b6d0d56190 87 int MQTTPacket_len(int rem_len);
samdanbury 6:37b6d0d56190 88 int MQTTPacket_equals(MQTTString* a, char* b);
samdanbury 6:37b6d0d56190 89
samdanbury 6:37b6d0d56190 90 int MQTTPacket_encode(unsigned char* buf, int length);
samdanbury 6:37b6d0d56190 91 int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value);
samdanbury 6:37b6d0d56190 92 int MQTTPacket_decodeBuf(unsigned char* buf, int* value);
samdanbury 6:37b6d0d56190 93
samdanbury 6:37b6d0d56190 94 int readInt(unsigned char** pptr);
samdanbury 6:37b6d0d56190 95 char readChar(unsigned char** pptr);
samdanbury 6:37b6d0d56190 96 void writeChar(unsigned char** pptr, char c);
samdanbury 6:37b6d0d56190 97 void writeInt(unsigned char** pptr, int anInt);
samdanbury 6:37b6d0d56190 98 int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata);
samdanbury 6:37b6d0d56190 99 void writeCString(unsigned char** pptr, const char* string);
samdanbury 6:37b6d0d56190 100 void writeMQTTString(unsigned char** pptr, MQTTString mqttstring);
samdanbury 6:37b6d0d56190 101
samdanbury 6:37b6d0d56190 102 int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int));
samdanbury 6:37b6d0d56190 103
samdanbury 6:37b6d0d56190 104 #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
samdanbury 6:37b6d0d56190 105 }
samdanbury 6:37b6d0d56190 106 #endif
samdanbury 6:37b6d0d56190 107
samdanbury 6:37b6d0d56190 108
samdanbury 6:37b6d0d56190 109 #endif /* MQTTPACKET_H_ */