123

Committer:
hudakz
Date:
Mon Sep 15 12:43:54 2014 +0000
Revision:
0:83c732b10e95
Child:
1:87da395325fc
rev. 00

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:83c732b10e95 1 /*
hudakz 0:83c732b10e95 2 MQTTClient.h - A simple client for MQTT.
hudakz 0:83c732b10e95 3 Nicholas O'Leary
hudakz 0:83c732b10e95 4 http://knolleary.net
hudakz 0:83c732b10e95 5 */
hudakz 0:83c732b10e95 6
hudakz 0:83c732b10e95 7 #ifndef MQTTClient_h
hudakz 0:83c732b10e95 8 #define MQTTClient_h
hudakz 0:83c732b10e95 9
hudakz 0:83c732b10e95 10 #include <mbed.h>
hudakz 0:83c732b10e95 11 #include "Client.h"
hudakz 0:83c732b10e95 12 //#include "Stream.h"
hudakz 0:83c732b10e95 13
hudakz 0:83c732b10e95 14 // MQTT_MAX_PACKET_SIZE : Maximum packet size
hudakz 0:83c732b10e95 15 #define MQTT_MAX_PACKET_SIZE 128
hudakz 0:83c732b10e95 16
hudakz 0:83c732b10e95 17 // MQTT_KEEPALIVE : keepAlive interval in Seconds
hudakz 0:83c732b10e95 18 #define MQTT_KEEPALIVE 15
hudakz 0:83c732b10e95 19
hudakz 0:83c732b10e95 20 #define MQTTPROTOCOLVERSION 3
hudakz 0:83c732b10e95 21 #define MQTTCONNECT 1 << 4 // Client request to connect to Server
hudakz 0:83c732b10e95 22 #define MQTTCONNACK 2 << 4 // Connect Acknowledgment
hudakz 0:83c732b10e95 23 #define MQTTPUBLISH 3 << 4 // Publish message
hudakz 0:83c732b10e95 24 #define MQTTPUBACK 4 << 4 // Publish Acknowledgment
hudakz 0:83c732b10e95 25 #define MQTTPUBREC 5 << 4 // Publish Received (assured delivery part 1)
hudakz 0:83c732b10e95 26 #define MQTTPUBREL 6 << 4 // Publish Release (assured delivery part 2)
hudakz 0:83c732b10e95 27 #define MQTTPUBCOMP 7 << 4 // Publish Complete (assured delivery part 3)
hudakz 0:83c732b10e95 28 #define MQTTSUBSCRIBE 8 << 4 // Client Subscribe request
hudakz 0:83c732b10e95 29 #define MQTTSUBACK 9 << 4 // Subscribe Acknowledgment
hudakz 0:83c732b10e95 30 #define MQTTUNSUBSCRIBE 10 << 4 // Client Unsubscribe request
hudakz 0:83c732b10e95 31 #define MQTTUNSUBACK 11 << 4 // Unsubscribe Acknowledgment
hudakz 0:83c732b10e95 32 #define MQTTPINGREQ 12 << 4 // PING Request
hudakz 0:83c732b10e95 33 #define MQTTPINGRESP 13 << 4 // PING Response
hudakz 0:83c732b10e95 34 #define MQTTDISCONNECT 14 << 4 // Client is Disconnecting
hudakz 0:83c732b10e95 35 #define MQTTReserved 15 << 4 // Reserved
hudakz 0:83c732b10e95 36
hudakz 0:83c732b10e95 37 #define MQTTQOS0 (0 << 1)
hudakz 0:83c732b10e95 38 #define MQTTQOS1 (1 << 1)
hudakz 0:83c732b10e95 39 #define MQTTQOS2 (2 << 1)
hudakz 0:83c732b10e95 40
hudakz 0:83c732b10e95 41 class MQTTClient
hudakz 0:83c732b10e95 42 {
hudakz 0:83c732b10e95 43 private:
hudakz 0:83c732b10e95 44 Client* _client;
hudakz 0:83c732b10e95 45 uint8_t buffer[MQTT_MAX_PACKET_SIZE];
hudakz 0:83c732b10e95 46 uint16_t nextMsgId;
hudakz 0:83c732b10e95 47 time_t lastOutActivity;
hudakz 0:83c732b10e95 48 time_t lastInActivity;
hudakz 0:83c732b10e95 49 bool pingOutstanding;
hudakz 0:83c732b10e95 50 void (*onMessage)(char*,uint8_t*,unsigned int);
hudakz 0:83c732b10e95 51 uint16_t readPacket(uint8_t*);
hudakz 0:83c732b10e95 52 uint8_t readByte();
hudakz 0:83c732b10e95 53 bool write(uint8_t header, uint8_t* buf, uint16_t length);
hudakz 0:83c732b10e95 54 uint16_t writeString(char* string, uint8_t* buf, uint16_t pos);
hudakz 0:83c732b10e95 55 IPAddress ip;
hudakz 0:83c732b10e95 56 char* domain;
hudakz 0:83c732b10e95 57 uint16_t port;
hudakz 0:83c732b10e95 58 Stream* stream;
hudakz 0:83c732b10e95 59 public:
hudakz 0:83c732b10e95 60 MQTTClient();
hudakz 0:83c732b10e95 61 MQTTClient(IPAddress&, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client);
hudakz 0:83c732b10e95 62 MQTTClient(IPAddress&, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client, Stream&);
hudakz 0:83c732b10e95 63 MQTTClient(char*, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client);
hudakz 0:83c732b10e95 64 MQTTClient(char*, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client, Stream&);
hudakz 0:83c732b10e95 65 bool connect(char*);
hudakz 0:83c732b10e95 66 bool connect(char*, char*, char*);
hudakz 0:83c732b10e95 67 bool connect(char*, char*, uint8_t, uint8_t, char*);
hudakz 0:83c732b10e95 68 bool connect(char*, char*, char*, char*, uint8_t, uint8_t, char*);
hudakz 0:83c732b10e95 69 void disconnect();
hudakz 0:83c732b10e95 70 bool publish(char*, char*);
hudakz 0:83c732b10e95 71 bool publish(char*, uint8_t*, unsigned int);
hudakz 0:83c732b10e95 72 bool publish(char*, uint8_t*, unsigned int, bool);
hudakz 0:83c732b10e95 73 bool subscribe(char*);
hudakz 0:83c732b10e95 74 bool subscribe(char*, uint8_t qos);
hudakz 0:83c732b10e95 75 bool unsubscribe(char*);
hudakz 0:83c732b10e95 76 bool loop();
hudakz 0:83c732b10e95 77 bool connected();
hudakz 0:83c732b10e95 78 };
hudakz 0:83c732b10e95 79
hudakz 0:83c732b10e95 80
hudakz 0:83c732b10e95 81 #endif