Local fixes

Committer:
hudakz
Date:
Thu Sep 12 20:26:40 2019 +0000
Revision:
4:8620de6d1696
Parent:
3:5e31b4687aad
Child:
5:361a6987739b
Modified.

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 2:ca17c5d846e7 5
hudakz 3:5e31b4687aad 6 Ported to mbed by Zoltan Hudak <hudakz@outlook.com>
hudakz 0:83c732b10e95 7 */
hudakz 0:83c732b10e95 8 #ifndef MQTTClient_h
hudakz 0:83c732b10e95 9 #define MQTTClient_h
hudakz 0:83c732b10e95 10
hudakz 3:5e31b4687aad 11 #include "mbed.h"
hudakz 3:5e31b4687aad 12 #include "TcpClient.h"
hudakz 0:83c732b10e95 13
hudakz 4:8620de6d1696 14 #define MQTT_MAX_PACKET_SIZE 256 // MQTT_MAX_PACKET_SIZE : Maximum packet size
hudakz 4:8620de6d1696 15 #define MQTT_KEEPALIVE 15 // MQTT_KEEPALIVE : keepAlive interval in Seconds
hudakz 4:8620de6d1696 16 #define MQTTPROTOCOLVERSION 3
hudakz 4:8620de6d1696 17 #define MQTTCONNECT 1 << 4 // Client request to connect to Server
hudakz 4:8620de6d1696 18 #define MQTTCONNACK 2 << 4 // Connect Acknowledgment
hudakz 4:8620de6d1696 19 #define MQTTPUBLISH 3 << 4 // Publish message
hudakz 4:8620de6d1696 20 #define MQTTPUBACK 4 << 4 // Publish Acknowledgment
hudakz 4:8620de6d1696 21 #define MQTTPUBREC 5 << 4 // Publish Received (assured delivery part 1)
hudakz 4:8620de6d1696 22 #define MQTTPUBREL 6 << 4 // Publish Release (assured delivery part 2)
hudakz 4:8620de6d1696 23 #define MQTTPUBCOMP 7 << 4 // Publish Complete (assured delivery part 3)
hudakz 4:8620de6d1696 24 #define MQTTSUBSCRIBE 8 << 4 // Client Subscribe request
hudakz 4:8620de6d1696 25 #define MQTTSUBACK 9 << 4 // Subscribe Acknowledgment
hudakz 4:8620de6d1696 26 #define MQTTUNSUBSCRIBE 10 << 4 // Client Unsubscribe request
hudakz 4:8620de6d1696 27 #define MQTTUNSUBACK 11 << 4 // Unsubscribe Acknowledgment
hudakz 4:8620de6d1696 28 #define MQTTPINGREQ 12 << 4 // PING Request
hudakz 4:8620de6d1696 29 #define MQTTPINGRESP 13 << 4 // PING Response
hudakz 4:8620de6d1696 30 #define MQTTDISCONNECT 14 << 4 // Client is Disconnecting
hudakz 4:8620de6d1696 31 #define MQTTReserved 15 << 4 // Reserved
hudakz 4:8620de6d1696 32 #define MQTTQOS0 (0<< 1)
hudakz 4:8620de6d1696 33 #define MQTTQOS1 (1<< 1)
hudakz 4:8620de6d1696 34 #define MQTTQOS2 (2<< 1)
hudakz 0:83c732b10e95 35
hudakz 4:8620de6d1696 36 class MQTTClient
hudakz 0:83c732b10e95 37 {
hudakz 4:8620de6d1696 38 TcpClient _client;
hudakz 4:8620de6d1696 39 uint8_t _buffer[MQTT_MAX_PACKET_SIZE];
hudakz 4:8620de6d1696 40 uint16_t _nextMsgId;
hudakz 4:8620de6d1696 41 time_t _lastOutActivity;
hudakz 4:8620de6d1696 42 time_t _lastInActivity;
hudakz 4:8620de6d1696 43 bool _pingOutstanding;
hudakz 4:8620de6d1696 44 IpAddress _ip;
hudakz 4:8620de6d1696 45 char* _domain;
hudakz 4:8620de6d1696 46 uint16_t _port;
hudakz 4:8620de6d1696 47 Stream* _stream;
hudakz 3:5e31b4687aad 48
hudakz 4:8620de6d1696 49 uint16_t _readPacket(uint8_t* );
hudakz 4:8620de6d1696 50 uint8_t _readByte();
hudakz 4:8620de6d1696 51 bool _write(uint8_t header, uint8_t* buf, uint16_t length);
hudakz 4:8620de6d1696 52 uint16_t _writeString(const char* string, uint8_t* buf, uint16_t length);
hudakz 4:8620de6d1696 53
hudakz 4:8620de6d1696 54 Callback<void (char *, uint8_t *, uint16_t)> _onMessage;
hudakz 0:83c732b10e95 55 public:
hudakz 4:8620de6d1696 56 MQTTClient();
hudakz 4:8620de6d1696 57 MQTTClient(IpAddress& , uint16_t, Callback<void (char*, uint8_t*, uint16_t)> );
hudakz 4:8620de6d1696 58 MQTTClient(IpAddress& , uint16_t, Callback<void (char*, uint8_t*, uint16_t)> , Stream& );
hudakz 4:8620de6d1696 59 MQTTClient(const char* , uint16_t, Callback<void (char*, uint8_t*, uint16_t)> );
hudakz 4:8620de6d1696 60 MQTTClient(const char* , uint16_t, Callback<void (char*, uint8_t*, uint16_t)> , Stream& );
hudakz 3:5e31b4687aad 61 bool connect(const char* );
hudakz 3:5e31b4687aad 62 bool connect(const char* , const char* , const char* );
hudakz 3:5e31b4687aad 63 bool connect(const char* , const char* , uint8_t, uint8_t, const char* );
hudakz 3:5e31b4687aad 64 bool connect(const char* , const char* , const char* , const char* , uint8_t, uint8_t, const char* );
hudakz 4:8620de6d1696 65 void disconnect();
hudakz 3:5e31b4687aad 66 bool publish(const char* , const char* );
hudakz 4:8620de6d1696 67 bool publish(const char* , uint8_t* , uint16_t);
hudakz 4:8620de6d1696 68 bool publish(const char* , uint8_t* , uint16_t, bool);
hudakz 3:5e31b4687aad 69 bool subscribe(const char* );
hudakz 3:5e31b4687aad 70 bool subscribe(const char* , uint8_t qos);
hudakz 3:5e31b4687aad 71 bool unsubscribe(const char* );
hudakz 4:8620de6d1696 72 bool process();
hudakz 4:8620de6d1696 73 bool connected();
hudakz 0:83c732b10e95 74 };
hudakz 0:83c732b10e95 75 #endif