-
Notifications
You must be signed in to change notification settings - Fork 0
/
MQTT.cpp
550 lines (474 loc) · 17.8 KB
/
MQTT.cpp
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#include "MQTT.h"
#define LOGGING
#define MQTTQOS0_HEADER_MASK (0 << 1)
#define MQTTQOS1_HEADER_MASK (1 << 1)
#define MQTTQOS2_HEADER_MASK (2 << 1)
#define DUP_FLAG_OFF_MASK (0<<3)
#define DUP_FLAG_ON_MASK (1<<3)
MQTT::MQTT(char* domain, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int),
bool thread) {
this->initialize(domain, NULL, port, MQTT_DEFAULT_KEEPALIVE, MQTT_MAX_PACKET_SIZE, callback, thread);
}
MQTT::MQTT(char* domain, uint16_t port, int maxpacketsize, void (*callback)(char*,uint8_t*,unsigned int),
bool thread) {
this->initialize(domain, NULL, port, MQTT_DEFAULT_KEEPALIVE, maxpacketsize, callback, thread);
}
MQTT::MQTT(uint8_t *ip, uint16_t port, void (*callback)(char*,uint8_t*,unsigned int),
bool thread) {
this->initialize(NULL, ip, port, MQTT_DEFAULT_KEEPALIVE, MQTT_MAX_PACKET_SIZE, callback, thread);
}
MQTT::MQTT(uint8_t *ip, uint16_t port, int maxpacketsize, void (*callback)(char*,uint8_t*,unsigned int),
bool thread) {
this->initialize(NULL, ip, port, MQTT_DEFAULT_KEEPALIVE, maxpacketsize, callback, thread);
}
MQTT::MQTT(char* domain, uint16_t port, int maxpacketsize, int keepalive, void (*callback)(char*,uint8_t*,unsigned int),
bool thread) {
this->initialize(domain, NULL, port, keepalive, maxpacketsize, callback, thread);
}
MQTT::MQTT(uint8_t *ip, uint16_t port, int maxpacketsize, int keepalive, void (*callback)(char*,uint8_t*,unsigned int),
bool thread) {
this->initialize(NULL, ip, port, keepalive, maxpacketsize, callback, thread);
}
MQTT::~MQTT() {
if (isConnected()) {
disconnect();
}
if (buffer != NULL)
delete[] buffer;
}
void MQTT::initialize(char* domain, uint8_t *ip, uint16_t port, int keepalive, int maxpacketsize,
void (*callback)(char*,uint8_t*,unsigned int), bool thread) {
if (thread) {
this->thread = true;
os_mutex_create(&mutex_lock);
}
this->callback = callback;
this->qoscallback = NULL;
if (ip != NULL)
this->ip = ip;
if (domain != NULL)
this->domain = domain;
this->port = port;
this->keepalive = keepalive;
// if maxpacketsize is over MQTT_MAX_PACKET_SIZE.
this->maxpacketsize = (maxpacketsize <= MQTT_MAX_PACKET_SIZE ? MQTT_MAX_PACKET_SIZE : maxpacketsize);
if (buffer != NULL)
delete[] buffer;
buffer = new uint8_t[this->maxpacketsize];
}
void MQTT::setBroker(char* domain, uint16_t port) {
if(isConnected()) {
disconnect();
}
this->domain = domain;
this->ip = NULL;
this->port = port;
}
void MQTT::setBroker(uint8_t *ip, uint16_t port) {
if(isConnected()) {
disconnect();
}
this->domain = "";
this->ip = ip;
this->port = port;
}
void MQTT::addQosCallback(void (*qoscallback)(unsigned int)) {
this->qoscallback = qoscallback;
}
bool MQTT::connect(const char *id) {
return connect(id, NULL, NULL, 0, QOS0, 0, 0, true);
}
bool MQTT::connect(const char *id, const char *user, const char *pass) {
return connect(id, user, pass, 0, QOS0, 0, 0, true);
}
bool MQTT::connect(const char *id, const char *user, const char *pass, const char* willTopic, EMQTT_QOS willQos, uint8_t willRetain, const char* willMessage, bool cleanSession, MQTT_VERSION version) {
if (!isConnected()) {
MutexLocker lock(this);
int result = 0;
if (ip == NULL)
result = _client.connect(this->domain.c_str(), this->port);
else
result = _client.connect(this->ip, this->port);
if (result) {
nextMsgId = 1;
uint16_t length = 5;
if (version == MQTT_V311) {
const uint8_t MQTT_HEADER_V311[] = {0x00,0x04,'M','Q','T','T',MQTT_V311};
memcpy(buffer + length, MQTT_HEADER_V311, sizeof(MQTT_HEADER_V311));
length+=sizeof(MQTT_HEADER_V311);
} else {
const uint8_t MQTT_HEADER_V31[] = {0x00,0x06,'M','Q','I','s','d','p', MQTT_V31};
memcpy(buffer + length, MQTT_HEADER_V31, sizeof(MQTT_HEADER_V31));
length+=sizeof(MQTT_HEADER_V31);
}
uint8_t v = 0;
if (willTopic) {
v = 0x06|(willQos<<3)|(willRetain<<5);
} else {
v = 0x02;
}
if (!cleanSession) {
v = v&0xfd;
}
if(user != NULL) {
v = v|0x80;
if(pass != NULL) {
v = v|(0x80>>1);
}
}
buffer[length++] = v;
buffer[length++] = ((this->keepalive) >> 8);
buffer[length++] = ((this->keepalive) & 0xFF);
length = writeString(id, buffer, length);
if (willTopic) {
length = writeString(willTopic, buffer, length);
length = writeString(willMessage, buffer, length);
}
if(user != NULL) {
length = writeString(user,buffer,length);
if(pass != NULL) {
length = writeString(pass,buffer,length);
}
}
write(MQTTCONNECT, buffer, length-5);
lastInActivity = lastOutActivity = millis();
while (!_client.available()) {
unsigned long t = millis();
if (t-lastInActivity > this->keepalive*1000UL) {
_client.stop();
return false;
}
}
uint8_t llen;
uint16_t len = readPacket(&llen);
if (len == 4) {
if (buffer[3] == CONN_ACCEPT) {
lastInActivity = millis();
pingOutstanding = false;
debug_print(" Connect success\n");
return true;
} else {
// check EMQTT_CONNACK_RESPONSE code.
debug_print(" Connect fail. code = [%d]\n", buffer[3]);
}
}
}
_client.stop();
}
return false;
}
uint8_t MQTT::readByte() {
while(!_client.available()) {}
return _client.read();
}
uint16_t MQTT::readPacket(uint8_t* lengthLength) {
uint16_t len = 0;
buffer[len++] = readByte();
bool isPublish = (buffer[0]&0xF0) == MQTTPUBLISH;
uint32_t multiplier = 1;
uint16_t length = 0;
uint8_t digit = 0;
uint16_t skip = 0;
uint8_t start = 0;
do {
digit = readByte();
buffer[len++] = digit;
length += (digit & 127) * multiplier;
multiplier *= 128;
} while ((digit & 128) != 0);
*lengthLength = len-1;
if (isPublish) {
// Read in topic length to calculate bytes to skip over for Stream writing
buffer[len++] = readByte();
buffer[len++] = readByte();
skip = (buffer[*lengthLength+1]<<8)+buffer[*lengthLength+2];
start = 2;
if (buffer[0] & MQTTQOS1_HEADER_MASK) {
// skip message id
skip += 2;
}
}
for (uint16_t i = start;i<length;i++) {
digit = readByte();
if (len < this->maxpacketsize) {
buffer[len] = digit;
}
len++;
}
if (len > this->maxpacketsize) {
len = 0; // This will cause the packet to be ignored.
}
return len;
}
bool MQTT::loop() {
if (isConnected()) {
MutexLocker lock(this);
unsigned long t = millis();
if ((t - lastInActivity > this->keepalive*1000UL) || (t - lastOutActivity > this->keepalive*1000UL)) {
if (pingOutstanding) {
_client.stop();
return false;
} else {
buffer[0] = MQTTPINGREQ;
buffer[1] = 0;
_client.write(buffer,2);
lastOutActivity = t;
lastInActivity = t;
pingOutstanding = true;
}
}
if (_client.available()) {
uint8_t llen;
uint16_t len = readPacket(&llen);
uint16_t msgId = 0;
uint8_t *payload;
if (len > 0) {
lastInActivity = t;
uint8_t type = buffer[0]&0xF0;
if (type == MQTTPUBLISH) {
if (callback) {
uint16_t tl = (buffer[llen+1]<<8)+buffer[llen+2]; // topic length
char topic[tl+1];
for (uint16_t i=0;i<tl;i++) {
topic[i] = buffer[llen+3+i];
}
topic[tl] = 0;
// msgId only present for QOS>0
if ((buffer[0]&0x06) == MQTTQOS1_HEADER_MASK) { // QoS=1
msgId = (buffer[llen+3+tl]<<8)+buffer[llen+3+tl+1];
payload = buffer+llen+3+tl+2;
callback(topic,payload,len-llen-3-tl-2);
buffer[0] = MQTTPUBACK; // respond with PUBACK
buffer[1] = 2;
buffer[2] = (msgId >> 8);
buffer[3] = (msgId & 0xFF);
_client.write(buffer,4);
lastOutActivity = t;
} else if ((buffer[0] & 0x06) == MQTTQOS2_HEADER_MASK) { // QoS=2
msgId = (buffer[llen + 3 + tl] << 8) + buffer[llen + 3 + tl + 1];
payload = buffer + llen + 3 + tl + 2;
callback(topic, payload, len - llen - 3 - tl - 2);
buffer[0] = MQTTPUBREC; // respond with PUBREC
buffer[1] = 2;
buffer[2] = (msgId >> 8);
buffer[3] = (msgId & 0xFF);
_client.write(buffer, 4);
lastOutActivity = t;
} else {
payload = buffer+llen+3+tl;
callback(topic,payload,len-llen-3-tl);
}
}
} else if (type == MQTTPUBREC) {
// check for the situation that QoS2 receive PUBREC, should return PUBREL
msgId = (buffer[2] << 8) + buffer[3];
this->publishRelease(msgId);
} else if (type == MQTTPUBACK) {
if (qoscallback) {
// this case QOS==1
if (len == 4 && (buffer[0]&0x06) == MQTTQOS0_HEADER_MASK) {
msgId = (buffer[2]<<8)+buffer[3];
this->qoscallback(msgId);
}
}
} else if (type == MQTTPUBREL) {
msgId = (buffer[2] << 8) + buffer[3];
this->publishComplete(msgId);
} else if (type == MQTTPUBCOMP) {
if (qoscallback) {
// msgId only present for QOS==0
if (len == 4 && (buffer[0]&0x06) == MQTTQOS0_HEADER_MASK) {
msgId = (buffer[2]<<8)+buffer[3];
this->qoscallback(msgId);
}
}
} else if (type == MQTTSUBACK) {
// if something...
} else if (type == MQTTPINGREQ) {
buffer[0] = MQTTPINGRESP;
buffer[1] = 0;
_client.write(buffer,2);
} else if (type == MQTTPINGRESP) {
pingOutstanding = false;
}
}
}
return true;
}
return false;
}
bool MQTT::publish(const char* topic, const char* payload) {
return publish(topic, (uint8_t*)payload, strlen(payload), false, QOS0, NULL);
}
bool MQTT::publish(const char* topic, const char* payload, bool retain) {
return publish(topic, (uint8_t*)payload, strlen(payload), retain, QOS0, NULL);
}
bool MQTT::publish(const char * topic, const char* payload, EMQTT_QOS qos, bool dup, uint16_t *messageid) {
return publish(topic, (uint8_t*)payload, strlen(payload), false, qos, dup, messageid);
}
bool MQTT::publish(const char * topic, const char* payload, EMQTT_QOS qos, uint16_t *messageid) {
return publish(topic, (uint8_t*)payload, strlen(payload), false, qos, messageid);
}
bool MQTT::publish(const char* topic, const uint8_t* payload, unsigned int plength) {
return publish(topic, payload, plength, false, QOS0, NULL);
}
bool MQTT::publish(const char* topic, const uint8_t* payload, unsigned int plength, EMQTT_QOS qos, bool dup, uint16_t *messageid) {
return publish(topic, payload, plength, false, qos, dup, messageid);
}
bool MQTT::publish(const char* topic, const uint8_t* payload, unsigned int plength, EMQTT_QOS qos, uint16_t *messageid) {
return publish(topic, payload, plength, false, qos, messageid);
}
bool MQTT::publish(const char* topic, const uint8_t* payload, unsigned int plength, bool retain) {
return publish(topic, payload, plength, retain, QOS0, NULL);
}
bool MQTT::publish(const char* topic, const uint8_t* payload, unsigned int plength, bool retain, EMQTT_QOS qos, uint16_t *messageid) {
return publish(topic, payload, plength, retain, qos, false, messageid);
}
bool MQTT::publish(const char* topic, const uint8_t* payload, unsigned int plength, bool retain, EMQTT_QOS qos, bool dup, uint16_t *messageid) {
if (isConnected()) {
MutexLocker lock(this);
// Leave room in the buffer for header and variable length field
uint16_t length = 5;
memset(buffer, 0, this->maxpacketsize);
length = writeString(topic, buffer, length);
if (qos == QOS2 || qos == QOS1) {
nextMsgId += 1;
buffer[length++] = (nextMsgId >> 8);
buffer[length++] = (nextMsgId & 0xFF);
if (messageid != NULL)
*messageid = nextMsgId++;
}
for (uint16_t i=0; i < plength && length < this->maxpacketsize; i++) {
buffer[length++] = payload[i];
}
uint8_t header = MQTTPUBLISH;
if (retain) {
header |= 1;
}
if (dup) {
header |= DUP_FLAG_ON_MASK;
}
if (qos == QOS2)
header |= MQTTQOS2_HEADER_MASK;
else if (qos == QOS1)
header |= MQTTQOS1_HEADER_MASK;
else
header |= MQTTQOS0_HEADER_MASK;
return write(header, buffer, length-5);
}
return false;
}
bool MQTT::publishRelease(uint16_t messageid) {
if (isConnected()) {
MutexLocker lock(this);
uint16_t length = 0;
// reserved bits in MQTT v3.1.1
buffer[length++] = MQTTPUBREL | MQTTQOS1_HEADER_MASK;
buffer[length++] = 2;
buffer[length++] = (messageid >> 8);
buffer[length++] = (messageid & 0xFF);
return _client.write(buffer, length);
}
return false;
}
bool MQTT::publishComplete(uint16_t messageid) {
if (isConnected()) {
MutexLocker lock(this);
uint16_t length = 0;
// reserved bits in MQTT v3.1.1
buffer[length++] = MQTTPUBCOMP | MQTTQOS1_HEADER_MASK;
buffer[length++] = 2;
buffer[length++] = (messageid >> 8);
buffer[length++] = (messageid & 0xFF);
return _client.write(buffer, length);
}
return false;
}
bool MQTT::write(uint8_t header, uint8_t* buf, uint16_t length) {
uint8_t lenBuf[4];
uint8_t llen = 0;
uint8_t digit;
uint8_t pos = 0;
uint16_t rc;
uint16_t len = length;
do {
digit = len % 128;
len = len / 128;
if (len > 0) {
digit |= 0x80;
}
lenBuf[pos++] = digit;
llen++;
} while(len > 0);
buf[4-llen] = header;
for (int i = 0; i < llen; i++) {
buf[5-llen+i] = lenBuf[i];
}
rc = _client.write(buf+(4-llen), length+1+llen);
lastOutActivity = millis();
return (rc == 1+llen+length);
}
bool MQTT::subscribe(const char* topic) {
return subscribe(topic, QOS0);
}
bool MQTT::subscribe(const char* topic, EMQTT_QOS qos) {
if (isConnected()) {
// Leave room in the buffer for header and variable length field
MutexLocker lock(this);
uint16_t length = 5;
nextMsgId++;
if (nextMsgId == 0) {
nextMsgId = 1;
}
buffer[length++] = (nextMsgId >> 8);
buffer[length++] = (nextMsgId & 0xFF);
length = writeString(topic, buffer,length);
buffer[length++] = qos;
return write(MQTTSUBSCRIBE | MQTTQOS1_HEADER_MASK,buffer,length-5);
}
return false;
}
bool MQTT::unsubscribe(const char* topic) {
if (isConnected()) {
MutexLocker lock(this);
uint16_t length = 5;
nextMsgId++;
if (nextMsgId == 0) {
nextMsgId = 1;
}
buffer[length++] = (nextMsgId >> 8);
buffer[length++] = (nextMsgId & 0xFF);
length = writeString(topic, buffer,length);
return write(MQTTUNSUBSCRIBE | MQTTQOS1_HEADER_MASK,buffer,length-5);
}
return false;
}
void MQTT::disconnect() {
MutexLocker lock(this);
buffer[0] = MQTTDISCONNECT;
buffer[1] = 0;
_client.write(buffer,2);
_client.stop();
lastInActivity = lastOutActivity = millis();
}
uint16_t MQTT::writeString(const char* string, uint8_t* buf, uint16_t pos) {
const char* idp = string;
uint16_t i = 0;
pos += 2;
while (*idp && pos < this->maxpacketsize) {
buf[pos++] = *idp++;
i++;
}
buf[pos-i-2] = (i >> 8);
buf[pos-i-1] = (i & 0xFF);
return pos;
}
bool MQTT::isConnected() {
bool rc = (int)_client.connected();
if (!rc) _client.stop();
return rc;
}
void MQTT::clear() {
_client.stop();
lastInActivity = lastOutActivity = millis();
}