-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathMqttClient.h
173 lines (140 loc) · 5.03 KB
/
MqttClient.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
This file is part of the ArduinoMqttClient library.
Copyright (c) 2019 Arduino SA. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _MQTT_CLIENT_H_
#define _MQTT_CLIENT_H_
#include <Arduino.h>
#include <Client.h>
#define MQTT_CONNECTION_REFUSED -2
#define MQTT_CONNECTION_TIMEOUT -1
#define MQTT_SUCCESS 0
#define MQTT_UNACCEPTABLE_PROTOCOL_VERSION 1
#define MQTT_IDENTIFIER_REJECTED 2
#define MQTT_SERVER_UNAVAILABLE 3
#define MQTT_BAD_USER_NAME_OR_PASSWORD 4
#define MQTT_NOT_AUTHORIZED 5
class MqttClient : public Client {
public:
MqttClient(Client& client);
virtual ~MqttClient();
void onMessage(void(*)(int));
int parseMessage();
String messageTopic() const;
int messageDup() const;
int messageQoS() const;
int messageRetain() const;
int beginMessage(const char* topic, unsigned long size, bool retain = false, uint8_t qos = 0, bool dup = false);
int beginMessage(const String& topic, unsigned long size, bool retain = false, uint8_t qos = 0, bool dup = false);
int beginMessage(const char* topic, bool retain = false, uint8_t qos = 0, bool dup = false);
int beginMessage(const String& topic, bool retain = false, uint8_t qos = 0, bool dup = false);
int endMessage();
int beginWill(const char* topic, unsigned short size, bool retain, uint8_t qos);
int beginWill(const String& topic, unsigned short size, bool retain, uint8_t qos);
int beginWill(const char* topic, bool retain, uint8_t qos);
int beginWill(const String& topic, bool retain, uint8_t qos);
int endWill();
int subscribe(const char* topic, uint8_t qos = 0);
int subscribe(const String& topic, uint8_t qos = 0);
int unsubscribe(const char* topic);
int unsubscribe(const String& topic);
void poll();
// from Client
virtual int connect(IPAddress ip, uint16_t port = 1883);
virtual int connect(const char *host, uint16_t port = 1883);
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *buf, size_t size);
virtual int available();
virtual int read();
virtual int read(uint8_t *buf, size_t size);
virtual int peek();
virtual void flush();
virtual void stop();
virtual uint8_t connected();
virtual operator bool();
void setId(const char* id);
void setId(const String& id);
void setUsernamePassword(const char* username, const char* password);
void setUsernamePassword(const String& username, const String& password);
void setCleanSession(bool cleanSession);
void setKeepAliveInterval(unsigned long interval);
void setConnectionTimeout(unsigned long timeout);
int connectError() const;
int subscribeQoS() const;
private:
int connect(IPAddress ip, const char* host, uint16_t port);
int publishHeader(size_t length);
void puback(uint16_t id);
void pubrec(uint16_t id);
void pubrel(uint16_t id);
void pubcomp(uint16_t id);
void ping();
void disconnect();
int beginPacket(uint8_t type, uint8_t flags, size_t length, uint8_t* buffer);
int writeString(const char* s, uint16_t length);
int write8(uint8_t val);
int write16(uint16_t val);
int writeData(const void* data, size_t length);
int endPacket();
void ackRxMessage();
uint8_t clientConnected();
int clientAvailable();
int clientRead();
int clientTimedRead();
int clientPeek();
size_t clientWrite(const uint8_t *buf, size_t size);
private:
Client* _client;
void (*_onMessage)(int);
String _id;
String _username;
String _password;
bool _cleanSession;
unsigned long _keepAliveInterval;
unsigned long _connectionTimeout;
int _connectError;
bool _connected;
int _subscribeQos;
int _rxState;
uint8_t _rxType;
uint8_t _rxFlags;
size_t _rxLength;
uint32_t _rxLengthMultiplier;
int _returnCode;
String _rxMessageTopic;
size_t _rxMessageTopicLength;
bool _rxMessageDup;
uint8_t _rxMessageQoS;
bool _rxMessageRetain;
uint16_t _rxPacketId;
uint8_t _rxMessageBuffer[3];
size_t _rxMessageIndex;
unsigned long _lastRx;
String _txMessageTopic;
bool _txMessageRetain;
uint8_t _txMessageQoS;
bool _txMessageDup;
uint16_t _txPacketId;
uint8_t* _txBuffer;
size_t _txBufferIndex;
bool _txStreamPayload;
uint8_t* _txPayloadBuffer;
size_t _txPayloadBufferIndex;
unsigned long _lastPingTx;
uint8_t* _willBuffer;
uint16_t _willBufferIndex;
size_t _willMessageIndex;
uint8_t _willFlags;
};
#endif