123

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