Local fixes
MQTTClient.h@7:0f12a3d0bd10, 2020-09-24 (annotated)
- Committer:
- ivo_n
- Date:
- Thu Sep 24 21:19:41 2020 +0000
- Revision:
- 7:0f12a3d0bd10
- Parent:
- 5:361a6987739b
Local Fixes
Who changed what in which revision?
User | Revision | Line number | New 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 | 5:361a6987739b | 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 | 5:361a6987739b | 32 | #define MQTTQOS0 0 << 1 |
hudakz | 5:361a6987739b | 33 | #define MQTTQOS1 1 << 1 |
hudakz | 5:361a6987739b | 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 | 5:361a6987739b | 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 | 5:361a6987739b | 54 | Callback<void (char*, uint8_t*, uint16_t)> _onMessage; |
hudakz | 0:83c732b10e95 | 55 | public: |
hudakz | 4:8620de6d1696 | 56 | MQTTClient(); |
hudakz | 5:361a6987739b | 57 | MQTTClient(IpAddress& ip, uint16_t port); |
hudakz | 5:361a6987739b | 58 | MQTTClient(IpAddress& ip, uint16_t port , Stream& stream); |
hudakz | 5:361a6987739b | 59 | MQTTClient(const char* domain, uint16_t port); |
hudakz | 5:361a6987739b | 60 | MQTTClient(const char* domain, uint16_t port, Stream& stream); |
hudakz | 5:361a6987739b | 61 | bool connect(const char*); |
hudakz | 5:361a6987739b | 62 | bool connect(const char* , const char* , const char*); |
hudakz | 5:361a6987739b | 63 | bool connect(const char* , const char* , uint8_t, uint8_t, const char*); |
hudakz | 5:361a6987739b | 64 | bool connect(const char* , const char* , const char* , const char* , uint8_t, uint8_t, const char*); |
hudakz | 4:8620de6d1696 | 65 | void disconnect(); |
hudakz | 5:361a6987739b | 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 | 5:361a6987739b | 69 | bool subscribe(const char*); |
hudakz | 3:5e31b4687aad | 70 | bool subscribe(const char* , uint8_t qos); |
hudakz | 5:361a6987739b | 71 | bool unsubscribe(const char*); |
hudakz | 5:361a6987739b | 72 | bool poll(); |
hudakz | 4:8620de6d1696 | 73 | bool connected(); |
hudakz | 5:361a6987739b | 74 | void attach(Callback<void (char*, uint8_t*, uint16_t)>); |
hudakz | 5:361a6987739b | 75 | template<typename T, typename M> |
hudakz | 5:361a6987739b | 76 | void attach(T *obj, M method ) { attach(callback(obj, method)); } |
hudakz | 0:83c732b10e95 | 77 | }; |
hudakz | 0:83c732b10e95 | 78 | #endif |