-
Notifications
You must be signed in to change notification settings - Fork 6
/
MQTT.cpp
753 lines (611 loc) · 17.5 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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/*
MQTT.cpp - MQTT packet classes
Copyright (C) 2015 Ian Tester
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
*/
#include "MQTT.h"
namespace MQTT {
//! Write a 16-bit value, big-endian order
void write(uint8_t *buf, uint32_t& bufpos, uint16_t data) {
buf[bufpos++] = data >> 8;
buf[bufpos++] = data & 0xff;
}
//! Write an arbitrary chunk of data, with 16-bit length first
void write(uint8_t *buf, uint32_t& bufpos, uint8_t *data, uint16_t dlen) {
write(buf, bufpos, dlen);
memcpy(buf + bufpos, data, dlen);
bufpos += dlen;
}
//! Write a string, with 16-bit length first
void write(uint8_t *buf, uint32_t& bufpos, String str) {
const char* c = str.c_str();
uint32_t length_pos = bufpos;
bufpos += 2;
uint16_t count = 0;
while (*c) {
buf[bufpos++] = *c++;
count++;
}
write(buf, length_pos, count);
}
void write_bare_payload(uint8_t *buf, uint32_t& bufpos, uint8_t *data, uint32_t dlen) {
memcpy(buf + bufpos, data, dlen);
bufpos += dlen;
}
//! Template function to read from a buffer
template <typename T>
T read(uint8_t *buf, uint32_t& pos);
template <>
uint8_t read<uint8_t>(uint8_t *buf, uint32_t& pos) {
return buf[pos++];
}
template <>
uint16_t read<uint16_t>(uint8_t *buf, uint32_t& pos) {
uint16_t val = buf[pos++] << 8;
val |= buf[pos++];
return val;
}
template <>
String read<String>(uint8_t *buf, uint32_t& pos) {
uint16_t len = read<uint16_t>(buf, pos);
String val;
val.reserve(len);
for (uint16_t i = 0; i < len; i++)
val += (char)read<uint8_t>(buf, pos);
return val;
}
//! Template function to read from a Client object
template <typename T>
T read(Client& client);
template <>
uint8_t read<uint8_t>(Client& client) {
while(!client.available()) {}
return client.read();
}
template <>
uint16_t read<uint16_t>(Client& client) {
uint16_t val = read<uint8_t>(client) << 8;
val |= read<uint8_t>(client);
return val;
}
template <>
String read<String>(Client& client) {
uint16_t len = read<uint16_t>(client);
String val;
val.reserve(len);
for (uint16_t i = 0; i < len; i++)
val += (char)read<uint8_t>(client);
return val;
}
// Message class
uint8_t Message::fixed_header_length(uint32_t rlength) const {
if (rlength < 128)
return 2;
else if (rlength < 16384)
return 3;
else if (rlength < 2097152)
return 4;
else
return 5;
}
void Message::write_fixed_header(uint8_t *buf, uint32_t& bufpos, uint32_t rlength) const {
buf[bufpos] = _type << 4;
switch (_type) {
case PUBLISH:
buf[bufpos] |= _flags & 0x0f;
break;
case PUBREL:
case SUBSCRIBE:
case UNSUBSCRIBE:
buf[bufpos] |= 0x02;
}
bufpos++;
// Remaining length
do {
uint8_t digit = rlength & 0x7f;
rlength >>= 7;
if (rlength)
digit |= 0x80;
buf[bufpos++] = digit;
} while (rlength);
}
void Message::write_packet_id(uint8_t *buf, uint32_t& bufpos) const {
write(buf, bufpos, _packet_id);
}
bool Message::send(Client& client) {
uint32_t variable_header_len = variable_header_length();
uint32_t remaining_length = variable_header_len + payload_length();
uint32_t packet_length = fixed_header_length(remaining_length);
if (_payload_callback == NULL)
packet_length += remaining_length;
else
packet_length += variable_header_len;
uint8_t *packet = new uint8_t[packet_length];
uint32_t pos = 0;
write_fixed_header(packet, pos, remaining_length);
write_variable_header(packet, pos);
write_payload(packet, pos);
uint32_t sent = client.write(const_cast<const uint8_t*>(packet), packet_length);
delete [] packet;
if (sent != packet_length)
return false;
if (_payload_callback != NULL)
return _payload_callback(client);
return true;
}
// Parser
Message* readPacket(Client& client) {
// Read type and flags
uint8_t type = read<uint8_t>(client);
uint8_t flags = type & 0x0f;
type >>= 4;
// Read the remaining length
uint32_t remaining_length = 0;
{
uint8_t lenbuf[4], lenlen = 0;
uint8_t shifter = 0;
uint8_t digit;
do {
digit = read<uint8_t>(client);
lenbuf[lenlen++] = digit;
remaining_length += (digit & 0x7f) << shifter;
shifter += 7;
} while (digit & 0x80);
}
// Read variable header and/or payload
uint8_t *remaining_data = NULL;
if (remaining_length > 0) {
if (remaining_length > 1024) {
switch (type) {
case PUBLISH:
return new Publish(flags, client, remaining_length);
case SUBACK:
return new SubscribeAck(client, remaining_length);
default:
return NULL;
}
}
remaining_data = new uint8_t[remaining_length];
{
uint8_t *read_point = remaining_data;
uint32_t rem = remaining_length;
while (client.available() && rem) {
uint32_t read_size = client.read(read_point, rem);
rem -= read_size;
read_point += read_size;
}
}
}
// Use the type value to return an object of the appropriate class
Message *obj;
switch (type) {
case CONNACK:
obj = new ConnectAck(remaining_data, remaining_length);
break;
case PUBLISH:
obj = new Publish(flags, remaining_data, remaining_length);
break;
case PUBACK:
obj = new PublishAck(remaining_data, remaining_length);
break;
case PUBREC:
obj = new PublishRec(remaining_data, remaining_length);
break;
case PUBREL:
obj = new PublishRel(remaining_data, remaining_length);
break;
case PUBCOMP:
obj = new PublishComp(remaining_data, remaining_length);
break;
case SUBACK:
obj = new SubscribeAck(remaining_data, remaining_length);
break;
case UNSUBACK:
obj = new UnsubscribeAck(remaining_data, remaining_length);
break;
case PINGREQ:
obj = new Ping;
break;
case PINGRESP:
obj = new PingResp;
break;
}
if (remaining_data != NULL)
delete [] remaining_data;
return obj;
}
// Connect class
Connect::Connect(String cid) :
Message(CONNECT),
_clean_session(true),
_clientid(cid),
_will_message(NULL), _will_message_len(0),
_keepalive(MQTT_KEEPALIVE)
{}
Connect& Connect::set_will(String willTopic, String willMessage, uint8_t willQos, bool willRetain) {
_will_topic = willTopic;
_will_qos = willQos;
_will_retain = willRetain;
if (_will_message != NULL)
delete [] _will_message;
_will_message_len = willMessage.length();
_will_message = new uint8_t[_will_message_len];
memcpy(_will_message, willMessage.c_str(), _will_message_len);
return *this;
}
Connect& Connect::set_will(String willTopic, uint8_t *willMessage, uint16_t willMessageLength, uint8_t willQos, bool willRetain) {
_will_topic = willTopic;
_will_qos = willQos;
_will_retain = willRetain;
if (_will_message != NULL)
delete [] _will_message;
_will_message_len = willMessageLength;
_will_message = new uint8_t[_will_message_len];
memcpy(_will_message, willMessage, _will_message_len);
return *this;
}
Connect::~Connect() {
if (_will_message != NULL)
delete [] _will_message;
}
uint32_t Connect::variable_header_length(void) const {
return 10;
}
void Connect::write_variable_header(uint8_t *buf, uint32_t& bufpos) const {
write(buf, bufpos, "MQTT"); // Protocol name
buf[bufpos++] = 4; // Protocol level
buf[bufpos] = 0; // Connect flags
if (_clean_session)
buf[bufpos] |= 0x02;
if (_will_topic.length()) {
buf[bufpos] |= 0x04;
if (_will_qos > 2)
buf[bufpos] |= 2 << 3;
else
buf[bufpos] |= _will_qos << 3;
buf[bufpos] |= _will_retain << 5;
}
if (_username.length()) {
buf[bufpos] |= 0x80;
if (_password.length())
buf[bufpos] |= 0x40;
}
bufpos++;
write(buf, bufpos, _keepalive); // Keepalive period
}
uint32_t Connect::payload_length(void) const {
uint32_t len = 2 + _clientid.length();
if (_will_topic.length()) {
len += 2 + _will_topic.length();
len += 2 + _will_message_len;
}
if (_username.length()) {
len += 2 + _username.length();
if (_password.length())
len += 2 + _password.length();
}
return len;
}
void Connect::write_payload(uint8_t *buf, uint32_t& bufpos) const {
write(buf, bufpos, _clientid);
if (_will_topic.length()) {
write(buf, bufpos, _will_topic);
write(buf, bufpos, _will_message, _will_message_len);
}
if (_username.length()) {
write(buf, bufpos, _username);
if (_password.length())
write(buf, bufpos, _password);
}
}
// ConnectAck class
ConnectAck::ConnectAck(uint8_t* data, uint32_t length) :
Message(CONNACK)
{
uint32_t pos = 0;
uint8_t reserved = read<uint8_t>(data, pos);
_session_present = reserved & 0x01;
_rc = read<uint8_t>(data, pos);
}
// Publish class
Publish::Publish(String topic, String payload) :
Message(PUBLISH),
_topic(topic),
_payload(NULL), _payload_len(0),
_payload_mine(false)
{
if (payload.length() > 0) {
_payload = new uint8_t[payload.length()];
memcpy(_payload, payload.c_str(), payload.length());
_payload_len = payload.length();
_payload_mine = true;
}
}
Publish::Publish(String topic, const __FlashStringHelper* payload) :
Message(PUBLISH),
_topic(topic),
_payload_len(strlen_P((PGM_P)payload)), _payload(new uint8_t[_payload_len + 1]),
_payload_mine(true)
{
strncpy((char*)_payload, (PGM_P)payload, _payload_len);
}
Publish Publish_P(String topic, PGM_P payload, uint32_t length) {
uint8_t *p = new uint8_t[length];
memcpy_P(p, payload, length);
return Publish(topic, p, length, true);
}
Publish::Publish(uint8_t flags, uint8_t* data, uint32_t length) :
Message(PUBLISH, flags),
_payload(NULL), _payload_len(0),
_payload_mine(false)
{
uint32_t pos = 0;
_topic = read<String>(data, pos);
if (qos() > 0)
_packet_id = read<uint16_t>(data, pos);
_payload_len = length - pos;
if (_payload_len > 0) {
_payload = new uint8_t[_payload_len];
memcpy(_payload, data + pos, _payload_len);
_payload_mine = true;
}
}
Publish::Publish(String topic, payload_callback_t pcb, uint32_t length) :
Message(PUBLISH),
_topic(topic),
_payload_len(length),
_payload(NULL), _payload_mine(false)
{
_payload_callback = pcb;
}
Publish::Publish(uint8_t flags, Client& client, uint32_t remaining_length) :
Message(PUBLISH, flags),
_payload(NULL), _payload_len(remaining_length),
_payload_mine(false)
{
_stream_client = &client;
// Read the topic
_topic = read<String>(client);
_payload_len -= 2 + _topic.length();
if (qos() > 0) {
// Read the packet id
_packet_id = read<uint16_t>(client);
_payload_len -= 2;
}
// Client stream is now at the start of the payload
}
Publish::~Publish() {
if ((_payload_mine) && (_payload != NULL))
delete [] _payload;
}
Publish& Publish::set_qos(uint8_t q) {
if (q > 2)
q = 2;
_flags &= ~0x06;
if (q) {
_flags |= q << 1;
_need_packet_id = true;
}
return *this;
}
String Publish::payload_string(void) const {
String str;
str.reserve(_payload_len);
for (uint32_t i = 0; i < _payload_len; i++)
str += (char)_payload[i];
return str;
}
uint32_t Publish::variable_header_length(void) const {
return 2 + _topic.length() + (qos() ? 2 : 0);
}
void Publish::write_variable_header(uint8_t *buf, uint32_t& bufpos) const {
write(buf, bufpos, _topic);
if (qos())
write_packet_id(buf, bufpos);
}
uint32_t Publish::payload_length(void) const {
return _payload_len;
}
void Publish::write_payload(uint8_t *buf, uint32_t& bufpos) const {
if (_payload != NULL)
write_bare_payload(buf, bufpos, _payload, _payload_len);
}
message_type Publish::response_type(void) const {
switch (qos()) {
case 0:
return None;
case 1:
return PUBACK;
case 2:
return PUBREC;
}
}
// PublishAck class
PublishAck::PublishAck(uint16_t pid) :
Message(PUBACK)
{
_packet_id = pid;
}
PublishAck::PublishAck(uint8_t* data, uint32_t length) :
Message(PUBACK)
{
uint32_t pos = 0;
_packet_id = read<uint16_t>(data, pos);
}
// PublishRec class
PublishRec::PublishRec(uint16_t pid) :
Message(PUBREC)
{
_packet_id = pid;
}
PublishRec::PublishRec(uint8_t* data, uint32_t length) :
Message(PUBREC)
{
uint32_t pos = 0;
_packet_id = read<uint16_t>(data, pos);
}
uint32_t PublishRec::variable_header_length(void) const {
return 2;
}
void PublishRec::write_variable_header(uint8_t *buf, uint32_t& bufpos) const {
write_packet_id(buf, bufpos);
}
// PublishRel class
PublishRel::PublishRel(uint16_t pid) :
Message(PUBREL)
{
_packet_id = pid;
}
PublishRel::PublishRel(uint8_t* data, uint32_t length) :
Message(PUBREL)
{
uint32_t pos = 0;
_packet_id = read<uint16_t>(data, pos);
}
uint32_t PublishRel::variable_header_length(void) const {
return 2;
}
void PublishRel::write_variable_header(uint8_t *buf, uint32_t& bufpos) const {
write_packet_id(buf, bufpos);
}
// PublishComp class
PublishComp::PublishComp(uint16_t pid) :
Message(PUBREC)
{
_packet_id = pid;
}
PublishComp::PublishComp(uint8_t* data, uint32_t length) :
Message(PUBCOMP)
{
uint32_t pos = 0;
_packet_id = read<uint16_t>(data, pos);
}
uint32_t PublishComp::variable_header_length(void) const {
return 2;
}
void PublishComp::write_variable_header(uint8_t *buf, uint32_t& bufpos) const {
write_packet_id(buf, bufpos);
}
// Subscribe class
Subscribe::Subscribe() :
Message(SUBSCRIBE),
_buffer(NULL), _buflen(0)
{
_need_packet_id = true;
}
Subscribe::Subscribe(String topic, uint8_t qos) :
Message(SUBSCRIBE),
_buffer(NULL), _buflen(0)
{
_need_packet_id = true;
_buffer = new uint8_t[2 + topic.length() + 1];
write(_buffer, _buflen, topic);
_buffer[_buflen++] = qos;
}
Subscribe::~Subscribe() {
delete [] _buffer;
}
Subscribe& Subscribe::add_topic(String topic, uint8_t qos) {
_buffer = (uint8_t*)realloc(_buffer, _buflen + 2 + topic.length() + 1);
write(_buffer, _buflen, topic);
_buffer[_buflen++] = qos;
return *this;
}
uint32_t Subscribe::variable_header_length(void) const {
return 2;
}
void Subscribe::write_variable_header(uint8_t *buf, uint32_t& bufpos) const {
write_packet_id(buf, bufpos);
}
uint32_t Subscribe::payload_length(void) const {
return _buflen;
}
void Subscribe::write_payload(uint8_t *buf, uint32_t& bufpos) const {
if (_buffer != NULL)
write_bare_payload(buf, bufpos, _buffer, _buflen);
}
// SubscribeAck class
SubscribeAck::SubscribeAck(uint8_t* data, uint32_t length) :
Message(SUBACK),
_rcs(NULL)
{
uint32_t pos = 0;
_packet_id = read<uint16_t>(data, pos);
_num_rcs = length - pos;
if (_num_rcs > 0) {
_rcs = new uint8_t[_num_rcs];
for (uint32_t i = 0; i < _num_rcs; i++)
_rcs[i] = read<uint8_t>(data, pos);
}
}
SubscribeAck::SubscribeAck(Client& client, uint32_t remaining_length) :
Message(SUBACK),
_rcs(NULL),
_num_rcs(remaining_length - 2)
{
_stream_client = &client;
// Read packet id
_packet_id = read<uint16_t>(client);
// Client stream is now at the start of the list of rcs
}
SubscribeAck::~SubscribeAck() {
if (_rcs != NULL)
delete [] _rcs;
}
uint8_t SubscribeAck::next_rc(void) const {
return read<uint8_t>(*_stream_client);
}
// Unsubscribe class
Unsubscribe::Unsubscribe() :
Message(UNSUBSCRIBE),
_buffer(NULL), _buflen(0)
{
_need_packet_id = true;
}
Unsubscribe::Unsubscribe(String topic) :
Message(UNSUBSCRIBE),
_buffer(NULL), _buflen(0)
{
_need_packet_id = true;
_buffer = (uint8_t*)malloc(2 + topic.length());
write(_buffer, _buflen, topic);
}
Unsubscribe::~Unsubscribe() {
free(_buffer);
}
Unsubscribe& Unsubscribe::add_topic(String topic) {
_buffer = (uint8_t*)realloc(_buffer, _buflen + 2 + topic.length());
write(_buffer, _buflen, topic);
return *this;
}
uint32_t Unsubscribe::variable_header_length(void) const {
return 2;
}
void Unsubscribe::write_variable_header(uint8_t *buf, uint32_t& bufpos) const {
write_packet_id(buf, bufpos);
}
uint32_t Unsubscribe::payload_length(void) const {
return _buflen;
}
void Unsubscribe::write_payload(uint8_t *buf, uint32_t& bufpos) const {
if (_buffer != NULL)
write_bare_payload(buf, bufpos, _buffer, _buflen);
}
// SubscribeAck class
UnsubscribeAck::UnsubscribeAck(uint8_t* data, uint32_t length) :
Message(UNSUBACK)
{
uint32_t pos = 0;
_packet_id = read<uint16_t>(data, pos);
}
} // namespace MQTT