forked from knolleary/pubsubclient
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathPubSubClient.cpp
More file actions
340 lines (274 loc) · 7.01 KB
/
Copy pathPubSubClient.cpp
File metadata and controls
340 lines (274 loc) · 7.01 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
PubSubClient.cpp - A simple client for MQTT.
Nicholas O'Leary
http://knolleary.net
*/
#include "PubSubClient.h"
#include <string.h>
PubSubClient::PubSubClient(){}
PubSubClient::PubSubClient(Client& c) :
_callback(NULL),
_client(&c),
_max_retries(10)
{}
PubSubClient::PubSubClient(Client& c, IPAddress &ip, uint16_t port) :
_callback(NULL),
_client(&c),
_max_retries(10),
server_ip(ip),
server_port(port)
{}
PubSubClient::PubSubClient(Client& c, String hostname, uint16_t port) :
_callback(NULL),
_client(&c),
_max_retries(10),
server_port(port),
server_hostname(hostname)
{}
PubSubClient& PubSubClient::set_server(IPAddress &ip, uint16_t port) {
server_hostname = "";
server_ip = ip;
server_port = port;
return *this;
}
PubSubClient& PubSubClient::set_server(String hostname, uint16_t port) {
server_hostname = hostname;
server_port = port;
return *this;
}
MQTT::Message* PubSubClient::_recv_message(void) {
MQTT::Message *msg = MQTT::readPacket(*_client);
if (msg != NULL)
lastInActivity = millis();
return msg;
}
bool PubSubClient::_send_message(MQTT::Message& msg, bool need_reply) {
MQTT::message_type r_type = msg.response_type();
if (msg.need_packet_id())
msg.set_packet_id(_next_packet_id());
uint8_t retries = 0;
send:
if (!msg.send(*_client)) {
if (retries < _max_retries) {
retries++;
goto send;
}
return false;
}
lastOutActivity = millis();
if (!need_reply || (r_type == MQTT::None))
return true;
if (!_wait_for(r_type, msg.packet_id())) {
if (retries < _max_retries) {
retries++;
goto send;
}
return false;
}
return true;
}
void PubSubClient::_process_message(MQTT::Message* msg) {
switch (msg->type()) {
case MQTT::PUBLISH:
{
MQTT::Publish *pub = static_cast<MQTT::Publish*>(msg); // RTTI is disabled on embedded, so no dynamic_cast<>()
if (_callback)
_callback(*pub);
if (pub->qos() == 1) {
MQTT::PublishAck puback(pub->packet_id());
_send_message(puback);
} else if (pub->qos() == 2) {
uint8_t retries = 0;
{
MQTT::PublishRec pubrec(pub->packet_id());
if (!_send_message(pubrec, true))
return;
}
{
MQTT::PublishComp pubcomp(pub->packet_id());
_send_message(pubcomp);
}
}
}
break;
case MQTT::PINGREQ:
{
MQTT::PingResp pr;
_send_message(pr);
}
break;
case MQTT::PINGRESP:
pingOutstanding = false;
}
}
bool PubSubClient::_wait_for(MQTT::message_type match_type, uint16_t match_pid) {
while (!_client->available()) {
if (millis() - lastInActivity > keepalive * 1000UL)
return false;
yield();
}
while (millis() < lastInActivity + (keepalive * 1000)) {
// Read the packet and check it
MQTT::Message *msg = _recv_message();
if (msg != NULL) {
if (msg->type() == match_type) {
uint16_t pid = msg->packet_id();
delete msg;
if (match_pid)
return pid == match_pid;
return true;
}
_process_message(msg);
delete msg;
}
yield();
}
return false;
}
bool PubSubClient::connect(String id) {
return connect(id, "", 0, false, "");
}
bool PubSubClient::connect(String id, String willTopic, uint8_t willQos, bool willRetain, String willMessage) {
MQTT::Connect conn(id);
if (willTopic.length())
conn.set_will(willTopic, willMessage, willQos, willRetain);
return connect(conn);
}
bool PubSubClient::connect(MQTT::Connect &conn) {
if (connected())
return false;
int result = 0;
if (server_hostname.length() > 0)
result = _client->connect(server_hostname.c_str(), server_port);
else
result = _client->connect(server_ip, server_port);
if (!result) {
_client->stop();
return false;
}
pingOutstanding = false;
nextMsgId = 1; // Init the next packet id
lastInActivity = millis(); // Init this so that _wait_for() doesn't think we've already timed-out
keepalive = conn.keepalive(); // Store the keepalive period from this connection
if (!_send_message(conn, true)) {
_client->stop();
return false;
}
return true;
}
bool PubSubClient::loop() {
if (!connected())
return false;
unsigned long t = millis();
if ((t - lastInActivity > keepalive * 1000UL) || (t - lastOutActivity > keepalive * 1000UL)) {
if (pingOutstanding) {
_client->stop();
return false;
} else {
MQTT::Ping ping;
if (!_send_message(ping))
return false;
lastInActivity = lastOutActivity;
pingOutstanding = true;
}
}
if (_client->available()) {
// Read the packet and check it
MQTT::Message *msg = _recv_message();
if (msg != NULL) {
_process_message(msg);
delete msg;
}
}
return true;
}
bool PubSubClient::publish(String topic, String payload) {
if (!connected())
return false;
MQTT::Publish pub(topic, payload);
return publish(pub);
}
bool PubSubClient::publish(String topic, String payload, bool retained) {
if (!connected())
return false;
MQTT::Publish pub(topic, payload);
pub.set_retain(retained);
return publish(pub);
}
bool PubSubClient::publish(String topic, const uint8_t* payload, uint32_t plength, bool retained) {
if (!connected())
return false;
MQTT::Publish pub(topic, const_cast<uint8_t*>(payload), plength);
pub.set_retain(retained);
return publish(pub);
}
bool PubSubClient::publish(String topic, MQTT::payload_callback_t pcb, uint32_t length, bool retained) {
if (!connected())
return false;
MQTT::Publish pub(topic, pcb, length);
pub.set_retain(retained);
return publish(pub);
}
bool PubSubClient::publish_P(String topic, PGM_P payload, uint32_t plength, bool retained) {
if (!connected())
return false;
MQTT::Publish pub = MQTT::Publish_P(topic, payload, plength);
pub.set_retain(retained);
return publish(pub);
}
bool PubSubClient::publish(MQTT::Publish &pub) {
if (!connected())
return false;
switch (pub.qos()) {
case 0:
return _send_message(pub);
case 1:
return _send_message(pub, true);
case 2:
{
if (!_send_message(pub, true))
return false;
MQTT::PublishRel pubrel(pub.packet_id());
return _send_message(pubrel, true);
}
}
return false;
}
bool PubSubClient::subscribe(String topic, uint8_t qos) {
if (!connected())
return false;
if (qos > 2)
return false;
MQTT::Subscribe sub(topic, qos);
return subscribe(sub);
}
bool PubSubClient::subscribe(MQTT::Subscribe &sub) {
if (!connected())
return false;
return _send_message(sub, true);
}
bool PubSubClient::unsubscribe(String topic) {
if (!connected())
return false;
MQTT::Unsubscribe unsub(topic);
return unsubscribe(unsub);
}
bool PubSubClient::unsubscribe(MQTT::Unsubscribe &unsub) {
if (!connected())
return false;
return _send_message(unsub, true);
}
void PubSubClient::disconnect() {
if (!connected())
return;
MQTT::Disconnect discon;
if (_send_message(discon))
lastInActivity = lastOutActivity;
_client->stop();
}
bool PubSubClient::connected() {
bool rc = _client->connected();
if (!rc)
_client->stop();
return rc;
}