forked from eclipse/paho.mqtt.embedded-c
-
Notifications
You must be signed in to change notification settings - Fork 11
/
MqttClient.h
1160 lines (1061 loc) · 30.7 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
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*******************************************************************************
*
* Purpose: Simple Synchronous MQTT Client implementation.
* Based on Eclipse Paho MQTT Client (https://github.com/eclipse/paho.mqtt.embedded-c)
*
*******************************************************************************
* Copyright Oleg Kovalenko 2016.
*
* Distributed under the MIT License.
* (See accompanying file LICENSE or copy at http://opensource.org/licenses/MIT)
*******************************************************************************
*/
#ifndef MQTT_CLIENT_H_
#define MQTT_CLIENT_H_
/* External Includes */
#include <stdarg.h>
/* Internal Includes */
#include "MQTTPacket/MQTTPacket.h"
#define MQTT_LOG_SIZE_MAX 128
#ifndef MQTT_LOG_ENABLED
#define MQTT_LOG_ENABLED 0
#endif
#ifdef __AVR__
#define MQTT_PSTR(s) PSTR(s)
#define MQTT_PSTR_ENABLED 1
#else
#define MQTT_PSTR(s) s
#define MQTT_PSTR_ENABLED 0
#endif
#if MQTT_LOG_ENABLED
#define MQTT_LOG_PRINTFLN(fmt, ...) mLogger.printfln_P(MQTT_PSTR("MQTT - " fmt), ##__VA_ARGS__)
#else
#define MQTT_LOG_PRINTFLN(fmt, ...) ((void)0)
#endif
class MqttClient {
public:
class Logger {
public:
virtual ~Logger() {}
/** Logs the specified string */
virtual void println(const char*) = 0;
private:
friend class MqttClient;
void printfln_P(const char *fmt, ...) {
char buf[MQTT_LOG_SIZE_MAX];
va_list ap;
va_start(ap, fmt);
#if MQTT_PSTR_ENABLED
vsnprintf_P(buf, MQTT_LOG_SIZE_MAX, fmt, ap);
#else
vsnprintf(buf, MQTT_LOG_SIZE_MAX, fmt, ap);
#endif
va_end(ap);
println(buf);
}
};
template<class Log>
class LoggerImpl: public Logger {
public:
LoggerImpl(Log &logger): mLogger(logger) {}
void println(const char* v) {mLogger.println(v);}
protected:
Log &mLogger;
};
class System {
public:
virtual ~System() {}
/** Returns the current time in milliseconds. */
virtual unsigned long millis() const = 0;
/** Calls the background system functions.
*
* It will be called regularly while long waits.
* Some systems like ESP requires calling the `yield` regularly.
* Implement this method with all required actions.
*/
virtual void yield(void) {}
};
class Network {
public:
virtual ~Network() {}
/**
* Reads the specified number of bytes from network.
*
* @param buffer - array to store received bytes
* @param len - desired/maximum number of bytes to read
* @param timeoutMs - time to wait bytes from network, in milliseconds
* @return number of received bytes
*/
virtual int read(unsigned char* buffer, int len, unsigned long timeoutMs) = 0;
/**
* Writes the specified number of bytes to network.
*
* @param buffer - array of the source bytes
* @param len - number of bytes to write
* @param timeoutMs - maximum time of bytes processing by network, in milliseconds
* @return number of written bytes
*/
virtual int write(unsigned char* buffer, int len, unsigned long timeoutMs) = 0;
/**
* Checks whether or not the network is connected.
*
* @return true if connected
*/
virtual bool connected() {return true;}
};
template<class Net>
class NetworkImpl: public Network {
public:
NetworkImpl(Net &network, System& system): mNetwork(network), mSystem(system) {}
int read(unsigned char* buffer, int len, unsigned long timeoutMs) {
mSystem.yield();
return mNetwork.read(buffer, len, timeoutMs);
}
int write(unsigned char* buffer, int len, unsigned long timeoutMs) {
mSystem.yield();
return mNetwork.write(buffer, len, timeoutMs);
}
private:
Net &mNetwork;
System &mSystem;
};
template<class Client>
class NetworkClientImpl: public Network {
public:
NetworkClientImpl(Client &client, System& system): mClient(client), mSystem(system) {}
int read(unsigned char* buffer, int len, unsigned long timeoutMs) {
// Request time-blocked operation...if supported
mClient.setTimeout(timeoutMs);
// Read
return connected() ? mClient.read((uint8_t*)buffer, len) : -1;
}
int write(unsigned char* buffer, int len, unsigned long timeoutMs) {
// Request time-blocked operation...if supported
mClient.setTimeout(timeoutMs);
// Write
return connected() ? mClient.write((const uint8_t*)buffer, len) : -1;
}
bool connected() {
mSystem.yield();
return mClient.connected();
}
private:
Client &mClient;
System &mSystem;
};
struct Buffer {
virtual ~Buffer() {}
/** Gets pointer to array */
virtual unsigned char* get() = 0;
/** Gets the array size */
virtual int size() const = 0;
};
template<int BUFFER_SIZE = 100>
struct ArrayBuffer: public Buffer {
unsigned char* get() {return buf;}
int size() const {return BUFFER_SIZE;}
private:
unsigned char buf[BUFFER_SIZE];
};
struct Options {
/** Maximum time of the MQTT message transmission, in milliseconds.
* Also used as delay of keep-alive messages in case of reception timer
* expiration while another message has been just sent.
*/
unsigned long commandTimeoutMs = 5000;
};
enum QoS {QOS0, QOS1, QOS2};
struct Error {
typedef int type;
enum {
BUFFER_OVERFLOW = -8,
DECODING_FAILURE_REM_LENGHT = -7,
WAIT_TIMEOUT = -6,
NETWORK_FAILURE = -5,
DECODING_FAILURE = -4,
ENCODING_FAILURE = -3,
REFUSED = -2,
FAILURE = -1,
SUCCESS = 0
};
};
struct Message {
QoS qos;
bool retained;
bool dup;
unsigned short id;
void *payload;
int payloadLen;
};
struct MessageData {
MessageData(MQTTString &aTopicName, struct Message &aMessage)
: topicName(aTopicName), message(aMessage) {}
MQTTString &topicName;
Message &message;
};
typedef void (*MessageHandlerCbk)(MessageData&);
struct MessageHandler {
const char *topic = NULL;
MessageHandlerCbk cbk = NULL;
/** Checks handler association with the topic/callback. True if associated */
bool isUsed() const {
return topic != NULL;
}
/** Removes topic/callback association */
void reset() {
topic = NULL;
cbk = NULL;
}
};
class MessageHandlers {
public:
virtual ~MessageHandlers() {}
/** Gets the size of handlers array */
virtual int size() const = 0;
/** Checks empty slot availability. True if not available */
virtual bool isFull() const = 0;
/** Gets array of handlers. Used and not used */
virtual MessageHandler* get() = 0;
/** Sets/Updates handler by topic */
virtual bool set(const char* topic, MessageHandlerCbk handler) = 0;
/** Removes handler by topic */
virtual void reset(const char* topic) = 0;
/** Removes all handlers */
virtual void reset() = 0;
};
template<int SIZE = 5>
class MessageHandlersImpl: public MessageHandlers {
public:
MessageHandlersImpl() {
for (int i = 0; i < size(); ++i) {
handlers[i] = MessageHandler();
}
}
virtual ~MessageHandlersImpl() {}
int size() const {return SIZE;}
bool isFull() const {
for (int i = 0; i < size(); ++i) {
if (!handlers[i].isUsed()) {
return false;
}
}
return true;
}
MessageHandler* get() {return handlers;}
bool set(const char* topic, MessageHandlerCbk handler) {
bool res = false;
int emptyIdx = -1;
// Try to update existing handler with the same topic
for (int i = 0; i < size(); ++i) {
if (handlers[i].isUsed()) {
if (strcmp(handlers[i].topic, topic) == 0) {
// Replace
onDeAllocateTopic(handlers[i].topic, i);
const char *t = onAllocateTopic(topic, i);
if (!t) {
return false;
}
handlers[i].topic = t;
handlers[i].cbk = handler;
res = true;
break;
}
} else if (emptyIdx < 0) {
// Store empty slot index
emptyIdx = i;
}
}
// Check result and try to use empty slot if available
if (!res && emptyIdx >= 0) {
// Set to the first empty slot
const char *t = onAllocateTopic(topic, emptyIdx);
if (!t) {
return false;
}
handlers[emptyIdx].topic = t;
handlers[emptyIdx].cbk = handler;
res = true;
}
return res;
}
void reset(const char* topic) {
for (int i = 0; i < size(); ++i) {
if (handlers[i].isUsed() && strcmp(handlers[i].topic, topic) == 0) {
onDeAllocateTopic(handlers[i].topic, i);
handlers[i].reset();
break;
}
}
}
void reset() {
for (int i = 0; i < size(); ++i) {
if (handlers[i].isUsed()) {
onDeAllocateTopic(handlers[i].topic, i);
handlers[i].reset();
}
}
}
protected:
virtual const char* onAllocateTopic(const char *topic, int storageIdx) {
// Keep provided pointer
return topic;
}
virtual void onDeAllocateTopic(const char *topic, int storageIdx) {
// Nothing to do
}
private:
MessageHandler handlers[SIZE];
};
template<int SIZE, int TOPIC_SIZE>
class MessageHandlersStaticImpl: public MessageHandlersImpl<SIZE> {
private:
char topics[SIZE][TOPIC_SIZE];
const char* onAllocateTopic(const char *topic, int storageIdx) {
if (!topic) {
return NULL;
}
if (storageIdx < 0 || storageIdx >= SIZE) {
// Wrong index
return NULL;
}
if (strlen(topic) >= TOPIC_SIZE) {
// Topic is too long
return NULL;
}
// Copy topic
strncpy(topics[storageIdx], topic, TOPIC_SIZE);
topics[storageIdx][TOPIC_SIZE - 1] = '\0';
return topics[storageIdx];
}
void onDeAllocateTopic(const char *topic, int storageIdx) {
// Nothing to do
}
};
template<int SIZE>
class MessageHandlersDynamicImpl: public MessageHandlersImpl<SIZE> {
public:
~MessageHandlersDynamicImpl() {
MessageHandlersImpl<SIZE>::reset();
}
private:
const char* onAllocateTopic(const char *topic, int storageIdx) {
if (!topic) {
return NULL;
}
// Copy topic
char *res = new char[strlen(topic) + 1];
if (res) {
strcpy(res, topic);
}
return res;
}
void onDeAllocateTopic(const char *topic, int storageIdx) {
delete[] topic;
}
};
struct ConnectResult {
struct Code {
typedef unsigned char type;
enum {
SUCCESS = 0,
REFUSED_PROTO_VER,
REFUSED_ID,
REFUSED_SRV_UNV,
REFUSED_USR_PSWD,
REFUSED_AUTH
};
};
Code::type code;
bool sessionPresent;
};
/**
* Constructs the client.
*
* @param options - the client options
* @param logger - the logger used for printing logs if MQTT_LOG_ENABLED
* set to 1, see LoggerImpl
* @param system - used to access system functions like time, yield, etc...
* @param network - used to send/receive data to/from broker, see NetworkImpl
* @param sendBuffer - buffer to temporarily store the transmitted message, see ArrayBuffer
* @param recvBuffer - buffer to temporarily store the received message, see ArrayBuffer
* @param messageHandlers - storage for subscription callback functions, see MessageHandlersImpl
* @param netMinTmMs - the minimum amount of time allowed for single network operation, in milliseconds
*/
MqttClient(const Options& options, Logger& logger, System& system, Network& network,
Buffer& sendBuffer, Buffer& recvBuffer, MessageHandlers& messageHandlers,
unsigned long netMinTmMs = 10)
:
NET_MIN_TM_MS(netMinTmMs),
mOptions(options), mLogger(logger), mSystem(system), mNetwork(network),
mSendBuffer(sendBuffer), mRecvBuffer(recvBuffer), mMessageHandlers(messageHandlers),
mSession(system)
{}
/**
* Checks the connection status.
*
* @return true if the client is connected
*/
bool isConnected() {
return mSession.isConnected;
}
/**
* Connects to the broker.
*
* @param connectOptions - connect options
* @param result - connect result
* @return execution status code
*/
Error::type connect(const MQTTPacket_connectData& connectOptions, ConnectResult& result) {
Timer timer(mSystem, mOptions.commandTimeoutMs);
MQTT_LOG_PRINTFLN("Connect, clean-session: %u, ts: %lu", connectOptions.cleansession, mSystem.millis());
if (isConnected()) {
return Error::REFUSED;
}
mSession.reset();
// Make actual keep-alive timer slightly shorter to avoid expiration on broker side
mSession.keepAliveTmSec = adjustKeepaliveTm(connectOptions.keepAliveInterval);
int len = 0;
if ((len = MQTTSerialize_connect(mSendBuffer.get(), mSendBuffer.size(), &(MQTTPacket_connectData&)connectOptions)) <= 0) {
return Error::ENCODING_FAILURE;
}
Error::type rc = sendPacket(len);
if (rc != Error::SUCCESS) {
MQTT_LOG_PRINTFLN("Can't send connect, rc: %i", rc);
return rc;
}
rc = waitFor(CONNACK, timer);
if (rc == Error::SUCCESS) {
MQTT_LOG_PRINTFLN("Connect ack received");
unsigned char ackRc = 255;
unsigned char ackSessionPresent = 0;
if (MQTTDeserialize_connack(&ackSessionPresent, &ackRc, mRecvBuffer.get(), mRecvBuffer.size()) != 1) {
return Error::DECODING_FAILURE;
}
result.code = ackRc;
result.sessionPresent = (ackSessionPresent > 0);
MQTT_LOG_PRINTFLN("Connect ack, code: %u", result.code);
mSession.isConnected = (result.code == ConnectResult::Code::SUCCESS);
if (isConnected()) {
// Start keep-alive timers
mSession.lastSentTimer.set(mSession.keepAliveTmSec*1000L);
mSession.lastRecvTimer.set(mSession.keepAliveTmSec*1000L);
MQTT_LOG_PRINTFLN("Keepalive interval: %u sec", mSession.keepAliveTmSec);
// Process session-present flag
if (!result.sessionPresent) {
MQTT_LOG_PRINTFLN("Session is not present => reset subscription");
mMessageHandlers.reset();
}
}
return isConnected() ? Error::SUCCESS : Error::REFUSED;
} else {
MQTT_LOG_PRINTFLN("Connect ack is not received, rc: %i, ts: %lu", rc, mSystem.millis());
return rc;
}
}
/**
* Disconnects from the broker.
*
* @return execution status code
*/
Error::type disconnect() {
MQTT_LOG_PRINTFLN("Disconnecting, ts: %lu", mSystem.millis());
int len = MQTTSerialize_disconnect(mSendBuffer.get(), mSendBuffer.size());
if (len <= 0) {
return Error::ENCODING_FAILURE;
}
Error::type rc = sendPacket(len);
mSession.reset();
return rc;
}
/**
* Publishes message to the specific topic.
*
* @param topic - the topic name
* @param message - the message
* @return execution status code
*/
Error::type publish(const char* topic, Message& message) {
Timer timer(mSystem, mOptions.commandTimeoutMs);
MQTT_LOG_PRINTFLN("Publish, to: %s, size: %u", topic, message.payloadLen);
if (!isConnected()) {
return Error::FAILURE;
}
MQTTString topicString = MQTTString_initializer;
topicString.cstring = (char*)topic;
unsigned short id;
switch (message.qos) {
case QOS1:
case QOS2:
id = mPacketId.getNext();
break;
default:
break;
}
int len = MQTTSerialize_publish(mSendBuffer.get(), mSendBuffer.size(),
0, message.qos, message.retained, id,
topicString, (unsigned char*)message.payload, message.payloadLen
);
if (len <= 0) {
return Error::ENCODING_FAILURE;
}
Error::type rc = sendPacket(len);
if (rc == Error::SUCCESS) switch (message.qos) {
case QOS1:
{
rc = waitFor(PUBACK, timer);
if (rc == Error::SUCCESS) {
unsigned short ackId;
unsigned char ackType, ackDup;
if (MQTTDeserialize_ack(&ackType, &ackDup, &ackId, mRecvBuffer.get(), mRecvBuffer.size()) != 1) {
rc = Error::DECODING_FAILURE;
} else {
MQTT_LOG_PRINTFLN("Publish ack received");
}
}
}
break;
case QOS2:
{
// Simplified QoS 2 implementation
rc = waitFor(PUBREC, timer);
if (rc == Error::SUCCESS) {
unsigned short ackId;
unsigned char ackType, ackDup;
if (MQTTDeserialize_ack(&ackType, &ackDup, &ackId, mRecvBuffer.get(), mRecvBuffer.size()) != 1) {
rc = Error::DECODING_FAILURE;
} else {
int len = MQTTSerialize_ack(mSendBuffer.get(), mSendBuffer.size(), PUBREL, 0, ackId);
if (len > 0) {
rc = sendPacket(len);
if (rc == Error::SUCCESS) {
rc = waitFor(PUBCOMP, timer);
if (rc == Error::SUCCESS) {
if (MQTTDeserialize_ack(&ackType, &ackDup, &ackId, mRecvBuffer.get(), mRecvBuffer.size()) != 1) {
rc = Error::DECODING_FAILURE;
}
}
}
} else {
rc = Error::ENCODING_FAILURE;
}
}
}
}
break;
default:
break;
}
if (rc != Error::SUCCESS) {
mSession.reset();
}
return rc;
}
/**
* Subscribes on specific topic.
*
* @param topic - the topic pattern which can include wildcards
* @param qos - the MQTT QoS value
* @param cbk - the callback function to be invoked when a message is received for this subscription
* @return execution status code
*/
Error::type subscribe(const char* topic, enum QoS qos, MessageHandlerCbk cbk) {
Timer timer(mSystem, mOptions.commandTimeoutMs);
MQTT_LOG_PRINTFLN("Subscribe, to: %s, qos: %u", topic, qos);
if (!isConnected()) {
return Error::FAILURE;
}
// Set handler
if (!mMessageHandlers.set(topic, cbk)) {
MQTT_LOG_PRINTFLN("Can't set message handler");
return Error::FAILURE;
}
Error::type rc = Error::SUCCESS;
// Prepare message
int len = 0;
{
MQTTString mqttTopic = {(char*)topic, {0, 0}};
int mqttQos = qos;
len = MQTTSerialize_subscribe(mSendBuffer.get(), mSendBuffer.size(),
0, mPacketId.getNext(), 1, &mqttTopic, &mqttQos
);
}
if (len <= 0) {
rc = Error::ENCODING_FAILURE;
} else {
// Send message
rc = sendPacket(len);
if (rc != Error::SUCCESS) {
MQTT_LOG_PRINTFLN("Can't send subscribe, rc: %i", rc);
} else {
// Wait the response message
rc = waitFor(SUBACK, timer);
if (rc == Error::SUCCESS) {
MQTT_LOG_PRINTFLN("Subscribe ack received");
int ackCount = 0, ackQoS = -1;
unsigned short ackId;
if (MQTTDeserialize_suback(&ackId, 1, &ackCount, &ackQoS, mRecvBuffer.get(), mRecvBuffer.size()) != 1) {
rc = Error::DECODING_FAILURE;
} else {
// Process the response message
if (ackQoS == 0x80) {
rc = Error::REFUSED;
}
}
} else {
MQTT_LOG_PRINTFLN("Subscribe ack is not received, rc: %i, ts: %lu", rc, mSystem.millis());
}
}
}
// Release handler if failed
if (rc != Error::SUCCESS) {
mMessageHandlers.reset(topic);
}
return rc;
}
/**
* Unsubscribes from specific topic.
*
* @param topic - the topic name used in subscribe call before
* @return execution status code
*/
Error::type unsubscribe(const char* topic) {
Timer timer(mSystem, mOptions.commandTimeoutMs);
MQTT_LOG_PRINTFLN("Unsubscribe, from: %s", topic);
if (!isConnected()) {
return Error::FAILURE;
}
MQTTString mqttTopic = {(char*)topic, {0, 0}};
int len = MQTTSerialize_unsubscribe(mSendBuffer.get(), mSendBuffer.size(),
0, mPacketId.getNext(), 1, &mqttTopic
);
if (len <= 0) {
return Error::ENCODING_FAILURE;
}
Error::type rc = sendPacket(len);
if (rc != Error::SUCCESS) {
MQTT_LOG_PRINTFLN("Can't send unsubscribe, rc: %i", rc);
return rc;
}
rc = waitFor(UNSUBACK, timer);
if (rc == Error::SUCCESS) {
MQTT_LOG_PRINTFLN("Unsubscribe ack received");
unsigned short ackId;
if (MQTTDeserialize_unsuback(&ackId, mRecvBuffer.get(), mRecvBuffer.size()) != 1) {
rc = Error::DECODING_FAILURE;
} else {
mMessageHandlers.reset(topic);
}
} else {
MQTT_LOG_PRINTFLN("Unsubscribe ack is not received, rc: %i, ts: %lu", rc, mSystem.millis());
}
return rc;
}
/**
* Processes incoming messages and keep-alive transmission during given interval.
* Call is interrupted if connection is lost.
* Mast be called regularly.
*
* @param timeoutMs - the time to wait, in milliseconds
*/
void yield(unsigned long timeoutMs = 1000L) {
Timer timer(mSystem, timeoutMs);
if (timeoutMs >= 1000L) {
MQTT_LOG_PRINTFLN("Yield for %lu ms", timer.leftMs());
}
do {
ReadPacketResult result;
cycle(result);
} while (isConnected() && !timer.expired());
}
/**
* Calculates the time interval until the next keep-alive transmission.
* Could be used to estimate time until next call to yield is required.
*
* @return time in milliseconds
*/
unsigned long getIdleInterval() {
unsigned long res = 0;
if (isConnected() && !mSession.keepaliveSent) {
res = min(mSession.lastSentTimer.leftMs(), mSession.lastRecvTimer.leftMs());
}
return res;
}
private:
class PacketId {
public:
PacketId() {}
unsigned short getNext() {
return next = (next == MAX_PACKET_ID) ? 1 : (next + 1);
}
private:
static const unsigned short MAX_PACKET_ID = 65535;
unsigned short next = 0;
};
class Timer {
public:
Timer(const System& system): mSystem(system) {
}
Timer(const System& system, unsigned long durationMs): Timer(system) {
set(durationMs);
}
Timer(const Timer &timer, unsigned long minDurationMs, unsigned long maxDurationMs): Timer(timer.mSystem) {
unsigned long durationMs = (timer.leftMs() > maxDurationMs) ? maxDurationMs : timer.leftMs();
set(durationMs > minDurationMs ? durationMs : minDurationMs);
}
void set(unsigned long durationMs) {
mStartMs = mSystem.millis();
mDuration = durationMs;
}
void reset() {
set(mDuration);
}
bool expired() const {
return elapsedMs() >= mDuration;
}
unsigned long leftMs() const {
return expired() ? 0 : mDuration - elapsedMs();
}
unsigned long elapsedMs() const {
return mSystem.millis() - mStartMs;
}
private:
const System &mSystem;
unsigned long mStartMs = 0;
unsigned long mDuration = 0;
};
struct ReadPacketResult {
bool isPacketReceived = false;
enum msgTypes packetType;
void reset() {
isPacketReceived = false;
}
};
struct Session {
bool isConnected = false;
unsigned int keepAliveTmSec = 0;;
Timer lastSentTimer;
Timer lastRecvTimer;
bool keepaliveSent = false;
Timer keepaliveAckTimer;
Session(const System& system): lastSentTimer(system), lastRecvTimer(system),
keepaliveAckTimer(system) {}
void reset() {
isConnected = false;
keepAliveTmSec = 0;
keepaliveSent = false;
}
};
const unsigned long NET_MIN_TM_MS;
PacketId mPacketId;
Options mOptions;
Logger &mLogger;
System &mSystem;
Network &mNetwork;
Buffer &mSendBuffer;
Buffer &mRecvBuffer;
MessageHandlers &mMessageHandlers;
Session mSession;
Error::type sendPacket(int length) {
Timer timer(mSystem, NET_MIN_TM_MS);
int sent = sendBytes(mSendBuffer.get(), length, timer);
if (sent < 0) {
return Error::NETWORK_FAILURE;
} else if (sent == length) {
mSession.lastSentTimer.reset();
return Error::SUCCESS;
} else {
return Error::FAILURE;
}
}
Error::type recvPacket(ReadPacketResult& result) {
Timer timer(mSystem, NET_MIN_TM_MS);
result.reset();
Error::type rc = Error::SUCCESS;
int len = 0;
// Read the fixed header byte
if (recvBytes(mRecvBuffer.get(), 1, timer) != 1) {
return mNetwork.connected() ? Error::WAIT_TIMEOUT : Error::NETWORK_FAILURE;
}
len++;
// Read the remaining length
timer.reset();
int remLen = 0;
rc = recvRemainingLength(&remLen, timer);
if (rc != Error::SUCCESS) {
return rc;
}
// Put the original remaining length into the buffer
len += MQTTPacket_encode(mRecvBuffer.get() + len, remLen);
// Verify buffer size
if ((remLen + len) > mRecvBuffer.size()) {
return Error::BUFFER_OVERFLOW;
}
// Read the rest of the packet
timer.reset();
if (remLen > 0 && (recvBytes(mRecvBuffer.get() + len, remLen, timer) != remLen)) {
return Error::NETWORK_FAILURE;
}
// Get packet type
MQTTHeader header = {0};
header.byte = mRecvBuffer.get()[0];
result.packetType = (msgTypes)header.bits.type;
result.isPacketReceived = true;
mSession.lastRecvTimer.reset();
return Error::SUCCESS;
}
Error::type recvRemainingLength(int* result, Timer& timer) {
unsigned char c;
long multiplier = 1;
const long MAX_MULTIPLIER = 128L*128L*128L;
*result = 0;
do {
if (recvBytes(&c, 1, timer) != 1) {
return Error::NETWORK_FAILURE;
}
*result += (c & 127) * multiplier;
multiplier *= 128;
if (multiplier > MAX_MULTIPLIER) {
return Error::DECODING_FAILURE_REM_LENGHT;
}
timer.reset();
} while ((c & 128) != 0);
return Error::SUCCESS;
}
int recvBytes(unsigned char* buffer, int length, Timer& timer) {
int qty = 0;
while (qty < length) {
int r = mNetwork.read(buffer + qty, length - qty, timer.leftMs());
if (r < 0) {
return r;
} else if (r > 0) {
qty += r;
timer.reset();
} else if (timer.expired()) {
break;
}
}
return qty;
}
int sendBytes(unsigned char* buffer, int length, Timer& timer) {
int qty = 0;
while (qty < length) {
int r = mNetwork.write(buffer + qty, length - qty, timer.leftMs());
if (r < 0) {
return r;
} else if (r > 0) {
qty += r;
timer.reset();
} else if (timer.expired()) {
break;
}
}
return qty;
}
Error::type waitFor(enum msgTypes packetType, const Timer& timer) {
MQTT_LOG_PRINTFLN("Wait for message, type: %i, tm: %lu ms", packetType, timer.leftMs());
ReadPacketResult result;
Error::type rc = Error::SUCCESS;
do {
rc = cycle(result);
if (result.isPacketReceived && result.packetType == packetType) {
rc = Error::SUCCESS;
break;
}
if (timer.expired()) {
rc = Error::WAIT_TIMEOUT;
break;
}
} while (rc == Error::SUCCESS || rc == Error::WAIT_TIMEOUT);
return rc;
}
Error::type cycle(ReadPacketResult& result) {
Error::type rc = recvPacket(result);
if (rc == Error::NETWORK_FAILURE) {
mSession.reset();
}
if (result.isPacketReceived) {
rc = processPacket(result.packetType);
}
if (mSession.keepaliveSent && mSession.keepaliveAckTimer.expired()) {
MQTT_LOG_PRINTFLN("Keepalive ack failure, ts: %lu", mSystem.millis());
mSession.reset();
}
if (isConnected()) {
rc = keepalive();
}
return rc;
}
Error::type processPacket(enum msgTypes packetType) {
MQTT_LOG_PRINTFLN("Process message, type: %i", packetType);
Error::type rc = Error::SUCCESS;
switch (packetType) {
default:
case CONNACK:
case PUBACK:
case PUBCOMP:
case SUBACK:
break;
case PINGRESP:
MQTT_LOG_PRINTFLN("Keepalive ack received, ts: %lu", mSystem.millis());
mSession.keepaliveSent = false;
break;
case PUBLISH:
{
MQTTString topicName = MQTTString_initializer;
Message msg;
int msgQos;